Improve handling 'empty'/null string in Service fields
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / impl / ResourceBusinessLogic.java
index 1f107e4..84e68e6 100644 (file)
@@ -58,6 +58,7 @@ import org.openecomp.sdc.be.catalog.enums.ChangeTypeEnum;
 import org.openecomp.sdc.be.components.csar.CsarArtifactsAndGroupsBusinessLogic;
 import org.openecomp.sdc.be.components.csar.CsarBusinessLogic;
 import org.openecomp.sdc.be.components.csar.CsarInfo;
+import org.openecomp.sdc.be.components.csar.OnboardedCsarInfo;
 import org.openecomp.sdc.be.components.impl.ArtifactsBusinessLogic.ArtifactOperationEnum;
 import org.openecomp.sdc.be.components.impl.ImportUtils.ResultStatusEnum;
 import org.openecomp.sdc.be.components.impl.artifact.ArtifactOperationInfo;
@@ -72,7 +73,6 @@ import org.openecomp.sdc.be.components.lifecycle.LifecycleChangeInfoWithAction.L
 import org.openecomp.sdc.be.components.merge.TopologyComparator;
 import org.openecomp.sdc.be.components.merge.property.PropertyDataValueMergeBusinessLogic;
 import org.openecomp.sdc.be.components.merge.resource.ResourceDataMergeBusinessLogic;
-import org.openecomp.sdc.be.components.merge.utils.MergeInstanceUtils;
 import org.openecomp.sdc.be.components.property.PropertyConstraintsUtils;
 import org.openecomp.sdc.be.components.validation.component.ComponentContactIdValidator;
 import org.openecomp.sdc.be.components.validation.component.ComponentDescriptionValidator;
@@ -213,7 +213,6 @@ public class ResourceBusinessLogic extends ComponentBusinessLogic {
     private final CompositionBusinessLogic compositionBusinessLogic;
     private final ResourceDataMergeBusinessLogic resourceDataMergeBusinessLogic;
     private final CsarArtifactsAndGroupsBusinessLogic csarArtifactsAndGroupsBusinessLogic;
-    private final MergeInstanceUtils mergeInstanceUtils;
     private final UiComponentDataConverter uiComponentDataConverter;
     private final CsarBusinessLogic csarBusinessLogic;
     private final PropertyBusinessLogic propertyBusinessLogic;
@@ -247,16 +246,15 @@ public class ResourceBusinessLogic extends ComponentBusinessLogic {
                                  final OutputsBusinessLogic outputsBusinessLogic, final CompositionBusinessLogic compositionBusinessLogic,
                                  final ResourceDataMergeBusinessLogic resourceDataMergeBusinessLogic,
                                  final CsarArtifactsAndGroupsBusinessLogic csarArtifactsAndGroupsBusinessLogic,
-                                 final MergeInstanceUtils mergeInstanceUtils, final UiComponentDataConverter uiComponentDataConverter,
-                                 final CsarBusinessLogic csarBusinessLogic, final ArtifactsOperations artifactToscaOperation,
-                                 final PropertyBusinessLogic propertyBusinessLogic, final ComponentContactIdValidator componentContactIdValidator,
-                                 final ComponentNameValidator componentNameValidator, final ComponentTagsValidator componentTagsValidator,
-                                 final ComponentValidator componentValidator, final ComponentIconValidator componentIconValidator,
+                                 final UiComponentDataConverter uiComponentDataConverter, final CsarBusinessLogic csarBusinessLogic,
+                                 final ArtifactsOperations artifactToscaOperation, final PropertyBusinessLogic propertyBusinessLogic,
+                                 final ComponentContactIdValidator componentContactIdValidator, final ComponentNameValidator componentNameValidator,
+                                 final ComponentTagsValidator componentTagsValidator, final ComponentValidator componentValidator,
+                                 final ComponentIconValidator componentIconValidator,
                                  final ComponentProjectCodeValidator componentProjectCodeValidator,
                                  final ComponentDescriptionValidator componentDescriptionValidator, final PolicyBusinessLogic policyBusinessLogic,
-                                 final ModelBusinessLogic modelBusinessLogic,
-                                 final DataTypeBusinessLogic dataTypeBusinessLogic, final PolicyTypeBusinessLogic policyTypeBusinessLogic,
-                                 final ModelOperation modelOperation) {
+                                 final ModelBusinessLogic modelBusinessLogic, final DataTypeBusinessLogic dataTypeBusinessLogic,
+                                 final PolicyTypeBusinessLogic policyTypeBusinessLogic, final ModelOperation modelOperation) {
         super(elementDao, groupOperation, groupInstanceOperation, groupTypeOperation, groupBusinessLogic, interfaceOperation,
             interfaceLifecycleTypeOperation, artifactsBusinessLogic, artifactToscaOperation, componentContactIdValidator, componentNameValidator,
             componentTagsValidator, componentValidator, componentIconValidator, componentProjectCodeValidator, componentDescriptionValidator);
@@ -267,7 +265,6 @@ public class ResourceBusinessLogic extends ComponentBusinessLogic {
         this.compositionBusinessLogic = compositionBusinessLogic;
         this.resourceDataMergeBusinessLogic = resourceDataMergeBusinessLogic;
         this.csarArtifactsAndGroupsBusinessLogic = csarArtifactsAndGroupsBusinessLogic;
-        this.mergeInstanceUtils = mergeInstanceUtils;
         this.uiComponentDataConverter = uiComponentDataConverter;
         this.csarBusinessLogic = csarBusinessLogic;
         this.propertyBusinessLogic = propertyBusinessLogic;
@@ -336,10 +333,6 @@ public class ResourceBusinessLogic extends ComponentBusinessLogic {
         this.artifactsBusinessLogic = artifactsManager;
     }
 
-    public ApplicationDataTypeCache getApplicationDataTypeCache() {
-        return applicationDataTypeCache;
-    }
-
     @Autowired
     @Override
     public void setApplicationDataTypeCache(ApplicationDataTypeCache applicationDataTypeCache) {
@@ -524,7 +517,7 @@ public class ResourceBusinessLogic extends ComponentBusinessLogic {
         validateLifecycleState(oldResource, user);
         String lockedResourceId = oldResource.getUniqueId();
         List<ArtifactDefinition> createdArtifacts = new ArrayList<>();
-        CsarInfo csarInfo = csarBusinessLogic.getCsarInfo(newResource, oldResource, user, csarUIPayload, csarUUID);
+        CsarInfo csarInfo = csarBusinessLogic.getCsarInfo(newResource, oldResource, user, csarUIPayload, csarUUID, null);
         lockComponent(lockedResourceId, oldResource, "update Resource From Csar");
         Map<String, NodeTypeInfo> nodeTypesInfo = csarInfo.extractTypesInfo();
         Either<Map<String, EnumMap<ArtifactOperationEnum, List<ArtifactDefinition>>>, ResponseFormat> findNodeTypesArtifactsToHandleRes = findNodeTypesArtifactsToHandle(
@@ -1041,7 +1034,8 @@ public class ResourceBusinessLogic extends ComponentBusinessLogic {
         loggerSupportability
             .log(LoggerSupportabilityActions.CREATE_RESOURCE_FROM_YAML, StatusCode.STARTED, "Starting to create Resource From Csar by user {}",
                 user.getUserId());
-        CsarInfo csarInfo = csarBusinessLogic.getCsarInfo(resource, null, user, csarUIPayload, csarUUID);
+        OnboardedCsarInfo csarInfo = csarBusinessLogic.getCsarInfo(resource, null, user, csarUIPayload, csarUUID, null);
+
         Map<String, NodeTypeInfo> nodeTypesInfo = csarInfo.extractTypesInfo();
         final String model = resource.getModel();
         if (StringUtils.isNotEmpty(model)) {
@@ -1057,7 +1051,6 @@ public class ResourceBusinessLogic extends ComponentBusinessLogic {
                 policyTypeBusinessLogic.createPolicyTypeFromYaml(new Yaml().dump(policyTypesToCreate), model, true);
             }
         }
-
         Either<Map<String, EnumMap<ArtifactOperationEnum, List<ArtifactDefinition>>>, ResponseFormat> findNodeTypesArtifactsToHandleRes = findNodeTypesArtifactsToHandle(
             nodeTypesInfo, csarInfo, resource);
         if (findNodeTypesArtifactsToHandleRes.isRight()) {
@@ -1353,7 +1346,7 @@ public class ResourceBusinessLogic extends ComponentBusinessLogic {
         Function<Resource, Boolean> validator = resource -> validateResourceCreationFromNodeType(resource, creator);
         return resourceImportManager
             .importCertifiedResource(nodeTypeYaml, resourceMetaData, creator, validator, lifecycleChangeInfo, isInTransaction, true, needLock,
-                nodeTypeArtifactsToHandle, nodeTypesNewCreatedArtifacts, forceCertificationAllowed, csarInfo, nodeName, isNested);
+                nodeTypeArtifactsToHandle, nodeTypesNewCreatedArtifacts, forceCertificationAllowed, csarInfo, nodeName, isNested, null);
     }
 
     /**
@@ -1414,6 +1407,7 @@ public class ResourceBusinessLogic extends ComponentBusinessLogic {
         resourceMetaData.setIcon(ImportUtils.Constants.DEFAULT_ICON);
         resourceMetaData.setContactId(user.getUserId());
         resourceMetaData.setVendorName(resourceVf.getVendorName());
+        resourceMetaData.setTenant(resourceVf.getTenant());
         resourceMetaData.setVendorRelease(resourceVf.getVendorRelease());
         resourceMetaData.setModel(resourceVf.getModel());
         // Setting tag
@@ -1447,6 +1441,7 @@ public class ResourceBusinessLogic extends ComponentBusinessLogic {
         cvfc.setContactId(csarInfo.getModifier().getUserId());
         cvfc.setCreatorUserId(csarInfo.getModifier().getUserId());
         cvfc.setVendorName(resourceVf.getVendorName());
+        cvfc.setTenant(resourceVf.getTenant());
         cvfc.setVendorRelease(resourceVf.getVendorRelease());
         cvfc.setModel(resourceVf.getModel());
         cvfc.setResourceVendorModelNumber(resourceVf.getResourceVendorModelNumber());
@@ -1512,13 +1507,15 @@ public class ResourceBusinessLogic extends ComponentBusinessLogic {
                 createResourcePropertiesOnGraph(resource);
                 final Map<String, UploadComponentInstanceInfo> instancesToCreate = getInstancesToCreate(parsedToscaYamlInfo, resource.getModel());
 
-                log.trace("************* Going to create nodes, RI's and Relations  from yaml {}", yamlName);
-                loggerSupportability
-                    .log(LoggerSupportabilityActions.CREATE_RESOURCE_FROM_YAML, resource.getComponentMetadataForSupportLog(), StatusCode.STARTED,
-                        "Start create nodes, RI and Relations  from yaml: {}", yamlName);
-                resource = createRIAndRelationsFromYaml(yamlName, resource, instancesToCreate, topologyTemplateYaml,
-                    nodeTypesNewCreatedArtifacts, nodeTypesInfo, csarInfo, nodeTypesArtifactsToCreate, nodeName,
-                    parsedToscaYamlInfo.getSubstitutionMappingNodeType());
+                if (MapUtils.isNotEmpty(instancesToCreate)) {
+                    log.trace("************* Going to create nodes, RI's and Relations  from yaml {}", yamlName);
+                    loggerSupportability
+                        .log(LoggerSupportabilityActions.CREATE_RESOURCE_FROM_YAML, resource.getComponentMetadataForSupportLog(), StatusCode.STARTED,
+                            "Start create nodes, RI and Relations  from yaml: {}", yamlName);
+                    resource = createRIAndRelationsFromYaml(yamlName, resource, instancesToCreate, topologyTemplateYaml,
+                        nodeTypesNewCreatedArtifacts, nodeTypesInfo, csarInfo, nodeTypesArtifactsToCreate, nodeName,
+                        parsedToscaYamlInfo.getSubstitutionMappingNodeType());
+                }
             } else {
                 final Resource genericResource = fetchAndSetDerivedFromGenericType(resource, null);
                 resource = createResourceTransaction(resource, csarInfo.getModifier(), isNormative);
@@ -2084,12 +2081,12 @@ public class ResourceBusinessLogic extends ComponentBusinessLogic {
                 Constants.VENDOR_LICENSE_MODEL, ArtifactTypeEnum.VENDOR_LICENSE.getType(), ArtifactGroupTypeEnum.DEPLOYMENT,
                 Constants.VENDOR_LICENSE_LABEL, Constants.VENDOR_LICENSE_DISPLAY_NAME, Constants.VENDOR_LICENSE_DESCRIPTION, vendorLicenseModelId,
                 artifactOperation, null, true, shouldLock, inTransaction);
-            createOrUpdateSingleNonMetaArtifact(resource, csarInfo, CsarUtils.ARTIFACTS_PATH + Constants.VF_LICENSE_MODEL, Constants.VF_LICENSE_MODEL,
-                ArtifactTypeEnum.VF_LICENSE.getType(), ArtifactGroupTypeEnum.DEPLOYMENT, Constants.VF_LICENSE_LABEL,
-                Constants.VF_LICENSE_DISPLAY_NAME, Constants.VF_LICENSE_DESCRIPTION, vfLicenseModelId, artifactOperation, null, true, shouldLock,
-                inTransaction);
-            Either<Resource, ResponseFormat> eitherCreateResult = createOrUpdateNonMetaArtifacts(csarInfo, resource, createdArtifacts, shouldLock,
-                inTransaction, artifactOperation);
+            createOrUpdateSingleNonMetaArtifact(resource, csarInfo, CsarUtils.ARTIFACTS_PATH + Constants.VF_LICENSE_MODEL,
+                Constants.VF_LICENSE_MODEL, ArtifactTypeEnum.VF_LICENSE.getType(), ArtifactGroupTypeEnum.DEPLOYMENT,
+                Constants.VF_LICENSE_LABEL, Constants.VF_LICENSE_DISPLAY_NAME, Constants.VF_LICENSE_DESCRIPTION, vfLicenseModelId,
+                artifactOperation, null, true, shouldLock, inTransaction);
+            Either<Resource, ResponseFormat> eitherCreateResult
+                = createOrUpdateNonMetaArtifacts(csarInfo, resource, createdArtifacts, shouldLock, inTransaction, artifactOperation);
             if (eitherCreateResult.isRight()) {
                 return Either.right(eitherCreateResult.right().value());
             }
@@ -3474,7 +3471,7 @@ public class ResourceBusinessLogic extends ComponentBusinessLogic {
                 nodeForceCertification(resource, user, lifecycleChangeInfo, inTransaction, needLock);
             }
             if (resource.getLifecycleState() == LifecycleStateEnum.CERTIFIED) {
-                Either<ArtifactDefinition, Operation> eitherPopulated = populateToscaArtifacts(resource, user, false, inTransaction, needLock, false);
+                populateToscaArtifacts(resource, user, false, inTransaction, needLock, false);
                 return resource;
             }
             return nodeFullCertification(resource.getUniqueId(), user, lifecycleChangeInfo, inTransaction, needLock);
@@ -3646,9 +3643,7 @@ public class ResourceBusinessLogic extends ComponentBusinessLogic {
             if (MapUtils.isEmpty(newResource.getToscaArtifacts())) {
                 setToscaArtifactsPlaceHolders(newResource, user);
             }
-            if (MapUtils.isEmpty(newResource.getInterfaces())) {
-                newResource.setInterfaces(oldResource.getInterfaces());
-            }
+
             if (CollectionUtils.isEmpty(newResource.getAttributes())) {
                 newResource.setAttributes(oldResource.getAttributes());
             }
@@ -3711,6 +3706,7 @@ public class ResourceBusinessLogic extends ComponentBusinessLogic {
         if (newResource.getContactId() == null) {
             newResource.setContactId(oldResource.getContactId());
         }
+        newResource.setIcon(oldResource.getIcon());
         newResource.setCategories(oldResource.getCategories());
         if (newResource.getVendorName() == null) {
             newResource.setVendorName(oldResource.getVendorName());
@@ -3822,7 +3818,7 @@ public class ResourceBusinessLogic extends ComponentBusinessLogic {
             while (intItr.hasNext() && eitherResult.isLeft()) {
                 InterfaceDefinition interfaceDefinition = intItr.next();
                 String intType = interfaceDefinition.getUniqueId();
-                Either<InterfaceDefinition, StorageOperationStatus> eitherCapTypeFound = interfaceTypeOperation.getInterface(intType);
+                Either<InterfaceDefinition, StorageOperationStatus> eitherCapTypeFound = interfaceTypeOperation.getInterface(UniqueIdBuilder.buildInterfaceTypeUid(resource.getModel(), intType));
                 if (eitherCapTypeFound.isRight()) {
                     if (eitherCapTypeFound.right().value() == StorageOperationStatus.NOT_FOUND) {
                         BeEcompErrorManager.getInstance()
@@ -3979,11 +3975,13 @@ public class ResourceBusinessLogic extends ComponentBusinessLogic {
             componentsUtils.auditResource(componentsUtils.getResponseFormat(ActionStatus.CREATED), user, createdResource, actionEnum);
             ASDCKpiApi.countCreatedResourcesKPI();
         } catch (ComponentException e) {
+            janusGraphDao.rollback();
             ResponseFormat responseFormat =
                 e.getResponseFormat() == null ? componentsUtils.getResponseFormat(e.getActionStatus(), e.getParams()) : e.getResponseFormat();
             componentsUtils.auditResource(responseFormat, user, resource, actionEnum);
             throw e;
         } catch (StorageException e) {
+            janusGraphDao.rollback();
             ResponseFormat responseFormat = componentsUtils
                 .getResponseFormat(componentsUtils.convertFromStorageResponse(e.getStorageOperationStatus()));
             componentsUtils.auditResource(responseFormat, user, resource, actionEnum);
@@ -4330,7 +4328,7 @@ public class ResourceBusinessLogic extends ComponentBusinessLogic {
             } else {
                 newResource.setDerivedFrom(null);
             }
-            Either<Resource, ResponseFormat> dataModelResponse = updateResourceMetadata(resourceIdToUpdate, newResource, user, currentResource, false,
+            Either<Resource, ResponseFormat> dataModelResponse = updateResourceMetadata(resourceIdToUpdate, newResource, user, currentResource,
                 true);
             if (dataModelResponse.isRight()) {
                 log.debug("failed to update resource metadata!!!");
@@ -4352,7 +4350,7 @@ public class ResourceBusinessLogic extends ComponentBusinessLogic {
     }
 
     private Either<Resource, ResponseFormat> updateResourceMetadata(String resourceIdToUpdate, Resource newResource, User user,
-                                                                    Resource currentResource, boolean shouldLock, boolean inTransaction) {
+                                                                    Resource currentResource, boolean inTransaction) {
         updateVfModuleGroupsNames(currentResource, newResource);
         validateResourceFieldsBeforeUpdate(currentResource, newResource, inTransaction, false);
         // Setting last updater and uniqueId
@@ -4787,14 +4785,14 @@ public class ResourceBusinessLogic extends ComponentBusinessLogic {
             throw new ByActionStatusComponentException(ActionStatus.RESOURCE_TOO_MUCH_SUBCATEGORIES);
         }
         SubCategoryDefinition subcategory = subcategories.get(0);
-        if (!ValidationUtils.validateStringNotEmpty(category.getName())) {
+        if (StringUtils.isEmpty(category.getName())) {
             log.debug(CATEGORY_IS_EMPTY);
             ResponseFormat responseFormat = componentsUtils
                 .getResponseFormat(ActionStatus.COMPONENT_MISSING_CATEGORY, ComponentTypeEnum.RESOURCE.getValue());
             componentsUtils.auditResource(responseFormat, user, resource, actionEnum);
             throw new ByActionStatusComponentException(ActionStatus.COMPONENT_MISSING_CATEGORY, ComponentTypeEnum.RESOURCE.getValue());
         }
-        if (!ValidationUtils.validateStringNotEmpty(subcategory.getName())) {
+        if (StringUtils.isEmpty(subcategory.getName())) {
             log.debug(CATEGORY_IS_EMPTY);
             ResponseFormat responseFormat = componentsUtils
                 .getResponseFormat(ActionStatus.COMPONENT_MISSING_SUBCATEGORY, ComponentTypeEnum.RESOURCE.getValue());
@@ -4843,7 +4841,7 @@ public class ResourceBusinessLogic extends ComponentBusinessLogic {
     public void validateVendorReleaseName(User user, Resource resource, AuditingActionEnum actionEnum) {
         String vendorRelease = resource.getVendorRelease();
         log.debug("validate vendor relese name");
-        if (!ValidationUtils.validateStringNotEmpty(vendorRelease)) {
+        if (StringUtils.isEmpty(vendorRelease)) {
             log.info("vendor relese name is missing.");
             ResponseFormat errorResponse = componentsUtils.getResponseFormat(ActionStatus.MISSING_VENDOR_RELEASE);
             componentsUtils.auditResource(errorResponse, user, resource, actionEnum);
@@ -4872,7 +4870,7 @@ public class ResourceBusinessLogic extends ComponentBusinessLogic {
 
     private void validateVendorName(User user, Resource resource, AuditingActionEnum actionEnum) {
         String vendorName = resource.getVendorName();
-        if (!ValidationUtils.validateStringNotEmpty(vendorName)) {
+        if (StringUtils.isEmpty(vendorName)) {
             log.info("vendor name is missing.");
             ResponseFormat errorResponse = componentsUtils.getResponseFormat(ActionStatus.MISSING_VENDOR_NAME);
             componentsUtils.auditResource(errorResponse, user, resource, actionEnum);
@@ -5030,44 +5028,41 @@ public class ResourceBusinessLogic extends ComponentBusinessLogic {
     }
 
     public void iterateOverProperties(List<PropertyDefinition> properties, String model) {
-        String type = null;
-        String innerType = null;
         for (PropertyDefinition property : properties) {
             if (!propertyOperation.isPropertyTypeValid(property, model)) {
                 log.info("Invalid type for property {}", property);
                 throw new ByActionStatusComponentException(ActionStatus.INVALID_PROPERTY_TYPE, property.getType(), property.getName());
             }
             Map<String, DataTypeDefinition> allDataTypes = componentsUtils.getAllDataTypes(applicationDataTypeCache, model);
-            type = property.getType();
+            String type = property.getType();
             if (type.equals(ToscaPropertyType.LIST.getType()) || type.equals(ToscaPropertyType.MAP.getType())) {
-                ResponseFormat responseFormat = validateMapOrListPropertyType(property, innerType, allDataTypes);
+                ResponseFormat responseFormat = validateMapOrListPropertyType(property, allDataTypes);
                 if (responseFormat != null) {
                     break;
                 }
             }
-            validateDefaultPropertyValue(property, allDataTypes, type, innerType);
+            validateDefaultPropertyValue(property, allDataTypes, type);
         }
     }
 
-    private void validateDefaultPropertyValue(PropertyDefinition property, Map<String, DataTypeDefinition> allDataTypes, String type,
-                                              String innerType) {
+    private void validateDefaultPropertyValue(PropertyDefinition property, Map<String, DataTypeDefinition> allDataTypes, String type) {
         if (!propertyOperation.isPropertyDefaultValueValid(property, allDataTypes)) {
             log.info("Invalid default value for property {}", property);
             ResponseFormat responseFormat;
             if (type.equals(ToscaPropertyType.LIST.getType()) || type.equals(ToscaPropertyType.MAP.getType())) {
-                throw new ByActionStatusComponentException(ActionStatus.INVALID_COMPLEX_DEFAULT_VALUE, property.getName(), type, innerType,
+                throw new ByActionStatusComponentException(ActionStatus.INVALID_COMPLEX_DEFAULT_VALUE, property.getName(), type,
                     property.getDefaultValue());
             }
             throw new ByActionStatusComponentException(ActionStatus.INVALID_DEFAULT_VALUE, property.getName(), type, property.getDefaultValue());
         }
     }
 
-    private ResponseFormat validateMapOrListPropertyType(PropertyDefinition property, String innerType,
+    private ResponseFormat validateMapOrListPropertyType(PropertyDefinition property,
                                                          Map<String, DataTypeDefinition> allDataTypes) {
         ResponseFormat responseFormat = null;
         ImmutablePair<String, Boolean> propertyInnerTypeValid = propertyOperation.isPropertyInnerTypeValid(property, allDataTypes);
-        innerType = propertyInnerTypeValid.getLeft();
-        if (!propertyInnerTypeValid.getRight()) {
+        String innerType = propertyInnerTypeValid.getLeft();
+        if (Boolean.FALSE.equals(propertyInnerTypeValid.getRight())) {
             log.info("Invalid inner type for property {}", property);
             responseFormat = componentsUtils.getResponseFormat(ActionStatus.INVALID_PROPERTY_INNER_TYPE, innerType, property.getName());
         }