From 798f858d18403a31a4d464b07bb1a9a74666c9c7 Mon Sep 17 00:00:00 2001
From: aribeiro <anderson.ribeiro@est.tech>
Date: Fri, 28 Aug 2020 11:50:52 +0100
Subject: Update node_filter capabilities

Issue-ID: SDC-3265
Signed-off-by: aribeiro <anderson.ribeiro@est.tech>
Change-Id: Ib2dbd299ff5fc336258bf29a303210eacfd984cf
---
 .../impl/ComponentNodeFilterBusinessLogic.java     | 50 ++++++++++++++++++----
 .../be/servlets/ComponentNodeFilterServlet.java    |  8 +++-
 2 files changed, 49 insertions(+), 9 deletions(-)

(limited to 'catalog-be/src/main/java/org/openecomp')

diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ComponentNodeFilterBusinessLogic.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ComponentNodeFilterBusinessLogic.java
index 962407ca92..1b9054e187 100644
--- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ComponentNodeFilterBusinessLogic.java
+++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ComponentNodeFilterBusinessLogic.java
@@ -31,6 +31,7 @@ import org.openecomp.sdc.be.components.impl.exceptions.BusinessLogicException;
 import org.openecomp.sdc.be.components.impl.utils.NodeFilterConstraintAction;
 import org.openecomp.sdc.be.components.validation.NodeFilterValidator;
 import org.openecomp.sdc.be.dao.api.ActionStatus;
+import org.openecomp.sdc.be.datamodel.utils.ConstraintConvertor;
 import org.openecomp.sdc.be.datatypes.elements.CINodeFilterDataDefinition;
 import org.openecomp.sdc.be.datatypes.elements.ListDataDefinition;
 import org.openecomp.sdc.be.datatypes.elements.RequirementNodeFilterCapabilityDataDefinition;
@@ -269,7 +270,8 @@ public class ComponentNodeFilterBusinessLogic extends BaseBusinessLogic {
                                                                  final String componentInstanceId,
                                                                  final List<String> constraints,
                                                                  final boolean shouldLock,
-                                                                 final ComponentTypeEnum componentTypeEnum)
+                                                                 final ComponentTypeEnum componentTypeEnum,
+                                                                 final NodeFilterConstraintType nodeFilterConstraintType)
         throws BusinessLogicException {
 
         final Component component = getComponent(componentId);
@@ -296,10 +298,9 @@ public class ComponentNodeFilterBusinessLogic extends BaseBusinessLogic {
                 lockComponent(component.getUniqueId(), component,"Update Node Filter on Component");
                 wasLocked = true;
             }
-            final List<RequirementNodeFilterPropertyDataDefinition> properties = constraints.stream()
-                .map(this::getRequirementNodeFilterPropertyDataDefinition).collect(Collectors.toList());
-            final Either<CINodeFilterDataDefinition, StorageOperationStatus> result = nodeFilterOperation
-                .updateProperties(componentId, componentInstanceId, nodeFilterDataDefinition, properties);
+            final Either<CINodeFilterDataDefinition, StorageOperationStatus> result =
+                updateNodeFilterConstraint(componentId, componentInstanceId, constraints, nodeFilterConstraintType,
+                    nodeFilterDataDefinition);
 
             if (result.isRight()) {
                 janusGraphDao.rollback();
@@ -326,6 +327,22 @@ public class ComponentNodeFilterBusinessLogic extends BaseBusinessLogic {
         return Optional.ofNullable(nodeFilterDataDefinition);
     }
 
+    private Either<CINodeFilterDataDefinition, StorageOperationStatus> updateNodeFilterConstraint(
+        final String componentId, final String componentInstanceId, final List<String> constraints,
+        final NodeFilterConstraintType nodeFilterConstraintType, final CINodeFilterDataDefinition nodeFilterDataDefinition) {
+
+        if (NodeFilterConstraintType.PROPERTIES.equals(nodeFilterConstraintType)) {
+            final List<RequirementNodeFilterPropertyDataDefinition> properties = constraints.stream()
+                .map(this::getRequirementNodeFilterPropertyDataDefinition).collect(Collectors.toList());
+            return nodeFilterOperation.updateProperties(componentId, componentInstanceId,
+                nodeFilterDataDefinition, properties);
+        }
+        final List<RequirementNodeFilterCapabilityDataDefinition> capabilities = constraints.stream()
+            .map(this::getRequirementNodeFilterCapabilityDataDefinition).collect(Collectors.toList());
+        return nodeFilterOperation.updateCapabilities(componentId, componentInstanceId,
+            nodeFilterDataDefinition, capabilities);
+    }
+
     private Either<CINodeFilterDataDefinition, StorageOperationStatus> addNewNodeFilter(
         final String componentId,
         final String componentInstanceId,
@@ -350,6 +367,7 @@ public class ComponentNodeFilterBusinessLogic extends BaseBusinessLogic {
             requirementNodeFilterCapabilityDataDefinition);
     }
 
+
     private void unlockComponent(final String componentUniqueId,
                                  final ComponentTypeEnum componentType) {
         graphLockOperation.unlockComponent(componentUniqueId, componentType.getNodeType());
@@ -395,9 +413,25 @@ public class ComponentNodeFilterBusinessLogic extends BaseBusinessLogic {
     private RequirementNodeFilterPropertyDataDefinition getRequirementNodeFilterPropertyDataDefinition(
         final String constraint) {
 
-        final RequirementNodeFilterPropertyDataDefinition pdd = new RequirementNodeFilterPropertyDataDefinition();
-        pdd.setConstraints(Arrays.asList(constraint));
-        return pdd;
+        final RequirementNodeFilterPropertyDataDefinition requirementNodeFilterPropertyDataDefinition =
+            new RequirementNodeFilterPropertyDataDefinition();
+        requirementNodeFilterPropertyDataDefinition.setConstraints(Arrays.asList(constraint));
+        return requirementNodeFilterPropertyDataDefinition;
+    }
+
+    private RequirementNodeFilterCapabilityDataDefinition getRequirementNodeFilterCapabilityDataDefinition(
+        final String constraint) {
+
+        final RequirementNodeFilterCapabilityDataDefinition requirementNodeFilterCapabilityDataDefinition =
+            new RequirementNodeFilterCapabilityDataDefinition();
+        final ListDataDefinition<RequirementNodeFilterPropertyDataDefinition> propertyDataDefinitionList =
+            new ListDataDefinition<>();
+        propertyDataDefinitionList.getListToscaDataDefinition().addAll(
+            Collections.singleton(getRequirementNodeFilterPropertyDataDefinition(constraint)));
+        requirementNodeFilterCapabilityDataDefinition.setName(new ConstraintConvertor().convert(constraint)
+            .getServicePropertyName());
+        requirementNodeFilterCapabilityDataDefinition.setProperties(propertyDataDefinitionList);
+        return requirementNodeFilterCapabilityDataDefinition;
     }
 
     private CINodeFilterDataDefinition validateAndReturnNodeFilterDefinition(final String componentInstanceId,
diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/ComponentNodeFilterServlet.java b/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/ComponentNodeFilterServlet.java
index 5146d5e5b5..2291d8e984 100644
--- a/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/ComponentNodeFilterServlet.java
+++ b/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/ComponentNodeFilterServlet.java
@@ -207,9 +207,15 @@ public class ComponentNodeFilterServlet extends AbstractValidationsServlet {
                     "Failed to parse constraint data", constraintData));
             }
             final List<String> constraints = new ConstraintConvertor().convertToList(uiConstraints);
+            final Optional<NodeFilterConstraintType> nodeFilterConstraintType =
+                NodeFilterConstraintType.parse(constraintType);
+            if (!nodeFilterConstraintType.isPresent()) {
+                return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.INVALID_CONTENT_PARAM,
+                    "Invalid value for NodeFilterConstraintType enum %s", constraintType));
+            }
             final Optional<CINodeFilterDataDefinition> actionResponse = componentNodeFilterBusinessLogic
                 .updateNodeFilter(componentId.toLowerCase(), componentInstanceId, constraints,
-                    true, componentTypeEnum);
+                    true, componentTypeEnum, nodeFilterConstraintType.get());
 
             if (!actionResponse.isPresent()) {
                 LOGGER.error(FAILED_TO_UPDATE_NODE_FILTER);
-- 
cgit 1.2.3-korg