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