Fix use of Optional in ComponentBusinessLogic
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / impl / ComponentBusinessLogic.java
index 9ce54c2..6625d72 100644 (file)
@@ -111,7 +111,6 @@ public abstract class ComponentBusinessLogic extends BaseBusinessLogic {
     protected ComponentProjectCodeValidator componentProjectCodeValidator;
     protected CatalogOperation catalogOperations;
     protected ComponentIconValidator componentIconValidator;
-    protected ComponentValidator componentValidator;
     protected ComponentTagsValidator componentTagsValidator;
     protected ComponentNameValidator componentNameValidator;
     protected ComponentContactIdValidator componentContactIdValidator;
@@ -131,7 +130,6 @@ public abstract class ComponentBusinessLogic extends BaseBusinessLogic {
         this.componentContactIdValidator = componentContactIdValidator;
         this.componentNameValidator = componentNameValidator;
         this.componentTagsValidator = componentTagsValidator;
-        this.componentValidator = componentValidator;
         this.componentIconValidator = componentIconValidator;
         this.componentProjectCodeValidator = componentProjectCodeValidator;
         this.componentDescriptionValidator = componentDescriptionValidator;
@@ -425,14 +423,17 @@ public abstract class ComponentBusinessLogic extends BaseBusinessLogic {
         }
     }
 
-    public Either<List<Component>, ResponseFormat> getLatestVersionNotAbstractComponentsMetadata(boolean isAbstractAbstract,
-                                                                                                 HighestFilterEnum highestFilter,
-                                                                                                 ComponentTypeEnum componentTypeEnum,
-                                                                                                 String internalComponentType, String userId) {
+    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);
-            Either<List<Component>, StorageOperationStatus> nonCheckoutCompResponse = toscaOperationFacade
-                .getLatestVersionNotAbstractMetadataOnly(isAbstractAbstract, componentTypeEnum, internalComponentType);
+           nonCheckoutCompResponse = toscaOperationFacade
+                .getLatestVersionNotAbstractMetadataOnly(isAbstractAbstract, componentTypeEnum, internalComponentType, modelName, includeNormativeExtensionModels);
             if (nonCheckoutCompResponse.isLeft()) {
                 log.debug("Retrieved Resource successfully.");
                 return Either.left(nonCheckoutCompResponse.left().value());
@@ -440,7 +441,9 @@ public abstract class ComponentBusinessLogic extends BaseBusinessLogic {
             return Either
                 .right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(nonCheckoutCompResponse.right().value())));
         } finally {
-            janusGraphDao.commit();
+            if(nonCheckoutCompResponse != null && nonCheckoutCompResponse.isLeft() ) {
+                janusGraphDao.commit();
+            }
         }
     }
 
@@ -540,8 +543,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);
     }
 
@@ -684,14 +691,34 @@ public abstract class ComponentBusinessLogic extends BaseBusinessLogic {
         });
         return componentNonGenericInputs;
     }
+    
+    protected void generatePropertiesFromGenericType(final Component component, final Resource genericType) {
+        if (CollectionUtils.isEmpty(genericType.getProperties())) {
+            return;
+        }
+        final List<PropertyDefinition> genericTypePropertyList = genericType.getProperties().stream().map(PropertyDefinition::new)
+            .peek(propertyDefinition -> propertyDefinition.setUniqueId(null)).collect(Collectors.toList());
+        if (component.getProperties() == null) {
+            component.setProperties(new ArrayList<>(genericTypePropertyList));
+        } else {
+            List<PropertyDefinition> servicePropertyList = component.getProperties();
+            genericTypePropertyList.stream()
+                .filter(property -> servicePropertyList.stream().noneMatch(property1 -> property1.getName().equals(property.getName())))
+                .forEach(servicePropertyList::add);
+        }
+        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(T component) {
-        Either<Resource, ResponseFormat> genericTypeEither = this.genericTypeBusinessLogic.fetchDerivedFromGenericType(component);
+    protected <T extends Component> Resource fetchAndSetDerivedFromGenericType(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());
             throw new ByActionStatusComponentException(ActionStatus.GENERIC_TYPE_NOT_FOUND, component.assetType());
         }
-        Resource genericTypeResource = genericTypeEither.left().value();
+        final Resource genericTypeResource = genericTypeEither.left().value();
         component.setDerivedFromGenericInfo(genericTypeResource);
         return genericTypeResource;
     }
@@ -823,7 +850,7 @@ public abstract class ComponentBusinessLogic extends BaseBusinessLogic {
         if (ToscaPropertyType.isScalarType(propertyType)) {
             return false;
         }
-        Either<DataTypeDefinition, StorageOperationStatus> getDataTypeByNameRes = propertyOperation.getDataTypeByName(propertyType);
+        Either<DataTypeDefinition, StorageOperationStatus> getDataTypeByNameRes = propertyOperation.getDataTypeByName(propertyType, null);
         if (getDataTypeByNameRes.isRight()) {
             return false;
         }
@@ -853,7 +880,7 @@ public abstract class ComponentBusinessLogic extends BaseBusinessLogic {
     }
 
     public Either<Boolean, ResponseFormat> shouldUpgradeToLatestGeneric(Component clonedComponent) {
-        if (!clonedComponent.deriveFromGeneric()) {
+        if (!clonedComponent.deriveFromGeneric() || StringUtils.isNotEmpty(clonedComponent.getModel())) {
             return Either.left(false);
         }
         Boolean shouldUpgrade = false;
@@ -887,16 +914,20 @@ public abstract class ComponentBusinessLogic extends BaseBusinessLogic {
         Either<Map<String, PropertyDefinition>, String> validPropertiesMerge = validateNoConflictingProperties(genericTypeProps,
             ((Resource) componentToCheckOut).getProperties());
         if (validPropertiesMerge.isRight()) {
-            log.debug("property {} cannot be overriden, check out performed without upgrading to latest generic",
-                validPropertiesMerge.right().value());
+            if (log.isDebugEnabled()) {
+                log.debug("property {} cannot be overriden, check out performed without upgrading to latest generic",
+                        validPropertiesMerge.right().value());
+            }
             return false;
         }
         List<AttributeDefinition> genericTypeAttributes = latestGeneric.getAttributes();
         final Either<Map<String, AttributeDefinition>, String> validAttributesMerge = validateNoConflictingProperties(genericTypeAttributes,
             ((Resource) componentToCheckOut).getAttributes());
         if (validAttributesMerge.isRight()) {
-            log.debug("attribute {} cannot be overriden, check out performed without upgrading to latest generic",
-                validAttributesMerge.right().value());
+            if (log.isDebugEnabled()) {
+                log.debug("attribute {} cannot be overriden, check out performed without upgrading to latest generic",
+                        validAttributesMerge.right().value());
+            }
             return false;
         }
         return true;
@@ -925,7 +956,9 @@ public abstract class ComponentBusinessLogic extends BaseBusinessLogic {
         }
         Either<Map<String, InputDefinition>, String> eitherMerged = validateNoConflictingProperties(genericTypeInputs, currentList);
         if (eitherMerged.isRight()) {
-            log.debug("input {} cannot be overriden, check out performed without upgrading to latest generic", eitherMerged.right().value());
+            if (log.isDebugEnabled()) {
+                log.debug("input {} cannot be overriden, check out performed without upgrading to latest generic", eitherMerged.right().value());
+            }
             return false;
         }
         componentToCheckOut.setInputs(new ArrayList<>(eitherMerged.left().value().values()));
@@ -963,8 +996,10 @@ public abstract class ComponentBusinessLogic extends BaseBusinessLogic {
     }
 
     protected Either<Component, ResponseFormat> updateCatalog(Component component, ChangeTypeEnum changeStatus) {
-        log.debug("update Catalog start with Component Type {} And Componet Name {} with change status {}", component.getComponentType().name(),
-            component.getName(), changeStatus.name());
+        if (log.isDebugEnabled()) {
+            log.debug("update Catalog start with Component Type {} And Componet Name {} with change status {}",
+                    component.getComponentType().name(),component.getName(), changeStatus.name());
+        }
         ActionStatus status = catalogOperations.updateCatalog(changeStatus, component);
         if (status != ActionStatus.OK) {
             return Either.right(componentsUtils.getResponseFormat(status));