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.openecomp.appc.instar.dme2client;
26 import java.io.FileInputStream;
27 import java.io.InputStream;
29 import java.nio.charset.Charset;
30 import java.util.HashMap;
31 import java.util.Properties;
32 import javax.net.ssl.HostnameVerifier;
33 import javax.net.ssl.SSLContext;
34 import javax.ws.rs.HttpMethod;
35 import javax.ws.rs.core.MediaType;
37 import org.apache.commons.io.IOUtils;
38 import org.openecomp.appc.instar.utils.InstarClientConstant;
41 import com.att.eelf.configuration.EELFLogger;
42 import com.att.eelf.configuration.EELFManager;
43 import com.sun.jersey.api.client.Client;
44 import com.sun.jersey.api.client.ClientResponse;
45 import com.sun.jersey.api.client.WebResource;
46 import com.sun.jersey.api.client.config.DefaultClientConfig;
47 import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter;
50 public class Dme2Client {
52 private static final EELFLogger log = EELFManager.getInstance().getLogger(Dme2Client.class);
53 private static final String SDNC_CONFIG_DIR_VAR = "SDNC_CONFIG_DIR";
54 //DME2Client client = null;
55 Properties props = new Properties();
56 String operationName ;
59 public Dme2Client(String optName, String subCtxt, HashMap<String, String> data) throws Exception{
60 log.info("Setting Properties for DME2 Client for INSTAR connection");
61 this.operationName=optName;
62 this.appendContext = data.get(subCtxt);
63 String propDir = System.getenv(SDNC_CONFIG_DIR_VAR);
65 throw new Exception(" Cannot find Property file -" + SDNC_CONFIG_DIR_VAR);
66 String propFile = propDir + InstarClientConstant.OUTBOUND_PROPERTIES;
67 InputStream propStream = new FileInputStream(propFile);
70 props.load(propStream);
74 throw new Exception("Could not load properties file " + propFile, e);
84 log.warn("Could not close FileInputStream", e);
89 public ClientResponse sendtoInstar() throws Exception {
91 log.info("Called Send with operation Name=" + this.operationName + "and = " + props.getProperty(operationName+InstarClientConstant.BASE_URL));
92 String resourceUri = props.getProperty(operationName+InstarClientConstant.BASE_URL)+
93 props.getProperty(operationName + InstarClientConstant.URL_SUFFIX) +
94 props.getProperty(operationName + InstarClientConstant.SUB_CONTEXT)+ appendContext ;
96 log.info("DME Endpoint URI:" + resourceUri);
98 WebResource webResource = null;
99 ClientResponse clientResponse = null;
100 String authorization = props.getProperty("authorization");
101 String requestDataType = "application/json";
102 String responseDataType= MediaType.APPLICATION_JSON;
103 String methodType = props.getProperty("getIpAddressByVnf_method");
105 String userId=props.getProperty("MechID");
106 String password=props.getProperty("MechPass");
108 log.info("authorization = " + authorization + "methodType= " + methodType);
110 DefaultClientConfig defaultClientConfig = new DefaultClientConfig();
111 System.setProperty("jsse.enableSNIExtension", "false");
112 SSLContext sslContext = null;
113 SecureRestClientTrustManager secureRestClientTrustManager = new SecureRestClientTrustManager();
114 sslContext = SSLContext.getInstance("SSL");
115 sslContext.init(null, new javax.net.ssl.TrustManager[] { secureRestClientTrustManager }, null);
116 defaultClientConfig.getProperties().put(
117 com.sun.jersey.client.urlconnection.HTTPSProperties.PROPERTY_HTTPS_PROPERTIES,
118 new com.sun.jersey.client.urlconnection.HTTPSProperties(getHostnameVerifier(), sslContext));
119 client = Client.create(defaultClientConfig);
120 client.addFilter(new HTTPBasicAuthFilter(userId, password));
122 webResource = client.resource(new URI(resourceUri));
123 webResource.setProperty("Content-Type", "application/json;charset=UTF-8");
125 if(HttpMethod.GET.equalsIgnoreCase(methodType)){
126 clientResponse = webResource.accept(responseDataType).get(ClientResponse.class);
127 }else if(HttpMethod.POST.equalsIgnoreCase(methodType)){
128 clientResponse = webResource.type(requestDataType).post(ClientResponse.class, request);
129 }else if(HttpMethod.PUT.equalsIgnoreCase(methodType)){
130 clientResponse = webResource.type(requestDataType).put(ClientResponse.class,request);
131 }else if(HttpMethod.DELETE.equalsIgnoreCase(methodType)){
132 clientResponse = webResource.delete(ClientResponse.class);
135 return clientResponse;
137 }catch (Exception e) {
138 log.info("failed in RESTCONT Action ("+methodType+") for the resource " + resourceUri + ", falut message :"+e.getMessage());
139 throw new Exception("Error While gettting Data from INSTAR" + e.getMessage());
153 public String send() {
154 String response = null;
158 props.getProperty(InstarClientConstant.MOCK_INSTAR) != null &&
159 props.getProperty(InstarClientConstant.MOCK_INSTAR).equalsIgnoreCase("true"))
160 return IOUtils.toString(Dme2Client.class.getClassLoader().getResourceAsStream("/tmp/sampleResponse"), Charset.defaultCharset());
162 ClientResponse clientResponse = sendtoInstar();
163 if(clientResponse != null){
164 response = clientResponse.getEntity(String.class);
165 log.info(clientResponse.getStatus() + " Status, Response :" + response);
168 } catch (Exception t) {
174 private HostnameVerifier getHostnameVerifier() {
175 return new HostnameVerifier() {
177 public boolean verify(String hostname, javax.net.ssl.SSLSession sslSession) {