Improve handling 'empty'/null string in Service fields
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / impl / ComponentBusinessLogic.java
index 7d4ba3b..0b60a36 100644 (file)
@@ -104,6 +104,7 @@ import org.springframework.beans.factory.annotation.Autowired;
 public abstract class ComponentBusinessLogic extends BaseBusinessLogic {
 
     private static final Logger log = Logger.getLogger(ComponentBusinessLogic.class.getName());
+
     protected final GroupBusinessLogic groupBusinessLogic;
     protected ArtifactsBusinessLogic artifactsBusinessLogic;
     protected GenericTypeBusinessLogic genericTypeBusinessLogic;
@@ -205,6 +206,10 @@ public abstract class ComponentBusinessLogic extends BaseBusinessLogic {
      */
     public abstract Either<UiComponentDataTransfer, ResponseFormat> getUiComponentDataTransferByComponentId(String componentId,
                                                                                                             List<String> dataParamsToReturn);
+    
+    public Either<ComponentMetadataData, StorageOperationStatus> getComponentMetadata(final String componentId) {
+        return toscaOperationFacade.getComponentMetadata(componentId);
+    }
 
     User validateUser(User user, String ecompErrorContext, Component component, AuditingActionEnum auditAction, boolean inTransaction) {
         User validatedUser;
@@ -299,7 +304,7 @@ public abstract class ComponentBusinessLogic extends BaseBusinessLogic {
         log.debug("validate Icon");
         ComponentTypeEnum type = component.getComponentType();
         String icon = component.getIcon();
-        if (!ValidationUtils.validateStringNotEmpty(icon)) {
+        if (StringUtils.isEmpty(icon)) {
             log.info("icon is missing.");
             ResponseFormat errorResponse = componentsUtils.getResponseFormat(ActionStatus.COMPONENT_MISSING_ICON, type.getValue());
             componentsUtils.auditComponentAdmin(errorResponse, user, component, actionEnum, type);
@@ -423,16 +428,18 @@ public abstract class ComponentBusinessLogic extends BaseBusinessLogic {
         }
     }
 
-    public Either<List<Component>, ResponseFormat> getLatestVersionNotAbstractComponentsMetadata(boolean isAbstractAbstract,
-                                                                                                 HighestFilterEnum highestFilter,
-                                                                                                 ComponentTypeEnum componentTypeEnum,
-                                                                                                 String internalComponentType, String userId,
-                                                                                                 String modelName) {
+    public Either<List<Component>, ResponseFormat> getLatestVersionNotAbstractComponentsMetadata(final boolean isAbstractAbstract,
+                                                                                                 final HighestFilterEnum highestFilter,
+                                                                                                 final ComponentTypeEnum componentTypeEnum,
+                                                                                                 final String internalComponentType, String userId,
+                                                                                                 final String modelName,
+                                                                                                 final boolean includeNormativeExtensionModels) {
         Either<List<Component>, StorageOperationStatus> nonCheckoutCompResponse = null;
         try {
             validateUserExists(userId);
-           nonCheckoutCompResponse = toscaOperationFacade
-                .getLatestVersionNotAbstractMetadataOnly(isAbstractAbstract, componentTypeEnum, internalComponentType, modelName);
+            nonCheckoutCompResponse = toscaOperationFacade
+                .getLatestVersionNotAbstractMetadataOnly(isAbstractAbstract, componentTypeEnum, internalComponentType, modelName,
+                    includeNormativeExtensionModels);
             if (nonCheckoutCompResponse.isLeft()) {
                 log.debug("Retrieved Resource successfully.");
                 return Either.left(nonCheckoutCompResponse.left().value());
@@ -440,7 +447,7 @@ public abstract class ComponentBusinessLogic extends BaseBusinessLogic {
             return Either
                 .right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(nonCheckoutCompResponse.right().value())));
         } finally {
-            if(nonCheckoutCompResponse != null && nonCheckoutCompResponse.isLeft() ) {
+            if (nonCheckoutCompResponse != null && nonCheckoutCompResponse.isLeft()) {
                 janusGraphDao.commit();
             }
         }
@@ -542,8 +549,12 @@ public abstract class ComponentBusinessLogic extends BaseBusinessLogic {
             throw new ByResponseFormatComponentException(
                 componentsUtils.getResponseFormat(ActionStatus.ARTIFACT_NOT_FOUND, ArtifactTypeEnum.TOSCA_CSAR.name()));
         }
-        ArtifactDefinition csarArtifact = component.getToscaArtifacts().values().stream()
-            .filter(p -> p.getArtifactType().equals(ArtifactTypeEnum.TOSCA_CSAR.getType())).findAny().get();
+
+        final ArtifactDefinition csarArtifact = component.getToscaArtifacts().values().stream()
+            .filter(p -> p.getArtifactType().equals(ArtifactTypeEnum.TOSCA_CSAR.getType())).findAny().orElseThrow(() -> {
+                throw new ByResponseFormatComponentException(
+                    componentsUtils.getResponseFormat(ActionStatus.ARTIFACT_NOT_FOUND, ArtifactTypeEnum.TOSCA_CSAR.name()));
+            });
         return artifactsBusinessLogic.handleDownloadToscaModelRequest(component, csarArtifact);
     }
 
@@ -686,7 +697,7 @@ public abstract class ComponentBusinessLogic extends BaseBusinessLogic {
         });
         return componentNonGenericInputs;
     }
-    
+
     protected void generatePropertiesFromGenericType(final Component component, final Resource genericType) {
         if (CollectionUtils.isEmpty(genericType.getProperties())) {
             return;
@@ -703,21 +714,27 @@ public abstract class ComponentBusinessLogic extends BaseBusinessLogic {
         }
         component.getProperties().forEach(propertyDefinition -> propertyDefinition.setUniqueId(null));
     }
+
     protected <T extends Component> Resource fetchAndSetDerivedFromGenericType(final T component) {
         return fetchAndSetDerivedFromGenericType(component, null);
     }
 
     protected <T extends Component> Resource fetchAndSetDerivedFromGenericType(final T component, final String toscaType) {
+        final Resource genericTypeResource = fetchDerivedFromGenericType(component, toscaType);
+        component.setDerivedFromGenericInfo(genericTypeResource);
+        return genericTypeResource;
+    }
+
+    public <T extends Component> Resource fetchDerivedFromGenericType(final T component, final String toscaType) {
         final Either<Resource, ResponseFormat> genericTypeEither = this.genericTypeBusinessLogic.fetchDerivedFromGenericType(component, toscaType);
         if (genericTypeEither.isRight()) {
-            log.debug("Failed to fetch latest generic type for component {} of type", component.getName(), component.assetType());
+            log.debug("Failed to fetch latest generic type for component {} of type {}", component.getName(), component.assetType());
             throw new ByActionStatusComponentException(ActionStatus.GENERIC_TYPE_NOT_FOUND, component.assetType());
         }
-        final Resource genericTypeResource = genericTypeEither.left().value();
-        component.setDerivedFromGenericInfo(genericTypeResource);
-        return genericTypeResource;
+        return genericTypeEither.left().value();
     }
 
+
     public Either<Map<String, List<IComponentInstanceConnectedElement>>, ResponseFormat> getFilteredComponentInstanceProperties(String componentId,
                                                                                                                                 Map<FilterKeyEnum, List<String>> filters,
                                                                                                                                 String userId) {
@@ -911,7 +928,7 @@ public abstract class ComponentBusinessLogic extends BaseBusinessLogic {
         if (validPropertiesMerge.isRight()) {
             if (log.isDebugEnabled()) {
                 log.debug("property {} cannot be overriden, check out performed without upgrading to latest generic",
-                        validPropertiesMerge.right().value());
+                    validPropertiesMerge.right().value());
             }
             return false;
         }
@@ -921,7 +938,7 @@ public abstract class ComponentBusinessLogic extends BaseBusinessLogic {
         if (validAttributesMerge.isRight()) {
             if (log.isDebugEnabled()) {
                 log.debug("attribute {} cannot be overriden, check out performed without upgrading to latest generic",
-                        validAttributesMerge.right().value());
+                    validAttributesMerge.right().value());
             }
             return false;
         }
@@ -993,7 +1010,7 @@ public abstract class ComponentBusinessLogic extends BaseBusinessLogic {
     protected Either<Component, ResponseFormat> updateCatalog(Component component, ChangeTypeEnum changeStatus) {
         if (log.isDebugEnabled()) {
             log.debug("update Catalog start with Component Type {} And Componet Name {} with change status {}",
-                    component.getComponentType().name(),component.getName(), changeStatus.name());
+                component.getComponentType().name(), component.getName(), changeStatus.name());
         }
         ActionStatus status = catalogOperations.updateCatalog(changeStatus, component);
         if (status != ActionStatus.OK) {