Fix 'Unable to drag a VFC on to composition if an existing VFC instance has the same...
[sdc.git] / catalog-model / src / main / java / org / openecomp / sdc / be / model / jsonjanusgraph / operations / ToscaOperationFacade.java
index 0546a91..d654f23 100644 (file)
  */
 package org.openecomp.sdc.be.model.jsonjanusgraph.operations;
 
-import static java.util.Objects.requireNonNull;
-import static org.apache.commons.collections.CollectionUtils.isEmpty;
-import static org.apache.commons.collections.CollectionUtils.isNotEmpty;
-import static org.janusgraph.core.attribute.Text.REGEX;
-
 import com.vdurmont.semver4j.Semver;
 import com.vdurmont.semver4j.Semver.SemverType;
 import fj.data.Either;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.Comparator;
-import java.util.EnumMap;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.Iterator;
-import java.util.LinkedList;
-import java.util.List;
-import java.util.Map;
-import java.util.Map.Entry;
-import java.util.Objects;
-import java.util.Optional;
-import java.util.Set;
-import java.util.TreeSet;
-import java.util.function.BiPredicate;
-import java.util.stream.Collectors;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 import org.apache.commons.collections.CollectionUtils;
 import org.apache.commons.collections.MapUtils;
 import org.apache.commons.lang3.StringUtils;
@@ -55,6 +33,7 @@ import org.apache.tinkerpop.gremlin.structure.Edge;
 import org.janusgraph.graphdb.query.JanusGraphPredicate;
 import org.openecomp.sdc.be.config.Configuration;
 import org.openecomp.sdc.be.config.ConfigurationManager;
+import org.openecomp.sdc.be.dao.api.exception.JanusGraphException;
 import org.openecomp.sdc.be.dao.janusgraph.HealingJanusGraphDao;
 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
@@ -67,6 +46,7 @@ import org.openecomp.sdc.be.datatypes.elements.CapabilityDataDefinition;
 import org.openecomp.sdc.be.datatypes.elements.ComponentInstanceDataDefinition;
 import org.openecomp.sdc.be.datatypes.elements.DataTypeDataDefinition;
 import org.openecomp.sdc.be.datatypes.elements.GroupDataDefinition;
+import org.openecomp.sdc.be.datatypes.elements.InterfaceDataDefinition;
 import org.openecomp.sdc.be.datatypes.elements.ListCapabilityDataDefinition;
 import org.openecomp.sdc.be.datatypes.elements.ListRequirementDataDefinition;
 import org.openecomp.sdc.be.datatypes.elements.MapArtifactDataDefinition;
@@ -117,7 +97,6 @@ import org.openecomp.sdc.be.model.catalog.CatalogComponent;
 import org.openecomp.sdc.be.model.jsonjanusgraph.config.ContainerInstanceTypesData;
 import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.TopologyTemplate;
 import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.ToscaElement;
-import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.ToscaElementTypeEnum;
 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.exception.ToscaOperationExceptionSupplier;
 import org.openecomp.sdc.be.model.jsonjanusgraph.utils.ModelConverter;
 import org.openecomp.sdc.be.model.operations.StorageException;
@@ -129,10 +108,37 @@ import org.openecomp.sdc.be.model.utils.GroupUtils;
 import org.openecomp.sdc.be.resources.data.ComponentMetadataData;
 import org.openecomp.sdc.common.jsongraph.util.CommonUtility;
 import org.openecomp.sdc.common.jsongraph.util.CommonUtility.LogLevelEnum;
+import org.openecomp.sdc.common.log.enums.EcompLoggerErrorCode;
 import org.openecomp.sdc.common.log.wrappers.Logger;
 import org.openecomp.sdc.common.util.ValidationUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.EnumMap;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Objects;
+import java.util.Optional;
+import java.util.Set;
+import java.util.TreeSet;
+import java.util.function.BiPredicate;
+import java.util.stream.Collectors;
+
+import static java.util.Objects.requireNonNull;
+import static org.apache.commons.collections.CollectionUtils.isEmpty;
+import static org.apache.commons.collections.CollectionUtils.isNotEmpty;
+import static org.janusgraph.core.attribute.Text.REGEX;
+import static org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum.TOPOLOGY_TEMPLATE;
+
 @org.springframework.stereotype.Component("tosca-operation-facade")
 public class ToscaOperationFacade {
 
@@ -147,6 +153,8 @@ public class ToscaOperationFacade {
     private static final String VF = "VF";
     private static final String NOT_SUPPORTED_COMPONENT_TYPE = "Not supported component type {}";
     private static final String COMPONENT_CREATED_SUCCESSFULLY = "Component created successfully!!!";
+    private static final String INPUTS_ASSOCIATED_TO_COMPONENT_SUCCESSFULLY = "Inputs associated to component successfully!";
+    private static final String OUTPUTS_ASSOCIATED_TO_COMPONENT_SUCCESSFULLY = "Outputs associated to component successfully!";
     private static final String COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR = "Couldn't fetch component with and unique id {}, error: {}";
     private static final Logger log = Logger.getLogger(ToscaOperationFacade.class.getName());
     @Autowired
@@ -599,7 +607,7 @@ public class ToscaOperationFacade {
 
     public Either<Resource, StorageOperationStatus> getLatestResourceByToscaResourceName(String toscaResourceName) {
         if (toscaResourceName != null && toscaResourceName.contains("org.openecomp.resource.vf")) {
-            return getLatestResourceByToscaResourceName(toscaResourceName, VertexTypeEnum.TOPOLOGY_TEMPLATE, JsonParseFlagEnum.ParseMetadata);
+            return getLatestResourceByToscaResourceName(toscaResourceName, TOPOLOGY_TEMPLATE, JsonParseFlagEnum.ParseMetadata);
         } else {
             return getLatestResourceByToscaResourceName(toscaResourceName, VertexTypeEnum.NODE_TYPE, JsonParseFlagEnum.ParseMetadata);
         }
@@ -676,7 +684,7 @@ public class ToscaOperationFacade {
     public Either<List<RequirementCapabilityRelDef>, StorageOperationStatus> associateResourceInstances(Component component, String componentId,
                                                                                                         List<RequirementCapabilityRelDef> relations) {
         Either<List<RequirementCapabilityRelDef>, StorageOperationStatus> reqAndCapListEither = nodeTemplateOperation
-            .associateResourceInstances(component, componentId, relations);
+            .associateResourceInstances(componentId, relations);
         if (component != null) {
             updateInstancesCapAndReqOnComponentFromDB(component);
         }
@@ -779,7 +787,7 @@ public class ToscaOperationFacade {
         return updateToscaElement(componentToUpdate, new ComponentParametersView());
     }
 
-    public <T extends Component> Either<T, StorageOperationStatus> updateToscaElement(T componentToUpdate, ComponentParametersView filterResult) {
+    private <T extends Component> Either<T, StorageOperationStatus> updateToscaElement(T componentToUpdate, ComponentParametersView filterResult) {
         String componentId = componentToUpdate.getUniqueId();
         Either<GraphVertex, JanusGraphOperationStatus> getVertexEither = janusGraphDao.getVertexById(componentId, JsonParseFlagEnum.ParseAll);
         if (getVertexEither.isRight()) {
@@ -843,44 +851,26 @@ public class ToscaOperationFacade {
         return getLatestByName(property, nodeName, JsonParseFlagEnum.ParseMetadata, modelName);
     }
 
-    public <T extends Component> Either<List<T>, StorageOperationStatus> getBySystemName(ComponentTypeEnum componentType, String systemName) {
-        Either<List<T>, StorageOperationStatus> result = null;
-        Either<T, StorageOperationStatus> getComponentRes;
-        List<T> components = new ArrayList<>();
-        List<GraphVertex> componentVertices;
-        Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
-        Map<GraphPropertyEnum, Object> propertiesNotToMatch = new EnumMap<>(GraphPropertyEnum.class);
+    public <T extends Component> Either<T, StorageOperationStatus> getBySystemNameAndVersion(final ComponentTypeEnum componentType,
+                                                                                             final String systemName,
+                                                                                             final String version) {
+        final Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
+        final Map<GraphPropertyEnum, Object> propertiesNotToMatch = new EnumMap<>(GraphPropertyEnum.class);
         propertiesToMatch.put(GraphPropertyEnum.SYSTEM_NAME, systemName);
+        propertiesToMatch.put(GraphPropertyEnum.VERSION, version);
         if (componentType != null) {
             propertiesToMatch.put(GraphPropertyEnum.COMPONENT_TYPE, componentType.name());
         }
         propertiesNotToMatch.put(GraphPropertyEnum.IS_DELETED, true);
-        Either<List<GraphVertex>, JanusGraphOperationStatus> getComponentsRes = janusGraphDao
-            .getByCriteria(null, propertiesToMatch, propertiesNotToMatch, JsonParseFlagEnum.ParseAll);
-        if (getComponentsRes.isRight()) {
-            JanusGraphOperationStatus status = getComponentsRes.right().value();
-            log.debug("Failed to fetch the component with system name {}. Status is {} ", systemName, status);
-            result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
-        }
-        if (result == null) {
-            componentVertices = getComponentsRes.left().value();
-            for (GraphVertex componentVertex : componentVertices) {
-                getComponentRes = getToscaElementByOperation(componentVertex);
-                if (getComponentRes.isRight()) {
-                    log.debug("Failed to get the component {}. Status is {} ", componentVertex.getJsonMetadataField(JsonPresentationFields.NAME),
-                        getComponentRes.right().value());
-                    result = Either.right(getComponentRes.right().value());
-                    break;
-                }
-                T componentBySystemName = getComponentRes.left().value();
-                log.debug("Found component, id: {}", componentBySystemName.getUniqueId());
-                components.add(componentBySystemName);
-            }
-        }
-        if (result == null) {
-            result = Either.left(components);
+
+        final Either<List<GraphVertex>, JanusGraphOperationStatus> getResourceResult
+            = janusGraphDao.getByCriteria(null, propertiesToMatch, propertiesNotToMatch, JsonParseFlagEnum.ParseAll);
+        if (getResourceResult.isRight()) {
+            final JanusGraphOperationStatus status = getResourceResult.right().value();
+            log.debug("Failed to find resource with systemName {}, version {}. Status is {} ", systemName, version, status);
+            return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
         }
-        return result;
+        return getToscaElementByOperation(getResourceResult.left().value().get(0));
     }
 
     public <T extends Component> Either<T, StorageOperationStatus> getComponentByNameAndVersion(ComponentTypeEnum componentType, String name,
@@ -890,7 +880,6 @@ public class ToscaOperationFacade {
 
     public <T extends Component> Either<T, StorageOperationStatus> getComponentByNameAndVersion(ComponentTypeEnum componentType, String name,
                                                                                                 String version, JsonParseFlagEnum parseFlag) {
-        Either<T, StorageOperationStatus> result;
         Map<GraphPropertyEnum, Object> hasProperties = new EnumMap<>(GraphPropertyEnum.class);
         Map<GraphPropertyEnum, Object> hasNotProperties = new EnumMap<>(GraphPropertyEnum.class);
         hasProperties.put(GraphPropertyEnum.NAME, name);
@@ -904,8 +893,7 @@ public class ToscaOperationFacade {
         if (getResourceRes.isRight()) {
             JanusGraphOperationStatus status = getResourceRes.right().value();
             log.debug("failed to find resource with name {}, version {}. Status is {} ", name, version, status);
-            result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
-            return result;
+            return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
         }
         return getToscaElementByOperation(getResourceRes.left().value().get(0));
     }
@@ -1001,11 +989,15 @@ public class ToscaOperationFacade {
     }
 
     public List<String> deleteService(String invariantUUID, final boolean inTransaction) {
-        List<GraphVertex> allServiceVerticesToDelete = getVerticesForAllVersions(invariantUUID, ToscaElementTypeEnum.TOPOLOGY_TEMPLATE);
+        return deleteComponent(invariantUUID, NodeTypeEnum.Service, inTransaction);
+    }
+
+    public List<String> deleteComponent(String invariantUUID, NodeTypeEnum componentType, final boolean inTransaction) {
+        final List<GraphVertex> allServiceVerticesToDelete = findVertexListByInvariantUuid(invariantUUID);
         List<String> affectedComponentIds = new ArrayList<>();
         try {
-            checkNotUsed( allServiceVerticesToDelete);
-            lockAllVerticesByNodeType(allServiceVerticesToDelete, NodeTypeEnum.Service);
+            checkNotUsed(allServiceVerticesToDelete);
+            lockAllVerticesByNodeType(allServiceVerticesToDelete, componentType);
             for (GraphVertex elementV : allServiceVerticesToDelete) {
                 Either<ToscaElement, StorageOperationStatus> deleteToscaElement = deleteToscaElement(elementV);
                 if (deleteToscaElement.isRight()) {
@@ -1024,7 +1016,7 @@ public class ToscaOperationFacade {
             }
             throw exception;
         } finally {
-            unlockAllVerticesByNodeType(allServiceVerticesToDelete, NodeTypeEnum.Service);
+            unlockAllVerticesByNodeType(allServiceVerticesToDelete, componentType);
         }
         return affectedComponentIds;
     }
@@ -1032,12 +1024,12 @@ public class ToscaOperationFacade {
     private void checkNotUsed(List<GraphVertex> vertices) {
         boolean isInUse = isAnyComponentInUse(vertices);
         if (isInUse) {
-            Set<GraphVertex> listOfVertices =  getComponentsUsingComponents(vertices);
+            Set<GraphVertex> listOfVertices = getComponentsUsingComponents(vertices);
             List<String> listOfStringComponents = new ArrayList<>();
             for (GraphVertex componentVertex : listOfVertices) {
                 listOfStringComponents.add(
-                        componentVertex.getMetadataJson().get(GraphPropertyEnum.COMPONENT_TYPE.getProperty()) + " "
-                                + componentVertex.getMetadataJson().get(GraphPropertyEnum.NAME.getProperty())
+                    componentVertex.getMetadataJson().get(GraphPropertyEnum.COMPONENT_TYPE.getProperty()) + " "
+                        + componentVertex.getMetadataJson().get(GraphPropertyEnum.NAME.getProperty())
                 );
             }
             String stringOfComponents = String.join(", ", listOfStringComponents);
@@ -1045,22 +1037,13 @@ public class ToscaOperationFacade {
         }
     }
 
-    private List<GraphVertex> getVerticesForAllVersions(String invariantUUID, ToscaElementTypeEnum componentType){
-        Either<List<Component>, StorageOperationStatus> allComponents =
-                getComponentListByInvariantUuid(invariantUUID, null);
-        if (allComponents.isRight()) {
-            throwStorageException(allComponents.right().value());
-        }
-        List<GraphVertex> allComponentVertices = new ArrayList<>();
-        for (Component component : allComponents.left().value()) {
-            Either<GraphVertex, StorageOperationStatus> componentGraphVertex = topologyTemplateOperation
-                    .getComponentByLabelAndId(component.getUniqueId(), componentType, JsonParseFlagEnum.ParseAll);
-            if (componentGraphVertex.isRight()) {
-                throwStorageException(componentGraphVertex.right().value());
-            }
-            allComponentVertices.add(componentGraphVertex.left().value());
+    public List<GraphVertex> findVertexListByInvariantUuid(final String invariantUuid) {
+        try {
+            return janusGraphDao.findAllVertexByInvariantUuid(invariantUuid, Collections.emptyMap());
+        } catch (final JanusGraphException e) {
+            log.error(EcompLoggerErrorCode.DATA_ERROR, this.getClass().getName(), e.getMessage());
+            throw new StorageException(e.getStatus());
         }
-        return allComponentVertices;
     }
 
     public void commitAndCheck(String componentId) {
@@ -1098,10 +1081,10 @@ public class ToscaOperationFacade {
 
     private List<GraphVertex> isInUse(GraphVertex elementV) {
         final List<EdgeLabelEnum> forbiddenEdgeLabelEnums = Arrays
-                .asList(EdgeLabelEnum.INSTANCE_OF, EdgeLabelEnum.PROXY_OF, EdgeLabelEnum.ALLOTTED_OF);
+            .asList(EdgeLabelEnum.INSTANCE_OF, EdgeLabelEnum.PROXY_OF, EdgeLabelEnum.ALLOTTED_OF);
         for (EdgeLabelEnum edgeLabelEnum : forbiddenEdgeLabelEnums) {
             Either<List<GraphVertex>, JanusGraphOperationStatus> inUseBy =
-                    janusGraphDao.getParentVertices(elementV, edgeLabelEnum, JsonParseFlagEnum.ParseAll);
+                janusGraphDao.getParentVertices(elementV, edgeLabelEnum, JsonParseFlagEnum.ParseAll);
             if (inUseBy.isLeft()) {
                 if (log.isDebugEnabled()) {
                     log.debug("Element {} in use.", elementV.getUniqueId());
@@ -1322,14 +1305,22 @@ public class ToscaOperationFacade {
     /**
      * @return max counter of component instance Id's, null if not found
      */
-    private Integer getMaxCounterFromNamesAndIds(Component containerComponent, String normalizedName) {
-        List<String> countersInNames = containerComponent.getComponentInstances().stream()
+    private Integer getMaxCounterFromNamesAndIds(final Component containerComponent, final String normalizedName) {
+        final Pattern COUNTER_PATTERN = Pattern.compile(normalizedName + "[\\s_:-]?\\d+$");
+        final List<String> countersInNames = containerComponent.getComponentInstances().stream()
             .filter(ci -> ci.getNormalizedName() != null && ci.getNormalizedName().startsWith(normalizedName))
-            .map(ci -> ci.getNormalizedName().split(normalizedName)[1]).collect(Collectors.toList());
-        List<String> countersInIds = containerComponent.getComponentInstances().stream()
-            .filter(ci -> ci.getUniqueId() != null && ci.getUniqueId().contains(normalizedName)).map(ci -> ci.getUniqueId().split(normalizedName)[1])
+            .filter(ci -> !ci.getNormalizedName().equals(normalizedName))
+            .map(ComponentInstance::getNormalizedName)
+            .map(COUNTER_PATTERN::matcher).filter(Matcher::find).map(matcher -> matcher.group(0))
+            .map(nn -> nn.replaceAll("\\D", ""))
+            .collect(Collectors.toList());
+        final List<String> countersInIds = containerComponent.getComponentInstances().stream()
+            .filter(ci -> ci.getUniqueId() != null && ci.getUniqueId().contains(normalizedName))
+            .map(ComponentInstance::getUniqueId)
+            .map(COUNTER_PATTERN::matcher).filter(Matcher::find).map(matcher -> matcher.group(0))
+            .map(nn -> nn.replaceAll("\\D", ""))
             .collect(Collectors.toList());
-        List<String> namesAndIdsList = new ArrayList<>(countersInNames);
+        final List<String> namesAndIdsList = new ArrayList<>(countersInNames);
         namesAndIdsList.addAll(countersInIds);
         return getMaxInteger(namesAndIdsList);
     }
@@ -1344,7 +1335,6 @@ public class ToscaOperationFacade {
                     maxCounter = currCounter;
                 }
             } catch (NumberFormatException e) {
-                continue;
             }
         }
         return currCounter == null ? null : maxCounter;
@@ -1352,7 +1342,7 @@ public class ToscaOperationFacade {
 
     public Either<RequirementCapabilityRelDef, StorageOperationStatus> associateResourceInstances(Component component, String componentId,
                                                                                                   RequirementCapabilityRelDef requirementDef) {
-        return nodeTemplateOperation.associateResourceInstances(component, componentId, requirementDef);
+        return nodeTemplateOperation.associateResourceInstances(componentId, requirementDef);
     }
 
     public Either<List<InputDefinition>, StorageOperationStatus> createAndAssociateInputs(Map<String, InputDefinition> inputs, String componentId) {
@@ -1366,7 +1356,7 @@ public class ToscaOperationFacade {
             .collect(Collectors.toMap(Map.Entry::getKey, e -> new PropertyDataDefinition(e.getValue())));
         StorageOperationStatus status = topologyTemplateOperation.associateInputsToComponent(vertex, inputsMap, componentId);
         if (StorageOperationStatus.OK == status) {
-            log.debug(COMPONENT_CREATED_SUCCESSFULLY);
+            log.debug(INPUTS_ASSOCIATED_TO_COMPONENT_SUCCESSFULLY);
             List<InputDefinition> inputsResList = null;
             if (inputsMap != null && !inputsMap.isEmpty()) {
                 inputsResList = inputsMap.values().stream().map(InputDefinition::new).collect(Collectors.toList());
@@ -1376,6 +1366,28 @@ public class ToscaOperationFacade {
         return Either.right(status);
     }
 
+    public Either<List<OutputDefinition>, StorageOperationStatus> createAndAssociateOutputs(final Map<String, OutputDefinition> outputs,
+                                                                                            final String componentId) {
+        final Either<GraphVertex, JanusGraphOperationStatus> getVertexEither = janusGraphDao.getVertexById(componentId, JsonParseFlagEnum.NoParse);
+        if (getVertexEither.isRight()) {
+            log.debug(COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, componentId, getVertexEither.right().value());
+            return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVertexEither.right().value()));
+        }
+        final GraphVertex vertex = getVertexEither.left().value();
+        final Map<String, OutputDefinition> outputsMap = outputs.entrySet().stream()
+            .collect(Collectors.toMap(Map.Entry::getKey, e -> new OutputDefinition(e.getValue())));
+        final StorageOperationStatus status = topologyTemplateOperation.associateOutputsToComponent(vertex, outputsMap, componentId);
+        if (StorageOperationStatus.OK == status) {
+            log.debug(OUTPUTS_ASSOCIATED_TO_COMPONENT_SUCCESSFULLY);
+            List<OutputDefinition> outputsResList = null;
+            if (MapUtils.isNotEmpty(outputsMap)) {
+                outputsResList = outputsMap.values().stream().map(OutputDefinition::new).collect(Collectors.toList());
+            }
+            return Either.left(outputsResList);
+        }
+        return Either.right(status);
+    }
+
     public Either<List<InputDefinition>, StorageOperationStatus> addInputsToComponent(Map<String, InputDefinition> inputs, String componentId) {
         Either<GraphVertex, JanusGraphOperationStatus> getVertexEither = janusGraphDao.getVertexById(componentId, JsonParseFlagEnum.NoParse);
         if (getVertexEither.isRight()) {
@@ -1513,7 +1525,7 @@ public class ToscaOperationFacade {
         if (StorageOperationStatus.OK == status) {
             log.debug(COMPONENT_CREATED_SUCCESSFULLY);
             List<InputDefinition> inputsResList = null;
-            if (inputsAsDataDef != null && !inputsAsDataDef.isEmpty()) {
+            if (CollectionUtils.isNotEmpty(inputsAsDataDef)) {
                 inputsResList = inputsAsDataDef.stream().map(InputDefinition::new).collect(Collectors.toList());
             }
             return Either.left(inputsResList);
@@ -1521,6 +1533,27 @@ public class ToscaOperationFacade {
         return Either.right(status);
     }
 
+    public Either<List<OutputDefinition>, StorageOperationStatus> updateOutputsToComponent(List<OutputDefinition> outputs, String componentId) {
+        Either<GraphVertex, JanusGraphOperationStatus> getVertexEither = janusGraphDao.getVertexById(componentId, JsonParseFlagEnum.NoParse);
+        if (getVertexEither.isRight()) {
+            log.debug(COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, componentId, getVertexEither.right().value());
+            return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVertexEither.right().value()));
+        }
+        GraphVertex vertex = getVertexEither.left().value();
+        List<AttributeDataDefinition> outputsAsDataDef = outputs.stream().map(AttributeDataDefinition::new).collect(Collectors.toList());
+        StorageOperationStatus status = topologyTemplateOperation
+            .updateToscaDataOfToscaElement(vertex, EdgeLabelEnum.OUTPUTS, VertexTypeEnum.OUTPUTS, outputsAsDataDef, JsonPresentationFields.NAME);
+        if (StorageOperationStatus.OK == status) {
+            log.debug(COMPONENT_CREATED_SUCCESSFULLY);
+            List<OutputDefinition> outputsResList = null;
+            if (!outputsAsDataDef.isEmpty()) {
+                outputsResList = outputsAsDataDef.stream().map(OutputDefinition::new).collect(Collectors.toList());
+            }
+            return Either.left(outputsResList);
+        }
+        return Either.right(status);
+    }
+
     // region - ComponentInstance
     public Either<Map<String, List<ComponentInstanceProperty>>, StorageOperationStatus> associateComponentInstancePropertiesToComponent(
         Map<String, List<ComponentInstanceProperty>> instProperties, String componentId) {
@@ -1624,6 +1657,49 @@ public class ToscaOperationFacade {
         return Either.right(status);
     }
 
+    public Either<Map<String, MapInterfaceDataDefinition>, StorageOperationStatus> associateComponentInstanceInterfacesToComponent(
+        Map<String, Map<String, InterfaceDefinition>> instInterfaces,
+        String componentId
+    ) {
+        Either<GraphVertex, JanusGraphOperationStatus> getVertexEither = janusGraphDao.getVertexById(
+            componentId,
+            JsonParseFlagEnum.NoParse
+        );
+        if (getVertexEither.isRight()) {
+            log.debug(
+                COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR,
+                componentId,
+                getVertexEither.right().value()
+            );
+            return Either.right(
+                DaoStatusConverter.convertJanusGraphStatusToStorageStatus(
+                    getVertexEither.right().value()
+                )
+            );
+        }
+        GraphVertex vertex = getVertexEither.left().value();
+        Map<String, MapInterfaceDataDefinition> instInterfacesMap = new HashMap<>();
+        if (instInterfaces != null) {
+
+            for (Map.Entry<String, Map<String, InterfaceDefinition>> entryInstances : instInterfaces.entrySet()) {
+                Map<String, InterfaceDataDefinition> incomingInterfacesMap = entryInstances.getValue().entrySet().stream()
+                    .collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue()));
+                MapInterfaceDataDefinition interfacesMap = new MapInterfaceDataDefinition();
+                interfacesMap.setMapToscaDataDefinition(incomingInterfacesMap);
+                instInterfacesMap.put(entryInstances.getKey(), interfacesMap);
+            }
+        }
+        StorageOperationStatus status = topologyTemplateOperation.associateInstInterfacesToComponent(
+            vertex,
+            instInterfacesMap
+        );
+        if (StorageOperationStatus.OK == status) {
+            log.debug(COMPONENT_CREATED_SUCCESSFULLY);
+            return Either.left(instInterfacesMap);
+        }
+        return Either.right(status);
+    }
+
     public Either<Map<String, List<ComponentInstanceInput>>, StorageOperationStatus> addComponentInstanceInputsToComponent(
         Component containerComponent, Map<String, List<ComponentInstanceInput>> instProperties) {
         requireNonNull(instProperties);
@@ -1654,33 +1730,33 @@ public class ToscaOperationFacade {
     }
 
     public Either<Map<String, List<ComponentInstanceOutput>>, StorageOperationStatus> addComponentInstanceOutputsToComponent(
-        Component containerComponent, Map<String, List<ComponentInstanceOutput>> instProperties) {
-        requireNonNull(instProperties);
+        Component containerComponent, Map<String, List<ComponentInstanceOutput>> instOutputs) {
+        requireNonNull(instOutputs);
         StorageOperationStatus status;
-        for (final Entry<String, List<ComponentInstanceOutput>> entry : instProperties.entrySet()) {
-            final List<ComponentInstanceOutput> props = entry.getValue();
+        for (final Entry<String, List<ComponentInstanceOutput>> entry : instOutputs.entrySet()) {
+            final List<ComponentInstanceOutput> outputs = entry.getValue();
             final String componentInstanceId = entry.getKey();
-            if (!isEmpty(props)) {
-                for (final ComponentInstanceOutput property : props) {
-                    final List<ComponentInstanceOutput> componentInstancesInputs = containerComponent.getComponentInstancesOutputs()
+            if (!isEmpty(outputs)) {
+                for (final ComponentInstanceOutput output : outputs) {
+                    final List<ComponentInstanceOutput> componentInstanceOutputs = containerComponent.getComponentInstancesOutputs()
                         .get(componentInstanceId);
-                    final Optional<ComponentInstanceOutput> instanceProperty = componentInstancesInputs.stream()
-                        .filter(p -> p.getName().equals(property.getName())).findAny();
-                    if (instanceProperty.isPresent()) {
-                        status = updateComponentInstanceOutput(containerComponent, componentInstanceId, property);
+                    final Optional<ComponentInstanceOutput> componentInstanceOutput = componentInstanceOutputs.stream()
+                        .filter(p -> p.getName().equals(output.getName())).findAny();
+                    if (componentInstanceOutput.isPresent()) {
+                        status = updateComponentInstanceOutput(containerComponent, componentInstanceId, output);
                     } else {
-                        status = addComponentInstanceOutput(containerComponent, componentInstanceId, property);
+                        status = addComponentInstanceOutput(containerComponent, componentInstanceId, output);
                     }
                     if (status != StorageOperationStatus.OK) {
-                        log.debug("Failed to update instance input {} for instance {} error {} ", property, componentInstanceId, status);
+                        log.debug("Failed to update instance output {} for instance {} error {} ", output, componentInstanceId, status);
                         return Either.right(status);
                     } else {
-                        log.trace("instance input {} for instance {} updated", property, componentInstanceId);
+                        log.trace("instance output {} for instance {} updated", output, componentInstanceId);
                     }
                 }
             }
         }
-        return Either.left(instProperties);
+        return Either.left(instOutputs);
     }
 
     public Either<Map<String, List<ComponentInstanceProperty>>, StorageOperationStatus> addComponentInstancePropertiesToComponent(
@@ -2041,6 +2117,7 @@ public class ToscaOperationFacade {
         Component updatedComponent = componentEither.left().value();
         component.setCapabilities(updatedComponent.getCapabilities());
         component.setRequirements(updatedComponent.getRequirements());
+        component.setComponentInstancesRelations(updatedComponent.getComponentInstancesRelations());
         component.setComponentInstances(updatedComponent.getComponentInstances());
     }
 
@@ -2268,7 +2345,7 @@ public class ToscaOperationFacade {
     }
 
     private VertexTypeEnum getVertexTypeEnum(final ResourceTypeEnum resourceType) {
-        return ModelConverter.isAtomicComponent(resourceType) ? VertexTypeEnum.NODE_TYPE : VertexTypeEnum.TOPOLOGY_TEMPLATE;
+        return ModelConverter.isAtomicComponent(resourceType) ? VertexTypeEnum.NODE_TYPE : TOPOLOGY_TEMPLATE;
     }
 
     private Map<GraphPropertyEnum, Object> propertiesToMatch(final String normalizedName, final ComponentTypeEnum componentType) {
@@ -2340,7 +2417,7 @@ public class ToscaOperationFacade {
         }
         if (ComponentTypeEnum.SERVICE == componentTypeEnum || SERVICE.equalsIgnoreCase(internalComponentType) || VF.equalsIgnoreCase(
             internalComponentType)) {
-            internalVertexTypes.add(VertexTypeEnum.TOPOLOGY_TEMPLATE);
+            internalVertexTypes.add(TOPOLOGY_TEMPLATE);
         }
         return internalVertexTypes;
     }
@@ -2701,7 +2778,7 @@ public class ToscaOperationFacade {
         }
         propertiesToMatch.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
         propertiesNotToMatch.put(GraphPropertyEnum.IS_DELETED, true);
-        if (distStatus != null && !distStatus.isEmpty()) {
+        if (CollectionUtils.isNotEmpty(distStatus)) {
             for (DistributionStatusEnum state : distStatus) {
                 propertiesToMatch.put(GraphPropertyEnum.DISTRIBUTION_STATUS, state.name());
                 Either<List<Service>, StorageOperationStatus> fetchServicesByCriteria = fetchServicesByCriteria(servicesAll, propertiesToMatch,
@@ -2722,8 +2799,12 @@ public class ToscaOperationFacade {
                                                                                   Map<GraphPropertyEnum, Object> propertiesToMatch,
                                                                                   Map<GraphPropertyEnum, Object> propertiesNotToMatch,
                                                                                   String modelName) {
-        Either<List<GraphVertex>, JanusGraphOperationStatus> getRes = janusGraphDao
-            .getByCriteria(VertexTypeEnum.TOPOLOGY_TEMPLATE, propertiesToMatch, propertiesNotToMatch, JsonParseFlagEnum.ParseAll, modelName);
+        Either<List<GraphVertex>, JanusGraphOperationStatus> getRes;
+        if (StringUtils.isEmpty(modelName)) {
+            getRes = janusGraphDao.getByCriteria(TOPOLOGY_TEMPLATE, propertiesToMatch, propertiesNotToMatch, JsonParseFlagEnum.ParseAll);
+        } else {
+            getRes = janusGraphDao.getByCriteria(TOPOLOGY_TEMPLATE, propertiesToMatch, propertiesNotToMatch, JsonParseFlagEnum.ParseAll, modelName);
+        }
         if (getRes.isRight()) {
             if (getRes.right().value() != JanusGraphOperationStatus.NOT_FOUND) {
                 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG,
@@ -2777,9 +2858,9 @@ public class ToscaOperationFacade {
         return nodeTemplateOperation.generateCustomizationUUIDOnInstanceGroup(componentId, instanceId, groupInstances);
     }
 
-    public Either<PropertyDefinition, StorageOperationStatus> addPropertyToComponent(String propertyName, PropertyDefinition newPropertyDefinition,
+    public Either<PropertyDefinition, StorageOperationStatus> addPropertyToComponent(PropertyDefinition newPropertyDefinition,
                                                                                      Component component) {
-        newPropertyDefinition.setName(propertyName);
+        final String propertyName = newPropertyDefinition.getName();
         StorageOperationStatus status = getToscaElementOperation(component)
             .addToscaDataToToscaElement(component.getUniqueId(), EdgeLabelEnum.PROPERTIES, VertexTypeEnum.PROPERTIES, newPropertyDefinition,
                 JsonPresentationFields.NAME);
@@ -3083,7 +3164,7 @@ public class ToscaOperationFacade {
                                                                                                                String componentInstanceId) {
         String uniqueId = componentInstance.getUniqueId();
         StorageOperationStatus status = nodeTemplateOperation
-            .deleteToscaDataDeepElementsBlockOfToscaElement(containerComponent.getUniqueId(), EdgeLabelEnum.INST_GROUPS, VertexTypeEnum.INST_GROUPS,
+            .deleteToscaDataDeepElementsBlockOfToscaElement(containerComponent.getUniqueId(), EdgeLabelEnum.INST_GROUPS,
                 uniqueId);
         if (status != StorageOperationStatus.OK && status != StorageOperationStatus.NOT_FOUND) {
             CommonUtility
@@ -3246,7 +3327,7 @@ public class ToscaOperationFacade {
                                                                                        final Component component) {
 
         final boolean match = component.getInterfaces().keySet().stream().anyMatch(s -> s.equals(interfaceName));
-        StorageOperationStatus status = StorageOperationStatus.OK;
+        StorageOperationStatus status;
         final ToscaElementOperation toscaElementOperation = getToscaElementOperation(component);
         if (match) {
             status = toscaElementOperation.updateToscaDataOfToscaElement(component.getUniqueId(), EdgeLabelEnum.INTERFACE_ARTIFACTS,
@@ -3293,14 +3374,14 @@ public class ToscaOperationFacade {
 
     public StorageOperationStatus deleteAllCalculatedCapabilitiesRequirements(String topologyTemplateId) {
         StorageOperationStatus status = topologyTemplateOperation
-            .removeToscaData(topologyTemplateId, EdgeLabelEnum.CALCULATED_CAPABILITIES, VertexTypeEnum.CALCULATED_CAPABILITIES);
+            .removeToscaData(topologyTemplateId, EdgeLabelEnum.CALCULATED_CAPABILITIES);
         if (status == StorageOperationStatus.OK) {
             status = topologyTemplateOperation
-                .removeToscaData(topologyTemplateId, EdgeLabelEnum.CALCULATED_REQUIREMENTS, VertexTypeEnum.CALCULATED_REQUIREMENTS);
+                .removeToscaData(topologyTemplateId, EdgeLabelEnum.CALCULATED_REQUIREMENTS);
         }
         if (status == StorageOperationStatus.OK) {
             status = topologyTemplateOperation
-                .removeToscaData(topologyTemplateId, EdgeLabelEnum.CALCULATED_CAP_PROPERTIES, VertexTypeEnum.CALCULATED_CAP_PROPERTIES);
+                .removeToscaData(topologyTemplateId, EdgeLabelEnum.CALCULATED_CAP_PROPERTIES);
         }
         return status;
     }
@@ -3375,7 +3456,7 @@ public class ToscaOperationFacade {
             log.error(COULDNT_FETCH_A_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, componentId, getVertexEither.right().value());
             result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVertexEither.right().value()));
         } else {
-            if (getVertexEither.left().value().getLabel() != VertexTypeEnum.TOPOLOGY_TEMPLATE) {
+            if (getVertexEither.left().value().getLabel() != TOPOLOGY_TEMPLATE) {
                 log.error("Policy association to component of Tosca type {} is not allowed. ", getVertexEither.left().value().getLabel());
                 result = Either.right(StorageOperationStatus.BAD_REQUEST);
             }