optical-service package added for service create
[sdnc/northbound.git] / optical-service / provider / src / main / java / org / onap / sdnc / northbound / asyncrequests / SdncOdlConnection.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : SDN-C
4  * ================================================================================
5  * Copyright (C) 2019-2020 Fujitsu Limited 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 package org.onap.sdnc.northbound.asyncrequests;
22
23 import java.io.BufferedReader;
24 import java.io.DataOutputStream;
25 import java.io.IOException;
26 import java.io.InputStreamReader;
27 import java.net.Authenticator;
28 import java.net.HttpURLConnection;
29 import java.net.PasswordAuthentication;
30 import java.net.URL;
31 import javax.net.ssl.HostnameVerifier;
32 import javax.net.ssl.HttpsURLConnection;
33 import javax.net.ssl.SSLSession;
34 import org.apache.commons.codec.binary.Base64;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38
39 public class SdncOdlConnection {
40
41     private static final Logger LOG = LoggerFactory
42         .getLogger(SdncOdlConnection.class);
43
44     private HttpURLConnection httpConn = null;
45
46     private String url = null;
47     private String user = null;
48     private String password = null;
49
50     private class SdncAuthenticator extends Authenticator {
51
52         private String user;
53         private String passwd;
54
55         SdncAuthenticator(String user, String passwd) {
56             this.user = user;
57             this.passwd = passwd;
58         }
59
60         @Override
61         protected PasswordAuthentication getPasswordAuthentication() {
62             return new PasswordAuthentication(user, passwd.toCharArray());
63         }
64     }
65
66     private SdncOdlConnection() {
67
68     }
69
70     private SdncOdlConnection(String url, String user, String password) {
71         this.url = url;
72         this.user = user;
73         this.password = password;
74
75         try {
76             URL sdncUrl = new URL(url);
77             Authenticator.setDefault(new SdncAuthenticator(user, password));
78
79             this.httpConn = (HttpURLConnection) sdncUrl.openConnection();
80         } catch (Exception e) {
81             LOG.error("Unable to create http connection", e);
82         }
83     }
84
85     public static SdncOdlConnection newInstance(String url, String user, String password) {
86         return new SdncOdlConnection(url, user, password);
87     }
88
89
90     public String send(String method, String contentType, String msg) throws IOException {
91
92         LOG.info(String.format("Sending REST %s to %s", method, url));
93         LOG.info(String.format("Message body:%n%s", msg));
94         String authStr = user + ":" + password;
95         String encodedAuthStr = new String(Base64.encodeBase64(authStr.getBytes()));
96
97         httpConn.addRequestProperty("Authentication", "Basic " + encodedAuthStr);
98
99         httpConn.setRequestMethod(method);
100         httpConn.setRequestProperty("Content-Type", contentType);
101         httpConn.setRequestProperty("Accept", contentType);
102
103         httpConn.setDoInput(true);
104         httpConn.setDoOutput(true);
105         httpConn.setUseCaches(false);
106
107         if (httpConn instanceof HttpsURLConnection) {
108             HostnameVerifier hostnameVerifier = new HostnameVerifier() {
109                 @Override
110                 public boolean verify(String hostname, SSLSession session) {
111                     return true;
112                 }
113             };
114             ((HttpsURLConnection) httpConn).setHostnameVerifier(hostnameVerifier);
115         }
116
117         // Write message
118         httpConn.setRequestProperty("Content-Length", Integer.toString(msg.length()));
119         DataOutputStream outStr = new DataOutputStream(httpConn.getOutputStream());
120         outStr.write(msg.getBytes());
121         outStr.close();
122
123         // Read response
124         BufferedReader respRdr;
125
126         LOG.info("Response: " + httpConn.getResponseCode() + " " + httpConn.getResponseMessage());
127
128         if (httpConn.getResponseCode() < 300) {
129
130             respRdr = new BufferedReader(new InputStreamReader(httpConn.getInputStream()));
131         } else {
132             respRdr = new BufferedReader(new InputStreamReader(httpConn.getErrorStream()));
133         }
134
135         StringBuilder respBuff = new StringBuilder();
136
137         String respLn;
138
139         while ((respLn = respRdr.readLine()) != null) {
140             respBuff.append(respLn).append("\n");
141         }
142         respRdr.close();
143
144         String respString = respBuff.toString();
145
146         LOG.info(String.format("Response body :%n%s", respString));
147
148         return respString;
149     }
150 }