Release version 1.1.0 of sli/northbound
[ccsdk/sli/northbound.git] / dmaap-listener / src / main / java / org / onap / ccsdk / sli / northbound / dmaapclient / SdncOdlConnection.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : SDN-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                      reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.ccsdk.sli.northbound.dmaapclient;
23
24 import java.io.BufferedReader;
25 import java.io.DataOutputStream;
26 import java.io.IOException;
27 import java.io.InputStreamReader;
28 import java.net.Authenticator;
29 import java.net.HttpURLConnection;
30 import java.net.PasswordAuthentication;
31 import java.net.URL;
32 import javax.net.ssl.HostnameVerifier;
33 import javax.net.ssl.HttpsURLConnection;
34 import javax.net.ssl.SSLSession;
35 import org.apache.commons.codec.binary.Base64;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39
40 public class SdncOdlConnection {
41
42     private static final Logger LOG = LoggerFactory
43         .getLogger(SdncOdlConnection.class);
44
45     private HttpURLConnection httpConn = null;
46
47     private String url = null;
48     private String user = null;
49     private String password = null;
50
51     private class SdncAuthenticator extends Authenticator {
52
53         private String user;
54         private String passwd;
55
56         SdncAuthenticator(String user, String passwd) {
57             this.user = user;
58             this.passwd = passwd;
59         }
60
61         @Override
62         protected PasswordAuthentication getPasswordAuthentication() {
63             return new PasswordAuthentication(user, passwd.toCharArray());
64         }
65     }
66
67     private SdncOdlConnection() {
68
69     }
70
71     private SdncOdlConnection(String url, String user, String password) {
72         this.url = url;
73         this.user = user;
74         this.password = password;
75
76         try {
77             URL sdncUrl = new URL(url);
78             Authenticator.setDefault(new SdncAuthenticator(user, password));
79
80             this.httpConn = (HttpURLConnection) sdncUrl.openConnection();
81         } catch (Exception e) {
82             LOG.error("Unable to create http connection", e);
83         }
84     }
85
86     public static SdncOdlConnection newInstance(String url, String user, String password) {
87         return new SdncOdlConnection(url, user, password);
88     }
89
90
91     public String send(String method, String contentType, String msg) throws IOException {
92
93         LOG.info(String.format("Sending REST %s to %s", method, url));
94         LOG.info(String.format("Message body:%n%s", msg));
95         String authStr = user + ":" + password;
96         String encodedAuthStr = new String(Base64.encodeBase64(authStr.getBytes()));
97
98         httpConn.addRequestProperty("Authentication", "Basic " + encodedAuthStr);
99
100         httpConn.setRequestMethod(method);
101         httpConn.setRequestProperty("Content-Type", contentType);
102         httpConn.setRequestProperty("Accept", contentType);
103
104         httpConn.setDoInput(true);
105         httpConn.setDoOutput(true);
106         httpConn.setUseCaches(false);
107
108         if (httpConn instanceof HttpsURLConnection) {
109             HostnameVerifier hostnameVerifier = new HostnameVerifier() {
110                 @Override
111                 public boolean verify(String hostname, SSLSession session) {
112                     return true;
113                 }
114             };
115             ((HttpsURLConnection) httpConn).setHostnameVerifier(hostnameVerifier);
116         }
117
118         // Write message
119         httpConn.setRequestProperty("Content-Length", Integer.toString(msg.length()));
120         DataOutputStream outStr = new DataOutputStream(httpConn.getOutputStream());
121         outStr.write(msg.getBytes());
122         outStr.close();
123
124         // Read response
125         BufferedReader respRdr;
126
127         LOG.info("Response: " + httpConn.getResponseCode() + " " + httpConn.getResponseMessage());
128
129         if (httpConn.getResponseCode() < 300) {
130
131             respRdr = new BufferedReader(new InputStreamReader(httpConn.getInputStream()));
132         } else {
133             respRdr = new BufferedReader(new InputStreamReader(httpConn.getErrorStream()));
134         }
135
136         StringBuilder respBuff = new StringBuilder();
137
138         String respLn;
139
140         while ((respLn = respRdr.readLine()) != null) {
141             respBuff.append(respLn).append("\n");
142         }
143         respRdr.close();
144
145         String respString = respBuff.toString();
146
147         LOG.info(String.format("Response body :%n%s", respString));
148
149         return respString;
150     }
151 }