Change to enable SDC list type input
[sdc.git] / catalog-model / src / main / java / org / openecomp / sdc / be / model / jsontitan / 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
21 package org.openecomp.sdc.be.model.jsontitan.operations;
22
23 import com.datastax.driver.core.DataType;
24 import fj.data.Either;
25 import org.apache.commons.collections.CollectionUtils;
26 import org.apache.commons.collections.MapUtils;
27 import org.apache.commons.lang3.StringUtils;
28 import org.apache.commons.lang3.tuple.ImmutablePair;
29 import org.apache.tinkerpop.gremlin.structure.Direction;
30 import org.apache.tinkerpop.gremlin.structure.Edge;
31 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
32 import org.openecomp.sdc.be.dao.jsongraph.HealingTitanDao;
33 import org.openecomp.sdc.be.dao.jsongraph.types.EdgeLabelEnum;
34 import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum;
35 import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum;
36 import org.openecomp.sdc.be.dao.titan.TitanOperationStatus;
37 import org.openecomp.sdc.be.datatypes.elements.*;
38 import org.openecomp.sdc.be.datatypes.elements.MapInterfaceDataDefinition;
39 import org.openecomp.sdc.be.datatypes.enums.*;
40 import org.openecomp.sdc.be.model.*;
41 import org.openecomp.sdc.be.model.catalog.CatalogComponent;
42 import org.openecomp.sdc.be.model.jsontitan.datamodel.TopologyTemplate;
43 import org.openecomp.sdc.be.model.jsontitan.datamodel.ToscaElement;
44 import org.openecomp.sdc.be.model.jsontitan.utils.ModelConverter;
45 import org.openecomp.sdc.be.model.operations.StorageException;
46 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
47 import org.openecomp.sdc.be.model.operations.impl.DaoStatusConverter;
48 import org.openecomp.sdc.be.model.operations.impl.UniqueIdBuilder;
49 import org.openecomp.sdc.be.resources.data.ComponentMetadataData;
50 import org.openecomp.sdc.be.resources.data.DataTypeData;
51 import org.openecomp.sdc.common.jsongraph.util.CommonUtility;
52 import org.openecomp.sdc.common.jsongraph.util.CommonUtility.LogLevelEnum;
53 import org.openecomp.sdc.common.log.wrappers.Logger;
54 import org.openecomp.sdc.common.util.ValidationUtils;
55 import org.springframework.beans.factory.annotation.Autowired;
56
57 import java.util.*;
58 import java.util.Map.Entry;
59 import java.util.function.BiPredicate;
60 import java.util.stream.Collectors;
61
62 import static java.util.Objects.requireNonNull;
63 import static org.apache.commons.collections.CollectionUtils.isEmpty;
64 import static org.apache.commons.collections.CollectionUtils.isNotEmpty;
65
66
67 @org.springframework.stereotype.Component("tosca-operation-facade")
68 public class ToscaOperationFacade {
69
70     // region - Fields
71
72     private static final String COULDNT_FETCH_A_COMPONENT_WITH_AND_UNIQUE_ID_ERROR = "Couldn't fetch a component with and UniqueId {}, error: {}";
73     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 {}. ";
74     private static final String FAILED_TO_GET_UPDATED_RESOURCE_STATUS_IS = "Failed to get updated resource {}. Status is {}. ";
75     private static final String FAILED_TO_ADD_THE_PROPERTY_TO_THE_RESOURCE_STATUS_IS = "Failed to add the property {} to the resource {}. Status is {}. ";
76     private static final String SERVICE = "service";
77     private static final String NOT_SUPPORTED_COMPONENT_TYPE = "Not supported component type {}";
78     private static final String COMPONENT_CREATED_SUCCESSFULLY = "Component created successfully!!!";
79     private static final String COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR = "Couldn't fetch component with and unique id {}, error: {}";
80     @Autowired
81     private NodeTypeOperation nodeTypeOperation;
82     @Autowired
83     private TopologyTemplateOperation topologyTemplateOperation;
84     @Autowired
85     private NodeTemplateOperation nodeTemplateOperation;
86     @Autowired
87     private GroupsOperation groupsOperation;
88     @Autowired
89     private HealingTitanDao titanDao;
90
91     private static final Logger log = Logger.getLogger(ToscaOperationFacade.class.getName());
92     // endregion
93
94     // region - ToscaElement - GetById
95     public static final String PROXY_SUFFIX = "_proxy";
96
97     public <T extends Component> Either<T, StorageOperationStatus> getToscaFullElement(String componentId) {
98         ComponentParametersView filters = new ComponentParametersView();
99         filters.setIgnoreCapabiltyProperties(false);
100         filters.setIgnoreForwardingPath(false);
101         return getToscaElement(componentId, filters);
102     }
103
104     public <T extends Component> Either<T, StorageOperationStatus> getToscaElement(String componentId) {
105
106         return getToscaElement(componentId, JsonParseFlagEnum.ParseAll);
107
108     }
109
110     public <T extends Component> Either<T, StorageOperationStatus> getToscaElement(String componentId, ComponentParametersView filters) {
111
112         Either<GraphVertex, TitanOperationStatus> getVertexEither = titanDao.getVertexById(componentId, filters.detectParseFlag());
113         if (getVertexEither.isRight()) {
114             log.debug(COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, componentId, getVertexEither.right().value());
115             return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(getVertexEither.right().value()));
116
117         }
118         return getToscaElementByOperation(getVertexEither.left().value(), filters);
119     }
120
121     public <T extends Component> Either<T, StorageOperationStatus> getToscaElement(String componentId, JsonParseFlagEnum parseFlag) {
122
123         Either<GraphVertex, TitanOperationStatus> getVertexEither = titanDao.getVertexById(componentId, parseFlag);
124         if (getVertexEither.isRight()) {
125             log.debug(COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, componentId, getVertexEither.right().value());
126             return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(getVertexEither.right().value()));
127
128         }
129         return getToscaElementByOperation(getVertexEither.left().value());
130     }
131
132     public <T extends Component> Either<T, StorageOperationStatus> getToscaElement(GraphVertex componentVertex) {
133         return getToscaElementByOperation(componentVertex);
134     }
135
136     public Either<Boolean, StorageOperationStatus> validateComponentExists(String componentId) {
137
138         Either<GraphVertex, TitanOperationStatus> getVertexEither = titanDao.getVertexById(componentId, JsonParseFlagEnum.NoParse);
139         if (getVertexEither.isRight()) {
140             TitanOperationStatus status = getVertexEither.right().value();
141             if (status == TitanOperationStatus.NOT_FOUND) {
142                 return Either.left(false);
143             } else {
144                 log.debug(COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, componentId, getVertexEither.right().value());
145                 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(getVertexEither.right().value()));
146             }
147         }
148         return Either.left(true);
149     }
150
151     public <T extends Component> Either<T, StorageOperationStatus> findLastCertifiedToscaElementByUUID(T component) {
152         Map<GraphPropertyEnum, Object> props = new EnumMap<>(GraphPropertyEnum.class);
153         props.put(GraphPropertyEnum.UUID, component.getUUID());
154         props.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
155         props.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
156
157         Either<List<GraphVertex>, TitanOperationStatus> getVertexEither = titanDao.getByCriteria(ModelConverter.getVertexType(component), props);
158         if (getVertexEither.isRight()) {
159             log.debug(COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, component.getUniqueId(), getVertexEither.right().value());
160             return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(getVertexEither.right().value()));
161
162         }
163         return getToscaElementByOperation(getVertexEither.left().value().get(0));
164     }
165
166     // endregion
167     // region - ToscaElement - GetByOperation
168     private <T extends Component> Either<T, StorageOperationStatus> getToscaElementByOperation(GraphVertex componentV) {
169         return getToscaElementByOperation(componentV, new ComponentParametersView());
170     }
171
172     private <T extends Component> Either<T, StorageOperationStatus> getToscaElementByOperation(GraphVertex componentV, ComponentParametersView filters) {
173         VertexTypeEnum label = componentV.getLabel();
174
175         ToscaElementOperation toscaOperation = getToscaElementOperation(componentV);
176         log.debug("getToscaElementByOperation: toscaOperation={}", toscaOperation.getClass());
177         Either<ToscaElement, StorageOperationStatus> toscaElement;
178         String componentId = componentV.getUniqueId();
179         if (toscaOperation != null) {
180             log.debug("Need to fetch tosca element for id {}", componentId);
181             toscaElement = toscaOperation.getToscaElement(componentV, filters);
182         } else {
183             log.debug("not supported tosca type {} for id {}", label, componentId);
184             toscaElement = Either.right(StorageOperationStatus.BAD_REQUEST);
185         }
186         if (toscaElement.isRight()) {
187             return Either.right(toscaElement.right().value());
188         }
189         return Either.left(ModelConverter.convertFromToscaElement(toscaElement.left().value()));
190     }
191
192     // endregion
193     private ToscaElementOperation getToscaElementOperation(GraphVertex componentV) {
194         VertexTypeEnum label = componentV.getLabel();
195         switch (label) {
196             case NODE_TYPE:
197                 return nodeTypeOperation;
198             case TOPOLOGY_TEMPLATE:
199                 return topologyTemplateOperation;
200             default:
201                 return null;
202         }
203     }
204
205     public <T extends Component> Either<T, StorageOperationStatus> createToscaComponent(T resource) {
206         ToscaElement toscaElement = ModelConverter.convertToToscaElement(resource);
207
208         ToscaElementOperation toscaElementOperation = getToscaElementOperation(resource);
209         Either<ToscaElement, StorageOperationStatus> createToscaElement = toscaElementOperation.createToscaElement(toscaElement);
210         if (createToscaElement.isLeft()) {
211             log.debug(COMPONENT_CREATED_SUCCESSFULLY);
212             T dataModel = ModelConverter.convertFromToscaElement(createToscaElement.left().value());
213             return Either.left(dataModel);
214         }
215         return Either.right(createToscaElement.right().value());
216     }
217
218     // region - ToscaElement Delete
219     public StorageOperationStatus markComponentToDelete(Component componentToDelete) {
220
221         if ((componentToDelete.getIsDeleted() != null) && componentToDelete.getIsDeleted() && !componentToDelete.isHighestVersion()) {
222             // component already marked for delete
223             return StorageOperationStatus.OK;
224         } else {
225
226             Either<GraphVertex, TitanOperationStatus> getResponse = titanDao.getVertexById(componentToDelete.getUniqueId(), JsonParseFlagEnum.ParseAll);
227             if (getResponse.isRight()) {
228                 log.debug(COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, componentToDelete.getUniqueId(), getResponse.right().value());
229                 return DaoStatusConverter.convertTitanStatusToStorageStatus(getResponse.right().value());
230
231             }
232             GraphVertex componentV = getResponse.left().value();
233
234             // same operation for node type and topology template operations
235             Either<GraphVertex, StorageOperationStatus> result = nodeTypeOperation.markComponentToDelete(componentV);
236             if (result.isRight()) {
237                 return result.right().value();
238             }
239             return StorageOperationStatus.OK;
240         }
241     }
242
243     public <T extends Component> Either<T, StorageOperationStatus> deleteToscaComponent(String componentId) {
244
245         Either<GraphVertex, TitanOperationStatus> getVertexEither = titanDao.getVertexById(componentId, JsonParseFlagEnum.ParseAll);
246         if (getVertexEither.isRight()) {
247             log.debug("Couldn't fetch component vertex with and unique id {}, error: {}", componentId, getVertexEither.right().value());
248             return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(getVertexEither.right().value()));
249
250         }
251         Either<ToscaElement, StorageOperationStatus> deleteElement = deleteToscaElement(getVertexEither.left().value());
252         if (deleteElement.isRight()) {
253             log.debug("Failed to delete component with and unique id {}, error: {}", componentId, deleteElement.right().value());
254             return Either.right(deleteElement.right().value());
255         }
256         T dataModel = ModelConverter.convertFromToscaElement(deleteElement.left().value());
257
258         return Either.left(dataModel);
259     }
260
261     private Either<ToscaElement, StorageOperationStatus> deleteToscaElement(GraphVertex componentV) {
262         VertexTypeEnum label = componentV.getLabel();
263         Either<ToscaElement, StorageOperationStatus> toscaElement;
264         Object componentId = componentV.getUniqueId();
265         switch (label) {
266             case NODE_TYPE:
267                 log.debug("Need to fetch node type for id {}", componentId);
268                 toscaElement = nodeTypeOperation.deleteToscaElement(componentV);
269                 break;
270             case TOPOLOGY_TEMPLATE:
271                 log.debug("Need to fetch topology template for id {}", componentId);
272                 toscaElement = topologyTemplateOperation.deleteToscaElement(componentV);
273                 break;
274             default:
275                 log.debug("not supported tosca type {} for id {}", label, componentId);
276                 toscaElement = Either.right(StorageOperationStatus.BAD_REQUEST);
277                 break;
278         }
279         return toscaElement;
280     }
281     // endregion
282
283     private ToscaElementOperation getToscaElementOperation(Component component) {
284         return ModelConverter.isAtomicComponent(component) ? nodeTypeOperation : topologyTemplateOperation;
285     }
286
287     public <T extends Component> Either<T, StorageOperationStatus> getLatestByToscaResourceName(String toscaResourceName) {
288         return getLatestByName(GraphPropertyEnum.TOSCA_RESOURCE_NAME, toscaResourceName);
289     }
290
291     public <T extends Component> Either<T, StorageOperationStatus> getFullLatestComponentByToscaResourceName(String toscaResourceName) {
292         ComponentParametersView fetchAllFilter = new ComponentParametersView();
293         fetchAllFilter.setIgnoreForwardingPath(true);
294         fetchAllFilter.setIgnoreCapabiltyProperties(false);
295         return getLatestByName(GraphPropertyEnum.TOSCA_RESOURCE_NAME, toscaResourceName, JsonParseFlagEnum.ParseAll, fetchAllFilter);
296     }
297
298     public <T extends Component> Either<T, StorageOperationStatus> getLatestByName(String resourceName) {
299         return getLatestByName(GraphPropertyEnum.NAME, resourceName);
300
301     }
302
303     public StorageOperationStatus validateCsarUuidUniqueness(String csarUUID) {
304
305         Map<GraphPropertyEnum, Object> properties = new EnumMap<>(GraphPropertyEnum.class);
306         properties.put(GraphPropertyEnum.CSAR_UUID, csarUUID);
307
308         Either<List<GraphVertex>, TitanOperationStatus> resources = titanDao.getByCriteria(null, properties, JsonParseFlagEnum.ParseMetadata);
309
310         if (resources.isRight()) {
311             if (resources.right().value() == TitanOperationStatus.NOT_FOUND) {
312                 return StorageOperationStatus.OK;
313             } else {
314                 log.debug("failed to get resources from graph with property name: {}", csarUUID);
315                 return DaoStatusConverter.convertTitanStatusToStorageStatus(resources.right().value());
316             }
317         }
318         return StorageOperationStatus.ENTITY_ALREADY_EXISTS;
319
320     }
321
322     public <T extends Component> Either<Set<T>, StorageOperationStatus> getFollowed(String userId, Set<LifecycleStateEnum> lifecycleStates, Set<LifecycleStateEnum> lastStateStates, ComponentTypeEnum componentType) {
323         Either<List<ToscaElement>, StorageOperationStatus> followedResources;
324         if (componentType == ComponentTypeEnum.RESOURCE) {
325             followedResources = nodeTypeOperation.getFollowedComponent(userId, lifecycleStates, lastStateStates, componentType);
326         } else {
327             followedResources = topologyTemplateOperation.getFollowedComponent(userId, lifecycleStates, lastStateStates, componentType);
328         }
329
330         Set<T> components = new HashSet<>();
331         if (followedResources.isRight() && followedResources.right().value() != StorageOperationStatus.NOT_FOUND) {
332             return Either.right(followedResources.right().value());
333         }
334         if (followedResources.isLeft()) {
335             List<ToscaElement> toscaElements = followedResources.left().value();
336             toscaElements.forEach(te -> {
337                 T component = ModelConverter.convertFromToscaElement(te);
338                 components.add(component);
339             });
340         }
341         return Either.left(components);
342     }
343
344     public Either<Resource, StorageOperationStatus> getLatestCertifiedNodeTypeByToscaResourceName(String toscaResourceName) {
345
346         return getLatestCertifiedByToscaResourceName(toscaResourceName, VertexTypeEnum.NODE_TYPE, JsonParseFlagEnum.ParseMetadata);
347     }
348
349     public Either<Resource, StorageOperationStatus> getLatestCertifiedByToscaResourceName(String toscaResourceName, VertexTypeEnum vertexType, JsonParseFlagEnum parseFlag) {
350
351         Either<Resource, StorageOperationStatus> result = null;
352         Map<GraphPropertyEnum, Object> props = new EnumMap<>(GraphPropertyEnum.class);
353         props.put(GraphPropertyEnum.TOSCA_RESOURCE_NAME, toscaResourceName);
354         props.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
355         props.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
356         Either<List<GraphVertex>, TitanOperationStatus> getLatestRes = titanDao.getByCriteria(vertexType, props, parseFlag);
357
358         if (getLatestRes.isRight()) {
359             TitanOperationStatus status = getLatestRes.right().value();
360             CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to fetch {} with name {}. status={} ", vertexType, toscaResourceName, status);
361             result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
362         }
363         if (result == null) {
364             List<GraphVertex> resources = getLatestRes.left().value();
365             double version = 0.0;
366             GraphVertex highestResource = null;
367             for (GraphVertex resource : resources) {
368                 double resourceVersion = Double.parseDouble((String) resource.getJsonMetadataField(JsonPresentationFields.VERSION));
369                 if (resourceVersion > version) {
370                     version = resourceVersion;
371                     highestResource = resource;
372                 }
373             }
374             result = getToscaFullElement(highestResource.getUniqueId());
375         }
376         return result;
377     }
378
379     public Either<Boolean, StorageOperationStatus> validateToscaResourceNameExists(String templateName) {
380         Either<Boolean, StorageOperationStatus> validateUniquenessRes = validateToscaResourceNameUniqueness(templateName);
381         if (validateUniquenessRes.isLeft()) {
382             return Either.left(!validateUniquenessRes.left().value());
383         }
384         return validateUniquenessRes;
385     }
386
387     public Either<RequirementCapabilityRelDef, StorageOperationStatus> dissociateResourceInstances(String componentId, RequirementCapabilityRelDef requirementDef) {
388         return nodeTemplateOperation.dissociateResourceInstances(componentId, requirementDef);
389     }
390
391     /**
392      * Allows to get fulfilled requirement by relation and received predicate
393      */
394     public Either<RequirementDataDefinition, StorageOperationStatus> getFulfilledRequirementByRelation(String componentId, String instanceId, RequirementCapabilityRelDef relation, BiPredicate<RelationshipInfo, RequirementDataDefinition> predicate) {
395         return nodeTemplateOperation.getFulfilledRequirementByRelation(componentId, instanceId, relation, predicate);
396     }
397
398     /**
399      * Allows to get fulfilled capability by relation and received predicate
400      */
401     public Either<CapabilityDataDefinition, StorageOperationStatus> getFulfilledCapabilityByRelation(String componentId, String instanceId, RequirementCapabilityRelDef relation, BiPredicate<RelationshipInfo, CapabilityDataDefinition> predicate) {
402         return nodeTemplateOperation.getFulfilledCapabilityByRelation(componentId, instanceId, relation, predicate);
403     }
404
405     public StorageOperationStatus associateResourceInstances(String componentId, List<RequirementCapabilityRelDef> relations) {
406         Either<List<RequirementCapabilityRelDef>, StorageOperationStatus> status = nodeTemplateOperation.associateResourceInstances(componentId, relations);
407         if (status.isRight()) {
408             return status.right().value();
409         }
410         return StorageOperationStatus.OK;
411     }
412
413     protected Either<Boolean, StorageOperationStatus> validateToscaResourceNameUniqueness(String name) {
414
415         Map<GraphPropertyEnum, Object> properties = new EnumMap<>(GraphPropertyEnum.class);
416         properties.put(GraphPropertyEnum.TOSCA_RESOURCE_NAME, name);
417
418         Either<List<GraphVertex>, TitanOperationStatus> resources = titanDao.getByCriteria(null, properties, JsonParseFlagEnum.ParseMetadata);
419
420         if (resources.isRight() && resources.right().value() != TitanOperationStatus.NOT_FOUND) {
421             log.debug("failed to get resources from graph with property name: {}", name);
422             return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(resources.right().value()));
423         }
424         List<GraphVertex> resourceList = (resources.isLeft() ? resources.left().value() : null);
425         if (isNotEmpty(resourceList)) {
426             if (log.isDebugEnabled()) {
427                 StringBuilder builder = new StringBuilder();
428                 for (GraphVertex resourceData : resourceList) {
429                     builder.append(resourceData.getUniqueId() + "|");
430                 }
431                 log.debug("resources  with property name:{} exists in graph. found {}", name, builder);
432             }
433             return Either.left(false);
434         } else {
435             log.debug("resources  with property name:{} does not exists in graph", name);
436             return Either.left(true);
437         }
438
439     }
440
441     // region - Component Update
442
443     public Either<Resource, StorageOperationStatus> overrideComponent(Resource newComponent, Resource oldComponent) {
444
445         copyArtifactsToNewComponent(newComponent, oldComponent);
446
447         Either<GraphVertex, TitanOperationStatus> componentVEither = titanDao.getVertexById(oldComponent.getUniqueId(), JsonParseFlagEnum.NoParse);
448         if (componentVEither.isRight()) {
449             log.debug("Falied to fetch component {} error {}", oldComponent.getUniqueId(), componentVEither.right().value());
450             return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(componentVEither.right().value()));
451         }
452         GraphVertex componentv = componentVEither.left().value();
453         Either<GraphVertex, TitanOperationStatus> parentVertexEither = titanDao.getParentVertex(componentv, EdgeLabelEnum.VERSION, JsonParseFlagEnum.NoParse);
454         if (parentVertexEither.isRight() && parentVertexEither.right().value() != TitanOperationStatus.NOT_FOUND) {
455             log.debug("Falied to fetch parent version for component {} error {}", oldComponent.getUniqueId(), parentVertexEither.right().value());
456             return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(parentVertexEither.right().value()));
457         }
458
459         Either<ToscaElement, StorageOperationStatus> deleteToscaComponent = deleteToscaElement(componentv);
460         if (deleteToscaComponent.isRight()) {
461             log.debug("Falied to remove old component {} error {}", oldComponent.getUniqueId(), deleteToscaComponent.right().value());
462             return Either.right(deleteToscaComponent.right().value());
463         }
464         Either<Resource, StorageOperationStatus> createToscaComponent = createToscaComponent(newComponent);
465         if (createToscaComponent.isRight()) {
466             log.debug("Falied to create tosca element component {} error {}", newComponent.getUniqueId(), createToscaComponent.right().value());
467             return Either.right(createToscaComponent.right().value());
468         }
469         Resource newElement = createToscaComponent.left().value();
470         Either<GraphVertex, TitanOperationStatus> newVersionEither = titanDao.getVertexById(newElement.getUniqueId(), JsonParseFlagEnum.NoParse);
471         if (newVersionEither.isRight()) {
472             log.debug("Falied to fetch new tosca element component {} error {}", newComponent.getUniqueId(), newVersionEither.right().value());
473             return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(newVersionEither.right().value()));
474         }
475         if (parentVertexEither.isLeft()) {
476             GraphVertex previousVersionV = parentVertexEither.left().value();
477             TitanOperationStatus createEdge = titanDao.createEdge(previousVersionV, newVersionEither.left().value(), EdgeLabelEnum.VERSION, null);
478             if (createEdge != TitanOperationStatus.OK) {
479                 log.debug("Falied to associate to previous version {} new version {} error {}", previousVersionV.getUniqueId(), newVersionEither.right().value(), createEdge);
480                 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(createEdge));
481             }
482         }
483         return Either.left(newElement);
484     }
485
486     void copyArtifactsToNewComponent(Resource newComponent, Resource oldComponent) {
487         // TODO - check if required
488         Map<String, ArtifactDefinition> toscaArtifacts = oldComponent.getToscaArtifacts();
489         if (toscaArtifacts != null && !toscaArtifacts.isEmpty()) {
490             toscaArtifacts.values().stream().forEach(a -> a.setDuplicated(Boolean.TRUE));
491         }
492         newComponent.setToscaArtifacts(toscaArtifacts);
493
494         Map<String, ArtifactDefinition> artifacts = oldComponent.getArtifacts();
495         if (artifacts != null && !artifacts.isEmpty()) {
496             artifacts.values().stream().forEach(a -> a.setDuplicated(Boolean.TRUE));
497         }
498         newComponent.setArtifacts(artifacts);
499
500         Map<String, ArtifactDefinition> depArtifacts = oldComponent.getDeploymentArtifacts();
501         if (depArtifacts != null && !depArtifacts.isEmpty()) {
502             depArtifacts.values().stream().forEach(a -> a.setDuplicated(Boolean.TRUE));
503         }
504         newComponent.setDeploymentArtifacts(depArtifacts);
505
506         newComponent.setGroups(oldComponent.getGroups());
507         newComponent.setLastUpdateDate(null);
508         newComponent.setHighestVersion(true);
509     }
510
511     public <T extends Component> Either<T, StorageOperationStatus> updateToscaElement(T componentToUpdate) {
512         return updateToscaElement(componentToUpdate, new ComponentParametersView());
513     }
514
515     public <T extends Component> Either<T, StorageOperationStatus> updateToscaElement(T componentToUpdate, ComponentParametersView filterResult) {
516         String componentId = componentToUpdate.getUniqueId();
517         Either<GraphVertex, TitanOperationStatus> getVertexEither = titanDao.getVertexById(componentId, JsonParseFlagEnum.ParseAll);
518         if (getVertexEither.isRight()) {
519             log.debug(COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, componentId, getVertexEither.right().value());
520             return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(getVertexEither.right().value()));
521         }
522         GraphVertex elementV = getVertexEither.left().value();
523         ToscaElementOperation toscaElementOperation = getToscaElementOperation(elementV);
524
525         ToscaElement toscaElementToUpdate = ModelConverter.convertToToscaElement(componentToUpdate);
526         Either<ToscaElement, StorageOperationStatus> updateToscaElement = toscaElementOperation.updateToscaElement(toscaElementToUpdate, elementV, filterResult);
527         if (updateToscaElement.isRight()) {
528             log.debug("Failed to update tosca element {} error {}", componentId, updateToscaElement.right().value());
529             return Either.right(updateToscaElement.right().value());
530         }
531         return Either.left(ModelConverter.convertFromToscaElement(updateToscaElement.left().value()));
532     }
533
534     private <T extends Component> Either<T, StorageOperationStatus> getLatestByName(GraphPropertyEnum property, String nodeName, JsonParseFlagEnum parseFlag) {
535         return getLatestByName(property, nodeName, parseFlag, new ComponentParametersView());
536     }
537
538     private <T extends Component> Either<T, StorageOperationStatus> getLatestByName(GraphPropertyEnum property, String nodeName, JsonParseFlagEnum parseFlag, ComponentParametersView filter) {
539         Either<T, StorageOperationStatus> result;
540
541         Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
542         Map<GraphPropertyEnum, Object> propertiesNotToMatch = new EnumMap<>(GraphPropertyEnum.class);
543
544         propertiesToMatch.put(property, nodeName);
545         propertiesToMatch.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
546
547         propertiesNotToMatch.put(GraphPropertyEnum.IS_DELETED, true);
548
549         Either<List<GraphVertex>, TitanOperationStatus> highestResources = titanDao.getByCriteria(null, propertiesToMatch, propertiesNotToMatch, parseFlag);
550         if (highestResources.isRight()) {
551             TitanOperationStatus status = highestResources.right().value();
552             log.debug("failed to find resource with name {}. status={} ", nodeName, status);
553             result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
554             return result;
555         }
556
557         List<GraphVertex> resources = highestResources.left().value();
558         double version = 0.0;
559         GraphVertex highestResource = null;
560         for (GraphVertex vertex : resources) {
561             Object versionObj = vertex.getMetadataProperty(GraphPropertyEnum.VERSION);
562             double resourceVersion = Double.parseDouble((String) versionObj);
563             if (resourceVersion > version) {
564                 version = resourceVersion;
565                 highestResource = vertex;
566             }
567         }
568         return getToscaElementByOperation(highestResource, filter);
569     }
570
571     // endregion
572     // region - Component Get By ..
573     private <T extends Component> Either<T, StorageOperationStatus> getLatestByName(GraphPropertyEnum property, String nodeName) {
574         return getLatestByName(property, nodeName, JsonParseFlagEnum.ParseMetadata);
575     }
576
577     public <T extends Component> Either<List<T>, StorageOperationStatus> getBySystemName(ComponentTypeEnum componentType, String systemName) {
578
579         Either<List<T>, StorageOperationStatus> result = null;
580         Either<T, StorageOperationStatus> getComponentRes;
581         List<T> components = new ArrayList<>();
582         List<GraphVertex> componentVertices;
583         Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
584         Map<GraphPropertyEnum, Object> propertiesNotToMatch = new EnumMap<>(GraphPropertyEnum.class);
585
586         propertiesToMatch.put(GraphPropertyEnum.SYSTEM_NAME, systemName);
587         if (componentType != null)
588             propertiesToMatch.put(GraphPropertyEnum.COMPONENT_TYPE, componentType.name());
589
590         propertiesNotToMatch.put(GraphPropertyEnum.IS_DELETED, true);
591
592         Either<List<GraphVertex>, TitanOperationStatus> getComponentsRes = titanDao.getByCriteria(null, propertiesToMatch, propertiesNotToMatch, JsonParseFlagEnum.ParseAll);
593         if (getComponentsRes.isRight()) {
594             TitanOperationStatus status = getComponentsRes.right().value();
595             log.debug("Failed to fetch the component with system name {}. Status is {} ", systemName, status);
596             result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
597         }
598         if (result == null) {
599             componentVertices = getComponentsRes.left().value();
600             for (GraphVertex componentVertex : componentVertices) {
601                 getComponentRes = getToscaElementByOperation(componentVertex);
602                 if (getComponentRes.isRight()) {
603                     log.debug("Failed to get the component {}. Status is {} ", componentVertex.getJsonMetadataField(JsonPresentationFields.NAME), getComponentRes.right().value());
604                     result = Either.right(getComponentRes.right().value());
605                     break;
606                 }
607                 T componentBySystemName = getComponentRes.left().value();
608                 log.debug("Found component, id: {}", componentBySystemName.getUniqueId());
609                 components.add(componentBySystemName);
610             }
611         }
612         if (result == null) {
613             result = Either.left(components);
614         }
615         return result;
616     }
617
618     public <T extends Component> Either<T, StorageOperationStatus> getComponentByNameAndVersion(ComponentTypeEnum componentType, String name, String version) {
619         return getComponentByNameAndVersion(componentType, name, version, JsonParseFlagEnum.ParseAll);
620     }
621
622     public <T extends Component> Either<T, StorageOperationStatus> getComponentByNameAndVersion(ComponentTypeEnum componentType, String name, String version, JsonParseFlagEnum parseFlag) {
623         Either<T, StorageOperationStatus> result;
624
625         Map<GraphPropertyEnum, Object> hasProperties = new EnumMap<>(GraphPropertyEnum.class);
626         Map<GraphPropertyEnum, Object> hasNotProperties = new EnumMap<>(GraphPropertyEnum.class);
627
628         hasProperties.put(GraphPropertyEnum.NAME, name);
629         hasProperties.put(GraphPropertyEnum.VERSION, version);
630         hasNotProperties.put(GraphPropertyEnum.IS_DELETED, true);
631         if (componentType != null) {
632             hasProperties.put(GraphPropertyEnum.COMPONENT_TYPE, componentType.name());
633         }
634         Either<List<GraphVertex>, TitanOperationStatus> getResourceRes = titanDao.getByCriteria(null, hasProperties, hasNotProperties, parseFlag);
635         if (getResourceRes.isRight()) {
636             TitanOperationStatus status = getResourceRes.right().value();
637             log.debug("failed to find resource with name {}, version {}. Status is {} ", name, version, status);
638             result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
639             return result;
640         }
641         return getToscaElementByOperation(getResourceRes.left().value().get(0));
642     }
643
644     public Either<List<CatalogComponent>, StorageOperationStatus> getCatalogOrArchiveComponents(boolean isCatalog, List<OriginTypeEnum> excludeTypes) {
645         List<ResourceTypeEnum> excludedResourceTypes = Optional.ofNullable(excludeTypes).orElse(Collections.emptyList()).stream().filter(type -> !type.equals(OriginTypeEnum.SERVICE)).map(type -> ResourceTypeEnum.getTypeByName(type.name()))
646                 .collect(Collectors.toList());
647         return topologyTemplateOperation.getElementCatalogData(isCatalog, excludedResourceTypes);
648     }
649
650     // endregion
651     public <T extends Component> Either<List<T>, StorageOperationStatus> getCatalogComponents(ComponentTypeEnum componentType, List<OriginTypeEnum> excludeTypes, boolean isHighestVersions) {
652         List<T> components = new ArrayList<>();
653         Either<List<ToscaElement>, StorageOperationStatus> catalogDataResult;
654         List<ToscaElement> toscaElements = new ArrayList<>();
655         List<ResourceTypeEnum> excludedResourceTypes = Optional.ofNullable(excludeTypes).orElse(Collections.emptyList()).stream().filter(type -> !type.equals(OriginTypeEnum.SERVICE)).map(type -> ResourceTypeEnum.getTypeByName(type.name()))
656                 .collect(Collectors.toList());
657
658         switch (componentType) {
659             case RESOURCE:
660                 catalogDataResult = nodeTypeOperation.getElementCatalogData(ComponentTypeEnum.RESOURCE, excludedResourceTypes, isHighestVersions);
661                 if (catalogDataResult.isRight()) {
662                     return Either.right(catalogDataResult.right().value());
663                 }
664                 toscaElements = catalogDataResult.left().value();
665                 break;
666             case SERVICE:
667                 if (excludeTypes != null && excludeTypes.contains(OriginTypeEnum.SERVICE)) {
668                     break;
669                 }
670                 catalogDataResult = topologyTemplateOperation.getElementCatalogData(ComponentTypeEnum.SERVICE, null, isHighestVersions);
671                 if (catalogDataResult.isRight()) {
672                     return Either.right(catalogDataResult.right().value());
673                 }
674                 toscaElements = catalogDataResult.left().value();
675                 break;
676             default:
677                 log.debug(NOT_SUPPORTED_COMPONENT_TYPE, componentType);
678                 return Either.right(StorageOperationStatus.BAD_REQUEST);
679         }
680         toscaElements.forEach(te -> {
681             T component = ModelConverter.convertFromToscaElement(te);
682             components.add(component);
683         });
684         return Either.left(components);
685     }
686
687     public Either<List<String>, StorageOperationStatus> deleteMarkedElements(ComponentTypeEnum componentType) {
688         Either<List<GraphVertex>, StorageOperationStatus> allComponentsMarkedForDeletion;
689         switch (componentType) {
690             case RESOURCE:
691                 allComponentsMarkedForDeletion = nodeTypeOperation.getAllComponentsMarkedForDeletion(componentType);
692                 break;
693             case SERVICE:
694             case PRODUCT:
695                 allComponentsMarkedForDeletion = topologyTemplateOperation.getAllComponentsMarkedForDeletion(componentType);
696                 break;
697             default:
698                 log.debug(NOT_SUPPORTED_COMPONENT_TYPE, componentType);
699                 return Either.right(StorageOperationStatus.BAD_REQUEST);
700         }
701         if (allComponentsMarkedForDeletion.isRight()) {
702             return Either.right(allComponentsMarkedForDeletion.right().value());
703         }
704         List<GraphVertex> allMarked = allComponentsMarkedForDeletion.left().value();
705         return Either.left(checkIfInUseAndDelete(allMarked));
706     }
707
708     private List<String> checkIfInUseAndDelete(List<GraphVertex> allMarked) {
709         final List<EdgeLabelEnum> forbiddenEdgeLabelEnums = Arrays.asList(EdgeLabelEnum.INSTANCE_OF, EdgeLabelEnum.PROXY_OF, EdgeLabelEnum.ALLOTTED_OF);
710         List<String> deleted = new ArrayList<>();
711
712         for (GraphVertex elementV : allMarked) {
713             boolean isAllowedToDelete = true;
714
715             for (EdgeLabelEnum edgeLabelEnum : forbiddenEdgeLabelEnums) {
716                 Either<Edge, TitanOperationStatus> belongingEdgeByCriteria = titanDao.getBelongingEdgeByCriteria(elementV, edgeLabelEnum, null);
717                 if (belongingEdgeByCriteria.isLeft()){
718                     log.debug("Marked element {} in use. don't delete it", elementV.getUniqueId());
719                     isAllowedToDelete = false;
720                     break;
721                 }
722             }
723
724             if (isAllowedToDelete) {
725                 Either<ToscaElement, StorageOperationStatus> deleteToscaElement = deleteToscaElement(elementV);
726                 if (deleteToscaElement.isRight()) {
727                     log.debug("Failed to delete marked element UniqueID {}, Name {}, error {}", elementV.getUniqueId(), elementV.getMetadataProperties().get(GraphPropertyEnum.NAME), deleteToscaElement.right().value());
728                     continue;
729                 }
730                 deleted.add(elementV.getUniqueId());
731             }
732         }
733         return deleted;
734     }
735
736     public Either<List<String>, StorageOperationStatus> getAllComponentsMarkedForDeletion(ComponentTypeEnum componentType) {
737         Either<List<GraphVertex>, StorageOperationStatus> allComponentsMarkedForDeletion;
738         switch (componentType) {
739             case RESOURCE:
740                 allComponentsMarkedForDeletion = nodeTypeOperation.getAllComponentsMarkedForDeletion(componentType);
741                 break;
742             case SERVICE:
743             case PRODUCT:
744                 allComponentsMarkedForDeletion = topologyTemplateOperation.getAllComponentsMarkedForDeletion(componentType);
745                 break;
746             default:
747                 log.debug(NOT_SUPPORTED_COMPONENT_TYPE, componentType);
748                 return Either.right(StorageOperationStatus.BAD_REQUEST);
749         }
750         if (allComponentsMarkedForDeletion.isRight()) {
751             return Either.right(allComponentsMarkedForDeletion.right().value());
752         }
753         return Either.left(allComponentsMarkedForDeletion.left().value().stream().map(GraphVertex::getUniqueId).collect(Collectors.toList()));
754     }
755
756     // region - Component Update
757     public Either<ImmutablePair<Component, String>, StorageOperationStatus> addComponentInstanceToTopologyTemplate(Component containerComponent, Component origComponent, ComponentInstance componentInstance, boolean allowDeleted, User user) {
758
759         Either<ImmutablePair<Component, String>, StorageOperationStatus> result = null;
760         Either<ToscaElement, StorageOperationStatus> updateContainerComponentRes = null;
761         if (StringUtils.isEmpty(componentInstance.getIcon())) {
762             componentInstance.setIcon(origComponent.getIcon());
763         }
764         String nameToFindForCounter = componentInstance.getOriginType() == OriginTypeEnum.ServiceProxy ? componentInstance.getSourceModelName() + PROXY_SUFFIX : origComponent.getName();
765         String nextComponentInstanceCounter = getNextComponentInstanceCounter(containerComponent, nameToFindForCounter);
766         Either<ImmutablePair<TopologyTemplate, String>, StorageOperationStatus> addResult = nodeTemplateOperation.addComponentInstanceToTopologyTemplate(ModelConverter.convertToToscaElement(containerComponent),
767                 ModelConverter.convertToToscaElement(origComponent), nextComponentInstanceCounter, componentInstance, allowDeleted, user);
768
769         if (addResult.isRight()) {
770             CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to add the component instance {} to container component {}. ", componentInstance.getName(), containerComponent.getName());
771             result = Either.right(addResult.right().value());
772         }
773         if (result == null) {
774             updateContainerComponentRes = topologyTemplateOperation.getToscaElement(containerComponent.getUniqueId());
775             if (updateContainerComponentRes.isRight()) {
776                 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to fetch updated topology template {} with updated component instance {}. ", containerComponent.getName(), componentInstance.getName());
777                 result = Either.right(updateContainerComponentRes.right().value());
778             }
779         }
780         if (result == null) {
781             Component updatedComponent = ModelConverter.convertFromToscaElement(updateContainerComponentRes.left().value());
782             String createdInstanceId = addResult.left().value().getRight();
783             CommonUtility.addRecordToLog(log, LogLevelEnum.TRACE, "The component instance {} has been added to container component {}. ", createdInstanceId, updatedComponent.getName());
784             result = Either.left(new ImmutablePair<>(updatedComponent, createdInstanceId));
785         }
786         return result;
787     }
788
789     public StorageOperationStatus associateComponentInstancesToComponent(Component containerComponent, Map<ComponentInstance, Resource> resourcesInstancesMap, boolean allowDeleted) {
790
791         StorageOperationStatus result = null;
792         CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Going to add component instances to component {}", containerComponent.getUniqueId());
793
794         Either<GraphVertex, TitanOperationStatus> metadataVertex = titanDao.getVertexById(containerComponent.getUniqueId(), JsonParseFlagEnum.ParseAll);
795         if (metadataVertex.isRight()) {
796             TitanOperationStatus status = metadataVertex.right().value();
797             if (status == TitanOperationStatus.NOT_FOUND) {
798                 status = TitanOperationStatus.INVALID_ID;
799             }
800             result = DaoStatusConverter.convertTitanStatusToStorageStatus(status);
801         }
802         if (result == null) {
803             result = nodeTemplateOperation.associateComponentInstancesToComponent(containerComponent, resourcesInstancesMap, metadataVertex.left().value(), allowDeleted);
804         }
805         return result;
806     }
807
808     public Either<ImmutablePair<Component, String>, StorageOperationStatus> updateComponentInstanceMetadataOfTopologyTemplate(Component containerComponent, Component origComponent, ComponentInstance componentInstance) {
809
810         Either<ImmutablePair<Component, String>, StorageOperationStatus> result = null;
811
812         CommonUtility.addRecordToLog(log, LogLevelEnum.TRACE, "Going to update the metadata of the component instance {} belonging to container component {}. ", componentInstance.getName(), containerComponent.getName());
813         componentInstance.setIcon(origComponent.getIcon());
814         Either<ImmutablePair<TopologyTemplate, String>, StorageOperationStatus> updateResult = nodeTemplateOperation.updateComponentInstanceMetadataOfTopologyTemplate(ModelConverter.convertToToscaElement(containerComponent),
815                 ModelConverter.convertToToscaElement(origComponent), componentInstance);
816         if (updateResult.isRight()) {
817             CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to update the metadata of the component instance {} belonging to container component {}. ", componentInstance.getName(), containerComponent.getName());
818             result = Either.right(updateResult.right().value());
819         }
820         if (result == null) {
821             Component updatedComponent = ModelConverter.convertFromToscaElement(updateResult.left().value().getLeft());
822             String createdInstanceId = updateResult.left().value().getRight();
823             CommonUtility.addRecordToLog(log, LogLevelEnum.TRACE, "The metadata of the component instance {} has been updated to container component {}. ", createdInstanceId, updatedComponent.getName());
824             result = Either.left(new ImmutablePair<>(updatedComponent, createdInstanceId));
825         }
826         return result;
827     }
828
829     public Either<Component, StorageOperationStatus> updateComponentInstanceMetadataOfTopologyTemplate(Component containerComponent) {
830         return updateComponentInstanceMetadataOfTopologyTemplate(containerComponent, new ComponentParametersView());
831     }
832
833     public Either<Component, StorageOperationStatus> updateComponentInstanceMetadataOfTopologyTemplate(Component containerComponent, ComponentParametersView filter) {
834
835         Either<Component, StorageOperationStatus> result = null;
836
837         CommonUtility.addRecordToLog(log, LogLevelEnum.TRACE, "Going to update the metadata  belonging to container component {}. ", containerComponent.getName());
838
839         Either<TopologyTemplate, StorageOperationStatus> updateResult = nodeTemplateOperation.updateComponentInstanceMetadataOfTopologyTemplate(ModelConverter.convertToToscaElement(containerComponent), filter);
840         if (updateResult.isRight()) {
841             CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to update the metadata  belonging to container component {}. ", containerComponent.getName());
842             result = Either.right(updateResult.right().value());
843         }
844         if (result == null) {
845             Component updatedComponent = ModelConverter.convertFromToscaElement(updateResult.left().value());
846             CommonUtility.addRecordToLog(log, LogLevelEnum.TRACE, "The metadata has been updated to container component {}. ", updatedComponent.getName());
847             result = Either.left(updatedComponent);
848         }
849         return result;
850     }
851     // endregion
852
853     public Either<ImmutablePair<Component, String>, StorageOperationStatus> deleteComponentInstanceFromTopologyTemplate(Component containerComponent, String resourceInstanceId) {
854
855         Either<ImmutablePair<Component, String>, StorageOperationStatus> result = null;
856
857         CommonUtility.addRecordToLog(log, LogLevelEnum.TRACE, "Going to delete the component instance {} belonging to container component {}. ", resourceInstanceId, containerComponent.getName());
858
859         Either<ImmutablePair<TopologyTemplate, String>, StorageOperationStatus> updateResult = nodeTemplateOperation.deleteComponentInstanceFromTopologyTemplate(ModelConverter.convertToToscaElement(containerComponent), resourceInstanceId);
860         if (updateResult.isRight()) {
861             CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to delete the component instance {} belonging to container component {}. ", resourceInstanceId, containerComponent.getName());
862             result = Either.right(updateResult.right().value());
863         }
864         if (result == null) {
865             Component updatedComponent = ModelConverter.convertFromToscaElement(updateResult.left().value().getLeft());
866             String deletedInstanceId = updateResult.left().value().getRight();
867             CommonUtility.addRecordToLog(log, LogLevelEnum.TRACE, "The component instance {} has been deleted from container component {}. ", deletedInstanceId, updatedComponent.getName());
868             result = Either.left(new ImmutablePair<>(updatedComponent, deletedInstanceId));
869         }
870         return result;
871     }
872
873     private String getNextComponentInstanceCounter(Component containerComponent, String originResourceName) {
874         Integer nextCounter = 0;
875         if (CollectionUtils.isNotEmpty(containerComponent.getComponentInstances())) {
876             String normalizedName = ValidationUtils.normalizeComponentInstanceName(originResourceName);
877             Integer maxCounter = getMaxCounterFromNamesAndIds(containerComponent, normalizedName);
878             if (maxCounter != null) {
879                 nextCounter = maxCounter + 1;
880             }
881         }
882         return nextCounter.toString();
883     }
884
885     /**
886      * @return max counter of component instance Id's, null if not found
887      */
888     private Integer getMaxCounterFromNamesAndIds(Component containerComponent, String normalizedName) {
889         List<String> countersInNames = containerComponent.getComponentInstances().stream()
890                 .filter(ci -> ci.getNormalizedName() != null && ci.getNormalizedName().startsWith(normalizedName))
891                 .map(ci -> ci.getNormalizedName().split(normalizedName)[1])
892                 .collect(Collectors.toList());
893         List<String> countersInIds = containerComponent.getComponentInstances().stream()
894                 .filter(ci -> ci.getUniqueId() != null && ci.getUniqueId().contains(normalizedName))
895                 .map(ci -> ci.getUniqueId().split(normalizedName)[1])
896                 .collect(Collectors.toList());
897         List<String> namesAndIdsList = new ArrayList<>(countersInNames);
898         namesAndIdsList.addAll(countersInIds);
899         return getMaxInteger(namesAndIdsList);
900     }
901
902     private Integer getMaxInteger(List<String> counters) {
903         Integer maxCounter = 0;
904         Integer currCounter = null;
905         for (String counter : counters) {
906             try {
907                 currCounter = Integer.parseInt(counter);
908                 if (maxCounter < currCounter) {
909                     maxCounter = currCounter;
910                 }
911             } catch (NumberFormatException e) {
912                 continue;
913             }
914         }
915         return currCounter == null ? null : maxCounter;
916     }
917
918     public Either<RequirementCapabilityRelDef, StorageOperationStatus> associateResourceInstances(String componentId, RequirementCapabilityRelDef requirementDef) {
919         return nodeTemplateOperation.associateResourceInstances(componentId, requirementDef);
920
921     }
922
923     public Either<List<InputDefinition>, StorageOperationStatus> createAndAssociateInputs(Map<String, InputDefinition> inputs, String componentId) {
924
925         Either<GraphVertex, TitanOperationStatus> getVertexEither = titanDao.getVertexById(componentId, JsonParseFlagEnum.NoParse);
926         if (getVertexEither.isRight()) {
927             log.debug(COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, componentId, getVertexEither.right().value());
928             return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(getVertexEither.right().value()));
929
930         }
931
932         GraphVertex vertex = getVertexEither.left().value();
933         Map<String, PropertyDataDefinition> inputsMap = inputs.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, e -> new PropertyDataDefinition(e.getValue())));
934
935         StorageOperationStatus status = topologyTemplateOperation.associateInputsToComponent(vertex, inputsMap, componentId);
936
937         if (StorageOperationStatus.OK == status) {
938             log.debug(COMPONENT_CREATED_SUCCESSFULLY);
939             List<InputDefinition> inputsResList = null;
940             if (inputsMap != null && !inputsMap.isEmpty()) {
941                 inputsResList = inputsMap.values().stream()
942                         .map(InputDefinition::new)
943                         .collect(Collectors.toList());
944             }
945             return Either.left(inputsResList);
946         }
947         return Either.right(status);
948
949     }
950
951     public Either<List<InputDefinition>, StorageOperationStatus> addInputsToComponent(Map<String, InputDefinition> inputs, String componentId) {
952
953         Either<GraphVertex, TitanOperationStatus> getVertexEither = titanDao.getVertexById(componentId, JsonParseFlagEnum.NoParse);
954         if (getVertexEither.isRight()) {
955             log.debug(COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, componentId, getVertexEither.right().value());
956             return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(getVertexEither.right().value()));
957
958         }
959
960         GraphVertex vertex = getVertexEither.left().value();
961                 Map<String, PropertyDefinition> inputsMap = inputs.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, e -> new PropertyDefinition(e.getValue())));
962
963         StorageOperationStatus status = topologyTemplateOperation.addToscaDataToToscaElement(vertex, EdgeLabelEnum.INPUTS, VertexTypeEnum.INPUTS, inputsMap, JsonPresentationFields.NAME);
964
965         if (StorageOperationStatus.OK == status) {
966             log.debug(COMPONENT_CREATED_SUCCESSFULLY);
967             List<InputDefinition> inputsResList = null;
968             if (inputsMap != null && !inputsMap.isEmpty()) {
969                 inputsResList = inputsMap.values().stream().map(InputDefinition::new).collect(Collectors.toList());
970             }
971             return Either.left(inputsResList);
972         }
973         return Either.right(status);
974
975     }
976
977     /**
978      * Add data types into a Component.
979      *
980      * @param dataTypes   datatypes to be added. the key should be each name of data type.
981      * @param componentId unique ID of Component.
982      * @return list of data types.
983      */
984     public Either<List<DataTypeDefinition>, StorageOperationStatus> addDataTypesToComponent(Map<String, DataTypeDefinition> dataTypes, String componentId) {
985
986         log.trace("#addDataTypesToComponent - enter, componentId={}", componentId);
987
988         /* get component vertex */
989         Either<GraphVertex, TitanOperationStatus> getVertexEither = titanDao.getVertexById(componentId, JsonParseFlagEnum.NoParse);
990         if (getVertexEither.isRight()) {
991             /* not found / error */
992             log.debug(COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, componentId, getVertexEither.right().value());
993             return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(getVertexEither.right().value()));
994         }
995         GraphVertex vertex = getVertexEither.left().value();
996         log.trace("#addDataTypesToComponent - get vertex ok");
997
998         // convert DataTypeDefinition to DataTypeDataDefinition
999         Map<String, DataTypeDataDefinition> dataTypeDataMap = dataTypes.entrySet().stream()
1000                 .collect(Collectors.toMap(Map.Entry::getKey, e -> convertDataTypeToDataTypeData(e.getValue())));
1001
1002         // add datatype(s) to the Component.
1003         // if child vertex does not exist, it will be created.
1004         StorageOperationStatus status = topologyTemplateOperation.addToscaDataToToscaElement(vertex,
1005                 EdgeLabelEnum.DATA_TYPES, VertexTypeEnum.DATA_TYPES, dataTypeDataMap, JsonPresentationFields.NAME);
1006
1007         if (StorageOperationStatus.OK == status) {
1008             log.debug(COMPONENT_CREATED_SUCCESSFULLY);
1009             List<DataTypeDefinition> inputsResList = null;
1010             if (!dataTypes.isEmpty()) {
1011                 inputsResList = new ArrayList<>(dataTypes.values());
1012             }
1013             return Either.left(inputsResList);
1014         }
1015
1016         log.trace("#addDataTypesToComponent - leave");
1017         return Either.right(status);
1018     }
1019
1020     private DataTypeDataDefinition convertDataTypeToDataTypeData(DataTypeDefinition dataType) {
1021         DataTypeDataDefinition dataTypeData = new DataTypeDataDefinition(dataType);
1022         if (CollectionUtils.isNotEmpty(dataType.getProperties())) {
1023             List<PropertyDataDefinition> propertyDataList = dataType.getProperties().stream()
1024                     .map(PropertyDataDefinition::new).collect(Collectors.toList());
1025             dataTypeData.setPropertiesData(propertyDataList);
1026         }
1027
1028         // if "derivedFrom" data_type exists, copy the name to "derivedFromName"
1029         if (dataType.getDerivedFrom() != null && StringUtils.isNotEmpty(dataType.getDerivedFrom().getName())) {
1030             // if names are different, log it
1031             if (!StringUtils.equals(dataTypeData.getDerivedFromName(), dataType.getDerivedFrom().getName())) {
1032                 log.debug("#convertDataTypeToDataTypeData - derivedFromName(={}) overwritten by derivedFrom.name(={})",
1033                         dataType.getDerivedFromName(), dataType.getDerivedFrom().getName());
1034             }
1035             dataTypeData.setDerivedFromName(dataType.getDerivedFrom().getName());
1036         }
1037
1038         // supply "name" field to toscaPresentationValue in each datatype object for DAO operations
1039         dataTypeData.setToscaPresentationValue(JsonPresentationFields.NAME, dataType.getName());
1040         return dataTypeData;
1041     }
1042
1043
1044     public Either<List<InputDefinition>, StorageOperationStatus> getComponentInputs(String componentId) {
1045
1046                 Either<GraphVertex, TitanOperationStatus> getVertexEither = titanDao.getVertexById(componentId, JsonParseFlagEnum.NoParse);
1047                 if (getVertexEither.isRight()) {
1048                         log.debug("Couldn't fetch component with and unique id {}, error: {}", componentId, getVertexEither.right().value());
1049                         return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(getVertexEither.right().value()));
1050
1051                 }
1052
1053                 Either<ToscaElement, StorageOperationStatus> toscaElement =
1054                                 topologyTemplateOperation.getToscaElement(componentId);
1055                 if(toscaElement.isRight()) {
1056                         return Either.right(toscaElement.right().value());
1057                 }
1058
1059                 TopologyTemplate topologyTemplate = (TopologyTemplate) toscaElement.left().value();
1060
1061                 Map<String, PropertyDataDefinition> inputsMap = topologyTemplate.getInputs();
1062
1063                 List<InputDefinition> inputs = new ArrayList<>();
1064                 if(MapUtils.isNotEmpty(inputsMap)) {
1065                         inputs =
1066                                         inputsMap.values().stream().map(p -> new InputDefinition(p)).collect(Collectors.toList());
1067                 }
1068
1069                 return Either.left(inputs);
1070         }
1071
1072         public Either<List<InputDefinition>, StorageOperationStatus> updateInputsToComponent(List<InputDefinition> inputs, String componentId) {
1073
1074         Either<GraphVertex, TitanOperationStatus> getVertexEither = titanDao.getVertexById(componentId, JsonParseFlagEnum.NoParse);
1075         if (getVertexEither.isRight()) {
1076             log.debug(COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, componentId, getVertexEither.right().value());
1077             return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(getVertexEither.right().value()));
1078
1079         }
1080
1081         GraphVertex vertex = getVertexEither.left().value();
1082         List<PropertyDataDefinition> inputsAsDataDef = inputs.stream().map(PropertyDataDefinition::new).collect(Collectors.toList());
1083
1084         StorageOperationStatus status = topologyTemplateOperation.updateToscaDataOfToscaElement(vertex, EdgeLabelEnum.INPUTS, VertexTypeEnum.INPUTS, inputsAsDataDef, JsonPresentationFields.NAME);
1085
1086         if (StorageOperationStatus.OK == status) {
1087             log.debug(COMPONENT_CREATED_SUCCESSFULLY);
1088             List<InputDefinition> inputsResList = null;
1089             if (inputsAsDataDef != null && !inputsAsDataDef.isEmpty()) {
1090                 inputsResList = inputsAsDataDef.stream().map(InputDefinition::new).collect(Collectors.toList());
1091             }
1092             return Either.left(inputsResList);
1093         }
1094         return Either.right(status);
1095
1096     }
1097
1098     // region - ComponentInstance
1099     public Either<Map<String, List<ComponentInstanceProperty>>, StorageOperationStatus> associateComponentInstancePropertiesToComponent(Map<String, List<ComponentInstanceProperty>> instProperties, String componentId) {
1100
1101         Either<GraphVertex, TitanOperationStatus> getVertexEither = titanDao.getVertexById(componentId, JsonParseFlagEnum.NoParse);
1102         if (getVertexEither.isRight()) {
1103             log.debug(COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, componentId, getVertexEither.right().value());
1104             return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(getVertexEither.right().value()));
1105
1106         }
1107
1108         GraphVertex vertex = getVertexEither.left().value();
1109         Map<String, MapPropertiesDataDefinition> instPropsMap = new HashMap<>();
1110         if (instProperties != null) {
1111
1112             MapPropertiesDataDefinition propertiesMap;
1113             for (Entry<String, List<ComponentInstanceProperty>> entry : instProperties.entrySet()) {
1114                 propertiesMap = new MapPropertiesDataDefinition();
1115
1116                 propertiesMap.setMapToscaDataDefinition(entry.getValue().stream().map(PropertyDataDefinition::new).collect(Collectors.toMap(PropertyDataDefinition::getName, e -> e)));
1117
1118                 instPropsMap.put(entry.getKey(), propertiesMap);
1119             }
1120         }
1121
1122         StorageOperationStatus status = topologyTemplateOperation.associateInstPropertiesToComponent(vertex, instPropsMap);
1123
1124         if (StorageOperationStatus.OK == status) {
1125             log.debug(COMPONENT_CREATED_SUCCESSFULLY);
1126             return Either.left(instProperties);
1127         }
1128         return Either.right(status);
1129
1130     }
1131
1132     /**
1133      * saves the instInputs as the updated instance inputs of the component container in DB
1134      */
1135     public Either<Map<String, List<ComponentInstanceInput>>, StorageOperationStatus> updateComponentInstanceInputsToComponent(Map<String, List<ComponentInstanceInput>> instInputs, String componentId) {
1136         if (instInputs == null || instInputs.isEmpty()) {
1137             return Either.left(instInputs);
1138         }
1139         StorageOperationStatus status;
1140         for (Entry<String, List<ComponentInstanceInput>> inputsPerIntance : instInputs.entrySet()) {
1141             List<ComponentInstanceInput> toscaDataListPerInst = inputsPerIntance.getValue();
1142             List<String> pathKeysPerInst = new ArrayList<>();
1143             pathKeysPerInst.add(inputsPerIntance.getKey());
1144             status = topologyTemplateOperation.updateToscaDataDeepElementsOfToscaElement(componentId, EdgeLabelEnum.INST_INPUTS, VertexTypeEnum.INST_INPUTS, toscaDataListPerInst, pathKeysPerInst, JsonPresentationFields.NAME);
1145             if (status != StorageOperationStatus.OK) {
1146                 log.debug("Failed to update component instance inputs for instance {} in component {} edge type {} error {}", inputsPerIntance.getKey(), componentId, EdgeLabelEnum.INST_INPUTS, status);
1147                 return Either.right(status);
1148             }
1149         }
1150
1151         return Either.left(instInputs);
1152     }
1153
1154     /**
1155      * saves the instProps as the updated instance properties of the component container in DB
1156      */
1157     public Either<Map<String, List<ComponentInstanceProperty>>, StorageOperationStatus> updateComponentInstancePropsToComponent(Map<String, List<ComponentInstanceProperty>> instProps, String componentId) {
1158         if (instProps == null || instProps.isEmpty()) {
1159             return Either.left(instProps);
1160         }
1161         StorageOperationStatus status;
1162         for (Entry<String, List<ComponentInstanceProperty>> propsPerIntance : instProps.entrySet()) {
1163             List<ComponentInstanceProperty> toscaDataListPerInst = propsPerIntance.getValue();
1164             List<String> pathKeysPerInst = new ArrayList<>();
1165             pathKeysPerInst.add(propsPerIntance.getKey());
1166             status = topologyTemplateOperation.updateToscaDataDeepElementsOfToscaElement(componentId, EdgeLabelEnum.INST_PROPERTIES, VertexTypeEnum.INST_PROPERTIES, toscaDataListPerInst, pathKeysPerInst, JsonPresentationFields.NAME);
1167             if (status != StorageOperationStatus.OK) {
1168                 log.debug("Failed to update component instance inputs for instance {} in component {} edge type {} error {}", propsPerIntance.getKey(), componentId, EdgeLabelEnum.INST_PROPERTIES, status);
1169                 return Either.right(status);
1170             }
1171         }
1172
1173         return Either.left(instProps);
1174     }
1175
1176     public Either<Map<String, List<ComponentInstanceInput>>, StorageOperationStatus> associateComponentInstanceInputsToComponent(Map<String, List<ComponentInstanceInput>> instInputs, String componentId) {
1177
1178         Either<GraphVertex, TitanOperationStatus> getVertexEither = titanDao.getVertexById(componentId, JsonParseFlagEnum.NoParse);
1179         if (getVertexEither.isRight()) {
1180             log.debug(COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, componentId, getVertexEither.right().value());
1181             return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(getVertexEither.right().value()));
1182
1183         }
1184         GraphVertex vertex = getVertexEither.left().value();
1185         Map<String, MapPropertiesDataDefinition> instPropsMap = new HashMap<>();
1186         if (instInputs != null) {
1187
1188             MapPropertiesDataDefinition propertiesMap;
1189             for (Entry<String, List<ComponentInstanceInput>> entry : instInputs.entrySet()) {
1190                 propertiesMap = new MapPropertiesDataDefinition();
1191
1192                 propertiesMap.setMapToscaDataDefinition(entry.getValue().stream().map(PropertyDataDefinition::new).collect(Collectors.toMap(PropertyDataDefinition::getName, e -> e)));
1193
1194                 instPropsMap.put(entry.getKey(), propertiesMap);
1195             }
1196         }
1197
1198         StorageOperationStatus status = topologyTemplateOperation.associateInstInputsToComponent(vertex, instPropsMap);
1199
1200         if (StorageOperationStatus.OK == status) {
1201             log.debug(COMPONENT_CREATED_SUCCESSFULLY);
1202             return Either.left(instInputs);
1203         }
1204         return Either.right(status);
1205
1206     }
1207
1208     public Either<Map<String, List<ComponentInstanceInput>>, StorageOperationStatus> addComponentInstanceInputsToComponent(Component containerComponent, Map<String, List<ComponentInstanceInput>> instProperties) {
1209         requireNonNull(instProperties);
1210         StorageOperationStatus status;
1211         for (Entry<String, List<ComponentInstanceInput>> entry : instProperties.entrySet()) {
1212             List<ComponentInstanceInput> props = entry.getValue();
1213             String componentInstanceId = entry.getKey();
1214             if (!isEmpty(props)) {
1215                 for (ComponentInstanceInput property : props) {
1216                     List<ComponentInstanceInput> componentInstancesInputs = containerComponent.getComponentInstancesInputs().get(componentInstanceId);
1217                     Optional<ComponentInstanceInput> instanceProperty = componentInstancesInputs.stream()
1218                             .filter(p -> p.getName().equals(property.getName()))
1219                             .findAny();
1220                     if (instanceProperty.isPresent()) {
1221                         status = updateComponentInstanceInput(containerComponent, componentInstanceId, property);
1222                     } else {
1223                         status = addComponentInstanceInput(containerComponent, componentInstanceId, property);
1224                     }
1225                     if (status != StorageOperationStatus.OK) {
1226                         log.debug("Failed to update instance input {} for instance {} error {} ", property, componentInstanceId, status);
1227                         return Either.right(status);
1228                     } else {
1229                         log.trace("instance input {} for instance {} updated", property, componentInstanceId);
1230                     }
1231                 }
1232             }
1233         }
1234         return Either.left(instProperties);
1235     }
1236
1237     public Either<Map<String, List<ComponentInstanceProperty>>, StorageOperationStatus> addComponentInstancePropertiesToComponent(Component containerComponent, Map<String, List<ComponentInstanceProperty>> instProperties) {
1238         requireNonNull(instProperties);
1239         StorageOperationStatus status;
1240         for (Entry<String, List<ComponentInstanceProperty>> entry : instProperties.entrySet()) {
1241             List<ComponentInstanceProperty> props = entry.getValue();
1242             String componentInstanceId = entry.getKey();
1243             List<ComponentInstanceProperty> instanceProperties = containerComponent.getComponentInstancesProperties().get(componentInstanceId);
1244             if (!isEmpty(props)) {
1245                 for (ComponentInstanceProperty property : props) {
1246                     Optional<ComponentInstanceProperty> instanceProperty = instanceProperties.stream()
1247                             .filter(p -> p.getUniqueId().equals(property.getUniqueId()))
1248                             .findAny();
1249                     if (instanceProperty.isPresent()) {
1250                         status = updateComponentInstanceProperty(containerComponent, componentInstanceId, property);
1251                     } else {
1252                         status = addComponentInstanceProperty(containerComponent, componentInstanceId, property);
1253                     }
1254                     if (status != StorageOperationStatus.OK) {
1255                         log.debug("Failed to update instance property {} for instance {} error {} ", property, componentInstanceId, status);
1256                         return Either.right(status);
1257                     }
1258                 }
1259             }
1260         }
1261         return Either.left(instProperties);
1262     }
1263
1264     public StorageOperationStatus associateDeploymentArtifactsToInstances(Map<String, Map<String, ArtifactDefinition>> instDeploymentArtifacts, String componentId, User user) {
1265
1266         Either<GraphVertex, TitanOperationStatus> getVertexEither = titanDao.getVertexById(componentId, JsonParseFlagEnum.NoParse);
1267         if (getVertexEither.isRight()) {
1268             log.debug(COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, componentId, getVertexEither.right().value());
1269             return DaoStatusConverter.convertTitanStatusToStorageStatus(getVertexEither.right().value());
1270
1271         }
1272
1273         GraphVertex vertex = getVertexEither.left().value();
1274         Map<String, MapArtifactDataDefinition> instArtMap = new HashMap<>();
1275         if (instDeploymentArtifacts != null) {
1276
1277             MapArtifactDataDefinition artifactsMap;
1278             for (Entry<String, Map<String, ArtifactDefinition>> entry : instDeploymentArtifacts.entrySet()) {
1279                 Map<String, ArtifactDefinition> artList = entry.getValue();
1280                 Map<String, ArtifactDataDefinition> artifacts = artList.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, e -> new ArtifactDataDefinition(e.getValue())));
1281                 artifactsMap = nodeTemplateOperation.prepareInstDeploymentArtifactPerInstance(artifacts, entry.getKey(), user, NodeTemplateOperation.HEAT_VF_ENV_NAME);
1282
1283                 instArtMap.put(entry.getKey(), artifactsMap);
1284             }
1285         }
1286
1287         return topologyTemplateOperation.associateInstDeploymentArtifactsToComponent(vertex, instArtMap);
1288
1289     }
1290
1291     public StorageOperationStatus associateArtifactsToInstances(Map<String, Map<String, ArtifactDefinition>> instArtifacts, String componentId) {
1292
1293         Either<GraphVertex, TitanOperationStatus> getVertexEither = titanDao.getVertexById(componentId, JsonParseFlagEnum.NoParse);
1294         if (getVertexEither.isRight()) {
1295             log.debug(COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, componentId, getVertexEither.right().value());
1296             return DaoStatusConverter.convertTitanStatusToStorageStatus(getVertexEither.right().value());
1297
1298         }
1299
1300         GraphVertex vertex = getVertexEither.left().value();
1301         Map<String, MapArtifactDataDefinition> instArtMap = new HashMap<>();
1302         if (instArtifacts != null) {
1303
1304             MapArtifactDataDefinition artifactsMap;
1305             for (Entry<String, Map<String, ArtifactDefinition>> entry : instArtifacts.entrySet()) {
1306                 Map<String, ArtifactDefinition> artList = entry.getValue();
1307                 Map<String, ArtifactDataDefinition> artifacts = artList.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, e -> new ArtifactDataDefinition(e.getValue())));
1308                 artifactsMap = new MapArtifactDataDefinition(artifacts);
1309
1310                 instArtMap.put(entry.getKey(), artifactsMap);
1311             }
1312         }
1313
1314         return topologyTemplateOperation.associateInstArtifactsToComponent(vertex, instArtMap);
1315
1316     }
1317
1318     public StorageOperationStatus associateInstAttributeToComponentToInstances(Map<String, List<PropertyDefinition>> instArttributes, String componentId) {
1319
1320         Either<GraphVertex, TitanOperationStatus> getVertexEither = titanDao.getVertexById(componentId, JsonParseFlagEnum.NoParse);
1321         if (getVertexEither.isRight()) {
1322             log.debug(COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, componentId, getVertexEither.right().value());
1323             return DaoStatusConverter.convertTitanStatusToStorageStatus(getVertexEither.right().value());
1324
1325         }
1326
1327         GraphVertex vertex = getVertexEither.left().value();
1328         Map<String, MapPropertiesDataDefinition> instAttr = new HashMap<>();
1329         if (instArttributes != null) {
1330
1331             MapPropertiesDataDefinition attributesMap;
1332             for (Entry<String, List<PropertyDefinition>> entry : instArttributes.entrySet()) {
1333                 attributesMap = new MapPropertiesDataDefinition();
1334                 attributesMap.setMapToscaDataDefinition(entry.getValue().stream().map(PropertyDataDefinition::new).collect(Collectors.toMap(PropertyDataDefinition::getName, e -> e)));
1335                 instAttr.put(entry.getKey(), attributesMap);
1336             }
1337         }
1338
1339         return topologyTemplateOperation.associateInstAttributeToComponent(vertex, instAttr);
1340
1341     }
1342     // endregion
1343
1344     public StorageOperationStatus associateOrAddCalculatedCapReq(Map<ComponentInstance, Map<String, List<CapabilityDefinition>>> instCapabilties, Map<ComponentInstance, Map<String, List<RequirementDefinition>>> instReg, String componentId) {
1345         Either<GraphVertex, TitanOperationStatus> getVertexEither = titanDao.getVertexById(componentId, JsonParseFlagEnum.NoParse);
1346         if (getVertexEither.isRight()) {
1347             log.debug(COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, componentId, getVertexEither.right().value());
1348             return DaoStatusConverter.convertTitanStatusToStorageStatus(getVertexEither.right().value());
1349
1350         }
1351
1352         GraphVertex vertex = getVertexEither.left().value();
1353
1354         Map<String, MapListRequirementDataDefinition> calcRequirements = new HashMap<>();
1355
1356         Map<String, MapListCapabilityDataDefinition> calcCapabilty = new HashMap<>();
1357         Map<String, MapCapabilityProperty> calculatedCapabilitiesProperties = new HashMap<>();
1358         if (instCapabilties != null) {
1359             for (Entry<ComponentInstance, Map<String, List<CapabilityDefinition>>> entry : instCapabilties.entrySet()) {
1360
1361                 Map<String, List<CapabilityDefinition>> caps = entry.getValue();
1362                 Map<String, ListCapabilityDataDefinition> mapToscaDataDefinition = new HashMap<>();
1363                 for (Entry<String, List<CapabilityDefinition>> instCapability : caps.entrySet()) {
1364                     mapToscaDataDefinition.put(instCapability.getKey(), new ListCapabilityDataDefinition(instCapability.getValue().stream().map(CapabilityDataDefinition::new).collect(Collectors.toList())));
1365                 }
1366
1367                 ComponentInstanceDataDefinition componentInstance = new ComponentInstanceDataDefinition(entry.getKey());
1368                 MapListCapabilityDataDefinition capMap = nodeTemplateOperation.prepareCalculatedCapabiltyForNodeType(mapToscaDataDefinition, componentInstance);
1369
1370                 MapCapabilityProperty mapCapabilityProperty = ModelConverter.convertToMapOfMapCapabiltyProperties(caps, componentInstance.getUniqueId(), true);
1371
1372                 calcCapabilty.put(entry.getKey().getUniqueId(), capMap);
1373                 calculatedCapabilitiesProperties.put(entry.getKey().getUniqueId(), mapCapabilityProperty);
1374             }
1375         }
1376
1377         if (instReg != null) {
1378             for (Entry<ComponentInstance, Map<String, List<RequirementDefinition>>> entry : instReg.entrySet()) {
1379
1380                 Map<String, List<RequirementDefinition>> req = entry.getValue();
1381                 Map<String, ListRequirementDataDefinition> mapToscaDataDefinition = new HashMap<>();
1382                 for (Entry<String, List<RequirementDefinition>> instReq : req.entrySet()) {
1383                     mapToscaDataDefinition.put(instReq.getKey(), new ListRequirementDataDefinition(instReq.getValue().stream().map(RequirementDataDefinition::new).collect(Collectors.toList())));
1384                 }
1385
1386                 MapListRequirementDataDefinition capMap = nodeTemplateOperation.prepareCalculatedRequirementForNodeType(mapToscaDataDefinition, new ComponentInstanceDataDefinition(entry.getKey()));
1387
1388                 calcRequirements.put(entry.getKey().getUniqueId(), capMap);
1389             }
1390         }
1391
1392         return topologyTemplateOperation.associateOrAddCalcCapReqToComponent(vertex, calcRequirements, calcCapabilty, calculatedCapabilitiesProperties);
1393     }
1394
1395     private Either<List<Service>, StorageOperationStatus> getLatestVersionNonCheckoutServicesMetadataOnly(Map<GraphPropertyEnum, Object> hasProps, Map<GraphPropertyEnum, Object> hasNotProps) {
1396         List<Service> services = new ArrayList<>();
1397         List<LifecycleStateEnum> states = new ArrayList<>();
1398         // include props
1399         hasProps.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
1400         hasProps.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
1401
1402         // exclude props
1403         states.add(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
1404         hasNotProps.put(GraphPropertyEnum.STATE, states);
1405         hasNotProps.put(GraphPropertyEnum.IS_DELETED, true);
1406         hasNotProps.put(GraphPropertyEnum.IS_ARCHIVED, true);
1407         return fetchServicesByCriteria(services, hasProps, hasNotProps);
1408     }
1409
1410     private Either<List<Component>, StorageOperationStatus> getLatestVersionNotAbstractToscaElementsMetadataOnly(boolean isAbstract, ComponentTypeEnum componentTypeEnum, String internalComponentType, VertexTypeEnum vertexType) {
1411         List<Service> services = null;
1412         Map<GraphPropertyEnum, Object> hasProps = new EnumMap<>(GraphPropertyEnum.class);
1413         Map<GraphPropertyEnum, Object> hasNotProps = new EnumMap<>(GraphPropertyEnum.class);
1414         fillPropsMap(hasProps, hasNotProps, internalComponentType, componentTypeEnum, isAbstract, vertexType);
1415         Either<List<GraphVertex>, TitanOperationStatus> getRes = titanDao.getByCriteria(vertexType, hasProps, hasNotProps, JsonParseFlagEnum.ParseMetadata);
1416         if (getRes.isRight()) {
1417             if (getRes.right().value().equals(TitanOperationStatus.NOT_FOUND)) {
1418                 return Either.left(new ArrayList<>());
1419             } else {
1420                 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(getRes.right().value()));
1421             }
1422         }
1423         // region -> Fetch non checked-out services
1424         if (internalComponentType != null && internalComponentType.toLowerCase().trim().equals(SERVICE) && VertexTypeEnum.NODE_TYPE == vertexType) {
1425             Either<List<Service>, StorageOperationStatus> result = getLatestVersionNonCheckoutServicesMetadataOnly(new EnumMap<>(GraphPropertyEnum.class), new EnumMap<>(GraphPropertyEnum.class));
1426             if (result.isRight()) {
1427                 log.debug("Failed to fetch services for");
1428                 return Either.right(result.right().value());
1429             }
1430             services = result.left().value();
1431             if (log.isTraceEnabled() && isEmpty(services))
1432                 log.trace("No relevant services available");
1433         }
1434         // endregion
1435         List<Component> nonAbstractLatestComponents = new ArrayList<>();
1436         ComponentParametersView params = new ComponentParametersView(true);
1437         params.setIgnoreAllVersions(false);
1438         for (GraphVertex vertexComponent : getRes.left().value()) {
1439             Either<ToscaElement, StorageOperationStatus> componentRes = topologyTemplateOperation.getLightComponent(vertexComponent, componentTypeEnum, params);
1440             if (componentRes.isRight()) {
1441                 log.debug("Failed to fetch light element for {} error {}", vertexComponent.getUniqueId(), componentRes.right().value());
1442                 return Either.right(componentRes.right().value());
1443             } else {
1444                 Component component = ModelConverter.convertFromToscaElement(componentRes.left().value());
1445                 nonAbstractLatestComponents.add(component);
1446             }
1447         }
1448         if (CollectionUtils.isNotEmpty(services)) {
1449             nonAbstractLatestComponents.addAll(services);
1450         }
1451         return Either.left(nonAbstractLatestComponents);
1452     }
1453
1454     public Either<ComponentMetadataData, StorageOperationStatus> getLatestComponentMetadataByUuid(String componentUuid, JsonParseFlagEnum parseFlag, Boolean isHighest) {
1455
1456         Either<ComponentMetadataData, StorageOperationStatus> result;
1457         Map<GraphPropertyEnum, Object> hasProperties = new EnumMap<>(GraphPropertyEnum.class);
1458         hasProperties.put(GraphPropertyEnum.UUID, componentUuid);
1459         if (isHighest != null) {
1460             hasProperties.put(GraphPropertyEnum.IS_HIGHEST_VERSION, isHighest);
1461         }
1462         Map<GraphPropertyEnum, Object> propertiesNotToMatch = new EnumMap<>(GraphPropertyEnum.class);
1463         propertiesNotToMatch.put(GraphPropertyEnum.IS_DELETED, true);
1464         propertiesNotToMatch.put(GraphPropertyEnum.IS_ARCHIVED, true); //US382674, US382683
1465
1466         Either<List<GraphVertex>, TitanOperationStatus> getRes = titanDao.getByCriteria(null, hasProperties, propertiesNotToMatch, parseFlag);
1467         if (getRes.isRight()) {
1468             result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(getRes.right().value()));
1469         } else {
1470             List<ComponentMetadataData> latestVersionList = getRes.left().value().stream().map(ModelConverter::convertToComponentMetadata).collect(Collectors.toList());
1471             ComponentMetadataData latestVersion = latestVersionList.size() == 1 ? latestVersionList.get(0)
1472                     : latestVersionList.stream().max((c1, c2) -> Double.compare(Double.parseDouble(c1.getMetadataDataDefinition().getVersion()), Double.parseDouble(c2.getMetadataDataDefinition().getVersion()))).get();
1473             result = Either.left(latestVersion);
1474         }
1475         return result;
1476     }
1477
1478     public Either<ComponentMetadataData, StorageOperationStatus> getComponentMetadata(String componentId) {
1479         Either<ComponentMetadataData, StorageOperationStatus> result;
1480         Either<GraphVertex, TitanOperationStatus> getRes = titanDao.getVertexById(componentId, JsonParseFlagEnum.ParseMetadata);
1481         if (getRes.isRight()) {
1482             result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(getRes.right().value()));
1483         } else {
1484             ComponentMetadataData componentMetadata = ModelConverter.convertToComponentMetadata(getRes.left().value());
1485             result = Either.left(componentMetadata);
1486         }
1487         return result;
1488     }
1489
1490     public Either<List<Component>, StorageOperationStatus> getLatestVersionNotAbstractComponents(boolean isAbstract, ComponentTypeEnum componentTypeEnum,
1491                                                                                                  String internalComponentType, List<String> componentUids) {
1492
1493         List<Component> components = new ArrayList<>();
1494         if (componentUids == null) {
1495             Either<List<String>, StorageOperationStatus> componentUidsRes = getComponentUids(isAbstract, componentTypeEnum, internalComponentType);
1496             if (componentUidsRes.isRight()) {
1497                 return Either.right(componentUidsRes.right().value());
1498             }
1499             componentUids = componentUidsRes.left().value();
1500         }
1501         if (!isEmpty(componentUids)) {
1502             for (String componentUid : componentUids) {
1503                 ComponentParametersView componentParametersView = buildComponentViewForNotAbstract();
1504                 if ("vl".equalsIgnoreCase(internalComponentType)) {
1505                     componentParametersView.setIgnoreCapabilities(false);
1506                     componentParametersView.setIgnoreRequirements(false);
1507                 }
1508                 Either<ToscaElement, StorageOperationStatus> getToscaElementRes = nodeTemplateOperation.getToscaElementOperation(componentTypeEnum).getLightComponent(componentUid, componentTypeEnum, componentParametersView);
1509                 if (getToscaElementRes.isRight()) {
1510                     log.debug("Failed to fetch resource for error is {}", getToscaElementRes.right().value());
1511                     return Either.right(getToscaElementRes.right().value());
1512                 }
1513                 Component component = ModelConverter.convertFromToscaElement(getToscaElementRes.left().value());
1514                 nullifySomeComponentProperties(component);
1515                 components.add(component);
1516             }
1517         }
1518         return Either.left(components);
1519     }
1520
1521     public void nullifySomeComponentProperties(Component component) {
1522         component.setContactId(null);
1523         component.setCreationDate(null);
1524         component.setCreatorUserId(null);
1525         component.setCreatorFullName(null);
1526         component.setLastUpdateDate(null);
1527         component.setLastUpdaterUserId(null);
1528         component.setLastUpdaterFullName(null);
1529         component.setNormalizedName(null);
1530     }
1531
1532     private Either<List<String>, StorageOperationStatus> getComponentUids(boolean isAbstract, ComponentTypeEnum componentTypeEnum, String internalComponentType) {
1533
1534         Either<List<Component>, StorageOperationStatus> getToscaElementsRes = getLatestVersionNotAbstractMetadataOnly(isAbstract, componentTypeEnum, internalComponentType);
1535         if (getToscaElementsRes.isRight()) {
1536             return Either.right(getToscaElementsRes.right().value());
1537         }
1538         List<Component> collection = getToscaElementsRes.left().value();
1539         List<String> componentUids;
1540         if (collection == null) {
1541             componentUids = new ArrayList<>();
1542         } else {
1543             componentUids = collection.stream()
1544                     .map(Component::getUniqueId)
1545                     .collect(Collectors.toList());
1546         }
1547         return Either.left(componentUids);
1548     }
1549
1550     private ComponentParametersView buildComponentViewForNotAbstract() {
1551         ComponentParametersView componentParametersView = new ComponentParametersView();
1552         componentParametersView.disableAll();
1553         componentParametersView.setIgnoreCategories(false);
1554         componentParametersView.setIgnoreAllVersions(false);
1555         return componentParametersView;
1556     }
1557
1558     public Either<Boolean, StorageOperationStatus> validateComponentNameExists(String name, ResourceTypeEnum resourceType, ComponentTypeEnum componentType) {
1559         Either<Boolean, StorageOperationStatus> result = validateComponentNameUniqueness(name, resourceType, componentType);
1560         if (result.isLeft()) {
1561             result = Either.left(!result.left().value());
1562         }
1563         return result;
1564     }
1565
1566     public Either<Boolean, StorageOperationStatus> validateComponentNameUniqueness(String name, ResourceTypeEnum resourceType, ComponentTypeEnum componentType) {
1567         VertexTypeEnum vertexType = ModelConverter.isAtomicComponent(resourceType) ? VertexTypeEnum.NODE_TYPE : VertexTypeEnum.TOPOLOGY_TEMPLATE;
1568         String normalizedName = ValidationUtils.normaliseComponentName(name);
1569         Map<GraphPropertyEnum, Object> properties = new EnumMap<>(GraphPropertyEnum.class);
1570         properties.put(GraphPropertyEnum.NORMALIZED_NAME, normalizedName);
1571         properties.put(GraphPropertyEnum.COMPONENT_TYPE, componentType.name());
1572
1573         Either<List<GraphVertex>, TitanOperationStatus> vertexEither = titanDao.getByCriteria(vertexType, properties, JsonParseFlagEnum.NoParse);
1574         if (vertexEither.isRight() && vertexEither.right().value() != TitanOperationStatus.NOT_FOUND) {
1575             log.debug("failed to get vertex from graph with property normalizedName: {}", normalizedName);
1576             return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(vertexEither.right().value()));
1577         }
1578         List<GraphVertex> vertexList = vertexEither.isLeft() ? vertexEither.left().value() : null;
1579         if (vertexList != null && !vertexList.isEmpty()) {
1580             return Either.left(false);
1581         } else {
1582             return Either.left(true);
1583         }
1584     }
1585
1586     private void fillNodeTypePropsMap(Map<GraphPropertyEnum, Object> hasProps, Map<GraphPropertyEnum, Object> hasNotProps, String internalComponentType) {
1587         switch (internalComponentType.toLowerCase()) {
1588             case "vf":
1589             case "cvfc":
1590                 hasNotProps.put(GraphPropertyEnum.RESOURCE_TYPE, Arrays.asList(ResourceTypeEnum.VFCMT.name(), ResourceTypeEnum.Configuration.name()));
1591                 break;
1592             case SERVICE:
1593             case "pnf":
1594             case "cr":
1595                 hasNotProps.put(GraphPropertyEnum.RESOURCE_TYPE, Arrays.asList(ResourceTypeEnum.VFC.name(), ResourceTypeEnum.VFCMT.name()));
1596                 break;
1597             case "vl":
1598                 hasProps.put(GraphPropertyEnum.RESOURCE_TYPE, ResourceTypeEnum.VL.name());
1599                 break;
1600             default:
1601                 break;
1602         }
1603     }
1604
1605     private void fillTopologyTemplatePropsMap(Map<GraphPropertyEnum, Object> hasProps, Map<GraphPropertyEnum, Object> hasNotProps, ComponentTypeEnum componentTypeEnum) {
1606         switch (componentTypeEnum) {
1607             case RESOURCE:
1608                 hasProps.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
1609                 break;
1610             case SERVICE:
1611                 hasProps.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
1612                 break;
1613             default:
1614                 break;
1615         }
1616         hasNotProps.put(GraphPropertyEnum.RESOURCE_TYPE, ResourceTypeEnum.CVFC.name());
1617     }
1618
1619     private void fillPropsMap(Map<GraphPropertyEnum, Object> hasProps, Map<GraphPropertyEnum, Object> hasNotProps, String internalComponentType, ComponentTypeEnum componentTypeEnum, boolean isAbstract, VertexTypeEnum internalVertexType) {
1620         hasNotProps.put(GraphPropertyEnum.STATE, LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT.name());
1621
1622         hasNotProps.put(GraphPropertyEnum.IS_DELETED, true);
1623         hasNotProps.put(GraphPropertyEnum.IS_ARCHIVED, true);
1624         hasProps.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
1625         if (VertexTypeEnum.NODE_TYPE == internalVertexType) {
1626             hasProps.put(GraphPropertyEnum.IS_ABSTRACT, isAbstract);
1627             if (internalComponentType != null) {
1628                 fillNodeTypePropsMap(hasProps, hasNotProps, internalComponentType);
1629             }
1630         } else {
1631             fillTopologyTemplatePropsMap(hasProps, hasNotProps, componentTypeEnum);
1632         }
1633     }
1634
1635     private List<VertexTypeEnum> getInternalVertexTypes(ComponentTypeEnum componentTypeEnum, String internalComponentType) {
1636         List<VertexTypeEnum> internalVertexTypes = new ArrayList<>();
1637         if (ComponentTypeEnum.RESOURCE == componentTypeEnum) {
1638             internalVertexTypes.add(VertexTypeEnum.NODE_TYPE);
1639         }
1640         if (ComponentTypeEnum.SERVICE == componentTypeEnum || SERVICE.equalsIgnoreCase(internalComponentType)) {
1641             internalVertexTypes.add(VertexTypeEnum.TOPOLOGY_TEMPLATE);
1642         }
1643         return internalVertexTypes;
1644     }
1645
1646     public Either<List<Component>, StorageOperationStatus> getLatestVersionNotAbstractMetadataOnly(boolean isAbstract, ComponentTypeEnum componentTypeEnum, String internalComponentType) {
1647         List<VertexTypeEnum> internalVertexTypes = getInternalVertexTypes(componentTypeEnum, internalComponentType);
1648         List<Component> result = new ArrayList<>();
1649         for (VertexTypeEnum vertexType : internalVertexTypes) {
1650             Either<List<Component>, StorageOperationStatus> listByVertexType = getLatestVersionNotAbstractToscaElementsMetadataOnly(isAbstract, componentTypeEnum, internalComponentType, vertexType);
1651             if (listByVertexType.isRight()) {
1652                 return listByVertexType;
1653             }
1654             result.addAll(listByVertexType.left().value());
1655         }
1656         return Either.left(result);
1657
1658     }
1659
1660     private Either<List<Component>, StorageOperationStatus> getLatestComponentListByUuid(String componentUuid, Map<GraphPropertyEnum, Object> additionalPropertiesToMatch) {
1661         Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
1662         if (additionalPropertiesToMatch != null) {
1663             propertiesToMatch.putAll(additionalPropertiesToMatch);
1664         }
1665         propertiesToMatch.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
1666         return getComponentListByUuid(componentUuid, propertiesToMatch);
1667     }
1668
1669     public Either<Component, StorageOperationStatus> getComponentByUuidAndVersion(String componentUuid, String version) {
1670         Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
1671
1672         propertiesToMatch.put(GraphPropertyEnum.UUID, componentUuid);
1673         propertiesToMatch.put(GraphPropertyEnum.VERSION, version);
1674
1675         Map<GraphPropertyEnum, Object> propertiesNotToMatch = new EnumMap<>(GraphPropertyEnum.class);
1676         propertiesNotToMatch.put(GraphPropertyEnum.IS_DELETED, true);
1677         Either<List<GraphVertex>, TitanOperationStatus> vertexEither = titanDao.getByCriteria(null, propertiesToMatch, propertiesNotToMatch, JsonParseFlagEnum.ParseAll);
1678         if (vertexEither.isRight()) {
1679             return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(vertexEither.right().value()));
1680         }
1681
1682         List<GraphVertex> vertexList = vertexEither.isLeft() ? vertexEither.left().value() : null;
1683         if (vertexList == null || vertexList.isEmpty() || vertexList.size() > 1) {
1684             return Either.right(StorageOperationStatus.NOT_FOUND);
1685         }
1686
1687         return getToscaElementByOperation(vertexList.get(0));
1688     }
1689
1690     public Either<List<Component>, StorageOperationStatus> getComponentListByUuid(String componentUuid, Map<GraphPropertyEnum, Object> additionalPropertiesToMatch) {
1691
1692         Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
1693
1694         if (additionalPropertiesToMatch != null) {
1695             propertiesToMatch.putAll(additionalPropertiesToMatch);
1696         }
1697
1698         propertiesToMatch.put(GraphPropertyEnum.UUID, componentUuid);
1699
1700         Map<GraphPropertyEnum, Object> propertiesNotToMatch = new EnumMap<>(GraphPropertyEnum.class);
1701         propertiesNotToMatch.put(GraphPropertyEnum.IS_DELETED, true);
1702         propertiesNotToMatch.put(GraphPropertyEnum.IS_ARCHIVED, true); //US382674, US382683
1703
1704         Either<List<GraphVertex>, TitanOperationStatus> vertexEither = titanDao.getByCriteria(null, propertiesToMatch, propertiesNotToMatch, JsonParseFlagEnum.ParseAll);
1705
1706         if (vertexEither.isRight()) {
1707             log.debug("Couldn't fetch metadata for component with uuid {}, error: {}", componentUuid, vertexEither.right().value());
1708             return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(vertexEither.right().value()));
1709         }
1710         List<GraphVertex> vertexList = vertexEither.isLeft() ? vertexEither.left().value() : null;
1711
1712         if (vertexList == null || vertexList.isEmpty()) {
1713             log.debug("Component with uuid {} was not found", componentUuid);
1714             return Either.right(StorageOperationStatus.NOT_FOUND);
1715         }
1716
1717         ArrayList<Component> latestComponents = new ArrayList<>();
1718         for (GraphVertex vertex : vertexList) {
1719             Either<Component, StorageOperationStatus> toscaElementByOperation = getToscaElementByOperation(vertex);
1720
1721             if (toscaElementByOperation.isRight()) {
1722                 log.debug("Could not fetch the following Component by UUID {}", vertex.getUniqueId());
1723                 return Either.right(toscaElementByOperation.right().value());
1724             }
1725
1726             latestComponents.add(toscaElementByOperation.left().value());
1727         }
1728
1729         if (latestComponents.size() > 1) {
1730             for (Component component : latestComponents) {
1731                 if (component.isHighestVersion()) {
1732                     LinkedList<Component> highestComponent = new LinkedList<>();
1733                     highestComponent.add(component);
1734                     return Either.left(highestComponent);
1735                 }
1736             }
1737         }
1738
1739         return Either.left(latestComponents);
1740     }
1741
1742     public Either<Component, StorageOperationStatus> getLatestServiceByUuid(String serviceUuid) {
1743         Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
1744         propertiesToMatch.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
1745         return getLatestComponentByUuid(serviceUuid, propertiesToMatch);
1746     }
1747
1748     public Either<Component, StorageOperationStatus> getLatestComponentByUuid(String componentUuid) {
1749         return getLatestComponentByUuid(componentUuid, null);
1750     }
1751
1752     public Either<Component, StorageOperationStatus> getLatestComponentByUuid(String componentUuid, Map<GraphPropertyEnum, Object> propertiesToMatch) {
1753
1754         Either<List<Component>, StorageOperationStatus> latestVersionListEither = getLatestComponentListByUuid(componentUuid, propertiesToMatch);
1755
1756         if (latestVersionListEither.isRight()) {
1757             return Either.right(latestVersionListEither.right().value());
1758         }
1759
1760         List<Component> latestVersionList = latestVersionListEither.left().value();
1761
1762         if (latestVersionList.isEmpty()) {
1763             return Either.right(StorageOperationStatus.NOT_FOUND);
1764         }
1765         Component component = latestVersionList.size() == 1 ? latestVersionList.get(0) : latestVersionList.stream().max((c1, c2) -> Double.compare(Double.parseDouble(c1.getVersion()), Double.parseDouble(c2.getVersion()))).get();
1766
1767         return Either.left(component);
1768     }
1769
1770     public Either<List<Resource>, StorageOperationStatus> getAllCertifiedResources(boolean isAbstract, Boolean isHighest) {
1771
1772         List<Resource> resources = new ArrayList<>();
1773         Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
1774         Map<GraphPropertyEnum, Object> propertiesNotToMatch = new EnumMap<>(GraphPropertyEnum.class);
1775
1776         propertiesToMatch.put(GraphPropertyEnum.IS_ABSTRACT, isAbstract);
1777         if (isHighest != null) {
1778             propertiesToMatch.put(GraphPropertyEnum.IS_HIGHEST_VERSION, isHighest);
1779         }
1780         propertiesToMatch.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
1781         propertiesToMatch.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
1782         propertiesNotToMatch.put(GraphPropertyEnum.IS_DELETED, true);
1783
1784         Either<List<GraphVertex>, TitanOperationStatus> getResourcesRes = titanDao.getByCriteria(null, propertiesToMatch, propertiesNotToMatch, JsonParseFlagEnum.ParseAll);
1785
1786         if (getResourcesRes.isRight()) {
1787             log.debug("Failed to fetch all certified resources. Status is {}", getResourcesRes.right().value());
1788             return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(getResourcesRes.right().value()));
1789         }
1790         List<GraphVertex> resourceVerticies = getResourcesRes.left().value();
1791         for (GraphVertex resourceV : resourceVerticies) {
1792             Either<Resource, StorageOperationStatus> getResourceRes = getToscaElement(resourceV);
1793             if (getResourceRes.isRight()) {
1794                 return Either.right(getResourceRes.right().value());
1795             }
1796             resources.add(getResourceRes.left().value());
1797         }
1798         return Either.left(resources);
1799     }
1800
1801     public <T extends Component> Either<T, StorageOperationStatus> getLatestByNameAndVersion(String name, String version, JsonParseFlagEnum parseFlag) {
1802         Either<T, StorageOperationStatus> result;
1803
1804         Map<GraphPropertyEnum, Object> hasProperties = new EnumMap<>(GraphPropertyEnum.class);
1805         Map<GraphPropertyEnum, Object> hasNotProperties = new EnumMap<>(GraphPropertyEnum.class);
1806
1807         hasProperties.put(GraphPropertyEnum.NAME, name);
1808         hasProperties.put(GraphPropertyEnum.VERSION, version);
1809         hasProperties.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
1810
1811         hasNotProperties.put(GraphPropertyEnum.IS_DELETED, true);
1812
1813         Either<List<GraphVertex>, TitanOperationStatus> getResourceRes = titanDao.getByCriteria(null, hasProperties, hasNotProperties, parseFlag);
1814         if (getResourceRes.isRight()) {
1815             TitanOperationStatus status = getResourceRes.right().value();
1816             log.debug("failed to find resource with name {}, version {}. Status is {} ", name, version, status);
1817             result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
1818             return result;
1819         }
1820         return getToscaElementByOperation(getResourceRes.left().value().get(0));
1821     }
1822
1823     public Either<Resource, StorageOperationStatus> getLatestComponentByCsarOrName(ComponentTypeEnum componentType, String csarUUID, String systemName) {
1824         return getLatestComponentByCsarOrName(componentType, csarUUID, systemName, JsonParseFlagEnum.ParseAll);
1825     }
1826
1827     public Either<Resource, StorageOperationStatus> getLatestComponentByCsarOrName(ComponentTypeEnum componentType, String csarUUID, String systemName, JsonParseFlagEnum parseFlag) {
1828         Map<GraphPropertyEnum, Object> props = new EnumMap<>(GraphPropertyEnum.class);
1829         Map<GraphPropertyEnum, Object> propsHasNot = new EnumMap<>(GraphPropertyEnum.class);
1830         props.put(GraphPropertyEnum.CSAR_UUID, csarUUID);
1831         props.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
1832         if (componentType != null) {
1833             props.put(GraphPropertyEnum.COMPONENT_TYPE, componentType.name());
1834         }
1835         propsHasNot.put(GraphPropertyEnum.IS_DELETED, true);
1836
1837         GraphVertex resourceMetadataData = null;
1838         List<GraphVertex> resourceMetadataDataList = null;
1839         Either<List<GraphVertex>, TitanOperationStatus> byCsar = titanDao.getByCriteria(null, props, propsHasNot, JsonParseFlagEnum.ParseMetadata);
1840         if (byCsar.isRight()) {
1841             if (TitanOperationStatus.NOT_FOUND == byCsar.right().value()) {
1842                 // Fix Defect DE256036
1843                 if (StringUtils.isEmpty(systemName)) {
1844                     return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(TitanOperationStatus.NOT_FOUND));
1845                 }
1846
1847                 props.clear();
1848                 props.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
1849                 props.put(GraphPropertyEnum.SYSTEM_NAME, systemName);
1850                 Either<List<GraphVertex>, TitanOperationStatus> bySystemname = titanDao.getByCriteria(null, props, JsonParseFlagEnum.ParseMetadata);
1851                 if (bySystemname.isRight()) {
1852                     log.debug("getLatestResourceByCsarOrName - Failed to find by system name {}  error {} ", systemName, bySystemname.right().value());
1853                     return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(bySystemname.right().value()));
1854                 }
1855                 if (bySystemname.left().value().size() > 2) {
1856                     log.debug("getLatestResourceByCsarOrName - getByCriteria(by system name) must return only 2 latest version, but was returned - {}", bySystemname.left().value().size());
1857                     return Either.right(StorageOperationStatus.GENERAL_ERROR);
1858                 }
1859                 resourceMetadataDataList = bySystemname.left().value();
1860                 if (resourceMetadataDataList.size() == 1) {
1861                     resourceMetadataData = resourceMetadataDataList.get(0);
1862                 } else {
1863                     for (GraphVertex curResource : resourceMetadataDataList) {
1864                         if (!((String) curResource.getJsonMetadataField(JsonPresentationFields.LIFECYCLE_STATE)).equals("CERTIFIED")) {
1865                             resourceMetadataData = curResource;
1866                             break;
1867                         }
1868                     }
1869                 }
1870                 if (resourceMetadataData == null) {
1871                     log.debug("getLatestResourceByCsarOrName - getByCriteria(by system name) returned 2 latest CERTIFIED versions");
1872                     return Either.right(StorageOperationStatus.GENERAL_ERROR);
1873                 }
1874                 if (resourceMetadataData.getJsonMetadataField(JsonPresentationFields.CSAR_UUID) != null && !((String) resourceMetadataData.getJsonMetadataField(JsonPresentationFields.CSAR_UUID)).equals(csarUUID)) {
1875                     log.debug("getLatestResourceByCsarOrName - same system name {} but different csarUUID. exist {} and new {} ", systemName, resourceMetadataData.getJsonMetadataField(JsonPresentationFields.CSAR_UUID), csarUUID);
1876                     // correct error will be returned from create flow. with all
1877                     // correct audit records!!!!!
1878                     return Either.right(StorageOperationStatus.NOT_FOUND);
1879                 }
1880                 return getToscaElement((String) resourceMetadataData.getUniqueId());
1881             }
1882         } else {
1883             resourceMetadataDataList = byCsar.left().value();
1884             if (resourceMetadataDataList.size() > 2) {
1885                 log.debug("getLatestResourceByCsarOrName - getByCriteria(by csar) must return only 2 latest version, but was returned - {}", byCsar.left().value().size());
1886                 return Either.right(StorageOperationStatus.GENERAL_ERROR);
1887             }
1888             if (resourceMetadataDataList.size() == 1) {
1889                 resourceMetadataData = resourceMetadataDataList.get(0);
1890             } else {
1891                 for (GraphVertex curResource : resourceMetadataDataList) {
1892                     if (!((String) curResource.getJsonMetadataField(JsonPresentationFields.LIFECYCLE_STATE)).equals("CERTIFIED")) {
1893                         resourceMetadataData = curResource;
1894                         break;
1895                     }
1896                 }
1897             }
1898             if (resourceMetadataData == null) {
1899                 log.debug("getLatestResourceByCsarOrName - getByCriteria(by csar) returned 2 latest CERTIFIED versions");
1900                 return Either.right(StorageOperationStatus.GENERAL_ERROR);
1901             }
1902             return getToscaElement((String) resourceMetadataData.getJsonMetadataField(JsonPresentationFields.UNIQUE_ID), parseFlag);
1903         }
1904         return null;
1905     }
1906
1907     public Either<Boolean, StorageOperationStatus> validateToscaResourceNameExtends(String templateNameCurrent, String templateNameExtends) {
1908
1909         String currentTemplateNameChecked = templateNameExtends;
1910
1911         while (currentTemplateNameChecked != null && !currentTemplateNameChecked.equalsIgnoreCase(templateNameCurrent)) {
1912             Either<Resource, StorageOperationStatus> latestByToscaResourceName = getLatestByToscaResourceName(currentTemplateNameChecked);
1913
1914             if (latestByToscaResourceName.isRight()) {
1915                 return latestByToscaResourceName.right().value() == StorageOperationStatus.NOT_FOUND ? Either.left(false) : Either.right(latestByToscaResourceName.right().value());
1916             }
1917
1918             Resource value = latestByToscaResourceName.left().value();
1919
1920             if (value.getDerivedFrom() != null) {
1921                 currentTemplateNameChecked = value.getDerivedFrom().get(0);
1922             } else {
1923                 currentTemplateNameChecked = null;
1924             }
1925         }
1926
1927         return (currentTemplateNameChecked != null && currentTemplateNameChecked.equalsIgnoreCase(templateNameCurrent)) ? Either.left(true) : Either.left(false);
1928     }
1929
1930     public Either<List<Component>, StorageOperationStatus> fetchMetaDataByResourceType(String resourceType, ComponentParametersView filterBy) {
1931         Map<GraphPropertyEnum, Object> props = new EnumMap<>(GraphPropertyEnum.class);
1932         props.put(GraphPropertyEnum.RESOURCE_TYPE, resourceType);
1933         props.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
1934         Map<GraphPropertyEnum, Object> propsHasNotToMatch = new EnumMap<>(GraphPropertyEnum.class);
1935         propsHasNotToMatch.put(GraphPropertyEnum.IS_DELETED, true);
1936         Either<List<GraphVertex>, TitanOperationStatus> resourcesByTypeEither = titanDao.getByCriteria(null, props, propsHasNotToMatch, JsonParseFlagEnum.ParseMetadata);
1937
1938         if (resourcesByTypeEither.isRight()) {
1939             return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(resourcesByTypeEither.right().value()));
1940         }
1941
1942         List<GraphVertex> vertexList = resourcesByTypeEither.left().value();
1943         List<Component> components = new ArrayList<>();
1944
1945         for (GraphVertex vertex : vertexList) {
1946             components.add(getToscaElementByOperation(vertex, filterBy).left().value());
1947         }
1948
1949         return Either.left(components);
1950     }
1951
1952     public void commit() {
1953         titanDao.commit();
1954     }
1955
1956     public Either<Service, StorageOperationStatus> updateDistributionStatus(Service service, User user, DistributionStatusEnum distributionStatus) {
1957         Either<GraphVertex, StorageOperationStatus> updateDistributionStatus = topologyTemplateOperation.updateDistributionStatus(service.getUniqueId(), user, distributionStatus);
1958         if (updateDistributionStatus.isRight()) {
1959             return Either.right(updateDistributionStatus.right().value());
1960         }
1961         GraphVertex serviceV = updateDistributionStatus.left().value();
1962         service.setDistributionStatus(distributionStatus);
1963         service.setLastUpdateDate((Long) serviceV.getJsonMetadataField(JsonPresentationFields.LAST_UPDATE_DATE));
1964         return Either.left(service);
1965     }
1966
1967     public Either<ComponentMetadataData, StorageOperationStatus> updateComponentLastUpdateDateOnGraph(Component component) {
1968
1969         Either<ComponentMetadataData, StorageOperationStatus> result = null;
1970         GraphVertex serviceVertex;
1971         Either<GraphVertex, TitanOperationStatus> updateRes = null;
1972         Either<GraphVertex, TitanOperationStatus> getRes = titanDao.getVertexById(component.getUniqueId(), JsonParseFlagEnum.ParseMetadata);
1973         if (getRes.isRight()) {
1974             TitanOperationStatus status = getRes.right().value();
1975             log.error("Failed to fetch component {}. status is {}", component.getUniqueId(), status);
1976             result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
1977         }
1978         if (result == null) {
1979             serviceVertex = getRes.left().value();
1980             long lastUpdateDate = System.currentTimeMillis();
1981             serviceVertex.setJsonMetadataField(JsonPresentationFields.LAST_UPDATE_DATE, lastUpdateDate);
1982             component.setLastUpdateDate(lastUpdateDate);
1983             updateRes = titanDao.updateVertex(serviceVertex);
1984             if (updateRes.isRight()) {
1985                 result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(updateRes.right().value()));
1986             }
1987         }
1988         if (result == null) {
1989             result = Either.left(ModelConverter.convertToComponentMetadata(updateRes.left().value()));
1990         }
1991         return result;
1992     }
1993
1994     public HealingTitanDao getTitanDao() {
1995         return titanDao;
1996     }
1997
1998     public Either<List<Service>, StorageOperationStatus> getCertifiedServicesWithDistStatus(Set<DistributionStatusEnum> distStatus) {
1999         Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
2000         propertiesToMatch.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
2001
2002         return getServicesWithDistStatus(distStatus, propertiesToMatch);
2003     }
2004
2005     public Either<List<Service>, StorageOperationStatus> getServicesWithDistStatus(Set<DistributionStatusEnum> distStatus, Map<GraphPropertyEnum, Object> additionalPropertiesToMatch) {
2006
2007         List<Service> servicesAll = new ArrayList<>();
2008
2009         Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
2010         Map<GraphPropertyEnum, Object> propertiesNotToMatch = new EnumMap<>(GraphPropertyEnum.class);
2011
2012         if (additionalPropertiesToMatch != null && !additionalPropertiesToMatch.isEmpty()) {
2013             propertiesToMatch.putAll(additionalPropertiesToMatch);
2014         }
2015
2016         propertiesToMatch.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
2017
2018         propertiesNotToMatch.put(GraphPropertyEnum.IS_DELETED, true);
2019
2020         if (distStatus != null && !distStatus.isEmpty()) {
2021             for (DistributionStatusEnum state : distStatus) {
2022                 propertiesToMatch.put(GraphPropertyEnum.DISTRIBUTION_STATUS, state.name());
2023                 Either<List<Service>, StorageOperationStatus> fetchServicesByCriteria = fetchServicesByCriteria(servicesAll, propertiesToMatch, propertiesNotToMatch);
2024                 if (fetchServicesByCriteria.isRight()) {
2025                     return fetchServicesByCriteria;
2026                 } else {
2027                     servicesAll = fetchServicesByCriteria.left().value();
2028                 }
2029             }
2030             return Either.left(servicesAll);
2031         } else {
2032             return fetchServicesByCriteria(servicesAll, propertiesToMatch, propertiesNotToMatch);
2033         }
2034     }
2035
2036     private Either<List<Service>, StorageOperationStatus> fetchServicesByCriteria(List<Service> servicesAll, Map<GraphPropertyEnum, Object> propertiesToMatch, Map<GraphPropertyEnum, Object> propertiesNotToMatch) {
2037         Either<List<GraphVertex>, TitanOperationStatus> getRes = titanDao.getByCriteria(VertexTypeEnum.TOPOLOGY_TEMPLATE, propertiesToMatch, propertiesNotToMatch, JsonParseFlagEnum.ParseAll);
2038         if (getRes.isRight()) {
2039             if (getRes.right().value() != TitanOperationStatus.NOT_FOUND) {
2040                 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to fetch certified services by match properties {} not match properties {} . Status is {}. ", propertiesToMatch, propertiesNotToMatch, getRes.right().value());
2041                 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(getRes.right().value()));
2042             }
2043         } else {
2044             for (GraphVertex vertex : getRes.left().value()) {
2045                 Either<ToscaElement, StorageOperationStatus> getServiceRes = topologyTemplateOperation.getLightComponent(vertex, ComponentTypeEnum.SERVICE, new ComponentParametersView(true));
2046
2047                 if (getServiceRes.isRight()) {
2048                     CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to fetch certified service {}. Status is {}. ", vertex.getJsonMetadataField(JsonPresentationFields.NAME), getServiceRes.right().value());
2049                     return Either.right(getServiceRes.right().value());
2050                 } else {
2051                     servicesAll.add(ModelConverter.convertFromToscaElement(getServiceRes.left().value()));
2052                 }
2053             }
2054         }
2055         return Either.left(servicesAll);
2056     }
2057
2058     public void rollback() {
2059         titanDao.rollback();
2060     }
2061
2062     public StorageOperationStatus addDeploymentArtifactsToInstance(String componentId, ComponentInstance componentInstance, Map<String, ArtifactDefinition> finalDeploymentArtifacts) {
2063         Map<String, ArtifactDataDefinition> instDeplArtifacts = finalDeploymentArtifacts.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, e -> new ArtifactDataDefinition(e.getValue())));
2064
2065         return nodeTemplateOperation.addDeploymentArtifactsToInstance(componentId, componentInstance.getUniqueId(), instDeplArtifacts);
2066     }
2067
2068     public StorageOperationStatus addInformationalArtifactsToInstance(String componentId, ComponentInstance componentInstance, Map<String, ArtifactDefinition> artifacts) {
2069         StorageOperationStatus status = StorageOperationStatus.OK;
2070         if (MapUtils.isNotEmpty(artifacts)) {
2071             Map<String, ArtifactDataDefinition> instDeplArtifacts = artifacts.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, e -> new ArtifactDataDefinition(e.getValue())));
2072             status = nodeTemplateOperation.addInformationalArtifactsToInstance(componentId, componentInstance.getUniqueId(), instDeplArtifacts);
2073         }
2074         return status;
2075     }
2076
2077     public StorageOperationStatus generateCustomizationUUIDOnInstance(String componentId, String instanceId) {
2078         return nodeTemplateOperation.generateCustomizationUUIDOnInstance(componentId, instanceId);
2079     }
2080
2081     public StorageOperationStatus generateCustomizationUUIDOnInstanceGroup(String componentId, String instanceId, List<String> groupInstances) {
2082         return nodeTemplateOperation.generateCustomizationUUIDOnInstanceGroup(componentId, instanceId, groupInstances);
2083     }
2084
2085                 public Either<PropertyDefinition, StorageOperationStatus> addPropertyToComponent(String propertyName,
2086                                                                                                                                                                          PropertyDefinition newPropertyDefinition,
2087                                                                                                                                                                          Component component) {
2088
2089                 Either<PropertyDefinition, StorageOperationStatus> result = null;
2090                 Either<Component, StorageOperationStatus> getUpdatedComponentRes = null;
2091                 newPropertyDefinition.setName(propertyName);
2092
2093                 StorageOperationStatus status = getToscaElementOperation(component)
2094                                 .addToscaDataToToscaElement(component.getUniqueId(), EdgeLabelEnum.PROPERTIES, VertexTypeEnum.PROPERTIES, newPropertyDefinition, JsonPresentationFields.NAME);
2095                 if (status != StorageOperationStatus.OK) {
2096                         CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to add the property {} to the component {}. Status is {}. ", propertyName, component.getName(), status);
2097                         result = Either.right(status);
2098                 }
2099                 if (result == null) {
2100                         ComponentParametersView filter = new ComponentParametersView(true);
2101                         filter.setIgnoreProperties(false);
2102                         filter.setIgnoreInputs(false);
2103                         getUpdatedComponentRes = getToscaElement(component.getUniqueId(), filter);
2104                         if (getUpdatedComponentRes.isRight()) {
2105                                 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to get updated component {}. Status is {}. ", component.getUniqueId(), getUpdatedComponentRes.right().value());
2106                                 result = Either.right(status);
2107                         }
2108                 }
2109                 if (result == null) {
2110                         PropertyDefinition newProperty = null;
2111                         List<PropertyDefinition> properties =
2112                                         (getUpdatedComponentRes.left().value()).getProperties();
2113                         if (CollectionUtils.isNotEmpty(properties)) {
2114                                 Optional<PropertyDefinition> propertyOptional = properties.stream().filter(
2115                                                 propertyEntry -> propertyEntry.getName().equals(propertyName)).findAny();
2116                                 if (propertyOptional.isPresent()) {
2117                                         newProperty = propertyOptional.get();
2118                                 }
2119                         }
2120                         if (newProperty == null) {
2121                                 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to find recently added property {} on the component {}. Status is {}. ", propertyName, component.getUniqueId(), StorageOperationStatus.NOT_FOUND);
2122                                 result = Either.right(StorageOperationStatus.NOT_FOUND);
2123                         } else {
2124                                 result = Either.left(newProperty);
2125                         }
2126                 }
2127                 return result;
2128         }
2129         public StorageOperationStatus deletePropertyOfComponent(Component component, String propertyName) {
2130                 return getToscaElementOperation(component).deleteToscaDataElement(component.getUniqueId(), EdgeLabelEnum.PROPERTIES, VertexTypeEnum.PROPERTIES, propertyName, JsonPresentationFields.NAME);
2131         }
2132
2133         public StorageOperationStatus deleteAttributeOfResource(Component component, String attributeName) {
2134                 return getToscaElementOperation(component).deleteToscaDataElement(component.getUniqueId(), EdgeLabelEnum.ATTRIBUTES, VertexTypeEnum.ATTRIBUTES, attributeName, JsonPresentationFields.NAME);
2135         }
2136
2137     public StorageOperationStatus deleteInputOfResource(Component resource, String inputName) {
2138         return getToscaElementOperation(resource).deleteToscaDataElement(resource.getUniqueId(), EdgeLabelEnum.INPUTS, VertexTypeEnum.INPUTS, inputName, JsonPresentationFields.NAME);
2139     }
2140
2141     /**
2142      * Deletes a data type from a component.
2143      * @param component the container which has the data type
2144      * @param dataTypeName the data type name to be deleted
2145      * @return Operation result.
2146      */
2147     public StorageOperationStatus deleteDataTypeOfComponent(Component component, String dataTypeName) {
2148         return getToscaElementOperation(component).deleteToscaDataElement(component.getUniqueId(), EdgeLabelEnum.DATA_TYPES, VertexTypeEnum.DATA_TYPES, dataTypeName, JsonPresentationFields.NAME);
2149     }
2150
2151         public Either<PropertyDefinition, StorageOperationStatus> updatePropertyOfComponent(Component component,
2152                                                                                                                                                                                 PropertyDefinition newPropertyDefinition) {
2153
2154                 Either<Component, StorageOperationStatus> getUpdatedComponentRes = null;
2155                 Either<PropertyDefinition, StorageOperationStatus> result = null;
2156                 StorageOperationStatus status = getToscaElementOperation(component).updateToscaDataOfToscaElement(component.getUniqueId(), EdgeLabelEnum.PROPERTIES, VertexTypeEnum.PROPERTIES, newPropertyDefinition, JsonPresentationFields.NAME);
2157                 if (status != StorageOperationStatus.OK) {
2158                         CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to add the property {} to the resource {}. Status is {}. ", newPropertyDefinition.getName(), component.getName(), status);
2159                         result = Either.right(status);
2160                 }
2161                 if (result == null) {
2162                         ComponentParametersView filter = new ComponentParametersView(true);
2163                         filter.setIgnoreProperties(false);
2164                         getUpdatedComponentRes = getToscaElement(component.getUniqueId(), filter);
2165                         if (getUpdatedComponentRes.isRight()) {
2166                                 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to get updated resource {}. Status is {}. ", component.getUniqueId(), getUpdatedComponentRes.right().value());
2167                                 result = Either.right(status);
2168                         }
2169                 }
2170                 if (result == null) {
2171                         Optional<PropertyDefinition> newProperty = (getUpdatedComponentRes.left().value())
2172                                         .getProperties().stream().filter(p -> p.getName().equals(newPropertyDefinition.getName())).findAny();
2173                         if (newProperty.isPresent()) {
2174                                 result = Either.left(newProperty.get());
2175                         } else {
2176                                 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to find recently added property {} on the resource {}. Status is {}. ", newPropertyDefinition.getName(), component.getUniqueId(), StorageOperationStatus.NOT_FOUND);
2177                                 result = Either.right(StorageOperationStatus.NOT_FOUND);
2178                         }
2179                 }
2180                 return result;
2181         }
2182
2183
2184
2185         public Either<PropertyDefinition, StorageOperationStatus> addAttributeOfResource(Component component, PropertyDefinition newAttributeDef) {
2186
2187         Either<Component, StorageOperationStatus> getUpdatedComponentRes = null;
2188         Either<PropertyDefinition, StorageOperationStatus> result = null;
2189         if (newAttributeDef.getUniqueId() == null || newAttributeDef.getUniqueId().isEmpty()) {
2190             String attUniqueId = UniqueIdBuilder.buildAttributeUid(component.getUniqueId(), newAttributeDef.getName());
2191             newAttributeDef.setUniqueId(attUniqueId);
2192         }
2193
2194         StorageOperationStatus status = getToscaElementOperation(component).addToscaDataToToscaElement(component.getUniqueId(), EdgeLabelEnum.ATTRIBUTES, VertexTypeEnum.ATTRIBUTES, newAttributeDef, JsonPresentationFields.NAME);
2195         if (status != StorageOperationStatus.OK) {
2196             CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, FAILED_TO_ADD_THE_PROPERTY_TO_THE_RESOURCE_STATUS_IS, newAttributeDef.getName(), component.getName(), status);
2197             result = Either.right(status);
2198         }
2199         if (result == null) {
2200             ComponentParametersView filter = new ComponentParametersView(true);
2201             filter.setIgnoreAttributesFrom(false);
2202             getUpdatedComponentRes = getToscaElement(component.getUniqueId(), filter);
2203             if (getUpdatedComponentRes.isRight()) {
2204                 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, FAILED_TO_GET_UPDATED_RESOURCE_STATUS_IS, component.getUniqueId(), getUpdatedComponentRes.right().value());
2205                 result = Either.right(status);
2206             }
2207         }
2208         if (result == null) {
2209             Optional<PropertyDefinition> newAttribute = ((Resource) getUpdatedComponentRes.left().value()).getAttributes().stream().filter(p -> p.getName().equals(newAttributeDef.getName())).findAny();
2210             if (newAttribute.isPresent()) {
2211                 result = Either.left(newAttribute.get());
2212             } else {
2213                 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, FAILED_TO_FIND_RECENTLY_ADDED_PROPERTY_ON_THE_RESOURCE_STATUS_IS, newAttributeDef.getName(), component.getUniqueId(), StorageOperationStatus.NOT_FOUND);
2214                 result = Either.right(StorageOperationStatus.NOT_FOUND);
2215             }
2216         }
2217         return result;
2218     }
2219
2220     public Either<PropertyDefinition, StorageOperationStatus> updateAttributeOfResource(Component component, PropertyDefinition newAttributeDef) {
2221
2222         Either<Component, StorageOperationStatus> getUpdatedComponentRes = null;
2223         Either<PropertyDefinition, StorageOperationStatus> result = null;
2224         StorageOperationStatus status = getToscaElementOperation(component).updateToscaDataOfToscaElement(component.getUniqueId(), EdgeLabelEnum.ATTRIBUTES, VertexTypeEnum.ATTRIBUTES, newAttributeDef, JsonPresentationFields.NAME);
2225         if (status != StorageOperationStatus.OK) {
2226             CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, FAILED_TO_ADD_THE_PROPERTY_TO_THE_RESOURCE_STATUS_IS, newAttributeDef.getName(), component.getName(), status);
2227             result = Either.right(status);
2228         }
2229         if (result == null) {
2230             ComponentParametersView filter = new ComponentParametersView(true);
2231             filter.setIgnoreAttributesFrom(false);
2232             getUpdatedComponentRes = getToscaElement(component.getUniqueId(), filter);
2233             if (getUpdatedComponentRes.isRight()) {
2234                 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, FAILED_TO_GET_UPDATED_RESOURCE_STATUS_IS, component.getUniqueId(), getUpdatedComponentRes.right().value());
2235                 result = Either.right(status);
2236             }
2237         }
2238         if (result == null) {
2239             Optional<PropertyDefinition> newProperty = ((Resource) getUpdatedComponentRes.left().value()).getAttributes().stream().filter(p -> p.getName().equals(newAttributeDef.getName())).findAny();
2240             if (newProperty.isPresent()) {
2241                 result = Either.left(newProperty.get());
2242             } else {
2243                 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, FAILED_TO_FIND_RECENTLY_ADDED_PROPERTY_ON_THE_RESOURCE_STATUS_IS, newAttributeDef.getName(), component.getUniqueId(), StorageOperationStatus.NOT_FOUND);
2244                 result = Either.right(StorageOperationStatus.NOT_FOUND);
2245             }
2246         }
2247         return result;
2248     }
2249
2250     public Either<InputDefinition, StorageOperationStatus> updateInputOfComponent(Component component, InputDefinition newInputDefinition) {
2251
2252         Either<Component, StorageOperationStatus> getUpdatedComponentRes = null;
2253         Either<InputDefinition, StorageOperationStatus> result = null;
2254         StorageOperationStatus status = getToscaElementOperation(component).updateToscaDataOfToscaElement(component.getUniqueId(), EdgeLabelEnum.INPUTS, VertexTypeEnum.INPUTS, newInputDefinition, JsonPresentationFields.NAME);
2255         if (status != StorageOperationStatus.OK) {
2256             CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to update the input {} to the component {}. Status is {}. ", newInputDefinition.getName(), component.getName(), status);
2257             result = Either.right(status);
2258         }
2259         if (result == null) {
2260             ComponentParametersView filter = new ComponentParametersView(true);
2261             filter.setIgnoreInputs(false);
2262             getUpdatedComponentRes = getToscaElement(component.getUniqueId(), filter);
2263             if (getUpdatedComponentRes.isRight()) {
2264                 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, FAILED_TO_GET_UPDATED_RESOURCE_STATUS_IS, component.getUniqueId(), getUpdatedComponentRes.right().value());
2265                 result = Either.right(status);
2266             }
2267         }
2268         if (result == null) {
2269             Optional<InputDefinition> updatedInput = getUpdatedComponentRes.left().value().getInputs().stream().filter(p -> p.getName().equals(newInputDefinition.getName())).findAny();
2270             if (updatedInput.isPresent()) {
2271                 result = Either.left(updatedInput.get());
2272             } else {
2273                 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to find recently updated inputs {} on the resource {}. Status is {}. ", newInputDefinition.getName(), component.getUniqueId(), StorageOperationStatus.NOT_FOUND);
2274                 result = Either.right(StorageOperationStatus.NOT_FOUND);
2275             }
2276         }
2277         return result;
2278     }
2279
2280     /**
2281      * method - ename the group instances after referenced container name renamed flow - VF rename -(triggers)-> Group rename
2282      *
2283      * @param containerComponent  - container such as service
2284      * @param componentInstance   - context component
2285      * @param componentInstanceId - id
2286      * @return - successfull/failed status
2287      **/
2288     public Either<StorageOperationStatus, StorageOperationStatus> cleanAndAddGroupInstancesToComponentInstance(Component containerComponent, ComponentInstance componentInstance, String componentInstanceId) {
2289         String uniqueId = componentInstance.getUniqueId();
2290         StorageOperationStatus status = nodeTemplateOperation.deleteToscaDataDeepElementsBlockOfToscaElement(containerComponent.getUniqueId(), EdgeLabelEnum.INST_GROUPS, VertexTypeEnum.INST_GROUPS, uniqueId);
2291         if (status != StorageOperationStatus.OK && status != StorageOperationStatus.NOT_FOUND) {
2292             CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to delete group instances for container {}. error {] ", componentInstanceId, status);
2293             return Either.right(status);
2294         }
2295         if (componentInstance.getGroupInstances() != null) {
2296             status = addGroupInstancesToComponentInstance(containerComponent, componentInstance, componentInstance.getGroupInstances());
2297             if (status != StorageOperationStatus.OK && status != StorageOperationStatus.NOT_FOUND) {
2298                 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to add group instances for container {}. error {] ", componentInstanceId, status);
2299                 return Either.right(status);
2300             }
2301         }
2302         return Either.left(status);
2303     }
2304
2305     public StorageOperationStatus addGroupInstancesToComponentInstance(Component containerComponent, ComponentInstance componentInstance, List<GroupDefinition> groups, Map<String, List<ArtifactDefinition>> groupInstancesArtifacts) {
2306         return nodeTemplateOperation.addGroupInstancesToComponentInstance(containerComponent, componentInstance, groups, groupInstancesArtifacts);
2307     }
2308
2309     public Either<List<GroupDefinition>, StorageOperationStatus> updateGroupsOnComponent(Component component, List<GroupDataDefinition> updatedGroups) {
2310         return groupsOperation.updateGroups(component, updatedGroups, true);
2311     }
2312
2313     public Either<List<GroupInstance>, StorageOperationStatus> updateGroupInstancesOnComponent(Component component, String instanceId, List<GroupInstance> updatedGroupInstances) {
2314         return groupsOperation.updateGroupInstances(component, instanceId, updatedGroupInstances);
2315     }
2316
2317     public StorageOperationStatus addGroupInstancesToComponentInstance(Component containerComponent, ComponentInstance componentInstance, List<GroupInstance> groupInstances) {
2318         return nodeTemplateOperation.addGroupInstancesToComponentInstance(containerComponent, componentInstance, groupInstances);
2319     }
2320
2321     public StorageOperationStatus addDeploymentArtifactsToComponentInstance(Component containerComponent, ComponentInstance componentInstance, Map<String, ArtifactDefinition> deploymentArtifacts) {
2322         return nodeTemplateOperation.addDeploymentArtifactsToComponentInstance(containerComponent, componentInstance, deploymentArtifacts);
2323     }
2324
2325     public StorageOperationStatus updateComponentInstanceProperty(Component containerComponent, String componentInstanceId, ComponentInstanceProperty property) {
2326         return nodeTemplateOperation.updateComponentInstanceProperty(containerComponent, componentInstanceId, property);
2327     }
2328
2329     public StorageOperationStatus updateComponentInstanceProperties(Component containerComponent, String componentInstanceId, List<ComponentInstanceProperty> properties) {
2330         return nodeTemplateOperation.updateComponentInstanceProperties(containerComponent, componentInstanceId, properties);
2331     }
2332
2333
2334     public StorageOperationStatus addComponentInstanceProperty(Component containerComponent, String componentInstanceId, ComponentInstanceProperty property) {
2335         return nodeTemplateOperation.addComponentInstanceProperty(containerComponent, componentInstanceId, property);
2336     }
2337
2338     public StorageOperationStatus updateComponentInstanceAttribute(Component containerComponent, String componentInstanceId, ComponentInstanceProperty property){
2339         return nodeTemplateOperation.updateComponentInstanceAttribute(containerComponent, componentInstanceId, property);
2340     }
2341
2342     public StorageOperationStatus addComponentInstanceAttribute(Component containerComponent, String componentInstanceId, ComponentInstanceProperty property){
2343         return nodeTemplateOperation.addComponentInstanceAttribute(containerComponent, componentInstanceId, property);
2344     }
2345
2346     public StorageOperationStatus updateComponentInstanceInput(Component containerComponent, String componentInstanceId, ComponentInstanceInput property) {
2347         return nodeTemplateOperation.updateComponentInstanceInput(containerComponent, componentInstanceId, property);
2348     }
2349
2350     public StorageOperationStatus updateComponentInstanceInputs(Component containerComponent, String componentInstanceId, List<ComponentInstanceInput> instanceInputs) {
2351         return nodeTemplateOperation.updateComponentInstanceInputs(containerComponent, componentInstanceId, instanceInputs);
2352     }
2353
2354     public StorageOperationStatus addComponentInstanceInput(Component containerComponent, String componentInstanceId, ComponentInstanceInput property) {
2355         return nodeTemplateOperation.addComponentInstanceInput(containerComponent, componentInstanceId, property);
2356     }
2357
2358     public void setNodeTypeOperation(NodeTypeOperation nodeTypeOperation) {
2359         this.nodeTypeOperation = nodeTypeOperation;
2360     }
2361
2362     public void setTopologyTemplateOperation(TopologyTemplateOperation topologyTemplateOperation) {
2363         this.topologyTemplateOperation = topologyTemplateOperation;
2364     }
2365
2366     public StorageOperationStatus deleteComponentInstanceInputsFromTopologyTemplate(Component containerComponent, List<InputDefinition> inputsToDelete) {
2367         return topologyTemplateOperation.deleteToscaDataElements(containerComponent.getUniqueId(), EdgeLabelEnum.INPUTS, inputsToDelete.stream().map(PropertyDataDefinition::getName).collect(Collectors.toList()));
2368     }
2369
2370     public StorageOperationStatus updateComponentInstanceCapabiltyProperty(Component containerComponent, String componentInstanceUniqueId, String capabilityUniqueId, ComponentInstanceProperty property) {
2371         return nodeTemplateOperation.updateComponentInstanceCapabilityProperty(containerComponent, componentInstanceUniqueId, capabilityUniqueId, property);
2372     }
2373
2374     public StorageOperationStatus updateComponentInstanceCapabilityProperties(Component containerComponent, String componentInstanceUniqueId) {
2375         return convertComponentInstanceProperties(containerComponent, componentInstanceUniqueId)
2376                 .map(instanceCapProps -> topologyTemplateOperation.updateComponentInstanceCapabilityProperties(containerComponent, componentInstanceUniqueId, instanceCapProps))
2377                 .orElse(StorageOperationStatus.NOT_FOUND);
2378     }
2379
2380     public StorageOperationStatus updateComponentInstanceInterfaces(Component containerComponent, String componentInstanceUniqueId) {
2381         MapInterfaceDataDefinition mapInterfaceDataDefinition =
2382                 convertComponentInstanceInterfaces(containerComponent, componentInstanceUniqueId);
2383         return topologyTemplateOperation
2384                 .updateComponentInstanceInterfaces(containerComponent, componentInstanceUniqueId, mapInterfaceDataDefinition);
2385     }
2386
2387         public StorageOperationStatus updateComponentCalculatedCapabilitiesProperties(Component containerComponent) {
2388                 Map<String, MapCapabilityProperty> mapCapabiltyPropertyMap =
2389         convertComponentCapabilitiesProperties(containerComponent);
2390                 return nodeTemplateOperation.overrideComponentCapabilitiesProperties(containerComponent, mapCapabiltyPropertyMap);
2391         }
2392
2393     public StorageOperationStatus deleteAllCalculatedCapabilitiesRequirements(String topologyTemplateId) {
2394         StorageOperationStatus status = topologyTemplateOperation.removeToscaData(topologyTemplateId, EdgeLabelEnum.CALCULATED_CAPABILITIES, VertexTypeEnum.CALCULATED_CAPABILITIES);
2395         if (status == StorageOperationStatus.OK) {
2396             status = topologyTemplateOperation.removeToscaData(topologyTemplateId, EdgeLabelEnum.CALCULATED_REQUIREMENTS, VertexTypeEnum.CALCULATED_REQUIREMENTS);
2397         }
2398         if (status == StorageOperationStatus.OK) {
2399             status = topologyTemplateOperation.removeToscaData(topologyTemplateId, EdgeLabelEnum.CALCULATED_CAP_PROPERTIES, VertexTypeEnum.CALCULATED_CAP_PROPERTIES);
2400         }
2401         return status;
2402     }
2403
2404     public Either<Component, StorageOperationStatus> shouldUpgradeToLatestDerived(Resource clonedResource) {
2405         String componentId = clonedResource.getUniqueId();
2406         Either<GraphVertex, TitanOperationStatus> getVertexEither = titanDao.getVertexById(componentId, JsonParseFlagEnum.NoParse);
2407         if (getVertexEither.isRight()) {
2408             log.debug(COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, componentId, getVertexEither.right().value());
2409             return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(getVertexEither.right().value()));
2410
2411         }
2412         GraphVertex nodeTypeV = getVertexEither.left().value();
2413
2414         ToscaElement toscaElementToUpdate = ModelConverter.convertToToscaElement(clonedResource);
2415
2416         Either<ToscaElement, StorageOperationStatus> shouldUpdateDerivedVersion = nodeTypeOperation.shouldUpdateDerivedVersion(toscaElementToUpdate, nodeTypeV);
2417         if (shouldUpdateDerivedVersion.isRight() && StorageOperationStatus.OK != shouldUpdateDerivedVersion.right().value()) {
2418             log.debug("Failed to update derived version for node type {} derived {}, error: {}", componentId, clonedResource.getDerivedFrom().get(0), shouldUpdateDerivedVersion.right().value());
2419             return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(getVertexEither.right().value()));
2420         }
2421         if (shouldUpdateDerivedVersion.isLeft()) {
2422             return Either.left(ModelConverter.convertFromToscaElement(shouldUpdateDerivedVersion.left().value()));
2423         }
2424         return Either.left(clonedResource);
2425     }
2426
2427     /**
2428      * Returns list of ComponentInstanceProperty belonging to component instance capability specified by name, type and ownerId
2429      */
2430     public Either<List<ComponentInstanceProperty>, StorageOperationStatus> getComponentInstanceCapabilityProperties(String componentId, String instanceId, String capabilityName, String capabilityType, String ownerId) {
2431         return topologyTemplateOperation.getComponentInstanceCapabilityProperties(componentId, instanceId, capabilityName, capabilityType, ownerId);
2432     }
2433
2434         private MapInterfaceDataDefinition convertComponentInstanceInterfaces(Component currComponent,
2435                                                                                                                                                                                                                                                                                                 String componentInstanceId) {
2436                 MapInterfaceDataDefinition mapInterfaceDataDefinition = new MapInterfaceDataDefinition();
2437                 List<ComponentInstanceInterface> componentInterface = currComponent.getComponentInstancesInterfaces().get(componentInstanceId);
2438
2439                 if(CollectionUtils.isNotEmpty(componentInterface)) {
2440                         componentInterface.stream().forEach(interfaceDef -> mapInterfaceDataDefinition.put
2441                                         (interfaceDef.getUniqueId(), interfaceDef));
2442                 }
2443
2444                 return mapInterfaceDataDefinition;
2445         }
2446
2447   private Map<String, MapCapabilityProperty> convertComponentCapabilitiesProperties(Component currComponent) {
2448     Map<String, MapCapabilityProperty> map = ModelConverter.extractCapabilityPropertiesFromGroups(currComponent.getGroups(), true);
2449     map.putAll(ModelConverter.extractCapabilityProperteisFromInstances(currComponent.getComponentInstances(), true));
2450     return map;
2451   }
2452
2453     private Optional<MapCapabilityProperty> convertComponentInstanceProperties(Component component, String instanceId) {
2454         return component.fetchInstanceById(instanceId)
2455                 .map(ci -> ModelConverter.convertToMapOfMapCapabiltyProperties(ci.getCapabilities(), instanceId));
2456     }
2457
2458     public Either<PolicyDefinition, StorageOperationStatus> associatePolicyToComponent(String componentId, PolicyDefinition policyDefinition, int counter) {
2459         Either<PolicyDefinition, StorageOperationStatus> result = null;
2460         Either<GraphVertex, TitanOperationStatus> getVertexEither;
2461         getVertexEither = titanDao.getVertexById(componentId, JsonParseFlagEnum.ParseMetadata);
2462         if (getVertexEither.isRight()) {
2463             log.error(COULDNT_FETCH_A_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, componentId, getVertexEither.right().value());
2464             result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(getVertexEither.right().value()));
2465         } else {
2466             if (getVertexEither.left().value().getLabel() != VertexTypeEnum.TOPOLOGY_TEMPLATE) {
2467                 log.error("Policy association to component of Tosca type {} is not allowed. ", getVertexEither.left().value().getLabel());
2468                 result = Either.right(StorageOperationStatus.BAD_REQUEST);
2469             }
2470         }
2471         if (result == null) {
2472             StorageOperationStatus status = topologyTemplateOperation.addPolicyToToscaElement(getVertexEither.left().value(), policyDefinition, counter);
2473             if (status != StorageOperationStatus.OK) {
2474                 return Either.right(status);
2475             }
2476         }
2477         if (result == null) {
2478             result = Either.left(policyDefinition);
2479         }
2480         return result;
2481     }
2482
2483     public StorageOperationStatus associatePoliciesToComponent(String componentId, List<PolicyDefinition> policies) {
2484         log.debug("#associatePoliciesToComponent - associating policies for component {}.", componentId);
2485         return titanDao.getVertexById(componentId, JsonParseFlagEnum.ParseMetadata)
2486                 .either(containerVertex -> topologyTemplateOperation.addPoliciesToToscaElement(containerVertex, policies),
2487                         DaoStatusConverter::convertTitanStatusToStorageStatus);
2488     }
2489
2490     public Either<PolicyDefinition, StorageOperationStatus> updatePolicyOfComponent(String componentId, PolicyDefinition policyDefinition) {
2491         Either<PolicyDefinition, StorageOperationStatus> result = null;
2492         Either<GraphVertex, TitanOperationStatus> getVertexEither;
2493         getVertexEither = titanDao.getVertexById(componentId, JsonParseFlagEnum.NoParse);
2494         if (getVertexEither.isRight()) {
2495             log.error(COULDNT_FETCH_A_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, componentId, getVertexEither.right().value());
2496             result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(getVertexEither.right().value()));
2497         }
2498         if (result == null) {
2499             StorageOperationStatus status = topologyTemplateOperation.updatePolicyOfToscaElement(getVertexEither.left().value(), policyDefinition);
2500             if (status != StorageOperationStatus.OK) {
2501                 return Either.right(status);
2502             }
2503         }
2504         if (result == null) {
2505             result = Either.left(policyDefinition);
2506         }
2507         return result;
2508     }
2509
2510     public StorageOperationStatus updatePoliciesOfComponent(String componentId, List<PolicyDefinition> policyDefinition) {
2511         log.debug("#updatePoliciesOfComponent - updating policies for component {}", componentId);
2512         return titanDao.getVertexById(componentId, JsonParseFlagEnum.NoParse)
2513                 .right()
2514                 .map(DaoStatusConverter::convertTitanStatusToStorageStatus)
2515                 .either(containerVertex -> topologyTemplateOperation.updatePoliciesOfToscaElement(containerVertex, policyDefinition),
2516                         err -> err);
2517     }
2518
2519     public StorageOperationStatus removePolicyFromComponent(String componentId, String policyId) {
2520         StorageOperationStatus status = null;
2521         Either<GraphVertex, TitanOperationStatus> getVertexEither = titanDao.getVertexById(componentId, JsonParseFlagEnum.NoParse);
2522         if (getVertexEither.isRight()) {
2523             log.error(COULDNT_FETCH_A_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, componentId, getVertexEither.right().value());
2524             status = DaoStatusConverter.convertTitanStatusToStorageStatus(getVertexEither.right().value());
2525         }
2526         if (status == null) {
2527             status = topologyTemplateOperation.removePolicyFromToscaElement(getVertexEither.left().value(), policyId);
2528         }
2529         return status;
2530     }
2531
2532     public boolean canAddGroups(String componentId) {
2533         GraphVertex vertex = titanDao.getVertexById(componentId)
2534                 .left()
2535                 .on(this::onTitanError);
2536         return topologyTemplateOperation.hasEdgeOfType(vertex, EdgeLabelEnum.GROUPS);
2537     }
2538
2539     GraphVertex onTitanError(TitanOperationStatus toe) {
2540         throw new StorageException(
2541                 DaoStatusConverter.convertTitanStatusToStorageStatus(toe));
2542     }
2543
2544     public void updateNamesOfCalculatedCapabilitiesRequirements(String componentId){
2545         topologyTemplateOperation
2546                 .updateNamesOfCalculatedCapabilitiesRequirements(componentId, getTopologyTemplate(componentId));
2547     }
2548
2549     public void revertNamesOfCalculatedCapabilitiesRequirements(String componentId) {
2550         topologyTemplateOperation
2551                 .revertNamesOfCalculatedCapabilitiesRequirements(componentId, getTopologyTemplate(componentId));
2552     }
2553
2554     private TopologyTemplate getTopologyTemplate(String componentId) {
2555         return (TopologyTemplate)topologyTemplateOperation
2556                 .getToscaElement(componentId, getFilterComponentWithCapProperties())
2557                 .left()
2558                 .on(this::throwStorageException);
2559     }
2560
2561     private ComponentParametersView getFilterComponentWithCapProperties() {
2562         ComponentParametersView filter = new ComponentParametersView();
2563         filter.setIgnoreCapabiltyProperties(false);
2564         return filter;
2565     }
2566
2567     private ToscaElement throwStorageException(StorageOperationStatus status) {
2568         throw new StorageException(status);
2569     }
2570
2571     public Either<Boolean, StorageOperationStatus> isComponentInUse(String componentId) {
2572         final List<EdgeLabelEnum> forbiddenEdgeLabelEnums = Arrays.asList(EdgeLabelEnum.INSTANCE_OF, EdgeLabelEnum.PROXY_OF, EdgeLabelEnum.ALLOTTED_OF);
2573         Either<GraphVertex, TitanOperationStatus> vertexById = titanDao.getVertexById(componentId);
2574         if (vertexById.isLeft()) {
2575             for (EdgeLabelEnum edgeLabelEnum : forbiddenEdgeLabelEnums) {
2576                 Iterator<Edge> edgeItr = vertexById.left().value().getVertex().edges(Direction.IN, edgeLabelEnum.name());
2577                 if(edgeItr != null && edgeItr.hasNext()){
2578                     return Either.left(true);
2579                 }
2580             }
2581         }
2582         return Either.left(false);
2583     }
2584
2585         public Either<List<Component>, StorageOperationStatus> getComponentListByInvariantUuid
2586                         (String componentInvariantUuid, Map<GraphPropertyEnum, Object> additionalPropertiesToMatch) {
2587
2588                 Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
2589                 if (MapUtils.isNotEmpty(additionalPropertiesToMatch)) {
2590                         propertiesToMatch.putAll(additionalPropertiesToMatch);
2591                 }
2592                 propertiesToMatch.put(GraphPropertyEnum.INVARIANT_UUID, componentInvariantUuid);
2593
2594                 Either<List<GraphVertex>, TitanOperationStatus> vertexEither = titanDao.getByCriteria(null, propertiesToMatch, JsonParseFlagEnum.ParseMetadata);
2595
2596                 if (vertexEither.isRight()) {
2597                         log.debug("Couldn't fetch metadata for component with type {} and invariantUUId {}, error: {}", componentInvariantUuid, vertexEither.right().value());
2598                         return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(vertexEither.right().value()));
2599                 }
2600                 List<GraphVertex> vertexList = vertexEither.isLeft() ? vertexEither.left().value() : null;
2601
2602                 if (vertexList == null || vertexList.isEmpty()) {
2603                         log.debug("Component with invariantUUId {} was not found", componentInvariantUuid);
2604                         return Either.right(StorageOperationStatus.NOT_FOUND);
2605                 }
2606
2607                 ArrayList<Component> components = new ArrayList<>();
2608                 for (GraphVertex vertex : vertexList) {
2609                         Either<Component, StorageOperationStatus> toscaElementByOperation = getToscaElementByOperation(vertex);
2610                         if (toscaElementByOperation.isRight()) {
2611                                 log.debug("Could not fetch the following Component by Invariant UUID {}", vertex.getUniqueId());
2612                                 return Either.right(toscaElementByOperation.right().value());
2613                         }
2614                         components.add(toscaElementByOperation.left().value());
2615                 }
2616
2617                 return Either.left(components);
2618         }
2619
2620     public Either<List<Component>, StorageOperationStatus> getParentComponents(String componentId) {
2621         List<Component> parentComponents = new ArrayList<>();
2622         final List<EdgeLabelEnum> relationEdgeLabelEnums = Arrays.asList(EdgeLabelEnum.INSTANCE_OF, EdgeLabelEnum.PROXY_OF);
2623         Either<GraphVertex, TitanOperationStatus> vertexById = titanDao.getVertexById(componentId);
2624         if (vertexById.isLeft()) {
2625             for (EdgeLabelEnum edgeLabelEnum : relationEdgeLabelEnums) {
2626                 Either<GraphVertex, TitanOperationStatus> parentVertexEither = titanDao.getParentVertex(vertexById.left().value(), edgeLabelEnum, JsonParseFlagEnum.ParseJson);
2627                 if(parentVertexEither.isLeft()){
2628                     Either<Component, StorageOperationStatus> componentEither = getToscaElement(parentVertexEither.left().value().getUniqueId());
2629                     if(componentEither.isLeft()){
2630                         parentComponents.add(componentEither.left().value());
2631                     }
2632                 }
2633             }
2634         }
2635         return Either.left(parentComponents);
2636     }
2637     public void updateCapReqOwnerId(String componentId) {
2638         topologyTemplateOperation
2639                 .updateCapReqOwnerId(componentId, getTopologyTemplate(componentId));
2640     }
2641 }