Sonar fix try-with-resources
[vfc/nfvo/driver/vnfm/svnfm.git] / huawei / vnfmadapter / VnfmadapterService / service / src / main / java / org / onap / vfc / nfvo / vnfm / svnfm / vnfmadapter / service / csm / connect / ConnectMgrVnfm.java
1 /*
2  * Copyright 2016-2017 Huawei Technologies Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.csm.connect;
18
19 import java.io.IOException;
20
21 import org.apache.commons.httpclient.Header;
22 import org.apache.commons.httpclient.HttpMethod;
23 import org.apache.commons.httpclient.HttpStatus;
24 import org.apache.http.client.methods.HttpPost;
25 import org.apache.http.impl.client.CloseableHttpClient;
26 import org.apache.http.impl.client.HttpClients;
27 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.common.VnfmException;
28 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.constant.Constant;
29 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.constant.ParamConstants;
30 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.csm.api.ConnectInfo;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 import net.sf.json.JSONException;
35 import net.sf.json.JSONObject;
36
37 /**
38  * VNFM connection manager
39  * .</br>
40  *
41  * @author
42  * @version VFC 1.0 Sep 14, 2016
43  */
44 public class ConnectMgrVnfm {
45
46     private static final Logger LOG = LoggerFactory.getLogger(ConnectMgrVnfm.class);
47
48     private static final String CONNECT_FAIL = "connect fail, code:";
49
50     private static final String CONNECT_JSONEXCEPTION = "function=connect, msg=connect JSONException e={}.";
51
52     private static final String CONNECT_VNFMEXCEPTION = "function=connect, msg=connect VnfmException e={}.";
53
54     private static final String CONNECT_IOEXCEPTION = "function=connect, msg=connect IOException e={}.";
55
56     private String accessSession;
57
58     private String roaRand;
59
60     public String getAccessSession() {
61         return accessSession;
62     }
63
64     public void setAccessSession(String accessSession) {
65         this.accessSession = accessSession;
66     }
67
68     public String getRoaRand() {
69         return roaRand;
70     }
71
72     public void setRoaRand(String roaRand) {
73         this.roaRand = roaRand;
74     }
75
76     public int connectVnfm(JSONObject vnfmObj, String authModel) {
77         LOG.info("function=connectVnfm, msg=enter connect function.");
78
79         ConnectInfo info = new ConnectInfo(vnfmObj.getString("url"), vnfmObj.getString(Constant.USERNAME),
80                 vnfmObj.getString(Constant.PASSWORD), authModel);
81         try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
82             HttpPost httpPost = new HttpPost(info.getUrl());
83         } catch (Exception e) {
84             LOG.error("Exception while creating connection: {}", e.getMessage());
85         }
86
87         return 1;
88     }
89
90     /**
91      * <br>
92      * 
93      * @param vnfmObj
94      * @param authModel
95      * @return
96      * @since VFC 1.0
97      */
98     public int connect(JSONObject vnfmObj, String authModel) {
99         LOG.info("function=connect, msg=enter connect function.");
100
101         ConnectInfo info = new ConnectInfo(vnfmObj.getString("url"), vnfmObj.getString(Constant.USERNAME),
102                 vnfmObj.getString(Constant.PASSWORD), authModel);
103         HttpMethod httpMethod = null;
104         int statusCode = Constant.INTERNAL_EXCEPTION;
105
106         try {
107             httpMethod = new HttpRequests.Builder(info.getAuthenticateMode())
108                     .setUrl(info.getUrl(), ParamConstants.CSM_AUTH_CONNECT)
109                     .setParams(String.format(ParamConstants.GET_TOKENS_V2, info.getUserName(), info.getUserPwd()))
110                     .post().execute();
111             statusCode = httpMethod.getStatusCode();
112
113             String result = httpMethod.getResponseBodyAsString();
114             LOG.info("connect result:" + result);
115             if(statusCode == HttpStatus.SC_CREATED) {
116                 JSONObject accessObj = JSONObject.fromObject(result);
117                 JSONObject tokenObj = accessObj.getJSONObject("token");
118                 Header header = httpMethod.getResponseHeader("accessSession");
119                 setAccessSession(header.getValue());
120                 setRoaRand(tokenObj.getString("roa_rand"));
121                 statusCode = HttpStatus.SC_OK;
122             } else {
123                 LOG.error(CONNECT_FAIL + statusCode + " re:" + result);
124             }
125
126         } catch(JSONException e) {
127             LOG.error(CONNECT_JSONEXCEPTION, e);
128         } catch(VnfmException e) {
129             LOG.error(CONNECT_VNFMEXCEPTION, e);
130         } catch(IOException e) {
131             LOG.error(CONNECT_IOEXCEPTION, e);
132         } finally {
133             clearCSMPwd(info);
134             if(httpMethod != null) {
135                 httpMethod.releaseConnection();
136             }
137         }
138         return statusCode;
139
140     }
141
142     /**
143      * <br>
144      * 
145      * @param vnfmObj
146      * @param authModel
147      * @return
148      * @since VFC 1.0
149      */
150     public int connectSouth(JSONObject vnfmObj, String authModel) {
151         LOG.info("function=connectSouth, msg=enter connect function.");
152         String oldUrl = vnfmObj.getString("url").trim();
153         String newUrl = oldUrl.replaceAll("30001", "30000");
154         LOG.info("function=connectSouth, url={}.", newUrl);
155         ConnectInfo info = new ConnectInfo(newUrl, vnfmObj.getString(Constant.USERNAME),
156                 vnfmObj.getString(Constant.PASSWORD), authModel);
157         HttpMethod httpMethod = null;
158         int statusCode = Constant.INTERNAL_EXCEPTION;
159
160         try {
161             httpMethod = new HttpRequests.Builder(info.getAuthenticateMode())
162                     .setUrl(info.getUrl(), ParamConstants.CSM_AUTH_CONNECT_SOUTH)
163                     .setParams(String.format(ParamConstants.GET_TOKENS_V3, info.getUserName(), info.getUserPwd(),
164                             info.getUserName()))
165                     .post().execute();
166             statusCode = httpMethod.getStatusCode();
167
168             String result = httpMethod.getResponseBodyAsString();
169             LOG.info("connect statusCode={}, result={}:", statusCode, result);
170             if(statusCode == HttpStatus.SC_CREATED) {
171                 LOG.info("function=connectSouth, header={}.", httpMethod.getResponseHeaders());
172                 Header header = httpMethod.getResponseHeader("X-Subject-Token");
173                 LOG.info("function=connectSouth, header={}.", header.getValue());
174                 setAccessSession(header.getValue());
175                 statusCode = HttpStatus.SC_OK;
176             } else {
177                 LOG.error(CONNECT_FAIL + statusCode + " re:" + result);
178             }
179
180         } catch(JSONException e) {
181             LOG.error(CONNECT_JSONEXCEPTION, e);
182         } catch(VnfmException e) {
183             LOG.error(CONNECT_VNFMEXCEPTION, e);
184         } catch(IOException e) {
185             LOG.error(CONNECT_IOEXCEPTION, e);
186         } finally {
187             clearCSMPwd(info);
188             if(httpMethod != null) {
189                 httpMethod.releaseConnection();
190             }
191         }
192         return statusCode;
193
194     }
195
196     /**
197      * Make connection
198      * <br>
199      *
200      * @param vnfmObj
201      * @return
202      * @since VFC 1.0
203      */
204     public int connect(JSONObject vnfmObj) {
205         LOG.info("function=connect, msg=enter connect function.");
206
207         ConnectInfo info = new ConnectInfo(vnfmObj.getString("url"), vnfmObj.getString(Constant.USERNAME),
208                 vnfmObj.getString(Constant.PASSWORD), Constant.ANONYMOUS);
209         HttpMethod httpMethod = null;
210         int statusCode = Constant.INTERNAL_EXCEPTION;
211
212         try {
213             httpMethod = new HttpRequests.Builder(info.getAuthenticateMode())
214                     .setUrl(info.getUrl(), ParamConstants.CSM_AUTH_CONNECT)
215                     .setParams(String.format(ParamConstants.GET_TOKENS_V2, info.getUserName(), info.getUserPwd()))
216                     .post().execute();
217             statusCode = httpMethod.getStatusCode();
218
219             String result = httpMethod.getResponseBodyAsString();
220
221             if(statusCode == HttpStatus.SC_CREATED) {
222                 JSONObject accessObj = JSONObject.fromObject(result);
223                 JSONObject tokenObj = accessObj.getJSONObject("token");
224                 Header header = httpMethod.getResponseHeader("accessSession");
225                 setAccessSession(header.getValue());
226                 setRoaRand(tokenObj.getString("roa_rand"));
227                 statusCode = HttpStatus.SC_OK;
228             } else {
229                 LOG.error(CONNECT_FAIL + statusCode + " re:" + result);
230             }
231
232         } catch(JSONException e) {
233             LOG.error(CONNECT_JSONEXCEPTION, e);
234         } catch(VnfmException e) {
235             LOG.error(CONNECT_VNFMEXCEPTION, e);
236         } catch(IOException e) {
237             LOG.error(CONNECT_IOEXCEPTION, e);
238         } finally {
239             clearCSMPwd(info);
240             if(httpMethod != null) {
241                 httpMethod.releaseConnection();
242             }
243         }
244         return statusCode;
245
246     }
247
248     private void clearCSMPwd(ConnectInfo connectInfo) {
249         if(null != connectInfo) {
250             connectInfo.setUserPwd("");
251         }
252     }
253 }