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 VFC 1.0 Sep 14, 2016
41 public class ConnectMgrVnfm {
43 private static final Logger LOG = LoggerFactory.getLogger(ConnectMgrVnfm.class);
45 private static final String CONNECT_FAIL = "connect fail, code:";
47 private static final String CONNECT_JSONEXCEPTION = "function=connect, msg=connect JSONException e={}.";
49 private static final String CONNECT_VNFMEXCEPTION = "function=connect, msg=connect VnfmException e={}.";
51 private static final String CONNECT_IOEXCEPTION = "function=connect, msg=connect IOException e={}.";
53 private String accessSession;
55 private String roaRand;
57 public String getAccessSession() {
61 public void setAccessSession(String accessSession) {
62 this.accessSession = accessSession;
65 public String getRoaRand() {
69 public void setRoaRand(String roaRand) {
70 this.roaRand = roaRand;
81 public int connect(JSONObject vnfmObj, String authModel) {
82 LOG.info("function=connect, msg=enter connect function.");
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;
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()))
94 statusCode = httpMethod.getStatusCode();
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;
106 LOG.error(CONNECT_FAIL + statusCode + " re:" + result);
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);
117 if(httpMethod != null) {
118 httpMethod.releaseConnection();
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;
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(),
149 statusCode = httpMethod.getStatusCode();
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;
160 LOG.error(CONNECT_FAIL + statusCode + " re:" + result);
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);
171 if(httpMethod != null) {
172 httpMethod.releaseConnection();
187 public int connect(JSONObject vnfmObj) {
188 LOG.info("function=connect, msg=enter connect function.");
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;
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()))
200 statusCode = httpMethod.getStatusCode();
202 String result = httpMethod.getResponseBodyAsString();
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;
212 LOG.error(CONNECT_FAIL + statusCode + " re:" + result);
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);
223 if(httpMethod != null) {
224 httpMethod.releaseConnection();
231 private void clearCSMPwd(ConnectInfo connectInfo) {
232 if(null != connectInfo) {
233 connectInfo.setUserPwd("");