Add nssiId for AllocateTNNSSI request
[so.git] / bpmn / so-bpmn-infrastructure-common / src / main / groovy / org / onap / so / bpmn / infrastructure / scripts / DoDeleteVFCNetworkServiceInstance.groovy
index 5ff9336..1ec11c6 100644 (file)
@@ -4,6 +4,8 @@
  * ================================================================================
  * Copyright (C) 2017 Huawei Technologies Co., Ltd. All rights reserved.
  * ================================================================================
+ * Modifications Copyright (c) 2019 Samsung
+ * ================================================================================
  * 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
@@ -25,29 +27,30 @@ import org.camunda.bpm.engine.delegate.DelegateExecution
 import org.onap.so.bpmn.common.scripts.AbstractServiceTaskProcessor
 import org.onap.so.bpmn.common.scripts.ExceptionUtil
 import org.onap.so.bpmn.core.json.JsonUtils
-//import org.onap.so.client.HttpClient
-import org.onap.so.client.aai.AAIObjectType
-import org.onap.so.client.aai.entities.uri.AAIResourceUri
-import org.onap.so.client.aai.entities.uri.AAIUriFactory
-import org.onap.so.logger.MessageEnum
-import org.onap.so.logger.MsoLogger
-import org.onap.so.rest.APIResponse
-import org.onap.so.rest.RESTClient
-import org.onap.so.rest.RESTConfig
-//import org.onap.so.utils.TargetEntity
+import org.onap.so.client.HttpClient
+import org.onap.so.client.HttpClientFactory
+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.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder
+import org.onap.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder.Types
+import org.slf4j.Logger
+import org.slf4j.LoggerFactory
+import org.onap.logging.filter.base.ONAPComponents;
 import org.onap.so.bpmn.core.UrnPropertiesReader
 
-//import javax.ws.rs.core.Response
+import javax.ws.rs.core.Response
 /**
  * This groovy class supports the <class>DoDeleteVFCNetworkServiceInstance.bpmn</class> process.
  * flow for E2E ServiceInstance Delete
  */
 public class DoDeleteVFCNetworkServiceInstance extends AbstractServiceTaskProcessor {
-       private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, DoDeleteVFCNetworkServiceInstance.class);
+    private static final Logger logger = LoggerFactory.getLogger( DoDeleteVFCNetworkServiceInstance.class);
 
     ExceptionUtil exceptionUtil = new ExceptionUtil()
 
     JsonUtils jsonUtil = new JsonUtils()
+    private final HttpClientFactory httpClientFactory = new HttpClientFactory()
 
     /**
      * Pre Process the BPMN Flow Request
@@ -57,21 +60,21 @@ public class DoDeleteVFCNetworkServiceInstance extends AbstractServiceTaskProces
     public void preProcessRequest (DelegateExecution execution) {
 
         String msg = ""
-        msoLogger.trace("preProcessRequest() ")
+        logger.trace("preProcessRequest() ")
         try {
             //deal with operation key
             String globalSubscriberId = execution.getVariable("globalSubscriberId")
-            msoLogger.info("globalSubscriberId:" + globalSubscriberId)
+            logger.info("globalSubscriberId:" + globalSubscriberId)
             String serviceType = execution.getVariable("serviceType")
-            msoLogger.info("serviceType:" + serviceType)
+            logger.info("serviceType:" + serviceType)
             String serviceId = execution.getVariable("serviceId")
-            msoLogger.info("serviceId:" + serviceId)
+            logger.info("serviceId:" + serviceId)
             String operationId = execution.getVariable("operationId")
-            msoLogger.info("serviceType:" + serviceType)
+            logger.info("serviceType:" + serviceType)
             String nodeTemplateUUID = execution.getVariable("resourceTemplateId")
-            msoLogger.info("nodeTemplateUUID:" + nodeTemplateUUID)
+            logger.info("nodeTemplateUUID:" + nodeTemplateUUID)
             String nsInstanceId = execution.getVariable("resourceInstanceId")
-            msoLogger.info("nsInstanceId:" + nsInstanceId)
+            logger.info("nsInstanceId:" + nsInstanceId)
             execution.setVariable("nsInstanceId",nsInstanceId)
             String nsOperationKey = """{
             "globalSubscriberId":"${globalSubscriberId}",
@@ -81,13 +84,13 @@ public class DoDeleteVFCNetworkServiceInstance extends AbstractServiceTaskProces
             "nodeTemplateUUID":"${nodeTemplateUUID}"
              }"""
             execution.setVariable("nsOperationKey", nsOperationKey);
-            msoLogger.info("nsOperationKey:" + nsOperationKey)
+            logger.info("nsOperationKey:" + nsOperationKey)
 
             String vfcAdapterUrl = UrnPropertiesReader.getVariable("mso.adapters.vfc.rest.endpoint", execution)
                        
             if (vfcAdapterUrl == null || vfcAdapterUrl.isEmpty()) {
                 msg = getProcessKey(execution) + ': mso:adapters:vfcc:rest:endpoint URN mapping is not defined'
-                msoLogger.debug(msg)
+                logger.debug(msg)
             }
  
             while (vfcAdapterUrl.endsWith('/')) {
@@ -100,10 +103,10 @@ public class DoDeleteVFCNetworkServiceInstance extends AbstractServiceTaskProces
             throw e;
         } catch (Exception ex){
             msg = "Exception in preProcessRequest " + ex.getMessage()
-            msoLogger.info(msg)
+            logger.info(msg)
             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
         }
-        msoLogger.trace("Exit preProcessRequest ")
+        logger.trace("Exit preProcessRequest ")
        }
 
     /**
@@ -111,23 +114,23 @@ public class DoDeleteVFCNetworkServiceInstance extends AbstractServiceTaskProces
      */
     public void deleteNSRelationship(DelegateExecution execution) {
         def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
-        utils.log("INFO"," ***** deleteNSRelationship *****",  isDebugEnabled)
+        logger.info(" ***** deleteNSRelationship *****")
         String nsInstanceId = execution.getVariable("resourceInstanceId")
         if(nsInstanceId == null || nsInstanceId == ""){
-            utils.log("INFO"," Delete NS failed",  isDebugEnabled)
+            logger.info(" Delete NS failed")
             return
         }
         String globalSubscriberId = execution.getVariable("globalSubscriberId")
         String serviceType = execution.getVariable("serviceType")
         String serviceId = execution.getVariable("serviceId")
-        AAIResourceUri serviceInstanceUri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, globalSubscriberId, serviceType, serviceId)
-        AAIResourceUri nsServiceInstanceUri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, globalSubscriberId, serviceType, nsInstanceId)
+        AAIResourceUri serviceInstanceUri = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business().customer(globalSubscriberId).serviceSubscription(serviceType).serviceInstance(serviceId))
+        AAIResourceUri nsServiceInstanceUri = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business().customer(globalSubscriberId).serviceSubscription(serviceType).serviceInstance(nsInstanceId))
         try {
             getAAIClient().disconnect(serviceInstanceUri, nsServiceInstanceUri)
         }catch(Exception e){
             exceptionUtil.buildAndThrowWorkflowException(execution,25000,"Exception occured while NS disconnect call: " + e.getMessage())
         }
-        utils.log("INFO"," *****Exit deleteNSRelationship *****",  isDebugEnabled)
+        logger.info(" *****Exit deleteNSRelationship *****")
     }
 
     /**
@@ -135,20 +138,19 @@ public class DoDeleteVFCNetworkServiceInstance extends AbstractServiceTaskProces
      */
     public void deleteNetworkService(DelegateExecution execution) {
 
-        msoLogger.trace("deleteNetworkService  start ")
+        logger.trace("deleteNetworkService  start ")
         String vfcAdapterUrl = execution.getVariable("vfcAdapterUrl")
         String nsOperationKey = execution.getVariable("nsOperationKey");
         String url = vfcAdapterUrl + "/ns/" + execution.getVariable("nsInstanceId")
-        APIResponse apiResponse = deleteRequest(execution, url, nsOperationKey)
-        String returnCode = apiResponse.getStatusCode()
-               String aaiResponseAsString = apiResponse.getResponseBodyAsString()
+        Response apiResponse = deleteRequest(execution, url, nsOperationKey)
+        String returnCode = apiResponse.getStatus()
         String operationStatus = "error";
         if(returnCode== "200" || returnCode== "202"){
             operationStatus = "finished"
         }
         execution.setVariable("operationStatus", operationStatus)
 
-        msoLogger.trace("deleteNetworkService  end ")
+        logger.trace("deleteNetworkService  end ")
     }
 
     /**
@@ -156,19 +158,19 @@ public class DoDeleteVFCNetworkServiceInstance extends AbstractServiceTaskProces
      */
     public void terminateNetworkService(DelegateExecution execution) {
 
-        msoLogger.trace("terminateNetworkService  start ")
+        logger.trace("terminateNetworkService  start ")
         String vfcAdapterUrl = execution.getVariable("vfcAdapterUrl")
         String nsOperationKey = execution.getVariable("nsOperationKey")
         String url =  vfcAdapterUrl + "/ns/" + execution.getVariable("nsInstanceId") + "/terminate"
-        APIResponse apiResponse = postRequest(execution, url, nsOperationKey)
-        String returnCode = apiResponse.getStatusCode()                
-        String aaiResponseAsString = apiResponse.getResponseBodyAsString()
+        Response apiResponse = postRequest(execution, url, nsOperationKey)
+        String returnCode = apiResponse.getStatus()
+        String aaiResponseAsString = apiResponse.readEntity(String.class)
         String jobId = "";
         if(returnCode== "200" || returnCode== "202"){
             jobId =  jsonUtil.getJsonValue(aaiResponseAsString, "jobId")
         }
         execution.setVariable("jobId", jobId)
-        msoLogger.trace("terminateNetworkService  end ")
+        logger.trace("terminateNetworkService  end ")
     }
 
     /**
@@ -176,20 +178,20 @@ public class DoDeleteVFCNetworkServiceInstance extends AbstractServiceTaskProces
      */
     public void queryNSProgress(DelegateExecution execution) {
 
-        msoLogger.trace("queryNSProgress  start ")
+        logger.trace("queryNSProgress  start ")
         String vfcAdapterUrl = execution.getVariable("vfcAdapterUrl")
         String jobId = execution.getVariable("jobId")
         String nsOperationKey = execution.getVariable("nsOperationKey");
         String url =  vfcAdapterUrl + "/jobs/" +  execution.getVariable("jobId")
-        APIResponse apiResponse = postRequest(execution, url, nsOperationKey)
-        String returnCode = apiResponse.getStatusCode()
-        String apiResponseAsString = apiResponse.getResponseBodyAsString()
+        Response apiResponse = postRequest(execution, url, nsOperationKey)
+        String returnCode = apiResponse.getStatus()
+        String apiResponseAsString = apiResponse.readEntity(String.class)
         String operationProgress = "100"
         if(returnCode== "200"){
             operationProgress = jsonUtil.getJsonValue(apiResponseAsString, "responseDescriptor.progress")
         }
         execution.setVariable("operationProgress", operationProgress)
-        msoLogger.trace("queryNSProgress  end ")
+        logger.trace("queryNSProgress  end ")
     }
 
     /**
@@ -199,7 +201,7 @@ public class DoDeleteVFCNetworkServiceInstance extends AbstractServiceTaskProces
         try {
             Thread.sleep(5000);
         } catch(InterruptedException e) {
-            msoLogger.info("Time Delay exception" + e)
+            logger.info("Time Delay exception" + e)
         }
     }
 
@@ -215,34 +217,28 @@ public class DoDeleteVFCNetworkServiceInstance extends AbstractServiceTaskProces
      * url: the url of the request
      * requestBody: the body of the request
      */
-    private APIResponse postRequest(DelegateExecution execution, String urlString, String requestBody){
+    private Response postRequest(DelegateExecution execution, String urlString, String requestBody){
 
-               msoLogger.trace("Started Execute VFC adapter Post Process ")
-               msoLogger.info("url:"+urlString +"\nrequestBody:"+ requestBody)
-               APIResponse apiResponse = null
+               logger.trace("Started Execute VFC adapter Post Process ")
+               logger.info("url:"+urlString +"\nrequestBody:"+ requestBody)
+               Response apiResponse = null
                try{
+                       URL url = new URL(urlString);
+
                        // Get the Basic Auth credentials for the VFCAdapter, username is 'bpel', auth is '07a7159d3bf51a0e53be7a8f89699be7'
-                       def basicAuthHeaderValue = ""
-                       RESTConfig config = new RESTConfig(urlString)
-                       RESTClient client = null;
-                       int statusCode = 0;
-                       
-                       // user 'bepl' authHeader is the same with mso.db.auth
-                       String basicAuthValuedb =  UrnPropertiesReader.getVariable("mso.db.auth", execution)
-                       msoLogger.debug("basicAuthValuedb: " + basicAuthValuedb)
-                       
-                       client = new RESTClient(config)
-                       client.addHeader("Accept", "application/json")
-                       client.addAuthorizationHeader(basicAuthValuedb)
-                       client.addHeader("Content-Type", "application/json")
-                       
-                       apiResponse = client.httpPost(requestBody)
-                       statusCode = apiResponse.getStatusCode()
-                               
-                       msoLogger.debug("response code:"+ apiResponse.getStatusCode() +"\nresponse body:"+ apiResponse.getResponseBodyAsString())
-               
+            // user 'bepl' authHeader is the same with mso.db.auth
+            String basicAuthValuedb =  UrnPropertiesReader.getVariable("mso.db.auth", execution)
+            HttpClient httpClient = httpClientFactory.newJsonClient(url, ONAPComponents.VNF_ADAPTER)
+            httpClient.addAdditionalHeader("Accept", "application/json")
+            httpClient.addAdditionalHeader("Authorization", basicAuthValuedb)
+
+                       apiResponse = httpClient.post(requestBody)
+
+                       logger.debug("response code:"+ apiResponse.getStatus() +"\nresponse body:"+ apiResponse.readEntity(String.class))
+
+                       logger.trace("Completed Execute VF-C adapter Post Process ")
                }catch(Exception e){
-            msoLogger.error("Exception occured while executing VF-C Post Call. Exception is: \n" + e.getMessage());
+            logger.error("Exception occured while executing VF-C Post Call. Exception is: \n" + e.getMessage());
             throw new BpmnError("MSOWorkflowException")
         }
         return apiResponse
@@ -252,37 +248,28 @@ public class DoDeleteVFCNetworkServiceInstance extends AbstractServiceTaskProces
      * url: the url of the request
      * requestBody: the body of the request
      */
-    private APIResponse deleteRequest(DelegateExecution execution, String url, String requestBody){
+    private Response deleteRequest(DelegateExecution execution, String url, String requestBody){
 
-        msoLogger.trace("Started Execute VFC adapter Delete Process ")
-        msoLogger.info("url:"+url +"\nrequestBody:"+ requestBody)
-       
-               APIResponse apiResponse = null
-               try{
-                       // Get the Basic Auth credentials for the VFCAdapter, username is 'bpel', auth is '07a7159d3bf51a0e53be7a8f89699be7'
-                       def basicAuthHeaderValue = ""
-                       RESTConfig config = new RESTConfig(url)
-                       RESTClient client = null;
-                       int statusCode = 0;
-                       
-                       // user 'bepl' authHeader is the same with mso.db.auth
-                       String basicAuthValuedb =  UrnPropertiesReader.getVariable("mso.db.auth", execution)
-                       msoLogger.debug("basicAuthValuedb: " + basicAuthValuedb)
-       
-                       client = new RESTClient(config)
-                       client.addHeader("Accept", "application/json")
-                       client.addAuthorizationHeader(basicAuthValuedb)
-                       client.addHeader("Content-Type", "application/json")
-                       
-                       apiResponse = client.httpDelete(requestBody)
-                       statusCode = apiResponse.getStatusCode()
-                               
-                       msoLogger.debug("response code:"+ apiResponse.getStatusCode() +"\nresponse body:"+ apiResponse.getResponseBodyAsString())
+        logger.trace("Started Execute VFC adapter Delete Process ")
+        logger.info("url:"+url +"\nrequestBody:"+ requestBody)
+       Response r
+        try{
+
+               URL Url = new URL(url)
+            // Get the Basic Auth credentials for the VFCAdapter, username is 'bpel', auth is '07a7159d3bf51a0e53be7a8f89699be7'
+            // user 'bepl' authHeader is the same with mso.db.auth
+            String basicAuthValuedb =  UrnPropertiesReader.getVariable("mso.db.auth", execution)
+            HttpClient httpClient = httpClientFactory.newJsonClient(url, ONAPComponents.VNF_ADAPTER)
+            httpClient.addAdditionalHeader("Accept", "application/json")
+            httpClient.addAdditionalHeader("Authorization", basicAuthValuedb)
+            httpClient.addAdditionalHeader("Content-Type", "application/json")
+            r = httpClient.delete(requestBody)
                
-               }catch(Exception e){
-                       msoLogger.error("Exception occured while executing VF-C Delete Call. Exception is: \n" + e.getMessage());
-                       throw new BpmnError("MSOWorkflowException")
-               }
-        return apiResponse
+            logger.trace("Completed Execute VF-C adapter Delete Process ")
+        }catch(Exception e){
+            logger.error("Exception occured while executing VF-C Post Call. Exception is: \n" + e.getMessage());
+            throw new BpmnError("MSOWorkflowException")
+        }
+        return r
     }
 }