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.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;
31 import net.sf.json.JSONException;
32 import net.sf.json.JSONObject;
35 * VNFM connection manager
39 * @version NFVO 0.5 Sep 14, 2016
41 public class ConnectMgrVnfm {
43 private static final Logger LOG = LoggerFactory.getLogger(ConnectMgrVnfm.class);
45 private String accessSession;
47 private String roaRand;
49 public String getAccessSession() {
53 public void setAccessSession(String accessSession) {
54 this.accessSession = accessSession;
57 public String getRoaRand() {
61 public void setRoaRand(String roaRand) {
62 this.roaRand = roaRand;
73 public int connect(JSONObject vnfmObj, String authModel) {
74 LOG.info("function=connect, msg=enter connect function.");
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;
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()))
86 statusCode = httpMethod.getStatusCode();
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;
98 LOG.error("connect fail, code:" + statusCode + " re:" + result);
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);
109 if(httpMethod != null) {
110 httpMethod.releaseConnection();
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);
131 new ConnectInfo(newUrl, vnfmObj.getString("userName"), vnfmObj.getString("password"), authModel);
132 HttpMethod httpMethod = null;
133 int statusCode = Constant.INTERNAL_EXCEPTION;
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()))
140 statusCode = httpMethod.getStatusCode();
142 String result = httpMethod.getResponseBodyAsString();
143 LOG.info("connect result:" + result);
144 if(statusCode == HttpStatus.SC_CREATED) {
145 Header header = httpMethod.getResponseHeader("accessSession");
146 setAccessSession(header.getValue());
147 statusCode = HttpStatus.SC_OK;
149 LOG.error("connect fail, code:" + statusCode + " re:" + result);
152 } catch(JSONException e) {
153 LOG.error("function=connect, msg=connect JSONException e={}.", e);
154 } catch(VnfmException e) {
155 LOG.error("function=connect, msg=connect VnfmException e={}.", e);
156 } catch(IOException e) {
157 LOG.error("function=connect, msg=connect IOException e={}.", e);
160 if(httpMethod != null) {
161 httpMethod.releaseConnection();
176 public int connect(JSONObject vnfmObj) {
177 LOG.info("function=connect, msg=enter connect function.");
179 ConnectInfo info = new ConnectInfo(vnfmObj.getString("url"), vnfmObj.getString("userName"),
180 vnfmObj.getString("password"), Constant.ANONYMOUS);
181 HttpMethod httpMethod = null;
182 int statusCode = Constant.INTERNAL_EXCEPTION;
185 httpMethod = new HttpRequests.Builder(info.getAuthenticateMode())
186 .setUrl(info.getUrl(), ParamConstants.CSM_AUTH_CONNECT)
187 .setParams(String.format(ParamConstants.GET_TOKENS_V2, info.getUserName(), info.getUserPwd()))
189 statusCode = httpMethod.getStatusCode();
191 String result = httpMethod.getResponseBodyAsString();
193 if(statusCode == HttpStatus.SC_CREATED) {
194 JSONObject accessObj = JSONObject.fromObject(result);
195 JSONObject tokenObj = accessObj.getJSONObject("token");
196 Header header = httpMethod.getResponseHeader("accessSession");
197 setAccessSession(header.getValue());
198 setRoaRand(tokenObj.getString("roa_rand"));
199 statusCode = HttpStatus.SC_OK;
201 LOG.error("connect fail, code:" + statusCode + " re:" + result);
204 } catch(JSONException e) {
205 LOG.error("function=connect, msg=connect JSONException e={}.", e);
206 } catch(VnfmException e) {
207 LOG.error("function=connect, msg=connect VnfmException e={}.", e);
208 } catch(IOException e) {
209 LOG.error("function=connect, msg=connect IOException e={}.", e);
212 if(httpMethod != null) {
213 httpMethod.releaseConnection();
220 private void clearCSMPwd(ConnectInfo connectInfo) {
221 if(null != connectInfo) {
222 connectInfo.setUserPwd("");