Support Activate,Deactivate and Terminate feature for NSMF based TN slices
[so.git] / bpmn / so-bpmn-infrastructure-common / src / main / groovy / org / onap / so / bpmn / infrastructure / scripts / DoDeallocateNSSI.groovy
index 0c795b3..daf5f46 100644 (file)
@@ -21,33 +21,26 @@ package org.onap.so.bpmn.infrastructure.scripts
 
 import com.fasterxml.jackson.databind.ObjectMapper
 import org.camunda.bpm.engine.delegate.DelegateExecution
-import org.onap.logging.filter.base.ONAPComponents
-import org.onap.so.beans.nsmf.DeAllocateNssi
-import org.onap.so.beans.nsmf.ServiceInfo
-import org.onap.so.beans.nsmf.EsrInfo
-import org.onap.so.beans.nsmf.JobStatusRequest
-import org.onap.so.beans.nsmf.JobStatusResponse
-import org.onap.so.beans.nsmf.NetworkType
-import org.onap.so.beans.nsmf.NssiDeAllocateRequest
-import org.onap.so.beans.nsmf.NssiResponse
+import org.onap.aai.domain.yang.SliceProfiles
+import org.onap.aaiclient.client.aai.entities.AAIResultWrapper
+import org.onap.aaiclient.client.aai.entities.uri.AAIPluralResourceUri
+import org.onap.aaiclient.client.aai.entities.uri.AAIResourceUri
+import org.onap.aaiclient.client.aai.entities.uri.AAISimpleUri
+import org.onap.aaiclient.client.aai.entities.uri.AAIUriFactory
+import org.onap.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder
+import org.onap.so.beans.nsmf.*
+import org.onap.so.beans.nsmf.oof.SubnetType
 import org.onap.so.bpmn.common.scripts.AbstractServiceTaskProcessor
 import org.onap.so.bpmn.common.scripts.ExceptionUtil
 import org.onap.so.bpmn.common.scripts.NssmfAdapterUtils
 import org.onap.so.bpmn.common.scripts.RequestDBUtil
-import org.onap.so.bpmn.core.UrnPropertiesReader
-import org.onap.so.bpmn.core.domain.ServiceArtifact
 import org.onap.so.bpmn.core.domain.ServiceDecomposition
 import org.onap.so.bpmn.core.json.JsonUtils
-import org.onap.so.client.HttpClient
-import org.onap.aaiclient.client.aai.AAIObjectType
-import org.onap.aaiclient.client.aai.entities.uri.AAIResourceUri
-import org.onap.aaiclient.client.aai.entities.uri.AAIUriFactory
 import org.onap.so.db.request.beans.OperationStatus
 import org.slf4j.Logger
 import org.slf4j.LoggerFactory
-import org.json.JSONObject
-import javax.ws.rs.core.Response
 
+import javax.ws.rs.NotFoundException
 
 class DoDeallocateNSSI extends AbstractServiceTaskProcessor
 {
@@ -55,6 +48,7 @@ class DoDeallocateNSSI extends AbstractServiceTaskProcessor
 
     private ExceptionUtil exceptionUtil = new ExceptionUtil()
     private JsonUtils jsonUtil = new JsonUtils()
+    ObjectMapper objectMapper = new ObjectMapper()
     private RequestDBUtil requestDBUtil = new RequestDBUtil()
     private NssmfAdapterUtils nssmfAdapterUtils = new NssmfAdapterUtils(httpClientFactory, jsonUtil)
 
@@ -110,15 +104,13 @@ class DoDeallocateNSSI extends AbstractServiceTaskProcessor
 
         try {
             ServiceDecomposition serviceDecomposition = execution.getVariable("serviceDecomposition") as ServiceDecomposition
-            ServiceArtifact serviceArtifact = serviceDecomposition ?.getServiceInfo()?.getServiceArtifact()?.get(0)
-            String content = serviceArtifact.getContent()
-            String vendor = jsonUtil.getJsonValue(content, "metadata.vendor")
-            String domainType  = jsonUtil.getJsonValue(content, "metadata.domainType")
+            String vendor = serviceDecomposition.getServiceRole()
+            NetworkType domainType = convertServiceCategory(serviceDecomposition.getServiceCategory())
 
             def currentNSSI = execution.getVariable("currentNSSI")
             currentNSSI['vendor'] = vendor
             currentNSSI['domainType'] = domainType
-            LOGGER.info("processDecomposition, current vendor-domainType:" +String.join("-", vendor, domainType))
+            LOGGER.info("processDecomposition, current vendor-domainType:" +String.join("-", vendor, domainType.toString()))
 
         } catch (any) {
             String exceptionMessage = "Bpmn error encountered in deallocate nssi. processDecomposition() - " + any.getMessage()
@@ -127,6 +119,30 @@ class DoDeallocateNSSI extends AbstractServiceTaskProcessor
         }
         LOGGER.debug("*****${PREFIX} Exit processDecomposition *****")
     }
+
+
+    /**
+     * get subnetType from serviceCategory
+     * @return
+     */
+    private NetworkType convertServiceCategory(String serviceCategory){
+        if(serviceCategory ==~ /CN.*/){
+            return SubnetType.CN.getNetworkType()
+        }
+        if (serviceCategory ==~ /AN.*/){
+            return SubnetType.AN.getNetworkType()
+        }
+        if (serviceCategory ==~ /TN.*BH.*/){
+            return SubnetType.TN_BH.getNetworkType()
+        }
+        if(serviceCategory ==~ /TN.*MH.*/){
+            return SubnetType.TN_MH.getNetworkType()
+        }
+       if(serviceCategory ==~ /TN.*FH.*/){
+           return SubnetType.TN_FH.getNetworkType()
+       }
+        return null
+    }
     
     /**
      * send deallocate request to nssmf
@@ -141,12 +157,11 @@ class DoDeallocateNSSI extends AbstractServiceTaskProcessor
         String nssiId = currentNSSI['nssiServiceInstanceId']
         String nsiId = currentNSSI['nsiServiceInstanceId']
         String scriptName = execution.getVariable("scriptName")
-        boolean modifyAction = execution.getVariable("terminateNSI")
 
         String serviceInvariantUuid = currentNSSI['modelInvariantId']
         String serviceUuid = currentNSSI['modelVersionId']
         String globalSubscriberId = currentNSSI['globalSubscriberId']
-        String subscriptionServiceType = execution.getVariable("serviceType")
+        String subscriptionServiceType = currentNSSI['serviceType']
         
         DeAllocateNssi deAllocateNssi = new DeAllocateNssi()
         deAllocateNssi.setNsiId(nsiId)
@@ -155,36 +170,38 @@ class DoDeallocateNSSI extends AbstractServiceTaskProcessor
         deAllocateNssi.setSnssaiList(Arrays.asList(snssai))
         deAllocateNssi.setScriptName(scriptName)
         deAllocateNssi.setSliceProfileId(profileId)
-        deAllocateNssi.setModifyAction(modifyAction)
         
         ServiceInfo serviceInfo = new ServiceInfo()
         serviceInfo.setServiceInvariantUuid(serviceInvariantUuid)
         serviceInfo.setServiceUuid(serviceUuid)
+        serviceInfo.setNsiId(nsiId)
+        serviceInfo.setNssiId(nssiId)
         serviceInfo.setGlobalSubscriberId(globalSubscriberId)
         serviceInfo.setSubscriptionServiceType(subscriptionServiceType)
+        String serviceInfoString = objectMapper.writeValueAsString(serviceInfo)
         
         EsrInfo esrInfo = getEsrInfo(currentNSSI)
+        String esrInfoString = objectMapper.writeValueAsString(esrInfo)
         
         execution.setVariable("deAllocateNssi",deAllocateNssi)
-        execution.setVariable("esrInfo",esrInfo)
-        execution.setVariable("serviceInfo",serviceInfo)
+        execution.setVariable("esrInfo", esrInfoString)
+        execution.setVariable("serviceInfo", serviceInfoString)
         String nssmfRequest = """
                 {
-                  "deAllocateNssi": "${execution.getVariable("deAllocateNssi") as JSONObject}",
-                  "esrInfo":  ${execution.getVariable("esrInfo") as JSONObject},
-                  "serviceInfo": ${execution.getVariable("serviceInfo") as JSONObject}
+                  "deAllocateNssi": ${objectMapper.writeValueAsString(deAllocateNssi)},
+                  "esrInfo":  ${esrInfoString},
+                  "serviceInfo": ${serviceInfoString}
                 }
               """
 
-        String urlStr = String.format("/api/rest/provMns/v1/NSS/nssi/%s",nssiId)
+        String urlStr = String.format("/api/rest/provMns/v1/NSS/SliceProfiles/%s", profileId)
 
         NssiResponse nssmfResponse = nssmfAdapterUtils.sendPostRequestNSSMF(execution, urlStr, nssmfRequest, NssiResponse.class)
         if (nssmfResponse != null) {
             currentNSSI['jobId']= nssmfResponse.getJobId() ?: "" 
             currentNSSI['jobProgress'] = 0            
             execution.setVariable("currentNSSI", currentNSSI)    
-            } 
-            else {
+        } else {
             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, "Received a Bad Response from NSSMF.")
         }
         LOGGER.debug("*****${PREFIX} Exit sendRequestToNSSMF *****")
@@ -198,32 +215,7 @@ class DoDeallocateNSSI extends AbstractServiceTaskProcessor
     {
         def currentNSSI = execution.getVariable("currentNSSI")
         String jobId = currentNSSI['jobId']
-        String nssiId = currentNSSI['nssiServiceInstanceId']
-        String nsiId = currentNSSI['nsiServiceInstanceId']
-        String serviceInvariantUuid = currentNSSI['modelInvariantId']
-        String serviceUuid = currentNSSI['modelVersionId']
-        String globalSubscriberId = currentNSSI['globalSubscriberId']
-        String subscriptionServiceType = execution.getVariable("serviceType")
-        String sST =  currentNSSI['sST']
-        String PLMNIdList = currentNSSI['PLMNIdList']
-        String nssiName = currentNSSI['nssiName']
-        
-        execution.setVariable("responseId", "3")
-        execution.setVariable("esrInfo", getEsrInfo(currentNSSI))
         execution.setVariable("jobId", jobId)
-        
-        Map<String, ?> serviceInfoMap = new HashMap<>()
-        serviceInfoMap.put("nssiId", nssiId)
-        serviceInfoMap.put("nsiId", nsiId)
-        serviceInfoMap.put("nssiName", nssiName)
-        serviceInfoMap.put("sST", sST)
-        serviceInfoMap.put("PLMNIdList", PLMNIdList)
-        serviceInfoMap.put("globalSubscriberId", globalSubscriberId)
-        serviceInfoMap.put("subscriptionServiceType", subscriptionServiceType)
-        serviceInfoMap.put("serviceInvariantUuid", serviceInvariantUuid)
-        serviceInfoMap.put("serviceUuid", serviceUuid)
-    
-        execution.setVariable("serviceInfo", serviceInfoMap)
     }
 
     
@@ -246,8 +238,9 @@ class DoDeallocateNSSI extends AbstractServiceTaskProcessor
                 LOGGER.error("job progress is null or empty!")
                 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, "Received a Bad Job progress from NSSMF.")
             }
+            def currentNSSI = execution.getVariable("currentNSSI")
             int oldProgress = currentNSSI['jobProgress']
-            int currentProgress = progress
+            int currentProgress = Integer.parseInt(progress)
 
             execution.setVariable("isNSSIDeAllocated", (currentProgress == 100))
             execution.setVariable("isNeedUpdateDB", (oldProgress != currentProgress))
@@ -255,6 +248,8 @@ class DoDeallocateNSSI extends AbstractServiceTaskProcessor
             currentNSSI['status'] = status
             currentNSSI['statusDescription'] = statusDescription
 
+            String nssiId = currentNSSI['nssiServiceInstanceId']
+            String nsiId = currentNSSI['nsiServiceInstanceId']
             LOGGER.debug("job status result: nsiId = ${nsiId}, nssiId=${nssiId}, oldProgress=${oldProgress}, progress = ${currentProgress}" )
         }
           else {
@@ -264,7 +259,7 @@ class DoDeallocateNSSI extends AbstractServiceTaskProcessor
         }
         catch (any)
         {
-            String msg = "Received a Bad Response from NSSMF." cause-"+any.getCause()"
+            String msg = "Received a Bad Response from NSSMF. cause-"+any.getCause()
             LOGGER.error(any.printStackTrace())
             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
         }
@@ -272,11 +267,11 @@ class DoDeallocateNSSI extends AbstractServiceTaskProcessor
 
     private EsrInfo getEsrInfo(def currentNSSI)
     {
-        String domaintype = currentNSSI['domainType']
+        NetworkType domainType = currentNSSI['domainType']
         String vendor = currentNSSI['vendor']
         
         EsrInfo info = new EsrInfo()
-        info.setNetworkType(NetworkType.fromString(domaintype))
+        info.setNetworkType(domainType)
         info.setVendor(vendor)
         return info
     }
@@ -291,17 +286,17 @@ class DoDeallocateNSSI extends AbstractServiceTaskProcessor
         def currentNSSI = execution.getVariable("currentNSSI")
         int currentProgress = currentNSSI["jobProgress"]
         def proportion = currentNSSI['proportion']
-        def statusDes = currentNSSI["statusDescription"]
         int progress = (currentProgress as int) == 0 ? 0 : (currentProgress as int) / 100 * (proportion as int)
         def status = currentNSSI['status']
-        
+
+
         OperationStatus operationStatus = new OperationStatus()
         operationStatus.setServiceId(currentNSSI['e2eServiceInstanceId'] as String)
         operationStatus.setOperationId(currentNSSI['operationId'] as String)
         operationStatus.setOperation("DELETE")
-        operationStatus.setResult(status as String)
+        operationStatus.setResult("processing")
         operationStatus.setProgress(progress as String)
-        operationStatus.setOperationContent(statusDes as String)
+        operationStatus.setOperationContent(currentNSSI['domainType'].toString() + " " + status.toString())
         requestDBUtil.prepareUpdateOperationStatus(execution, operationStatus)
         LOGGER.debug("update operation, currentProgress=${currentProgress}, proportion=${proportion}, progress = ${progress}" )
     }
@@ -310,19 +305,20 @@ class DoDeallocateNSSI extends AbstractServiceTaskProcessor
      * delete slice profile from aai
      * @param execution
      */
-    void delSliceProfileFromAAI(DelegateExecution execution)
+    void delSliceProfileServiceFromAAI(DelegateExecution execution)
     {
         LOGGER.debug("*****${PREFIX} start delSliceProfileFromAAI *****")
         def currentNSSI = execution.getVariable("currentNSSI")
         String nssiServiceInstanceId = currentNSSI['nssiServiceInstanceId']
         String profileId = currentNSSI['profileId']
         String globalSubscriberId = currentNSSI["globalSubscriberId"]
-        String serviceType = currentNSSI["serviceType"]
+        String serviceType = currentNSSI['serviceType']
 
         try
         {
             LOGGER.debug("delete nssiServiceInstanceId:${nssiServiceInstanceId}, profileId:${profileId}")
-            AAIResourceUri resourceUri = AAIUriFactory.createResourceUri(AAIObjectType.SLICE_PROFILE, globalSubscriberId, serviceType, nssiServiceInstanceId, profileId)
+            AAIResourceUri resourceUri = AAIUriFactory.createResourceUri(
+                AAIFluentTypeBuilder.business().customer(globalSubscriberId).serviceSubscription(serviceType).serviceInstance(profileId))
             if (!getAAIClient().exists(resourceUri)) {
                 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Service Instance was not found in aai")
             }
@@ -336,4 +332,41 @@ class DoDeallocateNSSI extends AbstractServiceTaskProcessor
         }
         LOGGER.debug("*****${PREFIX} Exist delSliceProfileFromAAI *****")
     }
+
+    void delSliceProfileFromAAI(DelegateExecution execution){
+
+        LOGGER.debug("*****${PREFIX} start delSliceProfileFromAAI *****")
+        def currentNSSI = execution.getVariable("currentNSSI")
+        String globalSubscriberId = currentNSSI["globalSubscriberId"]
+        String serviceType = currentNSSI['serviceType']
+        String sliceProfileInstId = currentNSSI['profileId']
+
+        try
+        {
+            AAIPluralResourceUri resourceUri = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business().customer(globalSubscriberId).serviceSubscription(serviceType).serviceInstance(sliceProfileInstId).sliceProfiles())
+            AAIResultWrapper wrapper = getAAIClient().get(resourceUri, NotFoundException.class)
+            Optional<SliceProfiles> sliceProfilesOpt =wrapper.asBean(SliceProfiles.class)
+            SliceProfiles sliceProfiles
+            String profileId
+            if(sliceProfilesOpt.isPresent()){
+                sliceProfiles = sliceProfilesOpt.get()
+                org.onap.aai.domain.yang.SliceProfile sliceProfile = sliceProfiles.getSliceProfile().get(0)
+                profileId = sliceProfile ? sliceProfile.getProfileId() : ""
+            }
+            if (profileId){
+                AAISimpleUri profileUri = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business().customer(globalSubscriberId).serviceSubscription(serviceType).serviceInstance(sliceProfileInstId).sliceProfile(profileId))
+                if (!getAAIClient().exists(profileUri)) {
+                    exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Service Instance was not found in aai")
+                }
+                getAAIClient().delete(profileUri)
+            }
+
+        }
+        catch (any)
+        {
+            String msg = "delete service profile from aai failed! cause-"+any.getCause()
+            LOGGER.error(any.printStackTrace())
+            exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg);
+        }
+    }
 }