Merge "add junit coverage"
[so.git] / bpmn / MSOCommonBPMN / src / main / java / org / onap / so / bpmn / servicedecomposition / tasks / BBInputSetup.java
index 3f664cb..88c36be 100644 (file)
@@ -62,6 +62,7 @@ import org.onap.so.bpmn.servicedecomposition.entities.WorkflowResourceIds;
 import org.onap.so.bpmn.servicedecomposition.generalobjects.OrchestrationContext;
 import org.onap.so.bpmn.servicedecomposition.generalobjects.RequestContext;
 import org.onap.so.bpmn.servicedecomposition.tasks.exceptions.NoServiceInstanceFoundException;
+import org.onap.so.bpmn.servicedecomposition.tasks.exceptions.ServiceModelNotFoundException;
 import org.onap.so.client.aai.AAICommonObjectMapperProvider;
 import org.onap.so.client.aai.AAIObjectType;
 import org.onap.so.client.aai.entities.AAIResultWrapper;
@@ -116,6 +117,7 @@ public class BBInputSetup implements JavaDelegate {
     private static final String VNF = "Vnf";
     private static final String NETWORK_COLLECTION = "NetworkCollection";
     private static final String PREPROV = "PREPROV";
+    private static final String CREATEVOLUME = "CreateVolume";
 
     @Autowired
     private BBInputSetupUtils bbInputSetupUtils;
@@ -253,41 +255,46 @@ public class BBInputSetup implements JavaDelegate {
         String bbName = executeBB.getBuildingBlock().getBpmnFlowName();
         String serviceInstanceId = lookupKeyMap.get(ResourceKey.SERVICE_INSTANCE_ID);
         org.onap.aai.domain.yang.ServiceInstance aaiServiceInstance = null;
-        if (serviceInstanceId != null) {
-            aaiServiceInstance = bbInputSetupUtils.getAAIServiceInstanceById(serviceInstanceId);
-        }
         Service service = null;
         boolean isReplace = false;
-        if (aaiServiceInstance != null) {
-            if (requestAction.equalsIgnoreCase("replaceInstance")) {
-                RelatedInstanceList[] relatedInstanceList = requestDetails.getRelatedInstanceList();
-                if (relatedInstanceList != null) {
-                    for (RelatedInstanceList relatedInstList : relatedInstanceList) {
-                        RelatedInstance relatedInstance = relatedInstList.getRelatedInstance();
-                        if (relatedInstance.getModelInfo().getModelType().equals(ModelType.service)) {
-                            service = bbInputSetupUtils
-                                    .getCatalogServiceByModelUUID(relatedInstance.getModelInfo().getModelVersionId());
-                            isReplace = true;
+        if (serviceInstanceId != null) {
+            aaiServiceInstance = bbInputSetupUtils.getAAIServiceInstanceById(serviceInstanceId);
+            if (aaiServiceInstance != null) {
+                if (requestAction.equalsIgnoreCase("replaceInstance")) {
+                    RelatedInstanceList[] relatedInstanceList = requestDetails.getRelatedInstanceList();
+                    if (relatedInstanceList != null) {
+                        for (RelatedInstanceList relatedInstList : relatedInstanceList) {
+                            RelatedInstance relatedInstance = relatedInstList.getRelatedInstance();
+                            if (relatedInstance.getModelInfo().getModelType().equals(ModelType.service)) {
+                                service = bbInputSetupUtils.getCatalogServiceByModelUUID(
+                                        relatedInstance.getModelInfo().getModelVersionId());
+                                isReplace = true;
+                            }
                         }
                     }
+                } else {
+                    service = bbInputSetupUtils.getCatalogServiceByModelUUID(aaiServiceInstance.getModelVersionId());
+                }
+                if (service == null) {
+                    String message = String.format(
+                            "Related service instance model not found in MSO CatalogDB: model-version-id=%s",
+                            aaiServiceInstance.getModelVersionId());
+                    throw new ServiceModelNotFoundException(message);
                 }
             } else {
-                service = bbInputSetupUtils.getCatalogServiceByModelUUID(aaiServiceInstance.getModelVersionId());
+                String message = String.format("Related service instance from AAI not found: service-instance-id=%s",
+                        serviceInstanceId);
+                throw new NoServiceInstanceFoundException(message);
             }
         }
-        if (aaiServiceInstance != null && service != null) {
-            ServiceInstance serviceInstance = this.getExistingServiceInstance(aaiServiceInstance);
-            serviceInstance.setModelInfoServiceInstance(this.mapperLayer.mapCatalogServiceIntoServiceInstance(service));
-            this.populateObjectsOnAssignAndCreateFlows(executeBB.getRequestId(), requestDetails, service, bbName,
-                    serviceInstance, lookupKeyMap, resourceId, vnfType, executeBB.getBuildingBlock().getKey(),
-                    executeBB.getConfigurationResourceKeys(), isReplace);
-            return this.populateGBBWithSIAndAdditionalInfo(requestDetails, serviceInstance, executeBB, requestAction,
-                    null);
-        } else {
-            logger.debug("Related Service Instance from AAI: {}", aaiServiceInstance);
-            logger.debug("Related Service Instance Model Info from AAI: {}", service);
-            throw new Exception("Could not find relevant information for related Service Instance");
-        }
+
+        ServiceInstance serviceInstance = this.getExistingServiceInstance(aaiServiceInstance);
+        serviceInstance.setModelInfoServiceInstance(this.mapperLayer.mapCatalogServiceIntoServiceInstance(service));
+        this.populateObjectsOnAssignAndCreateFlows(executeBB.getRequestId(), requestDetails, service, bbName,
+                serviceInstance, lookupKeyMap, resourceId, vnfType, executeBB.getBuildingBlock().getKey(),
+                executeBB.getConfigurationResourceKeys(), isReplace);
+        return this.populateGBBWithSIAndAdditionalInfo(requestDetails, serviceInstance, executeBB, requestAction, null);
+
     }
 
     protected GeneralBuildingBlock getGBBCM(ExecuteBuildingBlock executeBB, RequestDetails requestDetails,
@@ -361,6 +368,10 @@ public class BBInputSetup implements JavaDelegate {
 
         org.onap.so.serviceinstancebeans.Platform platform = requestDetails.getPlatform();
         org.onap.so.serviceinstancebeans.LineOfBusiness lineOfBusiness = requestDetails.getLineOfBusiness();
+        String applicationId = "";
+        if (requestDetails.getRequestInfo().getApplicationId() != null) {
+            applicationId = requestDetails.getRequestInfo().getApplicationId();
+        }
 
         if (modelType.equals(ModelType.network)) {
             lookupKeyMap.put(ResourceKey.NETWORK_ID, resourceId);
@@ -369,8 +380,10 @@ public class BBInputSetup implements JavaDelegate {
         } else if (modelType.equals(ModelType.vnf)) {
             lookupKeyMap.put(ResourceKey.GENERIC_VNF_ID, resourceId);
             this.populateGenericVnf(requestId, modelInfo, instanceName, platform, lineOfBusiness, service, bbName,
-                    serviceInstance, lookupKeyMap, relatedInstanceList, resourceId, vnfType, null, productFamilyId);
-        } else if (modelType.equals(ModelType.volumeGroup)) {
+                    serviceInstance, lookupKeyMap, relatedInstanceList, resourceId, vnfType, null, productFamilyId,
+                    applicationId);
+        } else if (modelType.equals(ModelType.volumeGroup) || (modelType.equals(ModelType.vfModule)
+                && (bbName.equalsIgnoreCase(AssignFlows.VOLUME_GROUP.toString()) || bbName.startsWith(CREATEVOLUME)))) {
             lookupKeyMap.put(ResourceKey.VOLUME_GROUP_ID, resourceId);
             this.populateVolumeGroup(requestId, modelInfo, service, bbName, serviceInstance, lookupKeyMap, resourceId,
                     relatedInstanceList, instanceName, vnfType, null);
@@ -594,6 +607,7 @@ public class BBInputSetup implements JavaDelegate {
                         bbInputSetupUtils.getAAIVolumeGroup(cloudOwner, cloudRegionId, volumeGroup.getVolumeGroupId())
                                 .getModelCustomizationId();
                 if (modelInfo.getModelCustomizationId().equalsIgnoreCase(volumeGroupCustId)) {
+                    logger.debug("Found volume group for vfModule: " + volumeGroup.getVolumeGroupId());
                     return Optional.of(volumeGroup.getVolumeGroupId());
                 }
             }
@@ -748,7 +762,7 @@ public class BBInputSetup implements JavaDelegate {
             org.onap.so.serviceinstancebeans.LineOfBusiness lineOfBusiness, Service service, String bbName,
             ServiceInstance serviceInstance, Map<ResourceKey, String> lookupKeyMap,
             RelatedInstanceList[] relatedInstanceList, String resourceId, String vnfType,
-            List<Map<String, String>> instanceParams, String productFamilyId) {
+            List<Map<String, String>> instanceParams, String productFamilyId, String applicationId) {
         GenericVnf vnf = null;
         ModelInfo instanceGroupModelInfo = null;
         String instanceGroupId = null;
@@ -777,7 +791,7 @@ public class BBInputSetup implements JavaDelegate {
         }
         if (vnf == null && bbName.equalsIgnoreCase(AssignFlows.VNF.toString())) {
             vnf = createGenericVnf(lookupKeyMap, instanceName, platform, lineOfBusiness, resourceId, generatedVnfType,
-                    instanceParams, productFamilyId);
+                    instanceParams, productFamilyId, applicationId);
             serviceInstance.getVnfs().add(vnf);
             mapVnfcCollectionInstanceGroup(vnf, modelInfo, service);
         }
@@ -831,7 +845,7 @@ public class BBInputSetup implements JavaDelegate {
     protected GenericVnf createGenericVnf(Map<ResourceKey, String> lookupKeyMap, String instanceName,
             org.onap.so.serviceinstancebeans.Platform platform,
             org.onap.so.serviceinstancebeans.LineOfBusiness lineOfBusiness, String vnfId, String vnfType,
-            List<Map<String, String>> instanceParams, String productFamilyId) {
+            List<Map<String, String>> instanceParams, String productFamilyId, String applicationId) {
         lookupKeyMap.put(ResourceKey.GENERIC_VNF_ID, vnfId);
         GenericVnf genericVnf = new GenericVnf();
         genericVnf.setVnfId(vnfId);
@@ -840,6 +854,7 @@ public class BBInputSetup implements JavaDelegate {
         genericVnf.setVnfType(vnfType);
         genericVnf.setProvStatus(PREPROV);
         genericVnf.setServiceId(productFamilyId);
+        genericVnf.setApplicationId(applicationId);
         if (platform != null) {
             genericVnf.setPlatform(this.mapperLayer.mapRequestPlatform(platform));
         }
@@ -1279,24 +1294,30 @@ public class BBInputSetup implements JavaDelegate {
         String serviceInstanceId = lookupKeyMap.get(ResourceKey.SERVICE_INSTANCE_ID);
         RequestDetails requestDetails = executeBB.getRequestDetails();
         GeneralBuildingBlock gBB = null;
+        Service service = null;
         if (serviceInstanceId != null) {
             aaiServiceInstance = bbInputSetupUtils.getAAIServiceInstanceById(serviceInstanceId);
+            if (aaiServiceInstance != null) {
+                service = bbInputSetupUtils.getCatalogServiceByModelUUID(aaiServiceInstance.getModelVersionId());
+                if (service == null) {
+                    String message = String.format(
+                            "Related service instance model not found in MSO CatalogDB: model-version-id=%s",
+                            aaiServiceInstance.getModelVersionId());
+                    throw new ServiceModelNotFoundException(message);
+                }
+            } else {
+                String message = String.format("Related service instance from AAI not found: service-instance-id=%s",
+                        serviceInstanceId);
+                throw new NoServiceInstanceFoundException(message);
+            }
         }
-        Service service = null;
-        if (aaiServiceInstance != null) {
-            service = bbInputSetupUtils.getCatalogServiceByModelUUID(aaiServiceInstance.getModelVersionId());
-        }
-        if (aaiServiceInstance != null && service != null) {
-            ServiceInstance serviceInstance = this.getExistingServiceInstance(aaiServiceInstance);
-            serviceInstance.setModelInfoServiceInstance(this.mapperLayer.mapCatalogServiceIntoServiceInstance(service));
-            updateInstanceName(executeBB.getRequestId(), ModelType.service, serviceInstance.getServiceInstanceName());
-            gBB = populateGBBWithSIAndAdditionalInfo(requestDetails, serviceInstance, executeBB, requestAction, null);
-        } else {
-            logger.debug("Related Service Instance from AAI: {}", aaiServiceInstance);
-            logger.debug("Related Service Instance Model Info from AAI: {}", service);
-            throw new Exception("Could not find relevant information for related Service Instance");
-        }
-        ServiceInstance serviceInstance = gBB.getServiceInstance();
+
+        ServiceInstance serviceInstance = this.getExistingServiceInstance(aaiServiceInstance);
+        serviceInstance.setModelInfoServiceInstance(this.mapperLayer.mapCatalogServiceIntoServiceInstance(service));
+        updateInstanceName(executeBB.getRequestId(), ModelType.service, serviceInstance.getServiceInstanceName());
+        gBB = populateGBBWithSIAndAdditionalInfo(requestDetails, serviceInstance, executeBB, requestAction, null);
+
+        serviceInstance = gBB.getServiceInstance();
         CloudRegion cloudRegion = null;
         if (cloudConfiguration == null) {
             Optional<CloudRegion> cloudRegionOp = cloudInfoFromAAI.getCloudInfoFromAAI(serviceInstance);
@@ -1431,9 +1452,10 @@ public class BBInputSetup implements JavaDelegate {
                 this.bbInputSetupUtils.updateInfraActiveRequestVnfId(request, vnfId);
             }
             String productFamilyId = requestDetails.getRequestInfo().getProductFamilyId();
+            String applicationId = "";
             this.populateGenericVnf(executeBB.getRequestId(), vnfs.getModelInfo(), vnfs.getInstanceName(),
                     vnfs.getPlatform(), vnfs.getLineOfBusiness(), service, bbName, serviceInstance, lookupKeyMap, null,
-                    vnfId, vnfType, vnfs.getInstanceParams(), productFamilyId);
+                    vnfId, vnfType, vnfs.getInstanceParams(), productFamilyId, applicationId);
         } else if (bbName.contains(VF_MODULE) || bbName.contains(VOLUME_GROUP)) {
             Pair<Vnfs, VfModules> vnfsAndVfModules = getVfModulesAndItsVnfsByKey(key, resources);
             if (vnfsAndVfModules != null) {