2  * ============LICENSE_START=======================================================
 
   4  * ================================================================================
 
   5  * Copyright (C) 2017 Huawei Technologies Co., Ltd. All rights reserved.
 
   6  * ================================================================================
 
   7  * Modifications Copyright (c) 2019 Samsung
 
   8  * ================================================================================
 
   9  * Licensed under the Apache License, Version 2.0 (the "License")
 
  10  * you may not use this file except in compliance with the License.
 
  11  * You may obtain a copy of the License at
 
  13  *      http://www.apache.org/licenses/LICENSE-2.0
 
  15  * Unless required by applicable law or agreed to in writing, software
 
  16  * distributed under the License is distributed on an "AS IS" BASIS,
 
  17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
  18  * See the License for the specific language governing permissions and
 
  19  * limitations under the License.
 
  20  * ============LICENSE_END=========================================================
 
  23 package org.onap.so.bpmn.infrastructure.scripts
 
  25 import org.onap.so.client.HttpClientFactory
 
  26 import org.onap.so.client.aai.AAIObjectType
 
  27 import org.onap.so.client.aai.entities.uri.AAIResourceUri
 
  28 import org.onap.so.client.aai.entities.uri.AAIUriFactory
 
  29 import org.camunda.bpm.engine.delegate.BpmnError
 
  30 import org.camunda.bpm.engine.delegate.DelegateExecution
 
  31 import org.onap.so.bpmn.common.scripts.AbstractServiceTaskProcessor
 
  32 import org.onap.so.bpmn.common.scripts.ExceptionUtil
 
  33 import org.onap.so.bpmn.core.json.JsonUtils
 
  34 import org.onap.so.client.HttpClient
 
  35 import org.slf4j.Logger
 
  36 import org.slf4j.LoggerFactory
 
  37 import org.onap.so.bpmn.core.UrnPropertiesReader
 
  39 import javax.ws.rs.core.Response
 
  40 import org.onap.logging.filter.base.ONAPComponents;
 
  43  * This groovy class supports the <class>DoCreateVFCNetworkServiceInstance.bpmn</class> process.
 
  44  * flow for VFC Network Service Create
 
  46 public class CreateVFCNSResource extends AbstractServiceTaskProcessor {
 
  47     private static final Logger logger = LoggerFactory.getLogger( CreateVFCNSResource.class)
 
  49     ExceptionUtil exceptionUtil = new ExceptionUtil()
 
  51     JsonUtils jsonUtil = new JsonUtils()
 
  55      * Pre Process the BPMN Flow Request
 
  57      * generate the nsOperationKey
 
  58      * generate the nsParameters
 
  60     public void preProcessRequest (DelegateExecution execution) {
 
  61         JsonUtils jsonUtil = new JsonUtils()
 
  64        logger.trace("preProcessRequest() ")
 
  66            //deal with nsName and Description
 
  67            String resourceInput = execution.getVariable("resourceInput")
 
  69            String resourceParameters = jsonUtil.getJsonValue(resourceInput, "resourceParameters")
 
  71            String resourceName = jsonUtil.getJsonValue(resourceInput, "resourceInstanceName")
 
  72            execution.setVariable("nsServiceName", resourceName)
 
  74            String nsServiceDescription = execution.getVariable("nsServiceDescription")
 
  75            logger.info("nsServiceName:" + resourceName + " nsServiceDescription:" + nsServiceDescription)
 
  76            //deal with operation key
 
  77            String globalSubscriberId = jsonUtil.getJsonValue(resourceInput, "globalSubscriberId")
 
  78            logger.info("globalSubscriberId:" + globalSubscriberId)
 
  79            //set local globalSubscriberId variable
 
  80            execution.setVariable("globalSubscriberId", globalSubscriberId)
 
  81            String serviceType = execution.getVariable("serviceType")
 
  82            logger.info("serviceType:" + serviceType)
 
  84            String serviceId = execution.getVariable("serviceInstanceId")
 
  85            logger.info("serviceId:" + serviceId)
 
  87            String operationId = jsonUtil.getJsonValue(resourceInput, "operationId")
 
  88            logger.info("serviceType:" + serviceType)
 
  90            String nodeTemplateUUID = jsonUtil.getJsonValue(resourceInput, "resourceModelInfo.modelCustomizationUuid")
 
  91            String nsServiceModelUUID = jsonUtil.getJsonValue(resourceParameters, "requestInputs.nsd0_providing_service_uuid")
 
  92            logger.info("nodeTemplateUUID:" + nodeTemplateUUID)
 
  94             * segmentInformation needed as a object of segment
 
  97             *     "nodeTemplateName":"",
 
 100             *       //this is the nsParameters sent to VF-C
 
 104            String nsParameters = jsonUtil.getJsonValue(resourceInput, "resourceParameters")
 
 105            logger.info("nsParameters:" + nsParameters)
 
 106            String nsOperationKey = """{
 
 107                    "globalSubscriberId":"${globalSubscriberId}",
 
 108                    "serviceType":"${serviceType}",
 
 109                    "serviceId":"${serviceId}",
 
 110                    "operationId":"${operationId}",
 
 111                    "nodeTemplateUUID":"${nodeTemplateUUID}"
 
 113            execution.setVariable("nsOperationKey", nsOperationKey)
 
 114            execution.setVariable("nsParameters", nsParameters)
 
 115            execution.setVariable("nsServiceModelUUID", nsServiceModelUUID)
 
 117            String vfcAdapterUrl = UrnPropertiesReader.getVariable("mso.adapters.vfc.rest.endpoint", execution)
 
 119            if (vfcAdapterUrl == null || vfcAdapterUrl.isEmpty()) {
 
 120                   msg = getProcessKey(execution) + ': mso:adapters:vfcc:rest:endpoint URN mapping is not defined'
 
 124            while (vfcAdapterUrl.endsWith('/')) {
 
 125                   vfcAdapterUrl = vfcAdapterUrl.substring(0, vfcAdapterUrl.length()-1)
 
 128            execution.setVariable("vfcAdapterUrl", vfcAdapterUrl)
 
 130        } catch (BpmnError e) {
 
 132        } catch (Exception ex){
 
 133            msg = "Exception in preProcessRequest " + ex.getMessage()
 
 135            exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
 
 137        logger.trace("Exit preProcessRequest ")
 
 143     public void createNetworkService(DelegateExecution execution) {
 
 144         logger.trace("createNetworkService ")
 
 145         String vfcAdapterUrl = execution.getVariable("vfcAdapterUrl")
 
 146         String nsOperationKey = execution.getVariable("nsOperationKey")
 
 147         String nsServiceModelUUID = execution.getVariable("nsServiceModelUUID")
 
 148         String nsParameters = execution.getVariable("nsParameters")
 
 149         String nsServiceName = execution.getVariable("nsServiceName")
 
 150         String nsServiceDescription = execution.getVariable("nsServiceDescription")
 
 151         String locationConstraints = jsonUtil.getJsonValue(nsParameters, "locationConstraints")
 
 152         String requestInputs = jsonUtil.getJsonValue(nsParameters, "requestInputs")
 
 154                 "nsServiceName":"${nsServiceName}",
 
 155                 "nsServiceDescription":"${nsServiceDescription}",
 
 156                 "nsServiceModelUUID":"${nsServiceModelUUID}",
 
 157                 "nsOperationKey":${nsOperationKey},
 
 159                      "locationConstraints":${locationConstraints},
 
 160                      "additionalParamForNs":${requestInputs}
 
 163         Response apiResponse = postRequest(execution, vfcAdapterUrl + "/ns", reqBody)
 
 164         String returnCode = apiResponse.getStatus ()
 
 165         String aaiResponseAsString = apiResponse.readEntity(String.class)
 
 166         String nsInstanceId = ""
 
 167         if(returnCode== "200" || returnCode == "201"){
 
 168             nsInstanceId =  jsonUtil.getJsonValue(aaiResponseAsString, "nsInstanceId")
 
 170         execution.setVariable("nsInstanceId", nsInstanceId)
 
 171         logger.info(" *****Exit  createNetworkService *****")
 
 175      * instantiate NS task
 
 177     public void instantiateNetworkService(DelegateExecution execution) {
 
 178         logger.trace("instantiateNetworkService ")
 
 179         String vfcAdapterUrl = execution.getVariable("vfcAdapterUrl")
 
 180         String nsOperationKey = execution.getVariable("nsOperationKey")
 
 181         String nsParameters = execution.getVariable("nsParameters")
 
 182         String nsServiceName = execution.getVariable("nsServiceName")
 
 183         String nsServiceDescription = execution.getVariable("nsServiceDescription")
 
 185         "nsServiceName":"${nsServiceName}",
 
 186         "nsServiceDescription":"${nsServiceDescription}",
 
 187         "nsOperationKey":${nsOperationKey},
 
 188         "nsParameters":${nsParameters}
 
 190         String nsInstanceId = execution.getVariable("nsInstanceId")
 
 191         String url = vfcAdapterUrl + "/ns/" +nsInstanceId + "/instantiate"
 
 192         Response apiResponse = postRequest(execution, url, reqBody)
 
 193         String returnCode = apiResponse.getStatus()
 
 194         String aaiResponseAsString = apiResponse.readEntity(String.class)
 
 196         if(returnCode== "200"|| returnCode == "201"){
 
 197             jobId =  jsonUtil.getJsonValue(aaiResponseAsString, "jobId")
 
 199         execution.setVariable("jobId", jobId)
 
 200         logger.info(" *****Exit  instantiateNetworkService *****")
 
 206     public void queryNSProgress(DelegateExecution execution) {
 
 207         logger.trace("queryNSProgress ")
 
 208         String vfcAdapterUrl = execution.getVariable("vfcAdapterUrl")
 
 209         String jobId = execution.getVariable("jobId")
 
 210         String nsOperationKey = execution.getVariable("nsOperationKey")
 
 211         String url = vfcAdapterUrl + "/jobs/" + jobId
 
 212         Response apiResponse = postRequest(execution, url, nsOperationKey)
 
 213         String returnCode = apiResponse.getStatus()
 
 214         String aaiResponseAsString = apiResponse.readEntity(String.class)
 
 215         String operationStatus = "error"
 
 216         if(returnCode== "200"|| returnCode == "201"){
 
 217             operationStatus = jsonUtil.getJsonValue(aaiResponseAsString, "responseDescriptor.status")
 
 219         execution.setVariable("operationStatus", operationStatus)
 
 220         logger.info(" *****Exit  queryNSProgress *****")
 
 226     public void timeDelay(DelegateExecution execution) {
 
 229         } catch(InterruptedException e) {
 
 230             logger.error("Time Delay exception" + e.getMessage())
 
 237     public void addNSRelationship(DelegateExecution execution) {
 
 238         logger.trace("addNSRelationship ")
 
 239         String nsInstanceId = execution.getVariable("nsInstanceId")
 
 240         if(nsInstanceId == null || nsInstanceId == ""){
 
 241             logger.info(" create NS failed, so do not need to add relationship")
 
 244         String globalSubscriberId = execution.getVariable("globalSubscriberId")
 
 245         String serviceType = execution.getVariable("serviceType")
 
 246         String serviceId = execution.getVariable("serviceInstanceId")
 
 248         AAIResourceUri nsUri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE,globalSubscriberId,serviceType,nsInstanceId)
 
 249         AAIResourceUri relatedServiceUri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE,globalSubscriberId,serviceType,serviceId)
 
 252             getAAIClient().connect(nsUri,relatedServiceUri)
 
 253             logger.info("NS relationship to Service added successfully")
 
 255             logger.error("Exception occured while Creating NS relationship."+ e.getMessage())
 
 256             throw new BpmnError("MSOWorkflowException")
 
 262      * url: the url of the request
 
 263      * requestBody: the body of the request
 
 265     private Response postRequest(DelegateExecution execution, String urlString, String requestBody){
 
 266         logger.trace("Started Execute VFC adapter Post Process ")
 
 267         logger.info("url:" + urlString +"\nrequestBody:"+ requestBody)
 
 268         Response apiResponse = null
 
 271             URL url = new URL(urlString)
 
 273             // Get the Basic Auth credentials for the VFCAdapter, username is 'bpel', auth is '07a7159d3bf51a0e53be7a8f89699be7'
 
 274             // user 'bepl' authHeader is the same with mso.db.auth
 
 275             String basicAuthValuedb =  UrnPropertiesReader.getVariable("mso.db.auth", execution)
 
 276             HttpClient httpClient = new HttpClientFactory().newJsonClient(url, ONAPComponents.VNF_ADAPTER)
 
 277             httpClient.addAdditionalHeader("Accept", "application/json")
 
 278             httpClient.addAdditionalHeader("Authorization", basicAuthValuedb)
 
 280             apiResponse = httpClient.post(requestBody)
 
 282             logger.debug("response code:"+ apiResponse.getStatus() +"\nresponse body:"+ apiResponse.readEntity(String.class))
 
 285             logger.error("VFC Aatpter Post Call Exception:" + e.getMessage())
 
 286             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, "VFC Aatpter Post Call Exception")
 
 289         logger.trace("Completed Execute VF-C adapter Post Process ")
 
 294         public void sendSyncResponse (DelegateExecution execution) {
 
 295                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
 
 296                 logger.debug( " *** sendSyncResponse *** ")
 
 299                         String operationStatus = execution.getVariable("operationStatus")
 
 300                         // RESTResponse for main flow
 
 301                         String resourceOperationResp = """{"operationStatus":"${operationStatus}"}""".trim()
 
 302                         logger.debug( " sendSyncResponse to APIH:" + "\n" + resourceOperationResp)
 
 303                         sendWorkflowResponse(execution, 202, resourceOperationResp)
 
 304                         execution.setVariable("sentSyncResponse", true)
 
 306                 } catch (Exception ex) {
 
 307                         String msg = "Exceptuion in sendSyncResponse:" + ex.getMessage()
 
 309                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
 
 311                 logger.debug(" ***** Exit sendSyncResopnse *****")