Implement 'Update Service by importing Tosca Model'-story
[sdc.git] / catalog-model / src / main / java / org / openecomp / sdc / be / model / jsonjanusgraph / operations / ToscaOperationFacade.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20 package org.openecomp.sdc.be.model.jsonjanusgraph.operations;
21
22 import com.vdurmont.semver4j.Semver;
23 import com.vdurmont.semver4j.Semver.SemverType;
24 import fj.data.Either;
25 import java.util.regex.Matcher;
26 import java.util.regex.Pattern;
27 import org.apache.commons.collections.CollectionUtils;
28 import org.apache.commons.collections.MapUtils;
29 import org.apache.commons.lang3.StringUtils;
30 import org.apache.commons.lang3.tuple.ImmutablePair;
31 import org.apache.tinkerpop.gremlin.structure.Direction;
32 import org.apache.tinkerpop.gremlin.structure.Edge;
33 import org.janusgraph.graphdb.query.JanusGraphPredicate;
34 import org.openecomp.sdc.be.config.Configuration;
35 import org.openecomp.sdc.be.config.ConfigurationManager;
36 import org.openecomp.sdc.be.dao.api.exception.JanusGraphException;
37 import org.openecomp.sdc.be.dao.janusgraph.HealingJanusGraphDao;
38 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
39 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
40 import org.openecomp.sdc.be.dao.jsongraph.types.EdgeLabelEnum;
41 import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum;
42 import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum;
43 import org.openecomp.sdc.be.datatypes.elements.ArtifactDataDefinition;
44 import org.openecomp.sdc.be.datatypes.elements.AttributeDataDefinition;
45 import org.openecomp.sdc.be.datatypes.elements.CapabilityDataDefinition;
46 import org.openecomp.sdc.be.datatypes.elements.ComponentInstanceDataDefinition;
47 import org.openecomp.sdc.be.datatypes.elements.DataTypeDataDefinition;
48 import org.openecomp.sdc.be.datatypes.elements.GroupDataDefinition;
49 import org.openecomp.sdc.be.datatypes.elements.InterfaceDataDefinition;
50 import org.openecomp.sdc.be.datatypes.elements.ListCapabilityDataDefinition;
51 import org.openecomp.sdc.be.datatypes.elements.ListRequirementDataDefinition;
52 import org.openecomp.sdc.be.datatypes.elements.MapArtifactDataDefinition;
53 import org.openecomp.sdc.be.datatypes.elements.MapAttributesDataDefinition;
54 import org.openecomp.sdc.be.datatypes.elements.MapCapabilityProperty;
55 import org.openecomp.sdc.be.datatypes.elements.MapInterfaceDataDefinition;
56 import org.openecomp.sdc.be.datatypes.elements.MapListCapabilityDataDefinition;
57 import org.openecomp.sdc.be.datatypes.elements.MapListRequirementDataDefinition;
58 import org.openecomp.sdc.be.datatypes.elements.MapPropertiesDataDefinition;
59 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
60 import org.openecomp.sdc.be.datatypes.elements.RequirementDataDefinition;
61 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
62 import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum;
63 import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
64 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
65 import org.openecomp.sdc.be.datatypes.enums.OriginTypeEnum;
66 import org.openecomp.sdc.be.datatypes.enums.PromoteVersionEnum;
67 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
68 import org.openecomp.sdc.be.model.ArtifactDefinition;
69 import org.openecomp.sdc.be.model.AttributeDefinition;
70 import org.openecomp.sdc.be.model.CapabilityDefinition;
71 import org.openecomp.sdc.be.model.CatalogUpdateTimestamp;
72 import org.openecomp.sdc.be.model.Component;
73 import org.openecomp.sdc.be.model.ComponentInstance;
74 import org.openecomp.sdc.be.model.ComponentInstanceAttribute;
75 import org.openecomp.sdc.be.model.ComponentInstanceInput;
76 import org.openecomp.sdc.be.model.ComponentInstanceInterface;
77 import org.openecomp.sdc.be.model.ComponentInstanceOutput;
78 import org.openecomp.sdc.be.model.ComponentInstanceProperty;
79 import org.openecomp.sdc.be.model.ComponentParametersView;
80 import org.openecomp.sdc.be.model.DataTypeDefinition;
81 import org.openecomp.sdc.be.model.DistributionStatusEnum;
82 import org.openecomp.sdc.be.model.GroupDefinition;
83 import org.openecomp.sdc.be.model.GroupInstance;
84 import org.openecomp.sdc.be.model.InputDefinition;
85 import org.openecomp.sdc.be.model.InterfaceDefinition;
86 import org.openecomp.sdc.be.model.LifecycleStateEnum;
87 import org.openecomp.sdc.be.model.OutputDefinition;
88 import org.openecomp.sdc.be.model.PolicyDefinition;
89 import org.openecomp.sdc.be.model.PropertyDefinition;
90 import org.openecomp.sdc.be.model.RelationshipInfo;
91 import org.openecomp.sdc.be.model.RequirementCapabilityRelDef;
92 import org.openecomp.sdc.be.model.RequirementDefinition;
93 import org.openecomp.sdc.be.model.Resource;
94 import org.openecomp.sdc.be.model.Service;
95 import org.openecomp.sdc.be.model.User;
96 import org.openecomp.sdc.be.model.catalog.CatalogComponent;
97 import org.openecomp.sdc.be.model.jsonjanusgraph.config.ContainerInstanceTypesData;
98 import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.TopologyTemplate;
99 import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.ToscaElement;
100 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.exception.ToscaOperationExceptionSupplier;
101 import org.openecomp.sdc.be.model.jsonjanusgraph.utils.ModelConverter;
102 import org.openecomp.sdc.be.model.operations.StorageException;
103 import org.openecomp.sdc.be.model.operations.api.IGraphLockOperation;
104 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
105 import org.openecomp.sdc.be.model.operations.impl.DaoStatusConverter;
106 import org.openecomp.sdc.be.model.operations.impl.UniqueIdBuilder;
107 import org.openecomp.sdc.be.model.utils.GroupUtils;
108 import org.openecomp.sdc.be.resources.data.ComponentMetadataData;
109 import org.openecomp.sdc.common.jsongraph.util.CommonUtility;
110 import org.openecomp.sdc.common.jsongraph.util.CommonUtility.LogLevelEnum;
111 import org.openecomp.sdc.common.log.enums.EcompLoggerErrorCode;
112 import org.openecomp.sdc.common.log.wrappers.Logger;
113 import org.openecomp.sdc.common.util.ValidationUtils;
114 import org.springframework.beans.factory.annotation.Autowired;
115
116 import java.util.ArrayList;
117 import java.util.Arrays;
118 import java.util.Collection;
119 import java.util.Collections;
120 import java.util.Comparator;
121 import java.util.EnumMap;
122 import java.util.HashMap;
123 import java.util.HashSet;
124 import java.util.Iterator;
125 import java.util.LinkedList;
126 import java.util.List;
127 import java.util.Map;
128 import java.util.Map.Entry;
129 import java.util.Objects;
130 import java.util.Optional;
131 import java.util.Set;
132 import java.util.TreeSet;
133 import java.util.function.BiPredicate;
134 import java.util.stream.Collectors;
135
136 import static java.util.Objects.requireNonNull;
137 import static org.apache.commons.collections.CollectionUtils.isEmpty;
138 import static org.apache.commons.collections.CollectionUtils.isNotEmpty;
139 import static org.janusgraph.core.attribute.Text.REGEX;
140 import static org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum.TOPOLOGY_TEMPLATE;
141
142 @org.springframework.stereotype.Component("tosca-operation-facade")
143 public class ToscaOperationFacade {
144
145     // region - ToscaElement - GetById
146     public static final String PROXY_SUFFIX = "_proxy";
147     // region - Fields
148     private static final String COULDNT_FETCH_A_COMPONENT_WITH_AND_UNIQUE_ID_ERROR = "Couldn't fetch a component with and UniqueId {}, error: {}";
149     private static final String FAILED_TO_FIND_RECENTLY_ADDED_PROPERTY_ON_THE_RESOURCE_STATUS_IS = "Failed to find recently added property {} on the resource {}. Status is {}. ";
150     private static final String FAILED_TO_GET_UPDATED_RESOURCE_STATUS_IS = "Failed to get updated resource {}. Status is {}. ";
151     private static final String FAILED_TO_ADD_THE_PROPERTY_TO_THE_RESOURCE_STATUS_IS = "Failed to add the property {} to the resource {}. Status is {}. ";
152     private static final String SERVICE = "service";
153     private static final String VF = "VF";
154     private static final String NOT_SUPPORTED_COMPONENT_TYPE = "Not supported component type {}";
155     private static final String COMPONENT_CREATED_SUCCESSFULLY = "Component created successfully!!!";
156     private static final String INPUTS_ASSOCIATED_TO_COMPONENT_SUCCESSFULLY = "Inputs associated to component successfully!";
157     private static final String OUTPUTS_ASSOCIATED_TO_COMPONENT_SUCCESSFULLY = "Outputs associated to component successfully!";
158     private static final String COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR = "Couldn't fetch component with and unique id {}, error: {}";
159     private static final Logger log = Logger.getLogger(ToscaOperationFacade.class.getName());
160     @Autowired
161     private IGraphLockOperation graphLockOperation;
162     @Autowired
163     private NodeTypeOperation nodeTypeOperation;
164     @Autowired
165     private TopologyTemplateOperation topologyTemplateOperation;
166     @Autowired
167     private NodeTemplateOperation nodeTemplateOperation;
168     @Autowired
169     private GroupsOperation groupsOperation;
170     @Autowired
171     private HealingJanusGraphDao janusGraphDao;
172     // endregion
173     @Autowired
174     private ContainerInstanceTypesData containerInstanceTypesData;
175
176     private static Optional<CapabilityDefinition> getPropertyCapability(String propertyParentUniqueId, Component containerComponent) {
177         Map<String, List<CapabilityDefinition>> componentCapabilities = containerComponent.getCapabilities();
178         if (MapUtils.isEmpty(componentCapabilities)) {
179             return Optional.empty();
180         }
181         List<CapabilityDefinition> capabilityDefinitionList = componentCapabilities.values().stream().flatMap(Collection::stream)
182             .collect(Collectors.toList());
183         if (CollectionUtils.isEmpty(capabilityDefinitionList)) {
184             return Optional.empty();
185         }
186         return capabilityDefinitionList.stream().filter(capabilityDefinition -> capabilityDefinition.getUniqueId().equals(propertyParentUniqueId))
187             .findAny();
188     }
189
190     public <T extends Component> Either<T, StorageOperationStatus> getToscaFullElement(String componentId) {
191         ComponentParametersView filters = new ComponentParametersView();
192         filters.setIgnoreCapabiltyProperties(false);
193         filters.setIgnoreServicePath(false);
194         return getToscaElement(componentId, filters);
195     }
196
197     public <T extends Component> Either<T, StorageOperationStatus> getToscaElement(String componentId) {
198         return getToscaElement(componentId, JsonParseFlagEnum.ParseAll);
199     }
200
201     public <T extends Component> Either<T, StorageOperationStatus> getToscaElement(String componentId, ComponentParametersView filters) {
202         Either<GraphVertex, JanusGraphOperationStatus> getVertexEither = janusGraphDao.getVertexById(componentId, filters.detectParseFlag());
203         if (getVertexEither.isRight()) {
204             log.debug(COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, componentId, getVertexEither.right().value());
205             return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVertexEither.right().value()));
206         }
207         return getToscaElementByOperation(getVertexEither.left().value(), filters);
208     }
209
210     public <T extends Component> Either<T, StorageOperationStatus> getToscaElement(String componentId, JsonParseFlagEnum parseFlag) {
211         Either<GraphVertex, JanusGraphOperationStatus> getVertexEither = janusGraphDao.getVertexById(componentId, parseFlag);
212         if (getVertexEither.isRight()) {
213             log.debug(COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, componentId, getVertexEither.right().value());
214             return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVertexEither.right().value()));
215         }
216         return getToscaElementByOperation(getVertexEither.left().value());
217     }
218
219     public <T extends Component> Either<T, StorageOperationStatus> getToscaElement(GraphVertex componentVertex) {
220         return getToscaElementByOperation(componentVertex);
221     }
222
223     public Either<Boolean, StorageOperationStatus> validateComponentExists(String componentId) {
224         Either<GraphVertex, JanusGraphOperationStatus> getVertexEither = janusGraphDao.getVertexById(componentId, JsonParseFlagEnum.NoParse);
225         if (getVertexEither.isRight()) {
226             JanusGraphOperationStatus status = getVertexEither.right().value();
227             if (status == JanusGraphOperationStatus.NOT_FOUND) {
228                 return Either.left(false);
229             } else {
230                 log.debug(COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, componentId, getVertexEither.right().value());
231                 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVertexEither.right().value()));
232             }
233         }
234         return Either.left(true);
235     }
236     // endregion
237
238     public <T extends Component> Either<T, StorageOperationStatus> findLastCertifiedToscaElementByUUID(T component) {
239         Map<GraphPropertyEnum, Object> props = new EnumMap<>(GraphPropertyEnum.class);
240         props.put(GraphPropertyEnum.UUID, component.getUUID());
241         props.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
242         props.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
243         Either<List<GraphVertex>, JanusGraphOperationStatus> getVertexEither = janusGraphDao
244             .getByCriteria(ModelConverter.getVertexType(component), props);
245         if (getVertexEither.isRight()) {
246             log.debug(COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, component.getUniqueId(), getVertexEither.right().value());
247             return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVertexEither.right().value()));
248         }
249         return getToscaElementByOperation(getVertexEither.left().value().get(0));
250     }
251
252     // region - ToscaElement - GetByOperation
253     private <T extends Component> Either<T, StorageOperationStatus> getToscaElementByOperation(GraphVertex componentV) {
254         return getToscaElementByOperation(componentV, new ComponentParametersView());
255     }
256
257     private <T extends Component> Either<T, StorageOperationStatus> getToscaElementByOperation(GraphVertex componentV,
258                                                                                                ComponentParametersView filters) {
259         if (componentV == null) {
260             log.debug("Unexpected null value for `componentV`");
261             return Either.right(StorageOperationStatus.GENERAL_ERROR);
262         } else {
263             VertexTypeEnum label = componentV.getLabel();
264             ToscaElementOperation toscaOperation = getToscaElementOperation(componentV);
265             if (toscaOperation != null) {
266                 log.debug("getToscaElementByOperation: toscaOperation={}", toscaOperation.getClass());
267             }
268             Either<ToscaElement, StorageOperationStatus> toscaElement;
269             String componentId = componentV.getUniqueId();
270             if (toscaOperation != null) {
271                 log.debug("Need to fetch tosca element for id {}", componentId);
272                 toscaElement = toscaOperation.getToscaElement(componentV, filters);
273             } else {
274                 log.debug("not supported tosca type {} for id {}", label, componentId);
275                 toscaElement = Either.right(StorageOperationStatus.BAD_REQUEST);
276             }
277             return toscaElement.left().map(ModelConverter::convertFromToscaElement);
278         }
279     }
280
281     // endregion
282     private ToscaElementOperation getToscaElementOperation(GraphVertex componentV) {
283         VertexTypeEnum label = componentV.getLabel();
284         switch (label) {
285             case NODE_TYPE:
286                 return nodeTypeOperation;
287             case TOPOLOGY_TEMPLATE:
288                 return topologyTemplateOperation;
289             default:
290                 return null;
291         }
292     }
293
294     public <T extends Component> Either<T, StorageOperationStatus> createToscaComponent(T resource) {
295         ToscaElement toscaElement = ModelConverter.convertToToscaElement(resource);
296         ToscaElementOperation toscaElementOperation = getToscaElementOperation(resource);
297         Either<ToscaElement, StorageOperationStatus> createToscaElement = toscaElementOperation.createToscaElement(toscaElement);
298         if (createToscaElement.isLeft()) {
299             log.debug(COMPONENT_CREATED_SUCCESSFULLY);
300             T dataModel = ModelConverter.convertFromToscaElement(createToscaElement.left().value());
301             return Either.left(dataModel);
302         }
303         return Either.right(createToscaElement.right().value());
304     }
305
306     // region - ToscaElement Delete
307     public StorageOperationStatus markComponentToDelete(Component componentToDelete) {
308         if (Boolean.TRUE.equals(componentToDelete.getIsDeleted()) && Boolean.FALSE.equals(componentToDelete.isHighestVersion())) {
309             // component already marked for delete
310             return StorageOperationStatus.OK;
311         } else {
312             Either<GraphVertex, JanusGraphOperationStatus> getResponse = janusGraphDao
313                 .getVertexById(componentToDelete.getUniqueId(), JsonParseFlagEnum.ParseAll);
314             if (getResponse.isRight()) {
315                 log.debug(COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, componentToDelete.getUniqueId(), getResponse.right().value());
316                 return DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getResponse.right().value());
317             }
318             GraphVertex componentV = getResponse.left().value();
319             // same operation for node type and topology template operations
320             Either<GraphVertex, StorageOperationStatus> result = nodeTypeOperation.markComponentToDelete(componentV);
321             if (result.isRight()) {
322                 return result.right().value();
323             }
324             return StorageOperationStatus.OK;
325         }
326     }
327
328     public <T extends Component> Either<T, StorageOperationStatus> deleteToscaComponent(String componentId) {
329         Either<GraphVertex, JanusGraphOperationStatus> getVertexEither = janusGraphDao.getVertexById(componentId, JsonParseFlagEnum.ParseAll);
330         if (getVertexEither.isRight()) {
331             log.debug("Couldn't fetch component vertex with and unique id {}, error: {}", componentId, getVertexEither.right().value());
332             return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVertexEither.right().value()));
333         }
334         Either<ToscaElement, StorageOperationStatus> deleteElement = deleteToscaElement(getVertexEither.left().value());
335         if (deleteElement.isRight()) {
336             log.debug("Failed to delete component with and unique id {}, error: {}", componentId, deleteElement.right().value());
337             return Either.right(deleteElement.right().value());
338         }
339         T dataModel = ModelConverter.convertFromToscaElement(deleteElement.left().value());
340         return Either.left(dataModel);
341     }
342
343     private Either<ToscaElement, StorageOperationStatus> deleteToscaElement(GraphVertex componentV) {
344         VertexTypeEnum label = componentV.getLabel();
345         Either<ToscaElement, StorageOperationStatus> toscaElement;
346         Object componentId = componentV.getUniqueId();
347         switch (label) {
348             case NODE_TYPE:
349                 log.debug("Need to fetch node type for id {}", componentId);
350                 toscaElement = nodeTypeOperation.deleteToscaElement(componentV);
351                 break;
352             case TOPOLOGY_TEMPLATE:
353                 log.debug("Need to fetch topology template for id {}", componentId);
354                 toscaElement = topologyTemplateOperation.deleteToscaElement(componentV);
355                 break;
356             default:
357                 log.debug("not supported tosca type {} for id {}", label, componentId);
358                 toscaElement = Either.right(StorageOperationStatus.BAD_REQUEST);
359                 break;
360         }
361         return toscaElement;
362     }
363
364     // endregion
365     private ToscaElementOperation getToscaElementOperation(Component component) {
366         return ModelConverter.isAtomicComponent(component) ? nodeTypeOperation : topologyTemplateOperation;
367     }
368
369     public <T extends Component> Either<T, StorageOperationStatus> getLatestByToscaResourceNameAndModel(final String toscaResourceName,
370                                                                                                         final String model) {
371         return getLatestByNameAndModel(toscaResourceName, JsonParseFlagEnum.ParseMetadata, new ComponentParametersView(), model);
372     }
373
374     private <T extends Component> Either<T, StorageOperationStatus> getLatestByNameAndModel(final String nodeName,
375                                                                                             final JsonParseFlagEnum parseFlag,
376                                                                                             final ComponentParametersView filter,
377                                                                                             final String model) {
378         Either<T, StorageOperationStatus> result;
379         final Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
380         final Map<GraphPropertyEnum, Object> propertiesNotToMatch = new EnumMap<>(GraphPropertyEnum.class);
381         propertiesToMatch.put(GraphPropertyEnum.TOSCA_RESOURCE_NAME, nodeName);
382         propertiesToMatch.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
383         propertiesNotToMatch.put(GraphPropertyEnum.IS_DELETED, true);
384         final Either<List<GraphVertex>, JanusGraphOperationStatus> highestResources = janusGraphDao
385             .getByCriteria(null, propertiesToMatch, propertiesNotToMatch, parseFlag, model);
386         if (highestResources.isRight()) {
387             final JanusGraphOperationStatus status = highestResources.right().value();
388             log.debug("failed to find resource with name {}. status={} ", nodeName, status);
389             result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
390             return result;
391         }
392         final List<GraphVertex> resources = highestResources.left().value();
393         double version = 0.0;
394         GraphVertex highestResource = null;
395         for (final GraphVertex vertex : resources) {
396             final Object versionObj = vertex.getMetadataProperty(GraphPropertyEnum.VERSION);
397             double resourceVersion = Double.parseDouble((String) versionObj);
398             if (resourceVersion > version) {
399                 version = resourceVersion;
400                 highestResource = vertex;
401             }
402         }
403         return getToscaElementByOperation(highestResource, filter);
404     }
405
406     public <T extends Component> Either<T, StorageOperationStatus> getLatestByToscaResourceName(String toscaResourceName, String modelName) {
407         return getLatestByName(GraphPropertyEnum.TOSCA_RESOURCE_NAME, toscaResourceName, modelName);
408     }
409
410     public <T extends Component> Either<T, StorageOperationStatus> getFullLatestComponentByToscaResourceName(String toscaResourceName) {
411         ComponentParametersView fetchAllFilter = new ComponentParametersView();
412         fetchAllFilter.setIgnoreServicePath(true);
413         fetchAllFilter.setIgnoreCapabiltyProperties(false);
414         return getLatestByName(GraphPropertyEnum.TOSCA_RESOURCE_NAME, toscaResourceName, JsonParseFlagEnum.ParseAll, fetchAllFilter, null);
415     }
416
417     public <T extends Component> Either<T, StorageOperationStatus> getLatestByName(String resourceName, String modelName) {
418         return getLatestByName(GraphPropertyEnum.NAME, resourceName, modelName);
419     }
420
421     public StorageOperationStatus validateCsarUuidUniqueness(String csarUUID) {
422         Map<GraphPropertyEnum, Object> properties = new EnumMap<>(GraphPropertyEnum.class);
423         properties.put(GraphPropertyEnum.CSAR_UUID, csarUUID);
424         Either<List<GraphVertex>, JanusGraphOperationStatus> resources = janusGraphDao
425             .getByCriteria(null, properties, JsonParseFlagEnum.ParseMetadata);
426         if (resources.isRight()) {
427             if (resources.right().value() == JanusGraphOperationStatus.NOT_FOUND) {
428                 return StorageOperationStatus.OK;
429             } else {
430                 log.debug("failed to get resources from graph with property name: {}", csarUUID);
431                 return DaoStatusConverter.convertJanusGraphStatusToStorageStatus(resources.right().value());
432             }
433         }
434         return StorageOperationStatus.ENTITY_ALREADY_EXISTS;
435     }
436
437     public <T extends Component> Either<Set<T>, StorageOperationStatus> getFollowed(String userId, Set<LifecycleStateEnum> lifecycleStates,
438                                                                                     Set<LifecycleStateEnum> lastStateStates,
439                                                                                     ComponentTypeEnum componentType) {
440         Either<List<ToscaElement>, StorageOperationStatus> followedResources;
441         if (componentType == ComponentTypeEnum.RESOURCE) {
442             followedResources = nodeTypeOperation.getFollowedComponent(userId, lifecycleStates, lastStateStates, componentType);
443         } else {
444             followedResources = topologyTemplateOperation.getFollowedComponent(userId, lifecycleStates, lastStateStates, componentType);
445         }
446         Set<T> components = new HashSet<>();
447         if (followedResources.isRight() && followedResources.right().value() != StorageOperationStatus.NOT_FOUND) {
448             return Either.right(followedResources.right().value());
449         }
450         if (followedResources.isLeft()) {
451             List<ToscaElement> toscaElements = followedResources.left().value();
452             toscaElements.forEach(te -> {
453                 T component = ModelConverter.convertFromToscaElement(te);
454                 components.add(component);
455             });
456         }
457         return Either.left(components);
458     }
459
460     public Either<Resource, StorageOperationStatus> getLatestCertifiedNodeTypeByToscaResourceName(String toscaResourceName) {
461         return getLatestCertifiedByToscaResourceName(toscaResourceName, VertexTypeEnum.NODE_TYPE, JsonParseFlagEnum.ParseMetadata);
462     }
463
464     public Either<Resource, StorageOperationStatus> getByToscaResourceNameMatchingVendorRelease(final String toscaResourceName,
465                                                                                                 final String vendorVersion) {
466         return getByToscaResourceNameMatchingVendorRelease(toscaResourceName, VertexTypeEnum.NODE_TYPE, JsonParseFlagEnum.ParseMetadata,
467             vendorVersion);
468     }
469
470     public Either<Resource, StorageOperationStatus> getByToscaResourceNameMatchingVendorRelease(String toscaResourceName, VertexTypeEnum vertexType,
471                                                                                                 JsonParseFlagEnum parseFlag, String vendorRelease) {
472         Map<GraphPropertyEnum, Object> props = new EnumMap<>(GraphPropertyEnum.class);
473         props.put(GraphPropertyEnum.TOSCA_RESOURCE_NAME, toscaResourceName);
474         props.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
475         Map<String, Entry<JanusGraphPredicate, Object>> predicateCriteria = getVendorVersionPredicate(vendorRelease);
476         Either<List<GraphVertex>, JanusGraphOperationStatus> getLatestRes = janusGraphDao
477             .getByCriteria(vertexType, props, null, predicateCriteria, parseFlag, null);
478         if (getLatestRes.isRight() || CollectionUtils.isEmpty(getLatestRes.left().value())) {
479             getLatestRes = janusGraphDao.getByCriteria(vertexType, props, parseFlag);
480         }
481         return getLatestRes.right().map(status -> {
482             CommonUtility
483                 .addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to fetch {} with name {}. status={} ", vertexType, toscaResourceName, status);
484             return DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status);
485         }).left().bind(resources -> {
486             double version = 0.0;
487             GraphVertex highestResource = null;
488             for (GraphVertex resource : resources) {
489                 double resourceVersion = Double.parseDouble((String) resource.getJsonMetadataField(JsonPresentationFields.VERSION));
490                 if (resourceVersion > version && isValidForVendorRelease(resource, vendorRelease)) {
491                     version = resourceVersion;
492                     highestResource = resource;
493                 }
494             }
495             if (highestResource != null) {
496                 return getToscaFullElement(highestResource.getUniqueId());
497             } else {
498                 log.debug("The vertex with the highest version could not be found for {}", toscaResourceName);
499                 return Either.right(StorageOperationStatus.GENERAL_ERROR);
500             }
501         });
502     }
503
504     public <T extends Component> Either<T, StorageOperationStatus> getByToscaResourceNameAndVersion(final String toscaResourceName,
505                                                                                                     final String version, final String model) {
506         Either<T, StorageOperationStatus> result;
507
508         Map<GraphPropertyEnum, Object> hasProperties = new EnumMap<>(GraphPropertyEnum.class);
509         Map<GraphPropertyEnum, Object> hasNotProperties = new EnumMap<>(GraphPropertyEnum.class);
510
511         hasProperties.put(GraphPropertyEnum.TOSCA_RESOURCE_NAME, toscaResourceName);
512         hasProperties.put(GraphPropertyEnum.VERSION, version);
513         hasNotProperties.put(GraphPropertyEnum.IS_DELETED, true);
514
515         Either<List<GraphVertex>, JanusGraphOperationStatus> getResourceRes = janusGraphDao
516             .getByCriteria(VertexTypeEnum.NODE_TYPE, hasProperties, hasNotProperties, JsonParseFlagEnum.ParseAll, model);
517         if (getResourceRes.isRight()) {
518             JanusGraphOperationStatus status = getResourceRes.right().value();
519             log.debug("failed to find resource with toscaResourceName {}, version {}. Status is {} ", toscaResourceName, version, status);
520             result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
521             return result;
522         }
523         return getToscaElementByOperation(getResourceRes.left().value().get(0));
524     }
525
526     private Map<String, Entry<JanusGraphPredicate, Object>> getVendorVersionPredicate(final String vendorRelease) {
527         Map<String, Entry<JanusGraphPredicate, Object>> predicateCriteria = new HashMap<>();
528         if (!"1.0".equals(vendorRelease)) {
529             String[] vendorReleaseElements = vendorRelease.split("\\.");
530             if (vendorReleaseElements.length > 0) {
531                 String regex = ".*\"vendorRelease\":\"";
532                 for (int i = 0; i < vendorReleaseElements.length; i++) {
533                     regex += vendorReleaseElements[i];
534                     regex += i < vendorReleaseElements.length - 1 ? "\\." : "\".*";
535                 }
536                 predicateCriteria.put("metadata", new HashMap.SimpleEntry<>(REGEX, regex));
537             }
538         }
539         return predicateCriteria;
540     }
541
542     public boolean isNodeAssociatedToModel(final String model, final Resource resource) {
543         final List<GraphVertex> modelElementVertices = getResourceModelElementVertices(resource);
544         if (model == null) {
545             return modelElementVertices.isEmpty();
546         }
547         return modelElementVertices.stream().anyMatch(graphVertex -> graphVertex.getMetadataProperty(GraphPropertyEnum.NAME).equals(model));
548     }
549
550     public List<GraphVertex> getResourceModelElementVertices(final Resource resource) {
551         final Either<GraphVertex, JanusGraphOperationStatus> vertex =
552             janusGraphDao.getVertexById(resource.getUniqueId(), JsonParseFlagEnum.NoParse);
553         if (vertex.isRight() || Objects.isNull(vertex.left().value())) {
554             return Collections.emptyList();
555         }
556         final Either<List<GraphVertex>, JanusGraphOperationStatus> nodeModelVertices =
557             janusGraphDao.getParentVertices(vertex.left().value(), EdgeLabelEnum.MODEL_ELEMENT, JsonParseFlagEnum.NoParse);
558         if (nodeModelVertices.isRight() || nodeModelVertices.left().value() == null) {
559             return Collections.emptyList();
560         }
561         return nodeModelVertices.left().value();
562     }
563
564     private boolean isValidForVendorRelease(final GraphVertex resource, final String vendorRelease) {
565         if (!vendorRelease.equals("1.0")) {
566             try {
567                 Semver resourceSemVer = new Semver((String) resource.getJsonMetadataField(JsonPresentationFields.VENDOR_RELEASE), SemverType.NPM);
568                 Semver packageSemVer = new Semver(vendorRelease, SemverType.NPM);
569                 return !resourceSemVer.isGreaterThan(packageSemVer);
570             } catch (Exception exception) {
571                 log.debug("Error in comparing vendor release", exception);
572                 return true;
573             }
574         }
575         return true;
576     }
577
578     public Either<Resource, StorageOperationStatus> getLatestCertifiedByToscaResourceName(String toscaResourceName, VertexTypeEnum vertexType,
579                                                                                           JsonParseFlagEnum parseFlag) {
580         Map<GraphPropertyEnum, Object> props = new EnumMap<>(GraphPropertyEnum.class);
581         props.put(GraphPropertyEnum.TOSCA_RESOURCE_NAME, toscaResourceName);
582         props.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
583         props.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
584         Either<List<GraphVertex>, JanusGraphOperationStatus> getLatestRes = janusGraphDao.getByCriteria(vertexType, props, parseFlag);
585         return getLatestRes.right().map(status -> {
586             CommonUtility
587                 .addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to fetch {} with name {}. status={} ", vertexType, toscaResourceName, status);
588             return DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status);
589         }).left().bind(resources -> {
590             double version = 0.0;
591             GraphVertex highestResource = null;
592             for (GraphVertex resource : resources) {
593                 double resourceVersion = Double.parseDouble((String) resource.getJsonMetadataField(JsonPresentationFields.VERSION));
594                 if (resourceVersion > version) {
595                     version = resourceVersion;
596                     highestResource = resource;
597                 }
598             }
599             if (highestResource != null) {
600                 return getToscaFullElement(highestResource.getUniqueId());
601             } else {
602                 log.debug("The vertex with the highest version could not be found for {}", toscaResourceName);
603                 return Either.right(StorageOperationStatus.GENERAL_ERROR);
604             }
605         });
606     }
607
608     public Either<Resource, StorageOperationStatus> getLatestResourceByToscaResourceName(String toscaResourceName) {
609         if (toscaResourceName != null && toscaResourceName.contains("org.openecomp.resource.vf")) {
610             return getLatestResourceByToscaResourceName(toscaResourceName, TOPOLOGY_TEMPLATE, JsonParseFlagEnum.ParseMetadata);
611         } else {
612             return getLatestResourceByToscaResourceName(toscaResourceName, VertexTypeEnum.NODE_TYPE, JsonParseFlagEnum.ParseMetadata);
613         }
614     }
615
616     public Either<Resource, StorageOperationStatus> getLatestResourceByToscaResourceName(String toscaResourceName, VertexTypeEnum vertexType,
617                                                                                          JsonParseFlagEnum parseFlag) {
618         Either<Resource, StorageOperationStatus> result = null;
619         Map<GraphPropertyEnum, Object> props = new EnumMap<>(GraphPropertyEnum.class);
620         props.put(GraphPropertyEnum.TOSCA_RESOURCE_NAME, toscaResourceName);
621         props.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
622         if (!toscaResourceName.contains("org.openecomp.resource.vf")) {
623             props.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
624         }
625         Either<List<GraphVertex>, JanusGraphOperationStatus> getLatestRes = janusGraphDao.getByCriteria(vertexType, props, parseFlag);
626         if (getLatestRes.isRight()) {
627             JanusGraphOperationStatus status = getLatestRes.right().value();
628             CommonUtility
629                 .addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to fetch {} with name {}. status={} ", vertexType, toscaResourceName, status);
630             result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
631         }
632         if (result == null) {
633             List<GraphVertex> resources = getLatestRes.left().value();
634             double version = 0.0;
635             GraphVertex highestResource = null;
636             for (GraphVertex resource : resources) {
637                 double resourceVersion = Double.parseDouble((String) resource.getJsonMetadataField(JsonPresentationFields.VERSION));
638                 if (resourceVersion > version) {
639                     version = resourceVersion;
640                     highestResource = resource;
641                 }
642             }
643             if (highestResource != null) {
644                 result = getToscaFullElement(highestResource.getUniqueId());
645             } else {
646                 log.debug("The vertex with the highest version could not be found for {}", toscaResourceName);
647                 result = Either.right(StorageOperationStatus.GENERAL_ERROR);
648             }
649         }
650         return result;
651     }
652
653     public Either<Boolean, StorageOperationStatus> validateToscaResourceNameExists(String templateName) {
654         Either<Boolean, StorageOperationStatus> validateUniquenessRes = validateToscaResourceNameUniqueness(templateName);
655         if (validateUniquenessRes.isLeft()) {
656             return Either.left(!validateUniquenessRes.left().value());
657         }
658         return validateUniquenessRes;
659     }
660
661     public Either<RequirementCapabilityRelDef, StorageOperationStatus> dissociateResourceInstances(String componentId,
662                                                                                                    RequirementCapabilityRelDef requirementDef) {
663         return nodeTemplateOperation.dissociateResourceInstances(componentId, requirementDef);
664     }
665
666     /**
667      * Allows to get fulfilled requirement by relation and received predicate
668      */
669     public Either<RequirementDataDefinition, StorageOperationStatus> getFulfilledRequirementByRelation(String componentId, String instanceId,
670                                                                                                        RequirementCapabilityRelDef relation,
671                                                                                                        BiPredicate<RelationshipInfo, RequirementDataDefinition> predicate) {
672         return nodeTemplateOperation.getFulfilledRequirementByRelation(componentId, instanceId, relation, predicate);
673     }
674
675     /**
676      * Allows to get fulfilled capability by relation and received predicate
677      */
678     public Either<CapabilityDataDefinition, StorageOperationStatus> getFulfilledCapabilityByRelation(String componentId, String instanceId,
679                                                                                                      RequirementCapabilityRelDef relation,
680                                                                                                      BiPredicate<RelationshipInfo, CapabilityDataDefinition> predicate) {
681         return nodeTemplateOperation.getFulfilledCapabilityByRelation(componentId, instanceId, relation, predicate);
682     }
683
684     public Either<List<RequirementCapabilityRelDef>, StorageOperationStatus> associateResourceInstances(Component component, String componentId,
685                                                                                                         List<RequirementCapabilityRelDef> relations) {
686         Either<List<RequirementCapabilityRelDef>, StorageOperationStatus> reqAndCapListEither = nodeTemplateOperation
687             .associateResourceInstances(componentId, relations);
688         if (component != null) {
689             updateInstancesCapAndReqOnComponentFromDB(component);
690         }
691         return reqAndCapListEither;
692     }
693
694     protected Either<Boolean, StorageOperationStatus> validateToscaResourceNameUniqueness(String name) {
695         Map<GraphPropertyEnum, Object> properties = new EnumMap<>(GraphPropertyEnum.class);
696         properties.put(GraphPropertyEnum.TOSCA_RESOURCE_NAME, name);
697         Either<List<GraphVertex>, JanusGraphOperationStatus> resources = janusGraphDao
698             .getByCriteria(null, properties, JsonParseFlagEnum.ParseMetadata);
699         if (resources.isRight() && resources.right().value() != JanusGraphOperationStatus.NOT_FOUND) {
700             log.debug("failed to get resources from graph with property name: {}", name);
701             return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(resources.right().value()));
702         }
703         List<GraphVertex> resourceList = (resources.isLeft() ? resources.left().value() : null);
704         if (isNotEmpty(resourceList)) {
705             if (log.isDebugEnabled()) {
706                 StringBuilder builder = new StringBuilder();
707                 for (GraphVertex resourceData : resourceList) {
708                     builder.append(resourceData.getUniqueId() + "|");
709                 }
710                 log.debug("resources  with property name:{} exists in graph. found {}", name, builder);
711             }
712             return Either.left(false);
713         } else {
714             log.debug("resources  with property name:{} does not exists in graph", name);
715             return Either.left(true);
716         }
717     }
718
719     // region - Component Update
720     public Either<Resource, StorageOperationStatus> overrideComponent(Resource newComponent, Resource oldComponent) {
721         copyArtifactsToNewComponent(newComponent, oldComponent);
722         Either<GraphVertex, JanusGraphOperationStatus> componentVEither = janusGraphDao
723             .getVertexById(oldComponent.getUniqueId(), JsonParseFlagEnum.NoParse);
724         if (componentVEither.isRight()) {
725             log.debug("Failed to fetch component {} error {}", oldComponent.getUniqueId(), componentVEither.right().value());
726             return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(componentVEither.right().value()));
727         }
728         GraphVertex componentv = componentVEither.left().value();
729         Either<GraphVertex, JanusGraphOperationStatus> parentVertexEither = janusGraphDao
730             .getParentVertex(componentv, EdgeLabelEnum.VERSION, JsonParseFlagEnum.NoParse);
731         if (parentVertexEither.isRight() && parentVertexEither.right().value() != JanusGraphOperationStatus.NOT_FOUND) {
732             log.debug("Failed to fetch parent version for component {} error {}", oldComponent.getUniqueId(), parentVertexEither.right().value());
733             return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(parentVertexEither.right().value()));
734         }
735         Either<ToscaElement, StorageOperationStatus> deleteToscaComponent = deleteToscaElement(componentv);
736         if (deleteToscaComponent.isRight()) {
737             log.debug("Failed to remove old component {} error {}", oldComponent.getUniqueId(), deleteToscaComponent.right().value());
738             return Either.right(deleteToscaComponent.right().value());
739         }
740         Either<Resource, StorageOperationStatus> createToscaComponent = createToscaComponent(newComponent);
741         if (createToscaComponent.isRight()) {
742             log.debug("Failed to create tosca element component {} error {}", newComponent.getUniqueId(), createToscaComponent.right().value());
743             return Either.right(createToscaComponent.right().value());
744         }
745         Resource newElement = createToscaComponent.left().value();
746         Either<GraphVertex, JanusGraphOperationStatus> newVersionEither = janusGraphDao
747             .getVertexById(newElement.getUniqueId(), JsonParseFlagEnum.NoParse);
748         if (newVersionEither.isRight()) {
749             log.debug("Failed to fetch new tosca element component {} error {}", newComponent.getUniqueId(), newVersionEither.right().value());
750             return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(newVersionEither.right().value()));
751         }
752         if (parentVertexEither.isLeft()) {
753             GraphVertex previousVersionV = parentVertexEither.left().value();
754             JanusGraphOperationStatus createEdge = janusGraphDao
755                 .createEdge(previousVersionV, newVersionEither.left().value(), EdgeLabelEnum.VERSION, null);
756             if (createEdge != JanusGraphOperationStatus.OK) {
757                 log.debug("Failed to associate to previous version {} new version {} error {}", previousVersionV.getUniqueId(),
758                     newVersionEither.right().value(), createEdge);
759                 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(createEdge));
760             }
761         }
762         return Either.left(newElement);
763     }
764
765     void copyArtifactsToNewComponent(Resource newComponent, Resource oldComponent) {
766         // TODO - check if required
767         Map<String, ArtifactDefinition> toscaArtifacts = oldComponent.getToscaArtifacts();
768         if (toscaArtifacts != null && !toscaArtifacts.isEmpty()) {
769             toscaArtifacts.values().stream().forEach(a -> a.setDuplicated(Boolean.TRUE));
770         }
771         newComponent.setToscaArtifacts(toscaArtifacts);
772         Map<String, ArtifactDefinition> artifacts = oldComponent.getArtifacts();
773         if (artifacts != null && !artifacts.isEmpty()) {
774             artifacts.values().stream().forEach(a -> a.setDuplicated(Boolean.TRUE));
775         }
776         newComponent.setArtifacts(artifacts);
777         Map<String, ArtifactDefinition> depArtifacts = oldComponent.getDeploymentArtifacts();
778         if (depArtifacts != null && !depArtifacts.isEmpty()) {
779             depArtifacts.values().stream().forEach(a -> a.setDuplicated(Boolean.TRUE));
780         }
781         newComponent.setDeploymentArtifacts(depArtifacts);
782         newComponent.setLastUpdateDate(null);
783         newComponent.setHighestVersion(true);
784     }
785
786     public <T extends Component> Either<T, StorageOperationStatus> updateToscaElement(T componentToUpdate) {
787         return updateToscaElement(componentToUpdate, new ComponentParametersView());
788     }
789
790     private <T extends Component> Either<T, StorageOperationStatus> updateToscaElement(T componentToUpdate, ComponentParametersView filterResult) {
791         String componentId = componentToUpdate.getUniqueId();
792         Either<GraphVertex, JanusGraphOperationStatus> getVertexEither = janusGraphDao.getVertexById(componentId, JsonParseFlagEnum.ParseAll);
793         if (getVertexEither.isRight()) {
794             log.debug(COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, componentId, getVertexEither.right().value());
795             return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVertexEither.right().value()));
796         }
797         GraphVertex elementV = getVertexEither.left().value();
798         ToscaElementOperation toscaElementOperation = getToscaElementOperation(elementV);
799         ToscaElement toscaElementToUpdate = ModelConverter.convertToToscaElement(componentToUpdate);
800         Either<ToscaElement, StorageOperationStatus> updateToscaElement = null;
801         if (toscaElementOperation != null) {
802             updateToscaElement = toscaElementOperation.updateToscaElement(toscaElementToUpdate, elementV, filterResult);
803         } else {
804             log.debug("Null value returned by `getToscaElementOperation` with value {}", elementV);
805             updateToscaElement = Either.right(StorageOperationStatus.GENERAL_ERROR);
806         }
807         return updateToscaElement.bimap(ModelConverter::convertFromToscaElement, status -> {
808             log.debug("Failed to update tosca element {} error {}", componentId, status);
809             return status;
810         });
811     }
812
813     private <T extends Component> Either<T, StorageOperationStatus> getLatestByName(GraphPropertyEnum property, String nodeName,
814                                                                                     JsonParseFlagEnum parseFlag, String modelName) {
815         return getLatestByName(property, nodeName, parseFlag, new ComponentParametersView(), modelName);
816     }
817
818     private <T extends Component> Either<T, StorageOperationStatus> getLatestByName(GraphPropertyEnum property, String nodeName,
819                                                                                     JsonParseFlagEnum parseFlag, ComponentParametersView filter,
820                                                                                     String model) {
821         Either<T, StorageOperationStatus> result;
822         Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
823         Map<GraphPropertyEnum, Object> propertiesNotToMatch = new EnumMap<>(GraphPropertyEnum.class);
824         propertiesToMatch.put(property, nodeName);
825         propertiesToMatch.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
826         propertiesNotToMatch.put(GraphPropertyEnum.IS_DELETED, true);
827         Either<List<GraphVertex>, JanusGraphOperationStatus> highestResources = janusGraphDao
828             .getByCriteria(null, propertiesToMatch, propertiesNotToMatch, parseFlag, model);
829         if (highestResources.isRight()) {
830             JanusGraphOperationStatus status = highestResources.right().value();
831             log.debug("failed to find resource with name {}. status={} ", nodeName, status);
832             result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
833             return result;
834         }
835         List<GraphVertex> resources = highestResources.left().value();
836         double version = 0.0;
837         GraphVertex highestResource = null;
838         for (GraphVertex vertex : resources) {
839             Object versionObj = vertex.getMetadataProperty(GraphPropertyEnum.VERSION);
840             double resourceVersion = Double.parseDouble((String) versionObj);
841             if (resourceVersion > version) {
842                 version = resourceVersion;
843                 highestResource = vertex;
844             }
845         }
846         return getToscaElementByOperation(highestResource, filter);
847     }
848
849     // region - Component Get By ..
850     private <T extends Component> Either<T, StorageOperationStatus> getLatestByName(GraphPropertyEnum property, String nodeName, String modelName) {
851         return getLatestByName(property, nodeName, JsonParseFlagEnum.ParseMetadata, modelName);
852     }
853
854     public <T extends Component> Either<T, StorageOperationStatus> getBySystemNameAndVersion(final ComponentTypeEnum componentType,
855                                                                                              final String systemName,
856                                                                                              final String version) {
857         final Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
858         final Map<GraphPropertyEnum, Object> propertiesNotToMatch = new EnumMap<>(GraphPropertyEnum.class);
859         propertiesToMatch.put(GraphPropertyEnum.SYSTEM_NAME, systemName);
860         propertiesToMatch.put(GraphPropertyEnum.VERSION, version);
861         if (componentType != null) {
862             propertiesToMatch.put(GraphPropertyEnum.COMPONENT_TYPE, componentType.name());
863         }
864         propertiesNotToMatch.put(GraphPropertyEnum.IS_DELETED, true);
865
866         final Either<List<GraphVertex>, JanusGraphOperationStatus> getResourceResult
867             = janusGraphDao.getByCriteria(null, propertiesToMatch, propertiesNotToMatch, JsonParseFlagEnum.ParseAll);
868         if (getResourceResult.isRight()) {
869             final JanusGraphOperationStatus status = getResourceResult.right().value();
870             log.debug("Failed to find resource with systemName {}, version {}. Status is {} ", systemName, version, status);
871             return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
872         }
873         return getToscaElementByOperation(getResourceResult.left().value().get(0));
874     }
875
876     public <T extends Component> Either<T, StorageOperationStatus> getComponentByNameAndVersion(ComponentTypeEnum componentType, String name,
877                                                                                                 String version) {
878         return getComponentByNameAndVersion(componentType, name, version, JsonParseFlagEnum.ParseAll);
879     }
880
881     public <T extends Component> Either<T, StorageOperationStatus> getComponentByNameAndVersion(ComponentTypeEnum componentType, String name,
882                                                                                                 String version, JsonParseFlagEnum parseFlag) {
883         Map<GraphPropertyEnum, Object> hasProperties = new EnumMap<>(GraphPropertyEnum.class);
884         Map<GraphPropertyEnum, Object> hasNotProperties = new EnumMap<>(GraphPropertyEnum.class);
885         hasProperties.put(GraphPropertyEnum.NAME, name);
886         hasProperties.put(GraphPropertyEnum.VERSION, version);
887         hasNotProperties.put(GraphPropertyEnum.IS_DELETED, true);
888         if (componentType != null) {
889             hasProperties.put(GraphPropertyEnum.COMPONENT_TYPE, componentType.name());
890         }
891         Either<List<GraphVertex>, JanusGraphOperationStatus> getResourceRes = janusGraphDao
892             .getByCriteria(null, hasProperties, hasNotProperties, parseFlag);
893         if (getResourceRes.isRight()) {
894             JanusGraphOperationStatus status = getResourceRes.right().value();
895             log.debug("failed to find resource with name {}, version {}. Status is {} ", name, version, status);
896             return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
897         }
898         return getToscaElementByOperation(getResourceRes.left().value().get(0));
899     }
900
901     public <T extends Component> Either<T, StorageOperationStatus> getComponentByNameAndVendorRelease(final ComponentTypeEnum componentType,
902                                                                                                       final String name, final String vendorRelease,
903                                                                                                       final JsonParseFlagEnum parseFlag,
904                                                                                                       final String modelName) {
905         Map<GraphPropertyEnum, Object> hasProperties = new EnumMap<>(GraphPropertyEnum.class);
906         Map<GraphPropertyEnum, Object> hasNotProperties = new EnumMap<>(GraphPropertyEnum.class);
907         hasProperties.put(GraphPropertyEnum.NAME, name);
908         hasNotProperties.put(GraphPropertyEnum.IS_DELETED, true);
909         if (componentType != null) {
910             hasProperties.put(GraphPropertyEnum.COMPONENT_TYPE, componentType.name());
911         }
912         Map<String, Entry<JanusGraphPredicate, Object>> predicateCriteria = getVendorVersionPredicate(vendorRelease);
913         Either<List<GraphVertex>, JanusGraphOperationStatus> getResourceRes = janusGraphDao.getByCriteria(null, hasProperties, hasNotProperties,
914             predicateCriteria, parseFlag, modelName);
915         if (getResourceRes.isRight()) {
916             JanusGraphOperationStatus status = getResourceRes.right().value();
917             log.debug("failed to find resource with name {}, version {}. Status is {} ", name, predicateCriteria, status);
918             return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
919         }
920         return getToscaElementByOperation(getResourceRes.left().value().get(0));
921     }
922
923     public Either<List<CatalogComponent>, StorageOperationStatus> getCatalogOrArchiveComponents(boolean isCatalog,
924                                                                                                 List<OriginTypeEnum> excludeTypes) {
925         List<ResourceTypeEnum> excludedResourceTypes = Optional.ofNullable(excludeTypes).orElse(Collections.emptyList()).stream()
926             .filter(type -> !type.equals(OriginTypeEnum.SERVICE)).map(type -> ResourceTypeEnum.getTypeByName(type.name()))
927             .collect(Collectors.toList());
928         return topologyTemplateOperation.getElementCatalogData(isCatalog, excludedResourceTypes);
929     }
930
931     // endregion
932     public <T extends Component> Either<List<T>, StorageOperationStatus> getCatalogComponents(ComponentTypeEnum componentType,
933                                                                                               List<OriginTypeEnum> excludeTypes,
934                                                                                               boolean isHighestVersions) {
935         List<T> components = new ArrayList<>();
936         Either<List<ToscaElement>, StorageOperationStatus> catalogDataResult;
937         List<ToscaElement> toscaElements = new ArrayList<>();
938         List<ResourceTypeEnum> excludedResourceTypes = Optional.ofNullable(excludeTypes).orElse(Collections.emptyList()).stream()
939             .filter(type -> !type.equals(OriginTypeEnum.SERVICE)).map(type -> ResourceTypeEnum.getTypeByName(type.name()))
940             .collect(Collectors.toList());
941         switch (componentType) {
942             case RESOURCE:
943                 catalogDataResult = nodeTypeOperation.getElementCatalogData(ComponentTypeEnum.RESOURCE, excludedResourceTypes, isHighestVersions);
944                 if (catalogDataResult.isRight()) {
945                     return Either.right(catalogDataResult.right().value());
946                 }
947                 toscaElements = catalogDataResult.left().value();
948                 break;
949             case SERVICE:
950                 if (excludeTypes != null && excludeTypes.contains(OriginTypeEnum.SERVICE)) {
951                     break;
952                 }
953                 catalogDataResult = topologyTemplateOperation.getElementCatalogData(ComponentTypeEnum.SERVICE, null, isHighestVersions);
954                 if (catalogDataResult.isRight()) {
955                     return Either.right(catalogDataResult.right().value());
956                 }
957                 toscaElements = catalogDataResult.left().value();
958                 break;
959             default:
960                 log.debug(NOT_SUPPORTED_COMPONENT_TYPE, componentType);
961                 return Either.right(StorageOperationStatus.BAD_REQUEST);
962         }
963         toscaElements.forEach(te -> {
964             T component = ModelConverter.convertFromToscaElement(te);
965             components.add(component);
966         });
967         return Either.left(components);
968     }
969
970     public Either<List<String>, StorageOperationStatus> deleteMarkedElements(ComponentTypeEnum componentType) {
971         Either<List<GraphVertex>, StorageOperationStatus> allComponentsMarkedForDeletion;
972         switch (componentType) {
973             case RESOURCE:
974                 allComponentsMarkedForDeletion = nodeTypeOperation.getAllComponentsMarkedForDeletion(componentType);
975                 break;
976             case SERVICE:
977             case PRODUCT:
978                 allComponentsMarkedForDeletion = topologyTemplateOperation.getAllComponentsMarkedForDeletion(componentType);
979                 break;
980             default:
981                 log.debug(NOT_SUPPORTED_COMPONENT_TYPE, componentType);
982                 return Either.right(StorageOperationStatus.BAD_REQUEST);
983         }
984         if (allComponentsMarkedForDeletion.isRight()) {
985             return Either.right(allComponentsMarkedForDeletion.right().value());
986         }
987         List<GraphVertex> allMarked = allComponentsMarkedForDeletion.left().value();
988         return Either.left(checkIfInUseAndDelete(allMarked));
989     }
990
991     public List<String> deleteService(String invariantUUID, final boolean inTransaction) {
992         return deleteComponent(invariantUUID, NodeTypeEnum.Service, inTransaction);
993     }
994
995     public List<String> deleteComponent(String invariantUUID, NodeTypeEnum componentType, final boolean inTransaction) {
996         final List<GraphVertex> allServiceVerticesToDelete = findVertexListByInvariantUuid(invariantUUID);
997         List<String> affectedComponentIds = new ArrayList<>();
998         try {
999             checkNotUsed(allServiceVerticesToDelete);
1000             lockAllVerticesByNodeType(allServiceVerticesToDelete, componentType);
1001             for (GraphVertex elementV : allServiceVerticesToDelete) {
1002                 Either<ToscaElement, StorageOperationStatus> deleteToscaElement = deleteToscaElement(elementV);
1003                 if (deleteToscaElement.isRight()) {
1004                     log.debug("Failed to delete element UniqueID {}, Name {}, error {}", elementV.getUniqueId(),
1005                         elementV.getMetadataProperties().get(GraphPropertyEnum.NAME), deleteToscaElement.right().value());
1006                     throwStorageException(deleteToscaElement.right().value());
1007                 }
1008                 affectedComponentIds.add(elementV.getUniqueId());
1009             }
1010             if (!inTransaction) {
1011                 janusGraphDao.commit();
1012             }
1013         } catch (Exception exception) {
1014             if (!inTransaction) {
1015                 janusGraphDao.rollback();
1016             }
1017             throw exception;
1018         } finally {
1019             unlockAllVerticesByNodeType(allServiceVerticesToDelete, componentType);
1020         }
1021         return affectedComponentIds;
1022     }
1023
1024     private void checkNotUsed(List<GraphVertex> vertices) {
1025         boolean isInUse = isAnyComponentInUse(vertices);
1026         if (isInUse) {
1027             Set<GraphVertex> listOfVertices = getComponentsUsingComponents(vertices);
1028             List<String> listOfStringComponents = new ArrayList<>();
1029             for (GraphVertex componentVertex : listOfVertices) {
1030                 listOfStringComponents.add(
1031                     componentVertex.getMetadataJson().get(GraphPropertyEnum.COMPONENT_TYPE.getProperty()) + " "
1032                         + componentVertex.getMetadataJson().get(GraphPropertyEnum.NAME.getProperty())
1033                 );
1034             }
1035             String stringOfComponents = String.join(", ", listOfStringComponents);
1036             throw ToscaOperationExceptionSupplier.componentInUse(stringOfComponents).get();
1037         }
1038     }
1039
1040     public List<GraphVertex> findVertexListByInvariantUuid(final String invariantUuid) {
1041         try {
1042             return janusGraphDao.findAllVertexByInvariantUuid(invariantUuid, Collections.emptyMap());
1043         } catch (final JanusGraphException e) {
1044             log.error(EcompLoggerErrorCode.DATA_ERROR, this.getClass().getName(), e.getMessage());
1045             throw new StorageException(e.getStatus());
1046         }
1047     }
1048
1049     public void commitAndCheck(String componentId) {
1050         JanusGraphOperationStatus status = janusGraphDao.commit();
1051         if (!status.equals(JanusGraphOperationStatus.OK)) {
1052             log.debug("error occurred when trying to DELETE {}. Return code is: {}", componentId, status);
1053             throwStorageException(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
1054         }
1055     }
1056
1057     private Set<GraphVertex> getComponentsUsingComponents(List<GraphVertex> componentVertices) {
1058         Set<GraphVertex> inUseBy = new TreeSet<>(Comparator.comparing(GraphVertex::getUniqueId));
1059         for (final GraphVertex elementV : componentVertices) {
1060             List<GraphVertex> inUseByVertex = isInUse(elementV);
1061             if (!inUseByVertex.isEmpty()) {
1062                 inUseBy.addAll(inUseByVertex);
1063             }
1064         }
1065         return inUseBy;
1066     }
1067
1068     private boolean isAnyComponentInUse(List<GraphVertex> componentVertices) {
1069         boolean isComponentInUse = false;
1070         if (log.isDebugEnabled()) {
1071             for (final GraphVertex graphVertex : componentVertices) {
1072                 if (!isInUse(graphVertex).isEmpty()) {
1073                     isComponentInUse = true;
1074                 }
1075             }
1076         } else {
1077             isComponentInUse = componentVertices.stream().anyMatch(vertex -> !isInUse(vertex).isEmpty());
1078         }
1079         return isComponentInUse;
1080     }
1081
1082     private List<GraphVertex> isInUse(GraphVertex elementV) {
1083         final List<EdgeLabelEnum> forbiddenEdgeLabelEnums = Arrays
1084             .asList(EdgeLabelEnum.INSTANCE_OF, EdgeLabelEnum.PROXY_OF, EdgeLabelEnum.ALLOTTED_OF);
1085         for (EdgeLabelEnum edgeLabelEnum : forbiddenEdgeLabelEnums) {
1086             Either<List<GraphVertex>, JanusGraphOperationStatus> inUseBy =
1087                 janusGraphDao.getParentVertices(elementV, edgeLabelEnum, JsonParseFlagEnum.ParseAll);
1088             if (inUseBy.isLeft()) {
1089                 if (log.isDebugEnabled()) {
1090                     log.debug("Element {} in use.", elementV.getUniqueId());
1091                 }
1092                 return inUseBy.left().value();
1093             }
1094         }
1095         return Collections.emptyList();
1096     }
1097
1098     private List<String> checkIfInUseAndDelete(List<GraphVertex> allMarked) {
1099         List<String> deleted = new ArrayList<>();
1100         for (GraphVertex elementV : allMarked) {
1101             boolean isAllowedToDelete = !isInUse(elementV).isEmpty();
1102             if (isAllowedToDelete) {
1103                 Either<ToscaElement, StorageOperationStatus> deleteToscaElement = deleteToscaElement(elementV);
1104                 if (deleteToscaElement.isRight()) {
1105                     log.debug("Failed to delete marked element UniqueID {}, Name {}, error {}", elementV.getUniqueId(),
1106                         elementV.getMetadataProperties().get(GraphPropertyEnum.NAME), deleteToscaElement.right().value());
1107                     continue;
1108                 }
1109                 deleted.add(elementV.getUniqueId());
1110             }
1111         }
1112         return deleted;
1113     }
1114
1115     private void lockAllVerticesByNodeType(List<GraphVertex> allVerticesToLock, NodeTypeEnum nodeType) {
1116         for (GraphVertex graphVertex : allVerticesToLock) {
1117             StorageOperationStatus storageOperationStatus = graphLockOperation.lockComponent(graphVertex.getUniqueId(), nodeType);
1118             if (!storageOperationStatus.equals(StorageOperationStatus.OK)) {
1119                 throwStorageException(storageOperationStatus);
1120             }
1121         }
1122     }
1123
1124     private void unlockAllVerticesByNodeType(List<GraphVertex> allVerticesToUnlock, NodeTypeEnum nodeType) {
1125         for (GraphVertex graphVertex : allVerticesToUnlock) {
1126             graphLockOperation.unlockComponent(graphVertex.getUniqueId(), nodeType);
1127         }
1128     }
1129
1130     public Either<List<String>, StorageOperationStatus> getAllComponentsMarkedForDeletion(ComponentTypeEnum componentType) {
1131         Either<List<GraphVertex>, StorageOperationStatus> allComponentsMarkedForDeletion;
1132         switch (componentType) {
1133             case RESOURCE:
1134                 allComponentsMarkedForDeletion = nodeTypeOperation.getAllComponentsMarkedForDeletion(componentType);
1135                 break;
1136             case SERVICE:
1137             case PRODUCT:
1138                 allComponentsMarkedForDeletion = topologyTemplateOperation.getAllComponentsMarkedForDeletion(componentType);
1139                 break;
1140             default:
1141                 log.debug(NOT_SUPPORTED_COMPONENT_TYPE, componentType);
1142                 return Either.right(StorageOperationStatus.BAD_REQUEST);
1143         }
1144         if (allComponentsMarkedForDeletion.isRight()) {
1145             return Either.right(allComponentsMarkedForDeletion.right().value());
1146         }
1147         return Either.left(allComponentsMarkedForDeletion.left().value().stream().map(GraphVertex::getUniqueId).collect(Collectors.toList()));
1148     }
1149
1150     // region - Component Update
1151     public Either<ImmutablePair<Component, String>, StorageOperationStatus> addComponentInstanceToTopologyTemplate(Component containerComponent,
1152                                                                                                                    Component origComponent,
1153                                                                                                                    ComponentInstance componentInstance,
1154                                                                                                                    boolean allowDeleted, User user) {
1155         Either<ImmutablePair<Component, String>, StorageOperationStatus> result = null;
1156         Either<ToscaElement, StorageOperationStatus> updateContainerComponentRes = null;
1157         if (StringUtils.isEmpty(componentInstance.getIcon())) {
1158             componentInstance.setIcon(origComponent.getIcon());
1159         }
1160         String nameToFindForCounter;
1161         switch (componentInstance.getOriginType()) {
1162             case ServiceProxy:
1163                 nameToFindForCounter = ValidationUtils.normaliseComponentName(componentInstance.getSourceModelName()) + PROXY_SUFFIX;
1164                 break;
1165             case ServiceSubstitution:
1166                 nameToFindForCounter = ValidationUtils.normaliseComponentName(componentInstance.getSourceModelName());
1167                 break;
1168             default:
1169                 nameToFindForCounter = origComponent.getName();
1170         }
1171         String nextComponentInstanceCounter = getNextComponentInstanceCounter(containerComponent, nameToFindForCounter);
1172         Either<ImmutablePair<TopologyTemplate, String>, StorageOperationStatus> addResult = nodeTemplateOperation
1173             .addComponentInstanceToTopologyTemplate(ModelConverter.convertToToscaElement(containerComponent),
1174                 ModelConverter.convertToToscaElement(origComponent), nextComponentInstanceCounter, componentInstance, allowDeleted, user);
1175         if (addResult.isRight()) {
1176             CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to add the component instance {} to container component {}. ",
1177                 componentInstance.getName(), containerComponent.getName());
1178             result = Either.right(addResult.right().value());
1179         }
1180         if (result == null) {
1181             updateContainerComponentRes = topologyTemplateOperation.getToscaElement(containerComponent.getUniqueId());
1182             if (updateContainerComponentRes.isRight()) {
1183                 CommonUtility
1184                     .addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to fetch updated topology template {} with updated component instance {}. ",
1185                         containerComponent.getName(), componentInstance.getName());
1186                 result = Either.right(updateContainerComponentRes.right().value());
1187             }
1188         }
1189         if (result == null) {
1190             Component updatedComponent = ModelConverter.convertFromToscaElement(updateContainerComponentRes.left().value());
1191             String createdInstanceId = addResult.left().value().getRight();
1192             CommonUtility
1193                 .addRecordToLog(log, LogLevelEnum.TRACE, "The component instance {} has been added to container component {}. ", createdInstanceId,
1194                     updatedComponent.getName());
1195             result = Either.left(new ImmutablePair<>(updatedComponent, createdInstanceId));
1196         }
1197         return result;
1198     }
1199
1200     public void associateComponentInstancesToComponent(Component containerComponent, Map<ComponentInstance, Resource> resourcesInstancesMap,
1201                                                        boolean allowDeleted, boolean isUpdateCsar) {
1202         CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Going to add component instances to component {}", containerComponent.getUniqueId());
1203         Either<GraphVertex, JanusGraphOperationStatus> metadataVertex = janusGraphDao
1204             .getVertexById(containerComponent.getUniqueId(), JsonParseFlagEnum.ParseAll);
1205         if (metadataVertex.isRight()) {
1206             JanusGraphOperationStatus status = metadataVertex.right().value();
1207             if (status == JanusGraphOperationStatus.NOT_FOUND) {
1208                 status = JanusGraphOperationStatus.INVALID_ID;
1209             }
1210             throw new StorageException(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
1211         }
1212         Map<String, ComponentInstanceDataDefinition> compnentInstancesMap = nodeTemplateOperation
1213             .associateComponentInstancesToComponent(containerComponent, resourcesInstancesMap, metadataVertex.left().value(), allowDeleted,
1214                 isUpdateCsar);
1215         containerComponent.setComponentInstances(ModelConverter.getComponentInstancesFromMapObject(compnentInstancesMap, containerComponent));
1216     }
1217
1218     public Either<ImmutablePair<Component, String>, StorageOperationStatus> updateComponentInstanceMetadataOfTopologyTemplate(
1219         Component containerComponent, Component origComponent, ComponentInstance componentInstance) {
1220         Either<ImmutablePair<Component, String>, StorageOperationStatus> result = null;
1221         CommonUtility.addRecordToLog(log, LogLevelEnum.TRACE,
1222             "Going to update the metadata of the component instance {} belonging to container component {}. ", componentInstance.getName(),
1223             containerComponent.getName());
1224         componentInstance.setIcon(origComponent.getIcon());
1225         Either<ImmutablePair<TopologyTemplate, String>, StorageOperationStatus> updateResult = nodeTemplateOperation
1226             .updateComponentInstanceMetadataOfTopologyTemplate(ModelConverter.convertToToscaElement(containerComponent),
1227                 ModelConverter.convertToToscaElement(origComponent), componentInstance);
1228         if (updateResult.isRight()) {
1229             CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG,
1230                 "Failed to update the metadata of the component instance {} belonging to container component {}. ", componentInstance.getName(),
1231                 containerComponent.getName());
1232             result = Either.right(updateResult.right().value());
1233         }
1234         if (result == null) {
1235             Component updatedComponent = ModelConverter.convertFromToscaElement(updateResult.left().value().getLeft());
1236             String createdInstanceId = updateResult.left().value().getRight();
1237             CommonUtility
1238                 .addRecordToLog(log, LogLevelEnum.TRACE, "The metadata of the component instance {} has been updated to container component {}. ",
1239                     createdInstanceId, updatedComponent.getName());
1240             result = Either.left(new ImmutablePair<>(updatedComponent, createdInstanceId));
1241         }
1242         return result;
1243     }
1244
1245     public Either<Component, StorageOperationStatus> updateComponentInstanceMetadataOfTopologyTemplate(Component containerComponent) {
1246         return updateComponentInstanceMetadataOfTopologyTemplate(containerComponent, new ComponentParametersView());
1247     }
1248
1249     public Either<Component, StorageOperationStatus> updateComponentInstanceMetadataOfTopologyTemplate(Component containerComponent,
1250                                                                                                        ComponentParametersView filter) {
1251         Either<Component, StorageOperationStatus> result = null;
1252         CommonUtility.addRecordToLog(log, LogLevelEnum.TRACE, "Going to update the metadata  belonging to container component {}. ",
1253             containerComponent.getName());
1254         Either<TopologyTemplate, StorageOperationStatus> updateResult = nodeTemplateOperation
1255             .updateComponentInstanceMetadataOfTopologyTemplate(ModelConverter.convertToToscaElement(containerComponent), filter);
1256         if (updateResult.isRight()) {
1257             CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to update the metadata  belonging to container component {}. ",
1258                 containerComponent.getName());
1259             result = Either.right(updateResult.right().value());
1260         }
1261         if (result == null) {
1262             Component updatedComponent = ModelConverter.convertFromToscaElement(updateResult.left().value());
1263             CommonUtility
1264                 .addRecordToLog(log, LogLevelEnum.TRACE, "The metadata has been updated to container component {}. ", updatedComponent.getName());
1265             result = Either.left(updatedComponent);
1266         }
1267         return result;
1268     }
1269
1270     // endregion
1271     public Either<ImmutablePair<Component, String>, StorageOperationStatus> deleteComponentInstanceFromTopologyTemplate(Component containerComponent,
1272                                                                                                                         String resourceInstanceId) {
1273         Either<ImmutablePair<Component, String>, StorageOperationStatus> result = null;
1274         CommonUtility.addRecordToLog(log, LogLevelEnum.TRACE, "Going to delete the component instance {} belonging to container component {}. ",
1275             resourceInstanceId, containerComponent.getName());
1276         Either<ImmutablePair<TopologyTemplate, String>, StorageOperationStatus> updateResult = nodeTemplateOperation
1277             .deleteComponentInstanceFromTopologyTemplate(ModelConverter.convertToToscaElement(containerComponent), resourceInstanceId);
1278         if (updateResult.isRight()) {
1279             CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to delete the component instance {} belonging to container component {}. ",
1280                 resourceInstanceId, containerComponent.getName());
1281             result = Either.right(updateResult.right().value());
1282         }
1283         if (result == null) {
1284             Component updatedComponent = ModelConverter.convertFromToscaElement(updateResult.left().value().getLeft());
1285             String deletedInstanceId = updateResult.left().value().getRight();
1286             CommonUtility.addRecordToLog(log, LogLevelEnum.TRACE, "The component instance {} has been deleted from container component {}. ",
1287                 deletedInstanceId, updatedComponent.getName());
1288             result = Either.left(new ImmutablePair<>(updatedComponent, deletedInstanceId));
1289         }
1290         return result;
1291     }
1292
1293     private String getNextComponentInstanceCounter(Component containerComponent, String originResourceName) {
1294         Integer nextCounter = 0;
1295         if (CollectionUtils.isNotEmpty(containerComponent.getComponentInstances())) {
1296             String normalizedName = ValidationUtils.normalizeComponentInstanceName(originResourceName);
1297             Integer maxCounter = getMaxCounterFromNamesAndIds(containerComponent, normalizedName);
1298             if (maxCounter != null) {
1299                 nextCounter = maxCounter + 1;
1300             }
1301         }
1302         return nextCounter.toString();
1303     }
1304
1305     /**
1306      * @return max counter of component instance Id's, null if not found
1307      */
1308     private Integer getMaxCounterFromNamesAndIds(final Component containerComponent, final String normalizedName) {
1309         final Pattern COUNTER_PATTERN = Pattern.compile(normalizedName + "[\\s_:-]?\\d+$");
1310         final List<String> countersInNames = containerComponent.getComponentInstances().stream()
1311             .filter(ci -> ci.getNormalizedName() != null && ci.getNormalizedName().startsWith(normalizedName))
1312             .filter(ci -> !ci.getNormalizedName().equals(normalizedName))
1313             .map(ComponentInstance::getNormalizedName)
1314             .map(COUNTER_PATTERN::matcher).filter(Matcher::find).map(matcher -> matcher.group(0))
1315             .map(nn -> nn.replaceAll("\\D", ""))
1316             .collect(Collectors.toList());
1317         final List<String> countersInIds = containerComponent.getComponentInstances().stream()
1318             .filter(ci -> ci.getUniqueId() != null && ci.getUniqueId().contains(normalizedName))
1319             .map(ComponentInstance::getUniqueId)
1320             .map(COUNTER_PATTERN::matcher).filter(Matcher::find).map(matcher -> matcher.group(0))
1321             .map(nn -> nn.replaceAll("\\D", ""))
1322             .collect(Collectors.toList());
1323         final List<String> namesAndIdsList = new ArrayList<>(countersInNames);
1324         namesAndIdsList.addAll(countersInIds);
1325         return getMaxInteger(namesAndIdsList);
1326     }
1327
1328     private Integer getMaxInteger(List<String> counters) {
1329         Integer maxCounter = 0;
1330         Integer currCounter = null;
1331         for (String counter : counters) {
1332             try {
1333                 currCounter = Integer.parseInt(counter);
1334                 if (maxCounter < currCounter) {
1335                     maxCounter = currCounter;
1336                 }
1337             } catch (NumberFormatException e) {
1338             }
1339         }
1340         return currCounter == null ? null : maxCounter;
1341     }
1342
1343     public Either<RequirementCapabilityRelDef, StorageOperationStatus> associateResourceInstances(Component component, String componentId,
1344                                                                                                   RequirementCapabilityRelDef requirementDef) {
1345         return nodeTemplateOperation.associateResourceInstances(componentId, requirementDef);
1346     }
1347
1348     public Either<List<InputDefinition>, StorageOperationStatus> createAndAssociateInputs(Map<String, InputDefinition> inputs, String componentId) {
1349         Either<GraphVertex, JanusGraphOperationStatus> getVertexEither = janusGraphDao.getVertexById(componentId, JsonParseFlagEnum.NoParse);
1350         if (getVertexEither.isRight()) {
1351             log.debug(COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, componentId, getVertexEither.right().value());
1352             return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVertexEither.right().value()));
1353         }
1354         GraphVertex vertex = getVertexEither.left().value();
1355         Map<String, PropertyDataDefinition> inputsMap = inputs.entrySet().stream()
1356             .collect(Collectors.toMap(Map.Entry::getKey, e -> new PropertyDataDefinition(e.getValue())));
1357         StorageOperationStatus status = topologyTemplateOperation.associateInputsToComponent(vertex, inputsMap, componentId);
1358         if (StorageOperationStatus.OK == status) {
1359             log.debug(INPUTS_ASSOCIATED_TO_COMPONENT_SUCCESSFULLY);
1360             List<InputDefinition> inputsResList = null;
1361             if (inputsMap != null && !inputsMap.isEmpty()) {
1362                 inputsResList = inputsMap.values().stream().map(InputDefinition::new).collect(Collectors.toList());
1363             }
1364             return Either.left(inputsResList);
1365         }
1366         return Either.right(status);
1367     }
1368
1369     public Either<List<OutputDefinition>, StorageOperationStatus> createAndAssociateOutputs(final Map<String, OutputDefinition> outputs,
1370                                                                                             final String componentId) {
1371         final Either<GraphVertex, JanusGraphOperationStatus> getVertexEither = janusGraphDao.getVertexById(componentId, JsonParseFlagEnum.NoParse);
1372         if (getVertexEither.isRight()) {
1373             log.debug(COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, componentId, getVertexEither.right().value());
1374             return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVertexEither.right().value()));
1375         }
1376         final GraphVertex vertex = getVertexEither.left().value();
1377         final Map<String, OutputDefinition> outputsMap = outputs.entrySet().stream()
1378             .collect(Collectors.toMap(Map.Entry::getKey, e -> new OutputDefinition(e.getValue())));
1379         final StorageOperationStatus status = topologyTemplateOperation.associateOutputsToComponent(vertex, outputsMap, componentId);
1380         if (StorageOperationStatus.OK == status) {
1381             log.debug(OUTPUTS_ASSOCIATED_TO_COMPONENT_SUCCESSFULLY);
1382             List<OutputDefinition> outputsResList = null;
1383             if (MapUtils.isNotEmpty(outputsMap)) {
1384                 outputsResList = outputsMap.values().stream().map(OutputDefinition::new).collect(Collectors.toList());
1385             }
1386             return Either.left(outputsResList);
1387         }
1388         return Either.right(status);
1389     }
1390
1391     public Either<List<InputDefinition>, StorageOperationStatus> addInputsToComponent(Map<String, InputDefinition> inputs, String componentId) {
1392         Either<GraphVertex, JanusGraphOperationStatus> getVertexEither = janusGraphDao.getVertexById(componentId, JsonParseFlagEnum.NoParse);
1393         if (getVertexEither.isRight()) {
1394             log.debug(COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, componentId, getVertexEither.right().value());
1395             return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVertexEither.right().value()));
1396         }
1397         GraphVertex vertex = getVertexEither.left().value();
1398         Map<String, PropertyDefinition> inputsMap = inputs.entrySet().stream()
1399             .collect(Collectors.toMap(Map.Entry::getKey, e -> new PropertyDefinition(e.getValue())));
1400         StorageOperationStatus status = topologyTemplateOperation
1401             .addToscaDataToToscaElement(vertex, EdgeLabelEnum.INPUTS, VertexTypeEnum.INPUTS, inputsMap, JsonPresentationFields.NAME);
1402         if (StorageOperationStatus.OK == status) {
1403             log.debug(COMPONENT_CREATED_SUCCESSFULLY);
1404             List<InputDefinition> inputsResList = null;
1405             if (inputsMap != null && !inputsMap.isEmpty()) {
1406                 inputsResList = inputsMap.values().stream().map(InputDefinition::new).collect(Collectors.toList());
1407             }
1408             return Either.left(inputsResList);
1409         }
1410         return Either.right(status);
1411     }
1412
1413     public Either<List<OutputDefinition>, StorageOperationStatus> addOutputsToComponent(Map<String, OutputDefinition> outputs, String componentId) {
1414         Either<GraphVertex, JanusGraphOperationStatus> getVertexEither = janusGraphDao.getVertexById(componentId, JsonParseFlagEnum.NoParse);
1415         if (getVertexEither.isRight()) {
1416             log.debug(COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, componentId, getVertexEither.right().value());
1417             return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVertexEither.right().value()));
1418         }
1419         GraphVertex vertex = getVertexEither.left().value();
1420         Map<String, AttributeDefinition> outputsMap = outputs.entrySet().stream()
1421             .collect(Collectors.toMap(Map.Entry::getKey, e -> new AttributeDefinition(e.getValue())));
1422         StorageOperationStatus status = topologyTemplateOperation
1423             .addToscaDataToToscaElement(vertex, EdgeLabelEnum.OUTPUTS, VertexTypeEnum.OUTPUTS, outputsMap, JsonPresentationFields.NAME);
1424         if (StorageOperationStatus.OK == status) {
1425             log.debug(COMPONENT_CREATED_SUCCESSFULLY);
1426             List<OutputDefinition> outputsResList = null;
1427             if (outputsMap != null && !outputsMap.isEmpty()) {
1428                 outputsResList = outputsMap.values().stream().map(OutputDefinition::new).collect(Collectors.toList());
1429             }
1430             return Either.left(outputsResList);
1431         }
1432         return Either.right(status);
1433     }
1434
1435     /**
1436      * Add data types into a Component.
1437      *
1438      * @param dataTypes   datatypes to be added. the key should be each name of data type.
1439      * @param componentId unique ID of Component.
1440      * @return list of data types.
1441      */
1442     public Either<List<DataTypeDefinition>, StorageOperationStatus> addDataTypesToComponent(Map<String, DataTypeDefinition> dataTypes,
1443                                                                                             String componentId) {
1444         log.trace("#addDataTypesToComponent - enter, componentId={}", componentId);
1445         /* get component vertex */
1446         Either<GraphVertex, JanusGraphOperationStatus> getVertexEither = janusGraphDao
1447             .getVertexById(componentId, JsonParseFlagEnum.NoParse);
1448         if (getVertexEither.isRight()) {
1449             /* not found / error */
1450             log.debug(COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, componentId, getVertexEither.right().value());
1451             return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVertexEither.right().value()));
1452         }
1453         GraphVertex vertex = getVertexEither.left().value();
1454         log.trace("#addDataTypesToComponent - get vertex ok");
1455         // convert DataTypeDefinition to DataTypeDataDefinition
1456         Map<String, DataTypeDataDefinition> dataTypeDataMap = dataTypes.entrySet().stream()
1457             .collect(Collectors.toMap(Map.Entry::getKey, e -> convertDataTypeToDataTypeData(e.getValue())));
1458         // add datatype(s) to the Component.
1459
1460         // if child vertex does not exist, it will be created.
1461         StorageOperationStatus status = topologyTemplateOperation
1462             .addToscaDataToToscaElement(vertex, EdgeLabelEnum.DATA_TYPES, VertexTypeEnum.DATA_TYPES, dataTypeDataMap, JsonPresentationFields.NAME);
1463         if (StorageOperationStatus.OK == status) {
1464             log.debug(COMPONENT_CREATED_SUCCESSFULLY);
1465             List<DataTypeDefinition> inputsResList = null;
1466             if (!dataTypes.isEmpty()) {
1467                 inputsResList = new ArrayList<>(dataTypes.values());
1468             }
1469             return Either.left(inputsResList);
1470         }
1471         log.trace("#addDataTypesToComponent - leave");
1472         return Either.right(status);
1473     }
1474
1475     private DataTypeDataDefinition convertDataTypeToDataTypeData(DataTypeDefinition dataType) {
1476         DataTypeDataDefinition dataTypeData = new DataTypeDataDefinition(dataType);
1477         if (CollectionUtils.isNotEmpty(dataType.getProperties())) {
1478             List<PropertyDataDefinition> propertyDataList = dataType.getProperties().stream().map(PropertyDataDefinition::new)
1479                 .collect(Collectors.toList());
1480             dataTypeData.setPropertiesData(propertyDataList);
1481         }
1482         // if "derivedFrom" data_type exists, copy the name to "derivedFromName"
1483         if (dataType.getDerivedFrom() != null && StringUtils.isNotEmpty(dataType.getDerivedFrom().getName())) {
1484             // if names are different, log it
1485             if (!StringUtils.equals(dataTypeData.getDerivedFromName(), dataType.getDerivedFrom().getName())) {
1486                 log.debug("#convertDataTypeToDataTypeData - derivedFromName(={}) overwritten by derivedFrom.name(={})", dataType.getDerivedFromName(),
1487                     dataType.getDerivedFrom().getName());
1488             }
1489             dataTypeData.setDerivedFromName(dataType.getDerivedFrom().getName());
1490         }
1491         // supply "name" field to toscaPresentationValue in each datatype object for DAO operations
1492         dataTypeData.setToscaPresentationValue(JsonPresentationFields.NAME, dataType.getName());
1493         return dataTypeData;
1494     }
1495
1496     public Either<List<InputDefinition>, StorageOperationStatus> getComponentInputs(String componentId) {
1497         Either<GraphVertex, JanusGraphOperationStatus> getVertexEither = janusGraphDao.getVertexById(componentId, JsonParseFlagEnum.NoParse);
1498         if (getVertexEither.isRight()) {
1499             log.debug(COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, componentId, getVertexEither.right().value());
1500             return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVertexEither.right().value()));
1501         }
1502         Either<ToscaElement, StorageOperationStatus> toscaElement = topologyTemplateOperation.getToscaElement(componentId);
1503         if (toscaElement.isRight()) {
1504             return Either.right(toscaElement.right().value());
1505         }
1506         TopologyTemplate topologyTemplate = (TopologyTemplate) toscaElement.left().value();
1507         Map<String, PropertyDataDefinition> inputsMap = topologyTemplate.getInputs();
1508         List<InputDefinition> inputs = new ArrayList<>();
1509         if (MapUtils.isNotEmpty(inputsMap)) {
1510             inputs = inputsMap.values().stream().map(p -> new InputDefinition(p)).collect(Collectors.toList());
1511         }
1512         return Either.left(inputs);
1513     }
1514
1515     public Either<List<InputDefinition>, StorageOperationStatus> updateInputsToComponent(List<InputDefinition> inputs, String componentId) {
1516         Either<GraphVertex, JanusGraphOperationStatus> getVertexEither = janusGraphDao.getVertexById(componentId, JsonParseFlagEnum.NoParse);
1517         if (getVertexEither.isRight()) {
1518             log.debug(COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, componentId, getVertexEither.right().value());
1519             return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVertexEither.right().value()));
1520         }
1521         GraphVertex vertex = getVertexEither.left().value();
1522         List<PropertyDataDefinition> inputsAsDataDef = inputs.stream().map(PropertyDataDefinition::new).collect(Collectors.toList());
1523         StorageOperationStatus status = topologyTemplateOperation
1524             .updateToscaDataOfToscaElement(vertex, EdgeLabelEnum.INPUTS, VertexTypeEnum.INPUTS, inputsAsDataDef, JsonPresentationFields.NAME);
1525         if (StorageOperationStatus.OK == status) {
1526             log.debug(COMPONENT_CREATED_SUCCESSFULLY);
1527             List<InputDefinition> inputsResList = null;
1528             if (CollectionUtils.isNotEmpty(inputsAsDataDef)) {
1529                 inputsResList = inputsAsDataDef.stream().map(InputDefinition::new).collect(Collectors.toList());
1530             }
1531             return Either.left(inputsResList);
1532         }
1533         return Either.right(status);
1534     }
1535
1536     public Either<List<OutputDefinition>, StorageOperationStatus> updateOutputsToComponent(List<OutputDefinition> outputs, String componentId) {
1537         Either<GraphVertex, JanusGraphOperationStatus> getVertexEither = janusGraphDao.getVertexById(componentId, JsonParseFlagEnum.NoParse);
1538         if (getVertexEither.isRight()) {
1539             log.debug(COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, componentId, getVertexEither.right().value());
1540             return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVertexEither.right().value()));
1541         }
1542         GraphVertex vertex = getVertexEither.left().value();
1543         List<AttributeDataDefinition> outputsAsDataDef = outputs.stream().map(AttributeDataDefinition::new).collect(Collectors.toList());
1544         StorageOperationStatus status = topologyTemplateOperation
1545             .updateToscaDataOfToscaElement(vertex, EdgeLabelEnum.OUTPUTS, VertexTypeEnum.OUTPUTS, outputsAsDataDef, JsonPresentationFields.NAME);
1546         if (StorageOperationStatus.OK == status) {
1547             log.debug(COMPONENT_CREATED_SUCCESSFULLY);
1548             List<OutputDefinition> outputsResList = null;
1549             if (!outputsAsDataDef.isEmpty()) {
1550                 outputsResList = outputsAsDataDef.stream().map(OutputDefinition::new).collect(Collectors.toList());
1551             }
1552             return Either.left(outputsResList);
1553         }
1554         return Either.right(status);
1555     }
1556
1557     // region - ComponentInstance
1558     public Either<Map<String, List<ComponentInstanceProperty>>, StorageOperationStatus> associateComponentInstancePropertiesToComponent(
1559         Map<String, List<ComponentInstanceProperty>> instProperties, String componentId) {
1560         Either<GraphVertex, JanusGraphOperationStatus> getVertexEither = janusGraphDao.getVertexById(componentId, JsonParseFlagEnum.NoParse);
1561         if (getVertexEither.isRight()) {
1562             log.debug(COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, componentId, getVertexEither.right().value());
1563             return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVertexEither.right().value()));
1564         }
1565         GraphVertex vertex = getVertexEither.left().value();
1566         Map<String, MapPropertiesDataDefinition> instPropsMap = new HashMap<>();
1567         if (instProperties != null) {
1568             MapPropertiesDataDefinition propertiesMap;
1569             for (Entry<String, List<ComponentInstanceProperty>> entry : instProperties.entrySet()) {
1570                 propertiesMap = new MapPropertiesDataDefinition();
1571                 propertiesMap.setMapToscaDataDefinition(
1572                     entry.getValue().stream().map(PropertyDataDefinition::new).collect(Collectors.toMap(PropertyDataDefinition::getName, e -> e)));
1573                 instPropsMap.put(entry.getKey(), propertiesMap);
1574             }
1575         }
1576         StorageOperationStatus status = topologyTemplateOperation.associateInstPropertiesToComponent(vertex, instPropsMap);
1577         if (StorageOperationStatus.OK == status) {
1578             log.debug(COMPONENT_CREATED_SUCCESSFULLY);
1579             return Either.left(instProperties);
1580         }
1581         return Either.right(status);
1582     }
1583
1584     /**
1585      * saves the instInputs as the updated instance inputs of the component container in DB
1586      */
1587     public Either<Map<String, List<ComponentInstanceInput>>, StorageOperationStatus> updateComponentInstanceInputsToComponent(
1588         Map<String, List<ComponentInstanceInput>> instInputs, String componentId) {
1589         if (instInputs == null || instInputs.isEmpty()) {
1590             return Either.left(instInputs);
1591         }
1592         StorageOperationStatus status;
1593         for (Entry<String, List<ComponentInstanceInput>> inputsPerIntance : instInputs.entrySet()) {
1594             List<ComponentInstanceInput> toscaDataListPerInst = inputsPerIntance.getValue();
1595             List<String> pathKeysPerInst = new ArrayList<>();
1596             pathKeysPerInst.add(inputsPerIntance.getKey());
1597             status = topologyTemplateOperation
1598                 .updateToscaDataDeepElementsOfToscaElement(componentId, EdgeLabelEnum.INST_INPUTS, VertexTypeEnum.INST_INPUTS, toscaDataListPerInst,
1599                     pathKeysPerInst, JsonPresentationFields.NAME);
1600             if (status != StorageOperationStatus.OK) {
1601                 log.debug("Failed to update component instance inputs for instance {} in component {} edge type {} error {}",
1602                     inputsPerIntance.getKey(), componentId, EdgeLabelEnum.INST_INPUTS, status);
1603                 return Either.right(status);
1604             }
1605         }
1606         return Either.left(instInputs);
1607     }
1608
1609     /**
1610      * saves the instProps as the updated instance properties of the component container in DB
1611      */
1612     public Either<Map<String, List<ComponentInstanceProperty>>, StorageOperationStatus> updateComponentInstancePropsToComponent(
1613         Map<String, List<ComponentInstanceProperty>> instProps, String componentId) {
1614         if (instProps == null || instProps.isEmpty()) {
1615             return Either.left(instProps);
1616         }
1617         StorageOperationStatus status;
1618         for (Entry<String, List<ComponentInstanceProperty>> propsPerIntance : instProps.entrySet()) {
1619             List<ComponentInstanceProperty> toscaDataListPerInst = propsPerIntance.getValue();
1620             List<String> pathKeysPerInst = new ArrayList<>();
1621             pathKeysPerInst.add(propsPerIntance.getKey());
1622             status = topologyTemplateOperation
1623                 .updateToscaDataDeepElementsOfToscaElement(componentId, EdgeLabelEnum.INST_PROPERTIES, VertexTypeEnum.INST_PROPERTIES,
1624                     toscaDataListPerInst, pathKeysPerInst, JsonPresentationFields.NAME);
1625             if (status != StorageOperationStatus.OK) {
1626                 log.debug("Failed to update component instance inputs for instance {} in component {} edge type {} error {}",
1627                     propsPerIntance.getKey(), componentId, EdgeLabelEnum.INST_PROPERTIES, status);
1628                 return Either.right(status);
1629             }
1630         }
1631         return Either.left(instProps);
1632     }
1633
1634     public Either<Map<String, List<ComponentInstanceInput>>, StorageOperationStatus> associateComponentInstanceInputsToComponent(
1635         Map<String, List<ComponentInstanceInput>> instInputs, String componentId) {
1636         Either<GraphVertex, JanusGraphOperationStatus> getVertexEither = janusGraphDao.getVertexById(componentId, JsonParseFlagEnum.NoParse);
1637         if (getVertexEither.isRight()) {
1638             log.debug(COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, componentId, getVertexEither.right().value());
1639             return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVertexEither.right().value()));
1640         }
1641         GraphVertex vertex = getVertexEither.left().value();
1642         Map<String, MapPropertiesDataDefinition> instPropsMap = new HashMap<>();
1643         if (instInputs != null) {
1644             MapPropertiesDataDefinition propertiesMap;
1645             for (Entry<String, List<ComponentInstanceInput>> entry : instInputs.entrySet()) {
1646                 propertiesMap = new MapPropertiesDataDefinition();
1647                 propertiesMap.setMapToscaDataDefinition(
1648                     entry.getValue().stream().map(PropertyDataDefinition::new).collect(Collectors.toMap(PropertyDataDefinition::getName, e -> e)));
1649                 instPropsMap.put(entry.getKey(), propertiesMap);
1650             }
1651         }
1652         StorageOperationStatus status = topologyTemplateOperation.associateInstInputsToComponent(vertex, instPropsMap);
1653         if (StorageOperationStatus.OK == status) {
1654             log.debug(COMPONENT_CREATED_SUCCESSFULLY);
1655             return Either.left(instInputs);
1656         }
1657         return Either.right(status);
1658     }
1659
1660     public Either<Map<String, MapInterfaceDataDefinition>, StorageOperationStatus> associateComponentInstanceInterfacesToComponent(
1661         Map<String, Map<String, InterfaceDefinition>> instInterfaces,
1662         String componentId
1663     ) {
1664         Either<GraphVertex, JanusGraphOperationStatus> getVertexEither = janusGraphDao.getVertexById(
1665             componentId,
1666             JsonParseFlagEnum.NoParse
1667         );
1668         if (getVertexEither.isRight()) {
1669             log.debug(
1670                 COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR,
1671                 componentId,
1672                 getVertexEither.right().value()
1673             );
1674             return Either.right(
1675                 DaoStatusConverter.convertJanusGraphStatusToStorageStatus(
1676                     getVertexEither.right().value()
1677                 )
1678             );
1679         }
1680         GraphVertex vertex = getVertexEither.left().value();
1681         Map<String, MapInterfaceDataDefinition> instInterfacesMap = new HashMap<>();
1682         if (instInterfaces != null) {
1683
1684             for (Map.Entry<String, Map<String, InterfaceDefinition>> entryInstances : instInterfaces.entrySet()) {
1685                 Map<String, InterfaceDataDefinition> incomingInterfacesMap = entryInstances.getValue().entrySet().stream()
1686                     .collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue()));
1687                 MapInterfaceDataDefinition interfacesMap = new MapInterfaceDataDefinition();
1688                 interfacesMap.setMapToscaDataDefinition(incomingInterfacesMap);
1689                 instInterfacesMap.put(entryInstances.getKey(), interfacesMap);
1690             }
1691         }
1692         StorageOperationStatus status = topologyTemplateOperation.associateInstInterfacesToComponent(
1693             vertex,
1694             instInterfacesMap
1695         );
1696         if (StorageOperationStatus.OK == status) {
1697             log.debug(COMPONENT_CREATED_SUCCESSFULLY);
1698             return Either.left(instInterfacesMap);
1699         }
1700         return Either.right(status);
1701     }
1702
1703     public Either<Map<String, List<ComponentInstanceInput>>, StorageOperationStatus> addComponentInstanceInputsToComponent(
1704         Component containerComponent, Map<String, List<ComponentInstanceInput>> instProperties) {
1705         requireNonNull(instProperties);
1706         StorageOperationStatus status;
1707         for (Entry<String, List<ComponentInstanceInput>> entry : instProperties.entrySet()) {
1708             List<ComponentInstanceInput> props = entry.getValue();
1709             String componentInstanceId = entry.getKey();
1710             if (!isEmpty(props)) {
1711                 for (ComponentInstanceInput property : props) {
1712                     List<ComponentInstanceInput> componentInstancesInputs = containerComponent.getComponentInstancesInputs().get(componentInstanceId);
1713                     Optional<ComponentInstanceInput> instanceProperty = componentInstancesInputs.stream()
1714                         .filter(p -> p.getName().equals(property.getName())).findAny();
1715                     if (instanceProperty.isPresent()) {
1716                         status = updateComponentInstanceInput(containerComponent, componentInstanceId, property);
1717                     } else {
1718                         status = addComponentInstanceInput(containerComponent, componentInstanceId, property);
1719                     }
1720                     if (status != StorageOperationStatus.OK) {
1721                         log.debug("Failed to update instance input {} for instance {} error {} ", property, componentInstanceId, status);
1722                         return Either.right(status);
1723                     } else {
1724                         log.trace("instance input {} for instance {} updated", property, componentInstanceId);
1725                     }
1726                 }
1727             }
1728         }
1729         return Either.left(instProperties);
1730     }
1731
1732     public Either<Map<String, List<ComponentInstanceOutput>>, StorageOperationStatus> addComponentInstanceOutputsToComponent(
1733         Component containerComponent, Map<String, List<ComponentInstanceOutput>> instOutputs) {
1734         requireNonNull(instOutputs);
1735         StorageOperationStatus status;
1736         for (final Entry<String, List<ComponentInstanceOutput>> entry : instOutputs.entrySet()) {
1737             final List<ComponentInstanceOutput> outputs = entry.getValue();
1738             final String componentInstanceId = entry.getKey();
1739             if (!isEmpty(outputs)) {
1740                 for (final ComponentInstanceOutput output : outputs) {
1741                     final List<ComponentInstanceOutput> componentInstanceOutputs = containerComponent.getComponentInstancesOutputs()
1742                         .get(componentInstanceId);
1743                     final Optional<ComponentInstanceOutput> componentInstanceOutput = componentInstanceOutputs.stream()
1744                         .filter(p -> p.getName().equals(output.getName())).findAny();
1745                     if (componentInstanceOutput.isPresent()) {
1746                         status = updateComponentInstanceOutput(containerComponent, componentInstanceId, output);
1747                     } else {
1748                         status = addComponentInstanceOutput(containerComponent, componentInstanceId, output);
1749                     }
1750                     if (status != StorageOperationStatus.OK) {
1751                         log.debug("Failed to update instance output {} for instance {} error {} ", output, componentInstanceId, status);
1752                         return Either.right(status);
1753                     } else {
1754                         log.trace("instance output {} for instance {} updated", output, componentInstanceId);
1755                     }
1756                 }
1757             }
1758         }
1759         return Either.left(instOutputs);
1760     }
1761
1762     public Either<Map<String, List<ComponentInstanceProperty>>, StorageOperationStatus> addComponentInstancePropertiesToComponent(
1763         Component containerComponent, Map<String, List<ComponentInstanceProperty>> instProperties) {
1764         requireNonNull(instProperties);
1765         for (Entry<String, List<ComponentInstanceProperty>> entry : instProperties.entrySet()) {
1766             List<ComponentInstanceProperty> props = entry.getValue();
1767             String componentInstanceId = entry.getKey();
1768             List<ComponentInstanceProperty> originalComponentInstProps = containerComponent.getComponentInstancesProperties()
1769                 .get(componentInstanceId);
1770             Map<String, List<CapabilityDefinition>> containerComponentCapabilities = containerComponent.getCapabilities();
1771             if (isEmpty(props)) {
1772                 continue;
1773             }
1774             for (ComponentInstanceProperty property : props) {
1775                 StorageOperationStatus status = null;
1776                 String propertyParentUniqueId = property.getParentUniqueId();
1777                 Optional<CapabilityDefinition> capPropDefinition = getPropertyCapability(propertyParentUniqueId, containerComponent);
1778                 if (capPropDefinition.isPresent() && MapUtils.isNotEmpty(containerComponentCapabilities)) {
1779                     status = populateAndUpdateInstanceCapProperty(containerComponent, componentInstanceId, containerComponentCapabilities, property,
1780                         capPropDefinition.get());
1781                 }
1782                 if (status == null) {
1783                     status = updateOrAddComponentInstanceProperty(containerComponent, componentInstanceId, originalComponentInstProps, property);
1784                 }
1785                 if (status != StorageOperationStatus.OK) {
1786                     return Either.right(status);
1787                 }
1788             }
1789         }
1790         return Either.left(instProperties);
1791     }
1792
1793     public Either<Map<String, List<ComponentInstanceAttribute>>, StorageOperationStatus> addComponentInstanceAttributesToComponent(
1794         final Component containerComponent, final Map<String, List<ComponentInstanceAttribute>> componentInstanceAttribute) {
1795         requireNonNull(componentInstanceAttribute);
1796         for (final Entry<String, List<ComponentInstanceAttribute>> entry : componentInstanceAttribute.entrySet()) {
1797             final List<ComponentInstanceAttribute> attributes = entry.getValue();
1798             if (isEmpty(attributes)) {
1799                 continue;
1800             }
1801             final String componentInstanceId = entry.getKey();
1802             final List<ComponentInstanceAttribute> componentInstanceAttributes = containerComponent.getComponentInstancesAttributes()
1803                 .get(componentInstanceId);
1804             for (final ComponentInstanceAttribute attribute : attributes) {
1805                 final StorageOperationStatus status = updateOrAddComponentInstanceAttribute(containerComponent, componentInstanceId,
1806                     componentInstanceAttributes, attribute);
1807                 if (status != StorageOperationStatus.OK) {
1808                     return Either.right(status);
1809                 }
1810             }
1811         }
1812         return Either.left(componentInstanceAttribute);
1813     }
1814
1815     private StorageOperationStatus populateAndUpdateInstanceCapProperty(Component containerComponent, String componentInstanceId,
1816                                                                         Map<String, List<CapabilityDefinition>> containerComponentCapabilities,
1817                                                                         ComponentInstanceProperty property,
1818                                                                         CapabilityDefinition capabilityDefinition) {
1819         List<CapabilityDefinition> capabilityDefinitions = containerComponentCapabilities.get(capabilityDefinition.getType());
1820         if (CollectionUtils.isEmpty(capabilityDefinitions)) {
1821             return null;
1822         }
1823         Optional<CapabilityDefinition> capDefToGetProp = capabilityDefinitions.stream()
1824             .filter(cap -> cap.getUniqueId().equals(capabilityDefinition.getUniqueId()) && cap.getPath().size() == 1).findAny();
1825         if (capDefToGetProp.isPresent()) {
1826             return updateInstanceCapabilityProperty(containerComponent, componentInstanceId, property, capDefToGetProp.get());
1827         }
1828         return null;
1829     }
1830
1831     private StorageOperationStatus updateOrAddComponentInstanceProperty(Component containerComponent, String componentInstanceId,
1832                                                                         List<ComponentInstanceProperty> originalComponentInstProps,
1833                                                                         ComponentInstanceProperty property) {
1834         StorageOperationStatus status;
1835         // check if the property already exists or not
1836         Optional<ComponentInstanceProperty> instanceProperty = originalComponentInstProps.stream()
1837             .filter(p -> p.getUniqueId().equals(property.getUniqueId())).findAny();
1838         if (instanceProperty.isPresent()) {
1839             status = updateComponentInstanceProperty(containerComponent, componentInstanceId, property);
1840         } else {
1841             status = addComponentInstanceProperty(containerComponent, componentInstanceId, property);
1842         }
1843         if (status != StorageOperationStatus.OK) {
1844             log.debug("Failed to update instance property {} for instance {} error {} ", property, componentInstanceId, status);
1845         }
1846         return status;
1847     }
1848
1849     private StorageOperationStatus updateOrAddComponentInstanceAttribute(Component containerComponent, String componentInstanceId,
1850                                                                          List<ComponentInstanceAttribute> componentInstanceAttributes,
1851                                                                          ComponentInstanceAttribute attribute) {
1852         StorageOperationStatus status;
1853         // check if the attribute already exists or not
1854         Optional<ComponentInstanceAttribute> instanceProperty = componentInstanceAttributes.stream()
1855             .filter(p -> p.getUniqueId().equals(attribute.getUniqueId())).findAny();
1856         if (instanceProperty.isPresent()) {
1857             status = updateComponentInstanceAttribute(containerComponent, componentInstanceId, attribute);
1858         } else {
1859             status = addComponentInstanceAttribute(containerComponent, componentInstanceId, attribute);
1860         }
1861         if (status != StorageOperationStatus.OK) {
1862             log.debug("Failed to update instance attribute {} for instance {} error {} ", attribute, componentInstanceId, status);
1863         }
1864         return status;
1865     }
1866
1867     public StorageOperationStatus updateInstanceCapabilityProperty(Component containerComponent, String componentInstanceId,
1868                                                                    ComponentInstanceProperty property, CapabilityDefinition capabilityDefinition) {
1869         Optional<ComponentInstance> fetchedCIOptional = containerComponent.getComponentInstanceById(componentInstanceId);
1870         if (!fetchedCIOptional.isPresent()) {
1871             return StorageOperationStatus.GENERAL_ERROR;
1872         }
1873         Either<Component, StorageOperationStatus> getComponentRes = getToscaFullElement(fetchedCIOptional.get().getComponentUid());
1874         if (getComponentRes.isRight()) {
1875             return StorageOperationStatus.GENERAL_ERROR;
1876         }
1877         Optional<Component> componentOptional = isNodeServiceProxy(getComponentRes.left().value());
1878         String propOwner;
1879         if (!componentOptional.isPresent()) {
1880             propOwner = componentInstanceId;
1881         } else {
1882             propOwner = fetchedCIOptional.get().getSourceModelUid();
1883         }
1884         StorageOperationStatus status;
1885         StringBuilder sb = new StringBuilder(componentInstanceId);
1886         sb.append(ModelConverter.CAP_PROP_DELIM).append(propOwner).append(ModelConverter.CAP_PROP_DELIM).append(capabilityDefinition.getType())
1887             .append(ModelConverter.CAP_PROP_DELIM).append(capabilityDefinition.getName());
1888         String capKey = sb.toString();
1889         status = updateComponentInstanceCapabiltyProperty(containerComponent, componentInstanceId, capKey, property);
1890         if (status != StorageOperationStatus.OK) {
1891             log.debug("Failed to update instance capability property {} for instance {} error {} ", property, componentInstanceId, status);
1892             return status;
1893         }
1894         return StorageOperationStatus.OK;
1895     }
1896
1897     private Optional<Component> isNodeServiceProxy(Component component) {
1898         if (component.getComponentType().equals(ComponentTypeEnum.SERVICE)) {
1899             return Optional.empty();
1900         }
1901         Resource resource = (Resource) component;
1902         ResourceTypeEnum resType = resource.getResourceType();
1903         if (resType.equals(ResourceTypeEnum.ServiceProxy)) {
1904             return Optional.of(component);
1905         }
1906         return Optional.empty();
1907     }
1908
1909     public StorageOperationStatus associateCapabilitiesToService(Map<String, ListCapabilityDataDefinition> capabilities, String componentId) {
1910         Either<GraphVertex, JanusGraphOperationStatus> getVertexEither = janusGraphDao.getVertexById(componentId, JsonParseFlagEnum.NoParse);
1911         if (getVertexEither.isRight()) {
1912             log.debug(COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, componentId, getVertexEither.right().value());
1913             return DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVertexEither.right().value());
1914         }
1915         GraphVertex vertex = getVertexEither.left().value();
1916         if (MapUtils.isNotEmpty(capabilities)) {
1917             Either<GraphVertex, StorageOperationStatus> associateElementToData = topologyTemplateOperation
1918                 .associateElementToData(vertex, VertexTypeEnum.CAPABILITIES, EdgeLabelEnum.CAPABILITIES, capabilities);
1919             if (associateElementToData.isRight()) {
1920                 return associateElementToData.right().value();
1921             }
1922         }
1923         return StorageOperationStatus.OK;
1924     }
1925
1926     public StorageOperationStatus associateRequirementsToService(Map<String, ListRequirementDataDefinition> requirements, String componentId) {
1927         Either<GraphVertex, JanusGraphOperationStatus> getVertexEither = janusGraphDao.getVertexById(componentId, JsonParseFlagEnum.NoParse);
1928         if (getVertexEither.isRight()) {
1929             log.debug(COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, componentId, getVertexEither.right().value());
1930             return DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVertexEither.right().value());
1931         }
1932         GraphVertex vertex = getVertexEither.left().value();
1933         if (MapUtils.isNotEmpty(requirements)) {
1934             Either<GraphVertex, StorageOperationStatus> associateElementToData = topologyTemplateOperation
1935                 .associateElementToData(vertex, VertexTypeEnum.REQUIREMENTS, EdgeLabelEnum.REQUIREMENTS, requirements);
1936             if (associateElementToData.isRight()) {
1937                 return associateElementToData.right().value();
1938             }
1939         }
1940         return StorageOperationStatus.OK;
1941     }
1942
1943     public StorageOperationStatus associateDeploymentArtifactsToInstances(Map<String, Map<String, ArtifactDefinition>> instDeploymentArtifacts,
1944                                                                           Component component, User user) {
1945         Either<GraphVertex, JanusGraphOperationStatus> getVertexEither = janusGraphDao
1946             .getVertexById(component.getUniqueId(), JsonParseFlagEnum.NoParse);
1947         if (getVertexEither.isRight()) {
1948             log.debug(COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, component.getUniqueId(), getVertexEither.right().value());
1949             return DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVertexEither.right().value());
1950         }
1951         GraphVertex vertex = getVertexEither.left().value();
1952         Map<String, MapArtifactDataDefinition> instArtMap = new HashMap<>();
1953         if (instDeploymentArtifacts != null) {
1954             MapArtifactDataDefinition artifactsMap;
1955             for (Entry<String, Map<String, ArtifactDefinition>> entry : instDeploymentArtifacts.entrySet()) {
1956                 Map<String, ArtifactDefinition> artList = entry.getValue();
1957                 Map<String, ArtifactDataDefinition> artifacts = artList.entrySet().stream()
1958                     .collect(Collectors.toMap(Map.Entry::getKey, e -> new ArtifactDataDefinition(e.getValue())));
1959                 artifactsMap = nodeTemplateOperation
1960                     .prepareInstDeploymentArtifactPerInstance(artifacts, entry.getKey(), user, NodeTemplateOperation.HEAT_VF_ENV_NAME);
1961                 instArtMap.put(entry.getKey(), artifactsMap);
1962             }
1963         }
1964         ModelConverter.setComponentInstancesDeploymentArtifactsToComponent(instArtMap, component);
1965         return topologyTemplateOperation.associateInstDeploymentArtifactsToComponent(vertex, instArtMap);
1966     }
1967
1968     public StorageOperationStatus associateArtifactsToInstances(Map<String, Map<String, ArtifactDefinition>> instArtifacts, Component component) {
1969         Either<GraphVertex, JanusGraphOperationStatus> getVertexEither = janusGraphDao
1970             .getVertexById(component.getUniqueId(), JsonParseFlagEnum.NoParse);
1971         if (getVertexEither.isRight()) {
1972             log.debug(COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, component.getUniqueId(), getVertexEither.right().value());
1973             return DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVertexEither.right().value());
1974         }
1975         GraphVertex vertex = getVertexEither.left().value();
1976         Map<String, MapArtifactDataDefinition> instArtMap = new HashMap<>();
1977         if (instArtifacts != null) {
1978             MapArtifactDataDefinition artifactsMap;
1979             for (Entry<String, Map<String, ArtifactDefinition>> entry : instArtifacts.entrySet()) {
1980                 Map<String, ArtifactDefinition> artList = entry.getValue();
1981                 Map<String, ArtifactDataDefinition> artifacts = artList.entrySet().stream()
1982                     .collect(Collectors.toMap(Map.Entry::getKey, e -> new ArtifactDataDefinition(e.getValue())));
1983                 artifactsMap = new MapArtifactDataDefinition(artifacts);
1984                 instArtMap.put(entry.getKey(), artifactsMap);
1985             }
1986         }
1987         ModelConverter.setComponentInstancesInformationalArtifactsToComponent(instArtMap, component);
1988         return topologyTemplateOperation.associateInstArtifactsToComponent(vertex, instArtMap);
1989     }
1990
1991     public StorageOperationStatus associateInstAttributeToComponentToInstances(Map<String, List<AttributeDefinition>> instArttributes,
1992                                                                                Component component) {
1993         Either<GraphVertex, JanusGraphOperationStatus> getVertexEither = janusGraphDao
1994             .getVertexById(component.getUniqueId(), JsonParseFlagEnum.NoParse);
1995         if (getVertexEither.isRight()) {
1996             log.debug(COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, component.getUniqueId(), getVertexEither.right().value());
1997             return DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVertexEither.right().value());
1998         }
1999         GraphVertex vertex = getVertexEither.left().value();
2000         Map<String, MapAttributesDataDefinition> instAttr = new HashMap<>();
2001         if (instArttributes != null) {
2002             MapAttributesDataDefinition attributesMap;
2003             for (Entry<String, List<AttributeDefinition>> entry : instArttributes.entrySet()) {
2004                 final List<AttributeDefinition> value = entry.getValue();
2005                 attributesMap = new MapAttributesDataDefinition();
2006                 attributesMap.setMapToscaDataDefinition(
2007                     value.stream().map(AttributeDefinition::new).collect(Collectors.toMap(AttributeDefinition::getName, e -> e)));
2008                 instAttr.put(entry.getKey(), attributesMap);
2009             }
2010         }
2011         setComponentInstanceAttributesOnComponent(component, instAttr);
2012         return topologyTemplateOperation.associateInstAttributeToComponent(vertex, instAttr);
2013     }
2014
2015     // endregion
2016     private void setComponentInstanceAttributesOnComponent(Component resource, Map<String, MapAttributesDataDefinition> instAttr) {
2017         Map<String, List<ComponentInstanceAttribute>> componentInstancesAttributes = resource.getComponentInstancesAttributes();
2018         if (componentInstancesAttributes == null) {
2019             componentInstancesAttributes = new HashMap<>();
2020         }
2021         componentInstancesAttributes.putAll(ModelConverter.getComponentInstancesAttributes(instAttr));
2022         resource.setComponentInstancesAttributes(componentInstancesAttributes);
2023     }
2024
2025     public StorageOperationStatus associateOrAddCalculatedCapReq(Map<ComponentInstance, Map<String, List<CapabilityDefinition>>> instCapabilties,
2026                                                                  Map<ComponentInstance, Map<String, List<RequirementDefinition>>> instReg,
2027                                                                  Component component) {
2028         Either<GraphVertex, JanusGraphOperationStatus> getVertexEither = janusGraphDao
2029             .getVertexById(component.getUniqueId(), JsonParseFlagEnum.NoParse);
2030         if (getVertexEither.isRight()) {
2031             log.debug(COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, component.getUniqueId(), getVertexEither.right().value());
2032             return DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVertexEither.right().value());
2033         }
2034         GraphVertex vertex = getVertexEither.left().value();
2035         Map<String, MapListRequirementDataDefinition> calcRequirements = new HashMap<>();
2036         Map<String, MapListCapabilityDataDefinition> calcCapabilty = new HashMap<>();
2037         Map<String, MapCapabilityProperty> calculatedCapabilitiesProperties = new HashMap<>();
2038         if (instCapabilties != null) {
2039             for (Entry<ComponentInstance, Map<String, List<CapabilityDefinition>>> entry : instCapabilties.entrySet()) {
2040                 Map<String, List<CapabilityDefinition>> caps = entry.getValue();
2041                 Map<String, ListCapabilityDataDefinition> mapToscaDataDefinition = new HashMap<>();
2042                 for (Entry<String, List<CapabilityDefinition>> instCapability : caps.entrySet()) {
2043                     mapToscaDataDefinition.put(instCapability.getKey(), new ListCapabilityDataDefinition(
2044                         instCapability.getValue().stream().map(CapabilityDataDefinition::new).collect(Collectors.toList())));
2045                 }
2046                 ComponentInstanceDataDefinition componentInstance = new ComponentInstanceDataDefinition(entry.getKey());
2047                 MapListCapabilityDataDefinition capMap = nodeTemplateOperation
2048                     .prepareCalculatedCapabiltyForNodeType(mapToscaDataDefinition, componentInstance);
2049                 MapCapabilityProperty mapCapabilityProperty = ModelConverter
2050                     .convertToMapOfMapCapabiltyProperties(caps, componentInstance.getUniqueId(), true);
2051                 calcCapabilty.put(entry.getKey().getUniqueId(), capMap);
2052                 calculatedCapabilitiesProperties.put(entry.getKey().getUniqueId(), mapCapabilityProperty);
2053             }
2054         }
2055         if (instReg != null) {
2056             for (Entry<ComponentInstance, Map<String, List<RequirementDefinition>>> entry : instReg.entrySet()) {
2057                 Map<String, List<RequirementDefinition>> req = entry.getValue();
2058                 Map<String, ListRequirementDataDefinition> mapToscaDataDefinition = new HashMap<>();
2059                 for (Entry<String, List<RequirementDefinition>> instReq : req.entrySet()) {
2060                     mapToscaDataDefinition.put(instReq.getKey(), new ListRequirementDataDefinition(
2061                         instReq.getValue().stream().map(RequirementDataDefinition::new).collect(Collectors.toList())));
2062                 }
2063                 MapListRequirementDataDefinition reqMap = nodeTemplateOperation
2064                     .prepareCalculatedRequirementForNodeType(mapToscaDataDefinition, new ComponentInstanceDataDefinition(entry.getKey()));
2065                 String componentInstanceId = entry.getKey().getUniqueId();
2066                 calcRequirements.put(componentInstanceId, reqMap);
2067             }
2068         }
2069         StorageOperationStatus storageOperationStatus = topologyTemplateOperation
2070             .associateOrAddCalcCapReqToComponent(vertex, calcRequirements, calcCapabilty, calculatedCapabilitiesProperties);
2071         updateInstancesCapAndReqOnComponentFromDB(component);
2072         return storageOperationStatus;
2073     }
2074
2075     public StorageOperationStatus updateCalculatedCapabilitiesRequirements(
2076         final Map<ComponentInstance, Map<String, List<CapabilityDefinition>>> instCapabilties,
2077         final Map<ComponentInstance, Map<String, List<RequirementDefinition>>> instReg,
2078         final Component component) {
2079         StorageOperationStatus storageOperationStatus = StorageOperationStatus.OK;
2080         if (instCapabilties != null) {
2081             for (Entry<ComponentInstance, Map<String, List<CapabilityDefinition>>> entry : instCapabilties.entrySet()) {
2082                 final Map<String, List<CapabilityDefinition>> cap = entry.getValue();
2083                 for (List<CapabilityDefinition> capabilityList : cap.values()) {
2084                     for (CapabilityDefinition capability : capabilityList) {
2085                         nodeTemplateOperation.updateComponentInstanceCapabilities(component.getUniqueId(), entry.getKey().getUniqueId(), capability);
2086                     }
2087                 }
2088             }
2089         }
2090         if (instReg != null) {
2091             for (Entry<ComponentInstance, Map<String, List<RequirementDefinition>>> entry : instReg.entrySet()) {
2092                 final Map<String, List<RequirementDefinition>> req = entry.getValue();
2093                 for (List<RequirementDefinition> requirementList : req.values()) {
2094                     for (RequirementDefinition requirement : requirementList) {
2095                         storageOperationStatus = nodeTemplateOperation.updateComponentInstanceRequirement(component.getUniqueId(),
2096                             entry.getKey().getUniqueId(), requirement);
2097                         if (storageOperationStatus != StorageOperationStatus.OK) {
2098                             return storageOperationStatus;
2099                         }
2100                     }
2101                 }
2102             }
2103         }
2104         return storageOperationStatus;
2105     }
2106
2107     private void updateInstancesCapAndReqOnComponentFromDB(Component component) {
2108         ComponentParametersView componentParametersView = new ComponentParametersView(true);
2109         componentParametersView.setIgnoreCapabilities(false);
2110         componentParametersView.setIgnoreRequirements(false);
2111         componentParametersView.setIgnoreCapabiltyProperties(false);
2112         componentParametersView.setIgnoreComponentInstances(false);
2113         Either<Component, StorageOperationStatus> componentEither = getToscaElement(component.getUniqueId(), componentParametersView);
2114         if (componentEither.isRight()) {
2115             throw new StorageException(StorageOperationStatus.NOT_FOUND);
2116         }
2117         Component updatedComponent = componentEither.left().value();
2118         component.setCapabilities(updatedComponent.getCapabilities());
2119         component.setRequirements(updatedComponent.getRequirements());
2120         component.setComponentInstancesRelations(updatedComponent.getComponentInstancesRelations());
2121         component.setComponentInstances(updatedComponent.getComponentInstances());
2122     }
2123
2124     private Either<List<Service>, StorageOperationStatus> getLatestVersionNonCheckoutServicesMetadataOnly(Map<GraphPropertyEnum, Object> hasProps,
2125                                                                                                           Map<GraphPropertyEnum, Object> hasNotProps,
2126                                                                                                           String modelName) {
2127         List<Service> services = new ArrayList<>();
2128         List<LifecycleStateEnum> states = new ArrayList<>();
2129         // include props
2130         hasProps.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
2131         hasProps.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
2132         if (modelName != null) {
2133             hasProps.put(GraphPropertyEnum.MODEL, modelName);
2134         }
2135         // exclude props
2136         states.add(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
2137         hasNotProps.put(GraphPropertyEnum.STATE, states);
2138         hasNotProps.put(GraphPropertyEnum.IS_DELETED, true);
2139         hasNotProps.put(GraphPropertyEnum.IS_ARCHIVED, true);
2140         return fetchServicesByCriteria(services, hasProps, hasNotProps, modelName);
2141     }
2142
2143     private Either<List<Component>, StorageOperationStatus> getLatestVersionNotAbstractToscaElementsMetadataOnly(final boolean isAbstract,
2144                                                                                                                  final ComponentTypeEnum componentTypeEnum,
2145                                                                                                                  final String internalComponentType,
2146                                                                                                                  final VertexTypeEnum vertexType,
2147                                                                                                                  final String modelName,
2148                                                                                                                  final boolean includeNormativeExtensionModels) {
2149         List<Service> services = null;
2150         Map<GraphPropertyEnum, Object> hasProps = new EnumMap<>(GraphPropertyEnum.class);
2151         Map<GraphPropertyEnum, Object> hasNotProps = new EnumMap<>(GraphPropertyEnum.class);
2152         fillPropsMap(hasProps, hasNotProps, internalComponentType, componentTypeEnum, isAbstract, vertexType, modelName);
2153         Either<List<GraphVertex>, JanusGraphOperationStatus> getRes = janusGraphDao
2154             .getByCriteria(vertexType, hasProps, hasNotProps, JsonParseFlagEnum.ParseMetadata, modelName, includeNormativeExtensionModels);
2155         if (getRes.isRight() && !JanusGraphOperationStatus.NOT_FOUND.equals(getRes.right().value())) {
2156             return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getRes.right().value()));
2157         }
2158         // region -> Fetch non checked-out services
2159         if (internalComponentType != null && internalComponentType.toLowerCase().trim().equals(SERVICE) && VertexTypeEnum.NODE_TYPE == vertexType) {
2160             Either<List<Service>, StorageOperationStatus> result = getLatestVersionNonCheckoutServicesMetadataOnly(
2161                 new EnumMap<>(GraphPropertyEnum.class), new EnumMap<>(GraphPropertyEnum.class), modelName);
2162             if (result.isRight()) {
2163                 log.debug("Failed to fetch services for");
2164                 return Either.right(result.right().value());
2165             }
2166             services = result.left().value();
2167             if (log.isTraceEnabled() && isEmpty(services)) {
2168                 log.trace("No relevant services available");
2169             }
2170         }
2171         // endregion
2172         List<Component> nonAbstractLatestComponents = new ArrayList<>();
2173         ComponentParametersView params = new ComponentParametersView(true);
2174         params.setIgnoreAllVersions(false);
2175         if (getRes.isLeft()) {
2176             for (GraphVertex vertexComponent : getRes.left().value()) {
2177                 Either<ToscaElement, StorageOperationStatus> componentRes = topologyTemplateOperation
2178                     .getLightComponent(vertexComponent, componentTypeEnum, params);
2179                 if (componentRes.isRight()) {
2180                     log.debug("Failed to fetch light element for {} error {}", vertexComponent.getUniqueId(), componentRes.right().value());
2181                     return Either.right(componentRes.right().value());
2182                 } else {
2183                     Component component = ModelConverter.convertFromToscaElement(componentRes.left().value());
2184                     nonAbstractLatestComponents.add(component);
2185                 }
2186             }
2187         }
2188         if (CollectionUtils.isNotEmpty(services)) {
2189             nonAbstractLatestComponents.addAll(services);
2190         }
2191         return Either.left(nonAbstractLatestComponents);
2192     }
2193
2194     public Either<ComponentMetadataData, StorageOperationStatus> getLatestComponentMetadataByUuid(String componentUuid, JsonParseFlagEnum parseFlag,
2195                                                                                                   Boolean isHighest) {
2196         Either<ComponentMetadataData, StorageOperationStatus> result;
2197         Map<GraphPropertyEnum, Object> hasProperties = new EnumMap<>(GraphPropertyEnum.class);
2198         hasProperties.put(GraphPropertyEnum.UUID, componentUuid);
2199         if (isHighest != null) {
2200             hasProperties.put(GraphPropertyEnum.IS_HIGHEST_VERSION, isHighest);
2201         }
2202         Map<GraphPropertyEnum, Object> propertiesNotToMatch = new EnumMap<>(GraphPropertyEnum.class);
2203         propertiesNotToMatch.put(GraphPropertyEnum.IS_DELETED, true);
2204         propertiesNotToMatch.put(GraphPropertyEnum.IS_ARCHIVED, true); //US382674, US382683
2205         Either<List<GraphVertex>, JanusGraphOperationStatus> getRes = janusGraphDao
2206             .getByCriteria(null, hasProperties, propertiesNotToMatch, parseFlag);
2207         if (getRes.isRight()) {
2208             result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getRes.right().value()));
2209         } else {
2210             List<ComponentMetadataData> latestVersionList = getRes.left().value().stream().map(ModelConverter::convertToComponentMetadata)
2211                 .collect(Collectors.toList());
2212             ComponentMetadataData latestVersion = latestVersionList.size() == 1 ? latestVersionList.get(0) : latestVersionList.stream().max(
2213                 (c1, c2) -> Double.compare(Double.parseDouble(c1.getMetadataDataDefinition().getVersion()),
2214                     Double.parseDouble(c2.getMetadataDataDefinition().getVersion()))).get();
2215             result = Either.left(latestVersion);
2216         }
2217         return result;
2218     }
2219
2220     public Either<ComponentMetadataData, StorageOperationStatus> getComponentMetadata(String componentId) {
2221         Either<ComponentMetadataData, StorageOperationStatus> result;
2222         Either<GraphVertex, JanusGraphOperationStatus> getRes = janusGraphDao.getVertexById(componentId, JsonParseFlagEnum.ParseMetadata);
2223         if (getRes.isRight()) {
2224             result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getRes.right().value()));
2225         } else {
2226             ComponentMetadataData componentMetadata = ModelConverter.convertToComponentMetadata(getRes.left().value());
2227             result = Either.left(componentMetadata);
2228         }
2229         return result;
2230     }
2231
2232     public Either<List<Component>, StorageOperationStatus> getLatestVersionNotAbstractComponents(boolean isAbstract,
2233                                                                                                  ComponentTypeEnum componentTypeEnum,
2234                                                                                                  String internalComponentType,
2235                                                                                                  List<String> componentUids) {
2236         List<Component> components = new ArrayList<>();
2237         if (componentUids == null) {
2238             Either<List<String>, StorageOperationStatus> componentUidsRes = getComponentUids(isAbstract, componentTypeEnum, internalComponentType);
2239             if (componentUidsRes.isRight()) {
2240                 return Either.right(componentUidsRes.right().value());
2241             }
2242             componentUids = componentUidsRes.left().value();
2243         }
2244         if (!isEmpty(componentUids)) {
2245             for (String componentUid : componentUids) {
2246                 ComponentParametersView componentParametersView = buildComponentViewForNotAbstract();
2247                 if ("vl".equalsIgnoreCase(internalComponentType)) {
2248                     componentParametersView.setIgnoreCapabilities(false);
2249                     componentParametersView.setIgnoreRequirements(false);
2250                 }
2251                 Either<ToscaElement, StorageOperationStatus> getToscaElementRes = nodeTemplateOperation.getToscaElementOperation(componentTypeEnum)
2252                     .getLightComponent(componentUid, componentTypeEnum, componentParametersView);
2253                 if (getToscaElementRes.isRight()) {
2254                     log.debug("Failed to fetch resource for error is {}", getToscaElementRes.right().value());
2255                     return Either.right(getToscaElementRes.right().value());
2256                 }
2257                 Component component = ModelConverter.convertFromToscaElement(getToscaElementRes.left().value());
2258                 nullifySomeComponentProperties(component);
2259                 components.add(component);
2260             }
2261         }
2262         return Either.left(components);
2263     }
2264
2265     public void nullifySomeComponentProperties(Component component) {
2266         component.setContactId(null);
2267         component.setCreationDate(null);
2268         component.setCreatorUserId(null);
2269         component.setCreatorFullName(null);
2270         component.setLastUpdateDate(null);
2271         component.setLastUpdaterUserId(null);
2272         component.setLastUpdaterFullName(null);
2273         component.setNormalizedName(null);
2274     }
2275
2276     private Either<List<String>, StorageOperationStatus> getComponentUids(boolean isAbstract, ComponentTypeEnum componentTypeEnum,
2277                                                                           String internalComponentType) {
2278         Either<List<Component>, StorageOperationStatus> getToscaElementsRes = getLatestVersionNotAbstractMetadataOnly(isAbstract, componentTypeEnum,
2279             internalComponentType, null, false);
2280         if (getToscaElementsRes.isRight()) {
2281             return Either.right(getToscaElementsRes.right().value());
2282         }
2283         List<Component> collection = getToscaElementsRes.left().value();
2284         List<String> componentUids;
2285         if (collection == null) {
2286             componentUids = new ArrayList<>();
2287         } else {
2288             componentUids = collection.stream().map(Component::getUniqueId).collect(Collectors.toList());
2289         }
2290         return Either.left(componentUids);
2291     }
2292
2293     private ComponentParametersView buildComponentViewForNotAbstract() {
2294         ComponentParametersView componentParametersView = new ComponentParametersView();
2295         componentParametersView.disableAll();
2296         componentParametersView.setIgnoreCategories(false);
2297         componentParametersView.setIgnoreAllVersions(false);
2298         return componentParametersView;
2299     }
2300
2301     public Either<Boolean, StorageOperationStatus> validateComponentNameExists(String name, ResourceTypeEnum resourceType,
2302                                                                                ComponentTypeEnum componentType) {
2303         Either<Boolean, StorageOperationStatus> result = validateComponentNameUniqueness(name, resourceType, componentType);
2304         if (result.isLeft()) {
2305             result = Either.left(!result.left().value());
2306         }
2307         return result;
2308     }
2309
2310     public Either<Boolean, StorageOperationStatus> validateComponentNameUniqueness(String name, ResourceTypeEnum resourceType,
2311                                                                                    ComponentTypeEnum componentType) {
2312         String normalizedName = ValidationUtils.normaliseComponentName(name);
2313         Either<List<GraphVertex>, JanusGraphOperationStatus> vertexEither = janusGraphDao
2314             .getByCriteria(getVertexTypeEnum(resourceType), propertiesToMatch(normalizedName, componentType), JsonParseFlagEnum.NoParse);
2315         if (vertexEither.isRight() && vertexEither.right().value() != JanusGraphOperationStatus.NOT_FOUND) {
2316             log.debug("failed to get vertex from graph with property normalizedName: {}", normalizedName);
2317             return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(vertexEither.right().value()));
2318         }
2319         return Either.left(CollectionUtils.isEmpty(vertexEither.isLeft() ? vertexEither.left().value() : null));
2320     }
2321
2322     public Either<Boolean, StorageOperationStatus> validateComponentNameAndModelExists(final String resourceName, final String model,
2323                                                                                        final ResourceTypeEnum resourceType,
2324                                                                                        final ComponentTypeEnum componentType) {
2325         Either<Boolean, StorageOperationStatus> result = validateComponentNameAndModelUniqueness(resourceName, model, resourceType, componentType);
2326         if (result.isLeft()) {
2327             result = Either.left(!result.left().value());
2328         }
2329         return result;
2330     }
2331
2332     private Either<Boolean, StorageOperationStatus> validateComponentNameAndModelUniqueness(final String resourceName, final String modelName,
2333                                                                                             final ResourceTypeEnum resourceType,
2334                                                                                             final ComponentTypeEnum componentType) {
2335         final String normalizedName = ValidationUtils.normaliseComponentName(resourceName);
2336         final Either<List<GraphVertex>, JanusGraphOperationStatus> vertexEither = janusGraphDao
2337             .getByCriteria(getVertexTypeEnum(resourceType), propertiesToMatch(normalizedName, componentType), null, null, JsonParseFlagEnum.NoParse,
2338                 modelName);
2339         if (vertexEither.isRight() && vertexEither.right().value() != JanusGraphOperationStatus.NOT_FOUND) {
2340             log.debug("failed to get vertex from graph with property normalizedName: {} and model: {}", normalizedName, modelName);
2341             return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(vertexEither.right().value()));
2342         }
2343         return Either.left(CollectionUtils.isEmpty(vertexEither.isLeft() ? vertexEither.left().value().stream()
2344             .collect(Collectors.toList()) : null));
2345     }
2346
2347     private VertexTypeEnum getVertexTypeEnum(final ResourceTypeEnum resourceType) {
2348         return ModelConverter.isAtomicComponent(resourceType) ? VertexTypeEnum.NODE_TYPE : TOPOLOGY_TEMPLATE;
2349     }
2350
2351     private Map<GraphPropertyEnum, Object> propertiesToMatch(final String normalizedName, final ComponentTypeEnum componentType) {
2352         final Map<GraphPropertyEnum, Object> properties = new EnumMap<>(GraphPropertyEnum.class);
2353         properties.put(GraphPropertyEnum.NORMALIZED_NAME, normalizedName);
2354         properties.put(GraphPropertyEnum.COMPONENT_TYPE, componentType.name());
2355         return properties;
2356     }
2357
2358     private void fillNodeTypePropsMap(final Map<GraphPropertyEnum, Object> hasProps, final Map<GraphPropertyEnum, Object> hasNotProps,
2359                                       final String internalComponentType, String modelName) {
2360         final Configuration configuration = ConfigurationManager.getConfigurationManager().getConfiguration();
2361         final List<String> allowedTypes;
2362         if (ComponentTypeEnum.SERVICE.getValue().equalsIgnoreCase(internalComponentType)) {
2363             allowedTypes = containerInstanceTypesData.getServiceAllowedList(modelName);
2364         } else {
2365             final ResourceTypeEnum resourceType = ResourceTypeEnum.getTypeIgnoreCase(internalComponentType);
2366             allowedTypes = containerInstanceTypesData.getComponentAllowedList(ComponentTypeEnum.RESOURCE, resourceType);
2367         }
2368         final List<String> allResourceTypes = configuration.getResourceTypes();
2369         if (allowedTypes == null) {
2370             hasNotProps.put(GraphPropertyEnum.RESOURCE_TYPE, allResourceTypes);
2371             return;
2372         }
2373         if (ResourceTypeEnum.VL.getValue().equalsIgnoreCase(internalComponentType)) {
2374             hasProps.put(GraphPropertyEnum.RESOURCE_TYPE, allowedTypes);
2375         } else {
2376             final List<String> notAllowedTypes = allResourceTypes.stream().filter(s -> !allowedTypes.contains(s)).collect(Collectors.toList());
2377             hasNotProps.put(GraphPropertyEnum.RESOURCE_TYPE, notAllowedTypes);
2378         }
2379     }
2380
2381     private void fillTopologyTemplatePropsMap(Map<GraphPropertyEnum, Object> hasProps, Map<GraphPropertyEnum, Object> hasNotProps,
2382                                               ComponentTypeEnum componentTypeEnum) {
2383         switch (componentTypeEnum) {
2384             case RESOURCE:
2385                 hasProps.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
2386                 break;
2387             case SERVICE:
2388                 hasProps.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
2389                 break;
2390             default:
2391                 break;
2392         }
2393         hasNotProps.put(GraphPropertyEnum.RESOURCE_TYPE, ResourceTypeEnum.CVFC.name());
2394     }
2395
2396     private void fillPropsMap(Map<GraphPropertyEnum, Object> hasProps, Map<GraphPropertyEnum, Object> hasNotProps, String internalComponentType,
2397                               ComponentTypeEnum componentTypeEnum, boolean isAbstract, VertexTypeEnum internalVertexType, String modelName) {
2398         hasNotProps.put(GraphPropertyEnum.STATE, LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT.name());
2399         hasNotProps.put(GraphPropertyEnum.IS_DELETED, true);
2400         hasNotProps.put(GraphPropertyEnum.IS_ARCHIVED, true);
2401         hasProps.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
2402
2403         if (VertexTypeEnum.NODE_TYPE == internalVertexType) {
2404             hasProps.put(GraphPropertyEnum.IS_ABSTRACT, isAbstract);
2405             if (internalComponentType != null) {
2406                 fillNodeTypePropsMap(hasProps, hasNotProps, internalComponentType, modelName);
2407             }
2408         } else {
2409             fillTopologyTemplatePropsMap(hasProps, hasNotProps, componentTypeEnum);
2410         }
2411     }
2412
2413     private List<VertexTypeEnum> getInternalVertexTypes(ComponentTypeEnum componentTypeEnum, String internalComponentType) {
2414         List<VertexTypeEnum> internalVertexTypes = new ArrayList<>();
2415         if (ComponentTypeEnum.RESOURCE == componentTypeEnum) {
2416             internalVertexTypes.add(VertexTypeEnum.NODE_TYPE);
2417         }
2418         if (ComponentTypeEnum.SERVICE == componentTypeEnum || SERVICE.equalsIgnoreCase(internalComponentType) || VF.equalsIgnoreCase(
2419             internalComponentType)) {
2420             internalVertexTypes.add(TOPOLOGY_TEMPLATE);
2421         }
2422         return internalVertexTypes;
2423     }
2424
2425     public Either<List<Component>, StorageOperationStatus> getLatestVersionNotAbstractMetadataOnly(boolean isAbstract,
2426                                                                                                    final ComponentTypeEnum componentTypeEnum,
2427                                                                                                    final String internalComponentType,
2428                                                                                                    final String modelName,
2429                                                                                                    final boolean includeNormativeExtensionModels) {
2430         List<VertexTypeEnum> internalVertexTypes = getInternalVertexTypes(componentTypeEnum, internalComponentType);
2431         List<Component> result = new ArrayList<>();
2432         for (VertexTypeEnum vertexType : internalVertexTypes) {
2433             Either<List<Component>, StorageOperationStatus> listByVertexType = getLatestVersionNotAbstractToscaElementsMetadataOnly(isAbstract,
2434                 componentTypeEnum, internalComponentType, vertexType, modelName, includeNormativeExtensionModels);
2435             if (listByVertexType.isRight()) {
2436                 return listByVertexType;
2437             }
2438             result.addAll(listByVertexType.left().value());
2439         }
2440         return Either.left(result);
2441     }
2442
2443     private Either<List<Component>, StorageOperationStatus> getLatestComponentListByUuid(String componentUuid,
2444                                                                                          Map<GraphPropertyEnum, Object> additionalPropertiesToMatch) {
2445         Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
2446         if (additionalPropertiesToMatch != null) {
2447             propertiesToMatch.putAll(additionalPropertiesToMatch);
2448         }
2449         propertiesToMatch.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
2450         return getComponentListByUuid(componentUuid, propertiesToMatch);
2451     }
2452
2453     public Either<Component, StorageOperationStatus> getComponentByUuidAndVersion(String componentUuid, String version) {
2454         Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
2455         propertiesToMatch.put(GraphPropertyEnum.UUID, componentUuid);
2456         propertiesToMatch.put(GraphPropertyEnum.VERSION, version);
2457         Map<GraphPropertyEnum, Object> propertiesNotToMatch = new EnumMap<>(GraphPropertyEnum.class);
2458         propertiesNotToMatch.put(GraphPropertyEnum.IS_DELETED, true);
2459         Either<List<GraphVertex>, JanusGraphOperationStatus> vertexEither = janusGraphDao
2460             .getByCriteria(null, propertiesToMatch, propertiesNotToMatch, JsonParseFlagEnum.ParseAll);
2461         if (vertexEither.isRight()) {
2462             return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(vertexEither.right().value()));
2463         }
2464         List<GraphVertex> vertexList = vertexEither.isLeft() ? vertexEither.left().value() : null;
2465         if (vertexList == null || vertexList.isEmpty() || vertexList.size() > 1) {
2466             return Either.right(StorageOperationStatus.NOT_FOUND);
2467         }
2468         return getToscaElementByOperation(vertexList.get(0));
2469     }
2470
2471     public Either<List<Component>, StorageOperationStatus> getComponentListByUuid(String componentUuid,
2472                                                                                   Map<GraphPropertyEnum, Object> additionalPropertiesToMatch) {
2473         Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
2474         if (additionalPropertiesToMatch != null) {
2475             propertiesToMatch.putAll(additionalPropertiesToMatch);
2476         }
2477         propertiesToMatch.put(GraphPropertyEnum.UUID, componentUuid);
2478         Map<GraphPropertyEnum, Object> propertiesNotToMatch = new EnumMap<>(GraphPropertyEnum.class);
2479         propertiesNotToMatch.put(GraphPropertyEnum.IS_DELETED, true);
2480         propertiesNotToMatch.put(GraphPropertyEnum.IS_ARCHIVED, true); //US382674, US382683
2481         Either<List<GraphVertex>, JanusGraphOperationStatus> vertexEither = janusGraphDao
2482             .getByCriteria(null, propertiesToMatch, propertiesNotToMatch, JsonParseFlagEnum.ParseAll);
2483         if (vertexEither.isRight()) {
2484             log.debug("Couldn't fetch metadata for component with uuid {}, error: {}", componentUuid, vertexEither.right().value());
2485             return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(vertexEither.right().value()));
2486         }
2487         List<GraphVertex> vertexList = vertexEither.isLeft() ? vertexEither.left().value() : null;
2488         if (vertexList == null || vertexList.isEmpty()) {
2489             log.debug("Component with uuid {} was not found", componentUuid);
2490             return Either.right(StorageOperationStatus.NOT_FOUND);
2491         }
2492         ArrayList<Component> latestComponents = new ArrayList<>();
2493         for (GraphVertex vertex : vertexList) {
2494             Either<Component, StorageOperationStatus> toscaElementByOperation = getToscaElementByOperation(vertex);
2495             if (toscaElementByOperation.isRight()) {
2496                 log.debug("Could not fetch the following Component by UUID {}", vertex.getUniqueId());
2497                 return Either.right(toscaElementByOperation.right().value());
2498             }
2499             latestComponents.add(toscaElementByOperation.left().value());
2500         }
2501         if (latestComponents.size() > 1) {
2502             for (Component component : latestComponents) {
2503                 if (Boolean.TRUE.equals(component.isHighestVersion())) {
2504                     LinkedList<Component> highestComponent = new LinkedList<>();
2505                     highestComponent.add(component);
2506                     return Either.left(highestComponent);
2507                 }
2508             }
2509         }
2510         return Either.left(latestComponents);
2511     }
2512
2513     public Either<Component, StorageOperationStatus> getLatestServiceByUuid(String serviceUuid) {
2514         Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
2515         propertiesToMatch.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
2516         return getLatestComponentByUuid(serviceUuid, propertiesToMatch);
2517     }
2518
2519     public Either<Component, StorageOperationStatus> getLatestComponentByUuid(String componentUuid) {
2520         return getLatestComponentByUuid(componentUuid, null);
2521     }
2522
2523     public Either<Component, StorageOperationStatus> getLatestComponentByUuid(String componentUuid,
2524                                                                               Map<GraphPropertyEnum, Object> propertiesToMatch) {
2525         Either<List<Component>, StorageOperationStatus> latestVersionListEither = getLatestComponentListByUuid(componentUuid, propertiesToMatch);
2526         if (latestVersionListEither.isRight()) {
2527             return Either.right(latestVersionListEither.right().value());
2528         }
2529         List<Component> latestVersionList = latestVersionListEither.left().value();
2530         if (latestVersionList.isEmpty()) {
2531             return Either.right(StorageOperationStatus.NOT_FOUND);
2532         }
2533         Component component = latestVersionList.size() == 1 ? latestVersionList.get(0)
2534             : latestVersionList.stream().max((c1, c2) -> Double.compare(Double.parseDouble(c1.getVersion()), Double.parseDouble(c2.getVersion())))
2535                 .get();
2536         return Either.left(component);
2537     }
2538
2539     public Either<List<Resource>, StorageOperationStatus> getAllCertifiedResources(boolean isAbstract, Boolean isHighest) {
2540         List<Resource> resources = new ArrayList<>();
2541         Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
2542         Map<GraphPropertyEnum, Object> propertiesNotToMatch = new EnumMap<>(GraphPropertyEnum.class);
2543         propertiesToMatch.put(GraphPropertyEnum.IS_ABSTRACT, isAbstract);
2544         if (isHighest != null) {
2545             propertiesToMatch.put(GraphPropertyEnum.IS_HIGHEST_VERSION, isHighest);
2546         }
2547         propertiesToMatch.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
2548         propertiesToMatch.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
2549         propertiesNotToMatch.put(GraphPropertyEnum.IS_DELETED, true);
2550         Either<List<GraphVertex>, JanusGraphOperationStatus> getResourcesRes = janusGraphDao
2551             .getByCriteria(null, propertiesToMatch, propertiesNotToMatch, JsonParseFlagEnum.ParseAll);
2552         if (getResourcesRes.isRight()) {
2553             log.debug("Failed to fetch all certified resources. Status is {}", getResourcesRes.right().value());
2554             return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getResourcesRes.right().value()));
2555         }
2556         List<GraphVertex> resourceVerticies = getResourcesRes.left().value();
2557         for (GraphVertex resourceV : resourceVerticies) {
2558             Either<Resource, StorageOperationStatus> getResourceRes = getToscaElement(resourceV);
2559             if (getResourceRes.isRight()) {
2560                 return Either.right(getResourceRes.right().value());
2561             }
2562             resources.add(getResourceRes.left().value());
2563         }
2564         return Either.left(resources);
2565     }
2566
2567     public <T extends Component> Either<T, StorageOperationStatus> getLatestByNameAndVersion(String name, String version,
2568                                                                                              JsonParseFlagEnum parseFlag, String model) {
2569         Either<T, StorageOperationStatus> result;
2570         Map<GraphPropertyEnum, Object> hasProperties = new EnumMap<>(GraphPropertyEnum.class);
2571         Map<GraphPropertyEnum, Object> hasNotProperties = new EnumMap<>(GraphPropertyEnum.class);
2572         hasProperties.put(GraphPropertyEnum.NAME, name);
2573         hasProperties.put(GraphPropertyEnum.VERSION, version);
2574         hasProperties.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
2575         hasNotProperties.put(GraphPropertyEnum.IS_DELETED, true);
2576         Either<List<GraphVertex>, JanusGraphOperationStatus> getResourceRes = janusGraphDao
2577             .getByCriteria(null, hasProperties, hasNotProperties, parseFlag, model);
2578         if (getResourceRes.isRight()) {
2579             JanusGraphOperationStatus status = getResourceRes.right().value();
2580             log.debug("failed to find resource with name {}, version {}. Status is {} ", name, version, status);
2581             result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
2582             return result;
2583         }
2584         return getToscaElementByOperation(getResourceRes.left().value().get(0));
2585     }
2586
2587     public Either<Resource, StorageOperationStatus> getLatestComponentByCsarOrName(ComponentTypeEnum componentType, String csarUUID,
2588                                                                                    String systemName) {
2589         return getLatestComponentByCsarOrName(componentType, csarUUID, systemName, JsonParseFlagEnum.ParseAll);
2590     }
2591
2592     public Either<Resource, StorageOperationStatus> getLatestComponentByCsarOrName(ComponentTypeEnum componentType, String csarUUID,
2593                                                                                    String systemName, JsonParseFlagEnum parseFlag) {
2594         Map<GraphPropertyEnum, Object> props = new EnumMap<>(GraphPropertyEnum.class);
2595         Map<GraphPropertyEnum, Object> propsHasNot = new EnumMap<>(GraphPropertyEnum.class);
2596         props.put(GraphPropertyEnum.CSAR_UUID, csarUUID);
2597         props.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
2598         if (componentType != null) {
2599             props.put(GraphPropertyEnum.COMPONENT_TYPE, componentType.name());
2600         }
2601         propsHasNot.put(GraphPropertyEnum.IS_DELETED, true);
2602         GraphVertex resourceMetadataData = null;
2603         List<GraphVertex> resourceMetadataDataList = null;
2604         Either<List<GraphVertex>, JanusGraphOperationStatus> byCsar = janusGraphDao
2605             .getByCriteria(null, props, propsHasNot, JsonParseFlagEnum.ParseMetadata);
2606         if (byCsar.isRight()) {
2607             if (JanusGraphOperationStatus.NOT_FOUND == byCsar.right().value()) {
2608                 // Fix Defect DE256036
2609                 if (StringUtils.isEmpty(systemName)) {
2610                     return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(JanusGraphOperationStatus.NOT_FOUND));
2611                 }
2612                 props.clear();
2613                 props.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
2614                 props.put(GraphPropertyEnum.SYSTEM_NAME, systemName);
2615                 Either<List<GraphVertex>, JanusGraphOperationStatus> bySystemname = janusGraphDao
2616                     .getByCriteria(null, props, JsonParseFlagEnum.ParseMetadata);
2617                 if (bySystemname.isRight()) {
2618                     log.debug("getLatestResourceByCsarOrName - Failed to find by system name {}  error {} ", systemName,
2619                         bySystemname.right().value());
2620                     return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(bySystemname.right().value()));
2621                 }
2622                 if (bySystemname.left().value().size() > 2) {
2623                     log.debug(
2624                         "getLatestResourceByCsarOrName - getByCriteria(by system name) must return only 2 latest version, but was returned - {}",
2625                         bySystemname.left().value().size());
2626                     return Either.right(StorageOperationStatus.GENERAL_ERROR);
2627                 }
2628                 resourceMetadataDataList = bySystemname.left().value();
2629                 if (resourceMetadataDataList.size() == 1) {
2630                     resourceMetadataData = resourceMetadataDataList.get(0);
2631                 } else {
2632                     for (GraphVertex curResource : resourceMetadataDataList) {
2633                         if (!((String) curResource.getJsonMetadataField(JsonPresentationFields.LIFECYCLE_STATE)).equals("CERTIFIED")) {
2634                             resourceMetadataData = curResource;
2635                             break;
2636                         }
2637                     }
2638                 }
2639                 if (resourceMetadataData == null) {
2640                     log.debug("getLatestResourceByCsarOrName - getByCriteria(by system name) returned 2 latest CERTIFIED versions");
2641                     return Either.right(StorageOperationStatus.GENERAL_ERROR);
2642                 }
2643                 final Object csarUuid = resourceMetadataData.getJsonMetadataField(JsonPresentationFields.CSAR_UUID);
2644                 if (csarUuid != null && !csarUuid.equals(csarUUID)) {
2645                     log.debug("getLatestResourceByCsarOrName - same system name {} but different csarUUID. exist {} and new {} ", systemName,
2646                         csarUuid, csarUUID);
2647                     // correct error will be returned from create flow. with all
2648
2649                     // correct audit records!!!!!
2650                     return Either.right(StorageOperationStatus.NOT_FOUND);
2651                 }
2652                 return getToscaElement(resourceMetadataData.getUniqueId());
2653             }
2654         } else {
2655             resourceMetadataDataList = byCsar.left().value();
2656             if (resourceMetadataDataList.size() > 2) {
2657                 log.debug("getLatestResourceByCsarOrName - getByCriteria(by csar) must return only 2 latest version, but was returned - {}",
2658                     byCsar.left().value().size());
2659                 return Either.right(StorageOperationStatus.GENERAL_ERROR);
2660             }
2661             if (resourceMetadataDataList.size() == 1) {
2662                 resourceMetadataData = resourceMetadataDataList.get(0);
2663             } else {
2664                 for (GraphVertex curResource : resourceMetadataDataList) {
2665                     if (!((String) curResource.getJsonMetadataField(JsonPresentationFields.LIFECYCLE_STATE)).equals("CERTIFIED")) {
2666                         resourceMetadataData = curResource;
2667                         break;
2668                     }
2669                 }
2670             }
2671             if (resourceMetadataData == null) {
2672                 log.debug("getLatestResourceByCsarOrName - getByCriteria(by csar) returned 2 latest CERTIFIED versions");
2673                 return Either.right(StorageOperationStatus.GENERAL_ERROR);
2674             }
2675             return getToscaElement((String) resourceMetadataData.getJsonMetadataField(JsonPresentationFields.UNIQUE_ID), parseFlag);
2676         }
2677         return null;
2678     }
2679
2680     public Either<Boolean, StorageOperationStatus> validateToscaResourceNameExtends(String templateNameCurrent, String templateNameExtends,
2681                                                                                     String model) {
2682         String currentTemplateNameChecked = templateNameExtends;
2683         while (currentTemplateNameChecked != null && !currentTemplateNameChecked.equalsIgnoreCase(templateNameCurrent)) {
2684             Either<Resource, StorageOperationStatus> latestByToscaResourceName = getLatestByToscaResourceName(currentTemplateNameChecked, model);
2685             if (latestByToscaResourceName.isRight()) {
2686                 return latestByToscaResourceName.right().value() == StorageOperationStatus.NOT_FOUND ? Either.left(false)
2687                     : Either.right(latestByToscaResourceName.right().value());
2688             }
2689             Resource value = latestByToscaResourceName.left().value();
2690             if (value.getDerivedFrom() != null) {
2691                 currentTemplateNameChecked = value.getDerivedFrom().get(0);
2692             } else {
2693                 currentTemplateNameChecked = null;
2694             }
2695         }
2696         return (currentTemplateNameChecked != null && currentTemplateNameChecked.equalsIgnoreCase(templateNameCurrent)) ? Either.left(true)
2697             : Either.left(false);
2698     }
2699
2700     public Either<List<Component>, StorageOperationStatus> fetchMetaDataByResourceType(String resourceType, ComponentParametersView filterBy) {
2701         Map<GraphPropertyEnum, Object> props = new EnumMap<>(GraphPropertyEnum.class);
2702         props.put(GraphPropertyEnum.RESOURCE_TYPE, resourceType);
2703         props.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
2704         Map<GraphPropertyEnum, Object> propsHasNotToMatch = new EnumMap<>(GraphPropertyEnum.class);
2705         propsHasNotToMatch.put(GraphPropertyEnum.IS_DELETED, true);
2706         Either<List<GraphVertex>, JanusGraphOperationStatus> resourcesByTypeEither = janusGraphDao
2707             .getByCriteria(null, props, propsHasNotToMatch, JsonParseFlagEnum.ParseMetadata);
2708         if (resourcesByTypeEither.isRight()) {
2709             return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(resourcesByTypeEither.right().value()));
2710         }
2711         List<GraphVertex> vertexList = resourcesByTypeEither.left().value();
2712         List<Component> components = new ArrayList<>();
2713         for (GraphVertex vertex : vertexList) {
2714             components.add(getToscaElementByOperation(vertex, filterBy).left().value());
2715         }
2716         return Either.left(components);
2717     }
2718
2719     public void commit() {
2720         janusGraphDao.commit();
2721     }
2722
2723     public Either<Service, StorageOperationStatus> updateDistributionStatus(Service service, User user, DistributionStatusEnum distributionStatus) {
2724         Either<GraphVertex, StorageOperationStatus> updateDistributionStatus = topologyTemplateOperation
2725             .updateDistributionStatus(service.getUniqueId(), user, distributionStatus);
2726         if (updateDistributionStatus.isRight()) {
2727             return Either.right(updateDistributionStatus.right().value());
2728         }
2729         GraphVertex serviceV = updateDistributionStatus.left().value();
2730         service.setDistributionStatus(distributionStatus);
2731         service.setLastUpdateDate((Long) serviceV.getJsonMetadataField(JsonPresentationFields.LAST_UPDATE_DATE));
2732         return Either.left(service);
2733     }
2734
2735     public Either<ComponentMetadataData, StorageOperationStatus> updateComponentLastUpdateDateOnGraph(Component component) {
2736         Either<ComponentMetadataData, StorageOperationStatus> result = null;
2737         GraphVertex serviceVertex;
2738         Either<GraphVertex, JanusGraphOperationStatus> updateRes = null;
2739         Either<GraphVertex, JanusGraphOperationStatus> getRes = janusGraphDao.getVertexById(component.getUniqueId(), JsonParseFlagEnum.ParseMetadata);
2740         if (getRes.isRight()) {
2741             JanusGraphOperationStatus status = getRes.right().value();
2742             log.error("Failed to fetch component {}. status is {}", component.getUniqueId(), status);
2743             result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
2744         }
2745         if (result == null) {
2746             serviceVertex = getRes.left().value();
2747             long lastUpdateDate = System.currentTimeMillis();
2748             serviceVertex.setJsonMetadataField(JsonPresentationFields.LAST_UPDATE_DATE, lastUpdateDate);
2749             component.setLastUpdateDate(lastUpdateDate);
2750             updateRes = janusGraphDao.updateVertex(serviceVertex);
2751             if (updateRes.isRight()) {
2752                 result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(updateRes.right().value()));
2753             }
2754         }
2755         if (result == null) {
2756             result = Either.left(ModelConverter.convertToComponentMetadata(updateRes.left().value()));
2757         }
2758         return result;
2759     }
2760
2761     public HealingJanusGraphDao getJanusGraphDao() {
2762         return janusGraphDao;
2763     }
2764
2765     public Either<List<Service>, StorageOperationStatus> getCertifiedServicesWithDistStatus(Set<DistributionStatusEnum> distStatus) {
2766         Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
2767         propertiesToMatch.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
2768         return getServicesWithDistStatus(distStatus, propertiesToMatch);
2769     }
2770
2771     public Either<List<Service>, StorageOperationStatus> getServicesWithDistStatus(Set<DistributionStatusEnum> distStatus,
2772                                                                                    Map<GraphPropertyEnum, Object> additionalPropertiesToMatch) {
2773         List<Service> servicesAll = new ArrayList<>();
2774         Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
2775         Map<GraphPropertyEnum, Object> propertiesNotToMatch = new EnumMap<>(GraphPropertyEnum.class);
2776         if (additionalPropertiesToMatch != null && !additionalPropertiesToMatch.isEmpty()) {
2777             propertiesToMatch.putAll(additionalPropertiesToMatch);
2778         }
2779         propertiesToMatch.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
2780         propertiesNotToMatch.put(GraphPropertyEnum.IS_DELETED, true);
2781         if (CollectionUtils.isNotEmpty(distStatus)) {
2782             for (DistributionStatusEnum state : distStatus) {
2783                 propertiesToMatch.put(GraphPropertyEnum.DISTRIBUTION_STATUS, state.name());
2784                 Either<List<Service>, StorageOperationStatus> fetchServicesByCriteria = fetchServicesByCriteria(servicesAll, propertiesToMatch,
2785                     propertiesNotToMatch, null);
2786                 if (fetchServicesByCriteria.isRight()) {
2787                     return fetchServicesByCriteria;
2788                 } else {
2789                     servicesAll = fetchServicesByCriteria.left().value();
2790                 }
2791             }
2792             return Either.left(servicesAll);
2793         } else {
2794             return fetchServicesByCriteria(servicesAll, propertiesToMatch, propertiesNotToMatch, null);
2795         }
2796     }
2797
2798     private Either<List<Service>, StorageOperationStatus> fetchServicesByCriteria(List<Service> servicesAll,
2799                                                                                   Map<GraphPropertyEnum, Object> propertiesToMatch,
2800                                                                                   Map<GraphPropertyEnum, Object> propertiesNotToMatch,
2801                                                                                   String modelName) {
2802         Either<List<GraphVertex>, JanusGraphOperationStatus> getRes;
2803         if (StringUtils.isEmpty(modelName)) {
2804             getRes = janusGraphDao.getByCriteria(TOPOLOGY_TEMPLATE, propertiesToMatch, propertiesNotToMatch, JsonParseFlagEnum.ParseAll);
2805         } else {
2806             getRes = janusGraphDao.getByCriteria(TOPOLOGY_TEMPLATE, propertiesToMatch, propertiesNotToMatch, JsonParseFlagEnum.ParseAll, modelName);
2807         }
2808         if (getRes.isRight()) {
2809             if (getRes.right().value() != JanusGraphOperationStatus.NOT_FOUND) {
2810                 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG,
2811                     "Failed to fetch certified services by match properties {} not match properties {} . Status is {}. ", propertiesToMatch,
2812                     propertiesNotToMatch, getRes.right().value());
2813                 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getRes.right().value()));
2814             }
2815         } else {
2816             for (final GraphVertex vertex : getRes.left().value()) {
2817                 Either<ToscaElement, StorageOperationStatus> getServiceRes = topologyTemplateOperation
2818                     .getLightComponent(vertex, ComponentTypeEnum.SERVICE, new ComponentParametersView(true));
2819                 if (getServiceRes.isRight()) {
2820                     CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to fetch certified service {}. Status is {}. ",
2821                         vertex.getJsonMetadataField(JsonPresentationFields.NAME), getServiceRes.right().value());
2822                     return Either.right(getServiceRes.right().value());
2823                 } else {
2824                     servicesAll.add(ModelConverter.convertFromToscaElement(getServiceRes.left().value()));
2825                 }
2826             }
2827         }
2828         return Either.left(servicesAll);
2829     }
2830
2831     public void rollback() {
2832         janusGraphDao.rollback();
2833     }
2834
2835     public StorageOperationStatus addDeploymentArtifactsToInstance(String componentId, ComponentInstance componentInstance,
2836                                                                    Map<String, ArtifactDefinition> finalDeploymentArtifacts) {
2837         Map<String, ArtifactDataDefinition> instDeplArtifacts = finalDeploymentArtifacts.entrySet().stream()
2838             .collect(Collectors.toMap(Map.Entry::getKey, e -> new ArtifactDataDefinition(e.getValue())));
2839         return nodeTemplateOperation.addDeploymentArtifactsToInstance(componentId, componentInstance.getUniqueId(), instDeplArtifacts);
2840     }
2841
2842     public StorageOperationStatus addInformationalArtifactsToInstance(String componentId, ComponentInstance componentInstance,
2843                                                                       Map<String, ArtifactDefinition> artifacts) {
2844         StorageOperationStatus status = StorageOperationStatus.OK;
2845         if (MapUtils.isNotEmpty(artifacts)) {
2846             Map<String, ArtifactDataDefinition> instDeplArtifacts = artifacts.entrySet().stream()
2847                 .collect(Collectors.toMap(Map.Entry::getKey, e -> new ArtifactDataDefinition(e.getValue())));
2848             status = nodeTemplateOperation.addInformationalArtifactsToInstance(componentId, componentInstance.getUniqueId(), instDeplArtifacts);
2849         }
2850         return status;
2851     }
2852
2853     public StorageOperationStatus generateCustomizationUUIDOnInstance(String componentId, String instanceId) {
2854         return nodeTemplateOperation.generateCustomizationUUIDOnInstance(componentId, instanceId);
2855     }
2856
2857     public StorageOperationStatus generateCustomizationUUIDOnInstanceGroup(String componentId, String instanceId, List<String> groupInstances) {
2858         return nodeTemplateOperation.generateCustomizationUUIDOnInstanceGroup(componentId, instanceId, groupInstances);
2859     }
2860
2861     public Either<PropertyDefinition, StorageOperationStatus> addPropertyToComponent(PropertyDefinition newPropertyDefinition,
2862                                                                                      Component component) {
2863         final String propertyName = newPropertyDefinition.getName();
2864         StorageOperationStatus status = getToscaElementOperation(component)
2865             .addToscaDataToToscaElement(component.getUniqueId(), EdgeLabelEnum.PROPERTIES, VertexTypeEnum.PROPERTIES, newPropertyDefinition,
2866                 JsonPresentationFields.NAME);
2867         if (status != StorageOperationStatus.OK) {
2868             CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to add the property {} to the component {}. Status is {}. ", propertyName,
2869                 component.getName(), status);
2870             return Either.right(status);
2871         }
2872         ComponentParametersView filter = new ComponentParametersView(true);
2873         filter.setIgnoreProperties(false);
2874         filter.setIgnoreInputs(false);
2875         Either<Component, StorageOperationStatus> getUpdatedComponentRes = getToscaElement(component.getUniqueId(), filter);
2876         if (getUpdatedComponentRes.isRight()) {
2877             CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to get updated component {}. Status is {}. ", component.getUniqueId(),
2878                 getUpdatedComponentRes.right().value());
2879             return Either.right(status);
2880         }
2881         PropertyDefinition newProperty = null;
2882         List<PropertyDefinition> properties = (getUpdatedComponentRes.left().value()).getProperties();
2883         if (CollectionUtils.isNotEmpty(properties)) {
2884             Optional<PropertyDefinition> propertyOptional = properties.stream().filter(propertyEntry -> propertyEntry.getName().equals(propertyName))
2885                 .findAny();
2886             if (propertyOptional.isPresent()) {
2887                 newProperty = propertyOptional.get();
2888             }
2889         }
2890         if (newProperty == null) {
2891             CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to find recently added property {} on the component {}. Status is {}. ",
2892                 propertyName, component.getUniqueId(), StorageOperationStatus.NOT_FOUND);
2893             return Either.right(StorageOperationStatus.NOT_FOUND);
2894         }
2895         return Either.left(newProperty);
2896     }
2897
2898     public Either<InputDefinition, StorageOperationStatus> addInputToComponent(String inputName, InputDefinition newInputDefinition,
2899                                                                                Component component) {
2900         newInputDefinition.setName(inputName);
2901         StorageOperationStatus status = getToscaElementOperation(component)
2902             .addToscaDataToToscaElement(component.getUniqueId(), EdgeLabelEnum.INPUTS, VertexTypeEnum.INPUTS, newInputDefinition,
2903                 JsonPresentationFields.NAME);
2904         if (status != StorageOperationStatus.OK) {
2905             CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to add the input {} to the component {}. Status is {}. ", inputName,
2906                 component.getName(), status);
2907             return Either.right(status);
2908         }
2909         ComponentParametersView filter = new ComponentParametersView(true);
2910         filter.setIgnoreProperties(false);
2911         filter.setIgnoreInputs(false);
2912         Either<Component, StorageOperationStatus> getUpdatedComponentRes = getToscaElement(component.getUniqueId(), filter);
2913         if (getUpdatedComponentRes.isRight()) {
2914             CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to get updated component {}. Status is {}. ", component.getUniqueId(),
2915                 getUpdatedComponentRes.right().value());
2916             return Either.right(status);
2917         }
2918         InputDefinition newInput = null;
2919         List<InputDefinition> inputs = (getUpdatedComponentRes.left().value()).getInputs();
2920         if (CollectionUtils.isNotEmpty(inputs)) {
2921             Optional<InputDefinition> inputOptional = inputs.stream().filter(inputEntry -> inputEntry.getName().equals(inputName)).findAny();
2922             if (inputOptional.isPresent()) {
2923                 newInput = inputOptional.get();
2924             }
2925         }
2926         if (newInput == null) {
2927             CommonUtility
2928                 .addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to find recently added input {} " + "on the component {}. Status is {}. ", inputs,
2929                     component.getUniqueId(), StorageOperationStatus.NOT_FOUND);
2930             return Either.right(StorageOperationStatus.NOT_FOUND);
2931         }
2932         return Either.left(newInput);
2933     }
2934
2935     public StorageOperationStatus deletePropertyOfComponent(Component component, String propertyName) {
2936         return getToscaElementOperation(component)
2937             .deleteToscaDataElement(component.getUniqueId(), EdgeLabelEnum.PROPERTIES, VertexTypeEnum.PROPERTIES, propertyName,
2938                 JsonPresentationFields.NAME);
2939     }
2940
2941     public StorageOperationStatus deleteAttributeOfResource(Component component, String attributeName) {
2942         return getToscaElementOperation(component)
2943             .deleteToscaDataElement(component.getUniqueId(), EdgeLabelEnum.ATTRIBUTES, VertexTypeEnum.ATTRIBUTES, attributeName,
2944                 JsonPresentationFields.NAME);
2945     }
2946
2947     public StorageOperationStatus deleteInputOfResource(Component resource, String inputName) {
2948         return getToscaElementOperation(resource)
2949             .deleteToscaDataElement(resource.getUniqueId(), EdgeLabelEnum.INPUTS, VertexTypeEnum.INPUTS, inputName, JsonPresentationFields.NAME);
2950     }
2951
2952     public StorageOperationStatus deleteOutputOfResource(final Component resource, final String outputName) {
2953         return getToscaElementOperation(resource)
2954             .deleteToscaDataElement(resource.getUniqueId(), EdgeLabelEnum.OUTPUTS, VertexTypeEnum.OUTPUTS, outputName, JsonPresentationFields.NAME);
2955     }
2956
2957     /**
2958      * Deletes a data type from a component.
2959      *
2960      * @param component    the container which has the data type
2961      * @param dataTypeName the data type name to be deleted
2962      * @return Operation result.
2963      */
2964     public StorageOperationStatus deleteDataTypeOfComponent(Component component, String dataTypeName) {
2965         return getToscaElementOperation(component)
2966             .deleteToscaDataElement(component.getUniqueId(), EdgeLabelEnum.DATA_TYPES, VertexTypeEnum.DATA_TYPES, dataTypeName,
2967                 JsonPresentationFields.NAME);
2968     }
2969
2970     public Either<PropertyDefinition, StorageOperationStatus> updatePropertyOfComponent(Component component,
2971                                                                                         PropertyDefinition newPropertyDefinition) {
2972         Either<Component, StorageOperationStatus> getUpdatedComponentRes = null;
2973         Either<PropertyDefinition, StorageOperationStatus> result = null;
2974         StorageOperationStatus status = getToscaElementOperation(component)
2975             .updateToscaDataOfToscaElement(component.getUniqueId(), EdgeLabelEnum.PROPERTIES, VertexTypeEnum.PROPERTIES, newPropertyDefinition,
2976                 JsonPresentationFields.NAME);
2977         if (status != StorageOperationStatus.OK) {
2978             CommonUtility
2979                 .addRecordToLog(log, LogLevelEnum.DEBUG, FAILED_TO_ADD_THE_PROPERTY_TO_THE_RESOURCE_STATUS_IS, newPropertyDefinition.getName(),
2980                     component.getName(), status);
2981             result = Either.right(status);
2982         }
2983         if (result == null) {
2984             ComponentParametersView filter = new ComponentParametersView(true);
2985             filter.setIgnoreProperties(false);
2986             getUpdatedComponentRes = getToscaElement(component.getUniqueId(), filter);
2987             if (getUpdatedComponentRes.isRight()) {
2988                 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, FAILED_TO_GET_UPDATED_RESOURCE_STATUS_IS, component.getUniqueId(),
2989                     getUpdatedComponentRes.right().value());
2990                 result = Either.right(status);
2991             }
2992         }
2993         if (result == null) {
2994             Optional<PropertyDefinition> newProperty = (getUpdatedComponentRes.left().value()).getProperties().stream()
2995                 .filter(p -> p.getName().equals(newPropertyDefinition.getName())).findAny();
2996             if (newProperty.isPresent()) {
2997                 result = Either.left(newProperty.get());
2998             } else {
2999                 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, FAILED_TO_FIND_RECENTLY_ADDED_PROPERTY_ON_THE_RESOURCE_STATUS_IS,
3000                     newPropertyDefinition.getName(), component.getUniqueId(), StorageOperationStatus.NOT_FOUND);
3001                 result = Either.right(StorageOperationStatus.NOT_FOUND);
3002             }
3003         }
3004         return result;
3005     }
3006
3007     public Either<AttributeDefinition, StorageOperationStatus> updateAttributeOfComponent(Component component,
3008                                                                                           AttributeDefinition newPropertyDefinition) {
3009         Either<Component, StorageOperationStatus> getUpdatedComponentRes = null;
3010         Either<AttributeDefinition, StorageOperationStatus> result = null;
3011         StorageOperationStatus status = getToscaElementOperation(component)
3012             .updateToscaDataOfToscaElement(component.getUniqueId(), EdgeLabelEnum.ATTRIBUTES, VertexTypeEnum.ATTRIBUTES, newPropertyDefinition,
3013                 JsonPresentationFields.NAME);
3014         if (status != StorageOperationStatus.OK) {
3015             CommonUtility
3016                 .addRecordToLog(log, LogLevelEnum.DEBUG, FAILED_TO_ADD_THE_PROPERTY_TO_THE_RESOURCE_STATUS_IS, newPropertyDefinition.getName(),
3017                     component.getName(), status);
3018             result = Either.right(status);
3019         }
3020         if (result == null) {
3021             ComponentParametersView filter = new ComponentParametersView(true);
3022             filter.setIgnoreProperties(false);
3023             getUpdatedComponentRes = getToscaElement(component.getUniqueId(), filter);
3024             if (getUpdatedComponentRes.isRight()) {
3025                 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, FAILED_TO_GET_UPDATED_RESOURCE_STATUS_IS, component.getUniqueId(),
3026                     getUpdatedComponentRes.right().value());
3027                 result = Either.right(status);
3028             }
3029         }
3030         if (result == null) {
3031             Optional<AttributeDefinition> newProperty = (getUpdatedComponentRes.left().value()).getAttributes().stream()
3032                 .filter(p -> p.getName().equals(newPropertyDefinition.getName())).findAny();
3033             if (newProperty.isPresent()) {
3034                 result = Either.left(newProperty.get());
3035             } else {
3036                 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, FAILED_TO_FIND_RECENTLY_ADDED_PROPERTY_ON_THE_RESOURCE_STATUS_IS,
3037                     newPropertyDefinition.getName(), component.getUniqueId(), StorageOperationStatus.NOT_FOUND);
3038                 result = Either.right(StorageOperationStatus.NOT_FOUND);
3039             }
3040         }
3041         return result;
3042     }
3043
3044     public Either<AttributeDefinition, StorageOperationStatus> addAttributeOfResource(Component component, AttributeDefinition newAttributeDef) {
3045         Either<Component, StorageOperationStatus> getUpdatedComponentRes = null;
3046         Either<AttributeDefinition, StorageOperationStatus> result = null;
3047         if (newAttributeDef.getUniqueId() == null || newAttributeDef.getUniqueId().isEmpty()) {
3048             String attUniqueId = UniqueIdBuilder.buildAttributeUid(component.getUniqueId(), newAttributeDef.getName());
3049             newAttributeDef.setUniqueId(attUniqueId);
3050             newAttributeDef.setOwnerId(component.getUniqueId());
3051         }
3052         StorageOperationStatus status = getToscaElementOperation(component)
3053             .addToscaDataToToscaElement(component.getUniqueId(), EdgeLabelEnum.ATTRIBUTES, VertexTypeEnum.ATTRIBUTES, newAttributeDef,
3054                 JsonPresentationFields.NAME);
3055         if (status != StorageOperationStatus.OK) {
3056             CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, FAILED_TO_ADD_THE_PROPERTY_TO_THE_RESOURCE_STATUS_IS, newAttributeDef.getName(),
3057                 component.getName(), status);
3058             result = Either.right(status);
3059         }
3060         if (result == null) {
3061             ComponentParametersView filter = new ComponentParametersView(true);
3062             filter.setIgnoreAttributes(false);
3063             getUpdatedComponentRes = getToscaElement(component.getUniqueId(), filter);
3064             if (getUpdatedComponentRes.isRight()) {
3065                 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, FAILED_TO_GET_UPDATED_RESOURCE_STATUS_IS, component.getUniqueId(),
3066                     getUpdatedComponentRes.right().value());
3067                 result = Either.right(status);
3068             }
3069         }
3070         if (result == null) {
3071             Optional<AttributeDefinition> newAttribute = ((Resource) getUpdatedComponentRes.left().value()).getAttributes().stream()
3072                 .filter(p -> p.getName().equals(newAttributeDef.getName())).findAny();
3073             if (newAttribute.isPresent()) {
3074                 result = Either.left(newAttribute.get());
3075             } else {
3076                 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, FAILED_TO_FIND_RECENTLY_ADDED_PROPERTY_ON_THE_RESOURCE_STATUS_IS,
3077                     newAttributeDef.getName(), component.getUniqueId(), StorageOperationStatus.NOT_FOUND);
3078                 result = Either.right(StorageOperationStatus.NOT_FOUND);
3079             }
3080         }
3081         return result;
3082     }
3083
3084     public Either<AttributeDefinition, StorageOperationStatus> updateAttributeOfResource(Component component, AttributeDefinition newAttributeDef) {
3085         Either<Component, StorageOperationStatus> getUpdatedComponentRes = null;
3086         Either<AttributeDefinition, StorageOperationStatus> result = null;
3087         StorageOperationStatus status = getToscaElementOperation(component)
3088             .updateToscaDataOfToscaElement(component.getUniqueId(), EdgeLabelEnum.ATTRIBUTES, VertexTypeEnum.ATTRIBUTES, newAttributeDef,
3089                 JsonPresentationFields.NAME);
3090         if (status != StorageOperationStatus.OK) {
3091             CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, FAILED_TO_ADD_THE_PROPERTY_TO_THE_RESOURCE_STATUS_IS, newAttributeDef.getName(),
3092                 component.getName(), status);
3093             result = Either.right(status);
3094         }
3095         if (result == null) {
3096             ComponentParametersView filter = new ComponentParametersView(true);
3097             filter.setIgnoreAttributes(false);
3098             getUpdatedComponentRes = getToscaElement(component.getUniqueId(), filter);
3099             if (getUpdatedComponentRes.isRight()) {
3100                 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, FAILED_TO_GET_UPDATED_RESOURCE_STATUS_IS, component.getUniqueId(),
3101                     getUpdatedComponentRes.right().value());
3102                 result = Either.right(status);
3103             }
3104         }
3105         if (result == null) {
3106             Optional<AttributeDefinition> newProperty = ((Resource) getUpdatedComponentRes.left().value()).getAttributes().stream()
3107                 .filter(p -> p.getName().equals(newAttributeDef.getName())).findAny();
3108             if (newProperty.isPresent()) {
3109                 result = Either.left(newProperty.get());
3110             } else {
3111                 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, FAILED_TO_FIND_RECENTLY_ADDED_PROPERTY_ON_THE_RESOURCE_STATUS_IS,
3112                     newAttributeDef.getName(), component.getUniqueId(), StorageOperationStatus.NOT_FOUND);
3113                 result = Either.right(StorageOperationStatus.NOT_FOUND);
3114             }
3115         }
3116         return result;
3117     }
3118
3119     public Either<InputDefinition, StorageOperationStatus> updateInputOfComponent(Component component, InputDefinition newInputDefinition) {
3120         Either<Component, StorageOperationStatus> getUpdatedComponentRes = null;
3121         Either<InputDefinition, StorageOperationStatus> result = null;
3122         StorageOperationStatus status = getToscaElementOperation(component)
3123             .updateToscaDataOfToscaElement(component.getUniqueId(), EdgeLabelEnum.INPUTS, VertexTypeEnum.INPUTS, newInputDefinition,
3124                 JsonPresentationFields.NAME);
3125         if (status != StorageOperationStatus.OK) {
3126             CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to update the input {} to the component {}. Status is {}. ",
3127                 newInputDefinition.getName(), component.getName(), status);
3128             result = Either.right(status);
3129         }
3130         if (result == null) {
3131             ComponentParametersView filter = new ComponentParametersView(true);
3132             filter.setIgnoreInputs(false);
3133             getUpdatedComponentRes = getToscaElement(component.getUniqueId(), filter);
3134             if (getUpdatedComponentRes.isRight()) {
3135                 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, FAILED_TO_GET_UPDATED_RESOURCE_STATUS_IS, component.getUniqueId(),
3136                     getUpdatedComponentRes.right().value());
3137                 result = Either.right(status);
3138             }
3139         }
3140         if (result == null) {
3141             Optional<InputDefinition> updatedInput = getUpdatedComponentRes.left().value().getInputs().stream()
3142                 .filter(p -> p.getName().equals(newInputDefinition.getName())).findAny();
3143             if (updatedInput.isPresent()) {
3144                 result = Either.left(updatedInput.get());
3145             } else {
3146                 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to find recently updated inputs {} on the resource {}. Status is {}. ",
3147                     newInputDefinition.getName(), component.getUniqueId(), StorageOperationStatus.NOT_FOUND);
3148                 result = Either.right(StorageOperationStatus.NOT_FOUND);
3149             }
3150         }
3151         return result;
3152     }
3153
3154     /**
3155      * method - ename the group instances after referenced container name renamed flow - VF rename -(triggers)-> Group rename
3156      *
3157      * @param containerComponent  - container such as service
3158      * @param componentInstance   - context component
3159      * @param componentInstanceId - id
3160      * @return - successfull/failed status
3161      **/
3162     public Either<StorageOperationStatus, StorageOperationStatus> cleanAndAddGroupInstancesToComponentInstance(Component containerComponent,
3163                                                                                                                ComponentInstance componentInstance,
3164                                                                                                                String componentInstanceId) {
3165         String uniqueId = componentInstance.getUniqueId();
3166         StorageOperationStatus status = nodeTemplateOperation
3167             .deleteToscaDataDeepElementsBlockOfToscaElement(containerComponent.getUniqueId(), EdgeLabelEnum.INST_GROUPS,
3168                 uniqueId);
3169         if (status != StorageOperationStatus.OK && status != StorageOperationStatus.NOT_FOUND) {
3170             CommonUtility
3171                 .addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to delete group instances for container {}. error {] ", componentInstanceId, status);
3172             return Either.right(status);
3173         }
3174         if (componentInstance.getGroupInstances() != null) {
3175             status = addGroupInstancesToComponentInstance(containerComponent, componentInstance, componentInstance.getGroupInstances());
3176             if (status != StorageOperationStatus.OK && status != StorageOperationStatus.NOT_FOUND) {
3177                 CommonUtility
3178                     .addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to add group instances for container {}. error {] ", componentInstanceId,
3179                         status);
3180                 return Either.right(status);
3181             }
3182         }
3183         return Either.left(status);
3184     }
3185
3186     public StorageOperationStatus addGroupInstancesToComponentInstance(Component containerComponent, ComponentInstance componentInstance,
3187                                                                        List<GroupDefinition> groups,
3188                                                                        Map<String, List<ArtifactDefinition>> groupInstancesArtifacts) {
3189         return nodeTemplateOperation.addGroupInstancesToComponentInstance(containerComponent, componentInstance, groups, groupInstancesArtifacts);
3190     }
3191
3192     public Either<List<GroupDefinition>, StorageOperationStatus> updateGroupsOnComponent(Component component,
3193                                                                                          List<GroupDataDefinition> updatedGroups) {
3194         return groupsOperation.updateGroups(component, updatedGroups, PromoteVersionEnum.MINOR);
3195     }
3196
3197     public Either<List<GroupInstance>, StorageOperationStatus> updateGroupInstancesOnComponent(Component component, String instanceId,
3198                                                                                                List<GroupInstance> updatedGroupInstances) {
3199         return groupsOperation.updateGroupInstances(component, instanceId, updatedGroupInstances);
3200     }
3201
3202     public StorageOperationStatus addGroupInstancesToComponentInstance(Component containerComponent, ComponentInstance componentInstance,
3203                                                                        List<GroupInstance> groupInstances) {
3204         return nodeTemplateOperation.addGroupInstancesToComponentInstance(containerComponent, componentInstance, groupInstances);
3205     }
3206
3207     public StorageOperationStatus addDeploymentArtifactsToComponentInstance(Component containerComponent, ComponentInstance componentInstance,
3208                                                                             Map<String, ArtifactDefinition> deploymentArtifacts) {
3209         return nodeTemplateOperation.addDeploymentArtifactsToComponentInstance(containerComponent, componentInstance, deploymentArtifacts);
3210     }
3211
3212     public StorageOperationStatus updateComponentInstanceProperty(Component containerComponent, String componentInstanceId,
3213                                                                   ComponentInstanceProperty property) {
3214         return nodeTemplateOperation.updateComponentInstanceProperty(containerComponent, componentInstanceId, property);
3215     }
3216
3217     public StorageOperationStatus updateComponentInstanceProperties(Component containerComponent, String componentInstanceId,
3218                                                                     List<ComponentInstanceProperty> properties) {
3219         return nodeTemplateOperation.updateComponentInstanceProperties(containerComponent, componentInstanceId, properties);
3220     }
3221
3222     public StorageOperationStatus updateComponentInstanceAttributes(final Component containerComponent, final String componentInstanceId,
3223                                                                     final List<ComponentInstanceAttribute> attributes) {
3224         return nodeTemplateOperation.updateComponentInstanceAttributes(containerComponent, componentInstanceId, attributes);
3225     }
3226
3227     public StorageOperationStatus addComponentInstanceProperty(Component containerComponent, String componentInstanceId,
3228                                                                ComponentInstanceProperty property) {
3229         return nodeTemplateOperation.addComponentInstanceProperty(containerComponent, componentInstanceId, property);
3230     }
3231
3232     public StorageOperationStatus updateComponentInstanceAttribute(final Component containerComponent, final String componentInstanceId,
3233                                                                    final ComponentInstanceAttribute attribute) {
3234         return nodeTemplateOperation.updateComponentInstanceAttribute(containerComponent, componentInstanceId, attribute);
3235     }
3236
3237     public StorageOperationStatus addComponentInstanceAttribute(Component containerComponent, String componentInstanceId,
3238                                                                 ComponentInstanceAttribute attribute) {
3239         return nodeTemplateOperation.addComponentInstanceAttribute(containerComponent, componentInstanceId, attribute);
3240     }
3241
3242     public StorageOperationStatus updateComponentInstanceInput(Component containerComponent, String componentInstanceId,
3243                                                                ComponentInstanceInput property) {
3244         return nodeTemplateOperation.updateComponentInstanceInput(containerComponent, componentInstanceId, property);
3245     }
3246
3247     public StorageOperationStatus updateComponentInstanceOutput(Component containerComponent, String componentInstanceId,
3248                                                                 ComponentInstanceOutput property) {
3249         return nodeTemplateOperation.updateComponentInstanceOutput(containerComponent, componentInstanceId, property);
3250     }
3251
3252     public StorageOperationStatus updateComponentInstanceInputs(Component containerComponent, String componentInstanceId,
3253                                                                 List<ComponentInstanceInput> instanceInputs) {
3254         return nodeTemplateOperation.updateComponentInstanceInputs(containerComponent, componentInstanceId, instanceInputs);
3255     }
3256
3257     public StorageOperationStatus updateComponentInstanceOutputs(Component containerComponent, String componentInstanceId,
3258                                                                  List<ComponentInstanceOutput> instanceInputs) {
3259         return nodeTemplateOperation.updateComponentInstanceOutputs(containerComponent, componentInstanceId, instanceInputs);
3260     }
3261
3262     public StorageOperationStatus addComponentInstanceInput(Component containerComponent, String componentInstanceId,
3263                                                             ComponentInstanceInput property) {
3264         return nodeTemplateOperation.addComponentInstanceInput(containerComponent, componentInstanceId, property);
3265     }
3266
3267     public StorageOperationStatus addComponentInstanceOutput(Component containerComponent, String componentInstanceId,
3268                                                              ComponentInstanceOutput property) {
3269         return nodeTemplateOperation.addComponentInstanceOutput(containerComponent, componentInstanceId, property);
3270     }
3271
3272     public void setNodeTypeOperation(NodeTypeOperation nodeTypeOperation) {
3273         this.nodeTypeOperation = nodeTypeOperation;
3274     }
3275
3276     public void setTopologyTemplateOperation(TopologyTemplateOperation topologyTemplateOperation) {
3277         this.topologyTemplateOperation = topologyTemplateOperation;
3278     }
3279
3280     public StorageOperationStatus deleteComponentInstanceInputsFromTopologyTemplate(Component containerComponent,
3281                                                                                     List<InputDefinition> inputsToDelete) {
3282         return topologyTemplateOperation.deleteToscaDataElements(containerComponent.getUniqueId(), EdgeLabelEnum.INPUTS,
3283             inputsToDelete.stream().map(PropertyDataDefinition::getName).collect(Collectors.toList()));
3284     }
3285
3286     public StorageOperationStatus deleteComponentInstanceOutputsFromTopologyTemplate(final Component containerComponent,
3287                                                                                      final List<OutputDefinition> outputsToDelete) {
3288         return topologyTemplateOperation.deleteToscaDataElements(containerComponent.getUniqueId(), EdgeLabelEnum.OUTPUTS,
3289             outputsToDelete.stream().map(AttributeDataDefinition::getName).collect(Collectors.toList()));
3290     }
3291
3292     public StorageOperationStatus updateComponentInstanceCapabiltyProperty(Component containerComponent, String componentInstanceUniqueId,
3293                                                                            String capabilityPropertyKey, ComponentInstanceProperty property) {
3294         return nodeTemplateOperation
3295             .updateComponentInstanceCapabilityProperty(containerComponent, componentInstanceUniqueId, capabilityPropertyKey, property);
3296     }
3297
3298     public StorageOperationStatus updateComponentInstanceCapabilityProperties(Component containerComponent, String componentInstanceUniqueId) {
3299         return convertComponentInstanceProperties(containerComponent, componentInstanceUniqueId).map(instanceCapProps -> topologyTemplateOperation
3300                 .updateComponentInstanceCapabilityProperties(containerComponent, componentInstanceUniqueId, instanceCapProps))
3301             .orElse(StorageOperationStatus.NOT_FOUND);
3302     }
3303
3304     public StorageOperationStatus updateComponentInstanceRequirement(String containerComponentId, String componentInstanceUniqueId,
3305                                                                      RequirementDataDefinition requirementDataDefinition) {
3306         return nodeTemplateOperation.updateComponentInstanceRequirement(containerComponentId, componentInstanceUniqueId, requirementDataDefinition);
3307     }
3308
3309     public CapabilityDataDefinition updateComponentInstanceCapability(final String containerComponentId, final String componentInstanceUniqueId,
3310                                                                       final CapabilityDataDefinition capabilityDataDefinition) {
3311
3312         return nodeTemplateOperation.updateComponentInstanceCapabilities(containerComponentId, componentInstanceUniqueId, capabilityDataDefinition);
3313     }
3314
3315     public StorageOperationStatus updateComponentInstanceInterfaces(Component containerComponent, String componentInstanceUniqueId) {
3316         MapInterfaceDataDefinition mapInterfaceDataDefinition = convertComponentInstanceInterfaces(containerComponent, componentInstanceUniqueId);
3317         return topologyTemplateOperation.updateComponentInstanceInterfaces(containerComponent, componentInstanceUniqueId, mapInterfaceDataDefinition);
3318     }
3319
3320     public StorageOperationStatus updateComponentInterfaces(final Component component, final String componentInterfaceUpdatedKey) {
3321         MapInterfaceDataDefinition mapInterfaceDataDefinition = convertComponentInterfaces(component.getInterfaces());
3322         return topologyTemplateOperation.updateComponentInterfaces(component.getUniqueId(), mapInterfaceDataDefinition, componentInterfaceUpdatedKey);
3323     }
3324
3325     public Either<InterfaceDefinition, StorageOperationStatus> addInterfaceToComponent(final String interfaceName,
3326                                                                                        final InterfaceDefinition interfaceDefinition,
3327                                                                                        final Component component) {
3328
3329         final boolean match = component.getInterfaces().keySet().stream().anyMatch(s -> s.equals(interfaceName));
3330         StorageOperationStatus status;
3331         final ToscaElementOperation toscaElementOperation = getToscaElementOperation(component);
3332         if (match) {
3333             status = toscaElementOperation.updateToscaDataOfToscaElement(component.getUniqueId(), EdgeLabelEnum.INTERFACE_ARTIFACTS,
3334                 VertexTypeEnum.INTERFACE_ARTIFACTS, interfaceDefinition, JsonPresentationFields.TYPE);
3335         } else {
3336             status = toscaElementOperation.addToscaDataToToscaElement(component.getUniqueId(), EdgeLabelEnum.INTERFACE_ARTIFACTS,
3337                 VertexTypeEnum.INTERFACE_ARTIFACTS, interfaceDefinition, JsonPresentationFields.TYPE);
3338         }
3339
3340         if (status != StorageOperationStatus.OK) {
3341             CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to add the interface {} to the component {}. Status is {}. ",
3342                 interfaceName, component.getName(), status);
3343             return Either.right(status);
3344         }
3345         final ComponentParametersView filter = new ComponentParametersView(true);
3346         filter.setIgnoreInterfaces(false);
3347         filter.setIgnoreInterfaceInstances(false);
3348         final Either<Component, StorageOperationStatus> getUpdatedComponentRes = getToscaElement(component.getUniqueId(), filter);
3349         if (getUpdatedComponentRes.isRight()) {
3350             CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to get updated component {}. Status is {}. ",
3351                 component.getUniqueId(), getUpdatedComponentRes.right().value());
3352             return Either.right(getUpdatedComponentRes.right().value());
3353         }
3354         InterfaceDefinition newInterfaceDefinition = null;
3355         final Map<String, InterfaceDefinition> interfaces = (getUpdatedComponentRes.left().value()).getInterfaces();
3356         if (MapUtils.isNotEmpty(interfaces)) {
3357             final Optional<String> interfaceNameOptional = interfaces.keySet().stream().filter(key -> key.equals(interfaceName)).findAny();
3358             if (interfaceNameOptional.isPresent()) {
3359                 newInterfaceDefinition = interfaces.get(interfaceNameOptional.get());
3360             }
3361         }
3362         if (newInterfaceDefinition == null) {
3363             CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to find recently added interface {} on the component {}. Status is {}. ",
3364                 interfaceName, component.getUniqueId(), StorageOperationStatus.NOT_FOUND);
3365             return Either.right(StorageOperationStatus.NOT_FOUND);
3366         }
3367         return Either.left(newInterfaceDefinition);
3368     }
3369
3370     public StorageOperationStatus updateComponentCalculatedCapabilitiesProperties(Component containerComponent) {
3371         Map<String, MapCapabilityProperty> mapCapabiltyPropertyMap = convertComponentCapabilitiesProperties(containerComponent);
3372         return nodeTemplateOperation.overrideComponentCapabilitiesProperties(containerComponent, mapCapabiltyPropertyMap);
3373     }
3374
3375     public StorageOperationStatus deleteAllCalculatedCapabilitiesRequirements(String topologyTemplateId) {
3376         StorageOperationStatus status = topologyTemplateOperation
3377             .removeToscaData(topologyTemplateId, EdgeLabelEnum.CALCULATED_CAPABILITIES);
3378         if (status == StorageOperationStatus.OK) {
3379             status = topologyTemplateOperation
3380                 .removeToscaData(topologyTemplateId, EdgeLabelEnum.CALCULATED_REQUIREMENTS);
3381         }
3382         if (status == StorageOperationStatus.OK) {
3383             status = topologyTemplateOperation
3384                 .removeToscaData(topologyTemplateId, EdgeLabelEnum.CALCULATED_CAP_PROPERTIES);
3385         }
3386         return status;
3387     }
3388
3389     public Either<Component, StorageOperationStatus> shouldUpgradeToLatestDerived(Resource clonedResource) {
3390         String componentId = clonedResource.getUniqueId();
3391         Either<GraphVertex, JanusGraphOperationStatus> getVertexEither = janusGraphDao.getVertexById(componentId, JsonParseFlagEnum.NoParse);
3392         if (getVertexEither.isRight()) {
3393             log.debug(COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, componentId, getVertexEither.right().value());
3394             return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVertexEither.right().value()));
3395         }
3396         GraphVertex nodeTypeV = getVertexEither.left().value();
3397         ToscaElement toscaElementToUpdate = ModelConverter.convertToToscaElement(clonedResource);
3398         Either<ToscaElement, StorageOperationStatus> shouldUpdateDerivedVersion = nodeTypeOperation
3399             .shouldUpdateDerivedVersion(toscaElementToUpdate, nodeTypeV);
3400         if (shouldUpdateDerivedVersion.isRight() && StorageOperationStatus.OK != shouldUpdateDerivedVersion.right().value()) {
3401             log.debug("Failed to update derived version for node type {} derived {}, error: {}", componentId, clonedResource.getDerivedFrom().get(0),
3402                 shouldUpdateDerivedVersion.right().value());
3403             return Either.right(shouldUpdateDerivedVersion.right().value());
3404         }
3405         if (shouldUpdateDerivedVersion.isLeft()) {
3406             return Either.left(ModelConverter.convertFromToscaElement(shouldUpdateDerivedVersion.left().value()));
3407         }
3408         return Either.left(clonedResource);
3409     }
3410
3411     /**
3412      * Returns list of ComponentInstanceProperty belonging to component instance capability specified by name, type and ownerId
3413      */
3414     public Either<List<ComponentInstanceProperty>, StorageOperationStatus> getComponentInstanceCapabilityProperties(String componentId,
3415                                                                                                                     String instanceId,
3416                                                                                                                     String capabilityName,
3417                                                                                                                     String capabilityType,
3418                                                                                                                     String ownerId) {
3419         return topologyTemplateOperation.getComponentInstanceCapabilityProperties(componentId, instanceId, capabilityName, capabilityType, ownerId);
3420     }
3421
3422     private MapInterfaceDataDefinition convertComponentInstanceInterfaces(Component currComponent, String componentInstanceId) {
3423         MapInterfaceDataDefinition mapInterfaceDataDefinition = new MapInterfaceDataDefinition();
3424         List<ComponentInstanceInterface> componentInterface = currComponent.getComponentInstancesInterfaces().get(componentInstanceId);
3425         if (CollectionUtils.isNotEmpty(componentInterface)) {
3426             componentInterface.stream().forEach(interfaceDef -> mapInterfaceDataDefinition.put(interfaceDef.getUniqueId(), interfaceDef));
3427         }
3428         return mapInterfaceDataDefinition;
3429     }
3430
3431     private MapInterfaceDataDefinition convertComponentInterfaces(final Map<String, InterfaceDefinition> interfaces) {
3432         final MapInterfaceDataDefinition mapInterfaceDataDefinition = new MapInterfaceDataDefinition();
3433         if (MapUtils.isNotEmpty(interfaces)) {
3434             interfaces.values().stream().forEach(interfaceDef -> mapInterfaceDataDefinition.put(interfaceDef.getType(), interfaceDef));
3435         }
3436         return mapInterfaceDataDefinition;
3437     }
3438
3439     private Map<String, MapCapabilityProperty> convertComponentCapabilitiesProperties(Component currComponent) {
3440         Map<String, MapCapabilityProperty> map = ModelConverter.extractCapabilityPropertiesFromGroups(currComponent.getGroups(), true);
3441         map.putAll(ModelConverter.extractCapabilityProperteisFromInstances(currComponent.getComponentInstances(), true));
3442         return map;
3443     }
3444
3445     private Optional<MapCapabilityProperty> convertComponentInstanceProperties(Component component, String instanceId) {
3446         return component.fetchInstanceById(instanceId)
3447             .map(ci -> ModelConverter.convertToMapOfMapCapabilityProperties(ci.getCapabilities(), instanceId, ci.getOriginType().isAtomicType()));
3448     }
3449
3450     public Either<PolicyDefinition, StorageOperationStatus> associatePolicyToComponent(String componentId, PolicyDefinition policyDefinition,
3451                                                                                        int counter) {
3452         Either<PolicyDefinition, StorageOperationStatus> result = null;
3453         Either<GraphVertex, JanusGraphOperationStatus> getVertexEither;
3454         getVertexEither = janusGraphDao.getVertexById(componentId, JsonParseFlagEnum.ParseMetadata);
3455         if (getVertexEither.isRight()) {
3456             log.error(COULDNT_FETCH_A_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, componentId, getVertexEither.right().value());
3457             result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVertexEither.right().value()));
3458         } else {
3459             if (getVertexEither.left().value().getLabel() != TOPOLOGY_TEMPLATE) {
3460                 log.error("Policy association to component of Tosca type {} is not allowed. ", getVertexEither.left().value().getLabel());
3461                 result = Either.right(StorageOperationStatus.BAD_REQUEST);
3462             }
3463         }
3464         if (result == null) {
3465             StorageOperationStatus status = topologyTemplateOperation
3466                 .addPolicyToToscaElement(getVertexEither.left().value(), policyDefinition, counter);
3467             if (status != StorageOperationStatus.OK) {
3468                 return Either.right(status);
3469             }
3470         }
3471         if (result == null) {
3472             result = Either.left(policyDefinition);
3473         }
3474         return result;
3475     }
3476
3477     public StorageOperationStatus associatePoliciesToComponent(String componentId, List<PolicyDefinition> policies) {
3478         log.debug("#associatePoliciesToComponent - associating policies for component {}.", componentId);
3479         return janusGraphDao.getVertexById(componentId, JsonParseFlagEnum.ParseMetadata)
3480             .either(containerVertex -> topologyTemplateOperation.addPoliciesToToscaElement(containerVertex, policies),
3481                 DaoStatusConverter::convertJanusGraphStatusToStorageStatus);
3482     }
3483
3484     public Either<PolicyDefinition, StorageOperationStatus> updatePolicyOfComponent(String componentId, PolicyDefinition policyDefinition,
3485                                                                                     PromoteVersionEnum promoteVersionEnum) {
3486         Either<PolicyDefinition, StorageOperationStatus> result = null;
3487         Either<GraphVertex, JanusGraphOperationStatus> getVertexEither;
3488         getVertexEither = janusGraphDao.getVertexById(componentId, JsonParseFlagEnum.NoParse);
3489         if (getVertexEither.isRight()) {
3490             log.error(COULDNT_FETCH_A_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, componentId, getVertexEither.right().value());
3491             result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVertexEither.right().value()));
3492         }
3493         if (result == null) {
3494             policyDefinition.setVersion(GroupUtils.updateVersion(promoteVersionEnum, policyDefinition.getVersion()));
3495             StorageOperationStatus status = topologyTemplateOperation.updatePolicyOfToscaElement(getVertexEither.left().value(), policyDefinition);
3496             if (status != StorageOperationStatus.OK) {
3497                 return Either.right(status);
3498             }
3499         }
3500         if (result == null) {
3501             result = Either.left(policyDefinition);
3502         }
3503         return result;
3504     }
3505
3506     public StorageOperationStatus updatePoliciesOfComponent(String componentId, List<PolicyDefinition> policyDefinition) {
3507         log.debug("#updatePoliciesOfComponent - updating policies for component {}", componentId);
3508         return janusGraphDao.getVertexById(componentId, JsonParseFlagEnum.NoParse).right()
3509             .map(DaoStatusConverter::convertJanusGraphStatusToStorageStatus)
3510             .either(containerVertex -> topologyTemplateOperation.updatePoliciesOfToscaElement(containerVertex, policyDefinition), err -> err);
3511     }
3512
3513     public StorageOperationStatus removePolicyFromComponent(String componentId, String policyId) {
3514         StorageOperationStatus status = null;
3515         Either<GraphVertex, JanusGraphOperationStatus> getVertexEither = janusGraphDao.getVertexById(componentId, JsonParseFlagEnum.NoParse);
3516         if (getVertexEither.isRight()) {
3517             log.error(COULDNT_FETCH_A_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, componentId, getVertexEither.right().value());
3518             status = DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVertexEither.right().value());
3519         }
3520         if (status == null) {
3521             status = topologyTemplateOperation.removePolicyFromToscaElement(getVertexEither.left().value(), policyId);
3522         }
3523         return status;
3524     }
3525
3526     public boolean canAddGroups(String componentId) {
3527         GraphVertex vertex = janusGraphDao.getVertexById(componentId).left().on(this::onJanusGraphError);
3528         return topologyTemplateOperation.hasEdgeOfType(vertex, EdgeLabelEnum.GROUPS);
3529     }
3530
3531     GraphVertex onJanusGraphError(JanusGraphOperationStatus toe) {
3532         throw new StorageException(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(toe));
3533     }
3534
3535     public CatalogUpdateTimestamp updateCatalogTimes() {
3536         long now = System.currentTimeMillis();
3537         GraphVertex catalogRoot = janusGraphDao.getVertexByLabel(VertexTypeEnum.CATALOG_ROOT).left().on(this::onJanusGraphError);
3538         Long currentTime = (Long) catalogRoot.getMetadataProperty(GraphPropertyEnum.CURRENT_CATALOG_UPDATE_TIME);
3539         catalogRoot.addMetadataProperty(GraphPropertyEnum.PREV_CATALOG_UPDATE_TIME, currentTime);
3540         catalogRoot.addMetadataProperty(GraphPropertyEnum.CURRENT_CATALOG_UPDATE_TIME, now);
3541         janusGraphDao.updateVertex(catalogRoot).left().on(this::onJanusGraphError);
3542         return new CatalogUpdateTimestamp(currentTime, now);
3543     }
3544
3545     public CatalogUpdateTimestamp getCatalogTimes() {
3546         GraphVertex catalogRoot = janusGraphDao.getVertexByLabel(VertexTypeEnum.CATALOG_ROOT).left().on(this::onJanusGraphError);
3547         Long currentTime = (Long) catalogRoot.getMetadataProperty(GraphPropertyEnum.CURRENT_CATALOG_UPDATE_TIME);
3548         Long prevTime = (Long) catalogRoot.getMetadataProperty(GraphPropertyEnum.PREV_CATALOG_UPDATE_TIME);
3549         return new CatalogUpdateTimestamp(prevTime == null ? 0 : prevTime.longValue(), currentTime == null ? 0 : currentTime.longValue());
3550     }
3551
3552     public void updateNamesOfCalculatedCapabilitiesRequirements(String componentId) {
3553         topologyTemplateOperation.updateNamesOfCalculatedCapabilitiesRequirements(componentId, getTopologyTemplate(componentId));
3554     }
3555
3556     public void revertNamesOfCalculatedCapabilitiesRequirements(String componentId) {
3557         topologyTemplateOperation.revertNamesOfCalculatedCapabilitiesRequirements(componentId, getTopologyTemplate(componentId));
3558     }
3559
3560     private TopologyTemplate getTopologyTemplate(String componentId) {
3561         return (TopologyTemplate) topologyTemplateOperation.getToscaElement(componentId, getFilterComponentWithCapProperties()).left()
3562             .on(this::throwStorageException);
3563     }
3564
3565     private ComponentParametersView getFilterComponentWithCapProperties() {
3566         ComponentParametersView filter = new ComponentParametersView();
3567         filter.setIgnoreCapabiltyProperties(false);
3568         return filter;
3569     }
3570
3571     private ToscaElement throwStorageException(StorageOperationStatus status) {
3572         throw new StorageException(status);
3573     }
3574
3575     public Either<Boolean, StorageOperationStatus> isComponentInUse(String componentId) {
3576         final List<EdgeLabelEnum> forbiddenEdgeLabelEnums = Arrays
3577             .asList(EdgeLabelEnum.INSTANCE_OF, EdgeLabelEnum.PROXY_OF, EdgeLabelEnum.ALLOTTED_OF);
3578         Either<GraphVertex, JanusGraphOperationStatus> vertexById = janusGraphDao.getVertexById(componentId);
3579         if (vertexById.isLeft()) {
3580             for (EdgeLabelEnum edgeLabelEnum : forbiddenEdgeLabelEnums) {
3581                 Iterator<Edge> edgeItr = vertexById.left().value().getVertex().edges(Direction.IN, edgeLabelEnum.name());
3582                 if (edgeItr != null && edgeItr.hasNext()) {
3583                     return Either.left(true);
3584                 }
3585             }
3586         }
3587         return Either.left(false);
3588     }
3589
3590     public Either<List<Component>, StorageOperationStatus> getComponentListByInvariantUuid(String componentInvariantUuid,
3591                                                                                            Map<GraphPropertyEnum, Object> additionalPropertiesToMatch) {
3592         Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
3593         if (MapUtils.isNotEmpty(additionalPropertiesToMatch)) {
3594             propertiesToMatch.putAll(additionalPropertiesToMatch);
3595         }
3596         propertiesToMatch.put(GraphPropertyEnum.INVARIANT_UUID, componentInvariantUuid);
3597         Either<List<GraphVertex>, JanusGraphOperationStatus> vertexEither = janusGraphDao
3598             .getByCriteria(null, propertiesToMatch, JsonParseFlagEnum.ParseMetadata);
3599         if (vertexEither.isRight()) {
3600             log.debug("Couldn't fetch metadata for component with type {} and invariantUUId {}, error: {}", componentInvariantUuid,
3601                 vertexEither.right().value());
3602             return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(vertexEither.right().value()));
3603         }
3604         List<GraphVertex> vertexList = vertexEither.isLeft() ? vertexEither.left().value() : null;
3605         if (vertexList == null || vertexList.isEmpty()) {
3606             log.debug("Component with invariantUUId {} was not found", componentInvariantUuid);
3607             return Either.right(StorageOperationStatus.NOT_FOUND);
3608         }
3609         ArrayList<Component> components = new ArrayList<>();
3610         for (GraphVertex vertex : vertexList) {
3611             Either<Component, StorageOperationStatus> toscaElementByOperation = getToscaElementByOperation(vertex);
3612             if (toscaElementByOperation.isRight()) {
3613                 log.debug("Could not fetch the following Component by Invariant UUID {}", vertex.getUniqueId());
3614                 return Either.right(toscaElementByOperation.right().value());
3615             }
3616             components.add(toscaElementByOperation.left().value());
3617         }
3618         return Either.left(components);
3619     }
3620
3621     public Either<List<Component>, StorageOperationStatus> getParentComponents(String componentId) {
3622         List<Component> parentComponents = new ArrayList<>();
3623         final List<EdgeLabelEnum> relationEdgeLabelEnums = Arrays.asList(EdgeLabelEnum.INSTANCE_OF, EdgeLabelEnum.PROXY_OF);
3624         Either<GraphVertex, JanusGraphOperationStatus> vertexById = janusGraphDao.getVertexById(componentId);
3625         if (vertexById.isLeft()) {
3626             for (EdgeLabelEnum edgeLabelEnum : relationEdgeLabelEnums) {
3627                 Either<GraphVertex, JanusGraphOperationStatus> parentVertexEither = janusGraphDao
3628                     .getParentVertex(vertexById.left().value(), edgeLabelEnum, JsonParseFlagEnum.ParseJson);
3629                 if (parentVertexEither.isLeft()) {
3630                     Either<Component, StorageOperationStatus> componentEither = getToscaElement(parentVertexEither.left().value().getUniqueId());
3631                     if (componentEither.isLeft()) {
3632                         parentComponents.add(componentEither.left().value());
3633                     }
3634                 }
3635             }
3636         }
3637         return Either.left(parentComponents);
3638     }
3639
3640     public void updateCapReqPropertiesOwnerId(String componentId) {
3641         topologyTemplateOperation.updateCapReqPropertiesOwnerId(componentId, getTopologyTemplate(componentId));
3642     }
3643
3644     public <T extends Component> Either<T, StorageOperationStatus> getLatestByServiceName(String serviceName) {
3645         return getLatestByName(GraphPropertyEnum.NAME, serviceName, null);
3646     }
3647 }