2 * Copyright (c) 2019 AT&T Intellectual Property. All rights reserved.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.akraino.validation.ui.client.nexus;
18 import java.io.IOException;
19 import java.net.HttpURLConnection;
20 import java.net.InetSocketAddress;
21 import java.net.Proxy;
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;
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;
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;
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;
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;
65 public final class NexusExecutorClient {
67 private static final EELFLoggerDelegate LOGGER = EELFLoggerDelegate.getLogger(NexusExecutorClient.class);
69 private final Client client;
70 private final String baseurl;
71 private final HostnameVerifier hostnameVerifier;
72 private final TrustManager[] trustAll;
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() {
82 public HttpURLConnection getHttpURLConnection(URL url) throws IOException {
85 System.getenv("NEXUS_PROXY").substring(0, System.getenv("NEXUS_PROXY").lastIndexOf(":"));
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();
95 // Create all-trusting host name verifier
96 hostnameVerifier = new HostnameVerifier() {
98 public boolean verify(String hostname, SSLSession session) {
102 // Create a trust manager that does not validate certificate chains
103 trustAll = new TrustManager[] {new X509TrustManager() {
105 public X509Certificate[] getAcceptedIssuers() {
106 return null; // Not relevant.
110 public void checkClientTrusted(X509Certificate[] certs, String authType) {
111 // Do nothing. Just allow them all.
115 public void checkServerTrusted(X509Certificate[] certs, String authType) {
116 // Do nothing. Just allow them all.
121 public String getBaseUrl() {
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");
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));
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);
149 return listOfwrappers;
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");
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));
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));
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);
188 return robotTestResults;
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);