2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2019 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.
22 * ============LICENSE_END=========================================================
25 package org.onap.appc.design.services.util;
27 import com.att.eelf.configuration.EELFLogger;
28 import com.att.eelf.configuration.EELFManager;
29 import com.fasterxml.jackson.databind.JsonNode;
30 import com.fasterxml.jackson.databind.ObjectMapper;
31 import com.fasterxml.jackson.databind.node.ObjectNode;
32 import java.io.FileInputStream;
33 import java.io.IOException;
34 import java.io.InputStream;
36 import java.util.HashMap;
38 import java.util.Properties;
39 import javax.net.ssl.HostnameVerifier;
40 import javax.net.ssl.SSLContext;
41 import javax.ws.rs.HttpMethod;
42 import javax.ws.rs.client.Client;
43 import javax.ws.rs.client.ClientBuilder;
44 import javax.ws.rs.client.Entity;
45 import javax.ws.rs.client.WebTarget;
46 import javax.ws.rs.core.Response;
47 import javax.ws.rs.core.Feature;
48 import javax.ws.rs.core.MediaType;
49 import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
51 public class ArtifactHandlerClient {
53 private static final EELFLogger log = EELFManager.getInstance().getLogger(ArtifactHandlerClient.class);
54 public static final String SDNC_CONFIG_DIR_VAR = "SDNC_CONFIG_DIR";
55 private Properties props = new Properties();
57 public ArtifactHandlerClient() throws IOException {
58 String propDir = DesignServiceConstants.getEnvironmentVariable(SDNC_CONFIG_DIR_VAR);
59 if (propDir == null) {
60 throw new IOException(" Cannot find Property file -" + SDNC_CONFIG_DIR_VAR);
62 String propFile = propDir + "/" + DesignServiceConstants.DESIGN_SERVICE_PROPERTIES;
63 InputStream propStream = new FileInputStream(propFile);
65 props.load(propStream);
66 } catch (Exception e) {
67 throw new IOException("Could not load properties file " + propFile, e);
71 } catch (Exception e) {
72 log.warn("Could not close FileInputStream", e);
77 public String createArtifactData(String payload, String requestID) throws IOException {
79 ObjectMapper objectMapper = new ObjectMapper();
80 JsonNode payloadObject = objectMapper.readTree(payload);
82 ObjectNode json = objectMapper.createObjectNode();
83 String artifactName = payloadObject.get(DesignServiceConstants.ARTIFACT_NAME).textValue();
84 String artifactVersion = payloadObject.get(DesignServiceConstants.ARTIFACT_VERSOIN).textValue();
85 String artifactContents = payloadObject.get(DesignServiceConstants.ARTIFACT_CONTENTS).textValue();
87 ObjectNode requestInfo = objectMapper.createObjectNode();
88 requestInfo.put(DesignServiceConstants.REQUETS_ID, requestID);
89 requestInfo.put(DesignServiceConstants.REQUEST_ACTION, "StoreSdcDocumentRequest");
90 requestInfo.put(DesignServiceConstants.SOURCE, DesignServiceConstants.DESIGN_TOOL);
92 ObjectNode docParams = objectMapper.createObjectNode();
93 docParams.put(DesignServiceConstants.ARTIFACT_VERSOIN, artifactVersion);
94 docParams.put(DesignServiceConstants.ARTIFACT_NAME, artifactName);
95 docParams.put(DesignServiceConstants.ARTIFACT_CONTENTS, artifactContents);
97 json.put(DesignServiceConstants.REQUEST_INFORMATION, requestInfo);
98 json.put(DesignServiceConstants.DOCUMENT_PARAMETERS, docParams);
99 log.info("Final data =" + json.toString());
100 return String.format("{\"input\": %s}", json.toString());
103 public Map<String, String> execute(String payload, String rpc) throws IOException {
104 log.info("Configuring Rest Operation for Payload " + payload + " RPC : " + rpc);
105 Map<String, String> outputMessage = new HashMap<>();
106 Client client = null;
107 WebTarget webResource;
108 Response clientResponse = null;
109 EncryptionTool et = EncryptionTool.getInstance();
110 String responseDataType = MediaType.APPLICATION_JSON;
111 String requestDataType = MediaType.APPLICATION_JSON;
115 System.setProperty("jsse.enableSNIExtension", "false");
116 SSLContext sslContext;
117 SecureRestClientTrustManager secureRestClientTrustManager = new SecureRestClientTrustManager();
118 sslContext = SSLContext.getInstance("SSL");
119 sslContext.init(null, new javax.net.ssl.TrustManager[]{secureRestClientTrustManager}, null);
121 client = ClientBuilder.newBuilder().sslContext(sslContext).hostnameVerifier(getHostnameVerifier()).build();
123 String password = et.decrypt(props.getProperty("appc.upload.pass"));
124 client.register(HttpAuthenticationFeature.basic(props.getProperty("appc.upload.user"), password));
125 webResource = client.target(new URI(props.getProperty("appc.upload.provider.url")));
126 webResource.property("Content-Type", "application/json;charset=UTF-8");
127 log.info("Starting Rest Operation.....");
128 if (HttpMethod.GET.equalsIgnoreCase(rpc)) {
129 clientResponse = webResource.request(responseDataType).get(Response.class);
130 } else if (HttpMethod.POST.equalsIgnoreCase(rpc)) {
131 clientResponse = webResource.request(requestDataType).post(Entity.json(payload), Response.class);
132 } else if (HttpMethod.PUT.equalsIgnoreCase(rpc)) {
133 clientResponse = webResource.request(requestDataType).put(Entity.json(payload), Response.class);
134 } else if (HttpMethod.DELETE.equalsIgnoreCase(rpc)) {
135 clientResponse = webResource.request().delete(Response.class);
137 validateClientResponse(clientResponse);
138 log.info("Completed Rest Operation.....");
140 } catch (Exception e) {
141 log.debug("failed in RESTCONT Action", e);
142 throw new IOException("Error While Sending Rest Request: " + e.getMessage(), e);
145 if (client != null) {
149 return outputMessage;
152 private void validateClientResponse(Response clientResponse) throws ArtifactHandlerInternalException {
153 if (clientResponse == null) {
154 throw new ArtifactHandlerInternalException("Failed to create client response");
156 if (clientResponse.getStatus() != 200) {
157 throw new ArtifactHandlerInternalException("HTTP error code : " + clientResponse.getStatus());
161 private HostnameVerifier getHostnameVerifier() {
162 return (hostname, sslSession) -> true;