2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2017 Amdocs
8 * =============================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
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 com.sun.jersey.api.client.Client;
33 import com.sun.jersey.api.client.ClientResponse;
34 import com.sun.jersey.api.client.WebResource;
35 import com.sun.jersey.api.client.config.DefaultClientConfig;
36 import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter;
37 import com.sun.jersey.client.urlconnection.HTTPSProperties;
38 import java.io.FileInputStream;
39 import java.io.IOException;
40 import java.io.InputStream;
42 import java.util.HashMap;
44 import java.util.Properties;
45 import javax.net.ssl.HostnameVerifier;
46 import javax.net.ssl.SSLContext;
47 import javax.ws.rs.HttpMethod;
48 import javax.ws.rs.core.MediaType;
51 public class ArtifactHandlerClient {
53 private static final EELFLogger log = EELFManager.getInstance().getLogger(ArtifactHandlerClient.class);
54 private static final String SDNC_CONFIG_DIR_VAR = "SDNC_CONFIG_DIR";
55 private Properties props = new Properties();
57 public ArtifactHandlerClient() throws IOException {
58 String propDir = System.getenv(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 WebResource webResource;
108 ClientResponse clientResponse = null;
109 EncryptionTool et = EncryptionTool.getInstance();
110 String responseDataType = MediaType.APPLICATION_JSON;
111 String requestDataType = MediaType.APPLICATION_JSON;
114 DefaultClientConfig defaultClientConfig = new DefaultClientConfig();
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);
123 .put(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES, new HTTPSProperties(getHostnameVerifier(), sslContext));
125 client = Client.create(defaultClientConfig);
126 String password = et.decrypt(props.getProperty("appc.upload.pass"));
127 client.addFilter(new HTTPBasicAuthFilter(props.getProperty("appc.upload.user"), password));
128 webResource = client.resource(new URI(props.getProperty("appc.upload.provider.url")));
129 webResource.setProperty("Content-Type", "application/json;charset=UTF-8");
130 log.info("Starting Rest Operation.....");
131 if (HttpMethod.GET.equalsIgnoreCase(rpc)) {
132 clientResponse = webResource.accept(responseDataType).get(ClientResponse.class);
133 } else if (HttpMethod.POST.equalsIgnoreCase(rpc)) {
134 clientResponse = webResource.type(requestDataType).post(ClientResponse.class, payload);
135 } else if (HttpMethod.PUT.equalsIgnoreCase(rpc)) {
136 clientResponse = webResource.type(requestDataType).put(ClientResponse.class, payload);
137 } else if (HttpMethod.DELETE.equalsIgnoreCase(rpc)) {
138 clientResponse = webResource.delete(ClientResponse.class);
140 validateClientResponse(clientResponse);
141 log.info("Completed Rest Operation.....");
143 } catch (Exception e) {
144 log.debug("failed in RESTCONT Action", e);
145 throw new IOException("Error While Sending Rest Request" + e.getMessage());
148 if (client != null) {
152 return outputMessage;
155 private void validateClientResponse(ClientResponse clientResponse) throws ArtifactHandlerInternalException {
156 if (clientResponse == null) {
157 throw new ArtifactHandlerInternalException("Failed to create client response");
159 if (clientResponse.getStatus() != 200) {
160 throw new ArtifactHandlerInternalException("HTTP error code : " + clientResponse.getStatus());
164 private HostnameVerifier getHostnameVerifier() {
165 return (hostname, sslSession) -> true;