2  * ============LICENSE_START=======================================================
 
   4  * ================================================================================
 
   5  * Copyright (C) 2020 Huawei Technologies Co., Ltd. All rights reserved.
 
   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 org.camunda.bpm.engine.delegate.BpmnError
 
  24 import org.camunda.bpm.engine.delegate.DelegateExecution
 
  25 import org.onap.aai.domain.yang.NetworkPolicy
 
  26 import org.onap.aai.domain.yang.SliceProfile
 
  27 import org.onap.aaiclient.client.aai.AAIResourcesClient
 
  28 import org.onap.aaiclient.client.aai.entities.uri.AAIResourceUri
 
  29 import org.onap.aaiclient.client.aai.entities.uri.AAIUriFactory
 
  30 import org.onap.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder
 
  31 import org.onap.so.bpmn.common.scripts.AbstractServiceTaskProcessor
 
  32 import org.onap.so.bpmn.common.scripts.ExceptionUtil
 
  33 import org.onap.so.bpmn.core.json.JsonUtils
 
  34 import org.slf4j.Logger
 
  35 import org.slf4j.LoggerFactory
 
  37 import static org.apache.commons.lang3.StringUtils.isBlank
 
  38 import static org.apache.commons.lang3.StringUtils.isNotBlank
 
  40 class DoCreateTnNssiInstance extends AbstractServiceTaskProcessor {
 
  42     private static final Logger logger = LoggerFactory.getLogger(DoCreateTnNssiInstance.class);
 
  43     final String AAI_VERSION = "v21"
 
  44     JsonUtils jsonUtil = new JsonUtils()
 
  45     TnNssmfUtils tnNssmfUtils = new TnNssmfUtils()
 
  46     ExceptionUtil exceptionUtil = new ExceptionUtil()
 
  47     String Prefix = "DCTN_"
 
  49     void preProcessRequest(DelegateExecution execution) {
 
  51         logger.trace("Enter preProcessRequest()")
 
  53         execution.setVariable("prefix", Prefix)
 
  55         String modelInvariantUuid = execution.getVariable("modelInvariantUuid")
 
  56         String modelUuid = execution.getVariable("modelUuid")
 
  57         //here modelVersion is not set, we use modelUuid to decompose the service.
 
  58         def isDebugLogEnabled = true
 
  59         execution.setVariable("isDebugLogEnabled", isDebugLogEnabled)
 
  60         String serviceModelInfo = """{
 
  61             "modelInvariantUuid":"${modelInvariantUuid}",
 
  62             "modelUuid":"${modelUuid}",
 
  65         execution.setVariable("serviceModelInfo", serviceModelInfo)
 
  67         if (isBlank(execution.getVariable("enableSdnc"))) {
 
  68             tnNssmfUtils.setEnableSdncConfig(execution)
 
  71         logger.trace("Exit preProcessRequest")
 
  75     void createSliceProfile(DelegateExecution execution) {
 
  77         String ssInstanceId = execution.getVariable("sliceServiceInstanceId")
 
  78         String sliceProfileStr = execution.getVariable("sliceProfile")
 
  79         String sliceProfileId = UUID.randomUUID().toString()
 
  80         SliceProfile sliceProfile = new SliceProfile();
 
  81         sliceProfile.setProfileId(sliceProfileId)
 
  82         sliceProfile.setLatency(Integer.parseInt(jsonUtil.getJsonValue(sliceProfileStr, "latency")))
 
  83         sliceProfile.setResourceSharingLevel(jsonUtil.getJsonValue(sliceProfileStr, "resourceSharingLevel"))
 
  84         //sliceProfile.setSNssai(tnNssmfUtils.getFirstSnssaiFromSliceProfile(sliceProfileStr))
 
  86         sliceProfile.setMaxBandwidth(Integer.parseInt(jsonUtil.getJsonValue(sliceProfileStr, "maxBandwidth")))
 
  88         //sliceProfile.setReliability(new Object())
 
  90             AAIResourcesClient client = getAAIClient()
 
  92                     AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business()
 
  93                             .customer(execution.getVariable("globalSubscriberId"))
 
  94                             .serviceSubscription(execution.getVariable("subscriptionServiceType"))
 
  95                             .serviceInstance(ssInstanceId)
 
  96                             .sliceProfile(sliceProfileId))
 
  97             client.create(uri, sliceProfile)
 
  99         } catch (BpmnError e) {
 
 101         } catch (Exception ex) {
 
 102             String msg = "Exception in DoCreateSliceServiceInstance.instantiateSliceService. " + ex.getMessage()
 
 104             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
 
 109     void createServiceInstance(DelegateExecution execution) {
 
 111         String serviceRole = "nssi"
 
 112         String serviceType = execution.getVariable("subscriptionServiceType")
 
 113         String ssInstanceId = execution.getVariable("sliceServiceInstanceId")
 
 114         String sliceProfileStr = execution.getVariable("sliceProfile")
 
 116             if (sliceProfileStr == null || sliceProfileStr.isEmpty()) {
 
 117                 String msg = "ERROR: createServiceInstance: sliceProfile is null"
 
 119                 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
 
 122             org.onap.aai.domain.yang.ServiceInstance ss = new org.onap.aai.domain.yang.ServiceInstance()
 
 123             ss.setServiceInstanceId(ssInstanceId)
 
 124             String sliceInstanceName = execution.getVariable("sliceServiceInstanceName")
 
 125             if (isBlank(sliceInstanceName)) {
 
 126                 logger.error("ERROR: createServiceInstance: sliceInstanceName is null")
 
 127                 sliceInstanceName = ssInstanceId
 
 129             ss.setServiceInstanceName(sliceInstanceName)
 
 130             ss.setServiceType(serviceType)
 
 131             String serviceStatus = "deactivated"
 
 132             ss.setOrchestrationStatus(serviceStatus)
 
 133             String modelInvariantUuid = execution.getVariable("modelInvariantUuid")
 
 134             String modelUuid = execution.getVariable("modelUuid")
 
 135             ss.setModelInvariantId(modelInvariantUuid)
 
 136             ss.setModelVersionId(modelUuid)
 
 137             String serviceInstanceLocationId = tnNssmfUtils.getFirstPlmnIdFromSliceProfile(sliceProfileStr)
 
 138             ss.setServiceInstanceLocationId(serviceInstanceLocationId)
 
 139             String snssai = tnNssmfUtils.getFirstSnssaiFromSliceProfile(sliceProfileStr)
 
 140             //ss.setEnvironmentContext(snssai)
 
 141             ss.setEnvironmentContext("tn")
 
 142             ss.setServiceRole(serviceRole)
 
 144             String domainTypeStr = jsonUtil.getJsonValue(sliceProfileStr, "domainType")
 
 145             if (isNotBlank(domainTypeStr)) {
 
 146                 ss.setWorkloadContext(domainTypeStr)
 
 149             AAIResourcesClient client = getAAIClient()
 
 151                     AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business()
 
 152                             .customer(execution.getVariable("globalSubscriberId"))
 
 153                             .serviceSubscription(execution.getVariable("subscriptionServiceType"))
 
 154                             .serviceInstance(ssInstanceId))
 
 155             client.create(uri, ss)
 
 156         } catch (BpmnError e) {
 
 158         } catch (Exception ex) {
 
 159             String msg = "Exception in DoCreateTnNssiInstance.createServiceInstance: " + ex.getMessage()
 
 161             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
 
 166     void createAllottedResource(DelegateExecution execution) {
 
 167         String ssInstanceId = execution.getVariable('sliceServiceInstanceId')
 
 170             List<String> networkStrList = jsonUtil.StringArrayToList(execution.getVariable("transportSliceNetworks"))
 
 172             for (String networkStr : networkStrList) {
 
 173                 String allottedResourceId = UUID.randomUUID().toString()
 
 174                 AAIResourceUri allottedResourceUri =
 
 175                         AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business()
 
 176                                 .customer(execution.getVariable("globalSubscriberId"))
 
 177                                 .serviceSubscription(execution.getVariable("subscriptionServiceType"))
 
 178                                 .serviceInstance(execution.getVariable("sliceServiceInstanceId"))
 
 179                                 .allottedResource(allottedResourceId))
 
 180                 execution.setVariable("allottedResourceUri", allottedResourceUri)
 
 181                 String modelInvariantId = execution.getVariable("modelInvariantUuid")
 
 182                 String modelVersionId = execution.getVariable("modelUuid")
 
 184                 org.onap.aai.domain.yang.AllottedResource resource = new org.onap.aai.domain.yang.AllottedResource()
 
 185                 resource.setId(allottedResourceId)
 
 186                 resource.setType("TsciNetwork")
 
 187                 resource.setAllottedResourceName("network_" + execution.getVariable("sliceServiceInstanceName"))
 
 188                 getAAIClient().create(allottedResourceUri, resource)
 
 190                 createNetworkPolicyForAllocatedResource(execution, ssInstanceId, allottedResourceId)
 
 192                 String linkArrayStr = jsonUtil.getJsonValue(networkStr, "connectionLinks")
 
 193                 createLogicalLinksForAllocatedResource(execution, linkArrayStr, ssInstanceId, allottedResourceId)
 
 195         } catch (BpmnError e) {
 
 197         } catch (Exception ex) {
 
 198             String msg = "Exception in DoCreateTnNssiInstance.createAllottedResource: " + ex.getMessage()
 
 200             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
 
 204     void createNetworkPolicy(DelegateExecution execution, String ssInstanceId, String networkPolicyId) {
 
 207             String sliceProfileStr = execution.getVariable("sliceProfile")
 
 208             if (sliceProfileStr == null || sliceProfileStr.isEmpty()) {
 
 209                 String msg = "ERROR: createNetworkPolicy: sliceProfile is null"
 
 211                 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
 
 214             NetworkPolicy networkPolicy = new NetworkPolicy();
 
 215             networkPolicy.setNetworkPolicyId(networkPolicyId)
 
 216             networkPolicy.setName("TSCi policy")
 
 217             networkPolicy.setType("SLA")
 
 218             networkPolicy.setNetworkPolicyFqdn(ssInstanceId)
 
 220             String latencyStr = jsonUtil.getJsonValue(sliceProfileStr, "latency")
 
 221             if (latencyStr != null && !latencyStr.isEmpty()) {
 
 222                 networkPolicy.setLatency(Integer.parseInt(latencyStr))
 
 225             String bwStr = jsonUtil.getJsonValue(sliceProfileStr, "maxBandwidth")
 
 226             if (bwStr != null && !bwStr.isEmpty()) {
 
 227                 networkPolicy.setMaxBandwidth(Integer.parseInt(bwStr))
 
 229                 log.debug("ERROR: createNetworkPolicy: maxBandwidth is null")
 
 232             //networkPolicy.setReliability(new Object())
 
 234             AAIResourceUri networkPolicyUri =
 
 235                     AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.network().networkPolicy(networkPolicyId))
 
 236             getAAIClient().create(networkPolicyUri, networkPolicy)
 
 238         } catch (BpmnError e) {
 
 240         } catch (Exception ex) {
 
 241             String msg = "Exception in DoCreateSliceServiceInstance.instantiateSliceService. " + ex.getMessage()
 
 243             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
 
 247     void createNetworkPolicyForAllocatedResource(DelegateExecution execution,
 
 249                                                  String allottedResourceId) {
 
 251             AAIResourceUri allottedResourceUri =
 
 252                     AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business()
 
 253                             .customer(execution.getVariable("globalSubscriberId"))
 
 254                             .serviceSubscription(execution.getVariable("subscriptionServiceType"))
 
 255                             .serviceInstance(ssInstanceId)
 
 256                             .allottedResource(allottedResourceId))
 
 258             if (!getAAIClient().exists(allottedResourceUri)) {
 
 259                 logger.info("ERROR: createLogicalLinksForAllocatedResource: allottedResource not exist: uri={}",
 
 264             String networkPolicyId = UUID.randomUUID().toString()
 
 265             createNetworkPolicy(execution, ssInstanceId, networkPolicyId)
 
 267             tnNssmfUtils.attachNetworkPolicyToAllottedResource(execution, AAI_VERSION, allottedResourceUri, networkPolicyId);
 
 269         } catch (BpmnError e) {
 
 271         } catch (Exception ex) {
 
 272             String msg = "Exception in DoCreateSliceServiceInstance.instantiateSliceService. " + ex.getMessage()
 
 274             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
 
 278     void createLogicalLinksForAllocatedResource(DelegateExecution execution,
 
 279                                                 String linkArrayStr, String ssInstanceId,
 
 280                                                 String allottedResourceId) {
 
 282             AAIResourceUri allottedResourceUri =
 
 283                     AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business()
 
 284                             .customer(execution.getVariable("globalSubscriberId"))
 
 285                             .serviceSubscription(execution.getVariable("subscriptionServiceType"))
 
 286                             .serviceInstance(ssInstanceId)
 
 287                             .allottedResource(allottedResourceId))
 
 289             if (!getAAIClient().exists(allottedResourceUri)) {
 
 290                 logger.info("ERROR: createLogicalLinksForAllocatedResource: allottedResource not exist: uri={}",
 
 295             List<String> linkStrList = jsonUtil.StringArrayToList(linkArrayStr)
 
 297             for (String linkStr : linkStrList) {
 
 298                 String linkName = jsonUtil.getJsonValue(linkStr, "name")
 
 299                 if (isBlank(linkName)) {
 
 300                     linkName = "tn-nssmf-" + UUID.randomUUID().toString()
 
 302                 logger.debug("createLogicalLinksForAllocatedResource: linkName=" + linkName)
 
 304                 String epA = jsonUtil.getJsonValue(linkStr, "transportEndpointA")
 
 305                 String epB = jsonUtil.getJsonValue(linkStr, "transportEndpointB")
 
 306                 String modelInvariantId = execution.getVariable("modelInvariantUuid")
 
 307                 String modelVersionId = execution.getVariable("modelUuid")
 
 309                 org.onap.aai.domain.yang.LogicalLink resource = new org.onap.aai.domain.yang.LogicalLink()
 
 310                 resource.setLinkName(linkName)
 
 311                 resource.setLinkId(epA)
 
 312                 resource.setLinkName2(epB)
 
 313                 resource.setLinkType("TsciConnectionLink")
 
 314                 resource.setInMaint(false)
 
 317                 AAIResourceUri logicalLinkUri =
 
 318                         AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.network().logicalLink(linkName))
 
 319                 getAAIClient().create(logicalLinkUri, resource)
 
 321                 tnNssmfUtils.attachLogicalLinkToAllottedResource(execution, AAI_VERSION, allottedResourceUri, linkName);
 
 323         } catch (BpmnError e) {
 
 325         } catch (Exception ex) {
 
 326             String msg = "Exception in DoCreateTnNssiInstance.createLogicalLinksForAllocatedResource: " + ex.getMessage()
 
 328             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
 
 332     void preprocessSdncAllocateTnNssiRequest(DelegateExecution execution) {
 
 333         def method = getClass().getSimpleName() + '.preProcessSDNCActivateRequest(' +
 
 334                 'execution=' + execution.getId() +
 
 336         def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
 
 337         logger.trace('Entered ' + method)
 
 339         logger.trace("STARTED preProcessSDNCActivateRequest Process")
 
 341             String serviceInstanceId = execution.getVariable("sliceServiceInstanceId")
 
 343             String createSDNCRequest = tnNssmfUtils.buildSDNCRequest(execution, serviceInstanceId, "create")
 
 345             execution.setVariable("TNNSSMF_SDNCRequest", createSDNCRequest)
 
 346             logger.debug("Outgoing SDNCRequest is: \n" + createSDNCRequest)
 
 348         } catch (Exception e) {
 
 349             logger.debug("Exception Occured Processing preProcessSDNCActivateRequest. Exception is:\n" + e)
 
 350             exceptionUtil.buildAndThrowWorkflowException(execution, 1002,
 
 351                     "Error Occured during  preProcessSDNCActivateRequest Method:\n" + e.getMessage())
 
 353         logger.trace("COMPLETED  preProcessSDNCActivateRequest Process")
 
 357     void validateSDNCResponse(DelegateExecution execution, String response, String method) {
 
 358         tnNssmfUtils.validateSDNCResponse(execution, response, method)