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