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