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