Second part of onap rename
[appc.git] / appc-outbound / appc-network-inventory-client / provider / src / main / java / org / openecomp / appc / instar / dme2client / Dme2Client.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
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
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
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.
20  * 
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.appc.instar.dme2client;
26 import java.io.FileInputStream;
27 import java.io.InputStream;
28 import java.net.URI;
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;
36
37 import org.apache.commons.io.IOUtils;
38 import org.onap.appc.instar.utils.InstarClientConstant;
39
40
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;
48
49
50 public class Dme2Client {
51
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 ;
57         String appendContext; 
58
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);
64                 if (propDir == null)
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);
68                 try
69                 {
70                         props.load(propStream);
71                 }
72                 catch (Exception e)
73                 {
74                         throw new Exception("Could not load properties file " + propFile, e);
75                 }
76                 finally
77                 {
78                         try
79                         {
80                                 propStream.close();
81                         }
82                         catch (Exception e)
83                         {
84                                 log.warn("Could not close FileInputStream", e);
85                         }
86                 }
87         }
88
89         public  ClientResponse  sendtoInstar() throws Exception {
90
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 ;           
95
96                 log.info("DME Endpoint URI:" + resourceUri);     
97                 Client client = null;
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");
104                 String request = "";
105                 String userId=props.getProperty("MechID");
106                 String password=props.getProperty("MechPass");
107                 
108                 log.info("authorization = " + authorization + "methodType= " + methodType);
109                 try{
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));
121
122                         webResource = client.resource(new URI(resourceUri));
123                         webResource.setProperty("Content-Type", "application/json;charset=UTF-8");
124
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);
133                         }
134
135                         return clientResponse;
136
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());
140                 }
141                 finally {
142                         // clean up.
143                         webResource = null;
144                         if(client != null){
145                                 client.destroy();
146                                 client = null;
147                         }
148                 }
149
150
151         }
152
153         public String send() {
154                 String response = null;
155                 try{
156
157                         if(props !=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());
161
162                         ClientResponse clientResponse = sendtoInstar();
163                         if(clientResponse != null){
164                                 response = clientResponse.getEntity(String.class);
165                                 log.info(clientResponse.getStatus() + " Status, Response :" + response);
166
167                         }
168                 } catch (Exception t) {
169                         log.error("Error in Dme2Client for send() method while sending response:" ,t);
170                 }
171                 return response;
172         }
173
174         private HostnameVerifier getHostnameVerifier() {
175                 return new HostnameVerifier() {
176                         @Override
177                         public boolean verify(String hostname, javax.net.ssl.SSLSession sslSession) {
178                                 return true;
179                         }
180                 };
181         }
182
183
184 }