2  * ============LICENSE_START=======================================================
 
   4  * ================================================================================
 
   5  # Copyright (c) 2019, CMCC Technologies Co., Ltd.
 
   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=========================================================
 
  20 package org.onap.so.bpmn.infrastructure.scripts
 
  22 import com.fasterxml.jackson.databind.ObjectMapper
 
  23 import org.camunda.bpm.engine.delegate.DelegateExecution
 
  24 import org.onap.logging.filter.base.ONAPComponents
 
  25 import org.onap.so.beans.nsmf.DeAllocateNssi
 
  26 import org.onap.so.beans.nsmf.EsrInfo
 
  27 import org.onap.so.beans.nsmf.JobStatusRequest
 
  28 import org.onap.so.beans.nsmf.JobStatusResponse
 
  29 import org.onap.so.beans.nsmf.NetworkType
 
  30 import org.onap.so.beans.nsmf.NssiDeAllocateRequest
 
  31 import org.onap.so.beans.nsmf.NssiResponse
 
  32 import org.onap.so.bpmn.common.scripts.AbstractServiceTaskProcessor
 
  33 import org.onap.so.bpmn.common.scripts.ExceptionUtil
 
  34 import org.onap.so.bpmn.common.scripts.NssmfAdapterUtils
 
  35 import org.onap.so.bpmn.common.scripts.RequestDBUtil
 
  36 import org.onap.so.bpmn.core.UrnPropertiesReader
 
  37 import org.onap.so.bpmn.core.domain.ServiceArtifact
 
  38 import org.onap.so.bpmn.core.domain.ServiceDecomposition
 
  39 import org.onap.so.bpmn.core.json.JsonUtils
 
  40 import org.onap.so.client.HttpClient
 
  41 import org.onap.aaiclient.client.aai.AAIObjectType
 
  42 import org.onap.aaiclient.client.aai.entities.uri.AAIResourceUri
 
  43 import org.onap.aaiclient.client.aai.entities.uri.AAIUriFactory
 
  44 import org.onap.so.db.request.beans.OperationStatus
 
  45 import org.slf4j.Logger
 
  46 import org.slf4j.LoggerFactory
 
  48 import javax.ws.rs.core.Response
 
  51 class DoDeallocateNSSI extends AbstractServiceTaskProcessor
 
  53     private final String PREFIX ="DoDeallocateNSSI"
 
  55     private ExceptionUtil exceptionUtil = new ExceptionUtil()
 
  56     private JsonUtils jsonUtil = new JsonUtils()
 
  57     private RequestDBUtil requestDBUtil = new RequestDBUtil()
 
  58     private NssmfAdapterUtils nssmfAdapterUtils = new NssmfAdapterUtils(httpClientFactory, jsonUtil)
 
  60     private static final Logger LOGGER = LoggerFactory.getLogger( DoDeallocateNSSI.class)
 
  63     void preProcessRequest(DelegateExecution execution) {
 
  64         LOGGER.trace(" ***** ${PREFIX} Start preProcessRequest *****")
 
  66         def currentNSSI = execution.getVariable("currentNSSI")
 
  68             String msg = "currentNSSI is null"
 
  70             exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
 
  73         LOGGER.trace("***** ${PREFIX} Exit preProcessRequest *****")
 
  80     void prepareDecomposeService(DelegateExecution execution)
 
  82         LOGGER.trace(" *****${PREFIX} Start prepareDecomposeService *****")
 
  85             def currentNSSI = execution.getVariable("currentNSSI")
 
  86             String modelInvariantUuid = currentNSSI['modelInvariantId']
 
  87             String modelVersionId = currentNSSI['modelVersionId']
 
  88             String serviceModelInfo = """{
 
  89             "modelInvariantUuid":"${modelInvariantUuid}",
 
  90             "modelUuid":"${modelVersionId}",
 
  93             execution.setVariable("serviceModelInfo", serviceModelInfo)
 
  97             String exceptionMessage = "Bpmn error encountered in deallocate nssi. Unexpected Error from method prepareDecomposeService() - " + any.getMessage()
 
  98             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)
 
 100         LOGGER.debug(" ***** ${PREFIX} Exit prepareDecomposeService *****")
 
 107     void processDecomposition(DelegateExecution execution) {
 
 108         LOGGER.debug("*****${PREFIX} start processDecomposition *****")
 
 111             ServiceDecomposition serviceDecomposition = execution.getVariable("serviceDecomposition") as ServiceDecomposition
 
 112             ServiceArtifact serviceArtifact = serviceDecomposition ?.getServiceInfo()?.getServiceArtifact()?.get(0)
 
 113             String content = serviceArtifact.getContent()
 
 114             String vendor = jsonUtil.getJsonValue(content, "metadata.vendor")
 
 115             String domainType  = jsonUtil.getJsonValue(content, "metadata.domainType")
 
 117             def currentNSSI = execution.getVariable("currentNSSI")
 
 118             currentNSSI['vendor'] = vendor
 
 119             currentNSSI['domainType'] = domainType
 
 120             LOGGER.info("processDecomposition, current vendor-domainType:" +String.join("-", vendor, domainType))
 
 123             String exceptionMessage = "Bpmn error encountered in deallocate nssi. processDecomposition() - " + any.getMessage()
 
 124             LOGGER.debug(exceptionMessage)
 
 125             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)
 
 127         LOGGER.debug("*****${PREFIX} Exit processDecomposition *****")
 
 131      * send deallocate request to nssmf
 
 134     void sendRequestToNSSMF(DelegateExecution execution)
 
 136         LOGGER.debug("*****${PREFIX} start sendRequestToNSSMF *****")
 
 137         def currentNSSI = execution.getVariable("currentNSSI")
 
 138         String snssai= currentNSSI['snssai']
 
 139         String profileId = currentNSSI['profileId']
 
 140         String nssiId = currentNSSI['nssiServiceInstanceId']
 
 141         String nsiId = currentNSSI['nsiServiceInstanceId']
 
 142         String scriptName = execution.getVariable("scriptName")
 
 144         DeAllocateNssi deAllocateNssi = new DeAllocateNssi()
 
 145         deAllocateNssi.setNsiId(nsiId)
 
 146         deAllocateNssi.setNssiId(nssiId)
 
 147         deAllocateNssi.setTerminateNssiOption(0)
 
 148         deAllocateNssi.setSnssaiList(Arrays.asList(snssai))
 
 149         deAllocateNssi.setScriptName(scriptName)
 
 151         NssiDeAllocateRequest deAllocateRequest = new NssiDeAllocateRequest()
 
 152         deAllocateRequest.setDeAllocateNssi(deAllocateNssi)
 
 153         deAllocateRequest.setEsrInfo(getEsrInfo(currentNSSI))
 
 155         ObjectMapper mapper = new ObjectMapper()
 
 156         String nssmfRequest = mapper.writeValueAsString(deAllocateRequest)
 
 158         String urlStr = String.format("/api/rest/provMns/v1/NSS/SliceProfiles/%s",profileId)
 
 160         NssiResponse nssmfResponse = nssmfAdapterUtils.sendPostRequestNSSMF(execution, urlStr, nssmfRequest, NssiResponse.class)
 
 161         if (nssmfResponse != null) {
 
 162             currentNSSI['jobId']= nssmfResponse.getJobId() ?: ""
 
 163             currentNSSI['jobProgress'] = 0
 
 164             execution.setVariable("currentNSSI", currentNSSI)
 
 166             LOGGER.debug("*****${PREFIX} Exit sendRequestToNSSMF *****")
 
 168             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, "Received a Bad Response from NSSMF.")
 
 174      * send to nssmf query progress
 
 177     void getJobStatus(DelegateExecution execution)
 
 179         def currentNSSI = execution.getVariable("currentNSSI")
 
 180         String jobId = currentNSSI['jobId']
 
 181         String nssiId = currentNSSI['nssiServiceInstanceId']
 
 182         String nsiId = currentNSSI['nsiServiceInstanceId']
 
 184         JobStatusRequest jobStatusRequest = new JobStatusRequest()
 
 185         jobStatusRequest.setNssiId(nssiId)
 
 186         jobStatusRequest.setNsiId(nsiId)
 
 187         jobStatusRequest.setEsrInfo(getEsrInfo(currentNSSI))
 
 189         ObjectMapper mapper = new ObjectMapper()
 
 190         String nssmfRequest = mapper.writeValueAsString(jobStatusRequest)
 
 192         String urlStr = String.format("/api/rest/provMns/v1/NSS/jobs/%s", jobId)
 
 194         JobStatusResponse jobStatusResponse = nssmfAdapterUtils.sendPostRequestNSSMF(execution, urlStr, nssmfRequest, JobStatusResponse.class)
 
 196         if (jobStatusResponse != null) {
 
 197             def progress = jobStatusResponse?.getResponseDescriptor()?.getProgress()
 
 200                 LOGGER.error("job progress is null or empty!")
 
 201                 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, "Received a Bad Job progress from NSSMF.")
 
 203             int oldProgress = currentNSSI['jobProgress']
 
 204             int currentProgress = progress
 
 206             execution.setVariable("isNSSIDeAllocated", (currentProgress == 100))
 
 207             execution.setVariable("isNeedUpdateDB", (oldProgress != currentProgress))
 
 208             currentNSSI['jobProgress'] = currentProgress
 
 210             def statusDescription = jobStatusResponse?.getResponseDescriptor()?.getStatusDescription()
 
 211             currentNSSI['statusDescription'] = statusDescription
 
 213             LOGGER.debug("job status result: nsiId = ${nsiId}, nssiId=${nssiId}, oldProgress=${oldProgress}, progress = ${currentProgress}" )
 
 216             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, "Received a Bad Response from NSSMF.")
 
 221     private EsrInfo getEsrInfo(def currentNSSI)
 
 223         String domaintype = currentNSSI['domainType']
 
 224         String vendor = currentNSSI['vendor']
 
 226         EsrInfo info = new EsrInfo()
 
 227         info.setNetworkType(NetworkType.fromString(domaintype))
 
 228         info.setVendor(vendor)
 
 234      * prepare update requestdb
 
 237     void handleJobStatus(DelegateExecution execution)
 
 239         def currentNSSI = execution.getVariable("currentNSSI")
 
 240         int currentProgress = currentNSSI["jobProgress"]
 
 241         def proportion = currentNSSI['proportion']
 
 242         def statusDes = currentNSSI["statusDescription"]
 
 243         int progress = (currentProgress as int) == 0 ? 0 : (currentProgress as int) / 100 * (proportion as int)
 
 245         OperationStatus operationStatus = new OperationStatus()
 
 246         operationStatus.setServiceId(currentNSSI['e2eServiceInstanceId'] as String)
 
 247         operationStatus.setOperationId(currentNSSI['operationId'] as String)
 
 248         operationStatus.setOperation("DELETE")
 
 249         operationStatus.setResult("processing")
 
 250         operationStatus.setProgress(progress as String)
 
 251         operationStatus.setOperationContent(statusDes as String)
 
 252         requestDBUtil.prepareUpdateOperationStatus(execution, operationStatus)
 
 253         LOGGER.debug("update operation, currentProgress=${currentProgress}, proportion=${proportion}, progress = ${progress}" )
 
 256     void timeDelay(DelegateExecution execution) {
 
 259         } catch(InterruptedException e) {
 
 260             LOGGER.error("Time Delay exception" + e)
 
 265      * delete slice profile from aai
 
 268     void delSliceProfileFromAAI(DelegateExecution execution)
 
 270         LOGGER.debug("*****${PREFIX} start delSliceProfileFromAAI *****")
 
 271         def currentNSSI = execution.getVariable("currentNSSI")
 
 272         String nssiServiceInstanceId = currentNSSI['nssiServiceInstanceId']
 
 273         String profileId = currentNSSI['profileId']
 
 274         String globalSubscriberId = currentNSSI["globalSubscriberId"]
 
 275         String serviceType = currentNSSI["serviceType"]
 
 279             LOGGER.debug("delete nssiServiceInstanceId:${nssiServiceInstanceId}, profileId:${profileId}")
 
 280             AAIResourceUri resourceUri = AAIUriFactory.createResourceUri(AAIObjectType.SLICE_PROFILE, globalSubscriberId, serviceType, nssiServiceInstanceId, profileId)
 
 281             if (!getAAIClient().exists(resourceUri)) {
 
 282                 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Service Instance was not found in aai")
 
 284             getAAIClient().delete(resourceUri)
 
 288             String msg = "delete slice profile from aai failed! cause-"+any.getCause()
 
 289             LOGGER.error(any.printStackTrace())
 
 290             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
 
 292         LOGGER.debug("*****${PREFIX} Exist delSliceProfileFromAAI *****")