Support to change substitution mapping node or version after service creation
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / impl / ServiceBusinessLogic.java
index 9b095cc..a9014b4 100644 (file)
@@ -19,6 +19,7 @@
  * Modifications copyright (c) 2019 Nokia
  * ================================================================================
  */
+
 package org.openecomp.sdc.be.components.impl;
 
 import static org.apache.commons.collections.CollectionUtils.isNotEmpty;
@@ -97,6 +98,10 @@ import org.openecomp.sdc.be.datatypes.elements.InterfaceDataDefinition;
 import org.openecomp.sdc.be.datatypes.elements.ListDataDefinition;
 import org.openecomp.sdc.be.datatypes.elements.OperationInputDefinition;
 import org.openecomp.sdc.be.datatypes.elements.OperationOutputDefinition;
+import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
+import org.openecomp.sdc.be.datatypes.elements.ToscaFunction;
+import org.openecomp.sdc.be.datatypes.elements.ToscaFunctionType;
+import org.openecomp.sdc.be.datatypes.elements.ToscaGetFunctionDataDefinition;
 import org.openecomp.sdc.be.datatypes.enums.ComponentFieldsEnum;
 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
 import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
@@ -189,7 +194,6 @@ public class ServiceBusinessLogic extends ComponentBusinessLogic {
     private final ServiceInstantiationTypeValidator serviceInstantiationTypeValidator;
     private final ServiceCategoryValidator serviceCategoryValidator;
     private final ServiceValidator serviceValidator;
-    private final PolicyBusinessLogic policyBusinessLogic;
     private final GroupBusinessLogic groupBusinessLogic;
     private ForwardingPathOperation forwardingPathOperation;
     private AuditCassandraDao auditCassandraDao;
@@ -209,8 +213,7 @@ public class ServiceBusinessLogic extends ComponentBusinessLogic {
                                 ComponentDescriptionValidator componentDescriptionValidator, ModelOperation modelOperation,
                                 final ServiceRoleValidator serviceRoleValidator,
                                 final ServiceInstantiationTypeValidator serviceInstantiationTypeValidator,
-                                final ServiceCategoryValidator serviceCategoryValidator, final ServiceValidator serviceValidator,
-                                final PolicyBusinessLogic policyBusinessLogic) {
+                                final ServiceCategoryValidator serviceCategoryValidator, final ServiceValidator serviceValidator) {
         super(elementDao, groupOperation, groupInstanceOperation, groupTypeOperation, groupBusinessLogic, interfaceOperation,
             interfaceLifecycleTypeOperation, artifactsBusinessLogic, artifactToscaOperation, componentContactIdValidator, componentNameValidator,
             componentTagsValidator, componentValidator, componentIconValidator, componentProjectCodeValidator, componentDescriptionValidator);
@@ -224,7 +227,6 @@ public class ServiceBusinessLogic extends ComponentBusinessLogic {
         this.serviceInstantiationTypeValidator = serviceInstantiationTypeValidator;
         this.serviceCategoryValidator = serviceCategoryValidator;
         this.serviceValidator = serviceValidator;
-        this.policyBusinessLogic = policyBusinessLogic;
         this.groupBusinessLogic = groupBusinessLogic;
     }
 
@@ -849,21 +851,42 @@ public class ServiceBusinessLogic extends ComponentBusinessLogic {
             log.info("Restricted operation for user: {}, on service: {}", user.getUserId(), currentService.getCreatorUserId());
             return Either.right(componentsUtils.getResponseFormat(ActionStatus.RESTRICTED_OPERATION));
         }
-        Either<Service, ResponseFormat> validationRsponse = validateAndUpdateServiceMetadata(user, currentService, serviceUpdate);
-        if (validationRsponse.isRight()) {
+        List<String> subNodePropsToBeRemoved = getSubstitutionNodePropertiesToBeRemoved(currentService, serviceUpdate);
+        List<PropertyDefinition> subNodePropsToBeAdded = getSubstitutionNodePropertiesToBeAdded(currentService, serviceUpdate);
+        boolean subNodeChanged = isSubstitutionNodeChanged(currentService, serviceUpdate);
+        Either<Service, ResponseFormat> validationResponse =
+            validateAndUpdateServiceMetadata(user, currentService, serviceUpdate, subNodeChanged, ListUtils.emptyIfNull(subNodePropsToBeRemoved));
+        if (validationResponse.isRight()) {
             log.info("service update metadata: validations field.");
-            return validationRsponse;
+            return validationResponse;
         }
-        Service serviceToUpdate = validationRsponse.left().value();
+        Service serviceToUpdate = validationResponse.left().value();
         // lock resource
         lockComponent(serviceId, currentService, "Update Service Metadata");
         try {
+            if (subNodeChanged) {
+                if (!subNodePropsToBeRemoved.isEmpty()) {
+                    removePropertiesFromService(currentService, subNodePropsToBeRemoved);
+                    removeInputsFromService(currentService, subNodePropsToBeRemoved);
+                }
+                if (!subNodePropsToBeAdded.isEmpty()) {
+                    addPropertiesToService(currentService, subNodePropsToBeAdded);
+                    if (Constants.DEFAULT_MODEL_NAME.equals(currentService.getModel()) || currentService.getModel() == null) {
+                        addInputsToService(currentService, subNodePropsToBeAdded);
+                    }
+                }
+            }
             return toscaOperationFacade.updateToscaElement(serviceToUpdate).right().map(rf -> {
                 janusGraphDao.rollback();
                 BeEcompErrorManager.getInstance().logBeSystemError("Update Service Metadata");
                 log.debug("failed to update sevice {}", serviceToUpdate.getUniqueId());
                 return (componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR));
             }).left().bind(this::updateCatalogAndCommit);
+        } catch (ComponentException e) {
+            janusGraphDao.rollback();
+            BeEcompErrorManager.getInstance().logBeSystemError("Update Service Metadata");
+            log.debug("failed to update sevice {}", serviceToUpdate.getUniqueId());
+            return Either.right(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR));
         } finally {
             graphLockOperation.unlockComponent(serviceId, NodeTypeEnum.Service);
         }
@@ -1034,9 +1057,18 @@ public class ServiceBusinessLogic extends ComponentBusinessLogic {
     }
 
     @VisibleForTesting
-    Either<Service, ResponseFormat> validateAndUpdateServiceMetadata(User user, Service currentService, Service serviceUpdate) {
+    Either<Service, ResponseFormat> validateAndUpdateServiceMetadata(User user, Service currentService, Service serviceUpdate, boolean subNodeChanged,
+                                                                     List<String> subNodePropsToBeRemoved) {
         try {
             boolean hasBeenCertified = ValidationUtils.hasBeenCertified(currentService.getVersion());
+            if (subNodeChanged) {
+                if (!subNodePropsToBeRemoved.isEmpty()) {
+                    areSubstitutionNodePropertiesInUse(currentService, subNodePropsToBeRemoved);
+                }
+                currentService.setDerivedFromGenericVersion(serviceUpdate.getDerivedFromGenericVersion());
+                currentService.setDerivedFromGenericType(serviceUpdate.getDerivedFromGenericType());
+            }
+
             Either<Boolean, ResponseFormat> response = validateAndUpdateCategory(user, currentService, serviceUpdate, hasBeenCertified,
                 UPDATE_SERVICE_METADATA);
             if (response.isRight()) {
@@ -1103,6 +1135,131 @@ public class ServiceBusinessLogic extends ComponentBusinessLogic {
         }
     }
 
+    private void addPropertiesToService(Service currentService, List<PropertyDefinition> subNodePropsToBeAdded) {
+        ListUtils.emptyIfNull(subNodePropsToBeAdded).forEach(prop -> {
+            Either<PropertyDefinition, StorageOperationStatus> addPropertyEither =
+                toscaOperationFacade.addPropertyToComponent(prop, currentService);
+            if (addPropertyEither.isRight()) {
+                throw new ByActionStatusComponentException(ActionStatus.GENERAL_ERROR);
+            }
+        });
+    }
+
+    private void addInputsToService(Service currentService, List<PropertyDefinition> subNodePropsToBeAdded) {
+            ListUtils.emptyIfNull(subNodePropsToBeAdded).forEach(prop -> {
+                InputDefinition inputDef = new InputDefinition(prop);
+                Either<InputDefinition, StorageOperationStatus> status =
+                        toscaOperationFacade.addInputToComponent(prop.getName(), inputDef, currentService);
+                if (status.isRight()) {
+                    throw new ByActionStatusComponentException(ActionStatus.GENERAL_ERROR);
+                }
+            });
+    }
+
+    private void removePropertiesFromService(Service currentService, List<String> subNodePropsToBeRemoved) {
+        List<PropertyDefinition> props = currentService.getProperties();
+        List<String> propsUniqueIdsToBeRemoved =
+            props.stream().filter(prop -> subNodePropsToBeRemoved.contains(prop.getName())).map(PropertyDefinition::getUniqueId)
+                .collect(Collectors.toList());
+        ListUtils.emptyIfNull(props).stream().filter(prop -> propsUniqueIdsToBeRemoved.contains(prop.getUniqueId())).forEach(prop -> {
+            StorageOperationStatus status = toscaOperationFacade.deletePropertyOfComponent(currentService, prop.getName());
+            if (status != StorageOperationStatus.OK) {
+                throw new ByActionStatusComponentException(ActionStatus.GENERAL_ERROR);
+            }
+        });
+    }
+
+    private void removeInputsFromService(Service currentService, List<String> subNodePropsToBeRemoved) {
+        List<PropertyDefinition> props = currentService.getProperties();
+        List<InputDefinition> inputs = currentService.getInputs();
+        List<String> propsUniqueIdsToBeRemoved =
+            props.stream().filter(prop -> subNodePropsToBeRemoved.contains(prop.getName())).map(PropertyDefinition::getUniqueId)
+                .collect(Collectors.toList());
+        ListUtils.emptyIfNull(inputs).stream().filter(input -> input.isMappedToComponentProperty() &&
+            (propsUniqueIdsToBeRemoved.contains(input.getPropertyId()) || subNodePropsToBeRemoved.contains(input.getName()))).forEach(input -> {
+            StorageOperationStatus status = toscaOperationFacade.deleteInputOfResource(currentService, input.getName());
+            if (status != StorageOperationStatus.OK) {
+                throw new ByActionStatusComponentException(ActionStatus.GENERAL_ERROR);
+            }
+        });
+    }
+
+    private void areSubstitutionNodePropertiesInUse(Service service, List<String> subNodePropsToBeRemoved) {
+        Map<String, List<ComponentInstanceProperty>> componentInstancesProps = service.getComponentInstancesProperties();
+        List<String> propsUniqueIdsToBeRemoved =
+            ListUtils.emptyIfNull(service.getProperties()).stream().filter(prop -> subNodePropsToBeRemoved.contains(prop.getName()))
+                .map(PropertyDefinition::getUniqueId)
+                .collect(Collectors.toList());
+        List<String> inputsUniqueIdsToBeRemoved = ListUtils.emptyIfNull(service.getInputs()).stream()
+            .filter(input -> propsUniqueIdsToBeRemoved.contains(input.getPropertyId()) || subNodePropsToBeRemoved.contains(input.getName()))
+            .map(PropertyDefinition::getUniqueId)
+            .collect(Collectors.toList());
+        Map<String, List<String>> inUse = new HashMap<>();
+        if (componentInstancesProps != null && !componentInstancesProps.isEmpty()) {
+            componentInstancesProps.forEach((compInstanceId, listOfProps) -> {
+                List<String> propsInUse = new ArrayList<>();
+                listOfProps.stream()
+                    .filter(PropertyDataDefinition::isToscaFunction)
+                    .filter(compProp -> ToscaFunctionType.isGetFunction(compProp.getToscaFunction().getType()))
+                    .forEach(compProp -> {
+                        ToscaFunction toscaFunction = compProp.getToscaFunction();
+                        ToscaGetFunctionDataDefinition toscaGetFunction = (ToscaGetFunctionDataDefinition) toscaFunction;
+                        String propName = toscaGetFunction.getPropertyName();
+                        String propUniqueId = toscaGetFunction.getPropertyUniqueId();
+                        if (inputsUniqueIdsToBeRemoved.contains(propUniqueId) || propsUniqueIdsToBeRemoved.contains(propUniqueId) ||
+                            subNodePropsToBeRemoved.contains(propName)) {
+                            propsInUse.add(compProp.getName());
+                        }
+                    });
+                if (!propsInUse.isEmpty()) {
+                    Optional<ComponentInstance> componentInstance = service.getComponentInstanceById(compInstanceId);
+                    componentInstance.ifPresent(instance -> inUse.put(instance.getName(), propsInUse));
+                }
+
+            });
+        }
+        if (!inUse.isEmpty()) {
+            String propsInUse = inUse.entrySet().stream().map(entry -> {
+                String properties = entry.getValue().stream().map(Object::toString).collect(Collectors.joining(", "));
+                return properties + " on " + entry.getKey();
+            }).collect(Collectors.joining(", properties "));
+            throw new ByActionStatusComponentException(ActionStatus.SUBSTITUTION_NODE_TYPE_PROPERTY_IN_USE, propsInUse);
+        }
+    }
+
+
+    private boolean isSubstitutionNodeChanged(Service currentService, Service updatedService) {
+        String currentServiceType = currentService.getDerivedFromGenericType();
+        String updatedServiceType = updatedService.getDerivedFromGenericType();
+        String currentServiceVersion = currentService.getDerivedFromGenericVersion();
+        String updatedServiceVersion = updatedService.getDerivedFromGenericVersion();
+        return !(StringUtils.equals(currentServiceType, updatedServiceType) && StringUtils.equals(currentServiceVersion, updatedServiceVersion));
+    }
+
+    private List<String> getSubstitutionNodePropertiesToBeRemoved(Service currentService, Service serviceUpdate) {
+        List<PropertyDefinition> currentProps = ListUtils.emptyIfNull(fetchDerivedFromGenericType(currentService, null).getProperties());
+        List<PropertyDefinition> updatedProps = ListUtils.emptyIfNull(fetchDerivedFromGenericType(serviceUpdate, null).getProperties());
+        if (!StringUtils.equals(currentService.getDerivedFromGenericType(), serviceUpdate.getDerivedFromGenericType())) {
+            return currentProps.stream().map(PropertyDefinition::getName).collect(Collectors.toList());
+        }
+        List<String> updatedPropNames = updatedProps.stream().map(PropertyDefinition::getName).collect(Collectors.toList());
+        List<String> propNamesToBeRemoved = currentProps.stream().map(PropertyDefinition::getName).collect(Collectors.toList());
+        propNamesToBeRemoved.removeIf(updatedPropNames::contains);
+        return propNamesToBeRemoved;
+    }
+
+    private List<PropertyDefinition> getSubstitutionNodePropertiesToBeAdded(Service currentService, Service serviceUpdate) {
+        List<PropertyDefinition> currentProps = ListUtils.emptyIfNull(fetchDerivedFromGenericType(currentService, null).getProperties());
+        List<PropertyDefinition> updatedProps = ListUtils.emptyIfNull(fetchDerivedFromGenericType(serviceUpdate, null).getProperties());
+        if (!StringUtils.equals(currentService.getDerivedFromGenericType(), serviceUpdate.getDerivedFromGenericType())) {
+            return updatedProps;
+        }
+        Set<String> currentPropNames = currentProps.stream().map(PropertyDefinition::getName).collect(Collectors.toSet());
+        updatedProps.removeIf(prop -> currentPropNames.contains(prop.getName()));
+        return updatedProps;
+    }
+
+
     private void verifyValuesAreIdentical(Object updatedValue, Object originalValue, String fieldName) {
         if (updatedValue != null && !updatedValue.equals(originalValue)) {
             log.info("update service: received request to update {} to {} the field is not updatable ignoring.", fieldName, updatedValue);