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