Refactor codes for ztevnfmdriver
[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         CloseableHttpClient httpClient = HttpClients.createDefault();
82
83         try {
84             HttpPost httpPost = new HttpPost(info.getUrl());
85         } catch (Exception e) {
86             e.printStackTrace();
87         }
88
89         return 1;
90     }
91
92     /**
93      * <br>
94      * 
95      * @param vnfmObj
96      * @param authModel
97      * @return
98      * @since VFC 1.0
99      */
100     public int connect(JSONObject vnfmObj, String authModel) {
101         LOG.info("function=connect, msg=enter connect function.");
102
103         ConnectInfo info = new ConnectInfo(vnfmObj.getString("url"), vnfmObj.getString(Constant.USERNAME),
104                 vnfmObj.getString(Constant.PASSWORD), authModel);
105         HttpMethod httpMethod = null;
106         int statusCode = Constant.INTERNAL_EXCEPTION;
107
108         try {
109             httpMethod = new HttpRequests.Builder(info.getAuthenticateMode())
110                     .setUrl(info.getUrl(), ParamConstants.CSM_AUTH_CONNECT)
111                     .setParams(String.format(ParamConstants.GET_TOKENS_V2, info.getUserName(), info.getUserPwd()))
112                     .post().execute();
113             statusCode = httpMethod.getStatusCode();
114
115             String result = httpMethod.getResponseBodyAsString();
116             LOG.info("connect result:" + result);
117             if(statusCode == HttpStatus.SC_CREATED) {
118                 JSONObject accessObj = JSONObject.fromObject(result);
119                 JSONObject tokenObj = accessObj.getJSONObject("token");
120                 Header header = httpMethod.getResponseHeader("accessSession");
121                 setAccessSession(header.getValue());
122                 setRoaRand(tokenObj.getString("roa_rand"));
123                 statusCode = HttpStatus.SC_OK;
124             } else {
125                 LOG.error(CONNECT_FAIL + statusCode + " re:" + result);
126             }
127
128         } catch(JSONException e) {
129             LOG.error(CONNECT_JSONEXCEPTION, e);
130         } catch(VnfmException e) {
131             LOG.error(CONNECT_VNFMEXCEPTION, e);
132         } catch(IOException e) {
133             LOG.error(CONNECT_IOEXCEPTION, e);
134         } finally {
135             clearCSMPwd(info);
136             if(httpMethod != null) {
137                 httpMethod.releaseConnection();
138             }
139         }
140         return statusCode;
141
142     }
143
144     /**
145      * <br>
146      * 
147      * @param vnfmObj
148      * @param authModel
149      * @return
150      * @since VFC 1.0
151      */
152     public int connectSouth(JSONObject vnfmObj, String authModel) {
153         LOG.info("function=connectSouth, msg=enter connect function.");
154         String oldUrl = vnfmObj.getString("url").trim();
155         String newUrl = oldUrl.replaceAll("30001", "30000");
156         LOG.info("function=connectSouth, url={}.", newUrl);
157         ConnectInfo info = new ConnectInfo(newUrl, vnfmObj.getString(Constant.USERNAME),
158                 vnfmObj.getString(Constant.PASSWORD), authModel);
159         HttpMethod httpMethod = null;
160         int statusCode = Constant.INTERNAL_EXCEPTION;
161
162         try {
163             httpMethod = new HttpRequests.Builder(info.getAuthenticateMode())
164                     .setUrl(info.getUrl(), ParamConstants.CSM_AUTH_CONNECT_SOUTH)
165                     .setParams(String.format(ParamConstants.GET_TOKENS_V3, info.getUserName(), info.getUserPwd(),
166                             info.getUserName()))
167                     .post().execute();
168             statusCode = httpMethod.getStatusCode();
169
170             String result = httpMethod.getResponseBodyAsString();
171             LOG.info("connect statusCode={}, result={}:", statusCode, result);
172             if(statusCode == HttpStatus.SC_CREATED) {
173                 LOG.info("function=connectSouth, header={}.", httpMethod.getResponseHeaders());
174                 Header header = httpMethod.getResponseHeader("X-Subject-Token");
175                 LOG.info("function=connectSouth, header={}.", header.getValue());
176                 setAccessSession(header.getValue());
177                 statusCode = HttpStatus.SC_OK;
178             } else {
179                 LOG.error(CONNECT_FAIL + statusCode + " re:" + result);
180             }
181
182         } catch(JSONException e) {
183             LOG.error(CONNECT_JSONEXCEPTION, e);
184         } catch(VnfmException e) {
185             LOG.error(CONNECT_VNFMEXCEPTION, e);
186         } catch(IOException e) {
187             LOG.error(CONNECT_IOEXCEPTION, e);
188         } finally {
189             clearCSMPwd(info);
190             if(httpMethod != null) {
191                 httpMethod.releaseConnection();
192             }
193         }
194         return statusCode;
195
196     }
197
198     /**
199      * Make connection
200      * <br>
201      *
202      * @param vnfmObj
203      * @return
204      * @since VFC 1.0
205      */
206     public int connect(JSONObject vnfmObj) {
207         LOG.info("function=connect, msg=enter connect function.");
208
209         ConnectInfo info = new ConnectInfo(vnfmObj.getString("url"), vnfmObj.getString(Constant.USERNAME),
210                 vnfmObj.getString(Constant.PASSWORD), Constant.ANONYMOUS);
211         HttpMethod httpMethod = null;
212         int statusCode = Constant.INTERNAL_EXCEPTION;
213
214         try {
215             httpMethod = new HttpRequests.Builder(info.getAuthenticateMode())
216                     .setUrl(info.getUrl(), ParamConstants.CSM_AUTH_CONNECT)
217                     .setParams(String.format(ParamConstants.GET_TOKENS_V2, info.getUserName(), info.getUserPwd()))
218                     .post().execute();
219             statusCode = httpMethod.getStatusCode();
220
221             String result = httpMethod.getResponseBodyAsString();
222
223             if(statusCode == HttpStatus.SC_CREATED) {
224                 JSONObject accessObj = JSONObject.fromObject(result);
225                 JSONObject tokenObj = accessObj.getJSONObject("token");
226                 Header header = httpMethod.getResponseHeader("accessSession");
227                 setAccessSession(header.getValue());
228                 setRoaRand(tokenObj.getString("roa_rand"));
229                 statusCode = HttpStatus.SC_OK;
230             } else {
231                 LOG.error(CONNECT_FAIL + statusCode + " re:" + result);
232             }
233
234         } catch(JSONException e) {
235             LOG.error(CONNECT_JSONEXCEPTION, e);
236         } catch(VnfmException e) {
237             LOG.error(CONNECT_VNFMEXCEPTION, e);
238         } catch(IOException e) {
239             LOG.error(CONNECT_IOEXCEPTION, e);
240         } finally {
241             clearCSMPwd(info);
242             if(httpMethod != null) {
243                 httpMethod.releaseConnection();
244             }
245         }
246         return statusCode;
247
248     }
249
250     private void clearCSMPwd(ConnectInfo connectInfo) {
251         if(null != connectInfo) {
252             connectInfo.setUserPwd("");
253         }
254     }
255 }