Support Policies during Import Service
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / impl / ComponentBusinessLogic.java
index afb84a9..4372564 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;
@@ -423,14 +424,18 @@ 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 +443,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 +545,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);
     }
 
@@ -683,13 +694,34 @@ public abstract class ComponentBusinessLogic extends BaseBusinessLogic {
         return componentNonGenericInputs;
     }
 
-    protected <T extends Component> Resource fetchAndSetDerivedFromGenericType(T component) {
-        Either<Resource, ResponseFormat> genericTypeEither = this.genericTypeBusinessLogic.fetchDerivedFromGenericType(component);
+    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(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;
     }
@@ -821,7 +853,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;
         }
@@ -851,7 +883,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,7 +919,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;
         }
@@ -897,7 +929,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;
         }
@@ -969,7 +1001,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) {