2  * Copyright 2016-2017, Nokia Corporation
 
   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.driver.vnfm.svnfm.cbam.impl;
 
  19 import java.io.IOException;
 
  20 import java.util.HashMap;
 
  22 import org.apache.http.client.ClientProtocolException;
 
  23 import org.apache.log4j.Logger;
 
  24 import org.json.JSONException;
 
  25 import org.json.JSONObject;
 
  26 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMCreateVnfRequest;
 
  27 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMCreateVnfResponse;
 
  28 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMHealVnfRequest;
 
  29 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMHealVnfResponse;
 
  30 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMInstantiateVnfRequest;
 
  31 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMInstantiateVnfResponse;
 
  32 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMQueryOperExecutionResponse;
 
  33 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMQueryVnfResponse;
 
  34 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMScaleVnfRequest;
 
  35 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMScaleVnfResponse;
 
  36 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMTerminateVnfRequest;
 
  37 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMTerminateVnfResponse;
 
  38 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.inf.CbamMgmrInf;
 
  39 import org.onap.vfc.nfvo.driver.vnfm.svnfm.common.bo.AdaptorEnv;
 
  40 import org.onap.vfc.nfvo.driver.vnfm.svnfm.constant.CommonConstants;
 
  41 import org.onap.vfc.nfvo.driver.vnfm.svnfm.http.client.HttpClientProcessorInf;
 
  42 import org.springframework.beans.factory.annotation.Autowired;
 
  43 import org.springframework.http.MediaType;
 
  44 import org.springframework.stereotype.Component;
 
  45 import org.springframework.web.bind.annotation.RequestMethod;
 
  47 import com.google.gson.Gson;
 
  50 public class CbamMgmrImpl implements CbamMgmrInf {
 
  51         private static final Logger logger = Logger.getLogger(CbamMgmrImpl.class);
 
  52         private Gson gson = new Gson();
 
  55         private AdaptorEnv adaptorEnv;
 
  58         HttpClientProcessorInf httpClientProcessor;
 
  60         private String retrieveToken() throws ClientProtocolException, IOException, JSONException {
 
  62                 String url= adaptorEnv.getCbamApiUriFront() + CommonConstants.RetrieveCbamTokenPath;
 
  63                 HashMap<String, String> map = new HashMap<String, String>();
 
  64                 map.put(CommonConstants.ACCEPT, "*/*");
 
  65                 map.put(CommonConstants.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED_VALUE);
 
  67                 String bodyPostStr = String.format(CommonConstants.RetrieveCbamTokenPostStr, adaptorEnv.getGrantType(), adaptorEnv.getClientId(), adaptorEnv.getClientSecret());
 
  69                 String responseStr = httpClientProcessor.process(url, RequestMethod.GET, map, bodyPostStr).getContent();
 
  71                 logger.info("CbamMgmrImpl -> retrieveToken, responseStr is " + responseStr);
 
  73                 JSONObject tokenJsonObject = new JSONObject(responseStr);
 
  75                 result = tokenJsonObject.getString(CommonConstants.CBAM_TOKEN_KEY);
 
  80         public CBAMCreateVnfResponse createVnf(CBAMCreateVnfRequest cbamRequest) throws ClientProtocolException, IOException {
 
  81                 String httpPath = CommonConstants.CbamCreateVnfPath;
 
  82                 RequestMethod method = RequestMethod.POST;
 
  84                 String responseStr = operateCbamHttpTask(cbamRequest, httpPath, method);
 
  86                 logger.info("CbamMgmrImpl -> createVnf, responseStr is " + responseStr);
 
  88                 CBAMCreateVnfResponse response = gson.fromJson(responseStr, CBAMCreateVnfResponse.class);
 
  94          * @see com.nokia.vfcadaptor.cbam.impl.CbamMgmrInf#instantiateVnf(com.nokia.vfcadaptor.cbam.bo.CBAMInstantiateVnfRequest, java.lang.String)
 
  96         public CBAMInstantiateVnfResponse instantiateVnf(CBAMInstantiateVnfRequest cbamRequest, String vnfInstanceId) throws ClientProtocolException, IOException {
 
  97                 String httpPath = String.format(CommonConstants.CbamInstantiateVnfPath, vnfInstanceId);
 
  98                 RequestMethod method = RequestMethod.POST;
 
 100                 String responseStr = operateCbamHttpTask(cbamRequest, httpPath, method);
 
 102                 logger.info("CbamMgmrImpl -> instantiateVnf, responseStr is " + responseStr);
 
 104                 CBAMInstantiateVnfResponse response = gson.fromJson(responseStr, CBAMInstantiateVnfResponse.class);
 
 109         public CBAMTerminateVnfResponse terminateVnf(CBAMTerminateVnfRequest cbamRequest, String vnfInstanceId) throws ClientProtocolException, IOException {
 
 110                 String httpPath = String.format(CommonConstants.CbamTerminateVnfPath, vnfInstanceId);
 
 111                 RequestMethod method = RequestMethod.POST;
 
 113                 String responseStr = operateCbamHttpTask(cbamRequest, httpPath, method);
 
 115                 logger.info("CbamMgmrImpl -> terminateVnf, responseStr is " + responseStr);
 
 117                 CBAMTerminateVnfResponse response = gson.fromJson(responseStr, CBAMTerminateVnfResponse.class);
 
 122         public void deleteVnf(String vnfInstanceId) throws ClientProtocolException, IOException {
 
 123                 String httpPath = String.format(CommonConstants.CbamDeleteVnfPath, vnfInstanceId);
 
 124                 RequestMethod method = RequestMethod.DELETE;
 
 126                 operateCbamHttpTask(null, httpPath, method);
 
 128                 logger.info("CbamMgmrImpl -> deleteVnf.");
 
 131         public CBAMScaleVnfResponse scaleVnf(CBAMScaleVnfRequest cbamRequest, String vnfInstanceId) throws ClientProtocolException, IOException {
 
 132                 String httpPath = String.format(CommonConstants.CbamScaleVnfPath, vnfInstanceId);
 
 133                 RequestMethod method = RequestMethod.POST;
 
 135                 String responseStr = operateCbamHttpTask(cbamRequest, httpPath, method);
 
 137                 CBAMScaleVnfResponse response = gson.fromJson(responseStr, CBAMScaleVnfResponse.class);
 
 142         public CBAMHealVnfResponse healVnf(CBAMHealVnfRequest cbamRequest, String vnfInstanceId) throws ClientProtocolException, IOException {
 
 143                 String httpPath = String.format(CommonConstants.CbamHealVnfPath, vnfInstanceId);
 
 144                 RequestMethod method = RequestMethod.POST;
 
 146                 String responseStr = operateCbamHttpTask(cbamRequest, httpPath, method);
 
 148                 logger.info("CbamMgmrImpl -> healVnf, responseStr is " + responseStr);
 
 150                 CBAMHealVnfResponse response = gson.fromJson(responseStr, CBAMHealVnfResponse.class);
 
 155         public CBAMQueryVnfResponse queryVnf(String vnfInstanceId) throws ClientProtocolException, IOException {
 
 156                 String httpPath = String.format(CommonConstants.CbamQueryVnfPath, vnfInstanceId);
 
 157                 RequestMethod method = RequestMethod.GET;
 
 159                 String responseStr = operateCbamHttpTask(null, httpPath, method);
 
 161                 logger.info("CbamMgmrImpl -> queryVnf, responseStr is " + responseStr);
 
 163                 CBAMQueryVnfResponse response = gson.fromJson(responseStr, CBAMQueryVnfResponse.class);
 
 168         public String operateCbamHttpTask(Object httpBodyObj, String httpPath, RequestMethod method) throws ClientProtocolException, IOException {
 
 171                         token = retrieveToken();
 
 172                 } catch (JSONException e) {
 
 173                         logger.error("retrieveTokenError ", e);
 
 176                 String url= adaptorEnv.getCbamApiUriFront() + httpPath;
 
 178                 HashMap<String, String> map = new HashMap<String, String>();
 
 179                 map.put(CommonConstants.AUTHORIZATION, "bearer " + token);
 
 180                 map.put(CommonConstants.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
 
 182                 String responseStr = httpClientProcessor.process(url, method, map, gson.toJson(httpBodyObj)).getContent();
 
 187         public CBAMQueryOperExecutionResponse queryOperExecution(String execId) throws ClientProtocolException, IOException{
 
 188                 String httpPath = String.format(CommonConstants.CbamGetOperStatusPath, execId);
 
 189                 RequestMethod method = RequestMethod.GET;
 
 191                 String responseStr = operateCbamHttpTask(null, httpPath, method);
 
 193                 logger.info("CbamMgmrImpl -> CBAMQueryOperExecutionResponse, responseStr is " + responseStr);
 
 195                 CBAMQueryOperExecutionResponse response = gson.fromJson(responseStr, CBAMQueryOperExecutionResponse.class);
 
 200         public void setAdaptorEnv(AdaptorEnv adaptorEnv) {
 
 201                 this.adaptorEnv = adaptorEnv;