2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2017 Amdocs
8 * ================================================================================
9 * Modifications Copyright (C) 2019 Ericsson
10 * =============================================================================
11 * Licensed under the Apache License, Version 2.0 (the "License");
12 * you may not use this file except in compliance with the License.
13 * You may obtain a copy of the License at
15 * http://www.apache.org/licenses/LICENSE-2.0
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS,
19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
23 * ============LICENSE_END=========================================================
26 package org.onap.appc.design.services.util;
28 import com.att.eelf.configuration.EELFLogger;
29 import com.att.eelf.configuration.EELFManager;
30 import com.fasterxml.jackson.databind.JsonNode;
31 import com.fasterxml.jackson.databind.ObjectMapper;
32 import com.fasterxml.jackson.databind.node.ObjectNode;
34 import javax.ws.rs.client.Client;
35 import javax.ws.rs.client.ClientBuilder;
36 import javax.ws.rs.client.Entity;
37 import javax.ws.rs.client.WebTarget;
38 import javax.ws.rs.core.Response;
39 import javax.ws.rs.core.Feature;
40 import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
42 import java.io.FileInputStream;
43 import java.io.IOException;
44 import java.io.InputStream;
46 import java.util.HashMap;
48 import java.util.Properties;
49 import javax.net.ssl.HostnameVerifier;
50 import javax.net.ssl.SSLContext;
51 import javax.ws.rs.HttpMethod;
52 import javax.ws.rs.core.MediaType;
55 public class ArtifactHandlerClient {
57 private static final EELFLogger log = EELFManager.getInstance().getLogger(ArtifactHandlerClient.class);
58 static final String SDNC_CONFIG_DIR_VAR = "SDNC_CONFIG_DIR";
59 private Properties props = new Properties();
61 public ArtifactHandlerClient() throws IOException {
62 String propDir = DesignServiceConstants.getEnvironmentVariable(SDNC_CONFIG_DIR_VAR);
63 if (propDir == null) {
64 throw new IOException(" Cannot find Property file -" + SDNC_CONFIG_DIR_VAR);
66 String propFile = propDir + "/" + DesignServiceConstants.DESIGN_SERVICE_PROPERTIES;
67 InputStream propStream = new FileInputStream(propFile);
69 props.load(propStream);
70 } catch (Exception e) {
71 throw new IOException("Could not load properties file " + propFile, e);
75 } catch (Exception e) {
76 log.warn("Could not close FileInputStream", e);
81 public String createArtifactData(String payload, String requestID) throws IOException {
83 ObjectMapper objectMapper = new ObjectMapper();
84 JsonNode payloadObject = objectMapper.readTree(payload);
86 ObjectNode json = objectMapper.createObjectNode();
87 String artifactName = payloadObject.get(DesignServiceConstants.ARTIFACT_NAME).textValue();
88 String artifactVersion = payloadObject.get(DesignServiceConstants.ARTIFACT_VERSOIN).textValue();
89 String artifactContents = payloadObject.get(DesignServiceConstants.ARTIFACT_CONTENTS).textValue();
91 ObjectNode requestInfo = objectMapper.createObjectNode();
92 requestInfo.put(DesignServiceConstants.REQUETS_ID, requestID);
93 requestInfo.put(DesignServiceConstants.REQUEST_ACTION, "StoreSdcDocumentRequest");
94 requestInfo.put(DesignServiceConstants.SOURCE, DesignServiceConstants.DESIGN_TOOL);
96 ObjectNode docParams = objectMapper.createObjectNode();
97 docParams.put(DesignServiceConstants.ARTIFACT_VERSOIN, artifactVersion);
98 docParams.put(DesignServiceConstants.ARTIFACT_NAME, artifactName);
99 docParams.put(DesignServiceConstants.ARTIFACT_CONTENTS, artifactContents);
101 json.put(DesignServiceConstants.REQUEST_INFORMATION, requestInfo);
102 json.put(DesignServiceConstants.DOCUMENT_PARAMETERS, docParams);
103 log.info("Final data =" + json.toString());
104 return String.format("{\"input\": %s}", json.toString());
107 public Map<String, String> execute(String payload, String rpc) throws IOException {
108 log.info("Configuring Rest Operation for Payload " + payload + " RPC : " + rpc);
109 Map<String, String> outputMessage = new HashMap<>();
110 Client client = null;
111 WebTarget webResource;
112 Response clientResponse = null;
113 EncryptionTool et = EncryptionTool.getInstance();
114 String responseDataType = MediaType.APPLICATION_JSON;
115 String requestDataType = MediaType.APPLICATION_JSON;
119 System.setProperty("jsse.enableSNIExtension", "false");
120 SSLContext sslContext;
121 SecureRestClientTrustManager secureRestClientTrustManager = new SecureRestClientTrustManager();
122 sslContext = SSLContext.getInstance("SSL");
123 sslContext.init(null, new javax.net.ssl.TrustManager[]{secureRestClientTrustManager}, null);
126 client = ClientBuilder.newBuilder().sslContext(sslContext).hostnameVerifier(getHostnameVerifier()).build();
128 String password = et.decrypt(props.getProperty("appc.upload.pass"));
129 client.register(HttpAuthenticationFeature.basic(props.getProperty("appc.upload.user"), password));
130 webResource = client.target(new URI(props.getProperty("appc.upload.provider.url")));
131 webResource.property("Content-Type", "application/json;charset=UTF-8");
132 log.info("Starting Rest Operation.....");
133 if (HttpMethod.GET.equalsIgnoreCase(rpc)) {
134 clientResponse = webResource.request(responseDataType).get(Response.class);
135 } else if (HttpMethod.POST.equalsIgnoreCase(rpc)) {
136 clientResponse = webResource.request(requestDataType).post(Entity.json(payload),Response.class);
137 } else if (HttpMethod.PUT.equalsIgnoreCase(rpc)) {
138 clientResponse = webResource.request(requestDataType).put(Entity.json(payload),Response.class);
139 } else if (HttpMethod.DELETE.equalsIgnoreCase(rpc)) {
140 clientResponse = webResource.request().delete(Response.class);
142 validateClientResponse(clientResponse);
143 log.info("Completed Rest Operation.....");
145 } catch (Exception e) {
146 log.debug("failed in RESTCONT Action", e);
147 throw new IOException("Error While Sending Rest Request" + e.getMessage(), e);
150 if (client != null) {
154 return outputMessage;
157 private void validateClientResponse(Response clientResponse) throws ArtifactHandlerInternalException {
158 if (clientResponse == null) {
159 throw new ArtifactHandlerInternalException("Failed to create client response");
161 if (clientResponse.getStatus() != 200) {
162 throw new ArtifactHandlerInternalException("HTTP error code : " + clientResponse.getStatus());
166 private HostnameVerifier getHostnameVerifier() {
167 return (hostname, sslSession) -> true;