2  * ============LICENSE_START=======================================================
\r 
   4  * ================================================================================
\r 
   5  * Copyright (C) 2017 Huawei Technologies Co., Ltd. All rights reserved.
\r 
   6  * ================================================================================
\r 
   7  * Licensed under the Apache License, Version 2.0 (the "License");
\r 
   8  * you may not use this file except in compliance with the License.
\r 
   9  * You may obtain a copy of the License at
\r 
  11  *      http://www.apache.org/licenses/LICENSE-2.0
\r 
  13  * Unless required by applicable law or agreed to in writing, software
\r 
  14  * distributed under the License is distributed on an "AS IS" BASIS,
\r 
  15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r 
  16  * See the License for the specific language governing permissions and
\r 
  17  * limitations under the License.
\r 
  18  * ============LICENSE_END=========================================================
\r 
  21 package org.openecomp.mso.bpmn.infrastructure.scripts;
\r 
  23 import static org.apache.commons.lang3.StringUtils.*;
\r 
  24 import groovy.xml.XmlUtil
\r 
  25 import groovy.json.*
\r 
  26 import org.openecomp.mso.bpmn.common.scripts.AbstractServiceTaskProcessor 
\r 
  27 import org.openecomp.mso.bpmn.common.scripts.ExceptionUtil 
\r 
  28 import org.openecomp.mso.bpmn.core.WorkflowException 
\r 
  29 import org.openecomp.mso.bpmn.core.json.JsonUtils 
\r 
  30 import org.openecomp.mso.rest.APIResponse
\r 
  32 import java.util.UUID;
\r 
  34 import org.camunda.bpm.engine.delegate.BpmnError 
\r 
  35 import org.camunda.bpm.engine.runtime.Execution
\r 
  36 import org.apache.commons.lang3.*
\r 
  37 import org.apache.commons.codec.binary.Base64;
\r 
  38 import org.springframework.web.util.UriUtils 
\r 
  39 import org.openecomp.mso.rest.RESTClient 
\r 
  40 import org.openecomp.mso.rest.RESTConfig
\r 
  41 import org.openecomp.mso.rest.APIResponse;
\r 
  44  * This groovy class supports the <class>CreateGenericE2EServiceInstance.bpmn</class> process.
\r 
  45  * flow for E2E ServiceInstance Create
\r 
  47 public class CreateGenericE2EServiceInstance extends AbstractServiceTaskProcessor {
\r 
  49     String createUrl = "/vfc/vfcadapters/v1/ns"
\r 
  51     String instantiateUrl = "/vfcvfcadatpers/v1/ns/{nsInstanceId}/instantiate"
\r 
  53     String queryJobUrl = "/vfc/vfcadatpers/v1/jobs/{jobId}"
\r 
  55     ExceptionUtil exceptionUtil = new ExceptionUtil()
\r 
  57     JsonUtils jsonUtil = new JsonUtils()
\r 
  60      * Pre Process the BPMN Flow Request
\r 
  62      * generate the nsOperationKey
\r 
  63      * generate the nsParameters
\r 
  65     public void preProcessRequest (Execution execution) {
\r 
  66            def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
\r 
  68        utils.log("DEBUG", " *** preProcessRequest() *** ", isDebugEnabled)
\r 
  70            //deal with nsName and Description
\r 
  71            String nsServiceName = execution.getVariable("nsServiceName")
\r 
  72            String nsServiceDescription = execution.getVariable("nsServiceDescription")
\r 
  73            utils.log("DEBUG", "nsServiceName:" + nsServiceName + " nsServiceDescription:" + nsServiceDescription, isDebugEnabled)
\r 
  74            //deal with operation key
\r 
  75            String globalSubscriberId = execution.getVariable("globalSubscriberId")
\r 
  76            utils.log("DEBUG", "globalSubscriberId:" + globalSubscriberId, isDebugEnabled)
\r 
  77            String serviceType = execution.getVariable("serviceType")
\r 
  78            utils.log("DEBUG", "serviceType:" + serviceType, isDebugEnabled)
\r 
  79            String serviceId = execution.getVariable("serviceId")
\r 
  80            utils.log("DEBUG", "serviceId:" + serviceId, isDebugEnabled)
\r 
  81            String operationId = execution.getVariable("operationId")
\r 
  82            utils.log("DEBUG", "serviceType:" + serviceType, isDebugEnabled)
\r 
  83            String nodeTemplateUUID = execution.getVariable("nodeTemplateUUID")
\r 
  84            utils.log("DEBUG", "globalSubscriberId:" + globalSubscriberId, isDebugEnabled)
\r 
  86             * segmentInformation needed as a object of segment
\r 
  89             *     "nodeTemplateName":"",
\r 
  92             *       //this is the nsParameters sent to VF-C
\r 
  96            String siRequest = execution.getVariable("segmentInformation")
\r 
  97            utils.log("DEBUG", "Input Request:" + siRequest, isDebugEnabled)
\r 
  98            String nsOperationKey = "{\"globalSubscriberId\":\"" + globalSubscriberId + "\",\"serviceType:\""
\r 
  99                  + serviceType + "\",\"serviceId\":\"" + serviceId + "\",\"operationId\":\"" + operationId
\r 
 100                  +"\",\"nodeTemplateUUID\":\"" + nodeTemplateUUID + "\"}";
\r 
 101            execution.setVariable("nsOperationKey", nsOperationKey);
\r 
 102            execution.setVariable("nsParameters", jsonUtil.getJsonValue(siRequest, "nsParameters"))
\r 
 105        } catch (BpmnError e) {
\r 
 107        } catch (Exception ex){
\r 
 108            msg = "Exception in preProcessRequest " + ex.getMessage()
\r 
 109            utils.log("DEBUG", msg, isDebugEnabled)
\r 
 110            exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
\r 
 112        utils.log("DEBUG"," ***** Exit preProcessRequest *****",  isDebugEnabled)
\r 
 118     public void createNetworkService(Execution execution) {
\r 
 120         String nsOperationKey = excution.getVariable("nsOperationKey");
\r 
 121         String nsParameters = excution.getVariable("nsParameters");
\r 
 122         String nsServiceName = execution.getVariable("nsServiceName")
\r 
 123         String nsServiceDescription = execution.getVariable("nsServiceDescription")
\r 
 124         String reqBody = "{\"nsServiceName\":\"" + nsServiceName + "\",\"nsServiceDescription\":\"" + nsServiceDescription
\r 
 125               +"\",\"nsOperationKey\":" + nsOperationKey + ",\"nsParameters\":" + nsParameters
\r 
 126         APIResponse createRsp = postRequest(createUrl, reqBody)
\r 
 127         String returnCode = apiResponse.getStatusCode()
\r 
 128         String aaiResponseAsString = apiResponse.getResponseBodyAsString()
\r 
 129         String nsInstanceId = "";
\r 
 130         if(returnCode== "200"){
\r 
 131             nsInstanceId =  jsonUtil.getJsonValue(aaiResponseAsString, "nsInstanceId")
\r 
 133         execution.setVariable("nsInstanceId", nsInstanceId)
\r 
 138      * instantiate NS task
\r 
 140     public void instantiateNetworkService(Execution execution) {
\r 
 141         String nsOperationKey = excution.getVariable("nsOperationKey");
\r 
 142         String nsParameters = excution.getVariable("nsParameters");
\r 
 143         String nsServiceName = execution.getVariable("nsServiceName")
\r 
 144         String nsServiceDescription = execution.getVariable("nsServiceDescription")
\r 
 145         String reqBody = "{\"nsServiceName\":\"" + nsServiceName + "\",\"nsServiceDescription\":\"" + nsServiceDescription
\r 
 146               +"\",\"nsOperationKey\":" + nsOperationKey + ",\"nsParameters\":" + nsParameters
\r 
 147         String url = instantiateUrl.replaceAll("{nsInstanceId}", execution.getVariable("nsInstanceId")) 
\r 
 148         APIResponse createRsp = postRequest(url, reqBody)
\r 
 149         String returnCode = apiResponse.getStatusCode()
\r 
 150         String aaiResponseAsString = apiResponse.getResponseBodyAsString()
\r 
 152         if(returnCode== "200"){
\r 
 153             jobId =  jsonUtil.getJsonValue(aaiResponseAsString, "jobId")
\r 
 155         execution.setVariable("jobId", nsInstanceId)
\r 
 161     public void queryNSProgress(Execution execution) {
\r 
 162         String jobId = execution.getVariable("jobId")
\r 
 163         String nsOperationKey = excution.getVariable("nsOperationKey");
\r 
 164         String url = queryJobUrl.replaceAll("{jobId}", execution.getVariable("jobId")) 
\r 
 165         APIResponse createRsp = postRequest(url, nsOperationKey)
\r 
 166         String returnCode = apiResponse.getStatusCode()
\r 
 167         String aaiResponseAsString = apiResponse.getResponseBodyAsString()
\r 
 168         String operationStatus = "error"
\r 
 169         if(returnCode== "200"){
\r 
 170             operationStatus = jsonUtil.getJsonValue(aaiResponseAsString, "responseDescriptor.status")
\r 
 172         exection.setVariable("operationStatus", operationStatus)
\r 
 178     public void timeDelay(Execution execution) {
\r 
 180             Thread.sleep(5000);
\r 
 181         } catch(InterruptedException e) {           
\r 
 182             taskProcessor.utils.log("ERROR", "Time Delay exception" + e , isDebugEnabled)
\r 
 189     public void finishNSCreate(Execution execution) {
\r 
 190         //no need to do anything util now
\r 
 195      * url: the url of the request
\r 
 196      * requestBody: the body of the request
\r 
 198     private APIResponse postRequest(String url, String requestBody){
\r 
 199         def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
\r 
 200         taskProcessor.logDebug( " ======== Started Execute VFC adapter Post Process ======== ", isDebugEnabled)
\r 
 201         taskProcessor.logDebug( "url:"+url +"\nrequestBody:"+ requestBody, isDebugEnabled)
\r 
 202         APIResponse apiResponse = null
\r 
 204             RESTConfig config = new RESTConfig(url);
\r 
 205             RESTClient client = new RESTClient(config).addHeader("X-FromAppId", "MSO").addHeader("X-TransactionId", uuid).addHeader("Accept","application/json");
\r 
 206             apiResponse = client.httpPost(requestBody)
\r 
 207             taskProcessor.logDebug( "response code:"+ apiResponse.getStatusCode() +"\nresponse body:"+ apiResponse.getResponseBodyAsString(), isDebugEnabled)
\r 
 208             taskProcessor.logDebug( "======== Completed Execute VF-C adapter Post Process ======== ", isDebugEnabled)
\r 
 209         }catch(Exception e){
\r 
 210             taskProcessor.utils.log("ERROR", "Exception occured while executing AAI Post Call. Exception is: \n" + e, isDebugEnabled)
\r 
 211             throw new BpmnError("MSOWorkflowException")
\r