fix catalogdb cvfnc customization
[so.git] / bpmn / so-bpmn-tasks / src / main / java / org / onap / so / bpmn / infrastructure / workflow / tasks / WorkflowAction.java
index 99bda80..867be2e 100644 (file)
@@ -41,6 +41,7 @@ import org.onap.aai.domain.yang.GenericVnf;
 import org.onap.aai.domain.yang.L3Network;
 import org.onap.aai.domain.yang.Relationship;
 import org.onap.aai.domain.yang.ServiceInstance;
+import org.onap.aai.domain.yang.Vnfc;
 import org.onap.aai.domain.yang.VolumeGroup;
 import org.onap.so.bpmn.servicedecomposition.bbobjects.Configuration;
 import org.onap.so.bpmn.servicedecomposition.bbobjects.VfModule;
@@ -51,8 +52,11 @@ import org.onap.so.bpmn.servicedecomposition.entities.WorkflowResourceIds;
 import org.onap.so.bpmn.servicedecomposition.tasks.BBInputSetup;
 import org.onap.so.bpmn.servicedecomposition.tasks.BBInputSetupUtils;
 import org.onap.so.client.aai.AAICommonObjectMapperProvider;
+import org.onap.so.client.aai.AAIObjectType;
 import org.onap.so.client.aai.entities.AAIResultWrapper;
 import org.onap.so.client.aai.entities.Relationships;
+import org.onap.so.client.aai.entities.uri.AAIResourceUri;
+import org.onap.so.client.aai.entities.uri.AAIUriFactory;
 import org.onap.so.client.exception.ExceptionBuilder;
 import org.onap.so.client.orchestration.AAIConfigurationResources;
 import org.onap.so.db.catalog.beans.CollectionNetworkResourceCustomization;
@@ -60,7 +64,7 @@ import org.onap.so.db.catalog.beans.CollectionResourceCustomization;
 import org.onap.so.db.catalog.beans.CollectionResourceInstanceGroupCustomization;
 import org.onap.so.db.catalog.beans.CvnfcCustomization;
 import org.onap.so.db.catalog.beans.VfModuleCustomization;
-import org.onap.so.db.catalog.beans.VnfVfmoduleCvnfcConfigurationCustomization;
+import org.onap.so.db.catalog.beans.CvnfcConfigurationCustomization;
 import org.onap.so.db.catalog.beans.macro.NorthBoundRequest;
 import org.onap.so.db.catalog.beans.macro.OrchestrationFlow;
 import org.onap.so.db.catalog.client.CatalogDbClient;
@@ -108,7 +112,7 @@ public class WorkflowAction {
        private static final String ASSIGNINSTANCE = "assignInstance";
        private static final String CREATEINSTANCE = "createInstance";
        private static final String USERPARAMSERVICE = "service";
-       private static final String supportedTypes = "vnfs|vfModules|networks|networkCollections|volumeGroups|serviceInstances";
+       private static final String supportedTypes = "vnfs|vfModules|networks|networkCollections|volumeGroups|serviceInstances|instanceGroups";
        private static final String HOMINGSOLUTION = "Homing_Solution";
        private static final String FABRIC_CONFIGURATION = "FabricConfiguration";
        private static final String G_SERVICE_TYPE = "serviceType";
@@ -199,15 +203,23 @@ public class WorkflowAction {
                                if (orchFlows == null || orchFlows.isEmpty()) {
                                                orchFlows = queryNorthBoundRequestCatalogDb(execution, requestAction, resourceType, aLaCarte, cloudOwner, serviceType);
                                }
-                               orchFlows = filterOrchFlows(sIRequest, orchFlows, resourceType, execution);
                                String key = "";
                                ModelInfo modelInfo = sIRequest.getRequestDetails().getModelInfo();
-                               if(modelInfo.getModelType().equals(ModelType.service)) {
-                                       key = modelInfo.getModelVersionId();
-                               } else {
-                                       key = modelInfo.getModelCustomizationId();
+                               if(modelInfo != null) {
+                                       if(modelInfo.getModelType().equals(ModelType.service)) {
+                                               key = modelInfo.getModelVersionId();
+                                       } else {
+                                               key = modelInfo.getModelCustomizationId();
+                                       }
                                }
+                               boolean isConfiguration = isConfiguration(orchFlows);
                                Resource resourceKey = new Resource(resourceType, key, aLaCarte);
+                               if(isConfiguration && !requestAction.equalsIgnoreCase(CREATEINSTANCE)) {
+                                       List<ExecuteBuildingBlock> configBuildingBlocks = getConfigBuildingBlocks(sIRequest, orchFlows, requestId, resourceKey, apiVersion, resourceId, requestAction, aLaCarte, vnfType,
+                                                       workflowResourceIds, requestDetails, execution);
+                                       flowsToExecute.addAll(configBuildingBlocks);
+                               }
+                               orchFlows = orchFlows.stream().filter(item -> !item.getFlowName().contains(FABRIC_CONFIGURATION)).collect(Collectors.toList());
                                for (OrchestrationFlow orchFlow : orchFlows) {
                                        ExecuteBuildingBlock ebb = buildExecuteBuildingBlock(orchFlow, requestId, resourceKey, apiVersion, resourceId,
                                                        requestAction, aLaCarte, vnfType, workflowResourceIds, requestDetails, false, null, false);
@@ -339,6 +351,88 @@ public class WorkflowAction {
                        buildAndThrowException(execution, "Exception in create execution list " + ex.getMessage(), ex);
                }
        }
+       
+       protected <T> List<T> getRelatedResourcesInVfModule(String vnfId, String vfModuleId, Class<T> resultClass, AAIObjectType type) {
+               List<T> vnfcs = new ArrayList<>();
+               AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.VF_MODULE, vnfId, vfModuleId);
+               AAIResultWrapper vfModuleResultsWrapper = bbInputSetupUtils.getAAIResourceDepthOne(uri);
+               Optional<Relationships> relationshipsOp = vfModuleResultsWrapper.getRelationships();
+               if (!relationshipsOp.isPresent()) {
+                       logger.debug("No relationships were found for vfModule in AAI");
+               } else {
+                       Relationships relationships = relationshipsOp.get();
+                       List<AAIResultWrapper> vnfcResultWrappers = relationships.getByType(type);
+                       for(AAIResultWrapper vnfcResultWrapper : vnfcResultWrappers) {
+                               Optional<T> vnfcOp = vnfcResultWrapper.asBean(resultClass);
+                               if(vnfcOp.isPresent()) {
+                                       vnfcs.add(vnfcOp.get());
+                               }
+                       }
+               }
+               return vnfcs;
+       }
+       
+       protected boolean isConfiguration(List<OrchestrationFlow> orchFlows) {
+               for(OrchestrationFlow flow : orchFlows) {
+                       if(flow.getFlowName().contains("Configuration")) {
+                               return true;
+                       }
+               }
+               return false;
+       }
+
+       protected List<ExecuteBuildingBlock> getConfigBuildingBlocks(ServiceInstancesRequest sIRequest, List<OrchestrationFlow> orchFlows, String requestId, Resource resourceKey,
+                       String apiVersion, String resourceId, String requestAction, boolean aLaCarte, String vnfType,
+                       WorkflowResourceIds workflowResourceIds, RequestDetails requestDetails, DelegateExecution execution) {
+               List<ExecuteBuildingBlock> flowsToExecuteConfigs = new ArrayList<>();
+               List<OrchestrationFlow> result = new ArrayList<>(orchFlows);
+               result = orchFlows.stream().filter(item -> item.getFlowName().contains(FABRIC_CONFIGURATION)).collect(Collectors.toList());
+               String vnfId = workflowResourceIds.getVnfId();
+               String vfModuleId = workflowResourceIds.getVfModuleId();
+               
+               String vnfCustomizationUUID = bbInputSetupUtils.getAAIGenericVnf(vnfId).getModelCustomizationId();
+               String vfModuleCustomizationUUID = bbInputSetupUtils.getAAIVfModule(vnfId, vfModuleId).getModelCustomizationId();
+               List<org.onap.aai.domain.yang.Configuration> configurations = getRelatedResourcesInVfModule(vnfId, vfModuleId, org.onap.aai.domain.yang.Configuration.class, AAIObjectType.CONFIGURATION);
+               
+               for(org.onap.aai.domain.yang.Configuration configuration : configurations) {
+                       workflowResourceIds.setConfigurationId(configuration.getConfigurationId());
+                       for(OrchestrationFlow orchFlow : result) {
+                               resourceKey.setVfModuleCustomizationId(vfModuleCustomizationUUID);
+                               resourceKey.setCvnfModuleCustomizationId(configuration.getModelCustomizationId());
+                               resourceKey.setVnfCustomizationId(vnfCustomizationUUID);
+                               ExecuteBuildingBlock ebb = buildExecuteBuildingBlock(orchFlow, requestId, resourceKey, apiVersion, resourceId,
+                                               requestAction, aLaCarte, vnfType, workflowResourceIds, requestDetails, false, null, true);
+                               String vnfcName = getVnfcNameForConfiguration(configuration);
+                               if(vnfcName == null || vnfcName.isEmpty()) {
+                                       buildAndThrowException(execution, "Exception in create execution list " + ": VnfcName does not exist or is null while there is a configuration for the vfModule", new Exception("Vnfc and Configuration do not match"));
+                               }
+                               ebb.getConfigurationResourceKeys().setVnfcName(vnfcName);
+                               flowsToExecuteConfigs.add(ebb);
+                       }
+               }
+               return flowsToExecuteConfigs;
+       }
+       
+       protected String getVnfcNameForConfiguration(org.onap.aai.domain.yang.Configuration configuration) {
+               AAIResultWrapper wrapper = new AAIResultWrapper(configuration);
+               Optional<Relationships> relationshipsOp = wrapper.getRelationships();
+               if (!relationshipsOp.isPresent()) {
+                       logger.debug("No relationships were found for Configuration in AAI");
+                       return null;
+               } else {
+                       Relationships relationships = relationshipsOp.get();
+                       List<AAIResultWrapper> vnfcResultWrappers = relationships.getByType(AAIObjectType.VNFC);
+                       if(vnfcResultWrappers.size() > 1 || vnfcResultWrappers.isEmpty()) {
+                               logger.debug("Too many vnfcs or no vnfc found that are related to configuration");
+                       }
+                       Optional<Vnfc> vnfcOp = vnfcResultWrappers.get(0).asBean(Vnfc.class);
+                       if(vnfcOp.isPresent()) {
+                               return vnfcOp.get().getVnfcName();
+                       } else {
+                               return null;
+                       }
+               }
+       }
 
        protected List<Resource> sortVfModulesByBaseFirst(List<Resource> vfModuleResources) {
                int count = 0;
@@ -698,9 +792,9 @@ public class WorkflowAction {
                                                                                                vfModuleCustomizationUUID = vfModule.getModelInfo().getModelCustomizationUuid();
                                                                                        }
                                                                                        if(!vnfCustomizationUUID.equals("")&&!vfModuleCustomizationUUID.equals("")){
-                                                                                               List<String> configs = traverseCatalogDbForConfiguration(vnfCustomizationUUID,vfModuleCustomizationUUID);
-                                                                                               for(String config : configs){
-                                                                                                       Resource configResource = new Resource(WorkflowType.CONFIGURATION,config,false);
+                                                                                               List<CvnfcConfigurationCustomization> configs = traverseCatalogDbForConfiguration(validate.getModelInfo().getModelVersionId(),vnfCustomizationUUID,vfModuleCustomizationUUID);
+                                                                                               for(CvnfcConfigurationCustomization config : configs){
+                                                                                                       Resource configResource = new Resource(WorkflowType.CONFIGURATION,config.getConfigurationResource().getModelUUID(),false);
                                                                                                        resource.setVnfCustomizationId(vnf.getModelInfo().getModelCustomizationId());
                                                                                                        resource.setVfModuleCustomizationId(vfModule.getModelInfo().getModelCustomizationId());
                                                                                                        resourceCounter.add(configResource);
@@ -734,20 +828,19 @@ public class WorkflowAction {
                }
                return foundRelated;
        }
-       
 
-       protected List<String> traverseCatalogDbForConfiguration(String vnfCustomizationUUID, String vfModuleCustomizationUUID) {
-               List<String> configurations = new ArrayList<>();
+       protected List<CvnfcConfigurationCustomization> traverseCatalogDbForConfiguration(String serviceModelUUID, String vnfCustomizationUUID, String vfModuleCustomizationUUID) {
+               List<CvnfcConfigurationCustomization> configurations = new ArrayList<>();
                try{
-                       List<CvnfcCustomization> cvnfcCustomizations = catalogDbClient.getCvnfcCustomizationByVnfCustomizationUUIDAndVfModuleCustomizationUUID(vnfCustomizationUUID, vfModuleCustomizationUUID);
+                       List<CvnfcCustomization> cvnfcCustomizations = catalogDbClient.getCvnfcCustomization(serviceModelUUID,vnfCustomizationUUID, vfModuleCustomizationUUID);
                        for(CvnfcCustomization cvnfc : cvnfcCustomizations){
-                               for(VnfVfmoduleCvnfcConfigurationCustomization customization : cvnfc.getVnfVfmoduleCvnfcConfigurationCustomization()){
+                               for(CvnfcConfigurationCustomization customization : cvnfc.getCvnfcConfigurationCustomization()){
                                        if(customization.getConfigurationResource().getToscaNodeType().contains(FABRIC_CONFIGURATION)){
-                                               configurations.add(customization.getConfigurationResource().getModelUUID());
+                                               configurations.add(customization);
                                        }
                                }
                        }
-                       logger.debug("found {} configurations" , configurations.size() );
+                       logger.debug("found {} fabric configuration(s)" , configurations.size() );
                        return configurations;
                } catch (Exception ex){
                        logger.error("Error in finding configurations", ex);
@@ -774,6 +867,7 @@ public class WorkflowAction {
                workflowResourceIds.setVfModuleId((String) execution.getVariable("vfModuleId"));
                workflowResourceIds.setVnfId((String) execution.getVariable("vnfId"));
                workflowResourceIds.setVolumeGroupId((String) execution.getVariable("volumeGroupId"));
+               workflowResourceIds.setInstanceGroupId((String) execution.getVariable("instanceGroupId"));
                return workflowResourceIds;
        }
 
@@ -1048,11 +1142,13 @@ public class WorkflowAction {
                executeBuildingBlock.setRequestId(requestId);
                executeBuildingBlock.setBuildingBlock(buildingBlock);
                executeBuildingBlock.setRequestDetails(requestDetails);
-               if(isConfiguration){
+               if(resource != null && (isConfiguration || resource.getResourceType().equals(WorkflowType.CONFIGURATION))){
                        ConfigurationResourceKeys configurationResourceKeys = new ConfigurationResourceKeys();
-                       configurationResourceKeys.setCvnfcCustomizationUUID(resource.getCvnfModuleCustomizationId());
-                       configurationResourceKeys.setVfModuleCustomizationUUID(resource.getVfModuleCustomizationId());
-                       configurationResourceKeys.setVnfResourceCustomizationUUID(resource.getVnfCustomizationId());
+                       if (resource != null){
+                               configurationResourceKeys.setCvnfcCustomizationUUID(resource.getCvnfModuleCustomizationId());
+                               configurationResourceKeys.setVfModuleCustomizationUUID(resource.getVfModuleCustomizationId());
+                               configurationResourceKeys.setVnfResourceCustomizationUUID(resource.getVnfCustomizationId());
+                       }
                        executeBuildingBlock.setConfigurationResourceKeys(configurationResourceKeys);
                }
                return executeBuildingBlock;
@@ -1102,29 +1198,6 @@ public class WorkflowAction {
                }
                return listToExecute;
        }
-       
-       protected List<OrchestrationFlow> filterOrchFlows(ServiceInstancesRequest sIRequest, List<OrchestrationFlow> orchFlows, WorkflowType resourceType, DelegateExecution execution) {
-               List<OrchestrationFlow> result = new ArrayList<>(orchFlows);
-               String vnfCustomizationUUID = "";
-               String vfModuleCustomizationUUID = sIRequest.getRequestDetails().getModelInfo().getModelCustomizationUuid();
-               RelatedInstanceList[] relatedInstanceList = sIRequest.getRequestDetails().getRelatedInstanceList();
-               if (relatedInstanceList != null) {
-                       for (RelatedInstanceList relatedInstList : relatedInstanceList) {
-                               RelatedInstance relatedInstance = relatedInstList.getRelatedInstance();
-                               if (relatedInstance.getModelInfo().getModelType().equals(ModelType.vnf)) {
-                                       vnfCustomizationUUID = relatedInstance.getModelInfo().getModelCustomizationUuid();
-                               }
-                       }
-               }
-               
-               if (resourceType.equals(WorkflowType.VFMODULE)) {
-                       List<String> fabricCustomizations = traverseCatalogDbForConfiguration(vnfCustomizationUUID, vfModuleCustomizationUUID);
-                       if (fabricCustomizations.isEmpty()) {
-                               result = orchFlows.stream().filter(item -> !item.getFlowName().contains(FABRIC_CONFIGURATION)).collect(Collectors.toList());
-                       }
-               }
-               return result;
-       }
 
        protected void buildAndThrowException(DelegateExecution execution, String msg, Exception ex) {
                logger.error(msg, ex);