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