2 * Copyright 2016-2017 Huawei Technologies Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.csm.connect;
19 import java.io.IOException;
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;
34 import net.sf.json.JSONException;
35 import net.sf.json.JSONObject;
38 * VNFM connection manager
42 * @version VFC 1.0 Sep 14, 2016
44 public class ConnectMgrVnfm {
46 private static final Logger LOG = LoggerFactory.getLogger(ConnectMgrVnfm.class);
48 private static final String CONNECT_FAIL = "connect fail, code:";
50 private static final String CONNECT_JSONEXCEPTION = "function=connect, msg=connect JSONException e={}.";
52 private static final String CONNECT_VNFMEXCEPTION = "function=connect, msg=connect VnfmException e={}.";
54 private static final String CONNECT_IOEXCEPTION = "function=connect, msg=connect IOException e={}.";
56 private String accessSession;
58 private String roaRand;
60 public String getAccessSession() {
64 public void setAccessSession(String accessSession) {
65 this.accessSession = accessSession;
68 public String getRoaRand() {
72 public void setRoaRand(String roaRand) {
73 this.roaRand = roaRand;
76 public int connectVnfm(JSONObject vnfmObj, String authModel) {
77 LOG.info("function=connectVnfm, msg=enter connect function.");
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());
98 public int connect(JSONObject vnfmObj, String authModel) {
99 LOG.info("function=connect, msg=enter connect function.");
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;
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()))
111 statusCode = httpMethod.getStatusCode();
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;
123 LOG.error(CONNECT_FAIL + statusCode + " re:" + result);
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);
134 if(httpMethod != null) {
135 httpMethod.releaseConnection();
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;
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(),
166 statusCode = httpMethod.getStatusCode();
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;
177 LOG.error(CONNECT_FAIL + statusCode + " re:" + result);
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);
188 if(httpMethod != null) {
189 httpMethod.releaseConnection();
204 public int connect(JSONObject vnfmObj) {
205 LOG.info("function=connect, msg=enter connect function.");
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;
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()))
217 statusCode = httpMethod.getStatusCode();
219 String result = httpMethod.getResponseBodyAsString();
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;
229 LOG.error(CONNECT_FAIL + statusCode + " re:" + result);
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);
240 if(httpMethod != null) {
241 httpMethod.releaseConnection();
248 private void clearCSMPwd(ConnectInfo connectInfo) {
249 if(null != connectInfo) {
250 connectInfo.setUserPwd("");