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