Service Import - Node Template Relationship Template
[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].replaceAll("\\D", "")).collect(Collectors.toList());
1327         List<String> countersInIds = containerComponent.getComponentInstances().stream()
1328             .filter(ci -> ci.getUniqueId() != null && ci.getUniqueId().contains(normalizedName))
1329             .map(ci -> ci.getUniqueId().split(normalizedName)[1].replaceAll("\\D", "")).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.setComponentInstancesRelations(updatedComponent.getComponentInstancesRelations());
2086         component.setComponentInstances(updatedComponent.getComponentInstances());
2087     }
2088
2089     private Either<List<Service>, StorageOperationStatus> getLatestVersionNonCheckoutServicesMetadataOnly(Map<GraphPropertyEnum, Object> hasProps,
2090                                                                                                           Map<GraphPropertyEnum, Object> hasNotProps,
2091                                                                                                           String modelName) {
2092         List<Service> services = new ArrayList<>();
2093         List<LifecycleStateEnum> states = new ArrayList<>();
2094         // include props
2095         hasProps.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
2096         hasProps.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
2097         if (modelName != null) {
2098             hasProps.put(GraphPropertyEnum.MODEL, modelName);
2099         }
2100         // exclude props
2101         states.add(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
2102         hasNotProps.put(GraphPropertyEnum.STATE, states);
2103         hasNotProps.put(GraphPropertyEnum.IS_DELETED, true);
2104         hasNotProps.put(GraphPropertyEnum.IS_ARCHIVED, true);
2105         return fetchServicesByCriteria(services, hasProps, hasNotProps, modelName);
2106     }
2107
2108     private Either<List<Component>, StorageOperationStatus> getLatestVersionNotAbstractToscaElementsMetadataOnly(final boolean isAbstract,
2109                                                                                                                  final ComponentTypeEnum componentTypeEnum,
2110                                                                                                                  final String internalComponentType,
2111                                                                                                                  final VertexTypeEnum vertexType,
2112                                                                                                                  final String modelName,
2113                                                                                                                  final boolean includeNormativeExtensionModels) {
2114         List<Service> services = null;
2115         Map<GraphPropertyEnum, Object> hasProps = new EnumMap<>(GraphPropertyEnum.class);
2116         Map<GraphPropertyEnum, Object> hasNotProps = new EnumMap<>(GraphPropertyEnum.class);
2117         fillPropsMap(hasProps, hasNotProps, internalComponentType, componentTypeEnum, isAbstract, vertexType, modelName);
2118         Either<List<GraphVertex>, JanusGraphOperationStatus> getRes = janusGraphDao
2119             .getByCriteria(vertexType, hasProps, hasNotProps, JsonParseFlagEnum.ParseMetadata, modelName, includeNormativeExtensionModels);
2120         if (getRes.isRight() && !JanusGraphOperationStatus.NOT_FOUND.equals(getRes.right().value())) {
2121             return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getRes.right().value()));
2122         }
2123         // region -> Fetch non checked-out services
2124         if (internalComponentType != null && internalComponentType.toLowerCase().trim().equals(SERVICE) && VertexTypeEnum.NODE_TYPE == vertexType) {
2125             Either<List<Service>, StorageOperationStatus> result = getLatestVersionNonCheckoutServicesMetadataOnly(
2126                 new EnumMap<>(GraphPropertyEnum.class), new EnumMap<>(GraphPropertyEnum.class), modelName);
2127             if (result.isRight()) {
2128                 log.debug("Failed to fetch services for");
2129                 return Either.right(result.right().value());
2130             }
2131             services = result.left().value();
2132             if (log.isTraceEnabled() && isEmpty(services)) {
2133                 log.trace("No relevant services available");
2134             }
2135         }
2136         // endregion
2137         List<Component> nonAbstractLatestComponents = new ArrayList<>();
2138         ComponentParametersView params = new ComponentParametersView(true);
2139         params.setIgnoreAllVersions(false);
2140         if (getRes.isLeft()) {
2141             for (GraphVertex vertexComponent : getRes.left().value()) {
2142                 Either<ToscaElement, StorageOperationStatus> componentRes = topologyTemplateOperation
2143                     .getLightComponent(vertexComponent, componentTypeEnum, params);
2144                 if (componentRes.isRight()) {
2145                     log.debug("Failed to fetch light element for {} error {}", vertexComponent.getUniqueId(), componentRes.right().value());
2146                     return Either.right(componentRes.right().value());
2147                 } else {
2148                     Component component = ModelConverter.convertFromToscaElement(componentRes.left().value());
2149                     nonAbstractLatestComponents.add(component);
2150                 }
2151             }
2152         }
2153         if (CollectionUtils.isNotEmpty(services)) {
2154             nonAbstractLatestComponents.addAll(services);
2155         }
2156         return Either.left(nonAbstractLatestComponents);
2157     }
2158
2159     public Either<ComponentMetadataData, StorageOperationStatus> getLatestComponentMetadataByUuid(String componentUuid, JsonParseFlagEnum parseFlag,
2160                                                                                                   Boolean isHighest) {
2161         Either<ComponentMetadataData, StorageOperationStatus> result;
2162         Map<GraphPropertyEnum, Object> hasProperties = new EnumMap<>(GraphPropertyEnum.class);
2163         hasProperties.put(GraphPropertyEnum.UUID, componentUuid);
2164         if (isHighest != null) {
2165             hasProperties.put(GraphPropertyEnum.IS_HIGHEST_VERSION, isHighest);
2166         }
2167         Map<GraphPropertyEnum, Object> propertiesNotToMatch = new EnumMap<>(GraphPropertyEnum.class);
2168         propertiesNotToMatch.put(GraphPropertyEnum.IS_DELETED, true);
2169         propertiesNotToMatch.put(GraphPropertyEnum.IS_ARCHIVED, true); //US382674, US382683
2170         Either<List<GraphVertex>, JanusGraphOperationStatus> getRes = janusGraphDao
2171             .getByCriteria(null, hasProperties, propertiesNotToMatch, parseFlag);
2172         if (getRes.isRight()) {
2173             result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getRes.right().value()));
2174         } else {
2175             List<ComponentMetadataData> latestVersionList = getRes.left().value().stream().map(ModelConverter::convertToComponentMetadata)
2176                 .collect(Collectors.toList());
2177             ComponentMetadataData latestVersion = latestVersionList.size() == 1 ? latestVersionList.get(0) : latestVersionList.stream().max(
2178                 (c1, c2) -> Double.compare(Double.parseDouble(c1.getMetadataDataDefinition().getVersion()),
2179                     Double.parseDouble(c2.getMetadataDataDefinition().getVersion()))).get();
2180             result = Either.left(latestVersion);
2181         }
2182         return result;
2183     }
2184
2185     public Either<ComponentMetadataData, StorageOperationStatus> getComponentMetadata(String componentId) {
2186         Either<ComponentMetadataData, StorageOperationStatus> result;
2187         Either<GraphVertex, JanusGraphOperationStatus> getRes = janusGraphDao.getVertexById(componentId, JsonParseFlagEnum.ParseMetadata);
2188         if (getRes.isRight()) {
2189             result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getRes.right().value()));
2190         } else {
2191             ComponentMetadataData componentMetadata = ModelConverter.convertToComponentMetadata(getRes.left().value());
2192             result = Either.left(componentMetadata);
2193         }
2194         return result;
2195     }
2196
2197     public Either<List<Component>, StorageOperationStatus> getLatestVersionNotAbstractComponents(boolean isAbstract,
2198                                                                                                  ComponentTypeEnum componentTypeEnum,
2199                                                                                                  String internalComponentType,
2200                                                                                                  List<String> componentUids) {
2201         List<Component> components = new ArrayList<>();
2202         if (componentUids == null) {
2203             Either<List<String>, StorageOperationStatus> componentUidsRes = getComponentUids(isAbstract, componentTypeEnum, internalComponentType);
2204             if (componentUidsRes.isRight()) {
2205                 return Either.right(componentUidsRes.right().value());
2206             }
2207             componentUids = componentUidsRes.left().value();
2208         }
2209         if (!isEmpty(componentUids)) {
2210             for (String componentUid : componentUids) {
2211                 ComponentParametersView componentParametersView = buildComponentViewForNotAbstract();
2212                 if ("vl".equalsIgnoreCase(internalComponentType)) {
2213                     componentParametersView.setIgnoreCapabilities(false);
2214                     componentParametersView.setIgnoreRequirements(false);
2215                 }
2216                 Either<ToscaElement, StorageOperationStatus> getToscaElementRes = nodeTemplateOperation.getToscaElementOperation(componentTypeEnum)
2217                     .getLightComponent(componentUid, componentTypeEnum, componentParametersView);
2218                 if (getToscaElementRes.isRight()) {
2219                     log.debug("Failed to fetch resource for error is {}", getToscaElementRes.right().value());
2220                     return Either.right(getToscaElementRes.right().value());
2221                 }
2222                 Component component = ModelConverter.convertFromToscaElement(getToscaElementRes.left().value());
2223                 nullifySomeComponentProperties(component);
2224                 components.add(component);
2225             }
2226         }
2227         return Either.left(components);
2228     }
2229
2230     public void nullifySomeComponentProperties(Component component) {
2231         component.setContactId(null);
2232         component.setCreationDate(null);
2233         component.setCreatorUserId(null);
2234         component.setCreatorFullName(null);
2235         component.setLastUpdateDate(null);
2236         component.setLastUpdaterUserId(null);
2237         component.setLastUpdaterFullName(null);
2238         component.setNormalizedName(null);
2239     }
2240
2241     private Either<List<String>, StorageOperationStatus> getComponentUids(boolean isAbstract, ComponentTypeEnum componentTypeEnum,
2242                                                                           String internalComponentType) {
2243         Either<List<Component>, StorageOperationStatus> getToscaElementsRes = getLatestVersionNotAbstractMetadataOnly(isAbstract, componentTypeEnum,
2244             internalComponentType, null, false);
2245         if (getToscaElementsRes.isRight()) {
2246             return Either.right(getToscaElementsRes.right().value());
2247         }
2248         List<Component> collection = getToscaElementsRes.left().value();
2249         List<String> componentUids;
2250         if (collection == null) {
2251             componentUids = new ArrayList<>();
2252         } else {
2253             componentUids = collection.stream().map(Component::getUniqueId).collect(Collectors.toList());
2254         }
2255         return Either.left(componentUids);
2256     }
2257
2258     private ComponentParametersView buildComponentViewForNotAbstract() {
2259         ComponentParametersView componentParametersView = new ComponentParametersView();
2260         componentParametersView.disableAll();
2261         componentParametersView.setIgnoreCategories(false);
2262         componentParametersView.setIgnoreAllVersions(false);
2263         return componentParametersView;
2264     }
2265
2266     public Either<Boolean, StorageOperationStatus> validateComponentNameExists(String name, ResourceTypeEnum resourceType,
2267                                                                                ComponentTypeEnum componentType) {
2268         Either<Boolean, StorageOperationStatus> result = validateComponentNameUniqueness(name, resourceType, componentType);
2269         if (result.isLeft()) {
2270             result = Either.left(!result.left().value());
2271         }
2272         return result;
2273     }
2274
2275     public Either<Boolean, StorageOperationStatus> validateComponentNameUniqueness(String name, ResourceTypeEnum resourceType,
2276                                                                                    ComponentTypeEnum componentType) {
2277         String normalizedName = ValidationUtils.normaliseComponentName(name);
2278         Either<List<GraphVertex>, JanusGraphOperationStatus> vertexEither = janusGraphDao
2279             .getByCriteria(getVertexTypeEnum(resourceType), propertiesToMatch(normalizedName, componentType), JsonParseFlagEnum.NoParse);
2280         if (vertexEither.isRight() && vertexEither.right().value() != JanusGraphOperationStatus.NOT_FOUND) {
2281             log.debug("failed to get vertex from graph with property normalizedName: {}", normalizedName);
2282             return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(vertexEither.right().value()));
2283         }
2284         return Either.left(CollectionUtils.isEmpty(vertexEither.isLeft() ? vertexEither.left().value() : null));
2285     }
2286
2287     public Either<Boolean, StorageOperationStatus> validateComponentNameAndModelExists(final String resourceName, final String model,
2288                                                                                        final ResourceTypeEnum resourceType,
2289                                                                                        final ComponentTypeEnum componentType) {
2290         Either<Boolean, StorageOperationStatus> result = validateComponentNameAndModelUniqueness(resourceName, model, resourceType, componentType);
2291         if (result.isLeft()) {
2292             result = Either.left(!result.left().value());
2293         }
2294         return result;
2295     }
2296
2297     private Either<Boolean, StorageOperationStatus> validateComponentNameAndModelUniqueness(final String resourceName, final String modelName,
2298                                                                                             final ResourceTypeEnum resourceType,
2299                                                                                             final ComponentTypeEnum componentType) {
2300         final String normalizedName = ValidationUtils.normaliseComponentName(resourceName);
2301         final Either<List<GraphVertex>, JanusGraphOperationStatus> vertexEither = janusGraphDao
2302             .getByCriteria(getVertexTypeEnum(resourceType), propertiesToMatch(normalizedName, componentType), null, null, JsonParseFlagEnum.NoParse,
2303                 modelName);
2304         if (vertexEither.isRight() && vertexEither.right().value() != JanusGraphOperationStatus.NOT_FOUND) {
2305             log.debug("failed to get vertex from graph with property normalizedName: {} and model: {}", normalizedName, modelName);
2306             return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(vertexEither.right().value()));
2307         }
2308         return Either.left(CollectionUtils.isEmpty(vertexEither.isLeft() ? vertexEither.left().value().stream()
2309             .collect(Collectors.toList()) : null));
2310     }
2311
2312     private VertexTypeEnum getVertexTypeEnum(final ResourceTypeEnum resourceType) {
2313         return ModelConverter.isAtomicComponent(resourceType) ? VertexTypeEnum.NODE_TYPE : VertexTypeEnum.TOPOLOGY_TEMPLATE;
2314     }
2315
2316     private Map<GraphPropertyEnum, Object> propertiesToMatch(final String normalizedName, final ComponentTypeEnum componentType) {
2317         final Map<GraphPropertyEnum, Object> properties = new EnumMap<>(GraphPropertyEnum.class);
2318         properties.put(GraphPropertyEnum.NORMALIZED_NAME, normalizedName);
2319         properties.put(GraphPropertyEnum.COMPONENT_TYPE, componentType.name());
2320         return properties;
2321     }
2322
2323     private void fillNodeTypePropsMap(final Map<GraphPropertyEnum, Object> hasProps, final Map<GraphPropertyEnum, Object> hasNotProps,
2324                                       final String internalComponentType, String modelName) {
2325         final Configuration configuration = ConfigurationManager.getConfigurationManager().getConfiguration();
2326         final List<String> allowedTypes;
2327         if (ComponentTypeEnum.SERVICE.getValue().equalsIgnoreCase(internalComponentType)) {
2328             allowedTypes = containerInstanceTypesData.getServiceAllowedList(modelName);
2329         } else {
2330             final ResourceTypeEnum resourceType = ResourceTypeEnum.getTypeIgnoreCase(internalComponentType);
2331             allowedTypes = containerInstanceTypesData.getComponentAllowedList(ComponentTypeEnum.RESOURCE, resourceType);
2332         }
2333         final List<String> allResourceTypes = configuration.getResourceTypes();
2334         if (allowedTypes == null) {
2335             hasNotProps.put(GraphPropertyEnum.RESOURCE_TYPE, allResourceTypes);
2336             return;
2337         }
2338         if (ResourceTypeEnum.VL.getValue().equalsIgnoreCase(internalComponentType)) {
2339             hasProps.put(GraphPropertyEnum.RESOURCE_TYPE, allowedTypes);
2340         } else {
2341             final List<String> notAllowedTypes = allResourceTypes.stream().filter(s -> !allowedTypes.contains(s)).collect(Collectors.toList());
2342             hasNotProps.put(GraphPropertyEnum.RESOURCE_TYPE, notAllowedTypes);
2343         }
2344     }
2345
2346     private void fillTopologyTemplatePropsMap(Map<GraphPropertyEnum, Object> hasProps, Map<GraphPropertyEnum, Object> hasNotProps,
2347                                               ComponentTypeEnum componentTypeEnum) {
2348         switch (componentTypeEnum) {
2349             case RESOURCE:
2350                 hasProps.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
2351                 break;
2352             case SERVICE:
2353                 hasProps.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
2354                 break;
2355             default:
2356                 break;
2357         }
2358         hasNotProps.put(GraphPropertyEnum.RESOURCE_TYPE, ResourceTypeEnum.CVFC.name());
2359     }
2360
2361     private void fillPropsMap(Map<GraphPropertyEnum, Object> hasProps, Map<GraphPropertyEnum, Object> hasNotProps, String internalComponentType,
2362                               ComponentTypeEnum componentTypeEnum, boolean isAbstract, VertexTypeEnum internalVertexType, String modelName) {
2363         hasNotProps.put(GraphPropertyEnum.STATE, LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT.name());
2364         hasNotProps.put(GraphPropertyEnum.IS_DELETED, true);
2365         hasNotProps.put(GraphPropertyEnum.IS_ARCHIVED, true);
2366         hasProps.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
2367
2368         if (VertexTypeEnum.NODE_TYPE == internalVertexType) {
2369             hasProps.put(GraphPropertyEnum.IS_ABSTRACT, isAbstract);
2370             if (internalComponentType != null) {
2371                 fillNodeTypePropsMap(hasProps, hasNotProps, internalComponentType, modelName);
2372             }
2373         } else {
2374             fillTopologyTemplatePropsMap(hasProps, hasNotProps, componentTypeEnum);
2375         }
2376     }
2377
2378     private List<VertexTypeEnum> getInternalVertexTypes(ComponentTypeEnum componentTypeEnum, String internalComponentType) {
2379         List<VertexTypeEnum> internalVertexTypes = new ArrayList<>();
2380         if (ComponentTypeEnum.RESOURCE == componentTypeEnum) {
2381             internalVertexTypes.add(VertexTypeEnum.NODE_TYPE);
2382         }
2383         if (ComponentTypeEnum.SERVICE == componentTypeEnum || SERVICE.equalsIgnoreCase(internalComponentType) || VF.equalsIgnoreCase(
2384             internalComponentType)) {
2385             internalVertexTypes.add(VertexTypeEnum.TOPOLOGY_TEMPLATE);
2386         }
2387         return internalVertexTypes;
2388     }
2389
2390     public Either<List<Component>, StorageOperationStatus> getLatestVersionNotAbstractMetadataOnly(boolean isAbstract,
2391                                                                                                    final ComponentTypeEnum componentTypeEnum,
2392                                                                                                    final String internalComponentType,
2393                                                                                                    final String modelName,
2394                                                                                                    final boolean includeNormativeExtensionModels) {
2395         List<VertexTypeEnum> internalVertexTypes = getInternalVertexTypes(componentTypeEnum, internalComponentType);
2396         List<Component> result = new ArrayList<>();
2397         for (VertexTypeEnum vertexType : internalVertexTypes) {
2398             Either<List<Component>, StorageOperationStatus> listByVertexType = getLatestVersionNotAbstractToscaElementsMetadataOnly(isAbstract,
2399                 componentTypeEnum, internalComponentType, vertexType, modelName, includeNormativeExtensionModels);
2400             if (listByVertexType.isRight()) {
2401                 return listByVertexType;
2402             }
2403             result.addAll(listByVertexType.left().value());
2404         }
2405         return Either.left(result);
2406     }
2407
2408     private Either<List<Component>, StorageOperationStatus> getLatestComponentListByUuid(String componentUuid,
2409                                                                                          Map<GraphPropertyEnum, Object> additionalPropertiesToMatch) {
2410         Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
2411         if (additionalPropertiesToMatch != null) {
2412             propertiesToMatch.putAll(additionalPropertiesToMatch);
2413         }
2414         propertiesToMatch.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
2415         return getComponentListByUuid(componentUuid, propertiesToMatch);
2416     }
2417
2418     public Either<Component, StorageOperationStatus> getComponentByUuidAndVersion(String componentUuid, String version) {
2419         Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
2420         propertiesToMatch.put(GraphPropertyEnum.UUID, componentUuid);
2421         propertiesToMatch.put(GraphPropertyEnum.VERSION, version);
2422         Map<GraphPropertyEnum, Object> propertiesNotToMatch = new EnumMap<>(GraphPropertyEnum.class);
2423         propertiesNotToMatch.put(GraphPropertyEnum.IS_DELETED, true);
2424         Either<List<GraphVertex>, JanusGraphOperationStatus> vertexEither = janusGraphDao
2425             .getByCriteria(null, propertiesToMatch, propertiesNotToMatch, JsonParseFlagEnum.ParseAll);
2426         if (vertexEither.isRight()) {
2427             return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(vertexEither.right().value()));
2428         }
2429         List<GraphVertex> vertexList = vertexEither.isLeft() ? vertexEither.left().value() : null;
2430         if (vertexList == null || vertexList.isEmpty() || vertexList.size() > 1) {
2431             return Either.right(StorageOperationStatus.NOT_FOUND);
2432         }
2433         return getToscaElementByOperation(vertexList.get(0));
2434     }
2435
2436     public Either<List<Component>, StorageOperationStatus> getComponentListByUuid(String componentUuid,
2437                                                                                   Map<GraphPropertyEnum, Object> additionalPropertiesToMatch) {
2438         Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
2439         if (additionalPropertiesToMatch != null) {
2440             propertiesToMatch.putAll(additionalPropertiesToMatch);
2441         }
2442         propertiesToMatch.put(GraphPropertyEnum.UUID, componentUuid);
2443         Map<GraphPropertyEnum, Object> propertiesNotToMatch = new EnumMap<>(GraphPropertyEnum.class);
2444         propertiesNotToMatch.put(GraphPropertyEnum.IS_DELETED, true);
2445         propertiesNotToMatch.put(GraphPropertyEnum.IS_ARCHIVED, true); //US382674, US382683
2446         Either<List<GraphVertex>, JanusGraphOperationStatus> vertexEither = janusGraphDao
2447             .getByCriteria(null, propertiesToMatch, propertiesNotToMatch, JsonParseFlagEnum.ParseAll);
2448         if (vertexEither.isRight()) {
2449             log.debug("Couldn't fetch metadata for component with uuid {}, error: {}", componentUuid, vertexEither.right().value());
2450             return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(vertexEither.right().value()));
2451         }
2452         List<GraphVertex> vertexList = vertexEither.isLeft() ? vertexEither.left().value() : null;
2453         if (vertexList == null || vertexList.isEmpty()) {
2454             log.debug("Component with uuid {} was not found", componentUuid);
2455             return Either.right(StorageOperationStatus.NOT_FOUND);
2456         }
2457         ArrayList<Component> latestComponents = new ArrayList<>();
2458         for (GraphVertex vertex : vertexList) {
2459             Either<Component, StorageOperationStatus> toscaElementByOperation = getToscaElementByOperation(vertex);
2460             if (toscaElementByOperation.isRight()) {
2461                 log.debug("Could not fetch the following Component by UUID {}", vertex.getUniqueId());
2462                 return Either.right(toscaElementByOperation.right().value());
2463             }
2464             latestComponents.add(toscaElementByOperation.left().value());
2465         }
2466         if (latestComponents.size() > 1) {
2467             for (Component component : latestComponents) {
2468                 if (Boolean.TRUE.equals(component.isHighestVersion())) {
2469                     LinkedList<Component> highestComponent = new LinkedList<>();
2470                     highestComponent.add(component);
2471                     return Either.left(highestComponent);
2472                 }
2473             }
2474         }
2475         return Either.left(latestComponents);
2476     }
2477
2478     public Either<Component, StorageOperationStatus> getLatestServiceByUuid(String serviceUuid) {
2479         Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
2480         propertiesToMatch.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
2481         return getLatestComponentByUuid(serviceUuid, propertiesToMatch);
2482     }
2483
2484     public Either<Component, StorageOperationStatus> getLatestComponentByUuid(String componentUuid) {
2485         return getLatestComponentByUuid(componentUuid, null);
2486     }
2487
2488     public Either<Component, StorageOperationStatus> getLatestComponentByUuid(String componentUuid,
2489                                                                               Map<GraphPropertyEnum, Object> propertiesToMatch) {
2490         Either<List<Component>, StorageOperationStatus> latestVersionListEither = getLatestComponentListByUuid(componentUuid, propertiesToMatch);
2491         if (latestVersionListEither.isRight()) {
2492             return Either.right(latestVersionListEither.right().value());
2493         }
2494         List<Component> latestVersionList = latestVersionListEither.left().value();
2495         if (latestVersionList.isEmpty()) {
2496             return Either.right(StorageOperationStatus.NOT_FOUND);
2497         }
2498         Component component = latestVersionList.size() == 1 ? latestVersionList.get(0)
2499             : latestVersionList.stream().max((c1, c2) -> Double.compare(Double.parseDouble(c1.getVersion()), Double.parseDouble(c2.getVersion())))
2500                 .get();
2501         return Either.left(component);
2502     }
2503
2504     public Either<List<Resource>, StorageOperationStatus> getAllCertifiedResources(boolean isAbstract, Boolean isHighest) {
2505         List<Resource> resources = new ArrayList<>();
2506         Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
2507         Map<GraphPropertyEnum, Object> propertiesNotToMatch = new EnumMap<>(GraphPropertyEnum.class);
2508         propertiesToMatch.put(GraphPropertyEnum.IS_ABSTRACT, isAbstract);
2509         if (isHighest != null) {
2510             propertiesToMatch.put(GraphPropertyEnum.IS_HIGHEST_VERSION, isHighest);
2511         }
2512         propertiesToMatch.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
2513         propertiesToMatch.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
2514         propertiesNotToMatch.put(GraphPropertyEnum.IS_DELETED, true);
2515         Either<List<GraphVertex>, JanusGraphOperationStatus> getResourcesRes = janusGraphDao
2516             .getByCriteria(null, propertiesToMatch, propertiesNotToMatch, JsonParseFlagEnum.ParseAll);
2517         if (getResourcesRes.isRight()) {
2518             log.debug("Failed to fetch all certified resources. Status is {}", getResourcesRes.right().value());
2519             return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getResourcesRes.right().value()));
2520         }
2521         List<GraphVertex> resourceVerticies = getResourcesRes.left().value();
2522         for (GraphVertex resourceV : resourceVerticies) {
2523             Either<Resource, StorageOperationStatus> getResourceRes = getToscaElement(resourceV);
2524             if (getResourceRes.isRight()) {
2525                 return Either.right(getResourceRes.right().value());
2526             }
2527             resources.add(getResourceRes.left().value());
2528         }
2529         return Either.left(resources);
2530     }
2531
2532     public <T extends Component> Either<T, StorageOperationStatus> getLatestByNameAndVersion(String name, String version,
2533                                                                                              JsonParseFlagEnum parseFlag, String model) {
2534         Either<T, StorageOperationStatus> result;
2535         Map<GraphPropertyEnum, Object> hasProperties = new EnumMap<>(GraphPropertyEnum.class);
2536         Map<GraphPropertyEnum, Object> hasNotProperties = new EnumMap<>(GraphPropertyEnum.class);
2537         hasProperties.put(GraphPropertyEnum.NAME, name);
2538         hasProperties.put(GraphPropertyEnum.VERSION, version);
2539         hasProperties.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
2540         hasNotProperties.put(GraphPropertyEnum.IS_DELETED, true);
2541         Either<List<GraphVertex>, JanusGraphOperationStatus> getResourceRes = janusGraphDao
2542             .getByCriteria(null, hasProperties, hasNotProperties, parseFlag, model);
2543         if (getResourceRes.isRight()) {
2544             JanusGraphOperationStatus status = getResourceRes.right().value();
2545             log.debug("failed to find resource with name {}, version {}. Status is {} ", name, version, status);
2546             result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
2547             return result;
2548         }
2549         return getToscaElementByOperation(getResourceRes.left().value().get(0));
2550     }
2551
2552     public Either<Resource, StorageOperationStatus> getLatestComponentByCsarOrName(ComponentTypeEnum componentType, String csarUUID,
2553                                                                                    String systemName) {
2554         return getLatestComponentByCsarOrName(componentType, csarUUID, systemName, JsonParseFlagEnum.ParseAll);
2555     }
2556
2557     public Either<Resource, StorageOperationStatus> getLatestComponentByCsarOrName(ComponentTypeEnum componentType, String csarUUID,
2558                                                                                    String systemName, JsonParseFlagEnum parseFlag) {
2559         Map<GraphPropertyEnum, Object> props = new EnumMap<>(GraphPropertyEnum.class);
2560         Map<GraphPropertyEnum, Object> propsHasNot = new EnumMap<>(GraphPropertyEnum.class);
2561         props.put(GraphPropertyEnum.CSAR_UUID, csarUUID);
2562         props.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
2563         if (componentType != null) {
2564             props.put(GraphPropertyEnum.COMPONENT_TYPE, componentType.name());
2565         }
2566         propsHasNot.put(GraphPropertyEnum.IS_DELETED, true);
2567         GraphVertex resourceMetadataData = null;
2568         List<GraphVertex> resourceMetadataDataList = null;
2569         Either<List<GraphVertex>, JanusGraphOperationStatus> byCsar = janusGraphDao
2570             .getByCriteria(null, props, propsHasNot, JsonParseFlagEnum.ParseMetadata);
2571         if (byCsar.isRight()) {
2572             if (JanusGraphOperationStatus.NOT_FOUND == byCsar.right().value()) {
2573                 // Fix Defect DE256036
2574                 if (StringUtils.isEmpty(systemName)) {
2575                     return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(JanusGraphOperationStatus.NOT_FOUND));
2576                 }
2577                 props.clear();
2578                 props.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
2579                 props.put(GraphPropertyEnum.SYSTEM_NAME, systemName);
2580                 Either<List<GraphVertex>, JanusGraphOperationStatus> bySystemname = janusGraphDao
2581                     .getByCriteria(null, props, JsonParseFlagEnum.ParseMetadata);
2582                 if (bySystemname.isRight()) {
2583                     log.debug("getLatestResourceByCsarOrName - Failed to find by system name {}  error {} ", systemName,
2584                         bySystemname.right().value());
2585                     return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(bySystemname.right().value()));
2586                 }
2587                 if (bySystemname.left().value().size() > 2) {
2588                     log.debug(
2589                         "getLatestResourceByCsarOrName - getByCriteria(by system name) must return only 2 latest version, but was returned - {}",
2590                         bySystemname.left().value().size());
2591                     return Either.right(StorageOperationStatus.GENERAL_ERROR);
2592                 }
2593                 resourceMetadataDataList = bySystemname.left().value();
2594                 if (resourceMetadataDataList.size() == 1) {
2595                     resourceMetadataData = resourceMetadataDataList.get(0);
2596                 } else {
2597                     for (GraphVertex curResource : resourceMetadataDataList) {
2598                         if (!((String) curResource.getJsonMetadataField(JsonPresentationFields.LIFECYCLE_STATE)).equals("CERTIFIED")) {
2599                             resourceMetadataData = curResource;
2600                             break;
2601                         }
2602                     }
2603                 }
2604                 if (resourceMetadataData == null) {
2605                     log.debug("getLatestResourceByCsarOrName - getByCriteria(by system name) returned 2 latest CERTIFIED versions");
2606                     return Either.right(StorageOperationStatus.GENERAL_ERROR);
2607                 }
2608                 final Object csarUuid = resourceMetadataData.getJsonMetadataField(JsonPresentationFields.CSAR_UUID);
2609                 if (csarUuid != null && !csarUuid.equals(csarUUID)) {
2610                     log.debug("getLatestResourceByCsarOrName - same system name {} but different csarUUID. exist {} and new {} ", systemName,
2611                         csarUuid, csarUUID);
2612                     // correct error will be returned from create flow. with all
2613
2614                     // correct audit records!!!!!
2615                     return Either.right(StorageOperationStatus.NOT_FOUND);
2616                 }
2617                 return getToscaElement(resourceMetadataData.getUniqueId());
2618             }
2619         } else {
2620             resourceMetadataDataList = byCsar.left().value();
2621             if (resourceMetadataDataList.size() > 2) {
2622                 log.debug("getLatestResourceByCsarOrName - getByCriteria(by csar) must return only 2 latest version, but was returned - {}",
2623                     byCsar.left().value().size());
2624                 return Either.right(StorageOperationStatus.GENERAL_ERROR);
2625             }
2626             if (resourceMetadataDataList.size() == 1) {
2627                 resourceMetadataData = resourceMetadataDataList.get(0);
2628             } else {
2629                 for (GraphVertex curResource : resourceMetadataDataList) {
2630                     if (!((String) curResource.getJsonMetadataField(JsonPresentationFields.LIFECYCLE_STATE)).equals("CERTIFIED")) {
2631                         resourceMetadataData = curResource;
2632                         break;
2633                     }
2634                 }
2635             }
2636             if (resourceMetadataData == null) {
2637                 log.debug("getLatestResourceByCsarOrName - getByCriteria(by csar) returned 2 latest CERTIFIED versions");
2638                 return Either.right(StorageOperationStatus.GENERAL_ERROR);
2639             }
2640             return getToscaElement((String) resourceMetadataData.getJsonMetadataField(JsonPresentationFields.UNIQUE_ID), parseFlag);
2641         }
2642         return null;
2643     }
2644
2645     public Either<Boolean, StorageOperationStatus> validateToscaResourceNameExtends(String templateNameCurrent, String templateNameExtends,
2646                                                                                     String model) {
2647         String currentTemplateNameChecked = templateNameExtends;
2648         while (currentTemplateNameChecked != null && !currentTemplateNameChecked.equalsIgnoreCase(templateNameCurrent)) {
2649             Either<Resource, StorageOperationStatus> latestByToscaResourceName = getLatestByToscaResourceName(currentTemplateNameChecked, model);
2650             if (latestByToscaResourceName.isRight()) {
2651                 return latestByToscaResourceName.right().value() == StorageOperationStatus.NOT_FOUND ? Either.left(false)
2652                     : Either.right(latestByToscaResourceName.right().value());
2653             }
2654             Resource value = latestByToscaResourceName.left().value();
2655             if (value.getDerivedFrom() != null) {
2656                 currentTemplateNameChecked = value.getDerivedFrom().get(0);
2657             } else {
2658                 currentTemplateNameChecked = null;
2659             }
2660         }
2661         return (currentTemplateNameChecked != null && currentTemplateNameChecked.equalsIgnoreCase(templateNameCurrent)) ? Either.left(true)
2662             : Either.left(false);
2663     }
2664
2665     public Either<List<Component>, StorageOperationStatus> fetchMetaDataByResourceType(String resourceType, ComponentParametersView filterBy) {
2666         Map<GraphPropertyEnum, Object> props = new EnumMap<>(GraphPropertyEnum.class);
2667         props.put(GraphPropertyEnum.RESOURCE_TYPE, resourceType);
2668         props.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
2669         Map<GraphPropertyEnum, Object> propsHasNotToMatch = new EnumMap<>(GraphPropertyEnum.class);
2670         propsHasNotToMatch.put(GraphPropertyEnum.IS_DELETED, true);
2671         Either<List<GraphVertex>, JanusGraphOperationStatus> resourcesByTypeEither = janusGraphDao
2672             .getByCriteria(null, props, propsHasNotToMatch, JsonParseFlagEnum.ParseMetadata);
2673         if (resourcesByTypeEither.isRight()) {
2674             return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(resourcesByTypeEither.right().value()));
2675         }
2676         List<GraphVertex> vertexList = resourcesByTypeEither.left().value();
2677         List<Component> components = new ArrayList<>();
2678         for (GraphVertex vertex : vertexList) {
2679             components.add(getToscaElementByOperation(vertex, filterBy).left().value());
2680         }
2681         return Either.left(components);
2682     }
2683
2684     public void commit() {
2685         janusGraphDao.commit();
2686     }
2687
2688     public Either<Service, StorageOperationStatus> updateDistributionStatus(Service service, User user, DistributionStatusEnum distributionStatus) {
2689         Either<GraphVertex, StorageOperationStatus> updateDistributionStatus = topologyTemplateOperation
2690             .updateDistributionStatus(service.getUniqueId(), user, distributionStatus);
2691         if (updateDistributionStatus.isRight()) {
2692             return Either.right(updateDistributionStatus.right().value());
2693         }
2694         GraphVertex serviceV = updateDistributionStatus.left().value();
2695         service.setDistributionStatus(distributionStatus);
2696         service.setLastUpdateDate((Long) serviceV.getJsonMetadataField(JsonPresentationFields.LAST_UPDATE_DATE));
2697         return Either.left(service);
2698     }
2699
2700     public Either<ComponentMetadataData, StorageOperationStatus> updateComponentLastUpdateDateOnGraph(Component component) {
2701         Either<ComponentMetadataData, StorageOperationStatus> result = null;
2702         GraphVertex serviceVertex;
2703         Either<GraphVertex, JanusGraphOperationStatus> updateRes = null;
2704         Either<GraphVertex, JanusGraphOperationStatus> getRes = janusGraphDao.getVertexById(component.getUniqueId(), JsonParseFlagEnum.ParseMetadata);
2705         if (getRes.isRight()) {
2706             JanusGraphOperationStatus status = getRes.right().value();
2707             log.error("Failed to fetch component {}. status is {}", component.getUniqueId(), status);
2708             result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
2709         }
2710         if (result == null) {
2711             serviceVertex = getRes.left().value();
2712             long lastUpdateDate = System.currentTimeMillis();
2713             serviceVertex.setJsonMetadataField(JsonPresentationFields.LAST_UPDATE_DATE, lastUpdateDate);
2714             component.setLastUpdateDate(lastUpdateDate);
2715             updateRes = janusGraphDao.updateVertex(serviceVertex);
2716             if (updateRes.isRight()) {
2717                 result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(updateRes.right().value()));
2718             }
2719         }
2720         if (result == null) {
2721             result = Either.left(ModelConverter.convertToComponentMetadata(updateRes.left().value()));
2722         }
2723         return result;
2724     }
2725
2726     public HealingJanusGraphDao getJanusGraphDao() {
2727         return janusGraphDao;
2728     }
2729
2730     public Either<List<Service>, StorageOperationStatus> getCertifiedServicesWithDistStatus(Set<DistributionStatusEnum> distStatus) {
2731         Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
2732         propertiesToMatch.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
2733         return getServicesWithDistStatus(distStatus, propertiesToMatch);
2734     }
2735
2736     public Either<List<Service>, StorageOperationStatus> getServicesWithDistStatus(Set<DistributionStatusEnum> distStatus,
2737                                                                                    Map<GraphPropertyEnum, Object> additionalPropertiesToMatch) {
2738         List<Service> servicesAll = new ArrayList<>();
2739         Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
2740         Map<GraphPropertyEnum, Object> propertiesNotToMatch = new EnumMap<>(GraphPropertyEnum.class);
2741         if (additionalPropertiesToMatch != null && !additionalPropertiesToMatch.isEmpty()) {
2742             propertiesToMatch.putAll(additionalPropertiesToMatch);
2743         }
2744         propertiesToMatch.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
2745         propertiesNotToMatch.put(GraphPropertyEnum.IS_DELETED, true);
2746         if (distStatus != null && !distStatus.isEmpty()) {
2747             for (DistributionStatusEnum state : distStatus) {
2748                 propertiesToMatch.put(GraphPropertyEnum.DISTRIBUTION_STATUS, state.name());
2749                 Either<List<Service>, StorageOperationStatus> fetchServicesByCriteria = fetchServicesByCriteria(servicesAll, propertiesToMatch,
2750                     propertiesNotToMatch, null);
2751                 if (fetchServicesByCriteria.isRight()) {
2752                     return fetchServicesByCriteria;
2753                 } else {
2754                     servicesAll = fetchServicesByCriteria.left().value();
2755                 }
2756             }
2757             return Either.left(servicesAll);
2758         } else {
2759             return fetchServicesByCriteria(servicesAll, propertiesToMatch, propertiesNotToMatch, null);
2760         }
2761     }
2762
2763     private Either<List<Service>, StorageOperationStatus> fetchServicesByCriteria(List<Service> servicesAll,
2764                                                                                   Map<GraphPropertyEnum, Object> propertiesToMatch,
2765                                                                                   Map<GraphPropertyEnum, Object> propertiesNotToMatch,
2766                                                                                   String modelName) {
2767         Either<List<GraphVertex>, JanusGraphOperationStatus> getRes = janusGraphDao
2768             .getByCriteria(VertexTypeEnum.TOPOLOGY_TEMPLATE, propertiesToMatch, propertiesNotToMatch, JsonParseFlagEnum.ParseAll, modelName);
2769         if (getRes.isRight()) {
2770             if (getRes.right().value() != JanusGraphOperationStatus.NOT_FOUND) {
2771                 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG,
2772                     "Failed to fetch certified services by match properties {} not match properties {} . Status is {}. ", propertiesToMatch,
2773                     propertiesNotToMatch, getRes.right().value());
2774                 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getRes.right().value()));
2775             }
2776         } else {
2777             for (final GraphVertex vertex : getRes.left().value()) {
2778                 Either<ToscaElement, StorageOperationStatus> getServiceRes = topologyTemplateOperation
2779                     .getLightComponent(vertex, ComponentTypeEnum.SERVICE, new ComponentParametersView(true));
2780                 if (getServiceRes.isRight()) {
2781                     CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to fetch certified service {}. Status is {}. ",
2782                         vertex.getJsonMetadataField(JsonPresentationFields.NAME), getServiceRes.right().value());
2783                     return Either.right(getServiceRes.right().value());
2784                 } else {
2785                     servicesAll.add(ModelConverter.convertFromToscaElement(getServiceRes.left().value()));
2786                 }
2787             }
2788         }
2789         return Either.left(servicesAll);
2790     }
2791
2792     public void rollback() {
2793         janusGraphDao.rollback();
2794     }
2795
2796     public StorageOperationStatus addDeploymentArtifactsToInstance(String componentId, ComponentInstance componentInstance,
2797                                                                    Map<String, ArtifactDefinition> finalDeploymentArtifacts) {
2798         Map<String, ArtifactDataDefinition> instDeplArtifacts = finalDeploymentArtifacts.entrySet().stream()
2799             .collect(Collectors.toMap(Map.Entry::getKey, e -> new ArtifactDataDefinition(e.getValue())));
2800         return nodeTemplateOperation.addDeploymentArtifactsToInstance(componentId, componentInstance.getUniqueId(), instDeplArtifacts);
2801     }
2802
2803     public StorageOperationStatus addInformationalArtifactsToInstance(String componentId, ComponentInstance componentInstance,
2804                                                                       Map<String, ArtifactDefinition> artifacts) {
2805         StorageOperationStatus status = StorageOperationStatus.OK;
2806         if (MapUtils.isNotEmpty(artifacts)) {
2807             Map<String, ArtifactDataDefinition> instDeplArtifacts = artifacts.entrySet().stream()
2808                 .collect(Collectors.toMap(Map.Entry::getKey, e -> new ArtifactDataDefinition(e.getValue())));
2809             status = nodeTemplateOperation.addInformationalArtifactsToInstance(componentId, componentInstance.getUniqueId(), instDeplArtifacts);
2810         }
2811         return status;
2812     }
2813
2814     public StorageOperationStatus generateCustomizationUUIDOnInstance(String componentId, String instanceId) {
2815         return nodeTemplateOperation.generateCustomizationUUIDOnInstance(componentId, instanceId);
2816     }
2817
2818     public StorageOperationStatus generateCustomizationUUIDOnInstanceGroup(String componentId, String instanceId, List<String> groupInstances) {
2819         return nodeTemplateOperation.generateCustomizationUUIDOnInstanceGroup(componentId, instanceId, groupInstances);
2820     }
2821
2822     public Either<PropertyDefinition, StorageOperationStatus> addPropertyToComponent(PropertyDefinition newPropertyDefinition,
2823                                                                                      Component component) {
2824         final String propertyName = newPropertyDefinition.getName();
2825         StorageOperationStatus status = getToscaElementOperation(component)
2826             .addToscaDataToToscaElement(component.getUniqueId(), EdgeLabelEnum.PROPERTIES, VertexTypeEnum.PROPERTIES, newPropertyDefinition,
2827                 JsonPresentationFields.NAME);
2828         if (status != StorageOperationStatus.OK) {
2829             CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to add the property {} to the component {}. Status is {}. ", propertyName,
2830                 component.getName(), status);
2831             return Either.right(status);
2832         }
2833         ComponentParametersView filter = new ComponentParametersView(true);
2834         filter.setIgnoreProperties(false);
2835         filter.setIgnoreInputs(false);
2836         Either<Component, StorageOperationStatus> getUpdatedComponentRes = getToscaElement(component.getUniqueId(), filter);
2837         if (getUpdatedComponentRes.isRight()) {
2838             CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to get updated component {}. Status is {}. ", component.getUniqueId(),
2839                 getUpdatedComponentRes.right().value());
2840             return Either.right(status);
2841         }
2842         PropertyDefinition newProperty = null;
2843         List<PropertyDefinition> properties = (getUpdatedComponentRes.left().value()).getProperties();
2844         if (CollectionUtils.isNotEmpty(properties)) {
2845             Optional<PropertyDefinition> propertyOptional = properties.stream().filter(propertyEntry -> propertyEntry.getName().equals(propertyName))
2846                 .findAny();
2847             if (propertyOptional.isPresent()) {
2848                 newProperty = propertyOptional.get();
2849             }
2850         }
2851         if (newProperty == null) {
2852             CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to find recently added property {} on the component {}. Status is {}. ",
2853                 propertyName, component.getUniqueId(), StorageOperationStatus.NOT_FOUND);
2854             return Either.right(StorageOperationStatus.NOT_FOUND);
2855         }
2856         return Either.left(newProperty);
2857     }
2858
2859     public Either<InputDefinition, StorageOperationStatus> addInputToComponent(String inputName, InputDefinition newInputDefinition,
2860                                                                                Component component) {
2861         newInputDefinition.setName(inputName);
2862         StorageOperationStatus status = getToscaElementOperation(component)
2863             .addToscaDataToToscaElement(component.getUniqueId(), EdgeLabelEnum.INPUTS, VertexTypeEnum.INPUTS, newInputDefinition,
2864                 JsonPresentationFields.NAME);
2865         if (status != StorageOperationStatus.OK) {
2866             CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to add the input {} to the component {}. Status is {}. ", inputName,
2867                 component.getName(), status);
2868             return Either.right(status);
2869         }
2870         ComponentParametersView filter = new ComponentParametersView(true);
2871         filter.setIgnoreProperties(false);
2872         filter.setIgnoreInputs(false);
2873         Either<Component, StorageOperationStatus> getUpdatedComponentRes = getToscaElement(component.getUniqueId(), filter);
2874         if (getUpdatedComponentRes.isRight()) {
2875             CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to get updated component {}. Status is {}. ", component.getUniqueId(),
2876                 getUpdatedComponentRes.right().value());
2877             return Either.right(status);
2878         }
2879         InputDefinition newInput = null;
2880         List<InputDefinition> inputs = (getUpdatedComponentRes.left().value()).getInputs();
2881         if (CollectionUtils.isNotEmpty(inputs)) {
2882             Optional<InputDefinition> inputOptional = inputs.stream().filter(inputEntry -> inputEntry.getName().equals(inputName)).findAny();
2883             if (inputOptional.isPresent()) {
2884                 newInput = inputOptional.get();
2885             }
2886         }
2887         if (newInput == null) {
2888             CommonUtility
2889                 .addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to find recently added input {} " + "on the component {}. Status is {}. ", inputs,
2890                     component.getUniqueId(), StorageOperationStatus.NOT_FOUND);
2891             return Either.right(StorageOperationStatus.NOT_FOUND);
2892         }
2893         return Either.left(newInput);
2894     }
2895
2896     public StorageOperationStatus deletePropertyOfComponent(Component component, String propertyName) {
2897         return getToscaElementOperation(component)
2898             .deleteToscaDataElement(component.getUniqueId(), EdgeLabelEnum.PROPERTIES, VertexTypeEnum.PROPERTIES, propertyName,
2899                 JsonPresentationFields.NAME);
2900     }
2901
2902     public StorageOperationStatus deleteAttributeOfResource(Component component, String attributeName) {
2903         return getToscaElementOperation(component)
2904             .deleteToscaDataElement(component.getUniqueId(), EdgeLabelEnum.ATTRIBUTES, VertexTypeEnum.ATTRIBUTES, attributeName,
2905                 JsonPresentationFields.NAME);
2906     }
2907
2908     public StorageOperationStatus deleteInputOfResource(Component resource, String inputName) {
2909         return getToscaElementOperation(resource)
2910             .deleteToscaDataElement(resource.getUniqueId(), EdgeLabelEnum.INPUTS, VertexTypeEnum.INPUTS, inputName, JsonPresentationFields.NAME);
2911     }
2912
2913     public StorageOperationStatus deleteOutputOfResource(final Component resource, final String outputName) {
2914         return getToscaElementOperation(resource)
2915             .deleteToscaDataElement(resource.getUniqueId(), EdgeLabelEnum.OUTPUTS, VertexTypeEnum.OUTPUTS, outputName, JsonPresentationFields.NAME);
2916     }
2917
2918     /**
2919      * Deletes a data type from a component.
2920      *
2921      * @param component    the container which has the data type
2922      * @param dataTypeName the data type name to be deleted
2923      * @return Operation result.
2924      */
2925     public StorageOperationStatus deleteDataTypeOfComponent(Component component, String dataTypeName) {
2926         return getToscaElementOperation(component)
2927             .deleteToscaDataElement(component.getUniqueId(), EdgeLabelEnum.DATA_TYPES, VertexTypeEnum.DATA_TYPES, dataTypeName,
2928                 JsonPresentationFields.NAME);
2929     }
2930
2931     public Either<PropertyDefinition, StorageOperationStatus> updatePropertyOfComponent(Component component,
2932                                                                                         PropertyDefinition newPropertyDefinition) {
2933         Either<Component, StorageOperationStatus> getUpdatedComponentRes = null;
2934         Either<PropertyDefinition, StorageOperationStatus> result = null;
2935         StorageOperationStatus status = getToscaElementOperation(component)
2936             .updateToscaDataOfToscaElement(component.getUniqueId(), EdgeLabelEnum.PROPERTIES, VertexTypeEnum.PROPERTIES, newPropertyDefinition,
2937                 JsonPresentationFields.NAME);
2938         if (status != StorageOperationStatus.OK) {
2939             CommonUtility
2940                 .addRecordToLog(log, LogLevelEnum.DEBUG, FAILED_TO_ADD_THE_PROPERTY_TO_THE_RESOURCE_STATUS_IS, newPropertyDefinition.getName(),
2941                     component.getName(), status);
2942             result = Either.right(status);
2943         }
2944         if (result == null) {
2945             ComponentParametersView filter = new ComponentParametersView(true);
2946             filter.setIgnoreProperties(false);
2947             getUpdatedComponentRes = getToscaElement(component.getUniqueId(), filter);
2948             if (getUpdatedComponentRes.isRight()) {
2949                 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, FAILED_TO_GET_UPDATED_RESOURCE_STATUS_IS, component.getUniqueId(),
2950                     getUpdatedComponentRes.right().value());
2951                 result = Either.right(status);
2952             }
2953         }
2954         if (result == null) {
2955             Optional<PropertyDefinition> newProperty = (getUpdatedComponentRes.left().value()).getProperties().stream()
2956                 .filter(p -> p.getName().equals(newPropertyDefinition.getName())).findAny();
2957             if (newProperty.isPresent()) {
2958                 result = Either.left(newProperty.get());
2959             } else {
2960                 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, FAILED_TO_FIND_RECENTLY_ADDED_PROPERTY_ON_THE_RESOURCE_STATUS_IS,
2961                     newPropertyDefinition.getName(), component.getUniqueId(), StorageOperationStatus.NOT_FOUND);
2962                 result = Either.right(StorageOperationStatus.NOT_FOUND);
2963             }
2964         }
2965         return result;
2966     }
2967
2968     public Either<AttributeDefinition, StorageOperationStatus> updateAttributeOfComponent(Component component,
2969                                                                                           AttributeDefinition newPropertyDefinition) {
2970         Either<Component, StorageOperationStatus> getUpdatedComponentRes = null;
2971         Either<AttributeDefinition, StorageOperationStatus> result = null;
2972         StorageOperationStatus status = getToscaElementOperation(component)
2973             .updateToscaDataOfToscaElement(component.getUniqueId(), EdgeLabelEnum.ATTRIBUTES, VertexTypeEnum.ATTRIBUTES, newPropertyDefinition,
2974                 JsonPresentationFields.NAME);
2975         if (status != StorageOperationStatus.OK) {
2976             CommonUtility
2977                 .addRecordToLog(log, LogLevelEnum.DEBUG, FAILED_TO_ADD_THE_PROPERTY_TO_THE_RESOURCE_STATUS_IS, newPropertyDefinition.getName(),
2978                     component.getName(), status);
2979             result = Either.right(status);
2980         }
2981         if (result == null) {
2982             ComponentParametersView filter = new ComponentParametersView(true);
2983             filter.setIgnoreProperties(false);
2984             getUpdatedComponentRes = getToscaElement(component.getUniqueId(), filter);
2985             if (getUpdatedComponentRes.isRight()) {
2986                 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, FAILED_TO_GET_UPDATED_RESOURCE_STATUS_IS, component.getUniqueId(),
2987                     getUpdatedComponentRes.right().value());
2988                 result = Either.right(status);
2989             }
2990         }
2991         if (result == null) {
2992             Optional<AttributeDefinition> newProperty = (getUpdatedComponentRes.left().value()).getAttributes().stream()
2993                 .filter(p -> p.getName().equals(newPropertyDefinition.getName())).findAny();
2994             if (newProperty.isPresent()) {
2995                 result = Either.left(newProperty.get());
2996             } else {
2997                 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, FAILED_TO_FIND_RECENTLY_ADDED_PROPERTY_ON_THE_RESOURCE_STATUS_IS,
2998                     newPropertyDefinition.getName(), component.getUniqueId(), StorageOperationStatus.NOT_FOUND);
2999                 result = Either.right(StorageOperationStatus.NOT_FOUND);
3000             }
3001         }
3002         return result;
3003     }
3004
3005     public Either<AttributeDefinition, StorageOperationStatus> addAttributeOfResource(Component component, AttributeDefinition newAttributeDef) {
3006         Either<Component, StorageOperationStatus> getUpdatedComponentRes = null;
3007         Either<AttributeDefinition, StorageOperationStatus> result = null;
3008         if (newAttributeDef.getUniqueId() == null || newAttributeDef.getUniqueId().isEmpty()) {
3009             String attUniqueId = UniqueIdBuilder.buildAttributeUid(component.getUniqueId(), newAttributeDef.getName());
3010             newAttributeDef.setUniqueId(attUniqueId);
3011             newAttributeDef.setOwnerId(component.getUniqueId());
3012         }
3013         StorageOperationStatus status = getToscaElementOperation(component)
3014             .addToscaDataToToscaElement(component.getUniqueId(), EdgeLabelEnum.ATTRIBUTES, VertexTypeEnum.ATTRIBUTES, newAttributeDef,
3015                 JsonPresentationFields.NAME);
3016         if (status != StorageOperationStatus.OK) {
3017             CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, FAILED_TO_ADD_THE_PROPERTY_TO_THE_RESOURCE_STATUS_IS, newAttributeDef.getName(),
3018                 component.getName(), status);
3019             result = Either.right(status);
3020         }
3021         if (result == null) {
3022             ComponentParametersView filter = new ComponentParametersView(true);
3023             filter.setIgnoreAttributes(false);
3024             getUpdatedComponentRes = getToscaElement(component.getUniqueId(), filter);
3025             if (getUpdatedComponentRes.isRight()) {
3026                 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, FAILED_TO_GET_UPDATED_RESOURCE_STATUS_IS, component.getUniqueId(),
3027                     getUpdatedComponentRes.right().value());
3028                 result = Either.right(status);
3029             }
3030         }
3031         if (result == null) {
3032             Optional<AttributeDefinition> newAttribute = ((Resource) getUpdatedComponentRes.left().value()).getAttributes().stream()
3033                 .filter(p -> p.getName().equals(newAttributeDef.getName())).findAny();
3034             if (newAttribute.isPresent()) {
3035                 result = Either.left(newAttribute.get());
3036             } else {
3037                 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, FAILED_TO_FIND_RECENTLY_ADDED_PROPERTY_ON_THE_RESOURCE_STATUS_IS,
3038                     newAttributeDef.getName(), component.getUniqueId(), StorageOperationStatus.NOT_FOUND);
3039                 result = Either.right(StorageOperationStatus.NOT_FOUND);
3040             }
3041         }
3042         return result;
3043     }
3044
3045     public Either<AttributeDefinition, StorageOperationStatus> updateAttributeOfResource(Component component, AttributeDefinition newAttributeDef) {
3046         Either<Component, StorageOperationStatus> getUpdatedComponentRes = null;
3047         Either<AttributeDefinition, StorageOperationStatus> result = null;
3048         StorageOperationStatus status = getToscaElementOperation(component)
3049             .updateToscaDataOfToscaElement(component.getUniqueId(), EdgeLabelEnum.ATTRIBUTES, VertexTypeEnum.ATTRIBUTES, newAttributeDef,
3050                 JsonPresentationFields.NAME);
3051         if (status != StorageOperationStatus.OK) {
3052             CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, FAILED_TO_ADD_THE_PROPERTY_TO_THE_RESOURCE_STATUS_IS, newAttributeDef.getName(),
3053                 component.getName(), status);
3054             result = Either.right(status);
3055         }
3056         if (result == null) {
3057             ComponentParametersView filter = new ComponentParametersView(true);
3058             filter.setIgnoreAttributes(false);
3059             getUpdatedComponentRes = getToscaElement(component.getUniqueId(), filter);
3060             if (getUpdatedComponentRes.isRight()) {
3061                 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, FAILED_TO_GET_UPDATED_RESOURCE_STATUS_IS, component.getUniqueId(),
3062                     getUpdatedComponentRes.right().value());
3063                 result = Either.right(status);
3064             }
3065         }
3066         if (result == null) {
3067             Optional<AttributeDefinition> newProperty = ((Resource) getUpdatedComponentRes.left().value()).getAttributes().stream()
3068                 .filter(p -> p.getName().equals(newAttributeDef.getName())).findAny();
3069             if (newProperty.isPresent()) {
3070                 result = Either.left(newProperty.get());
3071             } else {
3072                 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, FAILED_TO_FIND_RECENTLY_ADDED_PROPERTY_ON_THE_RESOURCE_STATUS_IS,
3073                     newAttributeDef.getName(), component.getUniqueId(), StorageOperationStatus.NOT_FOUND);
3074                 result = Either.right(StorageOperationStatus.NOT_FOUND);
3075             }
3076         }
3077         return result;
3078     }
3079
3080     public Either<InputDefinition, StorageOperationStatus> updateInputOfComponent(Component component, InputDefinition newInputDefinition) {
3081         Either<Component, StorageOperationStatus> getUpdatedComponentRes = null;
3082         Either<InputDefinition, StorageOperationStatus> result = null;
3083         StorageOperationStatus status = getToscaElementOperation(component)
3084             .updateToscaDataOfToscaElement(component.getUniqueId(), EdgeLabelEnum.INPUTS, VertexTypeEnum.INPUTS, newInputDefinition,
3085                 JsonPresentationFields.NAME);
3086         if (status != StorageOperationStatus.OK) {
3087             CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to update the input {} to the component {}. Status is {}. ",
3088                 newInputDefinition.getName(), component.getName(), status);
3089             result = Either.right(status);
3090         }
3091         if (result == null) {
3092             ComponentParametersView filter = new ComponentParametersView(true);
3093             filter.setIgnoreInputs(false);
3094             getUpdatedComponentRes = getToscaElement(component.getUniqueId(), filter);
3095             if (getUpdatedComponentRes.isRight()) {
3096                 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, FAILED_TO_GET_UPDATED_RESOURCE_STATUS_IS, component.getUniqueId(),
3097                     getUpdatedComponentRes.right().value());
3098                 result = Either.right(status);
3099             }
3100         }
3101         if (result == null) {
3102             Optional<InputDefinition> updatedInput = getUpdatedComponentRes.left().value().getInputs().stream()
3103                 .filter(p -> p.getName().equals(newInputDefinition.getName())).findAny();
3104             if (updatedInput.isPresent()) {
3105                 result = Either.left(updatedInput.get());
3106             } else {
3107                 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to find recently updated inputs {} on the resource {}. Status is {}. ",
3108                     newInputDefinition.getName(), component.getUniqueId(), StorageOperationStatus.NOT_FOUND);
3109                 result = Either.right(StorageOperationStatus.NOT_FOUND);
3110             }
3111         }
3112         return result;
3113     }
3114
3115     /**
3116      * method - ename the group instances after referenced container name renamed flow - VF rename -(triggers)-> Group rename
3117      *
3118      * @param containerComponent  - container such as service
3119      * @param componentInstance   - context component
3120      * @param componentInstanceId - id
3121      * @return - successfull/failed status
3122      **/
3123     public Either<StorageOperationStatus, StorageOperationStatus> cleanAndAddGroupInstancesToComponentInstance(Component containerComponent,
3124                                                                                                                ComponentInstance componentInstance,
3125                                                                                                                String componentInstanceId) {
3126         String uniqueId = componentInstance.getUniqueId();
3127         StorageOperationStatus status = nodeTemplateOperation
3128             .deleteToscaDataDeepElementsBlockOfToscaElement(containerComponent.getUniqueId(), EdgeLabelEnum.INST_GROUPS, VertexTypeEnum.INST_GROUPS,
3129                 uniqueId);
3130         if (status != StorageOperationStatus.OK && status != StorageOperationStatus.NOT_FOUND) {
3131             CommonUtility
3132                 .addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to delete group instances for container {}. error {] ", componentInstanceId, status);
3133             return Either.right(status);
3134         }
3135         if (componentInstance.getGroupInstances() != null) {
3136             status = addGroupInstancesToComponentInstance(containerComponent, componentInstance, componentInstance.getGroupInstances());
3137             if (status != StorageOperationStatus.OK && status != StorageOperationStatus.NOT_FOUND) {
3138                 CommonUtility
3139                     .addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to add group instances for container {}. error {] ", componentInstanceId,
3140                         status);
3141                 return Either.right(status);
3142             }
3143         }
3144         return Either.left(status);
3145     }
3146
3147     public StorageOperationStatus addGroupInstancesToComponentInstance(Component containerComponent, ComponentInstance componentInstance,
3148                                                                        List<GroupDefinition> groups,
3149                                                                        Map<String, List<ArtifactDefinition>> groupInstancesArtifacts) {
3150         return nodeTemplateOperation.addGroupInstancesToComponentInstance(containerComponent, componentInstance, groups, groupInstancesArtifacts);
3151     }
3152
3153     public Either<List<GroupDefinition>, StorageOperationStatus> updateGroupsOnComponent(Component component,
3154                                                                                          List<GroupDataDefinition> updatedGroups) {
3155         return groupsOperation.updateGroups(component, updatedGroups, PromoteVersionEnum.MINOR);
3156     }
3157
3158     public Either<List<GroupInstance>, StorageOperationStatus> updateGroupInstancesOnComponent(Component component, String instanceId,
3159                                                                                                List<GroupInstance> updatedGroupInstances) {
3160         return groupsOperation.updateGroupInstances(component, instanceId, updatedGroupInstances);
3161     }
3162
3163     public StorageOperationStatus addGroupInstancesToComponentInstance(Component containerComponent, ComponentInstance componentInstance,
3164                                                                        List<GroupInstance> groupInstances) {
3165         return nodeTemplateOperation.addGroupInstancesToComponentInstance(containerComponent, componentInstance, groupInstances);
3166     }
3167
3168     public StorageOperationStatus addDeploymentArtifactsToComponentInstance(Component containerComponent, ComponentInstance componentInstance,
3169                                                                             Map<String, ArtifactDefinition> deploymentArtifacts) {
3170         return nodeTemplateOperation.addDeploymentArtifactsToComponentInstance(containerComponent, componentInstance, deploymentArtifacts);
3171     }
3172
3173     public StorageOperationStatus updateComponentInstanceProperty(Component containerComponent, String componentInstanceId,
3174                                                                   ComponentInstanceProperty property) {
3175         return nodeTemplateOperation.updateComponentInstanceProperty(containerComponent, componentInstanceId, property);
3176     }
3177
3178     public StorageOperationStatus updateComponentInstanceProperties(Component containerComponent, String componentInstanceId,
3179                                                                     List<ComponentInstanceProperty> properties) {
3180         return nodeTemplateOperation.updateComponentInstanceProperties(containerComponent, componentInstanceId, properties);
3181     }
3182
3183     public StorageOperationStatus updateComponentInstanceAttributes(final Component containerComponent, final String componentInstanceId,
3184                                                                     final List<ComponentInstanceAttribute> attributes) {
3185         return nodeTemplateOperation.updateComponentInstanceAttributes(containerComponent, componentInstanceId, attributes);
3186     }
3187
3188     public StorageOperationStatus addComponentInstanceProperty(Component containerComponent, String componentInstanceId,
3189                                                                ComponentInstanceProperty property) {
3190         return nodeTemplateOperation.addComponentInstanceProperty(containerComponent, componentInstanceId, property);
3191     }
3192
3193     public StorageOperationStatus updateComponentInstanceAttribute(final Component containerComponent, final String componentInstanceId,
3194                                                                    final ComponentInstanceAttribute attribute) {
3195         return nodeTemplateOperation.updateComponentInstanceAttribute(containerComponent, componentInstanceId, attribute);
3196     }
3197
3198     public StorageOperationStatus addComponentInstanceAttribute(Component containerComponent, String componentInstanceId,
3199                                                                 ComponentInstanceAttribute attribute) {
3200         return nodeTemplateOperation.addComponentInstanceAttribute(containerComponent, componentInstanceId, attribute);
3201     }
3202
3203     public StorageOperationStatus updateComponentInstanceInput(Component containerComponent, String componentInstanceId,
3204                                                                ComponentInstanceInput property) {
3205         return nodeTemplateOperation.updateComponentInstanceInput(containerComponent, componentInstanceId, property);
3206     }
3207
3208     public StorageOperationStatus updateComponentInstanceOutput(Component containerComponent, String componentInstanceId,
3209                                                                 ComponentInstanceOutput property) {
3210         return nodeTemplateOperation.updateComponentInstanceOutput(containerComponent, componentInstanceId, property);
3211     }
3212
3213     public StorageOperationStatus updateComponentInstanceInputs(Component containerComponent, String componentInstanceId,
3214                                                                 List<ComponentInstanceInput> instanceInputs) {
3215         return nodeTemplateOperation.updateComponentInstanceInputs(containerComponent, componentInstanceId, instanceInputs);
3216     }
3217
3218     public StorageOperationStatus updateComponentInstanceOutputs(Component containerComponent, String componentInstanceId,
3219                                                                  List<ComponentInstanceOutput> instanceInputs) {
3220         return nodeTemplateOperation.updateComponentInstanceOutputs(containerComponent, componentInstanceId, instanceInputs);
3221     }
3222
3223     public StorageOperationStatus addComponentInstanceInput(Component containerComponent, String componentInstanceId,
3224                                                             ComponentInstanceInput property) {
3225         return nodeTemplateOperation.addComponentInstanceInput(containerComponent, componentInstanceId, property);
3226     }
3227
3228     public StorageOperationStatus addComponentInstanceOutput(Component containerComponent, String componentInstanceId,
3229                                                              ComponentInstanceOutput property) {
3230         return nodeTemplateOperation.addComponentInstanceOutput(containerComponent, componentInstanceId, property);
3231     }
3232
3233     public void setNodeTypeOperation(NodeTypeOperation nodeTypeOperation) {
3234         this.nodeTypeOperation = nodeTypeOperation;
3235     }
3236
3237     public void setTopologyTemplateOperation(TopologyTemplateOperation topologyTemplateOperation) {
3238         this.topologyTemplateOperation = topologyTemplateOperation;
3239     }
3240
3241     public StorageOperationStatus deleteComponentInstanceInputsFromTopologyTemplate(Component containerComponent,
3242                                                                                     List<InputDefinition> inputsToDelete) {
3243         return topologyTemplateOperation.deleteToscaDataElements(containerComponent.getUniqueId(), EdgeLabelEnum.INPUTS,
3244             inputsToDelete.stream().map(PropertyDataDefinition::getName).collect(Collectors.toList()));
3245     }
3246
3247     public StorageOperationStatus deleteComponentInstanceOutputsFromTopologyTemplate(final Component containerComponent,
3248                                                                                      final List<OutputDefinition> outputsToDelete) {
3249         return topologyTemplateOperation.deleteToscaDataElements(containerComponent.getUniqueId(), EdgeLabelEnum.OUTPUTS,
3250             outputsToDelete.stream().map(AttributeDataDefinition::getName).collect(Collectors.toList()));
3251     }
3252
3253     public StorageOperationStatus updateComponentInstanceCapabiltyProperty(Component containerComponent, String componentInstanceUniqueId,
3254                                                                            String capabilityPropertyKey, ComponentInstanceProperty property) {
3255         return nodeTemplateOperation
3256             .updateComponentInstanceCapabilityProperty(containerComponent, componentInstanceUniqueId, capabilityPropertyKey, property);
3257     }
3258
3259     public StorageOperationStatus updateComponentInstanceCapabilityProperties(Component containerComponent, String componentInstanceUniqueId) {
3260         return convertComponentInstanceProperties(containerComponent, componentInstanceUniqueId).map(instanceCapProps -> topologyTemplateOperation
3261                 .updateComponentInstanceCapabilityProperties(containerComponent, componentInstanceUniqueId, instanceCapProps))
3262             .orElse(StorageOperationStatus.NOT_FOUND);
3263     }
3264
3265     public StorageOperationStatus updateComponentInstanceRequirement(String containerComponentId, String componentInstanceUniqueId,
3266                                                                      RequirementDataDefinition requirementDataDefinition) {
3267         return nodeTemplateOperation.updateComponentInstanceRequirement(containerComponentId, componentInstanceUniqueId, requirementDataDefinition);
3268     }
3269
3270     public CapabilityDataDefinition updateComponentInstanceCapability(final String containerComponentId, final String componentInstanceUniqueId,
3271                                                                       final CapabilityDataDefinition capabilityDataDefinition) {
3272
3273         return nodeTemplateOperation.updateComponentInstanceCapabilities(containerComponentId, componentInstanceUniqueId, capabilityDataDefinition);
3274     }
3275
3276     public StorageOperationStatus updateComponentInstanceInterfaces(Component containerComponent, String componentInstanceUniqueId) {
3277         MapInterfaceDataDefinition mapInterfaceDataDefinition = convertComponentInstanceInterfaces(containerComponent, componentInstanceUniqueId);
3278         return topologyTemplateOperation.updateComponentInstanceInterfaces(containerComponent, componentInstanceUniqueId, mapInterfaceDataDefinition);
3279     }
3280
3281     public StorageOperationStatus updateComponentInterfaces(final Component component, final String componentInterfaceUpdatedKey) {
3282         MapInterfaceDataDefinition mapInterfaceDataDefinition = convertComponentInterfaces(component.getInterfaces());
3283         return topologyTemplateOperation.updateComponentInterfaces(component.getUniqueId(), mapInterfaceDataDefinition, componentInterfaceUpdatedKey);
3284     }
3285
3286     public Either<InterfaceDefinition, StorageOperationStatus> addInterfaceToComponent(final String interfaceName,
3287                                                                                        final InterfaceDefinition interfaceDefinition,
3288                                                                                        final Component component) {
3289
3290         final boolean match = component.getInterfaces().keySet().stream().anyMatch(s -> s.equals(interfaceName));
3291         StorageOperationStatus status = StorageOperationStatus.OK;
3292         final ToscaElementOperation toscaElementOperation = getToscaElementOperation(component);
3293         if (match) {
3294             status = toscaElementOperation.updateToscaDataOfToscaElement(component.getUniqueId(), EdgeLabelEnum.INTERFACE_ARTIFACTS,
3295                 VertexTypeEnum.INTERFACE_ARTIFACTS, interfaceDefinition, JsonPresentationFields.TYPE);
3296         } else {
3297             status = toscaElementOperation.addToscaDataToToscaElement(component.getUniqueId(), EdgeLabelEnum.INTERFACE_ARTIFACTS,
3298                 VertexTypeEnum.INTERFACE_ARTIFACTS, interfaceDefinition, JsonPresentationFields.TYPE);
3299         }
3300
3301         if (status != StorageOperationStatus.OK) {
3302             CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to add the interface {} to the component {}. Status is {}. ",
3303                 interfaceName, component.getName(), status);
3304             return Either.right(status);
3305         }
3306         final ComponentParametersView filter = new ComponentParametersView(true);
3307         filter.setIgnoreInterfaces(false);
3308         filter.setIgnoreInterfaceInstances(false);
3309         final Either<Component, StorageOperationStatus> getUpdatedComponentRes = getToscaElement(component.getUniqueId(), filter);
3310         if (getUpdatedComponentRes.isRight()) {
3311             CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to get updated component {}. Status is {}. ",
3312                 component.getUniqueId(), getUpdatedComponentRes.right().value());
3313             return Either.right(getUpdatedComponentRes.right().value());
3314         }
3315         InterfaceDefinition newInterfaceDefinition = null;
3316         final Map<String, InterfaceDefinition> interfaces = (getUpdatedComponentRes.left().value()).getInterfaces();
3317         if (MapUtils.isNotEmpty(interfaces)) {
3318             final Optional<String> interfaceNameOptional = interfaces.keySet().stream().filter(key -> key.equals(interfaceName)).findAny();
3319             if (interfaceNameOptional.isPresent()) {
3320                 newInterfaceDefinition = interfaces.get(interfaceNameOptional.get());
3321             }
3322         }
3323         if (newInterfaceDefinition == null) {
3324             CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to find recently added interface {} on the component {}. Status is {}. ",
3325                 interfaceName, component.getUniqueId(), StorageOperationStatus.NOT_FOUND);
3326             return Either.right(StorageOperationStatus.NOT_FOUND);
3327         }
3328         return Either.left(newInterfaceDefinition);
3329     }
3330
3331     public StorageOperationStatus updateComponentCalculatedCapabilitiesProperties(Component containerComponent) {
3332         Map<String, MapCapabilityProperty> mapCapabiltyPropertyMap = convertComponentCapabilitiesProperties(containerComponent);
3333         return nodeTemplateOperation.overrideComponentCapabilitiesProperties(containerComponent, mapCapabiltyPropertyMap);
3334     }
3335
3336     public StorageOperationStatus deleteAllCalculatedCapabilitiesRequirements(String topologyTemplateId) {
3337         StorageOperationStatus status = topologyTemplateOperation
3338             .removeToscaData(topologyTemplateId, EdgeLabelEnum.CALCULATED_CAPABILITIES, VertexTypeEnum.CALCULATED_CAPABILITIES);
3339         if (status == StorageOperationStatus.OK) {
3340             status = topologyTemplateOperation
3341                 .removeToscaData(topologyTemplateId, EdgeLabelEnum.CALCULATED_REQUIREMENTS, VertexTypeEnum.CALCULATED_REQUIREMENTS);
3342         }
3343         if (status == StorageOperationStatus.OK) {
3344             status = topologyTemplateOperation
3345                 .removeToscaData(topologyTemplateId, EdgeLabelEnum.CALCULATED_CAP_PROPERTIES, VertexTypeEnum.CALCULATED_CAP_PROPERTIES);
3346         }
3347         return status;
3348     }
3349
3350     public Either<Component, StorageOperationStatus> shouldUpgradeToLatestDerived(Resource clonedResource) {
3351         String componentId = clonedResource.getUniqueId();
3352         Either<GraphVertex, JanusGraphOperationStatus> getVertexEither = janusGraphDao.getVertexById(componentId, JsonParseFlagEnum.NoParse);
3353         if (getVertexEither.isRight()) {
3354             log.debug(COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, componentId, getVertexEither.right().value());
3355             return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVertexEither.right().value()));
3356         }
3357         GraphVertex nodeTypeV = getVertexEither.left().value();
3358         ToscaElement toscaElementToUpdate = ModelConverter.convertToToscaElement(clonedResource);
3359         Either<ToscaElement, StorageOperationStatus> shouldUpdateDerivedVersion = nodeTypeOperation
3360             .shouldUpdateDerivedVersion(toscaElementToUpdate, nodeTypeV);
3361         if (shouldUpdateDerivedVersion.isRight() && StorageOperationStatus.OK != shouldUpdateDerivedVersion.right().value()) {
3362             log.debug("Failed to update derived version for node type {} derived {}, error: {}", componentId, clonedResource.getDerivedFrom().get(0),
3363                 shouldUpdateDerivedVersion.right().value());
3364             return Either.right(shouldUpdateDerivedVersion.right().value());
3365         }
3366         if (shouldUpdateDerivedVersion.isLeft()) {
3367             return Either.left(ModelConverter.convertFromToscaElement(shouldUpdateDerivedVersion.left().value()));
3368         }
3369         return Either.left(clonedResource);
3370     }
3371
3372     /**
3373      * Returns list of ComponentInstanceProperty belonging to component instance capability specified by name, type and ownerId
3374      */
3375     public Either<List<ComponentInstanceProperty>, StorageOperationStatus> getComponentInstanceCapabilityProperties(String componentId,
3376                                                                                                                     String instanceId,
3377                                                                                                                     String capabilityName,
3378                                                                                                                     String capabilityType,
3379                                                                                                                     String ownerId) {
3380         return topologyTemplateOperation.getComponentInstanceCapabilityProperties(componentId, instanceId, capabilityName, capabilityType, ownerId);
3381     }
3382
3383     private MapInterfaceDataDefinition convertComponentInstanceInterfaces(Component currComponent, String componentInstanceId) {
3384         MapInterfaceDataDefinition mapInterfaceDataDefinition = new MapInterfaceDataDefinition();
3385         List<ComponentInstanceInterface> componentInterface = currComponent.getComponentInstancesInterfaces().get(componentInstanceId);
3386         if (CollectionUtils.isNotEmpty(componentInterface)) {
3387             componentInterface.stream().forEach(interfaceDef -> mapInterfaceDataDefinition.put(interfaceDef.getUniqueId(), interfaceDef));
3388         }
3389         return mapInterfaceDataDefinition;
3390     }
3391
3392     private MapInterfaceDataDefinition convertComponentInterfaces(final Map<String, InterfaceDefinition> interfaces) {
3393         final MapInterfaceDataDefinition mapInterfaceDataDefinition = new MapInterfaceDataDefinition();
3394         if (MapUtils.isNotEmpty(interfaces)) {
3395             interfaces.values().stream().forEach(interfaceDef -> mapInterfaceDataDefinition.put(interfaceDef.getType(), interfaceDef));
3396         }
3397         return mapInterfaceDataDefinition;
3398     }
3399
3400     private Map<String, MapCapabilityProperty> convertComponentCapabilitiesProperties(Component currComponent) {
3401         Map<String, MapCapabilityProperty> map = ModelConverter.extractCapabilityPropertiesFromGroups(currComponent.getGroups(), true);
3402         map.putAll(ModelConverter.extractCapabilityProperteisFromInstances(currComponent.getComponentInstances(), true));
3403         return map;
3404     }
3405
3406     private Optional<MapCapabilityProperty> convertComponentInstanceProperties(Component component, String instanceId) {
3407         return component.fetchInstanceById(instanceId)
3408             .map(ci -> ModelConverter.convertToMapOfMapCapabilityProperties(ci.getCapabilities(), instanceId, ci.getOriginType().isAtomicType()));
3409     }
3410
3411     public Either<PolicyDefinition, StorageOperationStatus> associatePolicyToComponent(String componentId, PolicyDefinition policyDefinition,
3412                                                                                        int counter) {
3413         Either<PolicyDefinition, StorageOperationStatus> result = null;
3414         Either<GraphVertex, JanusGraphOperationStatus> getVertexEither;
3415         getVertexEither = janusGraphDao.getVertexById(componentId, JsonParseFlagEnum.ParseMetadata);
3416         if (getVertexEither.isRight()) {
3417             log.error(COULDNT_FETCH_A_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, componentId, getVertexEither.right().value());
3418             result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVertexEither.right().value()));
3419         } else {
3420             if (getVertexEither.left().value().getLabel() != VertexTypeEnum.TOPOLOGY_TEMPLATE) {
3421                 log.error("Policy association to component of Tosca type {} is not allowed. ", getVertexEither.left().value().getLabel());
3422                 result = Either.right(StorageOperationStatus.BAD_REQUEST);
3423             }
3424         }
3425         if (result == null) {
3426             StorageOperationStatus status = topologyTemplateOperation
3427                 .addPolicyToToscaElement(getVertexEither.left().value(), policyDefinition, counter);
3428             if (status != StorageOperationStatus.OK) {
3429                 return Either.right(status);
3430             }
3431         }
3432         if (result == null) {
3433             result = Either.left(policyDefinition);
3434         }
3435         return result;
3436     }
3437
3438     public StorageOperationStatus associatePoliciesToComponent(String componentId, List<PolicyDefinition> policies) {
3439         log.debug("#associatePoliciesToComponent - associating policies for component {}.", componentId);
3440         return janusGraphDao.getVertexById(componentId, JsonParseFlagEnum.ParseMetadata)
3441             .either(containerVertex -> topologyTemplateOperation.addPoliciesToToscaElement(containerVertex, policies),
3442                 DaoStatusConverter::convertJanusGraphStatusToStorageStatus);
3443     }
3444
3445     public Either<PolicyDefinition, StorageOperationStatus> updatePolicyOfComponent(String componentId, PolicyDefinition policyDefinition,
3446                                                                                     PromoteVersionEnum promoteVersionEnum) {
3447         Either<PolicyDefinition, StorageOperationStatus> result = null;
3448         Either<GraphVertex, JanusGraphOperationStatus> getVertexEither;
3449         getVertexEither = janusGraphDao.getVertexById(componentId, JsonParseFlagEnum.NoParse);
3450         if (getVertexEither.isRight()) {
3451             log.error(COULDNT_FETCH_A_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, componentId, getVertexEither.right().value());
3452             result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVertexEither.right().value()));
3453         }
3454         if (result == null) {
3455             policyDefinition.setVersion(GroupUtils.updateVersion(promoteVersionEnum, policyDefinition.getVersion()));
3456             StorageOperationStatus status = topologyTemplateOperation.updatePolicyOfToscaElement(getVertexEither.left().value(), policyDefinition);
3457             if (status != StorageOperationStatus.OK) {
3458                 return Either.right(status);
3459             }
3460         }
3461         if (result == null) {
3462             result = Either.left(policyDefinition);
3463         }
3464         return result;
3465     }
3466
3467     public StorageOperationStatus updatePoliciesOfComponent(String componentId, List<PolicyDefinition> policyDefinition) {
3468         log.debug("#updatePoliciesOfComponent - updating policies for component {}", componentId);
3469         return janusGraphDao.getVertexById(componentId, JsonParseFlagEnum.NoParse).right()
3470             .map(DaoStatusConverter::convertJanusGraphStatusToStorageStatus)
3471             .either(containerVertex -> topologyTemplateOperation.updatePoliciesOfToscaElement(containerVertex, policyDefinition), err -> err);
3472     }
3473
3474     public StorageOperationStatus removePolicyFromComponent(String componentId, String policyId) {
3475         StorageOperationStatus status = null;
3476         Either<GraphVertex, JanusGraphOperationStatus> getVertexEither = janusGraphDao.getVertexById(componentId, JsonParseFlagEnum.NoParse);
3477         if (getVertexEither.isRight()) {
3478             log.error(COULDNT_FETCH_A_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, componentId, getVertexEither.right().value());
3479             status = DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVertexEither.right().value());
3480         }
3481         if (status == null) {
3482             status = topologyTemplateOperation.removePolicyFromToscaElement(getVertexEither.left().value(), policyId);
3483         }
3484         return status;
3485     }
3486
3487     public boolean canAddGroups(String componentId) {
3488         GraphVertex vertex = janusGraphDao.getVertexById(componentId).left().on(this::onJanusGraphError);
3489         return topologyTemplateOperation.hasEdgeOfType(vertex, EdgeLabelEnum.GROUPS);
3490     }
3491
3492     GraphVertex onJanusGraphError(JanusGraphOperationStatus toe) {
3493         throw new StorageException(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(toe));
3494     }
3495
3496     public CatalogUpdateTimestamp updateCatalogTimes() {
3497         long now = System.currentTimeMillis();
3498         GraphVertex catalogRoot = janusGraphDao.getVertexByLabel(VertexTypeEnum.CATALOG_ROOT).left().on(this::onJanusGraphError);
3499         Long currentTime = (Long) catalogRoot.getMetadataProperty(GraphPropertyEnum.CURRENT_CATALOG_UPDATE_TIME);
3500         catalogRoot.addMetadataProperty(GraphPropertyEnum.PREV_CATALOG_UPDATE_TIME, currentTime);
3501         catalogRoot.addMetadataProperty(GraphPropertyEnum.CURRENT_CATALOG_UPDATE_TIME, now);
3502         janusGraphDao.updateVertex(catalogRoot).left().on(this::onJanusGraphError);
3503         return new CatalogUpdateTimestamp(currentTime, now);
3504     }
3505
3506     public CatalogUpdateTimestamp getCatalogTimes() {
3507         GraphVertex catalogRoot = janusGraphDao.getVertexByLabel(VertexTypeEnum.CATALOG_ROOT).left().on(this::onJanusGraphError);
3508         Long currentTime = (Long) catalogRoot.getMetadataProperty(GraphPropertyEnum.CURRENT_CATALOG_UPDATE_TIME);
3509         Long prevTime = (Long) catalogRoot.getMetadataProperty(GraphPropertyEnum.PREV_CATALOG_UPDATE_TIME);
3510         return new CatalogUpdateTimestamp(prevTime == null ? 0 : prevTime.longValue(), currentTime == null ? 0 : currentTime.longValue());
3511     }
3512
3513     public void updateNamesOfCalculatedCapabilitiesRequirements(String componentId) {
3514         topologyTemplateOperation.updateNamesOfCalculatedCapabilitiesRequirements(componentId, getTopologyTemplate(componentId));
3515     }
3516
3517     public void revertNamesOfCalculatedCapabilitiesRequirements(String componentId) {
3518         topologyTemplateOperation.revertNamesOfCalculatedCapabilitiesRequirements(componentId, getTopologyTemplate(componentId));
3519     }
3520
3521     private TopologyTemplate getTopologyTemplate(String componentId) {
3522         return (TopologyTemplate) topologyTemplateOperation.getToscaElement(componentId, getFilterComponentWithCapProperties()).left()
3523             .on(this::throwStorageException);
3524     }
3525
3526     private ComponentParametersView getFilterComponentWithCapProperties() {
3527         ComponentParametersView filter = new ComponentParametersView();
3528         filter.setIgnoreCapabiltyProperties(false);
3529         return filter;
3530     }
3531
3532     private ToscaElement throwStorageException(StorageOperationStatus status) {
3533         throw new StorageException(status);
3534     }
3535
3536     public Either<Boolean, StorageOperationStatus> isComponentInUse(String componentId) {
3537         final List<EdgeLabelEnum> forbiddenEdgeLabelEnums = Arrays
3538             .asList(EdgeLabelEnum.INSTANCE_OF, EdgeLabelEnum.PROXY_OF, EdgeLabelEnum.ALLOTTED_OF);
3539         Either<GraphVertex, JanusGraphOperationStatus> vertexById = janusGraphDao.getVertexById(componentId);
3540         if (vertexById.isLeft()) {
3541             for (EdgeLabelEnum edgeLabelEnum : forbiddenEdgeLabelEnums) {
3542                 Iterator<Edge> edgeItr = vertexById.left().value().getVertex().edges(Direction.IN, edgeLabelEnum.name());
3543                 if (edgeItr != null && edgeItr.hasNext()) {
3544                     return Either.left(true);
3545                 }
3546             }
3547         }
3548         return Either.left(false);
3549     }
3550
3551     public Either<List<Component>, StorageOperationStatus> getComponentListByInvariantUuid(String componentInvariantUuid,
3552                                                                                            Map<GraphPropertyEnum, Object> additionalPropertiesToMatch) {
3553         Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
3554         if (MapUtils.isNotEmpty(additionalPropertiesToMatch)) {
3555             propertiesToMatch.putAll(additionalPropertiesToMatch);
3556         }
3557         propertiesToMatch.put(GraphPropertyEnum.INVARIANT_UUID, componentInvariantUuid);
3558         Either<List<GraphVertex>, JanusGraphOperationStatus> vertexEither = janusGraphDao
3559             .getByCriteria(null, propertiesToMatch, JsonParseFlagEnum.ParseMetadata);
3560         if (vertexEither.isRight()) {
3561             log.debug("Couldn't fetch metadata for component with type {} and invariantUUId {}, error: {}", componentInvariantUuid,
3562                 vertexEither.right().value());
3563             return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(vertexEither.right().value()));
3564         }
3565         List<GraphVertex> vertexList = vertexEither.isLeft() ? vertexEither.left().value() : null;
3566         if (vertexList == null || vertexList.isEmpty()) {
3567             log.debug("Component with invariantUUId {} was not found", componentInvariantUuid);
3568             return Either.right(StorageOperationStatus.NOT_FOUND);
3569         }
3570         ArrayList<Component> components = new ArrayList<>();
3571         for (GraphVertex vertex : vertexList) {
3572             Either<Component, StorageOperationStatus> toscaElementByOperation = getToscaElementByOperation(vertex);
3573             if (toscaElementByOperation.isRight()) {
3574                 log.debug("Could not fetch the following Component by Invariant UUID {}", vertex.getUniqueId());
3575                 return Either.right(toscaElementByOperation.right().value());
3576             }
3577             components.add(toscaElementByOperation.left().value());
3578         }
3579         return Either.left(components);
3580     }
3581
3582     public Either<List<Component>, StorageOperationStatus> getParentComponents(String componentId) {
3583         List<Component> parentComponents = new ArrayList<>();
3584         final List<EdgeLabelEnum> relationEdgeLabelEnums = Arrays.asList(EdgeLabelEnum.INSTANCE_OF, EdgeLabelEnum.PROXY_OF);
3585         Either<GraphVertex, JanusGraphOperationStatus> vertexById = janusGraphDao.getVertexById(componentId);
3586         if (vertexById.isLeft()) {
3587             for (EdgeLabelEnum edgeLabelEnum : relationEdgeLabelEnums) {
3588                 Either<GraphVertex, JanusGraphOperationStatus> parentVertexEither = janusGraphDao
3589                     .getParentVertex(vertexById.left().value(), edgeLabelEnum, JsonParseFlagEnum.ParseJson);
3590                 if (parentVertexEither.isLeft()) {
3591                     Either<Component, StorageOperationStatus> componentEither = getToscaElement(parentVertexEither.left().value().getUniqueId());
3592                     if (componentEither.isLeft()) {
3593                         parentComponents.add(componentEither.left().value());
3594                     }
3595                 }
3596             }
3597         }
3598         return Either.left(parentComponents);
3599     }
3600
3601     public void updateCapReqPropertiesOwnerId(String componentId) {
3602         topologyTemplateOperation.updateCapReqPropertiesOwnerId(componentId, getTopologyTemplate(componentId));
3603     }
3604
3605     public <T extends Component> Either<T, StorageOperationStatus> getLatestByServiceName(String serviceName) {
3606         return getLatestByName(GraphPropertyEnum.NAME, serviceName, null);
3607     }
3608 }