Fix use of Optional in ComponentBusinessLogic
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / impl / ComponentBusinessLogic.java
index d0d40e9..6625d72 100644 (file)
@@ -423,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());
@@ -438,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();
+            }
         }
     }
 
@@ -538,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);
     }
 
@@ -682,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;
     }
@@ -851,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;