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         CloseableHttpClient httpClient = HttpClients.createDefault();
 
  84             HttpPost httpPost = new HttpPost(info.getUrl());
 
  85         } catch (Exception e) {
 
 100     public int connect(JSONObject vnfmObj, String authModel) {
 
 101         LOG.info("function=connect, msg=enter connect function.");
 
 103         ConnectInfo info = new ConnectInfo(vnfmObj.getString("url"), vnfmObj.getString(Constant.USERNAME),
 
 104                 vnfmObj.getString(Constant.PASSWORD), authModel);
 
 105         HttpMethod httpMethod = null;
 
 106         int statusCode = Constant.INTERNAL_EXCEPTION;
 
 109             httpMethod = new HttpRequests.Builder(info.getAuthenticateMode())
 
 110                     .setUrl(info.getUrl(), ParamConstants.CSM_AUTH_CONNECT)
 
 111                     .setParams(String.format(ParamConstants.GET_TOKENS_V2, info.getUserName(), info.getUserPwd()))
 
 113             statusCode = httpMethod.getStatusCode();
 
 115             String result = httpMethod.getResponseBodyAsString();
 
 116             LOG.info("connect result:" + result);
 
 117             if(statusCode == HttpStatus.SC_CREATED) {
 
 118                 JSONObject accessObj = JSONObject.fromObject(result);
 
 119                 JSONObject tokenObj = accessObj.getJSONObject("token");
 
 120                 Header header = httpMethod.getResponseHeader("accessSession");
 
 121                 setAccessSession(header.getValue());
 
 122                 setRoaRand(tokenObj.getString("roa_rand"));
 
 123                 statusCode = HttpStatus.SC_OK;
 
 125                 LOG.error(CONNECT_FAIL + statusCode + " re:" + result);
 
 128         } catch(JSONException e) {
 
 129             LOG.error(CONNECT_JSONEXCEPTION, e);
 
 130         } catch(VnfmException e) {
 
 131             LOG.error(CONNECT_VNFMEXCEPTION, e);
 
 132         } catch(IOException e) {
 
 133             LOG.error(CONNECT_IOEXCEPTION, e);
 
 136             if(httpMethod != null) {
 
 137                 httpMethod.releaseConnection();
 
 152     public int connectSouth(JSONObject vnfmObj, String authModel) {
 
 153         LOG.info("function=connectSouth, msg=enter connect function.");
 
 154         String oldUrl = vnfmObj.getString("url").trim();
 
 155         String newUrl = oldUrl.replaceAll("30001", "30000");
 
 156         LOG.info("function=connectSouth, url={}.", newUrl);
 
 157         ConnectInfo info = new ConnectInfo(newUrl, vnfmObj.getString(Constant.USERNAME),
 
 158                 vnfmObj.getString(Constant.PASSWORD), authModel);
 
 159         HttpMethod httpMethod = null;
 
 160         int statusCode = Constant.INTERNAL_EXCEPTION;
 
 163             httpMethod = new HttpRequests.Builder(info.getAuthenticateMode())
 
 164                     .setUrl(info.getUrl(), ParamConstants.CSM_AUTH_CONNECT_SOUTH)
 
 165                     .setParams(String.format(ParamConstants.GET_TOKENS_V3, info.getUserName(), info.getUserPwd(),
 
 168             statusCode = httpMethod.getStatusCode();
 
 170             String result = httpMethod.getResponseBodyAsString();
 
 171             LOG.info("connect statusCode={}, result={}:", statusCode, result);
 
 172             if(statusCode == HttpStatus.SC_CREATED) {
 
 173                 LOG.info("function=connectSouth, header={}.", httpMethod.getResponseHeaders());
 
 174                 Header header = httpMethod.getResponseHeader("X-Subject-Token");
 
 175                 LOG.info("function=connectSouth, header={}.", header.getValue());
 
 176                 setAccessSession(header.getValue());
 
 177                 statusCode = HttpStatus.SC_OK;
 
 179                 LOG.error(CONNECT_FAIL + statusCode + " re:" + result);
 
 182         } catch(JSONException e) {
 
 183             LOG.error(CONNECT_JSONEXCEPTION, e);
 
 184         } catch(VnfmException e) {
 
 185             LOG.error(CONNECT_VNFMEXCEPTION, e);
 
 186         } catch(IOException e) {
 
 187             LOG.error(CONNECT_IOEXCEPTION, e);
 
 190             if(httpMethod != null) {
 
 191                 httpMethod.releaseConnection();
 
 206     public int connect(JSONObject vnfmObj) {
 
 207         LOG.info("function=connect, msg=enter connect function.");
 
 209         ConnectInfo info = new ConnectInfo(vnfmObj.getString("url"), vnfmObj.getString(Constant.USERNAME),
 
 210                 vnfmObj.getString(Constant.PASSWORD), Constant.ANONYMOUS);
 
 211         HttpMethod httpMethod = null;
 
 212         int statusCode = Constant.INTERNAL_EXCEPTION;
 
 215             httpMethod = new HttpRequests.Builder(info.getAuthenticateMode())
 
 216                     .setUrl(info.getUrl(), ParamConstants.CSM_AUTH_CONNECT)
 
 217                     .setParams(String.format(ParamConstants.GET_TOKENS_V2, info.getUserName(), info.getUserPwd()))
 
 219             statusCode = httpMethod.getStatusCode();
 
 221             String result = httpMethod.getResponseBodyAsString();
 
 223             if(statusCode == HttpStatus.SC_CREATED) {
 
 224                 JSONObject accessObj = JSONObject.fromObject(result);
 
 225                 JSONObject tokenObj = accessObj.getJSONObject("token");
 
 226                 Header header = httpMethod.getResponseHeader("accessSession");
 
 227                 setAccessSession(header.getValue());
 
 228                 setRoaRand(tokenObj.getString("roa_rand"));
 
 229                 statusCode = HttpStatus.SC_OK;
 
 231                 LOG.error(CONNECT_FAIL + statusCode + " re:" + result);
 
 234         } catch(JSONException e) {
 
 235             LOG.error(CONNECT_JSONEXCEPTION, e);
 
 236         } catch(VnfmException e) {
 
 237             LOG.error(CONNECT_VNFMEXCEPTION, e);
 
 238         } catch(IOException e) {
 
 239             LOG.error(CONNECT_IOEXCEPTION, e);
 
 242             if(httpMethod != null) {
 
 243                 httpMethod.releaseConnection();
 
 250     private void clearCSMPwd(ConnectInfo connectInfo) {
 
 251         if(null != connectInfo) {
 
 252             connectInfo.setUserPwd("");