Add lombok support to simple classes
[sdc.git] / catalog-model / src / main / java / org / openecomp / sdc / be / model / jsontitan / operations / ToscaElementOperation.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.be.model.jsontitan.operations;
22
23 import com.google.gson.Gson;
24 import com.google.gson.reflect.TypeToken;
25 import fj.data.Either;
26 import org.apache.commons.collections.CollectionUtils;
27 import org.apache.tinkerpop.gremlin.structure.Direction;
28 import org.apache.tinkerpop.gremlin.structure.Edge;
29 import org.apache.tinkerpop.gremlin.structure.Vertex;
30 import org.apache.tinkerpop.gremlin.structure.VertexProperty;
31 import org.openecomp.sdc.be.config.ConfigurationManager;
32 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
33 import org.openecomp.sdc.be.dao.jsongraph.types.EdgeLabelEnum;
34 import org.openecomp.sdc.be.dao.jsongraph.types.EdgePropertyEnum;
35 import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum;
36 import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum;
37 import org.openecomp.sdc.be.dao.jsongraph.utils.JsonParserUtils;
38 import org.openecomp.sdc.be.dao.neo4j.GraphPropertiesDictionary;
39 import org.openecomp.sdc.be.dao.titan.TitanOperationStatus;
40 import org.openecomp.sdc.be.datatypes.elements.AdditionalInfoParameterDataDefinition;
41 import org.openecomp.sdc.be.datatypes.elements.ArtifactDataDefinition;
42 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
43 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
44 import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum;
45 import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
46 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
47 import org.openecomp.sdc.be.datatypes.tosca.ToscaDataDefinition;
48 import org.openecomp.sdc.be.model.ComponentParametersView;
49 import org.openecomp.sdc.be.model.LifecycleStateEnum;
50 import org.openecomp.sdc.be.model.catalog.CatalogComponent;
51 import org.openecomp.sdc.be.model.category.CategoryDefinition;
52 import org.openecomp.sdc.be.model.category.SubCategoryDefinition;
53 import org.openecomp.sdc.be.model.jsontitan.datamodel.NodeType;
54 import org.openecomp.sdc.be.model.jsontitan.datamodel.TopologyTemplate;
55 import org.openecomp.sdc.be.model.jsontitan.datamodel.ToscaElement;
56 import org.openecomp.sdc.be.model.jsontitan.datamodel.ToscaElementTypeEnum;
57 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
58 import org.openecomp.sdc.be.model.operations.impl.DaoStatusConverter;
59 import org.openecomp.sdc.be.model.operations.impl.UniqueIdBuilder;
60 import org.openecomp.sdc.common.jsongraph.util.CommonUtility;
61 import org.openecomp.sdc.common.jsongraph.util.CommonUtility.LogLevelEnum;
62 import org.openecomp.sdc.common.log.wrappers.Logger;
63 import org.openecomp.sdc.common.util.ValidationUtils;
64 import org.springframework.beans.factory.annotation.Autowired;
65 import org.springframework.util.StopWatch;
66
67 import java.lang.reflect.Type;
68 import java.util.*;
69 import java.util.Map.Entry;
70 import java.util.stream.Collectors;
71 import java.util.Iterator;
72 import java.util.List;
73 import java.util.Map;
74 import java.util.Set;
75
76 public abstract class ToscaElementOperation extends BaseOperation {
77     private static final String FAILED_TO_FETCH_FOR_TOSCA_ELEMENT_WITH_ID_ERROR = "failed to fetch {} for tosca element with id {}, error {}";
78
79     private static final String CANNOT_FIND_USER_IN_THE_GRAPH_STATUS_IS = "Cannot find user {} in the graph. status is {}";
80
81     private static final String FAILED_TO_CREATE_EDGE_WITH_LABEL_FROM_USER_VERTEX_TO_TOSCA_ELEMENT_VERTEX_ON_GRAPH_STATUS_IS = "Failed to create edge with label {} from user vertex {} to tosca element vertex {} on graph. Status is {}. ";
82
83     private static Logger log = Logger.getLogger(ToscaElementOperation.class.getName());
84
85     private static final Gson gson = new Gson();
86
87     @Autowired
88     protected CategoryOperation categoryOperation;
89
90     protected Gson getGson() {
91         return gson;
92     }
93
94
95     protected Either<GraphVertex, StorageOperationStatus> getComponentByLabelAndId(String uniqueId, ToscaElementTypeEnum nodeType, JsonParseFlagEnum parseFlag) {
96
97         Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
98         propertiesToMatch.put(GraphPropertyEnum.UNIQUE_ID, uniqueId);
99
100         VertexTypeEnum vertexType = ToscaElementTypeEnum.getVertexTypeByToscaType(nodeType);
101         Either<List<GraphVertex>, TitanOperationStatus> getResponse = titanDao.getByCriteria(vertexType, propertiesToMatch, parseFlag);
102         if (getResponse.isRight()) {
103             log.debug("Couldn't fetch component with type {} and unique id {}, error: {}", vertexType, uniqueId, getResponse.right().value());
104             return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(getResponse.right().value()));
105
106         }
107         List<GraphVertex> componentList = getResponse.left().value();
108         if (componentList.isEmpty()) {
109             log.debug("Component with type {} and unique id {} was not found", vertexType, uniqueId);
110             return Either.right(StorageOperationStatus.NOT_FOUND);
111         }
112         GraphVertex vertexG = componentList.get(0);
113         return Either.left(vertexG);
114     }
115
116     public Either<ToscaElement, StorageOperationStatus> getToscaElement(String uniqueId) {
117         return getToscaElement(uniqueId, new ComponentParametersView());
118     }
119
120     public Either<GraphVertex, StorageOperationStatus> markComponentToDelete(GraphVertex componentToDelete) {
121         Either<GraphVertex, StorageOperationStatus> result = null;
122
123         Boolean isDeleted = (Boolean) componentToDelete.getMetadataProperty(GraphPropertyEnum.IS_DELETED);
124         if (isDeleted != null && isDeleted && !(Boolean) componentToDelete.getMetadataProperty(GraphPropertyEnum.IS_HIGHEST_VERSION)) {
125             // component already marked for delete
126             result = Either.left(componentToDelete);
127             return result;
128         } else {
129
130             componentToDelete.addMetadataProperty(GraphPropertyEnum.IS_DELETED, Boolean.TRUE);
131             componentToDelete.setJsonMetadataField(JsonPresentationFields.LAST_UPDATE_DATE, System.currentTimeMillis());
132
133             Either<GraphVertex, TitanOperationStatus> updateNode = titanDao.updateVertex(componentToDelete);
134
135             StorageOperationStatus updateComponent;
136             if (updateNode.isRight()) {
137                 log.debug("Failed to update component {}. status is {}", componentToDelete.getUniqueId(), updateNode.right().value());
138                 updateComponent = DaoStatusConverter.convertTitanStatusToStorageStatus(updateNode.right().value());
139                 result = Either.right(updateComponent);
140                 return result;
141             }
142
143             result = Either.left(componentToDelete);
144             return result;
145         }
146     }
147
148     /**
149      * Performs a shadow clone of previousToscaElement
150      *
151      * @param previousToscaElement
152      * @param nextToscaElement
153      * @param user
154      * @return
155      */
156     public Either<GraphVertex, StorageOperationStatus> cloneToscaElement(GraphVertex previousToscaElement, GraphVertex nextToscaElement, GraphVertex user) {
157
158         Either<GraphVertex, StorageOperationStatus> result = null;
159         GraphVertex createdToscaElementVertex = null;
160         TitanOperationStatus status;
161
162         Either<GraphVertex, TitanOperationStatus> createNextVersionRes = titanDao.createVertex(nextToscaElement);
163         if (createNextVersionRes.isRight()) {
164             status = createNextVersionRes.right().value();
165             CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to create tosca element vertex {} with version {} on graph. Status is {}. ", previousToscaElement.getMetadataProperty(GraphPropertyEnum.NORMALIZED_NAME),
166                     previousToscaElement.getMetadataProperty(GraphPropertyEnum.VERSION), status);
167             result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
168         }
169         if (result == null) {
170             createdToscaElementVertex = createNextVersionRes.left().value();
171             Map<EdgePropertyEnum, Object> properties = new HashMap<>();
172             properties.put(EdgePropertyEnum.STATE, createdToscaElementVertex.getMetadataProperty(GraphPropertyEnum.STATE));
173             status = titanDao.createEdge(user.getVertex(), createdToscaElementVertex.getVertex(), EdgeLabelEnum.STATE, properties);
174             if (status != TitanOperationStatus.OK) {
175                 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, FAILED_TO_CREATE_EDGE_WITH_LABEL_FROM_USER_VERTEX_TO_TOSCA_ELEMENT_VERTEX_ON_GRAPH_STATUS_IS, EdgeLabelEnum.STATE, user.getUniqueId(),
176                         previousToscaElement.getMetadataProperty(GraphPropertyEnum.NORMALIZED_NAME), status);
177                 result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
178             }
179         }
180         if (result == null) {
181             status = titanDao.createEdge(user.getVertex(), createdToscaElementVertex.getVertex(), EdgeLabelEnum.LAST_MODIFIER, new HashMap<>());
182             if (status != TitanOperationStatus.OK) {
183                 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, FAILED_TO_CREATE_EDGE_WITH_LABEL_FROM_USER_VERTEX_TO_TOSCA_ELEMENT_VERTEX_ON_GRAPH_STATUS_IS, EdgeLabelEnum.LAST_MODIFIER, user.getUniqueId(),
184                         nextToscaElement.getMetadataProperty(GraphPropertyEnum.NORMALIZED_NAME), status);
185                 result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
186             }
187         }
188         if (result == null) {
189             status = titanDao.createEdge(user.getVertex(), createdToscaElementVertex.getVertex(), EdgeLabelEnum.CREATOR, new HashMap<>());
190             if (status != TitanOperationStatus.OK) {
191                 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, FAILED_TO_CREATE_EDGE_WITH_LABEL_FROM_USER_VERTEX_TO_TOSCA_ELEMENT_VERTEX_ON_GRAPH_STATUS_IS, EdgeLabelEnum.CREATOR, user.getUniqueId(),
192                         nextToscaElement.getMetadataProperty(GraphPropertyEnum.NORMALIZED_NAME), status);
193                 result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
194             }
195         }
196         if (result == null) {
197             Iterator<Edge> edgesToCopyIter = previousToscaElement.getVertex().edges(Direction.OUT);
198             while (edgesToCopyIter.hasNext()) {
199                 Edge currEdge = edgesToCopyIter.next();
200                 Vertex currVertex = currEdge.inVertex();
201                 status = titanDao.createEdge(createdToscaElementVertex.getVertex(), currVertex, EdgeLabelEnum.getEdgeLabelEnum(currEdge.label()), currEdge);
202                 if (status != TitanOperationStatus.OK) {
203                     CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to create edge with label {} from tosca element vertex {} to vertex with label {} on graph. Status is {}. ", currEdge.label(), createdToscaElementVertex.getUniqueId(),
204                             currVertex.property(GraphPropertyEnum.LABEL.getProperty()), status);
205                     result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
206                     break;
207                 }
208             }
209         }
210
211         if (result == null) {
212             result = Either.left(createdToscaElementVertex);
213         } else {
214             CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to clone tosca element {} with the name {}. ", previousToscaElement.getUniqueId(), previousToscaElement.getMetadataProperty(GraphPropertyEnum.NORMALIZED_NAME));
215         }
216         return result;
217     }
218
219     protected TitanOperationStatus setLastModifierFromGraph(GraphVertex componentV, ToscaElement toscaElement) {
220         Either<GraphVertex, TitanOperationStatus> parentVertex = titanDao.getParentVertex(componentV, EdgeLabelEnum.LAST_MODIFIER, JsonParseFlagEnum.NoParse);
221         if (parentVertex.isRight()) {
222             log.debug("Failed to fetch last modifier for tosca element with id {} error {}", componentV.getUniqueId(), parentVertex.right().value());
223             return parentVertex.right().value();
224         }
225         GraphVertex userV = parentVertex.left().value();
226         String userId = (String) userV.getMetadataProperty(GraphPropertyEnum.USERID);
227         toscaElement.setLastUpdaterUserId(userId);
228         toscaElement.setLastUpdaterFullName(buildFullName(userV));
229         return TitanOperationStatus.OK;
230     }
231
232     public String buildFullName(GraphVertex userV) {
233
234         String fullName = (String) userV.getMetadataProperty(GraphPropertyEnum.FIRST_NAME);
235         if (fullName == null) {
236             fullName = "";
237         } else {
238             fullName = fullName + " ";
239         }
240         String lastName = (String) userV.getMetadataProperty(GraphPropertyEnum.LAST_NAME);
241         if (lastName != null) {
242             fullName += lastName;
243         }
244         return fullName;
245     }
246
247     protected TitanOperationStatus setCreatorFromGraph(GraphVertex componentV, ToscaElement toscaElement) {
248         Either<GraphVertex, TitanOperationStatus> parentVertex = titanDao.getParentVertex(componentV, EdgeLabelEnum.CREATOR, JsonParseFlagEnum.NoParse);
249         if (parentVertex.isRight()) {
250             log.debug("Failed to fetch creator for tosca element with id {} error {}", componentV.getUniqueId(), parentVertex.right().value());
251             return parentVertex.right().value();
252         }
253         GraphVertex userV = parentVertex.left().value();
254         String creatorUserId = (String) userV.getMetadataProperty(GraphPropertyEnum.USERID);
255         toscaElement.setCreatorUserId(creatorUserId);
256         toscaElement.setCreatorFullName(buildFullName(userV));
257
258         return TitanOperationStatus.OK;
259     }
260
261     protected <T extends ToscaElement> T getResourceMetaDataFromResource(T toscaElement) {
262         if (toscaElement.getNormalizedName() == null || toscaElement.getNormalizedName().isEmpty()) {
263             toscaElement.setNormalizedName(ValidationUtils.normaliseComponentName(toscaElement.getName()));
264         }
265         if (toscaElement.getSystemName() == null || toscaElement.getSystemName().isEmpty()) {
266             toscaElement.setSystemName(ValidationUtils.convertToSystemName(toscaElement.getName()));
267         }
268
269         LifecycleStateEnum lifecycleStateEnum = toscaElement.getLifecycleState();
270         if (lifecycleStateEnum == null) {
271             toscaElement.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
272         }
273         long currentDate = System.currentTimeMillis();
274         if (toscaElement.getCreationDate() == null) {
275             toscaElement.setCreationDate(currentDate);
276         }
277         toscaElement.setLastUpdateDate(currentDate);
278
279         return toscaElement;
280     }
281
282     protected void fillCommonMetadata(GraphVertex nodeTypeVertex, ToscaElement toscaElement) {
283         if (toscaElement.isHighestVersion() == null) {
284             toscaElement.setHighestVersion(true);
285         }
286         nodeTypeVertex.addMetadataProperty(GraphPropertyEnum.IS_DELETED, toscaElement.getMetadataValue(JsonPresentationFields.IS_DELETED));
287         nodeTypeVertex.addMetadataProperty(GraphPropertyEnum.IS_HIGHEST_VERSION, toscaElement.getMetadataValueOrDefault(JsonPresentationFields.HIGHEST_VERSION, Boolean.TRUE));
288         nodeTypeVertex.addMetadataProperty(GraphPropertyEnum.STATE, toscaElement.getMetadataValue(JsonPresentationFields.LIFECYCLE_STATE));
289         nodeTypeVertex.addMetadataProperty(GraphPropertyEnum.RESOURCE_TYPE, toscaElement.getMetadataValue(JsonPresentationFields.RESOURCE_TYPE));
290         nodeTypeVertex.addMetadataProperty(GraphPropertyEnum.VERSION, toscaElement.getMetadataValue(JsonPresentationFields.VERSION));
291         nodeTypeVertex.addMetadataProperty(GraphPropertyEnum.NORMALIZED_NAME, toscaElement.getMetadataValue(JsonPresentationFields.NORMALIZED_NAME));
292         nodeTypeVertex.addMetadataProperty(GraphPropertyEnum.UNIQUE_ID, toscaElement.getMetadataValue(JsonPresentationFields.UNIQUE_ID));
293         nodeTypeVertex.addMetadataProperty(GraphPropertyEnum.TOSCA_RESOURCE_NAME, toscaElement.getMetadataValue(JsonPresentationFields.TOSCA_RESOURCE_NAME));
294         nodeTypeVertex.addMetadataProperty(GraphPropertyEnum.UUID, toscaElement.getMetadataValue(JsonPresentationFields.UUID));
295         nodeTypeVertex.addMetadataProperty(GraphPropertyEnum.IS_ABSTRACT, toscaElement.getMetadataValue(JsonPresentationFields.IS_ABSTRACT));
296         nodeTypeVertex.addMetadataProperty(GraphPropertyEnum.INVARIANT_UUID, toscaElement.getMetadataValue(JsonPresentationFields.INVARIANT_UUID));
297         nodeTypeVertex.addMetadataProperty(GraphPropertyEnum.NAME, toscaElement.getMetadataValue(JsonPresentationFields.NAME));
298         nodeTypeVertex.addMetadataProperty(GraphPropertyEnum.SYSTEM_NAME, toscaElement.getMetadataValue(JsonPresentationFields.SYSTEM_NAME));
299         nodeTypeVertex.addMetadataProperty(GraphPropertyEnum.IS_ARCHIVED, toscaElement.getMetadataValue(JsonPresentationFields.IS_ARCHIVED));
300         nodeTypeVertex.addMetadataProperty(GraphPropertyEnum.ARCHIVE_TIME, toscaElement.getMetadataValue(JsonPresentationFields.ARCHIVE_TIME));
301         nodeTypeVertex.addMetadataProperty(GraphPropertyEnum.IS_VSP_ARCHIVED, toscaElement.getMetadataValue(JsonPresentationFields.IS_VSP_ARCHIVED));
302         toscaElement.getMetadata().entrySet().stream().filter(e -> e.getValue() != null).forEach(e -> nodeTypeVertex.setJsonMetadataField(JsonPresentationFields.getByPresentation(e.getKey()), e.getValue()));
303
304         nodeTypeVertex.setUniqueId(toscaElement.getUniqueId());
305         nodeTypeVertex.setType(toscaElement.getComponentType());
306
307     }
308
309     protected StorageOperationStatus assosiateToUsers(GraphVertex nodeTypeVertex, ToscaElement toscaElement) {
310         // handle user
311         String userId = toscaElement.getCreatorUserId();
312
313         Either<GraphVertex, TitanOperationStatus> findUser = findUserVertex(userId);
314
315         if (findUser.isRight()) {
316             TitanOperationStatus status = findUser.right().value();
317             log.error(CANNOT_FIND_USER_IN_THE_GRAPH_STATUS_IS, userId, status);
318             return DaoStatusConverter.convertTitanStatusToStorageStatus(status);
319
320         }
321         GraphVertex creatorVertex = findUser.left().value();
322         GraphVertex updaterVertex = creatorVertex;
323         String updaterId = toscaElement.getLastUpdaterUserId();
324         if (updaterId != null && !updaterId.equals(userId)) {
325             findUser = findUserVertex(updaterId);
326             if (findUser.isRight()) {
327                 TitanOperationStatus status = findUser.right().value();
328                 log.error(CANNOT_FIND_USER_IN_THE_GRAPH_STATUS_IS, userId, status);
329                 return DaoStatusConverter.convertTitanStatusToStorageStatus(status);
330             } else {
331                 updaterVertex = findUser.left().value();
332             }
333         }
334         Map<EdgePropertyEnum, Object> props = new EnumMap<>(EdgePropertyEnum.class);
335         props.put(EdgePropertyEnum.STATE, (String) toscaElement.getMetadataValue(JsonPresentationFields.LIFECYCLE_STATE));
336
337         TitanOperationStatus result = titanDao.createEdge(updaterVertex, nodeTypeVertex, EdgeLabelEnum.STATE, props);
338         log.debug("After associating user {} to resource {}. Edge type is {}", updaterVertex, nodeTypeVertex.getUniqueId(), EdgeLabelEnum.STATE);
339         if (TitanOperationStatus.OK != result) {
340             return DaoStatusConverter.convertTitanStatusToStorageStatus(result);
341         }
342         result = titanDao.createEdge(updaterVertex, nodeTypeVertex, EdgeLabelEnum.LAST_MODIFIER, null);
343         log.debug("After associating user {}  to resource {}. Edge type is {}", updaterVertex, nodeTypeVertex.getUniqueId(), EdgeLabelEnum.LAST_MODIFIER);
344         if (!result.equals(TitanOperationStatus.OK)) {
345             log.error("Failed to associate user {}  to resource {}. Edge type is {}", updaterVertex, nodeTypeVertex.getUniqueId(), EdgeLabelEnum.LAST_MODIFIER);
346             return DaoStatusConverter.convertTitanStatusToStorageStatus(result);
347         }
348
349         toscaElement.setLastUpdaterUserId(toscaElement.getCreatorUserId());
350         toscaElement.setLastUpdaterFullName(toscaElement.getCreatorFullName());
351
352         result = titanDao.createEdge(creatorVertex, nodeTypeVertex, EdgeLabelEnum.CREATOR, null);
353         log.debug("After associating user {} to resource {}. Edge type is {} ", creatorVertex, nodeTypeVertex.getUniqueId(), EdgeLabelEnum.CREATOR);
354         if (!result.equals(TitanOperationStatus.OK)) {
355             log.error("Failed to associate user {} to resource {}. Edge type is {} ", creatorVertex, nodeTypeVertex.getUniqueId(), EdgeLabelEnum.CREATOR);
356             return DaoStatusConverter.convertTitanStatusToStorageStatus(result);
357         }
358         return StorageOperationStatus.OK;
359     }
360
361     protected StorageOperationStatus assosiateResourceMetadataToCategory(GraphVertex nodeTypeVertex, ToscaElement nodeType) {
362         String subcategoryName = nodeType.getCategories().get(0).getSubcategories().get(0).getName();
363         String categoryName = nodeType.getCategories().get(0).getName();
364         Either<GraphVertex, StorageOperationStatus> getCategoryVertex = getResourceCategoryVertex(nodeType.getUniqueId(), subcategoryName, categoryName);
365
366         if (getCategoryVertex.isRight()) {
367             return getCategoryVertex.right().value();
368         }
369
370         GraphVertex subCategoryV = getCategoryVertex.left().value();
371
372         TitanOperationStatus createEdge = titanDao.createEdge(nodeTypeVertex, subCategoryV, EdgeLabelEnum.CATEGORY, new HashMap<>());
373         if (createEdge != TitanOperationStatus.OK) {
374             log.trace("Failed to associate resource {} to category {} with id {}", nodeType.getUniqueId(), subcategoryName, subCategoryV.getUniqueId());
375             return DaoStatusConverter.convertTitanStatusToStorageStatus(createEdge);
376         }
377         return StorageOperationStatus.OK;
378     }
379
380     protected Either<GraphVertex, StorageOperationStatus> getResourceCategoryVertex(String elementId, String subcategoryName, String categoryName) {
381         Either<GraphVertex, StorageOperationStatus> category = categoryOperation.getCategory(categoryName, VertexTypeEnum.RESOURCE_CATEGORY);
382         if (category.isRight()) {
383             log.trace("Failed to fetch category {} for resource {} error {}", categoryName, elementId, category.right().value());
384             return Either.right(category.right().value());
385         }
386         GraphVertex categoryV = category.left().value();
387
388         if (subcategoryName != null) {
389             Either<GraphVertex, StorageOperationStatus> subCategory = categoryOperation.getSubCategoryForCategory(categoryV, subcategoryName);
390             if (subCategory.isRight()) {
391                 log.trace("Failed to fetch subcategory {} of category for resource {} error {}", subcategoryName, categoryName, elementId, subCategory.right().value());
392                 return Either.right(subCategory.right().value());
393             }
394
395             GraphVertex subCategoryV = subCategory.left().value();
396             return Either.left(subCategoryV);
397         }
398         return Either.left(categoryV);
399     }
400
401     private StorageOperationStatus associateArtifactsToResource(GraphVertex nodeTypeVertex, ToscaElement toscaElement) {
402         Map<String, ArtifactDataDefinition> artifacts = toscaElement.getArtifacts();
403         Either<GraphVertex, StorageOperationStatus> status;
404         if (artifacts != null) {
405             artifacts.values().stream().filter(a -> a.getUniqueId() == null).forEach(a -> {
406                 String uniqueId = UniqueIdBuilder.buildPropertyUniqueId(nodeTypeVertex.getUniqueId().toLowerCase(), a.getArtifactLabel().toLowerCase());
407                 a.setUniqueId(uniqueId);
408             });
409             status = associateElementToData(nodeTypeVertex, VertexTypeEnum.ARTIFACTS, EdgeLabelEnum.ARTIFACTS, artifacts);
410             if (status.isRight()) {
411                 return status.right().value();
412             }
413         }
414         Map<String, ArtifactDataDefinition> toscaArtifacts = toscaElement.getToscaArtifacts();
415         if (toscaArtifacts != null) {
416             toscaArtifacts.values().stream().filter(a -> a.getUniqueId() == null).forEach(a -> {
417                 String uniqueId = UniqueIdBuilder.buildPropertyUniqueId(nodeTypeVertex.getUniqueId().toLowerCase(), a.getArtifactLabel().toLowerCase());
418                 a.setUniqueId(uniqueId);
419             });
420             status = associateElementToData(nodeTypeVertex, VertexTypeEnum.TOSCA_ARTIFACTS, EdgeLabelEnum.TOSCA_ARTIFACTS, toscaArtifacts);
421             if (status.isRight()) {
422                 return status.right().value();
423             }
424         }
425         Map<String, ArtifactDataDefinition> deploymentArtifacts = toscaElement.getDeploymentArtifacts();
426         if (deploymentArtifacts != null) {
427             deploymentArtifacts.values().stream().filter(a -> a.getUniqueId() == null).forEach(a -> {
428                 String uniqueId = UniqueIdBuilder.buildPropertyUniqueId(nodeTypeVertex.getUniqueId().toLowerCase(), a.getArtifactLabel().toLowerCase());
429                 a.setUniqueId(uniqueId);
430             });
431             status = associateElementToData(nodeTypeVertex, VertexTypeEnum.DEPLOYMENT_ARTIFACTS, EdgeLabelEnum.DEPLOYMENT_ARTIFACTS, deploymentArtifacts);
432             if (status.isRight()) {
433                 return status.right().value();
434             }
435         }
436         return StorageOperationStatus.OK;
437     }
438
439     protected TitanOperationStatus disassociateAndDeleteCommonElements(GraphVertex toscaElementVertex) {
440         TitanOperationStatus status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.ARTIFACTS);
441         if (status != TitanOperationStatus.OK) {
442             log.debug("Failed to disaccociate artifact for {} error {}", toscaElementVertex.getUniqueId(), status);
443             return status;
444         }
445         status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.TOSCA_ARTIFACTS);
446         if (status != TitanOperationStatus.OK) {
447             log.debug("Failed to disaccociate tosca artifact for {} error {}", toscaElementVertex.getUniqueId(), status);
448             return status;
449         }
450         status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.DEPLOYMENT_ARTIFACTS);
451         if (status != TitanOperationStatus.OK) {
452             log.debug("Failed to deployment artifact for {} error {}", toscaElementVertex.getUniqueId(), status);
453             return status;
454         }
455         status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.PROPERTIES);
456         if (status != TitanOperationStatus.OK) {
457             log.debug("Failed to disaccociate properties for {} error {}", toscaElementVertex.getUniqueId(), status);
458             return status;
459         }
460         status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.ATTRIBUTES);
461         if (status != TitanOperationStatus.OK) {
462             log.debug("Failed to disaccociate attributes for {} error {}", toscaElementVertex.getUniqueId(), status);
463             return status;
464         }
465         status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.ADDITIONAL_INFORMATION);
466         if (status != TitanOperationStatus.OK) {
467             log.debug("Failed to disaccociate additional information for {} error {}", toscaElementVertex.getUniqueId(), status);
468             return status;
469         }
470         status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.CAPABILITIES);
471         if (status != TitanOperationStatus.OK) {
472             log.debug("Failed to disaccociate capabilities for {} error {}", toscaElementVertex.getUniqueId(), status);
473             return status;
474         }
475         status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.REQUIREMENTS);
476         if (status != TitanOperationStatus.OK) {
477             log.debug("Failed to disaccociate requirements for {} error {}", toscaElementVertex.getUniqueId(), status);
478             return status;
479         }
480         status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.FORWARDING_PATH);
481         if (status != TitanOperationStatus.OK) {
482             log.debug("Failed to disaccociate requirements for {} error {}", toscaElementVertex.getUniqueId(), status);
483             return status;
484         }
485         return TitanOperationStatus.OK;
486     }
487
488     protected StorageOperationStatus assosiateCommonForToscaElement(GraphVertex nodeTypeVertex, ToscaElement toscaElement, List<GraphVertex> derivedResources) {
489
490         StorageOperationStatus associateUsers = assosiateToUsers(nodeTypeVertex, toscaElement);
491         if (associateUsers != StorageOperationStatus.OK) {
492             return associateUsers;
493         }
494         StorageOperationStatus associateArtifacts = associateArtifactsToResource(nodeTypeVertex, toscaElement);
495         if (associateArtifacts != StorageOperationStatus.OK) {
496             return associateArtifacts;
497         }
498         StorageOperationStatus associateProperties = associatePropertiesToResource(nodeTypeVertex, toscaElement, derivedResources);
499         if (associateProperties != StorageOperationStatus.OK) {
500             return associateProperties;
501         }
502         StorageOperationStatus associateAdditionaInfo = associateAdditionalInfoToResource(nodeTypeVertex, toscaElement);
503         if (associateAdditionaInfo != StorageOperationStatus.OK) {
504             return associateAdditionaInfo;
505         }
506         if (needConnectToCatalog(toscaElement)) {
507             StorageOperationStatus associateToCatalog = associateToCatalogRoot(nodeTypeVertex);
508             if (associateToCatalog != StorageOperationStatus.OK) {
509                 return associateToCatalog;
510             }
511         }
512         return StorageOperationStatus.OK;
513     }
514
515     private boolean needConnectToCatalog(ToscaElement toscaElement) {
516         Boolean isAbstract = (Boolean) toscaElement.getMetadataValue(JsonPresentationFields.IS_ABSTRACT);
517         if (isAbstract != null && isAbstract) {
518             return false;
519         }
520         return toscaElement.isHighestVersion();
521     }
522
523     private StorageOperationStatus associateToCatalogRoot(GraphVertex nodeTypeVertex) {
524         Either<GraphVertex, TitanOperationStatus> catalog = titanDao.getVertexByLabel(VertexTypeEnum.CATALOG_ROOT);
525         if (catalog.isRight()) {
526             log.debug("Failed to fetch catalog vertex. error {}", catalog.right().value());
527             return DaoStatusConverter.convertTitanStatusToStorageStatus(catalog.right().value());
528         }
529         TitanOperationStatus createEdge = titanDao.createEdge(catalog.left().value(), nodeTypeVertex, EdgeLabelEnum.CATALOG_ELEMENT, null);
530
531         return DaoStatusConverter.convertTitanStatusToStorageStatus(createEdge);
532     }
533
534     protected StorageOperationStatus associatePropertiesToResource(GraphVertex nodeTypeVertex, ToscaElement nodeType, List<GraphVertex> derivedResources) {
535         // Note : currently only one derived supported!!!!
536         Either<Map<String, PropertyDataDefinition>, StorageOperationStatus> dataFromDerived = getDataFromDerived(derivedResources, EdgeLabelEnum.PROPERTIES);
537         if (dataFromDerived.isRight()) {
538             return dataFromDerived.right().value();
539         }
540         Map<String, PropertyDataDefinition> propertiesAll = dataFromDerived.left().value();
541
542         Map<String, PropertyDataDefinition> properties = nodeType.getProperties();
543
544         if (properties != null) {
545             properties.values().stream().filter(p -> p.getUniqueId() == null).forEach(p -> {
546                 String uid = UniqueIdBuilder.buildPropertyUniqueId(nodeTypeVertex.getUniqueId(), p.getName());
547                 p.setUniqueId(uid);
548             });
549
550             Either<Map<String, PropertyDataDefinition>, String> eitherMerged = ToscaDataDefinition.mergeDataMaps(propertiesAll, properties);
551             if (eitherMerged.isRight()) {
552                 // TODO re-factor error handling - moving BL to operation resulted in loss of info about the invalid property
553                 log.debug("property {} cannot be overriden", eitherMerged.right().value());
554                 return StorageOperationStatus.INVALID_PROPERTY;
555             }
556         }
557         if (!propertiesAll.isEmpty()) {
558             Either<GraphVertex, StorageOperationStatus> assosiateElementToData = associateElementToData(nodeTypeVertex, VertexTypeEnum.PROPERTIES, EdgeLabelEnum.PROPERTIES, propertiesAll);
559             if (assosiateElementToData.isRight()) {
560                 return assosiateElementToData.right().value();
561             }
562         }
563         return StorageOperationStatus.OK;
564     }
565
566     private StorageOperationStatus associateAdditionalInfoToResource(GraphVertex nodeTypeVertex, ToscaElement nodeType) {
567         Map<String, AdditionalInfoParameterDataDefinition> additionalInformation = nodeType.getAdditionalInformation();
568         if (additionalInformation != null) {
569             Either<GraphVertex, StorageOperationStatus> assosiateElementToData = associateElementToData(nodeTypeVertex, VertexTypeEnum.ADDITIONAL_INFORMATION, EdgeLabelEnum.ADDITIONAL_INFORMATION, additionalInformation);
570             if (assosiateElementToData.isRight()) {
571                 return assosiateElementToData.right().value();
572             }
573         }
574         return StorageOperationStatus.OK;
575     }
576
577     protected <T extends ToscaDataDefinition> Either<Map<String, T>, StorageOperationStatus> getDataFromDerived(List<GraphVertex> derivedResources, EdgeLabelEnum edge) {
578         Map<String, T> propertiesAll = new HashMap<>();
579
580         if (derivedResources != null && !derivedResources.isEmpty()) {
581             for (GraphVertex derived : derivedResources) {
582                 Either<List<GraphVertex>, TitanOperationStatus> derivedProperties = titanDao.getChildrenVertecies(derived, edge, JsonParseFlagEnum.ParseJson);
583                 if (derivedProperties.isRight()) {
584                     if (derivedProperties.right().value() != TitanOperationStatus.NOT_FOUND) {
585                         log.debug("Failed to get properties for derived from {} error {}", derived.getUniqueId(), derivedProperties.right().value());
586                         return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(derivedProperties.right().value()));
587                     } else {
588                         continue;
589                     }
590                 }
591                 List<GraphVertex> propList = derivedProperties.left().value();
592                 for (GraphVertex propV : propList) {
593                     Map<String, T> propertiesFromDerived = (Map<String, T>) propV.getJson();
594                     if (propertiesFromDerived != null) {
595                         propertiesFromDerived.entrySet().forEach(x -> x.getValue().setOwnerIdIfEmpty(derived.getUniqueId()));
596                         propertiesAll.putAll(propertiesFromDerived);
597                     }
598                 }
599             }
600         }
601         return Either.left(propertiesAll);
602     }
603
604     protected TitanOperationStatus setArtifactsFromGraph(GraphVertex componentV, ToscaElement toscaElement) {
605         Either<Map<String, ArtifactDataDefinition>, TitanOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.ARTIFACTS);
606         if (result.isLeft()) {
607             toscaElement.setArtifacts(result.left().value());
608         } else {
609             if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
610                 return result.right().value();
611             }
612         }
613         result = getDataFromGraph(componentV, EdgeLabelEnum.DEPLOYMENT_ARTIFACTS);
614         if (result.isLeft()) {
615             toscaElement.setDeploymentArtifacts(result.left().value());
616         } else {
617             if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
618                 return result.right().value();
619             }
620         }
621         result = getDataFromGraph(componentV, EdgeLabelEnum.TOSCA_ARTIFACTS);
622         if (result.isLeft()) {
623             toscaElement.setToscaArtifacts(result.left().value());
624         } else {
625             if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
626                 return result.right().value();
627             }
628         }
629         return TitanOperationStatus.OK;
630     }
631
632     protected TitanOperationStatus setAllVersions(GraphVertex componentV, ToscaElement toscaElement) {
633         Map<String, String> allVersion = new HashMap<>();
634
635         allVersion.put((String) componentV.getMetadataProperty(GraphPropertyEnum.VERSION), componentV.getUniqueId());
636         ArrayList<GraphVertex> allChildrenAndParants = new ArrayList<>();
637         Either<GraphVertex, TitanOperationStatus> childResourceRes = titanDao.getChildVertex(componentV, EdgeLabelEnum.VERSION, JsonParseFlagEnum.NoParse);
638         while (childResourceRes.isLeft()) {
639             GraphVertex child = childResourceRes.left().value();
640             allChildrenAndParants.add(child);
641             childResourceRes = titanDao.getChildVertex(child, EdgeLabelEnum.VERSION, JsonParseFlagEnum.NoParse);
642         }
643         TitanOperationStatus operationStatus = childResourceRes.right().value();
644
645         if (operationStatus != TitanOperationStatus.NOT_FOUND) {
646             return operationStatus;
647         } else {
648             Either<GraphVertex, TitanOperationStatus> parentResourceRes = titanDao.getParentVertex(componentV, EdgeLabelEnum.VERSION, JsonParseFlagEnum.NoParse);
649             while (parentResourceRes.isLeft()) {
650                 GraphVertex parent = parentResourceRes.left().value();
651                 allChildrenAndParants.add(parent);
652                 parentResourceRes = titanDao.getParentVertex(parent, EdgeLabelEnum.VERSION, JsonParseFlagEnum.NoParse);
653             }
654             operationStatus = parentResourceRes.right().value();
655             if (operationStatus != TitanOperationStatus.NOT_FOUND) {
656                 return operationStatus;
657             } else {
658                 allChildrenAndParants.stream().filter(vertex -> {
659                     Boolean isDeleted = (Boolean) vertex.getMetadataProperty(GraphPropertyEnum.IS_DELETED);
660                     return (isDeleted == null || !isDeleted);
661                 }).forEach(vertex -> allVersion.put((String) vertex.getMetadataProperty(GraphPropertyEnum.VERSION), vertex.getUniqueId()));
662
663                 toscaElement.setAllVersions(allVersion);
664                 return TitanOperationStatus.OK;
665             }
666         }
667     }
668
669     protected <T extends ToscaElement> Either<List<T>, StorageOperationStatus> getFollowedComponent(String userId, Set<LifecycleStateEnum> lifecycleStates, Set<LifecycleStateEnum> lastStateStates, ComponentTypeEnum neededType) {
670
671         Either<List<T>, StorageOperationStatus> result = null;
672
673         Map<GraphPropertyEnum, Object> props = null;
674
675         if (userId != null) {
676             props = new EnumMap<>(GraphPropertyEnum.class);
677             // for Designer retrieve specific user
678             props.put(GraphPropertyEnum.USERID, userId);
679         }
680         // in case of user id == null -> get all users by label
681         // for Tester and Admin retrieve all users
682         Either<List<GraphVertex>, TitanOperationStatus> usersByCriteria = titanDao.getByCriteria(VertexTypeEnum.USER, props, JsonParseFlagEnum.NoParse);
683         if (usersByCriteria.isRight()) {
684             log.debug("Failed to fetch users by criteria {} error {}", props, usersByCriteria.right().value());
685             return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(usersByCriteria.right().value()));
686         }
687         List<GraphVertex> users = usersByCriteria.left().value();
688
689         List<T> components = new ArrayList<>();
690         List<T> componentsPerUser;
691         for (GraphVertex userV : users) {
692
693             HashSet<String> ids = new HashSet<>();
694             Either<List<GraphVertex>, TitanOperationStatus> childrenVertecies = titanDao.getChildrenVertecies(userV, EdgeLabelEnum.STATE, JsonParseFlagEnum.NoParse);
695             if (childrenVertecies.isRight() && childrenVertecies.right().value() != TitanOperationStatus.NOT_FOUND) {
696                 log.debug("Failed to fetch children vertices for user {} by edge {} error {}", userV.getMetadataProperty(GraphPropertyEnum.USERID), EdgeLabelEnum.STATE, childrenVertecies.right().value());
697                 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(childrenVertecies.right().value()));
698             }
699
700             // get all resource with current state
701             if (childrenVertecies.isLeft()) {
702                 componentsPerUser = fetchComponents(lifecycleStates, childrenVertecies.left().value(), neededType, EdgeLabelEnum.STATE);
703
704                 if (componentsPerUser != null) {
705                     for (T comp : componentsPerUser) {
706                         ids.add(comp.getUniqueId());
707                         components.add(comp);
708                     }
709                 }
710             }
711             if (lastStateStates != null && !lastStateStates.isEmpty()) {
712                 // get all resource with last state
713                 childrenVertecies = titanDao.getChildrenVertecies(userV, EdgeLabelEnum.LAST_STATE, JsonParseFlagEnum.NoParse);
714                 if (childrenVertecies.isRight() && childrenVertecies.right().value() != TitanOperationStatus.NOT_FOUND) {
715                     log.debug("Failed to fetch children vertices for user {} by edge {} error {}", userV.getMetadataProperty(GraphPropertyEnum.USERID), EdgeLabelEnum.LAST_STATE, childrenVertecies.right().value());
716                     return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(childrenVertecies.right().value()));
717                 }
718                 if (childrenVertecies.isLeft()) {
719                     boolean isFirst;
720                     componentsPerUser = fetchComponents(lastStateStates, childrenVertecies.left().value(), neededType, EdgeLabelEnum.LAST_STATE);
721                     if (componentsPerUser != null) {
722                         for (T comp : componentsPerUser) {
723                             isFirst = true;
724
725                             if (ids.contains(comp.getUniqueId())) {
726                                 isFirst = false;
727                             }
728                             if (isFirst) {
729                                 components.add(comp);
730                             }
731
732                         }
733                     }
734                 }
735             }
736
737         } // whlile users
738         ;
739         result = Either.left(components);
740         return result;
741
742     }
743
744     private <T extends ToscaElement> List<T> fetchComponents(Set<LifecycleStateEnum> lifecycleStates, List<GraphVertex> vertices, ComponentTypeEnum neededType, EdgeLabelEnum edgelabel) {
745         List<T> components = new ArrayList<>();
746         for (GraphVertex node : vertices) {
747
748             Iterator<Edge> edges = node.getVertex().edges(Direction.IN, edgelabel.name());
749             while (edges.hasNext()) {
750                 Edge edge = edges.next();
751                 String stateStr = (String) titanDao.getProperty(edge, EdgePropertyEnum.STATE);
752
753                 LifecycleStateEnum nodeState = LifecycleStateEnum.findState(stateStr);
754                 if (nodeState == null) {
755                     log.debug("no supported STATE {} for element  {}", stateStr, node.getUniqueId());
756                     continue;
757                 }
758                 if (lifecycleStates != null && lifecycleStates.contains(nodeState)) {
759
760                     Boolean isDeleted = (Boolean) node.getMetadataProperty(GraphPropertyEnum.IS_DELETED);
761                     Boolean isArchived = (Boolean) node.getMetadataProperty(GraphPropertyEnum.IS_ARCHIVED);
762                     if (isDeleted != null && isDeleted || isArchived != null && isArchived) {
763                         log.trace("Deleted/Archived element  {}, discard", node.getUniqueId());
764                         continue;
765                     }
766
767                     Boolean isHighest = (Boolean) node.getMetadataProperty(GraphPropertyEnum.IS_HIGHEST_VERSION);
768                     if (isHighest) {
769
770                         ComponentTypeEnum componentType = node.getType();
771                         // get only latest versions
772
773                         if (componentType == null) {
774                             log.debug("No supported type {} for vertex {}", componentType, node.getUniqueId());
775                             continue;
776                         }
777                         if (neededType == componentType) {
778                             switch (componentType) {
779                                 case SERVICE:
780                                 case PRODUCT:
781                                     handleNode(components, node, componentType);
782                                     break;
783                                 case RESOURCE:
784                                     Boolean isAbtract = (Boolean) node.getMetadataProperty(GraphPropertyEnum.IS_ABSTRACT);
785                                     if (isAbtract == null || !isAbtract) {
786                                         handleNode(components, node, componentType);
787                                     } // if not abstract
788                                     break;
789                                 default:
790                                     log.debug("not supported node type {}", componentType);
791                                     break;
792                             }// case
793                         } // needed type
794                     }
795                 } // if
796             } // while edges
797         } // while resources
798         return components;
799     }
800
801     protected <T extends ToscaElement> void handleNode(List<T> components, GraphVertex vertexComponent, ComponentTypeEnum nodeType) {
802
803         Either<T, StorageOperationStatus> component = getLightComponent(vertexComponent, nodeType, new ComponentParametersView(true));
804         if (component.isRight()) {
805             log.debug("Failed to get component for id =  {}  error : {} skip resource", vertexComponent.getUniqueId(), component.right().value());
806         } else {
807             components.add(component.left().value());
808         }
809     }
810
811     protected <T extends ToscaElement> Either<T, StorageOperationStatus> getLightComponent(String componentUid, ComponentTypeEnum nodeType, ComponentParametersView parametersFilter) {
812         Either<GraphVertex, TitanOperationStatus> getVertexRes = titanDao.getVertexById(componentUid);
813         if (getVertexRes.isRight()) {
814             return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(getVertexRes.right().value()));
815         }
816         return getLightComponent(getVertexRes.left().value(), nodeType, parametersFilter);
817     }
818
819     protected <T extends ToscaElement> Either<T, StorageOperationStatus> getLightComponent(GraphVertex vertexComponent, ComponentTypeEnum nodeType, ComponentParametersView parametersFilter) {
820
821         log.trace("Starting to build light component of type {}, id {}", nodeType, vertexComponent.getUniqueId());
822
823         titanDao.parseVertexProperties(vertexComponent, JsonParseFlagEnum.ParseMetadata);
824
825         T toscaElement = convertToComponent(vertexComponent);
826
827         TitanOperationStatus status = setCreatorFromGraph(vertexComponent, toscaElement);
828         if (status != TitanOperationStatus.OK) {
829             return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
830         }
831
832         status = setLastModifierFromGraph(vertexComponent, toscaElement);
833         if (status != TitanOperationStatus.OK) {
834             return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
835         }
836         status = setCategoriesFromGraph(vertexComponent, toscaElement);
837         if (status != TitanOperationStatus.OK) {
838             return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
839         }
840         if (!parametersFilter.isIgnoreAllVersions()) {
841             status = setAllVersions(vertexComponent, toscaElement);
842             if (status != TitanOperationStatus.OK) {
843                 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
844             }
845         }
846         if (!parametersFilter.isIgnoreCapabilities()) {
847             status = setCapabilitiesFromGraph(vertexComponent, toscaElement);
848             if (status != TitanOperationStatus.OK) {
849                 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
850             }
851         }
852         if (!parametersFilter.isIgnoreRequirements()) {
853             status = setRequirementsFromGraph(vertexComponent, toscaElement);
854             if (status != TitanOperationStatus.OK) {
855                 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
856             }
857         }
858         log.debug("Ended to build light component of type {}, id {}", nodeType, vertexComponent.getUniqueId());
859         return Either.left(toscaElement);
860     }
861
862     @SuppressWarnings("unchecked")
863     protected <T extends ToscaElement> T convertToComponent(GraphVertex componentV) {
864         ToscaElement toscaElement = null;
865         VertexTypeEnum label = componentV.getLabel();
866         switch (label) {
867             case NODE_TYPE:
868                 toscaElement = new NodeType();
869                 break;
870             case TOPOLOGY_TEMPLATE:
871                 toscaElement = new TopologyTemplate();
872                 break;
873             default:
874                 log.debug("Not supported tosca type {}", label);
875                 break;
876         }
877
878         Map<String, Object> jsonMetada = componentV.getMetadataJson();
879         if (toscaElement != null) {
880             toscaElement.setMetadata(jsonMetada);
881         }
882         return (T) toscaElement;
883     }
884
885     protected TitanOperationStatus setResourceCategoryFromGraphV(Vertex vertex, CatalogComponent catalogComponent) {
886
887         Either<Vertex, TitanOperationStatus> childVertex = titanDao.getChildVertex(vertex, EdgeLabelEnum.CATEGORY, JsonParseFlagEnum.NoParse);
888         if (childVertex.isRight()) {
889             log.debug(FAILED_TO_FETCH_FOR_TOSCA_ELEMENT_WITH_ID_ERROR, EdgeLabelEnum.CATEGORY, catalogComponent.getUniqueId(), childVertex.right().value());
890             return childVertex.right().value();
891         }
892         Vertex subCategoryV = childVertex.left().value();
893         catalogComponent.setSubCategoryNormalizedName((String) subCategoryV.property(JsonPresentationFields.NORMALIZED_NAME.getPresentation()).value());
894         Either<Vertex, TitanOperationStatus> parentVertex = titanDao.getParentVertex(subCategoryV, EdgeLabelEnum.SUB_CATEGORY, JsonParseFlagEnum.NoParse);
895         Vertex categoryV = parentVertex.left().value();
896         catalogComponent.setCategoryNormalizedName((String) categoryV.property(JsonPresentationFields.NORMALIZED_NAME.getPresentation()).value());
897
898         return TitanOperationStatus.OK;
899     }
900
901     protected TitanOperationStatus setServiceCategoryFromGraphV(Vertex vertex, CatalogComponent catalogComponent) {
902         Either<Vertex, TitanOperationStatus> childVertex = titanDao.getChildVertex(vertex, EdgeLabelEnum.CATEGORY, JsonParseFlagEnum.NoParse);
903         if (childVertex.isRight()) {
904             log.debug(FAILED_TO_FETCH_FOR_TOSCA_ELEMENT_WITH_ID_ERROR, EdgeLabelEnum.CATEGORY, catalogComponent.getUniqueId(), childVertex.right().value());
905             return childVertex.right().value();
906         }
907         Vertex categoryV = childVertex.left().value();
908         catalogComponent.setCategoryNormalizedName((String) categoryV.property(JsonPresentationFields.NORMALIZED_NAME.getPresentation()).value());
909
910         return TitanOperationStatus.OK;
911     }
912
913     protected TitanOperationStatus setResourceCategoryFromGraph(GraphVertex componentV, ToscaElement toscaElement) {
914         List<CategoryDefinition> categories = new ArrayList<>();
915         SubCategoryDefinition subcategory;
916
917         Either<GraphVertex, TitanOperationStatus> childVertex = titanDao.getChildVertex(componentV, EdgeLabelEnum.CATEGORY, JsonParseFlagEnum.NoParse);
918         if (childVertex.isRight()) {
919             log.debug(FAILED_TO_FETCH_FOR_TOSCA_ELEMENT_WITH_ID_ERROR, EdgeLabelEnum.CATEGORY, componentV.getUniqueId(), childVertex.right().value());
920             return childVertex.right().value();
921         }
922         GraphVertex subCategoryV = childVertex.left().value();
923         Map<GraphPropertyEnum, Object> metadataProperties = subCategoryV.getMetadataProperties();
924         subcategory = new SubCategoryDefinition();
925         subcategory.setUniqueId(subCategoryV.getUniqueId());
926         subcategory.setNormalizedName((String) metadataProperties.get(GraphPropertyEnum.NORMALIZED_NAME));
927         subcategory.setName((String) metadataProperties.get(GraphPropertyEnum.NAME));
928
929         Type listTypeSubcat = new TypeToken<List<String>>() {
930         }.getType();
931         List<String> iconsfromJsonSubcat = getGson().fromJson((String) metadataProperties.get(GraphPropertyEnum.ICONS), listTypeSubcat);
932         subcategory.setIcons(iconsfromJsonSubcat);
933
934         Either<GraphVertex, TitanOperationStatus> parentVertex = titanDao.getParentVertex(subCategoryV, EdgeLabelEnum.SUB_CATEGORY, JsonParseFlagEnum.NoParse);
935         if (parentVertex.isRight()) {
936             log.debug("failed to fetch {} for category with id {}, error {}", EdgeLabelEnum.SUB_CATEGORY, subCategoryV.getUniqueId(), parentVertex.right().value());
937             return childVertex.right().value();
938         }
939         GraphVertex categoryV = parentVertex.left().value();
940         metadataProperties = categoryV.getMetadataProperties();
941
942         CategoryDefinition category = new CategoryDefinition();
943         category.setUniqueId(categoryV.getUniqueId());
944         category.setNormalizedName((String) metadataProperties.get(GraphPropertyEnum.NORMALIZED_NAME));
945         category.setName((String) metadataProperties.get(GraphPropertyEnum.NAME));
946
947         Type listTypeCat = new TypeToken<List<String>>() {
948         }.getType();
949         List<String> iconsfromJsonCat = getGson().fromJson((String) metadataProperties.get(GraphPropertyEnum.ICONS), listTypeCat);
950         category.setIcons(iconsfromJsonCat);
951
952         category.addSubCategory(subcategory);
953         categories.add(category);
954         toscaElement.setCategories(categories);
955
956         return TitanOperationStatus.OK;
957     }
958
959     public <T extends ToscaElement> Either<T, StorageOperationStatus> updateToscaElement(T toscaElementToUpdate, GraphVertex elementV, ComponentParametersView filterResult) {
960         Either<T, StorageOperationStatus> result = null;
961
962         log.debug("In updateToscaElement. received component uid = {}", (toscaElementToUpdate == null ? null : toscaElementToUpdate.getUniqueId()));
963         if (toscaElementToUpdate == null) {
964             log.error("Service object is null");
965             result = Either.right(StorageOperationStatus.BAD_REQUEST);
966             return result;
967         }
968
969         String modifierUserId = toscaElementToUpdate.getLastUpdaterUserId();
970         if (modifierUserId == null || modifierUserId.isEmpty()) {
971             log.error("UserId is missing in the request.");
972             result = Either.right(StorageOperationStatus.BAD_REQUEST);
973             return result;
974         }
975         Either<GraphVertex, TitanOperationStatus> findUser = findUserVertex(modifierUserId);
976
977         if (findUser.isRight()) {
978             TitanOperationStatus status = findUser.right().value();
979             log.error(CANNOT_FIND_USER_IN_THE_GRAPH_STATUS_IS, modifierUserId, status);
980             return result;
981         }
982
983         GraphVertex modifierV = findUser.left().value();
984         String toscaElementId = toscaElementToUpdate.getUniqueId();
985
986         Either<GraphVertex, TitanOperationStatus> parentVertex = titanDao.getParentVertex(elementV, EdgeLabelEnum.LAST_MODIFIER, JsonParseFlagEnum.NoParse);
987         if (parentVertex.isRight()) {
988             log.debug("Failed to fetch last modifier for tosca element with id {} error {}", toscaElementId, parentVertex.right().value());
989             return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(parentVertex.right().value()));
990         }
991         GraphVertex userV = parentVertex.left().value();
992         String currentModifier = (String) userV.getMetadataProperty(GraphPropertyEnum.USERID);
993
994         String prevSystemName = (String) elementV.getMetadataProperty(GraphPropertyEnum.SYSTEM_NAME);
995
996         if (currentModifier.equals(modifierUserId)) {
997             log.debug("Graph LAST MODIFIER edge should not be changed since the modifier is the same as the last modifier.");
998         } else {
999             log.debug("Going to update the last modifier user of the resource from {} to {}", currentModifier, modifierUserId);
1000             StorageOperationStatus status = moveLastModifierEdge(elementV, modifierV);
1001             log.debug("Finish to update the last modifier user of the resource from {} to {}. status is {}", currentModifier, modifierUserId, status);
1002             if (status != StorageOperationStatus.OK) {
1003                 result = Either.right(status);
1004                 return result;
1005             }
1006         }
1007
1008         final long currentTimeMillis = System.currentTimeMillis();
1009         log.debug("Going to update the last Update Date of the resource from {} to {}", elementV.getJsonMetadataField(JsonPresentationFields.LAST_UPDATE_DATE), currentTimeMillis);
1010         elementV.setJsonMetadataField(JsonPresentationFields.LAST_UPDATE_DATE, currentTimeMillis);
1011
1012         StorageOperationStatus checkCategories = validateCategories(toscaElementToUpdate, elementV);
1013         if (checkCategories != StorageOperationStatus.OK) {
1014             result = Either.right(checkCategories);
1015             return result;
1016         }
1017
1018         // update all data on vertex
1019         fillToscaElementVertexData(elementV, toscaElementToUpdate, JsonParseFlagEnum.ParseMetadata);
1020
1021         Either<GraphVertex, TitanOperationStatus> updateElement = titanDao.updateVertex(elementV);
1022
1023         if (updateElement.isRight()) {
1024             log.error("Failed to update resource {}. status is {}", toscaElementId, updateElement.right().value());
1025             result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(updateElement.right().value()));
1026             return result;
1027         }
1028         GraphVertex updateElementV = updateElement.left().value();
1029
1030         // DE230195 in case resource name changed update TOSCA artifacts
1031         // file names accordingly
1032         String newSystemName = (String) updateElementV.getMetadataProperty(GraphPropertyEnum.SYSTEM_NAME);
1033         if (newSystemName != null && !newSystemName.equals(prevSystemName)) {
1034             Either<Map<String, ArtifactDataDefinition>, TitanOperationStatus> resultToscaArt = getDataFromGraph(updateElementV, EdgeLabelEnum.TOSCA_ARTIFACTS);
1035             if (resultToscaArt.isRight()) {
1036                 log.debug("Failed to get  tosca artifact from graph for tosca element {} error {}", toscaElementId, resultToscaArt.right().value());
1037                 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(resultToscaArt.right().value()));
1038             }
1039
1040             Map<String, ArtifactDataDefinition> toscaArtifacts = resultToscaArt.left().value();
1041             if (toscaArtifacts != null) {
1042                 for (Entry<String, ArtifactDataDefinition> artifact : toscaArtifacts.entrySet()) {
1043                     generateNewToscaFileName(toscaElementToUpdate.getComponentType().getValue().toLowerCase(), newSystemName, artifact.getValue());
1044                 }
1045                 // TODO call to new Artifact operation in order to update list of artifacts
1046
1047             }
1048         }
1049
1050         if (toscaElementToUpdate.getComponentType() == ComponentTypeEnum.RESOURCE) {
1051             StorageOperationStatus resultDerived = updateDerived(toscaElementToUpdate, updateElementV);
1052             if (resultDerived != StorageOperationStatus.OK) {
1053                 log.debug("Failed to update from derived data for element {} error {}", toscaElementId, resultDerived);
1054                 return Either.right(resultDerived);
1055             }
1056         }
1057
1058         Either<T, StorageOperationStatus> updatedResource = getToscaElement(updateElementV, filterResult);
1059         if (updatedResource.isRight()) {
1060             log.error("Failed to fetch tosca element {} after update , error {}", toscaElementId, updatedResource.right().value());
1061             result = Either.right(StorageOperationStatus.BAD_REQUEST);
1062             return result;
1063         }
1064
1065         T updatedResourceValue = updatedResource.left().value();
1066         result = Either.left(updatedResourceValue);
1067
1068         return result;
1069     }
1070
1071     protected StorageOperationStatus moveLastModifierEdge(GraphVertex elementV, GraphVertex modifierV) {
1072         return DaoStatusConverter.convertTitanStatusToStorageStatus(titanDao.moveEdge(elementV, modifierV, EdgeLabelEnum.LAST_MODIFIER, Direction.IN));
1073     }
1074
1075     protected StorageOperationStatus moveCategoryEdge(GraphVertex elementV, GraphVertex categoryV) {
1076         return DaoStatusConverter.convertTitanStatusToStorageStatus(titanDao.moveEdge(elementV, categoryV, EdgeLabelEnum.CATEGORY, Direction.OUT));
1077     }
1078
1079     private void generateNewToscaFileName(String componentType, String componentName, ArtifactDataDefinition artifactInfo) {
1080         Map<String, Object> getConfig = (Map<String, Object>) ConfigurationManager.getConfigurationManager().getConfiguration().getToscaArtifacts().entrySet().stream().filter(p -> p.getKey().equalsIgnoreCase(artifactInfo.getArtifactLabel()))
1081                 .findAny().get().getValue();
1082         artifactInfo.setArtifactName(componentType + "-" + componentName + getConfig.get("artifactName"));
1083     }
1084
1085     protected <T extends ToscaElement> StorageOperationStatus validateResourceCategory(T toscaElementToUpdate, GraphVertex elementV) {
1086         StorageOperationStatus status = StorageOperationStatus.OK;
1087         List<CategoryDefinition> newCategoryList = toscaElementToUpdate.getCategories();
1088         CategoryDefinition newCategory = newCategoryList.get(0);
1089
1090         Either<GraphVertex, TitanOperationStatus> childVertex = titanDao.getChildVertex(elementV, EdgeLabelEnum.CATEGORY, JsonParseFlagEnum.NoParse);
1091         if (childVertex.isRight()) {
1092             log.debug(FAILED_TO_FETCH_FOR_TOSCA_ELEMENT_WITH_ID_ERROR, EdgeLabelEnum.CATEGORY, elementV.getUniqueId(), childVertex.right().value());
1093             return DaoStatusConverter.convertTitanStatusToStorageStatus(childVertex.right().value());
1094         }
1095         GraphVertex subCategoryV = childVertex.left().value();
1096         Map<GraphPropertyEnum, Object> metadataProperties = subCategoryV.getMetadataProperties();
1097         String subCategoryNameCurrent = (String) metadataProperties.get(GraphPropertyEnum.NAME);
1098
1099         Either<GraphVertex, TitanOperationStatus> parentVertex = titanDao.getParentVertex(subCategoryV, EdgeLabelEnum.SUB_CATEGORY, JsonParseFlagEnum.NoParse);
1100         if (parentVertex.isRight()) {
1101             log.debug("failed to fetch {} for category with id {}, error {}", EdgeLabelEnum.SUB_CATEGORY, subCategoryV.getUniqueId(), parentVertex.right().value());
1102             return DaoStatusConverter.convertTitanStatusToStorageStatus(childVertex.right().value());
1103         }
1104         GraphVertex categoryV = parentVertex.left().value();
1105         metadataProperties = categoryV.getMetadataProperties();
1106         String categoryNameCurrent = (String) metadataProperties.get(GraphPropertyEnum.NAME);
1107
1108         boolean categoryWasChanged = false;
1109
1110         String newCategoryName = newCategory.getName();
1111         SubCategoryDefinition newSubcategory = newCategory.getSubcategories().get(0);
1112         String newSubCategoryName = newSubcategory.getName();
1113         if (newCategoryName != null && !newCategoryName.equals(categoryNameCurrent)) {
1114             // the category was changed
1115             categoryWasChanged = true;
1116         } else {
1117             // the sub-category was changed
1118             if (newSubCategoryName != null && !newSubCategoryName.equals(subCategoryNameCurrent)) {
1119                 log.debug("Going to update the category of the resource from {} to {}", categoryNameCurrent, newCategory);
1120                 categoryWasChanged = true;
1121             }
1122         }
1123         if (categoryWasChanged) {
1124             Either<GraphVertex, StorageOperationStatus> getCategoryVertex = getResourceCategoryVertex(elementV.getUniqueId(), newSubCategoryName, newCategoryName);
1125
1126             if (getCategoryVertex.isRight()) {
1127                 return getCategoryVertex.right().value();
1128             }
1129             GraphVertex newCategoryV = getCategoryVertex.left().value();
1130             status = moveCategoryEdge(elementV, newCategoryV);
1131             log.debug("Going to update the category of the resource from {} to {}. status is {}", categoryNameCurrent, newCategory, status);
1132         }
1133         return status;
1134     }
1135
1136     public <T extends ToscaElement> Either<List<T>, StorageOperationStatus> getElementCatalogData(ComponentTypeEnum componentType, List<ResourceTypeEnum> excludeTypes, boolean isHighestVersions) {
1137         Either<List<GraphVertex>, TitanOperationStatus> listOfComponents;
1138         if (isHighestVersions) {
1139             listOfComponents = getListOfHighestComponents(componentType, excludeTypes, JsonParseFlagEnum.NoParse);
1140         } else {
1141             listOfComponents = getListOfHighestAndAllCertifiedComponents(componentType, excludeTypes);
1142         }
1143
1144         if (listOfComponents.isRight() && listOfComponents.right().value() != TitanOperationStatus.NOT_FOUND) {
1145             return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(listOfComponents.right().value()));
1146         }
1147         List<T> result = new ArrayList<>();
1148         if (listOfComponents.isLeft()) {
1149             List<GraphVertex> highestAndAllCertified = listOfComponents.left().value();
1150             if (highestAndAllCertified != null && !highestAndAllCertified.isEmpty()) {
1151                 for (GraphVertex vertexComponent : highestAndAllCertified) {
1152                     Either<T, StorageOperationStatus> component = getLightComponent(vertexComponent, componentType, new ComponentParametersView(true));
1153                     if (component.isRight()) {
1154                         log.debug("Failed to fetch light element for {} error {}", vertexComponent.getUniqueId(), component.right().value());
1155                         return Either.right(component.right().value());
1156                     } else {
1157                         result.add(component.left().value());
1158                     }
1159                 }
1160             }
1161         }
1162         return Either.left(result);
1163     }
1164
1165     public Either<List<CatalogComponent>, StorageOperationStatus> getElementCatalogData(boolean isCatalog, List<ResourceTypeEnum> excludeTypes) {
1166         StopWatch stopWatch = new StopWatch();
1167         stopWatch.start();
1168
1169         Map<String, CatalogComponent> existInCatalog = new HashMap<>();
1170         Either<Iterator<Vertex>, TitanOperationStatus> verticesEither = titanDao.getCatalogOrArchiveVerticies(isCatalog);
1171         if (verticesEither.isRight()) {
1172             return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(verticesEither.right().value()));
1173         }
1174         Iterator<Vertex> vertices = verticesEither.left().value();
1175         while (vertices.hasNext()) {
1176             handleCatalogComponent(existInCatalog, vertices.next(), excludeTypes);
1177         }
1178         stopWatch.stop();
1179         String timeToFetchElements = stopWatch.prettyPrint();
1180         log.info("time to fetch all catalog elements: {}", timeToFetchElements);
1181         return Either.left(existInCatalog.values().stream().collect(Collectors.toList()));
1182     }
1183
1184     private void handleCatalogComponent(Map<String, CatalogComponent> existInCatalog, Vertex vertex, List<ResourceTypeEnum> excludeTypes) {
1185         VertexProperty<Object> property = vertex.property(GraphPropertiesDictionary.METADATA.getProperty());
1186         String json = (String) property.value();
1187         Map<String, Object> metadatObj = JsonParserUtils.toMap(json);
1188
1189         String uniqueId = (String) metadatObj.get(JsonPresentationFields.UNIQUE_ID.getPresentation());
1190         Boolean isDeleted = (Boolean) metadatObj.get(JsonPresentationFields.IS_DELETED.getPresentation());
1191
1192
1193         if (isAddToCatalog(excludeTypes, metadatObj) && (existInCatalog.get(uniqueId) == null && (isDeleted == null || !isDeleted.booleanValue()))) {
1194             CatalogComponent catalogComponent = new CatalogComponent();
1195             catalogComponent.setUniqueId(uniqueId);
1196
1197             catalogComponent.setComponentType(ComponentTypeEnum.valueOf((String) metadatObj.get(JsonPresentationFields.COMPONENT_TYPE.getPresentation())));
1198             catalogComponent.setVersion((String) metadatObj.get(JsonPresentationFields.VERSION.getPresentation()));
1199             catalogComponent.setName((String) metadatObj.get(JsonPresentationFields.NAME.getPresentation()));
1200             catalogComponent.setIcon((String) metadatObj.get(JsonPresentationFields.ICON.getPresentation()));
1201             catalogComponent.setLifecycleState((String) metadatObj.get(JsonPresentationFields.LIFECYCLE_STATE.getPresentation()));
1202             catalogComponent.setLastUpdateDate((Long) metadatObj.get(JsonPresentationFields.LAST_UPDATE_DATE.getPresentation()));
1203             catalogComponent.setDistributionStatus((String) metadatObj.get(JsonPresentationFields.DISTRIBUTION_STATUS.getPresentation()));
1204             Object resourceType = metadatObj.get(JsonPresentationFields.RESOURCE_TYPE.getPresentation());
1205             if (resourceType != null) {
1206                 catalogComponent.setResourceType((String) resourceType);
1207             }
1208
1209             if (catalogComponent.getComponentType() == ComponentTypeEnum.SERVICE) {
1210                 setServiceCategoryFromGraphV(vertex, catalogComponent);
1211
1212             } else {
1213                 setResourceCategoryFromGraphV(vertex, catalogComponent);
1214             }
1215             List<String> tags = (List<String>) metadatObj.get(JsonPresentationFields.TAGS.getPresentation());
1216             if (tags != null) {
1217                 catalogComponent.setTags(tags);
1218             }
1219             existInCatalog.put(uniqueId, catalogComponent);
1220         }
1221     }
1222
1223     private boolean isAddToCatalog(List<ResourceTypeEnum> excludeTypes, Map<String, Object> metadatObj) {
1224         boolean isAddToCatalog = true;
1225         Object resourceTypeStr = metadatObj.get(JsonPresentationFields.RESOURCE_TYPE.getPresentation());
1226         if (resourceTypeStr != null) {
1227             ResourceTypeEnum resourceType = ResourceTypeEnum.getType((String) resourceTypeStr);
1228             if (!CollectionUtils.isEmpty(excludeTypes)) {
1229                 Optional<ResourceTypeEnum> op = excludeTypes.stream().filter(rt -> rt == resourceType).findAny();
1230                 if (op.isPresent())
1231                     isAddToCatalog = false;
1232             }
1233         }
1234         return isAddToCatalog;
1235     }
1236
1237     public Either<List<GraphVertex>, TitanOperationStatus> getListOfHighestComponents(ComponentTypeEnum
1238                                                                                               componentType, List<ResourceTypeEnum> excludeTypes, JsonParseFlagEnum parseFlag) {
1239         Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
1240         Map<GraphPropertyEnum, Object> propertiesHasNotToMatch = new EnumMap<>(GraphPropertyEnum.class);
1241         propertiesToMatch.put(GraphPropertyEnum.COMPONENT_TYPE, componentType.name());
1242         propertiesToMatch.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
1243
1244         if (componentType == ComponentTypeEnum.RESOURCE) {
1245             propertiesToMatch.put(GraphPropertyEnum.IS_ABSTRACT, false);
1246             propertiesHasNotToMatch.put(GraphPropertyEnum.RESOURCE_TYPE, excludeTypes);
1247         }
1248         propertiesHasNotToMatch.put(GraphPropertyEnum.IS_DELETED, true);
1249         propertiesHasNotToMatch.put(GraphPropertyEnum.IS_ARCHIVED, true); //US382674, US382683
1250
1251         return titanDao.getByCriteria(null, propertiesToMatch, propertiesHasNotToMatch, parseFlag);
1252     }
1253
1254     // highest + (certified && !highest)
1255     public Either<List<GraphVertex>, TitanOperationStatus> getListOfHighestAndAllCertifiedComponents
1256     (ComponentTypeEnum componentType, List<ResourceTypeEnum> excludeTypes) {
1257         long startFetchAllStates = System.currentTimeMillis();
1258         Either<List<GraphVertex>, TitanOperationStatus> highestNodes = getListOfHighestComponents(componentType, excludeTypes, JsonParseFlagEnum.ParseMetadata);
1259
1260         Map<GraphPropertyEnum, Object> propertiesToMatchCertified = new EnumMap<>(GraphPropertyEnum.class);
1261         Map<GraphPropertyEnum, Object> propertiesHasNotToMatchCertified = new EnumMap<>(GraphPropertyEnum.class);
1262         propertiesToMatchCertified.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
1263         propertiesToMatchCertified.put(GraphPropertyEnum.COMPONENT_TYPE, componentType.name());
1264         if (componentType == ComponentTypeEnum.RESOURCE) {
1265             propertiesToMatchCertified.put(GraphPropertyEnum.IS_ABSTRACT, false);
1266             propertiesHasNotToMatchCertified.put(GraphPropertyEnum.RESOURCE_TYPE, excludeTypes);
1267         }
1268
1269         propertiesHasNotToMatchCertified.put(GraphPropertyEnum.IS_DELETED, true);
1270         propertiesHasNotToMatchCertified.put(GraphPropertyEnum.IS_ARCHIVED, true);  //US382674, US382683
1271         propertiesHasNotToMatchCertified.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
1272
1273         Either<List<GraphVertex>, TitanOperationStatus> certifiedNotHighestNodes = titanDao.getByCriteria(null, propertiesToMatchCertified, propertiesHasNotToMatchCertified, JsonParseFlagEnum.ParseMetadata);
1274         if (certifiedNotHighestNodes.isRight() && certifiedNotHighestNodes.right().value() != TitanOperationStatus.NOT_FOUND) {
1275             return Either.right(certifiedNotHighestNodes.right().value());
1276         }
1277
1278         long endFetchAllStates = System.currentTimeMillis();
1279
1280         List<GraphVertex> allNodes = new ArrayList<>();
1281
1282         if (certifiedNotHighestNodes.isLeft()) {
1283             allNodes.addAll(certifiedNotHighestNodes.left().value());
1284         }
1285         if (highestNodes.isLeft()) {
1286             allNodes.addAll(highestNodes.left().value());
1287         }
1288
1289         log.debug("Fetch catalog {}s all states from graph took {} ms", componentType, endFetchAllStates - startFetchAllStates);
1290         return Either.left(allNodes);
1291     }
1292
1293     protected Either<List<GraphVertex>, StorageOperationStatus> getAllComponentsMarkedForDeletion(ComponentTypeEnum
1294                                                                                                           componentType) {
1295
1296         // get all components marked for delete
1297         Map<GraphPropertyEnum, Object> props = new EnumMap<>(GraphPropertyEnum.class);
1298         props.put(GraphPropertyEnum.IS_DELETED, true);
1299         props.put(GraphPropertyEnum.COMPONENT_TYPE, componentType.name());
1300
1301         Either<List<GraphVertex>, TitanOperationStatus> componentsToDelete = titanDao.getByCriteria(null, props, JsonParseFlagEnum.NoParse);
1302
1303         if (componentsToDelete.isRight()) {
1304             TitanOperationStatus error = componentsToDelete.right().value();
1305             if (error.equals(TitanOperationStatus.NOT_FOUND)) {
1306                 log.trace("no components to delete");
1307                 return Either.left(new ArrayList<>());
1308             } else {
1309                 log.info("failed to find components to delete. error : {}", error.name());
1310                 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(error));
1311             }
1312         }
1313         return Either.left(componentsToDelete.left().value());
1314     }
1315
1316     protected TitanOperationStatus setAdditionalInformationFromGraph(GraphVertex componentV, ToscaElement
1317             toscaElement) {
1318         Either<Map<String, AdditionalInfoParameterDataDefinition>, TitanOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.ADDITIONAL_INFORMATION);
1319         if (result.isLeft()) {
1320             toscaElement.setAdditionalInformation(result.left().value());
1321         } else {
1322             if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
1323                 return result.right().value();
1324             }
1325         }
1326         return TitanOperationStatus.OK;
1327     }
1328
1329     // --------------------------------------------
1330     public abstract <T extends
1331             ToscaElement> Either<T, StorageOperationStatus> getToscaElement(String uniqueId, ComponentParametersView componentParametersView);
1332
1333     public abstract <T extends
1334             ToscaElement> Either<T, StorageOperationStatus> getToscaElement(GraphVertex toscaElementVertex, ComponentParametersView componentParametersView);
1335
1336     public abstract <T extends
1337             ToscaElement> Either<T, StorageOperationStatus> deleteToscaElement(GraphVertex toscaElementVertex);
1338
1339     public abstract <T extends
1340             ToscaElement> Either<T, StorageOperationStatus> createToscaElement(ToscaElement toscaElement);
1341
1342     protected abstract <T extends ToscaElement> TitanOperationStatus
1343     setCategoriesFromGraph(GraphVertex vertexComponent, T toscaElement);
1344
1345     protected abstract <T extends ToscaElement> TitanOperationStatus
1346     setCapabilitiesFromGraph(GraphVertex componentV, T toscaElement);
1347
1348     protected abstract <T extends ToscaElement> TitanOperationStatus
1349     setRequirementsFromGraph(GraphVertex componentV, T toscaElement);
1350
1351     protected abstract <T extends ToscaElement> StorageOperationStatus
1352     validateCategories(T toscaElementToUpdate, GraphVertex elementV);
1353
1354     protected abstract <T extends ToscaElement> StorageOperationStatus
1355     updateDerived(T toscaElementToUpdate, GraphVertex updateElementV);
1356
1357     public abstract <T extends ToscaElement> void fillToscaElementVertexData(GraphVertex elementV, T
1358             toscaElementToUpdate, JsonParseFlagEnum flag);
1359
1360 }