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