c86e65d2c849f1c10c1a6e28510ffc695f6fa31a
[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.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.common.VnfmException;
25 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.constant.Constant;
26 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.constant.ParamConstants;
27 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.csm.api.ConnectInfo;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 import net.sf.json.JSONException;
32 import net.sf.json.JSONObject;
33
34 /**
35  * VNFM connection manager
36  * .</br>
37  *
38  * @author
39  * @version VFC 1.0 Sep 14, 2016
40  */
41 public class ConnectMgrVnfm {
42
43     private static final Logger LOG = LoggerFactory.getLogger(ConnectMgrVnfm.class);
44
45     private static final String CONNECT_FAIL = "connect fail, code:";
46
47     private static final String CONNECT_JSONEXCEPTION = "function=connect, msg=connect JSONException e={}.";
48
49     private static final String CONNECT_VNFMEXCEPTION = "function=connect, msg=connect VnfmException e={}.";
50
51     private static final String CONNECT_IOEXCEPTION = "function=connect, msg=connect IOException e={}.";
52
53     private String accessSession;
54
55     private String roaRand;
56
57     public String getAccessSession() {
58         return accessSession;
59     }
60
61     public void setAccessSession(String accessSession) {
62         this.accessSession = accessSession;
63     }
64
65     public String getRoaRand() {
66         return roaRand;
67     }
68
69     public void setRoaRand(String roaRand) {
70         this.roaRand = roaRand;
71     }
72
73     /**
74      * <br>
75      * 
76      * @param vnfmObj
77      * @param authModel
78      * @return
79      * @since VFC 1.0
80      */
81     public int connect(JSONObject vnfmObj, String authModel) {
82         LOG.info("function=connect, msg=enter connect function.");
83
84         ConnectInfo info = new ConnectInfo(vnfmObj.getString("url"), vnfmObj.getString(Constant.USERNAME),
85                 vnfmObj.getString(Constant.PASSWORD), authModel);
86         HttpMethod httpMethod = null;
87         int statusCode = Constant.INTERNAL_EXCEPTION;
88
89         try {
90             httpMethod = new HttpRequests.Builder(info.getAuthenticateMode())
91                     .setUrl(info.getUrl(), ParamConstants.CSM_AUTH_CONNECT)
92                     .setParams(String.format(ParamConstants.GET_TOKENS_V2, info.getUserName(), info.getUserPwd()))
93                     .post().execute();
94             statusCode = httpMethod.getStatusCode();
95
96             String result = httpMethod.getResponseBodyAsString();
97             LOG.info("connect result:" + result);
98             if(statusCode == HttpStatus.SC_CREATED) {
99                 JSONObject accessObj = JSONObject.fromObject(result);
100                 JSONObject tokenObj = accessObj.getJSONObject("token");
101                 Header header = httpMethod.getResponseHeader("accessSession");
102                 setAccessSession(header.getValue());
103                 setRoaRand(tokenObj.getString("roa_rand"));
104                 statusCode = HttpStatus.SC_OK;
105             } else {
106                 LOG.error(CONNECT_FAIL + statusCode + " re:" + result);
107             }
108
109         } catch(JSONException e) {
110             LOG.error(CONNECT_JSONEXCEPTION, e);
111         } catch(VnfmException e) {
112             LOG.error(CONNECT_VNFMEXCEPTION, e);
113         } catch(IOException e) {
114             LOG.error(CONNECT_IOEXCEPTION, e);
115         } finally {
116             clearCSMPwd(info);
117             if(httpMethod != null) {
118                 httpMethod.releaseConnection();
119             }
120         }
121         return statusCode;
122
123     }
124
125     /**
126      * <br>
127      * 
128      * @param vnfmObj
129      * @param authModel
130      * @return
131      * @since VFC 1.0
132      */
133     public int connectSouth(JSONObject vnfmObj, String authModel) {
134         LOG.info("function=connectSouth, msg=enter connect function.");
135         String oldUrl = vnfmObj.getString("url").trim();
136         String newUrl = oldUrl.replaceAll("30001", "30000");
137         LOG.info("function=connectSouth, url={}.", newUrl);
138         ConnectInfo info = new ConnectInfo(newUrl, vnfmObj.getString(Constant.USERNAME),
139                 vnfmObj.getString(Constant.PASSWORD), authModel);
140         HttpMethod httpMethod = null;
141         int statusCode = Constant.INTERNAL_EXCEPTION;
142
143         try {
144             httpMethod = new HttpRequests.Builder(info.getAuthenticateMode())
145                     .setUrl(info.getUrl(), ParamConstants.CSM_AUTH_CONNECT_SOUTH)
146                     .setParams(String.format(ParamConstants.GET_TOKENS_V3, info.getUserName(), info.getUserPwd(),
147                             info.getUserName()))
148                     .post().execute();
149             statusCode = httpMethod.getStatusCode();
150
151             String result = httpMethod.getResponseBodyAsString();
152             LOG.info("connect statusCode={}, result={}:", statusCode, result);
153             if(statusCode == HttpStatus.SC_CREATED) {
154                 LOG.info("function=connectSouth, header={}.", httpMethod.getResponseHeaders());
155                 Header header = httpMethod.getResponseHeader("X-Subject-Token");
156                 LOG.info("function=connectSouth, header={}.", header.getValue());
157                 setAccessSession(header.getValue());
158                 statusCode = HttpStatus.SC_OK;
159             } else {
160                 LOG.error(CONNECT_FAIL + statusCode + " re:" + result);
161             }
162
163         } catch(JSONException e) {
164             LOG.error(CONNECT_JSONEXCEPTION, e);
165         } catch(VnfmException e) {
166             LOG.error(CONNECT_VNFMEXCEPTION, e);
167         } catch(IOException e) {
168             LOG.error(CONNECT_IOEXCEPTION, e);
169         } finally {
170             clearCSMPwd(info);
171             if(httpMethod != null) {
172                 httpMethod.releaseConnection();
173             }
174         }
175         return statusCode;
176
177     }
178
179     /**
180      * Make connection
181      * <br>
182      *
183      * @param vnfmObj
184      * @return
185      * @since VFC 1.0
186      */
187     public int connect(JSONObject vnfmObj) {
188         LOG.info("function=connect, msg=enter connect function.");
189
190         ConnectInfo info = new ConnectInfo(vnfmObj.getString("url"), vnfmObj.getString(Constant.USERNAME),
191                 vnfmObj.getString(Constant.PASSWORD), Constant.ANONYMOUS);
192         HttpMethod httpMethod = null;
193         int statusCode = Constant.INTERNAL_EXCEPTION;
194
195         try {
196             httpMethod = new HttpRequests.Builder(info.getAuthenticateMode())
197                     .setUrl(info.getUrl(), ParamConstants.CSM_AUTH_CONNECT)
198                     .setParams(String.format(ParamConstants.GET_TOKENS_V2, info.getUserName(), info.getUserPwd()))
199                     .post().execute();
200             statusCode = httpMethod.getStatusCode();
201
202             String result = httpMethod.getResponseBodyAsString();
203
204             if(statusCode == HttpStatus.SC_CREATED) {
205                 JSONObject accessObj = JSONObject.fromObject(result);
206                 JSONObject tokenObj = accessObj.getJSONObject("token");
207                 Header header = httpMethod.getResponseHeader("accessSession");
208                 setAccessSession(header.getValue());
209                 setRoaRand(tokenObj.getString("roa_rand"));
210                 statusCode = HttpStatus.SC_OK;
211             } else {
212                 LOG.error(CONNECT_FAIL + statusCode + " re:" + result);
213             }
214
215         } catch(JSONException e) {
216             LOG.error(CONNECT_JSONEXCEPTION, e);
217         } catch(VnfmException e) {
218             LOG.error(CONNECT_VNFMEXCEPTION, e);
219         } catch(IOException e) {
220             LOG.error(CONNECT_IOEXCEPTION, e);
221         } finally {
222             clearCSMPwd(info);
223             if(httpMethod != null) {
224                 httpMethod.releaseConnection();
225             }
226         }
227         return statusCode;
228
229     }
230
231     private void clearCSMPwd(ConnectInfo connectInfo) {
232         if(null != connectInfo) {
233             connectInfo.setUserPwd("");
234         }
235     }
236 }