Fix bugs found in TN Slicing integration involving OOF
[so.git] / bpmn / so-bpmn-infrastructure-common / src / main / groovy / org / onap / so / bpmn / infrastructure / scripts / DoCreateTnNssiInstance.groovy
index 7beafef..d2ba282 100644 (file)
@@ -22,6 +22,7 @@ package org.onap.so.bpmn.infrastructure.scripts
 
 import org.camunda.bpm.engine.delegate.BpmnError
 import org.camunda.bpm.engine.delegate.DelegateExecution
+import org.onap.aai.domain.yang.NetworkPolicy
 import org.onap.aai.domain.yang.SliceProfile
 import org.onap.aaiclient.client.aai.AAIResourcesClient
 import org.onap.aaiclient.client.aai.entities.uri.AAIResourceUri
@@ -33,10 +34,12 @@ import org.onap.so.bpmn.core.json.JsonUtils
 import org.slf4j.Logger
 import org.slf4j.LoggerFactory
 
+import static org.apache.commons.lang3.StringUtils.isBlank
+import static org.apache.commons.lang3.StringUtils.isNotBlank
+
 class DoCreateTnNssiInstance extends AbstractServiceTaskProcessor {
 
     private static final Logger logger = LoggerFactory.getLogger(DoCreateTnNssiInstance.class);
-    final String AAI_VERSION = "v21"
     JsonUtils jsonUtil = new JsonUtils()
     TnNssmfUtils tnNssmfUtils = new TnNssmfUtils()
     ExceptionUtil exceptionUtil = new ExceptionUtil()
@@ -60,6 +63,10 @@ class DoCreateTnNssiInstance extends AbstractServiceTaskProcessor {
              }"""
         execution.setVariable("serviceModelInfo", serviceModelInfo)
 
+        if (isBlank(execution.getVariable("enableSdnc"))) {
+            tnNssmfUtils.setEnableSdncConfig(execution)
+        }
+
         logger.trace("Exit preProcessRequest")
     }
 
@@ -100,28 +107,50 @@ class DoCreateTnNssiInstance extends AbstractServiceTaskProcessor {
 
     void createServiceInstance(DelegateExecution execution) {
 
-        String serviceRole = "TN"
+        String serviceRole = "nssi"
         String serviceType = execution.getVariable("subscriptionServiceType")
         String ssInstanceId = execution.getVariable("sliceServiceInstanceId")
         String sliceProfileStr = execution.getVariable("sliceProfile")
+        String sst = execution.getVariable("sst")
         try {
+            if (sliceProfileStr == null || sliceProfileStr.isEmpty()) {
+                String msg = "ERROR: createServiceInstance: sliceProfile is null"
+                logger.error(msg)
+                exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
+            }
+
             org.onap.aai.domain.yang.ServiceInstance ss = new org.onap.aai.domain.yang.ServiceInstance()
             ss.setServiceInstanceId(ssInstanceId)
             String sliceInstanceName = execution.getVariable("sliceServiceInstanceName")
+            if (isBlank(sliceInstanceName)) {
+                logger.error("ERROR: createServiceInstance: sliceInstanceName is null")
+                sliceInstanceName = ssInstanceId
+            }
             ss.setServiceInstanceName(sliceInstanceName)
-            ss.setServiceType(serviceType)
-            String serviceStatus = "allocated"
+            ss.setServiceType(sst)
+            String serviceStatus = "deactivated"
             ss.setOrchestrationStatus(serviceStatus)
             String modelInvariantUuid = execution.getVariable("modelInvariantUuid")
             String modelUuid = execution.getVariable("modelUuid")
-            //TODO: need valid model ID from the caller, as AAI does not accept invalid IDs
-            //ss.setModelInvariantId(modelInvariantUuid)
-            //ss.setModelVersionId(modelUuid)
-            String serviceInstanceLocationid = tnNssmfUtils.getFirstPlmnIdFromSliceProfile(sliceProfileStr)
-            ss.setServiceInstanceLocationId(serviceInstanceLocationid)
+            ss.setModelInvariantId(modelInvariantUuid)
+            ss.setModelVersionId(modelUuid)
+            String serviceInstanceLocationId = tnNssmfUtils.getFirstPlmnIdFromSliceProfile(sliceProfileStr)
+            ss.setServiceInstanceLocationId(serviceInstanceLocationId)
             String snssai = tnNssmfUtils.getFirstSnssaiFromSliceProfile(sliceProfileStr)
-            ss.setEnvironmentContext(snssai)
+            //ss.setEnvironmentContext(snssai)
+            ss.setEnvironmentContext("tn")
             ss.setServiceRole(serviceRole)
+
+            String domainTypeStr = jsonUtil.getJsonValue(sliceProfileStr, "domainType")
+            if (isNotBlank(domainTypeStr)) {
+                ss.setWorkloadContext(domainTypeStr)
+            }
+
+            String resourceSharingLevel = jsonUtil.getJsonValue(sliceProfileStr, "resourceSharingLevel")
+            if (isNotBlank(resourceSharingLevel)) {
+                ss.setServiceFunction(resourceSharingLevel)
+            }
+
             AAIResourcesClient client = getAAIClient()
             AAIResourceUri uri =
                     AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business()
@@ -133,7 +162,7 @@ class DoCreateTnNssiInstance extends AbstractServiceTaskProcessor {
             throw e
         } catch (Exception ex) {
             String msg = "Exception in DoCreateTnNssiInstance.createServiceInstance: " + ex.getMessage()
-            logger.info(msg)
+            logger.error(msg)
             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
         }
     }
@@ -163,6 +192,8 @@ class DoCreateTnNssiInstance extends AbstractServiceTaskProcessor {
                 resource.setAllottedResourceName("network_" + execution.getVariable("sliceServiceInstanceName"))
                 getAAIClient().create(allottedResourceUri, resource)
 
+                createNetworkPolicyForAllocatedResource(execution, ssInstanceId, allottedResourceId)
+
                 String linkArrayStr = jsonUtil.getJsonValue(networkStr, "connectionLinks")
                 createLogicalLinksForAllocatedResource(execution, linkArrayStr, ssInstanceId, allottedResourceId)
             }
@@ -170,7 +201,83 @@ class DoCreateTnNssiInstance extends AbstractServiceTaskProcessor {
             throw e
         } catch (Exception ex) {
             String msg = "Exception in DoCreateTnNssiInstance.createAllottedResource: " + ex.getMessage()
-            logger.info(msg)
+            logger.error(msg)
+            exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
+        }
+    }
+
+    void createNetworkPolicy(DelegateExecution execution, String ssInstanceId, String networkPolicyId) {
+        try {
+
+            String sliceProfileStr = execution.getVariable("sliceProfile")
+            if (sliceProfileStr == null || sliceProfileStr.isEmpty()) {
+                String msg = "ERROR: createNetworkPolicy: sliceProfile is null"
+                logger.error(msg)
+                exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
+            }
+
+            NetworkPolicy networkPolicy = new NetworkPolicy();
+            networkPolicy.setNetworkPolicyId(networkPolicyId)
+            networkPolicy.setName("TSCi policy")
+            networkPolicy.setType("SLA")
+            networkPolicy.setNetworkPolicyFqdn(ssInstanceId)
+
+            String latencyStr = jsonUtil.getJsonValue(sliceProfileStr, "latency")
+            if (latencyStr != null && !latencyStr.isEmpty()) {
+                networkPolicy.setLatency(Integer.parseInt(latencyStr))
+            }
+
+            String bwStr = jsonUtil.getJsonValue(sliceProfileStr, "maxBandwidth")
+            if (bwStr != null && !bwStr.isEmpty()) {
+                networkPolicy.setMaxBandwidth(Integer.parseInt(bwStr))
+            } else {
+                logger.debug("ERROR: createNetworkPolicy: maxBandwidth is null")
+            }
+
+            //networkPolicy.setReliability(new Object())
+
+            AAIResourceUri networkPolicyUri =
+                    AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.network().networkPolicy(networkPolicyId))
+            getAAIClient().create(networkPolicyUri, networkPolicy)
+
+        } catch (BpmnError e) {
+            throw e
+        } catch (Exception ex) {
+            String msg = "Exception in DoCreateSliceServiceInstance.instantiateSliceService. " + ex.getMessage()
+            logger.error(msg)
+            exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
+        }
+    }
+
+    void createNetworkPolicyForAllocatedResource(DelegateExecution execution,
+                                                 String ssInstanceId,
+                                                 String allottedResourceId) {
+        try {
+            AAIResourceUri allottedResourceUri =
+                    AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business()
+                            .customer(execution.getVariable("globalSubscriberId"))
+                            .serviceSubscription(execution.getVariable("subscriptionServiceType"))
+                            .serviceInstance(ssInstanceId)
+                            .allottedResource(allottedResourceId))
+
+            if (!getAAIClient().exists(allottedResourceUri)) {
+                logger.info("ERROR: createLogicalLinksForAllocatedResource: allottedResource not exist: uri={}",
+                        allottedResourceUri)
+                return
+            }
+
+            String networkPolicyId = UUID.randomUUID().toString()
+            createNetworkPolicy(execution, ssInstanceId, networkPolicyId)
+
+            tnNssmfUtils.attachNetworkPolicyToAllottedResource(execution, tnNssmfUtils.AAI_VERSION,
+                    allottedResourceUri,
+                    networkPolicyId);
+
+        } catch (BpmnError e) {
+            throw e
+        } catch (Exception ex) {
+            String msg = "Exception in DoCreateSliceServiceInstance.instantiateSliceService. " + ex.getMessage()
+            logger.error(msg)
             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
         }
     }
@@ -195,14 +302,19 @@ class DoCreateTnNssiInstance extends AbstractServiceTaskProcessor {
             List<String> linkStrList = jsonUtil.StringArrayToList(linkArrayStr)
 
             for (String linkStr : linkStrList) {
-                String logicalLinkId = UUID.randomUUID().toString()
+                String linkId = jsonUtil.getJsonValue(linkStr, "name")
+                if (isBlank(linkId)) {
+                    linkId = "tn-nssmf-" + UUID.randomUUID().toString()
+                }
+                logger.debug("createLogicalLinksForAllocatedResource: linkId=" + linkId)
+
                 String epA = jsonUtil.getJsonValue(linkStr, "transportEndpointA")
                 String epB = jsonUtil.getJsonValue(linkStr, "transportEndpointB")
                 String modelInvariantId = execution.getVariable("modelInvariantUuid")
                 String modelVersionId = execution.getVariable("modelUuid")
 
                 org.onap.aai.domain.yang.LogicalLink resource = new org.onap.aai.domain.yang.LogicalLink()
-                resource.setLinkId(logicalLinkId)
+                resource.setLinkId(linkId)
                 resource.setLinkName(epA)
                 resource.setLinkName2(epB)
                 resource.setLinkType("TsciConnectionLink")
@@ -213,13 +325,14 @@ class DoCreateTnNssiInstance extends AbstractServiceTaskProcessor {
                         AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.network().logicalLink(epA))
                 getAAIClient().create(logicalLinkUri, resource)
 
-                tnNssmfUtils.attachLogicalLinkToAllottedResource(execution, AAI_VERSION, allottedResourceUri, epA);
+                tnNssmfUtils.attachLogicalLinkToAllottedResource(execution, tnNssmfUtils.AAI_VERSION,
+                        allottedResourceUri, epA);
             }
         } catch (BpmnError e) {
             throw e
         } catch (Exception ex) {
             String msg = "Exception in DoCreateTnNssiInstance.createLogicalLinksForAllocatedResource: " + ex.getMessage()
-            logger.info(msg)
+            logger.error(msg)
             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
         }
     }