2  * ============LICENSE_START=======================================================
 
   4  * ================================================================================
 
   5  * Copyright (C) 2017 AT&T Intellectual Property. 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 static org.apache.commons.lang3.StringUtils.*;
 
  26 import org.camunda.bpm.engine.delegate.BpmnError
 
  27 import org.camunda.bpm.engine.delegate.DelegateExecution
 
  28 import org.onap.aai.domain.yang.ServiceInstance
 
  29 import org.onap.aaiclient.client.aai.AAIObjectType
 
  30 import org.onap.aaiclient.client.aai.AAIResourcesClient
 
  31 import org.onap.aaiclient.client.aai.entities.AAIResultWrapper
 
  32 import org.onap.aaiclient.client.aai.entities.uri.AAIResourceUri
 
  33 import org.onap.aaiclient.client.aai.entities.uri.AAIUriFactory
 
  34 import org.onap.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder
 
  35 import org.onap.so.bpmn.common.scripts.AbstractServiceTaskProcessor
 
  36 import org.onap.so.bpmn.common.scripts.ExceptionUtil
 
  37 import org.onap.so.bpmn.common.scripts.MsoUtils
 
  38 import org.onap.so.bpmn.common.scripts.SDNCAdapterUtils
 
  39 import org.onap.so.bpmn.core.UrnPropertiesReader;
 
  40 import org.onap.so.bpmn.core.WorkflowException
 
  41 import org.onap.so.bpmn.core.json.JsonUtils
 
  42 import org.slf4j.Logger
 
  43 import org.slf4j.LoggerFactory
 
  44 import org.springframework.web.util.UriUtils;
 
  47  * This groovy class supports the <class>DoDeleteServiceInstance.bpmn</class> process.
 
  50  * @param - msoRequestId
 
  51  * @param - globalSubscriberId - O
 
  52  * @param - subscriptionServiceType - O
 
  53  * @param - serviceInstanceId
 
  54  * @param - serviceInstanceName - O
 
  55  * @param - serviceModelInfo - O
 
  56  * @param - productFamilyId
 
  57  * @param - serviceInputParams (should contain aic_zone for serviceTypes TRANSPORT,ATM)
 
  58  * @param - sdncVersion
 
  59  * @param - failNotFound - TODO
 
  60  * @param - serviceInputParams - TODO
 
  63  * @param - WorkflowException
 
  67 public class DoDeleteServiceInstance extends AbstractServiceTaskProcessor {
 
  68     private static final Logger logger = LoggerFactory.getLogger( DoDeleteServiceInstance.class);
 
  70         String Prefix="DDELSI_"
 
  71         ExceptionUtil exceptionUtil = new ExceptionUtil()
 
  72         JsonUtils jsonUtil = new JsonUtils()
 
  74         public void preProcessRequest (DelegateExecution execution) {
 
  76                 logger.trace("preProcessRequest ")
 
  80                         String requestId = execution.getVariable("msoRequestId")
 
  81                         execution.setVariable("prefix",Prefix)
 
  84                         //requestDetails.subscriberInfo. for AAI GET & PUT & SDNC assignToplology
 
  85                         String globalSubscriberId = execution.getVariable("globalSubscriberId") //globalCustomerId
 
  86                         if (globalSubscriberId == null)
 
  88                                 execution.setVariable("globalSubscriberId", "")
 
  91                         //requestDetails.requestParameters. for AAI PUT & SDNC assignTopology
 
  92                         String subscriptionServiceType = execution.getVariable("subscriptionServiceType")
 
  93                         if (subscriptionServiceType == null)
 
  95                                 execution.setVariable("subscriptionServiceType", "")
 
  98                         //Generated in parent for AAI PUT
 
  99                         String serviceInstanceId = execution.getVariable("serviceInstanceId")
 
 100                         if (isBlank(serviceInstanceId)){
 
 101                                 msg = "Input serviceInstanceId is null"
 
 103                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
 
 106                         String sdncCallbackUrl = UrnPropertiesReader.getVariable('mso.workflow.sdncadapter.callback',execution)
 
 107                         if (isBlank(sdncCallbackUrl)) {
 
 108                                 msg = "mso.workflow.sdncadapter.callback is null"
 
 110                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
 
 112                         execution.setVariable("sdncCallbackUrl", sdncCallbackUrl)
 
 113                         logger.debug("SDNC Callback URL: " + sdncCallbackUrl)
 
 115                         StringBuilder sbParams = new StringBuilder()
 
 116                         Map<String, String> paramsMap = execution.getVariable("serviceInputParams")
 
 117                         if (paramsMap != null)
 
 119                                 sbParams.append("<service-input-parameters>")
 
 120                                 for (Map.Entry<String, String> entry : paramsMap.entrySet()) {
 
 122                                         String paramName = entry.getKey()
 
 123                                         String paramValue = entry.getValue()
 
 126                                                         <name>${MsoUtils.xmlEscape(paramName)}</name>
 
 127                                                         <value>${MsoUtils.xmlEscape(paramValue)}</value>
 
 130                                         sbParams.append(paramsXml)
 
 132                                 sbParams.append("</service-input-parameters>")
 
 134                         String siParamsXml = sbParams.toString()
 
 135                         if (siParamsXml == null)
 
 137                         execution.setVariable("siParamsXml", siParamsXml)
 
 139                 } catch (BpmnError e) {
 
 141                 } catch (Exception ex){
 
 142                         msg = "Exception in preProcessRequest " + ex.getMessage()
 
 144                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
 
 146                 logger.trace("Exit preProcessRequest ")
 
 149         public void preProcessSDNCDelete (DelegateExecution execution) {
 
 151                 logger.trace("preProcessSDNCDelete ")
 
 155                         def serviceInstanceId = execution.getVariable("serviceInstanceId") ?: ""
 
 156                         def serviceInstanceName = execution.getVariable("serviceInstanceName") ?: ""
 
 157                         def callbackURL = execution.getVariable("sdncCallbackUrl") ?: ""
 
 158                         def requestId = execution.getVariable("msoRequestId") ?: ""
 
 159                         def serviceId = execution.getVariable("productFamilyId") ?: ""
 
 160                         def subscriptionServiceType = execution.getVariable("subscriptionServiceType") ?: ""
 
 161                         def globalSubscriberId = execution.getVariable("globalSubscriberId") ?: "" //globalCustomerId
 
 163                         String serviceModelInfo = execution.getVariable("serviceModelInfo") ?: ""
 
 164                         def modelInvariantUuid = ""
 
 165                         def modelVersion = ""
 
 168                         if (!isBlank(serviceModelInfo))
 
 170                                 modelInvariantUuid = jsonUtil.getJsonValue(serviceModelInfo, "modelInvariantUuid") ?: ""
 
 171                                 modelVersion = jsonUtil.getJsonValue(serviceModelInfo, "modelVersion") ?: ""
 
 172                                 modelUuid = jsonUtil.getJsonValue(serviceModelInfo, "modelUuid") ?: ""
 
 173                                 modelName = jsonUtil.getJsonValue(serviceModelInfo, "modelName") ?: ""
 
 177                         def siParamsXml = execution.getVariable("siParamsXml") ?: ""
 
 179                         // special URL for SDNW, msoAction helps set diff url in SDNCA
 
 180                         if("TRANSPORT".equalsIgnoreCase(execution.getVariable("serviceType")))
 
 182                                 msoAction = "TRANSPORT"
 
 185                         def sdncRequestId = UUID.randomUUID().toString()
 
 188                                         """<sdncadapterworkflow:SDNCAdapterWorkflowRequest xmlns:ns5="http://org.onap/so/request/types/v1"
 
 189                                                                                                         xmlns:sdncadapterworkflow="http://org.onap/so/workflow/schema/v1"
 
 190                                                                                                         xmlns:sdncadapter="http://org.onap/workflow/sdnc/adapter/schema/v1">
 
 191                                    <sdncadapter:RequestHeader>
 
 192                                                         <sdncadapter:RequestId>${MsoUtils.xmlEscape(sdncRequestId)}</sdncadapter:RequestId>
 
 193                                                         <sdncadapter:SvcInstanceId>${MsoUtils.xmlEscape(serviceInstanceId)}</sdncadapter:SvcInstanceId>
 
 194                                                         <sdncadapter:SvcAction>delete</sdncadapter:SvcAction>
 
 195                                                         <sdncadapter:SvcOperation>service-topology-operation</sdncadapter:SvcOperation>
 
 196                                                         <sdncadapter:CallbackUrl>${MsoUtils.xmlEscape(callbackURL)}</sdncadapter:CallbackUrl>
 
 197                                                         <sdncadapter:MsoAction>${MsoUtils.xmlEscape(msoAction)}</sdncadapter:MsoAction>
 
 198                                         </sdncadapter:RequestHeader>
 
 199                                 <sdncadapterworkflow:SDNCRequestData>
 
 200                                         <request-information>
 
 201                                                 <request-id>${MsoUtils.xmlEscape(requestId)}</request-id>
 
 206                                                 <request-action>DeleteServiceInstance</request-action>
 
 207                                         </request-information>
 
 208                                         <service-information>
 
 209                                                 <service-id>${MsoUtils.xmlEscape(serviceId)}</service-id>
 
 210                                                 <subscription-service-type>${MsoUtils.xmlEscape(subscriptionServiceType)}</subscription-service-type>
 
 211                                                 <onap-model-information>
 
 212                                                  <model-invariant-uuid>${MsoUtils.xmlEscape(modelInvariantUuid)}</model-invariant-uuid>
 
 213                                                  <model-uuid>${MsoUtils.xmlEscape(modelUuid)}</model-uuid>
 
 214                                                  <model-version>${MsoUtils.xmlEscape(modelVersion)}</model-version>
 
 215                                                  <model-name>${MsoUtils.xmlEscape(modelName)}</model-name>
 
 216                                             </onap-model-information>
 
 217                                                 <service-instance-id>${MsoUtils.xmlEscape(serviceInstanceId)}</service-instance-id>
 
 219                                                 <global-customer-id>${MsoUtils.xmlEscape(globalSubscriberId)}</global-customer-id>
 
 220                                         </service-information>
 
 221                                         <service-request-input>
 
 222                                                 <service-instance-name>${MsoUtils.xmlEscape(serviceInstanceName)}</service-instance-name>
 
 224                                         </service-request-input>
 
 225                                 </sdncadapterworkflow:SDNCRequestData>
 
 226                                 </sdncadapterworkflow:SDNCAdapterWorkflowRequest>"""
 
 228                         sdncDelete = utils.formatXml(sdncDelete)
 
 229                         def sdncRequestId2 = UUID.randomUUID().toString()
 
 230                         String sdncDeactivate = sdncDelete.replace(">delete<", ">deactivate<").replace(">${sdncRequestId}<", ">${sdncRequestId2}<")
 
 231                         execution.setVariable("sdncDelete", sdncDelete)
 
 232                         execution.setVariable("sdncDeactivate", sdncDeactivate)
 
 233                         logger.debug("sdncDeactivate:\n" + sdncDeactivate)
 
 234                         logger.debug("sdncDelete:\n" + sdncDelete)
 
 236                 } catch (BpmnError e) {
 
 238                 } catch(Exception ex) {
 
 239                         msg = "Exception in preProcessSDNCDelete. " + ex.getMessage()
 
 241                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, "Exception Occured in preProcessSDNCDelete.\n" + ex.getMessage())
 
 243                 logger.debug(" *****Exit preProcessSDNCDelete *****")
 
 246         public void postProcessSDNCDelete(DelegateExecution execution, String response, String method) {
 
 249                 logger.trace("postProcessSDNC " + method + " ")
 
 253                         WorkflowException workflowException = execution.getVariable("WorkflowException")
 
 254                         boolean successIndicator = execution.getVariable("SDNCA_SuccessIndicator")
 
 255                         logger.debug("SDNCResponse: " + response)
 
 256                         logger.debug("workflowException: " + workflowException)
 
 258                         SDNCAdapterUtils sdncAdapterUtils = new SDNCAdapterUtils()
 
 259                         sdncAdapterUtils.validateSDNCResponse(execution, response, workflowException, successIndicator)
 
 261                         if(execution.getVariable(Prefix + 'sdncResponseSuccess') == true){
 
 262                                 logger.debug("Good response from SDNC Adapter for service-instance " + method + "response:\n" + response)
 
 265                                 msg = "Bad Response from SDNC Adapter for service-instance " + method
 
 267                                 exceptionUtil.buildAndThrowWorkflowException(execution, 3500, msg)
 
 269                 } catch (BpmnError e) {
 
 271                 } catch(Exception ex) {
 
 272                         msg = "Exception in postProcessSDNC " + method + " Exception:" + ex.getMessage()
 
 274                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
 
 276                 logger.trace("Exit postProcessSDNC " + method + " ")
 
 280          * Gets the service instance uri from aai
 
 282         public void getServiceInstance(DelegateExecution execution) {
 
 283                 logger.trace("getServiceInstance ")
 
 285                         String serviceInstanceId = execution.getVariable('serviceInstanceId')
 
 287                         AAIResourcesClient resourceClient = new AAIResourcesClient()
 
 288                         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, serviceInstanceId)
 
 290                         if(resourceClient.exists(uri)){                         
 
 291                                 execution.setVariable("GENGS_FoundIndicator", true)
 
 292                                 execution.setVariable("GENGS_siResourceLink", uri.build().toString())
 
 293                                 Map<String, String> keys = uri.getURIKeys()
 
 294                                 String  globalSubscriberId = execution.getVariable(AAIFluentTypeBuilder.Types.CUSTOMER.getUriParams().globalCustomerId)
 
 295                                 if(isBlank(globalSubscriberId)){
 
 296                                         globalSubscriberId = keys.get("global-customer-id")
 
 297                                         execution.setVariable("globalSubscriberId", globalSubscriberId)
 
 300                                 //Extract Service Type if not provided on request
 
 301                                 String subscriptionServiceType = execution.getVariable("subscriptionServiceType")
 
 302                                 if(isBlank(subscriptionServiceType)){
 
 303                                         String serviceTypeEncoded = keys.get("service-type") //TODO will this produce as already decoded?
 
 304                                         subscriptionServiceType = UriUtils.decode(serviceTypeEncoded, "UTF-8")
 
 305                                         execution.setVariable("subscriptionServiceType", subscriptionServiceType)
 
 308                                 AAIResultWrapper wrapper = resourceClient.get(uri)
 
 309                                 if(wrapper.getRelationships().isPresent()){
 
 310                                         List<AAIResourceUri> uriList = wrapper.getRelationships().get().getRelatedAAIUris(AAIObjectType.ALLOTTED_RESOURCE)
 
 311                                         uriList.addAll(wrapper.getRelationships().get().getRelatedAAIUris(AAIObjectType.GENERIC_VNF))
 
 312                                         uriList.addAll(wrapper.getRelationships().get().getRelatedAAIUris(AAIObjectType.L3_NETWORK))
 
 314                                         if(uriList.isEmpty()){
 
 315                                                 Optional<ServiceInstance> si = wrapper.asBean(ServiceInstance.class)
 
 316                                                 String orchestrationStatus = si.get().getOrchestrationStatus()
 
 317                                                 String serviceType = si.get().getServiceType()
 
 318                                                 execution.setVariable("serviceType", serviceType)
 
 319                                                 execution.setVariable("serviceRole", si.get().getServiceRole())
 
 321                                                 if("TRANSPORT".equalsIgnoreCase(serviceType)){
 
 322                                                         if("PendingDelete".equals(orchestrationStatus)){
 
 323                                                                 execution.setVariable("skipDeactivate", true)
 
 325                                                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, "ServiceInstance of type TRANSPORT must in PendingDelete status to allow Delete. Orchestration-status: " + orchestrationStatus)
 
 329                                                 String svcTypes = UrnPropertiesReader.getVariable("sdnc.si.svc.types",execution) ?: ""
 
 330                                                 List<String> svcList = Arrays.asList(svcTypes.split("\\s*,\\s*"));
 
 331                                                 boolean isSdncService= false
 
 332                                                 for(String listEntry : svcList){
 
 333                                                         if(listEntry.equalsIgnoreCase(serviceType)){
 
 338                                                 execution.setVariable("sendToSDNC", true)
 
 339                                                 if(execution.getVariable("sdncVersion").equals("1610")){
 
 341                                                                 execution.setVariable("sendToSDNC", false)
 
 346                                                 execution.setVariable("siInUse", true)
 
 347                                                 logger.debug("Stopped deleting Service Instance, it has dependencies")
 
 348                                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, "Stopped deleting Service Instance, it has dependencies")
 
 352                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, "ServiceInstance not found in aai")
 
 355                 }catch(BpmnError e) {
 
 357                 }catch (Exception ex){
 
 358                         String msg = "Exception in getServiceInstance. " + ex.getMessage()
 
 360                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
 
 365          * Deletes the service instance in aai
 
 367         public void deleteServiceInstance(DelegateExecution execution) {
 
 368                 logger.trace("Entered deleteServiceInstance")
 
 370                         String globalCustId = execution.getVariable("globalSubscriberId")
 
 371                         String serviceType = execution.getVariable("subscriptionServiceType")
 
 372                         String serviceInstanceId = execution.getVariable("serviceInstanceId")
 
 374                         AAIResourcesClient resourceClient = new AAIResourcesClient();
 
 375                         AAIResourceUri serviceInstanceUri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, globalCustId, serviceType, serviceInstanceId)
 
 376                         resourceClient.delete(serviceInstanceUri)
 
 378                         logger.trace("Exited deleteServiceInstance")
 
 380                         logger.debug("Error occured within deleteServiceInstance method: " + e)
 
 381                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Error occured during deleteServiceInstance from aai")