2  * ============LICENSE_START=======================================================
 
   4  * ================================================================================
 
   5  * Copyright (c) 2019, CMCC Technologies Co., Ltd.
 
   6  * ================================================================================
 
   7  * Licensed under the Apache License, Version 2.0 (the "License");
 
   8  * you may not use this file except in compliance with the License.
 
   9  * You may obtain a copy of the License at
 
  11  *      http://www.apache.org/licenses/LICENSE-2.0
 
  13  * Unless required by applicable law or agreed to in writing, software
 
  14  * distributed under the License is distributed on an "AS IS" BASIS,
 
  15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
  16  * See the License for the specific language governing permissions and
 
  17  * limitations under the License.
 
  18  * ============LICENSE_END=========================================================
 
  21 package org.onap.so.bpmn.infrastructure.scripts
 
  23 import org.camunda.bpm.engine.delegate.BpmnError
 
  24 import org.camunda.bpm.engine.delegate.DelegateExecution
 
  25 import org.onap.aai.domain.yang.ServiceProfile
 
  26 import org.onap.aai.domain.yang.ServiceProfiles
 
  27 import org.onap.so.bpmn.common.scripts.AbstractServiceTaskProcessor
 
  28 import org.onap.so.bpmn.common.scripts.ExceptionUtil
 
  29 import org.onap.so.bpmn.common.scripts.MsoUtils
 
  30 import org.onap.so.bpmn.common.scripts.RequestDBUtil
 
  31 import org.onap.so.bpmn.core.WorkflowException
 
  32 import org.onap.so.bpmn.core.json.JsonUtils
 
  33 import org.onap.so.client.aai.AAIObjectType
 
  34 import org.onap.so.client.aai.AAIResourcesClient
 
  35 import org.onap.so.client.aai.entities.AAIResultWrapper
 
  36 import org.onap.so.client.aai.entities.uri.AAIResourceUri
 
  37 import org.onap.so.client.aai.entities.uri.AAIUriFactory
 
  38 import org.onap.so.db.request.beans.OperationStatus
 
  39 import org.slf4j.Logger
 
  40 import org.slf4j.LoggerFactory
 
  42 import javax.ws.rs.NotFoundException
 
  44 import static org.apache.commons.lang3.StringUtils.isBlank
 
  46 class DeleteSliceService extends AbstractServiceTaskProcessor {
 
  48     String Prefix="DELSS_"
 
  49     ExceptionUtil exceptionUtil = new ExceptionUtil()
 
  50     JsonUtils jsonUtil = new JsonUtils()
 
  51     private RequestDBUtil requestDBUtil = new RequestDBUtil()
 
  53     private static final Logger logger = LoggerFactory.getLogger( DeleteSliceService.class)
 
  56     void preProcessRequest(DelegateExecution execution) {
 
  57         execution.setVariable("prefix", Prefix)
 
  60         logger.trace("Starting preProcessRequest")
 
  63             // check for incoming json message/input
 
  64             String siRequest = execution.getVariable("bpmnRequest")
 
  65             String requestId = execution.getVariable("mso-request-id")
 
  66             execution.setVariable("msoRequestId", requestId)
 
  68             //e2eslice-service instance id
 
  69             String serviceInstanceId = execution.getVariable("serviceInstanceId")
 
  70             if (isBlank(serviceInstanceId)) {
 
  71                 msg = "e2eslice-service id is null"
 
  72                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
 
  74             logger.info("Input Request: ${siRequest}, reqId: ${requestId}, e2eslice-service: ${serviceInstanceId}")
 
  77             checkAndSetRequestParam(siRequest,"globalSubscriberId",false, execution)
 
  78             checkAndSetRequestParam(siRequest,"serviceType",false, execution)
 
  79             checkAndSetRequestParam(siRequest,"operationId",false, execution)
 
  81             //prepare init operation status
 
  82             execution.setVariable("progress", "0")
 
  83             execution.setVariable("result", "processing")
 
  84             execution.setVariable("operationType", "DELETE")
 
  85             execution.setVariable("operationContent", "Prepare init service")
 
  86             updateServiceOperationStatus(execution)
 
  88         } catch (BpmnError e) {
 
  90         } catch (Exception ex) {
 
  91             msg = "Exception in preProcessRequest " + ex.getMessage()
 
  93             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
 
  95         logger.trace("Exit preProcessRequest")
 
  99      * send asynchronous response
 
 102     void sendAsyncResponse(DelegateExecution execution) {
 
 103         logger.trace("Staring sendSyncResponse")
 
 106             String operationId = execution.getVariable("operationId")
 
 107             String syncResponse = """{"operationId":"${operationId}"}""".trim()
 
 108             logger.info("sendSynchResponse: xmlSyncResponse - " + "\n" + syncResponse)
 
 109             sendWorkflowResponse(execution, 202, syncResponse)
 
 111         } catch (Exception ex) {
 
 112             String msg  = "Exception in sendSyncResponse: " + ex.getMessage()
 
 113             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
 
 115         logger.trace("Exit sendSyncResponse")
 
 119      * Deletes the slice service instance in aai
 
 121     void deleteSliceServiceInstance(DelegateExecution execution) {
 
 122         logger.trace("Entered deleteSliceServiceInstance")
 
 125             AAIResourcesClient resourceClient = new AAIResourcesClient()
 
 126             AAIResourceUri serviceInstanceUri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, execution.getVariable("globalSubscriberId"), execution.getVariable("serviceType"), execution.getVariable("serviceInstanceId"))
 
 127             resourceClient.delete(serviceInstanceUri)
 
 129             execution.setVariable("progress", "100")
 
 130             execution.setVariable("result", "finished")
 
 131             execution.setVariable("operationContent", "NSMF completes slicing service termination.")
 
 132             updateServiceOperationStatus(execution)
 
 134             logger.trace("Exited deleteSliceServiceInstance")
 
 136             logger.debug("Error occured within deleteSliceServiceInstance method: " + e)
 
 137             exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Error occured during deleteSliceServiceInstance from aai")
 
 142      * update operation status
 
 145     private void updateServiceOperationStatus(DelegateExecution execution){
 
 147         OperationStatus operationStatus = new OperationStatus()
 
 148         operationStatus.setServiceId(execution.getVariable("serviceInstanceId"))
 
 149         operationStatus.setOperationId(execution.getVariable("operationId"))
 
 150         operationStatus.setUserId(execution.getVariable("globalSubscriberId"))
 
 151         operationStatus.setResult(execution.getVariable("result"))
 
 152         operationStatus.setProgress(execution.getVariable("progress"))
 
 153         operationStatus.setOperationContent(execution.getVariable("operationContent"))
 
 154         operationStatus.setReason(execution.getVariable("reason"))
 
 155         operationStatus.setOperation(execution.getVariable("operationType"))
 
 157         requestDBUtil.prepareUpdateOperationStatus(execution, operationStatus)
 
 161      * delete service profile from aai
 
 164     private void delServiceProfileFromAAI(DelegateExecution execution)
 
 166         String globalSubscriberId = execution.getVariable("globalSubscriberId")
 
 167         String serviceType = execution.getVariable("serviceType")
 
 168         String serviceInstanceId = execution.getVariable("serviceInstanceId")
 
 169         String profileId = ""
 
 173             AAIResourcesClient resourceClient = new AAIResourcesClient()
 
 174             AAIResourceUri resourceUri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_PROFILE_ALL, globalSubscriberId, serviceType, serviceInstanceId)
 
 175             AAIResultWrapper wrapper = resourceClient.get(resourceUri, NotFoundException.class)
 
 176             Optional<ServiceProfiles> serviceProfilesOpt =wrapper.asBean(ServiceProfiles.class)
 
 177             if(serviceProfilesOpt.isPresent()){
 
 178                 ServiceProfiles serviceProfiles = serviceProfilesOpt.get()
 
 179                 ServiceProfile serviceProfile = serviceProfiles.getServiceProfile().get(0)
 
 180                 profileId = serviceProfile ? serviceProfile.getProfileId() : ""
 
 182             resourceUri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_PROFILE, globalSubscriberId, serviceType, serviceInstanceId, profileId)
 
 183             if (!resourceClient.exists(resourceUri)) {
 
 184                 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Service Instance was not found in aai")
 
 186             resourceClient.delete(resourceUri)
 
 190             String msg = "delete service profile from aai failed! cause-"+any.getCause()
 
 191             logger.error(any.printStackTrace())
 
 192             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg);
 
 196      void sendSyncError(DelegateExecution execution) {
 
 197         logger.info("Starting sendSyncError")
 
 201             if (execution.getVariable("WorkflowException") instanceof WorkflowException) {
 
 202                 WorkflowException wfe = execution.getVariable("WorkflowException")
 
 203                 errorMessage = wfe.getErrorMessage()
 
 205                 errorMessage = "Sending Sync Error."
 
 208             String buildworkflowException =
 
 209                     """<aetgt:WorkflowException xmlns:aetgt="http://org.onap/so/workflow/schema/v1">
 
 210                                         <aetgt:ErrorMessage>${MsoUtils.xmlEscape(errorMessage)}</aetgt:ErrorMessage>
 
 211                                         <aetgt:ErrorCode>7000</aetgt:ErrorCode>
 
 212                                    </aetgt:WorkflowException>"""
 
 214             logger.debug(buildworkflowException)
 
 215             sendWorkflowResponse(execution, 500, buildworkflowException)
 
 217         } catch (Exception ex) {
 
 218             logger.error("Sending Sync Error Activity Failed. " + "\n" + ex.getMessage())
 
 223     void prepareEndOperationStatus(DelegateExecution execution){
 
 224         logger.debug(" ======== STARTED prepareEndOperationStatus Process ======== ")
 
 226         execution.setVariable("progress", "100")
 
 227         execution.setVariable("result", "error")
 
 228         execution.setVariable("operationContent", "NSSMF Terminate service failure")
 
 230         WorkflowException wfex = execution.getVariable("WorkflowException") as WorkflowException
 
 231         String errorMessage = wfex.getErrorMessage()
 
 232         errorMessage = errorMessage.length() > 200 ? errorMessage.substring(0,200) + "......" : errorMessage
 
 233         execution.setVariable("reason",errorMessage)
 
 234         updateServiceOperationStatus(execution)
 
 236         logger.debug("======== COMPLETED prepareEndOperationStatus Process ======== ")
 
 240      * check parameters from request body
 
 244      * @param isErrorException
 
 247     private void checkAndSetRequestParam(String siRequest, String paraName, boolean isErrorException, DelegateExecution execution)
 
 250         String paramValue = jsonUtil.getJsonValue(siRequest, paraName)
 
 251         if (isBlank(paramValue)) {
 
 252             msg = "Input ${paraName} is null"
 
 256                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
 
 260             execution.setVariable(paraName, paramValue)