2  * ============LICENSE_START=======================================================
 
   4  * ================================================================================
 
   5  * Copyright (C) 2020  Telecom Italia
 
   6  * ================================================================================
 
   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=========================================================
 
  21 package org.onap.so.bpmn.infrastructure.scripts
 
  23 import com.fasterxml.jackson.databind.ObjectMapper
 
  24 import org.camunda.bpm.engine.delegate.DelegateExecution
 
  25 import org.onap.aai.domain.yang.CloudRegion
 
  26 import org.onap.aai.domain.yang.Customer
 
  27 import org.onap.aai.domain.yang.ModelVer
 
  28 import org.onap.aai.domain.yang.OwningEntities
 
  29 import org.onap.aai.domain.yang.ServiceSubscription
 
  30 import org.onap.aai.domain.yang.SliceProfile
 
  31 import org.onap.aai.domain.yang.GenericVnf
 
  32 import org.onap.aai.domain.yang.ServiceInstance
 
  33 import org.onap.aai.domain.yang.Tenant
 
  34 import org.onap.aai.domain.yang.VfModule
 
  35 import org.onap.aaiclient.client.aai.AAIClient
 
  36 import org.onap.aaiclient.client.aai.AAIObjectType
 
  37 import org.onap.aaiclient.client.aai.AAIResourcesClient
 
  38 import org.onap.aaiclient.client.aai.entities.AAIResultWrapper
 
  39 import org.onap.aaiclient.client.aai.entities.Relationships
 
  40 import org.onap.aaiclient.client.aai.entities.uri.AAIResourceUri
 
  41 import org.onap.aaiclient.client.aai.entities.uri.AAIUriFactory
 
  42 import org.onap.logging.filter.base.ONAPComponents
 
  43 import org.onap.so.bpmn.common.scripts.AbstractServiceTaskProcessor
 
  44 import org.onap.so.bpmn.common.scripts.ExceptionUtil
 
  45 import org.onap.so.bpmn.common.scripts.MsoUtils
 
  46 import org.onap.so.bpmn.common.scripts.RequestDBUtil
 
  47 import org.onap.so.bpmn.core.UrnPropertiesReader
 
  48 import org.onap.so.bpmn.core.json.JsonUtils
 
  49 import org.onap.so.client.HttpClient
 
  50 import org.onap.so.client.HttpClientFactory
 
  51 import org.onap.so.db.request.beans.OperationStatus
 
  52 import org.onap.so.requestsdb.RequestsDbConstant
 
  53 import org.onap.so.serviceinstancebeans.CloudConfiguration
 
  54 import org.onap.so.serviceinstancebeans.LineOfBusiness
 
  55 import org.onap.so.serviceinstancebeans.ModelInfo
 
  56 import org.onap.so.serviceinstancebeans.ModelType
 
  57 import org.onap.so.serviceinstancebeans.OwningEntity
 
  58 import org.onap.so.serviceinstancebeans.Project
 
  59 import org.onap.so.serviceinstancebeans.RequestDetails
 
  60 import org.onap.so.serviceinstancebeans.RequestInfo
 
  61 import org.onap.so.serviceinstancebeans.RequestParameters
 
  62 import org.onap.so.serviceinstancebeans.Resources
 
  63 import org.onap.so.serviceinstancebeans.Service
 
  64 import org.onap.so.serviceinstancebeans.SubscriberInfo
 
  65 import org.onap.so.serviceinstancebeans.VfModules
 
  66 import org.onap.so.serviceinstancebeans.Vnfs
 
  67 import org.slf4j.Logger
 
  68 import org.slf4j.LoggerFactory
 
  70 import javax.ws.rs.core.Response
 
  72 class DoDeallocateCoreNSSI extends DoCommonCoreNSSI {
 
  73     private final String PREFIX ="DoDeallocateCoreNSSI"
 
  75     private ExceptionUtil exceptionUtil = new ExceptionUtil()
 
  76     private RequestDBUtil requestDBUtil = new RequestDBUtil()
 
  77     private MsoUtils utils = new MsoUtils()
 
  78     private JsonUtils jsonUtil = new JsonUtils()
 
  80     private static final Logger LOGGER = LoggerFactory.getLogger( DoDeallocateCoreNSSI.class)
 
  83      * Queries OOF for NSSI termination
 
  86     void executeTerminateNSSIQuery(DelegateExecution execution) {
 
  87         LOGGER.trace("${PREFIX} Start executeTerminateNSSIQuery")
 
  89         String urlString = UrnPropertiesReader.getVariable("mso.oof.endpoint", execution)
 
  91         //Prepare auth for OOF
 
  93         String basicAuth = UrnPropertiesReader.getVariable("mso.oof.auth", execution)
 
  94         String msokey = UrnPropertiesReader.getVariable("mso.msoKey", execution)
 
  96         String basicAuthValue = encryptBasicAuth(basicAuth, msokey)
 
  97         if (basicAuthValue != null) {
 
  98             String responseAuthHeader = getAuthHeader(execution, basicAuthValue, msokey)
 
  99             String errorCode = jsonUtil.getJsonValue(responseAuthHeader, "errorCode")
 
 100             if(errorCode == null || errorCode.isEmpty()) { // No error
 
 101                 authHeader = responseAuthHeader
 
 104                 exceptionUtil.buildAndThrowWorkflowException(execution, Integer.parseInt(errorCode), jsonUtil.getJsonValue(responseAuthHeader, "errorMessage"))
 
 107             LOGGER.error( "Unable to obtain BasicAuth - BasicAuth value null")
 
 108             exceptionUtil.buildAndThrowWorkflowException(execution, 401, "Internal Error - BasicAuth " +
 
 112         //Prepare send request to OOF
 
 113         String oofRequest = buildOOFRequest(execution)
 
 115         String callOOFResponse = callOOF(urlString, authHeader, oofRequest)
 
 116         String errorCode = jsonUtil.getJsonValue(callOOFResponse, "errorCode")
 
 117         if(errorCode == null || errorCode.isEmpty()) { // No error
 
 118             String oofResponse = callOOFResponse
 
 119             String isTerminateNSSI = jsonUtil.getJsonValue(oofResponse, "terminateResponse")
 
 121             execution.setVariable("isTerminateNSSI", Boolean.parseBoolean(isTerminateNSSI))
 
 124             LOGGER.error(jsonUtil.getJsonValue(callOOFResponse, "errorMessage"))
 
 125             exceptionUtil.buildAndThrowWorkflowException(execution, Integer.parseInt(errorCode), jsonUtil.getJsonValue(callOOFResponse, "errorMessage"))
 
 129         LOGGER.trace("${PREFIX} Exit executeTerminateNSSIQuery")
 
 134      * Executes sync call to OOF
 
 135      * @return OOF response
 
 137     String callOOF(String urlString, String authHeader, String oofRequest) {
 
 138         String errorCode = ""
 
 139         String errorMessage = ""
 
 143             URL url = new URL(urlString + "/api/oof/terminate/nxi/v1")
 
 144             HttpClient httpClient = new HttpClientFactory().newJsonClient(url, ONAPComponents.OOF)
 
 145             httpClient.addAdditionalHeader("Authorization", authHeader)
 
 146             httpClient.addAdditionalHeader("Accept", "application/json")
 
 147             httpClient.addAdditionalHeader("Content-Type", "application/json")
 
 149             Response httpResponse = httpClient.post(oofRequest)
 
 151             int responseCode = httpResponse.getStatus()
 
 152             LOGGER.debug("OOF sync response code is: " + responseCode)
 
 154             if (responseCode != 202) { // Accepted
 
 155                 errorCode = responseCode
 
 156                 errorMessage = "Received a Bad Sync Response from OOF."
 
 159                         " \"errorCode\": \"${errorCode}\",\n" +
 
 160                         " \"errorMessage\": \"${errorMessage}\"\n" +
 
 162                 //exceptionUtil.buildAndThrowWorkflowException(execution, responseCode, "Received a Bad Sync Response from OOF.")
 
 165             if (httpResponse.hasEntity()) {
 
 166                 response = httpResponse.readEntity(String.class)
 
 170                 errorMessage = "No response received from OOF."
 
 173                         " \"errorCode\": \"${errorCode}\",\n" +
 
 174                         " \"errorMessage\": \"${errorMessage}\"\n" +
 
 180             errorMessage = e.getMessage()
 
 183                     " \"errorCode\": \"${errorCode}\",\n" +
 
 184                     " \"errorMessage\": \"${errorMessage}\"\n" +
 
 193     String encryptBasicAuth(String basicAuth, String msoKey) {
 
 194         return utils.encrypt(basicAuth, msoKey)
 
 198     String getAuthHeader(DelegateExecution execution, String basicAuthValue, String msokey) {
 
 200         String errorCode = ""
 
 201         String errorMessage = ""
 
 203         LOGGER.debug("Obtained BasicAuth username and password for OOF: " + basicAuthValue)
 
 205             response = utils.getBasicAuth(basicAuthValue, msokey)
 
 206         } catch (Exception ex) {
 
 207             LOGGER.error("Unable to encode username and password string: ", ex)
 
 210             errorMessage = "Internal Error - Unable to encode username and password string"
 
 213                     " \"errorCode\": \"${errorCode}\",\n" +
 
 214                     " \"errorMessage\": \"${errorMessage}\"\n" +
 
 228     private String buildOOFRequest(DelegateExecution execution) {
 
 230         def currentNSSI = execution.getVariable("currentNSSI")
 
 232         String nssiId = currentNSSI['nssiId']
 
 233         String requestId = execution.getVariable("mso-request-id")
 
 235         String request =    "{\n" +
 
 236                             "  \"type\": \"NSSI\",\n" +
 
 237                             "  \"NxIId\": \"${nssiId}\",\n" +
 
 238                             "  \"requestInfo\": {\n" +
 
 239                             "    \"transactionId\": \"${requestId}\",\n" +
 
 240                             "    \"requestId\": \"${requestId}\",\n" +
 
 241                             "    \"sourceId\": \"so\",\n" +
 
 251      * Invokes deleteServiceOrder external API
 
 254     void deleteServiceOrder(DelegateExecution execution) {
 
 255         LOGGER.trace("${PREFIX} Start deleteServiceOrder")
 
 257         def currentNSSI = execution.getVariable("currentNSSI")
 
 260             //url:/nbi/api/v4/serviceOrder/"
 
 261             def nbiEndpointUrl = UrnPropertiesReader.getVariable("nbi.endpoint.url", execution)
 
 263             ServiceInstance networkServiceInstance = (ServiceInstance)currentNSSI['networkServiceInstance']
 
 265             String url = String.format("${nbiEndpointUrl}/api/v4/serviceOrder/%s", networkServiceInstance.getServiceInstanceId()) // Service Order ID = Network Service Instance ID
 
 267             String msoKey = UrnPropertiesReader.getVariable("mso.msoKey", execution)
 
 268             String basicAuth =  UrnPropertiesReader.getVariable("mso.infra.endpoint.auth", execution)
 
 270             String basicAuthValue = encryptBasicAuth(basicAuth, msoKey)
 
 272             if (basicAuthValue != null) {
 
 273                 String responseAuthHeader = getAuthHeader(execution, basicAuthValue, msoKey)
 
 274                 String errorCode = jsonUtil.getJsonValue(responseAuthHeader, "errorCode")
 
 275                 if(errorCode == null || errorCode.isEmpty()) { // No error
 
 276                     authHeader = responseAuthHeader
 
 279                     exceptionUtil.buildAndThrowWorkflowException(execution, Integer.parseInt(errorCode), jsonUtil.getJsonValue(responseAuthHeader, "errorMessage"))
 
 282                 LOGGER.error( "Unable to obtain BasicAuth - BasicAuth value null")
 
 283                 exceptionUtil.buildAndThrowWorkflowException(execution, 401, "Internal Error - BasicAuth " +
 
 287             String callDeleteServiceOrderResponse = callDeleteServiceOrder(execution, url, authHeader)
 
 288             String errorCode = jsonUtil.getJsonValue(callDeleteServiceOrderResponse, "errorCode")
 
 289             String deleteServcieResponse = ""
 
 291             if(errorCode == null || errorCode.isEmpty()) { // No error
 
 292                 deleteServcieResponse = callDeleteServiceOrderResponse // check the response ???
 
 295                 LOGGER.error(jsonUtil.getJsonValue(callDeleteServiceOrderResponse, "errorMessage"))
 
 296                 exceptionUtil.buildAndThrowWorkflowException(execution, Integer.parseInt(errorCode), jsonUtil.getJsonValue(callDeleteServiceOrderResponse, "errorMessage"))
 
 299             String msg = "Exception in DoDeallocateCoreNSSI.deleteServiceOrder. " + any.getCause()
 
 301             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
 
 304         LOGGER.trace("${PREFIX} Exit deleteServiceOrder")
 
 308     String callDeleteServiceOrder(DelegateExecution execution, String urlString, String authHeader) {
 
 309         String errorCode = ""
 
 310         String errorMessage = ""
 
 314             HttpClient httpClient = getHttpClientFactory().newJsonClient(new URL(urlString), ONAPComponents.EXTERNAL)
 
 315             httpClient.addAdditionalHeader("Authorization", authHeader)
 
 316             httpClient.addAdditionalHeader("Accept", "application/json")
 
 317             Response httpResponse = httpClient.delete()
 
 319             if (httpResponse.hasEntity()) {
 
 320                 response = httpResponse.readEntity(String.class)
 
 324                 errorMessage = "No response received."
 
 327                         " \"errorCode\": \"${errorCode}\",\n" +
 
 328                         " \"errorMessage\": \"${errorMessage}\"\n" +
 
 333             String msg = "Exception in DoDeallocateCoreNSSI.deleteServiceOrder. " + any.getCause()
 
 336                     " \"errorCode\": \"7000\",\n" +
 
 337                     " \"errorMessage\": \"${msg}\"\n" +
 
 346      * Removes NSSI association with NSI
 
 349     void removeNSSIAssociationWithNSI(DelegateExecution execution) {
 
 350         LOGGER.trace("${PREFIX} Start removeNSSIAssociationWithNSI")
 
 352         AAIResourcesClient client = getAAIClient()
 
 354         def currentNSSI = execution.getVariable("currentNSSI")
 
 356         String nssiId = currentNSSI['nssiId']
 
 357         String nsiId = currentNSSI['nsiId']
 
 359         AAIResourceUri nssiUri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, nssiId)
 
 360         AAIResourceUri nsiUri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, nsiId)
 
 363             client.disconnect(nssiUri, nsiUri)
 
 365             exceptionUtil.buildAndThrowWorkflowException(execution, 25000, "Exception occured while NSSI association with NSI disconnect call: " + e.getMessage())
 
 368         LOGGER.trace("${PREFIX} Exit removeNSSIAssociationWithNSI")
 
 373      * Delets NSSI Service Instance
 
 376     void deleteNSSIServiceInstance(DelegateExecution execution) {
 
 377         LOGGER.trace("${PREFIX} Start deleteNSSIServiceInstance")
 
 379         AAIResourcesClient client = getAAIClient()
 
 381         def currentNSSI = execution.getVariable("currentNSSI")
 
 383         String nssiId = currentNSSI['nssiId']
 
 384         AAIResourceUri nssiUri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, nssiId)
 
 387             getAAIClient().delete(nssiUri)
 
 389             exceptionUtil.buildAndThrowWorkflowException(execution, 25000, "Exception occured while NSSI Service Instance delete call: " + e.getMessage())
 
 392         LOGGER.trace("${PREFIX} Exit deleteNSSIServiceInstance")