Merge "[RECV-94] Separate docker/robot invoking"
[validation.git] / ui / src / main / java / org / akraino / validation / ui / client / nexus / NexusExecutorClient.java
1 /*
2  * Copyright (c) 2019 AT&T Intellectual Property. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may
5  * not use this file except in compliance with the License. You may obtain
6  * a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
13  * implied. See the License for the specific language governing
14  * permissions and limitations under the License.
15  */
16 package org.akraino.validation.ui.client.nexus;
17
18 import java.io.IOException;
19 import java.net.HttpURLConnection;
20 import java.net.InetSocketAddress;
21 import java.net.Proxy;
22 import java.net.URL;
23 import java.security.KeyManagementException;
24 import java.security.NoSuchAlgorithmException;
25 import java.security.cert.X509Certificate;
26 import java.util.ArrayList;
27 import java.util.List;
28 import java.util.Map;
29
30 import javax.net.ssl.HostnameVerifier;
31 import javax.net.ssl.HttpsURLConnection;
32 import javax.net.ssl.SSLContext;
33 import javax.net.ssl.SSLSession;
34 import javax.net.ssl.TrustManager;
35 import javax.net.ssl.X509TrustManager;
36
37 import org.akraino.validation.ui.client.nexus.resources.RobotTestResult;
38 import org.akraino.validation.ui.client.nexus.resources.WrapperRobotTestResult;
39 import org.akraino.validation.ui.data.BlueprintLayer;
40 import org.apache.commons.httpclient.HttpException;
41 import org.json.JSONObject;
42 import org.json.XML;
43 import org.jsoup.Jsoup;
44 import org.jsoup.nodes.Document;
45 import org.jsoup.nodes.Element;
46 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
47
48 import com.fasterxml.jackson.annotation.JsonInclude.Include;
49 import com.fasterxml.jackson.core.JsonParseException;
50 import com.fasterxml.jackson.databind.DeserializationFeature;
51 import com.fasterxml.jackson.databind.JsonMappingException;
52 import com.fasterxml.jackson.databind.ObjectMapper;
53 import com.sun.jersey.api.client.Client;
54 import com.sun.jersey.api.client.ClientHandlerException;
55 import com.sun.jersey.api.client.ClientResponse;
56 import com.sun.jersey.api.client.UniformInterfaceException;
57 import com.sun.jersey.api.client.WebResource;
58 import com.sun.jersey.api.client.config.ClientConfig;
59 import com.sun.jersey.api.client.config.DefaultClientConfig;
60 import com.sun.jersey.api.json.JSONConfiguration;
61 import com.sun.jersey.client.urlconnection.HTTPSProperties;
62 import com.sun.jersey.client.urlconnection.HttpURLConnectionFactory;
63 import com.sun.jersey.client.urlconnection.URLConnectionClientHandler;
64
65 public final class NexusExecutorClient {
66
67     private static final EELFLoggerDelegate LOGGER = EELFLoggerDelegate.getLogger(NexusExecutorClient.class);
68
69     private final Client client;
70     private final String baseurl;
71     private final HostnameVerifier hostnameVerifier;
72     private final TrustManager[] trustAll;
73
74     public NexusExecutorClient(String newBaseurl) {
75         this.baseurl = newBaseurl;
76         ClientConfig clientConfig = new DefaultClientConfig();
77         clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
78         this.client = new Client(new URLConnectionClientHandler(new HttpURLConnectionFactory() {
79             Proxy proxy = null;
80
81             @Override
82             public HttpURLConnection getHttpURLConnection(URL url) throws IOException {
83                 try {
84                     String proxyIp =
85                             System.getenv("NEXUS_PROXY").substring(0, System.getenv("NEXUS_PROXY").lastIndexOf(":"));
86                     String proxyPort =
87                             System.getenv("NEXUS_PROXY").substring(System.getenv("NEXUS_PROXY").lastIndexOf(":") + 1);
88                     proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyIp, Integer.parseInt(proxyPort)));
89                     return (HttpURLConnection) url.openConnection(proxy);
90                 } catch (Exception ex) {
91                     return (HttpURLConnection) url.openConnection();
92                 }
93             }
94         }), clientConfig);
95         // Create all-trusting host name verifier
96         hostnameVerifier = new HostnameVerifier() {
97             @Override
98             public boolean verify(String hostname, SSLSession session) {
99                 return true;
100             }
101         };
102         // Create a trust manager that does not validate certificate chains
103         trustAll = new TrustManager[] {new X509TrustManager() {
104             @Override
105             public X509Certificate[] getAcceptedIssuers() {
106                 return null; // Not relevant.
107             }
108
109             @Override
110             public void checkClientTrusted(X509Certificate[] certs, String authType) {
111                 // Do nothing. Just allow them all.
112             }
113
114             @Override
115             public void checkServerTrusted(X509Certificate[] certs, String authType) {
116                 // Do nothing. Just allow them all.
117             }
118         }};
119     }
120
121     public String getBaseUrl() {
122         return this.baseurl;
123     }
124
125     public List<WrapperRobotTestResult> getRobotTestResults() throws ClientHandlerException, UniformInterfaceException,
126             JsonParseException, JsonMappingException, IOException, KeyManagementException, NoSuchAlgorithmException {
127         List<WrapperRobotTestResult> listOfwrappers = new ArrayList<WrapperRobotTestResult>();
128         LOGGER.info(EELFLoggerDelegate.applicationLogger, "Trying to get the blueprint layers");
129         setProperties();
130         WebResource webResource = this.client.resource(this.baseurl + "/");
131         LOGGER.debug(EELFLoggerDelegate.debugLogger, "Request URI of get: " + webResource.getURI().toString());
132         ClientResponse response = webResource.get(ClientResponse.class);
133         if (response.getStatus() != 200) {
134             throw new HttpException("Could not retrieve blueprint layers from Nexus. HTTP error code : "
135                     + response.getStatus() + " and message: " + response.getEntity(String.class));
136         }
137         Document document = Jsoup.parse(response.getEntity(String.class));
138         List<Element> elements =
139                 document.getElementsByTag("body").get(0).getElementsByTag("table").get(0).getElementsByTag("tr");
140         for (int i = 2; i < elements.size(); i++) {
141             String layer = elements.get(i).getElementsByTag("td").get(0).getElementsByTag("a").get(0).text();
142             layer = layer.substring(0, layer.length() - 1);
143             List<RobotTestResult> robotTestResults = getResultsOfALayer(this.baseurl + "/" + layer);
144             WrapperRobotTestResult wrapper = new WrapperRobotTestResult();
145             wrapper.setBlueprintLayer(BlueprintLayer.valueOf(layer.substring(0, 1).toUpperCase() + layer.substring(1)));
146             wrapper.setRobotTestResults(robotTestResults);
147             listOfwrappers.add(wrapper);
148         }
149         return listOfwrappers;
150     }
151
152     private List<RobotTestResult> getResultsOfALayer(String resultsUrl)
153             throws ClientHandlerException, UniformInterfaceException, JsonParseException, JsonMappingException,
154             IOException, KeyManagementException, NoSuchAlgorithmException {
155         List<RobotTestResult> robotTestResults = new ArrayList<RobotTestResult>();
156         LOGGER.info(EELFLoggerDelegate.applicationLogger, "Trying to get Robot Test Results");
157         setProperties();
158         WebResource webResource = this.client.resource(resultsUrl + "/");
159         LOGGER.debug(EELFLoggerDelegate.debugLogger, "Request URI of get: " + webResource.getURI().toString());
160         ClientResponse response = webResource.get(ClientResponse.class);
161         if (response.getStatus() != 200) {
162             throw new HttpException("Could not retrieve robot test results from Nexus. HTTP error code : "
163                     + response.getStatus() + " and message: " + response.getEntity(String.class));
164         }
165         Document document = Jsoup.parse(response.getEntity(String.class));
166         List<Element> elements = document.getElementsByTag("body").get(0).getElementsByTag("table").get(0)
167                 .getElementsByTag("tbody").get(0).getElementsByTag("tr");
168         for (int i = 2; i < elements.size(); i++) {
169             String testSuiteName = elements.get(i).getElementsByTag("td").get(0).getElementsByTag("a").get(0).text();
170             testSuiteName = testSuiteName.substring(0, testSuiteName.length() - 1);
171             webResource = this.client.resource(resultsUrl + "/" + testSuiteName + "/output.xml");
172             LOGGER.debug(EELFLoggerDelegate.debugLogger, "Request URI of get: " + webResource.getURI().toString());
173             response = webResource.get(ClientResponse.class);
174             if (response.getStatus() != 200) {
175                 throw new HttpException("Could not retrieve robot test result from Nexus. HTTP error code : "
176                         + response.getStatus() + " and message: " + response.getEntity(String.class));
177             }
178             String result = response.getEntity(String.class);
179             JSONObject xmlJSONObj = XML.toJSONObject(result);
180             ObjectMapper mapper = new ObjectMapper();
181             mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
182             mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
183             mapper.setSerializationInclusion(Include.NON_NULL);
184             RobotTestResult robotTestResult = mapper.readValue(xmlJSONObj.toString(), RobotTestResult.class);
185             robotTestResult.setName(testSuiteName);
186             robotTestResults.add(robotTestResult);
187         }
188         return robotTestResults;
189     }
190
191     private void setProperties() throws NoSuchAlgorithmException, KeyManagementException {
192         SSLContext sslContext = SSLContext.getInstance("SSL");
193         sslContext.init(null, this.trustAll, new java.security.SecureRandom());
194         HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
195         // Install the all-trusting host verifier
196         HttpsURLConnection.setDefaultHostnameVerifier(this.hostnameVerifier);
197         DefaultClientConfig config = new DefaultClientConfig();
198         Map<String, Object> properties = config.getProperties();
199         HTTPSProperties httpsProperties = new HTTPSProperties((str, sslSession) -> true, sslContext);
200         properties.put(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES, httpsProperties);
201     }
202
203 }