Implement hiding mechanism
[sdc.git] / catalog-model / src / main / java / org / openecomp / sdc / be / model / jsonjanusgraph / operations / TopologyTemplateOperation.java
index 78aa4c7..603e09b 100644 (file)
@@ -31,6 +31,7 @@ import java.util.Map.Entry;
 import java.util.Optional;
 import java.util.stream.Collectors;
 import org.apache.commons.collections.MapUtils;
+import org.apache.commons.lang3.StringUtils;
 import org.apache.tinkerpop.gremlin.structure.Direction;
 import org.apache.tinkerpop.gremlin.structure.Edge;
 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
@@ -152,7 +153,7 @@ public class TopologyTemplateOperation extends ToscaElementOperation {
             return Either.right(associateInstProperties);
         }
         StorageOperationStatus associateInstInputs = associateInstInputsToComponent(topologyTemplateVertex, topologyTemplate);
-        if (associateInstProperties != StorageOperationStatus.OK) {
+        if (associateInstInputs != StorageOperationStatus.OK) {
             return Either.right(associateInstInputs);
         }
         StorageOperationStatus associateInstGroups = associateInstGroupsToComponent(topologyTemplateVertex, topologyTemplate);
@@ -184,7 +185,7 @@ public class TopologyTemplateOperation extends ToscaElementOperation {
             return Either.right(associateInterfaces);
         }
         StorageOperationStatus associatePathProperties = associateForwardingPathToResource(topologyTemplateVertex, topologyTemplate);
-        if (associateCapProperties != StorageOperationStatus.OK) {
+        if (associatePathProperties != StorageOperationStatus.OK) {
             return Either.right(associatePathProperties);
         }
         final StorageOperationStatus associateServiceToModel = associateComponentToModel(topologyTemplateVertex, topologyTemplate,
@@ -385,6 +386,23 @@ public class TopologyTemplateOperation extends ToscaElementOperation {
         return StorageOperationStatus.OK;
     }
 
+    public StorageOperationStatus associateInstInterfacesToComponent(
+        GraphVertex nodeTypeVertex,
+        Map<String, MapInterfaceDataDefinition> instInterfaces
+    ) {
+        if (instInterfaces != null && !instInterfaces.isEmpty()) {
+            Either<GraphVertex, StorageOperationStatus> associateElementToData = associateElementToData(
+                nodeTypeVertex,
+                VertexTypeEnum.INST_INTERFACES,
+                EdgeLabelEnum.INST_INTERFACES,
+                instInterfaces);
+            if (associateElementToData.isRight()) {
+                return associateElementToData.right().value();
+            }
+        }
+        return StorageOperationStatus.OK;
+    }
+
     public StorageOperationStatus associateInstGroupsToComponent(GraphVertex nodeTypeVertex, Map<String, MapGroupsDataDefinition> instGroups) {
         if (instGroups != null && !instGroups.isEmpty()) {
             Either<GraphVertex, StorageOperationStatus> assosiateElementToData = associateElementToData(nodeTypeVertex, VertexTypeEnum.INST_GROUPS,
@@ -402,8 +420,8 @@ public class TopologyTemplateOperation extends ToscaElementOperation {
                 List<String> uniqueKeys = new ArrayList<>(i.getValue().getMapToscaDataDefinition().keySet());
                 List<String> pathKeys = new ArrayList<>();
                 pathKeys.add(i.getKey());
-                StorageOperationStatus status = deleteToscaDataDeepElements(nodeTypeVertex, EdgeLabelEnum.INST_INPUTS, VertexTypeEnum.INST_INPUTS,
-                    uniqueKeys, pathKeys, JsonPresentationFields.NAME);
+                StorageOperationStatus status = deleteToscaDataDeepElements(nodeTypeVertex, EdgeLabelEnum.INST_INPUTS,
+                    uniqueKeys, pathKeys);
                 if (status != StorageOperationStatus.OK) {
                     return;
                 }
@@ -583,6 +601,21 @@ public class TopologyTemplateOperation extends ToscaElementOperation {
         return StorageOperationStatus.OK;
     }
 
+    public StorageOperationStatus associateOutputsToComponent(final GraphVertex nodeTypeVertex,
+                                                              final Map<String, ? extends AttributeDataDefinition> outputs,
+                                                              final String id) {
+        if (MapUtils.isNotEmpty(outputs)) {
+            outputs.values().stream().filter(e -> e.getUniqueId() == null)
+                .forEach(e -> e.setUniqueId(UniqueIdBuilder.buildPropertyUniqueId(id, e.getName())));
+            final Either<GraphVertex, StorageOperationStatus> associateElementToData
+                = associateElementToData(nodeTypeVertex, VertexTypeEnum.OUTPUTS, EdgeLabelEnum.OUTPUTS, outputs);
+            if (associateElementToData.isRight()) {
+                return associateElementToData.right().value();
+            }
+        }
+        return StorageOperationStatus.OK;
+    }
+
     private GraphVertex fillMetadata(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate, JsonParseFlagEnum flag) {
         nodeTypeVertex.setLabel(VertexTypeEnum.TOPOLOGY_TEMPLATE);
         fillCommonMetadata(nodeTypeVertex, topologyTemplate);
@@ -590,6 +623,8 @@ public class TopologyTemplateOperation extends ToscaElementOperation {
             nodeTypeVertex.setJson(topologyTemplate.getCompositions());
         }
         nodeTypeVertex.addMetadataProperty(GraphPropertyEnum.CSAR_UUID, topologyTemplate.getMetadataValue(JsonPresentationFields.CSAR_UUID));
+        nodeTypeVertex.addMetadataProperty(GraphPropertyEnum.CSAR_VERSION_UUID,
+            topologyTemplate.getMetadataValue(JsonPresentationFields.CSAR_VERSION_ID));
         nodeTypeVertex.addMetadataProperty(GraphPropertyEnum.DISTRIBUTION_STATUS,
             topologyTemplate.getMetadataValue(JsonPresentationFields.DISTRIBUTION_STATUS));
         return nodeTypeVertex;
@@ -607,7 +642,8 @@ public class TopologyTemplateOperation extends ToscaElementOperation {
 
     private StorageOperationStatus associateServiceMetadataToCategory(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
         String categoryName = topologyTemplate.getCategories().get(0).getName();
-        Either<GraphVertex, StorageOperationStatus> category = categoryOperation.getCategory(categoryName, VertexTypeEnum.SERVICE_CATEGORY);
+        Either<GraphVertex, StorageOperationStatus> category = categoryOperation.getCategory(categoryName,
+            VertexTypeEnum.SERVICE_CATEGORY);
         if (category.isRight()) {
             log.trace("NO category {} for service {}", categoryName, topologyTemplate.getUniqueId());
             return StorageOperationStatus.CATEGORY_NOT_FOUND;
@@ -771,12 +807,6 @@ public class TopologyTemplateOperation extends ToscaElementOperation {
                 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(storageStatus));
             }
         }
-        if (!componentParametersView.isIgnoreInterfaces()) {
-            JanusGraphOperationStatus storageStatus = setInterfcesFromGraph(componentV, toscaElement);
-            if (storageStatus != JanusGraphOperationStatus.OK) {
-                return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(storageStatus));
-            }
-        }
         if (!componentParametersView.isIgnoreComponentInstancesInterfaces()) {
             JanusGraphOperationStatus storageStatus = setComponentInstancesInterfacesFromGraph(componentV, toscaElement);
             if (storageStatus != JanusGraphOperationStatus.OK) {
@@ -804,18 +834,6 @@ public class TopologyTemplateOperation extends ToscaElementOperation {
         return JanusGraphOperationStatus.OK;
     }
 
-    private JanusGraphOperationStatus setInterfcesFromGraph(GraphVertex componentV, TopologyTemplate topologyTemplate) {
-        Either<Map<String, InterfaceDataDefinition>, JanusGraphOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.INTERFACE);
-        if (result.isLeft()) {
-            topologyTemplate.setInterfaces(result.left().value());
-        } else {
-            if (result.right().value() != JanusGraphOperationStatus.NOT_FOUND) {
-                return result.right().value();
-            }
-        }
-        return JanusGraphOperationStatus.OK;
-    }
-
     private JanusGraphOperationStatus setPoliciesFromGraph(GraphVertex componentV, TopologyTemplate toscaElement) {
         Either<Map<String, PolicyDataDefinition>, JanusGraphOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.POLICIES);
         if (result.isLeft()) {
@@ -982,7 +1000,7 @@ public class TopologyTemplateOperation extends ToscaElementOperation {
             if (MapUtils.isEmpty(filters)) {
                 return JanusGraphOperationStatus.OK;
             }
-            if(filters.values().size() > 1) {
+            if (filters.values().size() > 1) {
                 log.error(EcompLoggerErrorCode.DATA_ERROR, TopologyTemplateOperation.class.getName(),
                     (ErrorLogOptionalData) null, "Only a single substitution filter is expected, but got '{}'", filters.values().size());
                 return JanusGraphOperationStatus.GENERAL_ERROR;
@@ -1166,14 +1184,16 @@ public class TopologyTemplateOperation extends ToscaElementOperation {
         category.setUniqueId(categoryV.getUniqueId());
         category.setNormalizedName((String) metadataProperties.get(GraphPropertyEnum.NORMALIZED_NAME));
         category.setName((String) metadataProperties.get(GraphPropertyEnum.NAME));
-        final Boolean useServiceSubstitutionForNestedServices = (Boolean) metadataProperties
-            .get(GraphPropertyEnum.USE_SUBSTITUTION_FOR_NESTED_SERVICES);
+        final Object useServiceSubstitutionForNestedServices = metadataProperties.get(GraphPropertyEnum.USE_SUBSTITUTION_FOR_NESTED_SERVICES);
         category.setUseServiceSubstitutionForNestedServices(
-            useServiceSubstitutionForNestedServices == null ? false : useServiceSubstitutionForNestedServices);
+            useServiceSubstitutionForNestedServices != null && (boolean) useServiceSubstitutionForNestedServices);
         Type listTypeCat = new TypeToken<List<String>>() {
         }.getType();
-        List<String> iconsfromJsonCat = getGson().fromJson((String) metadataProperties.get(GraphPropertyEnum.ICONS.getProperty()), listTypeCat);
-        category.setIcons(iconsfromJsonCat);
+        category.setNotApplicableMetadataKeys(
+            (getGson().fromJson((String) metadataProperties.get(GraphPropertyEnum.NOT_APPLICABLE_METADATA_KEYS.getProperty()), listTypeCat)));
+        List<String> iconsFromJsonCat = getGson().fromJson((String) metadataProperties.get(GraphPropertyEnum.ICONS.getProperty()), listTypeCat);
+        category.setIcons(iconsFromJsonCat);
+        category.setModels((getGson().fromJson((String) metadataProperties.get(GraphPropertyEnum.MODEL.getProperty()), listTypeCat)));
         final Type metadataKeysTypeCat = new TypeToken<List<MetadataKeyDataDefinition>>() {
         }.getType();
         final List<MetadataKeyDataDefinition> metadataKeysfromJsonCat = getGson()
@@ -1489,6 +1509,16 @@ public class TopologyTemplateOperation extends ToscaElementOperation {
         return StorageOperationStatus.OK;
     }
 
+
+    public StorageOperationStatus updateComponentInterfaces(final String componentId, final MapInterfaceDataDefinition instanceInterfaces,
+                                                            final String componentInterfaceUpdatedKey) {
+        if (MapUtils.isNotEmpty(instanceInterfaces.getMapToscaDataDefinition())) {
+            return updateToscaDataDeepElementsBlockToToscaElement(componentId, EdgeLabelEnum.INTERFACE_ARTIFACTS, instanceInterfaces,
+                componentInterfaceUpdatedKey);
+        }
+        return StorageOperationStatus.OK;
+    }
+
     private boolean isNotEmptyMapOfProperties(String instanceId, Map<String, MapCapabilityProperty> mapPropertiesDataDefinition) {
         return MapUtils.isNotEmpty(mapPropertiesDataDefinition) && instanceId != null && mapPropertiesDataDefinition.get(instanceId) != null
             && MapUtils.isNotEmpty(mapPropertiesDataDefinition.get(instanceId).getMapToscaDataDefinition());
@@ -1548,7 +1578,7 @@ public class TopologyTemplateOperation extends ToscaElementOperation {
     }
 
     public StorageOperationStatus removePolicyFromToscaElement(GraphVertex componentV, String policyId) {
-        return deleteToscaDataElement(componentV, EdgeLabelEnum.POLICIES, VertexTypeEnum.POLICIES, policyId, JsonPresentationFields.UNIQUE_ID);
+        return deleteToscaDataElement(componentV, EdgeLabelEnum.POLICIES, policyId);
     }
 
     public StorageOperationStatus updateGroupOfToscaElement(GraphVertex componentV, GroupDefinition groupDefinition) {
@@ -1557,9 +1587,12 @@ public class TopologyTemplateOperation extends ToscaElementOperation {
     }
 
     private void fillPolicyDefinition(GraphVertex componentV, PolicyDefinition policyDefinition, int counter) {
-        String policyName = buildSubComponentName((String) componentV.getJsonMetadataField(JsonPresentationFields.NAME),
-            policyDefinition.getPolicyTypeName(), counter);
-        policyDefinition.setName(policyName);
+        String policyName = policyDefinition.getName();
+        if (StringUtils.isBlank(policyName)) {
+            policyName = buildSubComponentName((String) componentV.getJsonMetadataField(JsonPresentationFields.NAME),
+                policyDefinition.getPolicyTypeName(), counter);
+            policyDefinition.setName(policyName);
+        }
         policyDefinition.setInvariantName(policyName);
         policyDefinition.setComponentName((String) componentV.getJsonMetadataField(JsonPresentationFields.NAME));
         policyDefinition.setUniqueId(UniqueIdBuilder.buildPolicyUniqueId(componentV.getUniqueId(), policyName));