2  * Copyright 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.
 
  16 package org.onap.vnfsdk.marketplace.onboarding.hooks.validatelifecycle;
 
  19 import java.io.IOException;
 
  22 import org.apache.http.entity.ContentType;
 
  23 import org.apache.http.entity.mime.MultipartEntityBuilder;
 
  24 import org.onap.vnfsdk.marketplace.common.CommonConstant;
 
  25 import org.onap.vnfsdk.marketplace.common.JsonUtil;
 
  26 import org.onap.vnfsdk.marketplace.msb.MsbDetails;
 
  27 import org.onap.vnfsdk.marketplace.msb.MsbDetailsHolder;
 
  28 import org.onap.vnfsdk.marketplace.onboarding.entity.OnBoradingRequest;
 
  29 import org.onap.vnfsdk.marketplace.rest.RestConstant;
 
  30 import org.onap.vnfsdk.marketplace.rest.RestResponse;
 
  31 import org.onap.vnfsdk.marketplace.rest.RestfulClient;
 
  32 import org.slf4j.Logger;
 
  33 import org.slf4j.LoggerFactory;
 
  35 import com.fasterxml.jackson.core.JsonParseException;
 
  36 import com.fasterxml.jackson.databind.DeserializationFeature;
 
  37 import com.fasterxml.jackson.databind.JsonMappingException;
 
  38 import com.fasterxml.jackson.databind.ObjectMapper;
 
  40 public class LifecycleTestExceutor
 
  42     private static final Logger logger = LoggerFactory.getLogger(LifecycleTestExceutor.class);
 
  43     public static final String CATALOUGE_UPLOAD_URL_IN = "{0}:{1}/openoapi/catalog/v1/csars";
 
  45     private LifecycleTestExceutor()
 
  49      * Interface to Send Request to Start Function test
 
  50      * @param onBoradFuncTestReq
 
  53     @SuppressWarnings("unchecked")
 
  54     public static String uploadPackageToCatalouge(OnBoradingRequest onBoradFuncTestReq)
 
  56         String packagePath = onBoradFuncTestReq.getPackagePath() + File.separator + onBoradFuncTestReq.getPackageName();
 
  57         logger.info("Package file path uploadPackageToCatalouge:" + packagePath);
 
  59         String catalougeCsarId = null;
 
  60         MsbDetails oMsbDetails =  MsbDetailsHolder.getMsbDetails();
 
  61         if(null == oMsbDetails)
 
  63             logger.error("Failed to get MSB details during uploadPackageToCatalouge !!!");
 
  64             return catalougeCsarId;
 
  67         File fileData = new File (packagePath);
 
  69         MultipartEntityBuilder builder = MultipartEntityBuilder.create();
 
  70         builder.addBinaryBody("file", fileData, ContentType.MULTIPART_FORM_DATA, onBoradFuncTestReq.getPackageName());
 
  72         //IP and Port needs to be configured !!!
 
  73         RestResponse rsp = RestfulClient.post(oMsbDetails.getDefaultServer().getHost(),Integer.parseInt(oMsbDetails.getDefaultServer().getPort()),CommonConstant.CATALOUGE_UPLOAD_URL,builder.build());
 
  74         if(!checkValidResponse(rsp))
 
  76             logger.error("Failed to upload package to catalouge:" + rsp.getStatusCode());
 
  77             return catalougeCsarId;
 
  80         logger.info("Response for uploadPackageToCatalouge :" +  rsp.getResult());
 
  81         catalougeCsarId = getCsarIdValue(rsp.getResult());
 
  83         logger.info("CSARID for uploadPackageToCatalouge :" + catalougeCsarId);
 
  84         return catalougeCsarId;
 
  90     public static String execlifecycleTest(OnBoradingRequest onBoradFuncTestReq, LifeCycleTestReq oLifeCycleTestReq)
 
  92         String packagePath = onBoradFuncTestReq.getPackagePath() + File.separator + onBoradFuncTestReq.getPackageName();
 
  93         logger.info("Package file path Function test:" + packagePath);
 
  95         MsbDetails oMsbDetails =  MsbDetailsHolder.getMsbDetails();
 
  96         if(null == oMsbDetails) {
 
  97             logger.error("Failed to get MSB details during execlifecycleTest !!!");
 
 101         String rawDataJson = JsonUtil.toJson(oLifeCycleTestReq);
 
 102         if(null == rawDataJson) {
 
 103             logger.error("Failed to convert LifeCycleTestReq object to Json String !!!");
 
 107         RestResponse oResponse = RestfulClient.sendPostRequest(oMsbDetails.getDefaultServer().getHost(),
 
 108                 oMsbDetails.getDefaultServer().getPort(),
 
 109                 CommonConstant.LifeCycleTest.LIFECYCLE_TEST_URL, rawDataJson);
 
 111         if(!checkValidResponse(oResponse)) {
 
 112             logger.error("execlifecycleTest response is faliure :"+ oResponse.getStatusCode());
 
 115         logger.info("Response execlifecycleTest :"+ oResponse.getResult());
 
 116         return oResponse.getResult();
 
 120      * Check Response is Valid
 
 124     private static boolean checkValidResponse(RestResponse rsp)
 
 126         if (rsp.getStatusCode() == null || rsp.getResult() == null
 
 127                 || (RestConstant.RESPONSE_CODE_200 != rsp.getStatusCode() && RestConstant.RESPONSE_CODE_201 != rsp.getStatusCode()))
 
 139     private static String getCsarIdValue(String strJsonData)
 
 141         ObjectMapper mapper = new ObjectMapper();
 
 142         mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
 
 143         Map<String, String> dataMap = null;
 
 146             dataMap = mapper.readValue(strJsonData, Map.class);
 
 147         } catch(JsonParseException e) {
 
 148             logger.error("JsonParseException:Failed to upload package to catalouge:", e);
 
 149         } catch(JsonMappingException e) {
 
 150             logger.error("JsonMappingException:Failed to upload package to catalouge:", e);
 
 151         } catch(IOException e) {
 
 152             logger.error("IOException:Failed to upload package to catalouge:", e);
 
 156             if(dataMap != null) {
 
 157                 return dataMap.get("csarId");
 
 160         catch (NullPointerException e)
 
 162             logger.error("NullPointerException:Failed to get csarId", e);