Add script for delete slice service 00/102800/1
authorzm330 <zhangminyj@chinamobile.com>
Tue, 3 Mar 2020 02:55:27 +0000 (10:55 +0800)
committerzm330 <zhangminyj@chinamobile.com>
Tue, 3 Mar 2020 02:55:36 +0000 (10:55 +0800)
Issue-ID: SO-2368

Signed-off-by: zm330 <zhangminyj@chinamobile.com>
Change-Id: If8179344b10b9a31845fba6d6c725c3eb5cb0add

bpmn/so-bpmn-infrastructure-common/src/main/groovy/org/onap/so/bpmn/infrastructure/scripts/DeleteSliceService.groovy [new file with mode: 0644]

diff --git a/bpmn/so-bpmn-infrastructure-common/src/main/groovy/org/onap/so/bpmn/infrastructure/scripts/DeleteSliceService.groovy b/bpmn/so-bpmn-infrastructure-common/src/main/groovy/org/onap/so/bpmn/infrastructure/scripts/DeleteSliceService.groovy
new file mode 100644 (file)
index 0000000..f0d43cf
--- /dev/null
@@ -0,0 +1,263 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * ONAP - SO
+ * ================================================================================
+ * Copyright (c) 2019, CMCC Technologies Co., Ltd.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.so.bpmn.infrastructure.scripts
+
+import org.camunda.bpm.engine.delegate.BpmnError
+import org.camunda.bpm.engine.delegate.DelegateExecution
+import org.onap.aai.domain.yang.ServiceProfile
+import org.onap.aai.domain.yang.ServiceProfiles
+import org.onap.so.bpmn.common.scripts.AbstractServiceTaskProcessor
+import org.onap.so.bpmn.common.scripts.ExceptionUtil
+import org.onap.so.bpmn.common.scripts.MsoUtils
+import org.onap.so.bpmn.common.scripts.RequestDBUtil
+import org.onap.so.bpmn.core.WorkflowException
+import org.onap.so.bpmn.core.json.JsonUtils
+import org.onap.so.client.aai.AAIObjectType
+import org.onap.so.client.aai.AAIResourcesClient
+import org.onap.so.client.aai.entities.AAIResultWrapper
+import org.onap.so.client.aai.entities.uri.AAIResourceUri
+import org.onap.so.client.aai.entities.uri.AAIUriFactory
+import org.onap.so.db.request.beans.OperationStatus
+import org.slf4j.Logger
+import org.slf4j.LoggerFactory
+
+import javax.ws.rs.NotFoundException
+
+import static org.apache.commons.lang3.StringUtils.isBlank
+
+class DeleteSliceService extends AbstractServiceTaskProcessor {
+
+    String Prefix="DELSS_"
+    ExceptionUtil exceptionUtil = new ExceptionUtil()
+    JsonUtils jsonUtil = new JsonUtils()
+    private RequestDBUtil requestDBUtil = new RequestDBUtil()
+
+    private static final Logger logger = LoggerFactory.getLogger( DeleteSliceService.class)
+
+    @Override
+    void preProcessRequest(DelegateExecution execution) {
+        execution.setVariable("prefix", Prefix)
+        String msg = ""
+
+        logger.trace("Starting preProcessRequest")
+
+        try {
+            // check for incoming json message/input
+            String siRequest = execution.getVariable("bpmnRequest")
+            String requestId = execution.getVariable("mso-request-id")
+            execution.setVariable("msoRequestId", requestId)
+
+            //e2eslice-service instance id
+            String serviceInstanceId = execution.getVariable("serviceInstanceId")
+            if (isBlank(serviceInstanceId)) {
+                msg = "e2eslice-service id is null"
+                exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
+            }
+            logger.info("Input Request: ${siRequest}, reqId: ${requestId}, e2eslice-service: ${serviceInstanceId}")
+
+            //subscriberInfo
+            checkAndSetRequestParam(siRequest,"globalSubscriberId",false, execution)
+            checkAndSetRequestParam(siRequest,"serviceType",false, execution)
+            checkAndSetRequestParam(siRequest,"operationId",false, execution)
+
+            //prepare init operation status
+            execution.setVariable("progress", "0")
+            execution.setVariable("result", "processing")
+            execution.setVariable("operationType", "DELETE")
+            execution.setVariable("operationContent", "Prepare init service")
+            updateServiceOperationStatus(execution)
+
+        } catch (BpmnError e) {
+            throw e
+        } catch (Exception ex) {
+            msg = "Exception in preProcessRequest " + ex.getMessage()
+            logger.debug(msg)
+            exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
+        }
+        logger.trace("Exit preProcessRequest")
+    }
+
+    /**
+     * send asynchronous response
+     * @param execution
+     */
+    void sendAsyncResponse(DelegateExecution execution) {
+        logger.trace("Staring sendSyncResponse")
+
+        try {
+            String operationId = execution.getVariable("operationId")
+            String syncResponse = """{"operationId":"${operationId}"}""".trim()
+            logger.info("sendSynchResponse: xmlSyncResponse - " + "\n" + syncResponse)
+            sendWorkflowResponse(execution, 202, syncResponse)
+
+        } catch (Exception ex) {
+            String msg  = "Exception in sendSyncResponse: " + ex.getMessage()
+            exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
+        }
+        logger.trace("Exit sendSyncResponse")
+    }
+
+    /**
+     * Deletes the slice service instance in aai
+     */
+    void deleteSliceServiceInstance(DelegateExecution execution) {
+        logger.trace("Entered deleteSliceServiceInstance")
+        try {
+
+            AAIResourcesClient resourceClient = new AAIResourcesClient()
+            AAIResourceUri serviceInstanceUri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, execution.getVariable("globalSubscriberId"), execution.getVariable("serviceType"), execution.getVariable("serviceInstanceId"))
+            resourceClient.delete(serviceInstanceUri)
+
+            execution.setVariable("progress", "100")
+            execution.setVariable("result", "finished")
+            execution.setVariable("operationContent", "NSMF completes slicing service termination.")
+            updateServiceOperationStatus(execution)
+
+            logger.trace("Exited deleteSliceServiceInstance")
+        }catch(Exception e){
+            logger.debug("Error occured within deleteSliceServiceInstance method: " + e)
+            exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Error occured during deleteSliceServiceInstance from aai")
+        }
+    }
+
+    /**
+     * update operation status
+     * @param execution
+     */
+    private void updateServiceOperationStatus(DelegateExecution execution){
+
+        OperationStatus operationStatus = new OperationStatus()
+        operationStatus.setServiceId(execution.getVariable("serviceInstanceId"))
+        operationStatus.setOperationId(execution.getVariable("operationId"))
+        operationStatus.setUserId(execution.getVariable("globalSubscriberId"))
+        operationStatus.setResult(execution.getVariable("result"))
+        operationStatus.setProgress(execution.getVariable("progress"))
+        operationStatus.setOperationContent(execution.getVariable("operationContent"))
+        operationStatus.setReason(execution.getVariable("reason"))
+        operationStatus.setOperation(execution.getVariable("operationType"))
+
+        requestDBUtil.prepareUpdateOperationStatus(execution, operationStatus)
+    }
+
+    /**
+     * delete service profile from aai
+     * @param execution
+     */
+    private void delServiceProfileFromAAI(DelegateExecution execution)
+    {
+        String globalSubscriberId = execution.getVariable("globalSubscriberId")
+        String serviceType = execution.getVariable("serviceType")
+        String serviceInstanceId = execution.getVariable("serviceInstanceId")
+        String profileId = ""
+
+        try
+        {
+            AAIResourcesClient resourceClient = new AAIResourcesClient()
+            AAIResourceUri resourceUri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_PROFILE_ALL, globalSubscriberId, serviceType, serviceInstanceId)
+            AAIResultWrapper wrapper = resourceClient.get(resourceUri, NotFoundException.class)
+            Optional<ServiceProfiles> serviceProfilesOpt =wrapper.asBean(ServiceProfiles.class)
+            if(serviceProfilesOpt.isPresent()){
+                ServiceProfiles serviceProfiles = serviceProfilesOpt.get()
+                ServiceProfile serviceProfile = serviceProfiles.getServiceProfile().get(0)
+                profileId = serviceProfile ? serviceProfile.getProfileId() : ""
+            }
+            resourceUri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_PROFILE, globalSubscriberId, serviceType, serviceInstanceId, profileId)
+            if (!resourceClient.exists(resourceUri)) {
+                exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Service Instance was not found in aai")
+            }
+            resourceClient.delete(resourceUri)
+        }
+        catch (any)
+        {
+            String msg = "delete service profile from aai failed! cause-"+any.getCause()
+            logger.error(any.printStackTrace())
+            exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg);
+        }
+    }
+
+     void sendSyncError(DelegateExecution execution) {
+        logger.info("Starting sendSyncError")
+
+        try {
+            String errorMessage
+            if (execution.getVariable("WorkflowException") instanceof WorkflowException) {
+                WorkflowException wfe = execution.getVariable("WorkflowException")
+                errorMessage = wfe.getErrorMessage()
+            } else {
+                errorMessage = "Sending Sync Error."
+            }
+
+            String buildworkflowException =
+                    """<aetgt:WorkflowException xmlns:aetgt="http://org.onap/so/workflow/schema/v1">
+                                       <aetgt:ErrorMessage>${MsoUtils.xmlEscape(errorMessage)}</aetgt:ErrorMessage>
+                                       <aetgt:ErrorCode>7000</aetgt:ErrorCode>
+                                  </aetgt:WorkflowException>"""
+
+            logger.debug(buildworkflowException)
+            sendWorkflowResponse(execution, 500, buildworkflowException)
+
+        } catch (Exception ex) {
+            logger.error("Sending Sync Error Activity Failed. " + "\n" + ex.getMessage())
+        }
+
+    }
+
+    void prepareEndOperationStatus(DelegateExecution execution){
+        logger.debug(" ======== STARTED prepareEndOperationStatus Process ======== ")
+
+        execution.setVariable("progress", "100")
+        execution.setVariable("result", "error")
+        execution.setVariable("operationContent", "NSSMF Terminate service failure")
+
+        WorkflowException wfex = execution.getVariable("WorkflowException") as WorkflowException
+        String errorMessage = wfex.getErrorMessage()
+        errorMessage = errorMessage.length() > 200 ? errorMessage.substring(0,200) + "......" : errorMessage
+        execution.setVariable("reason",errorMessage)
+        updateServiceOperationStatus(execution)
+
+        logger.debug("======== COMPLETED prepareEndOperationStatus Process ======== ")
+    }
+
+    /**
+     * check parameters from request body
+     * set to execution
+     * @param siRequest
+     * @param paraName
+     * @param isErrorException
+     * @param execution
+     */
+    private void checkAndSetRequestParam(String siRequest, String paraName, boolean isErrorException, DelegateExecution execution)
+    {
+        String msg = ""
+        String paramValue = jsonUtil.getJsonValue(siRequest, paraName)
+        if (isBlank(paramValue)) {
+            msg = "Input ${paraName} is null"
+            logger.error(msg)
+            if(isErrorException)
+            {
+                exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
+            }
+
+        } else {
+            execution.setVariable(paraName, paramValue)
+        }
+    }
+}