62d04edf4facfbc6e7e7b049fae472b65869ea08
[sdc.git] /
1 package org.openecomp.sdc.be.model.jsontitan.operations;
2
3 import java.lang.reflect.Type;
4 import java.util.ArrayList;
5 import java.util.HashMap;
6 import java.util.Iterator;
7 import java.util.List;
8 import java.util.Map;
9
10 import org.apache.tinkerpop.gremlin.structure.Direction;
11 import org.apache.tinkerpop.gremlin.structure.Edge;
12 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
13 import org.openecomp.sdc.be.dao.jsongraph.types.EdgeLabelEnum;
14 import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum;
15 import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum;
16 import org.openecomp.sdc.be.dao.titan.TitanOperationStatus;
17 import org.openecomp.sdc.be.datatypes.elements.AdditionalInfoParameterDataDefinition;
18 import org.openecomp.sdc.be.datatypes.elements.ArtifactDataDefinition;
19 import org.openecomp.sdc.be.datatypes.elements.ComponentInstanceDataDefinition;
20 import org.openecomp.sdc.be.datatypes.elements.CompositionDataDefinition;
21 import org.openecomp.sdc.be.datatypes.elements.GroupDataDefinition;
22 import org.openecomp.sdc.be.datatypes.elements.MapArtifactDataDefinition;
23 import org.openecomp.sdc.be.datatypes.elements.MapAttributesDataDefinition;
24 import org.openecomp.sdc.be.datatypes.elements.MapCapabiltyProperty;
25 import org.openecomp.sdc.be.datatypes.elements.MapGroupsDataDefinition;
26 import org.openecomp.sdc.be.datatypes.elements.MapListCapabiltyDataDefinition;
27 import org.openecomp.sdc.be.datatypes.elements.MapListRequirementDataDefinition;
28 import org.openecomp.sdc.be.datatypes.elements.MapPropertiesDataDefinition;
29 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
30 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
31 import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum;
32 import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
33 import org.openecomp.sdc.be.datatypes.tosca.ToscaDataDefinition;
34 import org.openecomp.sdc.be.model.ComponentParametersView;
35 import org.openecomp.sdc.be.model.DistributionStatusEnum;
36 import org.openecomp.sdc.be.model.User;
37 import org.openecomp.sdc.be.model.category.CategoryDefinition;
38 import org.openecomp.sdc.be.model.jsontitan.datamodel.TopologyTemplate;
39 import org.openecomp.sdc.be.model.jsontitan.datamodel.ToscaElement;
40 import org.openecomp.sdc.be.model.jsontitan.datamodel.ToscaElementTypeEnum;
41 import org.openecomp.sdc.be.model.jsontitan.enums.JsonConstantKeysEnum;
42 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
43 import org.openecomp.sdc.be.model.operations.impl.DaoStatusConverter;
44 import org.openecomp.sdc.be.model.operations.impl.UniqueIdBuilder;
45 import org.openecomp.sdc.common.jsongraph.util.CommonUtility;
46 import org.openecomp.sdc.common.jsongraph.util.CommonUtility.LogLevelEnum;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
49
50 import com.google.gson.reflect.TypeToken;
51
52 import fj.data.Either;
53
54 @org.springframework.stereotype.Component("topology-template-operation")
55 public class TopologyTemplateOperation extends ToscaElementOperation {
56         private static Logger log = LoggerFactory.getLogger(TopologyTemplateOperation.class.getName());
57
58         public Either<TopologyTemplate, StorageOperationStatus> createTopologyTemplate(TopologyTemplate topologyTemplate) {
59                 Either<TopologyTemplate, StorageOperationStatus> result = null;
60
61                 topologyTemplate.generateUUID();
62
63                 topologyTemplate = (TopologyTemplate) getResourceMetaDataFromResource(topologyTemplate);
64                 String resourceUniqueId = topologyTemplate.getUniqueId();
65                 if (resourceUniqueId == null) {
66                         resourceUniqueId = UniqueIdBuilder.buildResourceUniqueId();
67                         topologyTemplate.setUniqueId(resourceUniqueId);
68                 }
69
70                 GraphVertex topologyTemplateVertex = new GraphVertex();
71                 topologyTemplateVertex = fillMetadata(topologyTemplateVertex, topologyTemplate, JsonParseFlagEnum.ParseAll);
72
73                 Either<GraphVertex, TitanOperationStatus> createdVertex = titanDao.createVertex(topologyTemplateVertex);
74                 if (createdVertex.isRight()) {
75                         TitanOperationStatus status = createdVertex.right().value();
76                         log.error("Error returned after creating topology template data node {}. status returned is ", topologyTemplateVertex, status);
77                         result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
78                         return result;
79                 }
80
81                 topologyTemplateVertex = createdVertex.left().value();
82
83                 StorageOperationStatus assosiateCommon = assosiateCommonForToscaElement(topologyTemplateVertex, topologyTemplate, null);
84                 if (assosiateCommon != StorageOperationStatus.OK) {
85                         result = Either.right(assosiateCommon);
86                         return result;
87                 }
88
89                 StorageOperationStatus associateCategory = assosiateMetadataToCategory(topologyTemplateVertex, topologyTemplate);
90                 if (associateCategory != StorageOperationStatus.OK) {
91                         result = Either.right(associateCategory);
92                         return result;
93                 }
94
95                 StorageOperationStatus associateInputs = associateInputsToComponent(topologyTemplateVertex, topologyTemplate);
96                 if (associateInputs != StorageOperationStatus.OK) {
97                         result = Either.right(associateInputs);
98                         return result;
99                 }
100                 StorageOperationStatus associateGroups = associateGroupsToComponent(topologyTemplateVertex, topologyTemplate);
101                 if (associateGroups != StorageOperationStatus.OK) {
102                         result = Either.right(associateGroups);
103                         return result;
104                 }
105                 StorageOperationStatus associateInstAttr = associateInstAttributesToComponent(topologyTemplateVertex, topologyTemplate);
106                 if (associateInstAttr != StorageOperationStatus.OK) {
107                         result = Either.right(associateInstAttr);
108                         return result;
109                 }
110                 StorageOperationStatus associateInstProperties = associateInstPropertiesToComponent(topologyTemplateVertex, topologyTemplate);
111                 if (associateInstProperties != StorageOperationStatus.OK) {
112                         result = Either.right(associateInstProperties);
113                         return result;
114                 }
115                 StorageOperationStatus associateInstInputs = associateInstInputsToComponent(topologyTemplateVertex, topologyTemplate);
116                 if (associateInstProperties != StorageOperationStatus.OK) {
117                         result = Either.right(associateInstInputs);
118                         return result;
119                 }
120                 StorageOperationStatus associateRequirements = associateRequirementsToResource(topologyTemplateVertex, topologyTemplate);
121                 if (associateRequirements != StorageOperationStatus.OK) {
122                         result = Either.right(associateRequirements);
123                         return result;
124                 }
125
126                 StorageOperationStatus associateCapabilities = associateCapabilitiesToResource(topologyTemplateVertex, topologyTemplate);
127                 if (associateCapabilities != StorageOperationStatus.OK) {
128                         result = Either.right(associateCapabilities);
129                         return result;
130                 }
131
132                 StorageOperationStatus associateArtifacts = associateTopologyTemplateArtifactsToComponent(topologyTemplateVertex, topologyTemplate);
133                 if (associateArtifacts != StorageOperationStatus.OK) {
134                         result = Either.right(associateArtifacts);
135                         return result;
136                 }
137
138                 StorageOperationStatus addAdditionalInformation = addAdditionalInformationToResource(topologyTemplateVertex, topologyTemplate);
139                 if (addAdditionalInformation != StorageOperationStatus.OK) {
140                         result = Either.right(addAdditionalInformation);
141                         return result;
142                 }
143                 StorageOperationStatus associateCapProperties = associateCapPropertiesToResource(topologyTemplateVertex, topologyTemplate);
144                 if (associateCapProperties != StorageOperationStatus.OK) {
145                         result = Either.right(associateCapProperties);
146                         return result;
147                 }
148                 return Either.left(topologyTemplate);
149
150         }
151
152         private StorageOperationStatus associateCapPropertiesToResource(GraphVertex topologyTemplateVertex, TopologyTemplate topologyTemplate) {
153                 Map<String, MapCapabiltyProperty> calculatedCapProperties = topologyTemplate.getCalculatedCapabilitiesProperties();
154                 if (calculatedCapProperties != null && !calculatedCapProperties.isEmpty()) {
155                         Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(topologyTemplateVertex, VertexTypeEnum.CALCULATED_CAP_PROPERTIES, EdgeLabelEnum.CALCULATED_CAP_PROPERTIES, calculatedCapProperties);
156                         if (assosiateElementToData.isRight()) {
157                                 return assosiateElementToData.right().value();
158                         }
159                 }
160                 return StorageOperationStatus.OK;
161         }
162
163         private StorageOperationStatus associateCapabilitiesToResource(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
164                 Map<String, MapListCapabiltyDataDefinition> calculatedCapabilities = topologyTemplate.getCalculatedCapabilities();
165                 if (calculatedCapabilities != null && !calculatedCapabilities.isEmpty()) {
166                         Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.CALCULATED_CAPABILITIES, EdgeLabelEnum.CALCULATED_CAPABILITIES, calculatedCapabilities);
167                         if (assosiateElementToData.isRight()) {
168                                 return assosiateElementToData.right().value();
169                         }
170                 }
171                 Map<String, MapListCapabiltyDataDefinition> fullfilledCapabilities = topologyTemplate.getFullfilledCapabilities();
172                 if (fullfilledCapabilities != null && !fullfilledCapabilities.isEmpty()) {
173                         Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.FULLFILLED_CAPABILITIES, EdgeLabelEnum.FULLFILLED_CAPABILITIES, fullfilledCapabilities);
174                         if (assosiateElementToData.isRight()) {
175                                 return assosiateElementToData.right().value();
176                         }
177                 }
178                 return StorageOperationStatus.OK;
179
180         }
181
182         private StorageOperationStatus associateRequirementsToResource(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
183                 Map<String, MapListRequirementDataDefinition> calculatedRequirements = topologyTemplate.getCalculatedRequirements();
184                 if (calculatedRequirements != null && !calculatedRequirements.isEmpty()) {
185                         Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.CALCULATED_REQUIREMENTS, EdgeLabelEnum.CALCULATED_REQUIREMENTS, calculatedRequirements);
186                         if (assosiateElementToData.isRight()) {
187                                 return assosiateElementToData.right().value();
188                         }
189                 }
190                 Map<String, MapListRequirementDataDefinition> fullfilledRequirements = topologyTemplate.getFullfilledRequirements();
191                 if (fullfilledRequirements != null && !fullfilledRequirements.isEmpty()) {
192                         Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.FULLFILLED_REQUIREMENTS, EdgeLabelEnum.FULLFILLED_REQUIREMENTS, fullfilledRequirements);
193                         if (assosiateElementToData.isRight()) {
194                                 return assosiateElementToData.right().value();
195                         }
196                 }
197                 return StorageOperationStatus.OK;
198         }
199
200         private StorageOperationStatus associateTopologyTemplateArtifactsToComponent(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
201                 Map<String, ArtifactDataDefinition> addInformation = topologyTemplate.getServiceApiArtifacts();
202
203                 if (addInformation != null && !addInformation.isEmpty()) {
204                         addInformation.values().stream().filter(a -> a.getUniqueId() == null).forEach(a -> {
205                                 String uniqueId = UniqueIdBuilder.buildPropertyUniqueId(nodeTypeVertex.getUniqueId().toLowerCase(), a.getArtifactLabel().toLowerCase());
206                                 a.setUniqueId(uniqueId);
207                         });
208                         Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.SERVICE_API_ARTIFACTS, EdgeLabelEnum.SERVICE_API_ARTIFACTS, addInformation);
209                         if (assosiateElementToData.isRight()) {
210                                 return assosiateElementToData.right().value();
211                         }
212                 }
213                 Map<String, MapArtifactDataDefinition> instArtifacts = topologyTemplate.getInstDeploymentArtifacts();
214
215                 if (instArtifacts != null && !instArtifacts.isEmpty()) {
216                         Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.INST_DEPLOYMENT_ARTIFACTS, EdgeLabelEnum.INST_DEPLOYMENT_ARTIFACTS, instArtifacts);
217                         if (assosiateElementToData.isRight()) {
218                                 return assosiateElementToData.right().value();
219                         }
220                 }
221                 Map<String, MapArtifactDataDefinition> instInfoArtifacts = topologyTemplate.getInstanceArtifacts();
222
223                 if (instInfoArtifacts != null && !instInfoArtifacts.isEmpty()) {
224                         Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.INSTANCE_ARTIFACTS, EdgeLabelEnum.INSTANCE_ARTIFACTS, instInfoArtifacts);
225                         if (assosiateElementToData.isRight()) {
226                                 return assosiateElementToData.right().value();
227                         }
228                 }
229                 return StorageOperationStatus.OK;
230         }
231
232         private StorageOperationStatus addAdditionalInformationToResource(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
233
234                 Map<String, AdditionalInfoParameterDataDefinition> addInformation = topologyTemplate.getAdditionalInformation();
235
236                 if (addInformation != null && !addInformation.isEmpty()) {
237                         Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.ADDITIONAL_INFORMATION, EdgeLabelEnum.ADDITIONAL_INFORMATION, addInformation);
238                         if (assosiateElementToData.isRight()) {
239                                 return assosiateElementToData.right().value();
240                         }
241                 }
242                 return StorageOperationStatus.OK;
243         }
244
245         public StorageOperationStatus associateInstPropertiesToComponent(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
246                 Map<String, MapPropertiesDataDefinition> instProps = topologyTemplate.getInstProperties();
247                 return associateInstPropertiesToComponent(nodeTypeVertex, instProps);
248         }
249
250         public StorageOperationStatus associateInstInputsToComponent(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
251                 Map<String, MapPropertiesDataDefinition> instProps = topologyTemplate.getInstInputs();
252                 return associateInstInputsToComponent(nodeTypeVertex, instProps);
253         }
254
255         public StorageOperationStatus associateInstPropertiesToComponent(GraphVertex nodeTypeVertex, Map<String, MapPropertiesDataDefinition> instProps) {
256                 if (instProps != null && !instProps.isEmpty()) {
257                         Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.INST_PROPERTIES, EdgeLabelEnum.INST_PROPERTIES, instProps);
258                         if (assosiateElementToData.isRight()) {
259                                 return assosiateElementToData.right().value();
260                         }
261                 }
262                 return StorageOperationStatus.OK;
263         }
264
265         public StorageOperationStatus associateInstInputsToComponent(GraphVertex nodeTypeVertex, Map<String, MapPropertiesDataDefinition> instInputs) {
266                 if (instInputs != null && !instInputs.isEmpty()) {
267                         Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.INST_INPUTS, EdgeLabelEnum.INST_INPUTS, instInputs);
268                         if (assosiateElementToData.isRight()) {
269                                 return assosiateElementToData.right().value();
270                         }
271                 }
272                 return StorageOperationStatus.OK;
273         }
274
275         public StorageOperationStatus addInstInputsToComponent(GraphVertex nodeTypeVertex, Map<String, MapPropertiesDataDefinition> instInputs) {
276
277                 if (instInputs != null && !instInputs.isEmpty()) {
278                         instInputs.entrySet().forEach(i -> {
279                                 StorageOperationStatus status = addToscaDataDeepElementsBlockToToscaElement(nodeTypeVertex, EdgeLabelEnum.INST_INPUTS, VertexTypeEnum.INST_INPUTS, i.getValue(), i.getKey());
280                                 if (status != StorageOperationStatus.OK) {
281                                         return;
282                                 }
283                         });
284                 }
285
286                 return StorageOperationStatus.OK;
287         }
288
289         public StorageOperationStatus deleteInstInputsToComponent(GraphVertex nodeTypeVertex, Map<String, MapPropertiesDataDefinition> instInputs) {
290
291                 if (instInputs != null && !instInputs.isEmpty()) {
292                         instInputs.entrySet().forEach(i -> {
293                                 List<String> uniqueKeys = new ArrayList<String>(i.getValue().getMapToscaDataDefinition().keySet());
294                                 List<String> pathKeys = new ArrayList<String>();
295                                 pathKeys.add(i.getKey());
296
297                                 StorageOperationStatus status = deleteToscaDataDeepElements(nodeTypeVertex, EdgeLabelEnum.INST_INPUTS, VertexTypeEnum.INST_INPUTS, uniqueKeys, pathKeys, JsonPresentationFields.NAME);
298                                 if (status != StorageOperationStatus.OK) {
299                                         return;
300                                 }
301                         });
302                 }
303
304                 return StorageOperationStatus.OK;
305         }
306
307         public StorageOperationStatus addInstPropertiesToComponent(GraphVertex nodeTypeVertex, Map<String, MapPropertiesDataDefinition> instInputs) {
308
309                 if (instInputs != null && !instInputs.isEmpty()) {
310                         instInputs.entrySet().forEach(i -> {
311                                 StorageOperationStatus status = addToscaDataDeepElementsBlockToToscaElement(nodeTypeVertex, EdgeLabelEnum.INST_PROPERTIES, VertexTypeEnum.INST_PROPERTIES, i.getValue(), i.getKey());
312                                 if (status != StorageOperationStatus.OK) {
313                                         return;
314                                 }
315                         });
316                 }
317
318                 return StorageOperationStatus.OK;
319         }
320
321         public StorageOperationStatus associateInstArtifactToComponent(GraphVertex nodeTypeVertex, Map<String, MapArtifactDataDefinition> instProps) {
322                 if (instProps != null && !instProps.isEmpty()) {
323                         Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.INST_DEPLOYMENT_ARTIFACTS, EdgeLabelEnum.INST_DEPLOYMENT_ARTIFACTS, instProps);
324                         if (assosiateElementToData.isRight()) {
325                                 return assosiateElementToData.right().value();
326                         }
327                 }
328                 return StorageOperationStatus.OK;
329         }
330
331         public StorageOperationStatus associateCalcCapReqToComponent(GraphVertex nodeTypeVertex, Map<String, MapListRequirementDataDefinition> calcRequirements, Map<String, MapListCapabiltyDataDefinition> calcCapabilty, Map<String, MapCapabiltyProperty> calculatedCapabilitiesProperties) {
332                 if (calcRequirements != null && !calcRequirements.isEmpty()) {
333                         Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.CALCULATED_REQUIREMENTS, EdgeLabelEnum.CALCULATED_REQUIREMENTS, calcRequirements);
334                         if (assosiateElementToData.isRight()) {
335                                 return assosiateElementToData.right().value();
336                         }
337                         Map<String, MapListRequirementDataDefinition> fullFilled = new HashMap<>();
338                         assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.FULLFILLED_REQUIREMENTS, EdgeLabelEnum.FULLFILLED_REQUIREMENTS, fullFilled);
339                         if (assosiateElementToData.isRight()) {
340                                 return assosiateElementToData.right().value();
341                         }
342                 }
343                 if (calcCapabilty != null && !calcCapabilty.isEmpty()) {
344                         Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.CALCULATED_CAPABILITIES, EdgeLabelEnum.CALCULATED_CAPABILITIES, calcCapabilty);
345                         if (assosiateElementToData.isRight()) {
346                                 return assosiateElementToData.right().value();
347                         }
348                         Map<String, MapListCapabiltyDataDefinition> fullFilled = new HashMap<>();
349                         assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.FULLFILLED_CAPABILITIES, EdgeLabelEnum.FULLFILLED_CAPABILITIES, fullFilled);
350                         if (assosiateElementToData.isRight()) {
351                                 return assosiateElementToData.right().value();
352                         }
353                 }
354                 if ( calculatedCapabilitiesProperties != null && !calculatedCapabilitiesProperties.isEmpty() ){
355                         Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.CALCULATED_CAP_PROPERTIES, EdgeLabelEnum.CALCULATED_CAP_PROPERTIES, calculatedCapabilitiesProperties);
356                         if (assosiateElementToData.isRight()) {
357                                 return assosiateElementToData.right().value();
358                         }
359                 }
360                 return StorageOperationStatus.OK;
361         }
362
363         private StorageOperationStatus associateInstAttributesToComponent(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
364                 Map<String, MapAttributesDataDefinition> instAttr = topologyTemplate.getInstAttributes();
365                 return associateInstAttributeToComponent(nodeTypeVertex, instAttr);
366         }
367
368         public StorageOperationStatus associateInstAttributeToComponent(GraphVertex nodeTypeVertex, Map<String, MapAttributesDataDefinition> instAttr) {
369                 if (instAttr != null && !instAttr.isEmpty()) {
370                         Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.INST_ATTRIBUTES, EdgeLabelEnum.INST_ATTRIBUTES, instAttr);
371                         if (assosiateElementToData.isRight()) {
372                                 return assosiateElementToData.right().value();
373                         }
374                 }
375                 return StorageOperationStatus.OK;
376         }
377
378         public StorageOperationStatus associateGroupsToComponent(GraphVertex nodeTypeVertex, Map<String, GroupDataDefinition> groups) {
379
380                 if (groups != null && !groups.isEmpty()) {
381                         groups.values().stream().filter(p -> p.getUniqueId() == null).forEach(p -> {
382                                 String uid = UniqueIdBuilder.buildGroupingUid(nodeTypeVertex.getUniqueId(), p.getName());
383                                 p.setUniqueId(uid);
384                         });
385                         Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.GROUPS, EdgeLabelEnum.GROUPS, groups);
386                         if (assosiateElementToData.isRight()) {
387                                 return assosiateElementToData.right().value();
388                         }
389                 }
390                 return StorageOperationStatus.OK;
391         }
392
393         private StorageOperationStatus associateGroupsToComponent(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
394                 return associateGroupsToComponent(nodeTypeVertex, topologyTemplate.getGroups());
395         }
396
397         public StorageOperationStatus associateInputsToComponent(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
398                 Map<String, PropertyDataDefinition> inputs = topologyTemplate.getInputs();
399                 return associateInputsToComponent(nodeTypeVertex, inputs, topologyTemplate.getUniqueId());
400         }
401
402         public StorageOperationStatus associateInputsToComponent(GraphVertex nodeTypeVertex, Map<String, PropertyDataDefinition> inputs, String id) {
403                 if (inputs != null && !inputs.isEmpty()) {
404                         inputs.values().stream().filter(e -> e.getUniqueId() == null).forEach(e -> e.setUniqueId(UniqueIdBuilder.buildPropertyUniqueId(id, e.getName())));
405
406                         Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.INPUTS, EdgeLabelEnum.INPUTS, inputs);
407                         if (assosiateElementToData.isRight()) {
408                                 return assosiateElementToData.right().value();
409                         }
410                 }
411                 return StorageOperationStatus.OK;
412         }
413
414         private GraphVertex fillMetadata(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate, JsonParseFlagEnum flag) {
415                 nodeTypeVertex.setLabel(VertexTypeEnum.TOPOLOGY_TEMPLATE);
416                 fillCommonMetadata(nodeTypeVertex, topologyTemplate);
417                 if (flag == JsonParseFlagEnum.ParseAll || flag == JsonParseFlagEnum.ParseJson) {
418                         nodeTypeVertex.setJson(topologyTemplate.getCompositions());
419                 }
420                 nodeTypeVertex.addMetadataProperty(GraphPropertyEnum.CSAR_UUID, topologyTemplate.getMetadataValue(JsonPresentationFields.CSAR_UUID));
421                 nodeTypeVertex.addMetadataProperty(GraphPropertyEnum.DISTRIBUTION_STATUS, topologyTemplate.getMetadataValue(JsonPresentationFields.DISTRIBUTION_STATUS));
422                 
423                 return nodeTypeVertex;
424
425         }
426
427         private StorageOperationStatus assosiateMetadataToCategory(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
428                 if (topologyTemplate.getResourceType() == null) {
429                         // service
430                         return associateServiceMetadataToCategory(nodeTypeVertex, topologyTemplate);
431                 } else {
432                         // VF
433                         return assosiateResourceMetadataToCategory(nodeTypeVertex, topologyTemplate);
434                 }
435         }
436
437         private StorageOperationStatus associateServiceMetadataToCategory(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
438                 String categoryName = topologyTemplate.getCategories().get(0).getName();
439                 Either<GraphVertex, StorageOperationStatus> category = categoryOperation.getCategory(categoryName, VertexTypeEnum.SERVICE_CATEGORY);
440                 if (category.isRight()) {
441                         log.trace("NO category {} for service {}", categoryName, topologyTemplate.getUniqueId());
442                         return StorageOperationStatus.CATEGORY_NOT_FOUND;
443                 }
444                 GraphVertex categoryV = category.left().value();
445                 TitanOperationStatus createEdge = titanDao.createEdge(nodeTypeVertex, categoryV, EdgeLabelEnum.CATEGORY, new HashMap<>());
446                 if (createEdge != TitanOperationStatus.OK) {
447                         log.trace("Failed to associate resource {} to category {} with id {}", topologyTemplate.getUniqueId(), categoryName, categoryV.getUniqueId());
448                         return DaoStatusConverter.convertTitanStatusToStorageStatus(createEdge);
449                 }
450                 return StorageOperationStatus.OK;
451         }
452
453         @Override
454         public Either<ToscaElement, StorageOperationStatus> getToscaElement(String uniqueId, ComponentParametersView componentParametersView) {
455                 JsonParseFlagEnum parseFlag = componentParametersView.detectParseFlag();
456
457                 Either<GraphVertex, StorageOperationStatus> componentByLabelAndId = getComponentByLabelAndId(uniqueId, ToscaElementTypeEnum.TopologyTemplate, parseFlag);
458                 if (componentByLabelAndId.isRight()) {
459                         return Either.right(componentByLabelAndId.right().value());
460                 }
461                 GraphVertex componentV = componentByLabelAndId.left().value();
462
463                 return getToscaElement(componentV, componentParametersView);
464
465         }
466         // -------------------------------------------------------------
467
468         public Either<ToscaElement, StorageOperationStatus> getToscaElement(GraphVertex componentV, ComponentParametersView componentParametersView) {
469                 TopologyTemplate toscaElement;
470
471                 toscaElement = convertToTopologyTemplate(componentV);
472                 TitanOperationStatus status = null;
473                 if (false == componentParametersView.isIgnoreUsers()) {
474                         status = setCreatorFromGraph(componentV, toscaElement);
475                         if (status != TitanOperationStatus.OK) {
476                                 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
477                         }
478
479                         status = setLastModifierFromGraph(componentV, toscaElement);
480                         if (status != TitanOperationStatus.OK) {
481                                 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
482                         }
483                 }
484                 if (false == componentParametersView.isIgnoreCategories()) {
485                         status = setTopologyTempalteCategoriesFromGraph(componentV, toscaElement);
486                         if (status != TitanOperationStatus.OK) {
487                                 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
488
489                         }
490                 }
491                 if (false == componentParametersView.isIgnoreArtifacts()) {
492                         TitanOperationStatus storageStatus = setAllArtifactsFromGraph(componentV, toscaElement);
493                         if (storageStatus != TitanOperationStatus.OK) {
494                                 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(storageStatus));
495                         }
496                 }
497                 if (false == componentParametersView.isIgnoreComponentInstancesProperties()) {
498                         status = setComponentInstancesPropertiesFromGraph(componentV, toscaElement);
499                         if (status != TitanOperationStatus.OK) {
500                                 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
501                         }
502                 }
503                 if (false == componentParametersView.isIgnoreCapabilities()) {
504                         status = setCapabilitiesFromGraph(componentV, toscaElement);
505                         if (status != TitanOperationStatus.OK) {
506                                 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
507                         }
508                 }
509                 if (false == componentParametersView.isIgnoreRequirements()) {
510                         status = setRequirementsFromGraph(componentV, toscaElement);
511                         if (status != TitanOperationStatus.OK) {
512                                 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
513                         }
514                 }
515                 if (false == componentParametersView.isIgnoreAllVersions()) {
516                         status = setAllVersions(componentV, toscaElement);
517                         if (status != TitanOperationStatus.OK) {
518                                 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
519                         }
520                 }
521                 if (false == componentParametersView.isIgnoreAdditionalInformation()) {
522                         status = setAdditionalInformationFromGraph(componentV, toscaElement);
523                         if (status != TitanOperationStatus.OK) {
524                                 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
525                         }
526                 }
527
528                 if (false == componentParametersView.isIgnoreGroups()) {
529                         status = setGroupsFromGraph(componentV, toscaElement);
530                         if (status != TitanOperationStatus.OK) {
531                                 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
532                         }
533
534                 }
535                 if (false == componentParametersView.isIgnoreComponentInstances()) {
536                         status = setInstGroupsFromGraph(componentV, toscaElement);
537                         if (status != TitanOperationStatus.OK) {
538                                 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
539                         }
540
541                 }
542                 if (false == componentParametersView.isIgnoreInputs()) {
543                         status = setInputsFromGraph(componentV, toscaElement);
544                         if (status != TitanOperationStatus.OK) {
545                                 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
546                         }
547
548                 }
549                 if (false == componentParametersView.isIgnoreProperties()) {
550                         status = setPropertiesFromGraph(componentV, toscaElement);
551                         if (status != TitanOperationStatus.OK) {
552                                 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
553                         }
554
555                 }
556
557                 if (false == componentParametersView.isIgnoreComponentInstancesInputs()) {
558                         status = setComponentInstancesInputsFromGraph(componentV, toscaElement);
559                         if (status != TitanOperationStatus.OK) {
560                                 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
561
562                         }
563                 }
564
565                 if (false == componentParametersView.isIgnoreCapabiltyProperties()) {
566                         status = setComponentInstancesCapPropertiesFromGraph(componentV, toscaElement);
567                         if (status != TitanOperationStatus.OK) {
568                                 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
569
570                         }
571                 }
572                 return Either.left(toscaElement);
573         }
574
575         private TitanOperationStatus setComponentInstancesCapPropertiesFromGraph(GraphVertex componentV, TopologyTemplate topologyTemplate) {
576                 Either<Map<String, MapCapabiltyProperty>, TitanOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.CALCULATED_CAP_PROPERTIES);
577                 if (result.isLeft()) {
578                         topologyTemplate.setCalculatedCapabilitiesProperties(result.left().value());
579                 } else {
580                         if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
581                                 return result.right().value();
582                         }
583                 }
584                 return TitanOperationStatus.OK;
585         }
586
587         private TitanOperationStatus setPropertiesFromGraph(GraphVertex componentV, TopologyTemplate toscaElement) {
588                 Either<Map<String, PropertyDataDefinition>, TitanOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.PROPERTIES);
589                 if (result.isLeft()) {
590                         toscaElement.setProperties(result.left().value());
591                 } else {
592                         if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
593                                 return result.right().value();
594                         }
595                 }
596                 return TitanOperationStatus.OK;
597         }
598
599         private TitanOperationStatus setInstGroupsFromGraph(GraphVertex componentV, TopologyTemplate topologyTemplate) {
600                 Either<Map<String, MapGroupsDataDefinition>, TitanOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.INST_GROUPS);
601                 if (result.isLeft()) {
602                         topologyTemplate.setInstGroups(result.left().value());
603                 } else {
604                         if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
605                                 return result.right().value();
606                         }
607                 }
608                 return TitanOperationStatus.OK;
609         }
610
611         private TitanOperationStatus setComponentInstancesPropertiesFromGraph(GraphVertex componentV, TopologyTemplate topologyTemplate) {
612                 Either<Map<String, MapPropertiesDataDefinition>, TitanOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.INST_PROPERTIES);
613                 if (result.isLeft()) {
614                         topologyTemplate.setInstProperties(result.left().value());
615                 } else {
616                         if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
617                                 return result.right().value();
618                         }
619                 }
620                 return TitanOperationStatus.OK;
621         }
622
623         private TitanOperationStatus setComponentInstancesInputsFromGraph(GraphVertex componentV, TopologyTemplate topologyTemplate) {
624                 Either<Map<String, MapPropertiesDataDefinition>, TitanOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.INST_INPUTS);
625                 if (result.isLeft()) {
626                         topologyTemplate.setInstInputs(result.left().value());
627                 } else {
628                         if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
629                                 return result.right().value();
630                         }
631                 }
632                 return TitanOperationStatus.OK;
633         }
634
635         @Override
636         protected <T extends ToscaElement> TitanOperationStatus setRequirementsFromGraph(GraphVertex componentV, T toscaElement) {
637                 Either<Map<String, MapListRequirementDataDefinition>, TitanOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.CALCULATED_REQUIREMENTS);
638                 if (result.isLeft()) {
639                         ((TopologyTemplate) toscaElement).setCalculatedRequirements(result.left().value());
640                 } else {
641                         if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
642                                 return result.right().value();
643                         }
644                 }
645                 result = getDataFromGraph(componentV, EdgeLabelEnum.FULLFILLED_REQUIREMENTS);
646                 if (result.isLeft()) {
647                         ((TopologyTemplate) toscaElement).setFullfilledRequirements(result.left().value());
648                 } else {
649                         if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
650                                 return result.right().value();
651                         }
652                 }
653                 return TitanOperationStatus.OK;
654
655         }
656
657         protected <T extends ToscaElement> TitanOperationStatus setCapabilitiesFromGraph(GraphVertex componentV, T toscaElement) {
658                 Either<Map<String, MapListCapabiltyDataDefinition>, TitanOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.CALCULATED_CAPABILITIES);
659                 if (result.isLeft()) {
660                         ((TopologyTemplate) toscaElement).setCalculatedCapabilities(result.left().value());
661                 } else {
662                         if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
663                                 return result.right().value();
664                         }
665                 }
666                 result = getDataFromGraph(componentV, EdgeLabelEnum.FULLFILLED_CAPABILITIES);
667                 if (result.isLeft()) {
668                         ((TopologyTemplate) toscaElement).setFullfilledCapabilities(result.left().value());
669                 } else {
670                         if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
671                                 return result.right().value();
672                         }
673                 }
674                 return TitanOperationStatus.OK;
675         }
676
677         private TitanOperationStatus setAllArtifactsFromGraph(GraphVertex componentV, TopologyTemplate toscaElement) {
678                 TitanOperationStatus storageStatus = setArtifactsFromGraph(componentV, toscaElement);
679                 if (storageStatus != TitanOperationStatus.OK) {
680                         return storageStatus;
681                 }
682                 Either<Map<String, ArtifactDataDefinition>, TitanOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.SERVICE_API_ARTIFACTS);
683                 if (result.isLeft()) {
684                         toscaElement.setServiceApiArtifacts(result.left().value());
685                 } else {
686                         if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
687                                 return result.right().value();
688                         }
689                 }
690                 Either<Map<String, MapArtifactDataDefinition>, TitanOperationStatus> resultInstArt = getDataFromGraph(componentV, EdgeLabelEnum.INST_DEPLOYMENT_ARTIFACTS);
691                 if (resultInstArt.isLeft()) {
692                         toscaElement.setInstDeploymentArtifacts(resultInstArt.left().value());
693                 } else {
694                         if (resultInstArt.right().value() != TitanOperationStatus.NOT_FOUND) {
695                                 return resultInstArt.right().value();
696                         }
697                 }
698                 Either<Map<String, MapArtifactDataDefinition>, TitanOperationStatus> instanceArt = getDataFromGraph(componentV, EdgeLabelEnum.INSTANCE_ARTIFACTS);
699                 if (instanceArt.isLeft()) {
700                         toscaElement.setInstanceArtifacts(instanceArt.left().value());
701                 } else {
702                         if (instanceArt.right().value() != TitanOperationStatus.NOT_FOUND) {
703                                 return instanceArt.right().value();
704                         }
705                 }
706                 return TitanOperationStatus.OK;
707         }
708
709         private TitanOperationStatus setInputsFromGraph(GraphVertex componentV, TopologyTemplate toscaElement) {
710                 Either<Map<String, PropertyDataDefinition>, TitanOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.INPUTS);
711                 if (result.isLeft()) {
712                         toscaElement.setInputs(result.left().value());
713                 } else {
714                         if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
715                                 return result.right().value();
716                         }
717                 }
718                 return TitanOperationStatus.OK;
719         }
720
721         private TitanOperationStatus setGroupsFromGraph(GraphVertex componentV, TopologyTemplate toscaElement) {
722                 Either<Map<String, GroupDataDefinition>, TitanOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.GROUPS);
723                 if (result.isLeft()) {
724                         toscaElement.setGroups(result.left().value());
725                 } else {
726                         if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
727                                 return result.right().value();
728                         }
729                 }
730                 return TitanOperationStatus.OK;
731         }
732
733         private TitanOperationStatus setTopologyTempalteCategoriesFromGraph(GraphVertex componentV, ToscaElement toscaElement) {
734                 List<CategoryDefinition> categories = new ArrayList<>();
735
736                 switch (componentV.getType()) {
737                 case RESOURCE:
738                         return setResourceCategoryFromGraph(componentV, toscaElement);
739                 case SERVICE:
740                         return setServiceCategoryFromGraph(componentV, toscaElement, categories);
741
742                 default:
743                         log.debug("Not supported component type {} ", componentV.getType());
744                         break;
745                 }
746                 return TitanOperationStatus.OK;
747         }
748
749         private TitanOperationStatus setServiceCategoryFromGraph(GraphVertex componentV, ToscaElement toscaElement, List<CategoryDefinition> categories) {
750                 Either<GraphVertex, TitanOperationStatus> childVertex = titanDao.getChildVertex(componentV, EdgeLabelEnum.CATEGORY, JsonParseFlagEnum.NoParse);
751                 if (childVertex.isRight()) {
752                         log.debug("failed to fetch {} for tosca element with id {}, error {}", EdgeLabelEnum.CATEGORY, componentV.getUniqueId(), childVertex.right().value());
753                         return childVertex.right().value();
754                 }
755                 GraphVertex categoryV = childVertex.left().value();
756                 Map<GraphPropertyEnum, Object> metadataProperties = categoryV.getMetadataProperties();
757                 CategoryDefinition category = new CategoryDefinition();
758                 category.setUniqueId((String) metadataProperties.get(GraphPropertyEnum.UNIQUE_ID));
759                 category.setNormalizedName((String) metadataProperties.get(GraphPropertyEnum.NORMALIZED_NAME));
760                 category.setName((String) metadataProperties.get(GraphPropertyEnum.NAME));
761
762                 Type listTypeCat = new TypeToken<List<String>>() {
763                 }.getType();
764                 List<String> iconsfromJsonCat = getGson().fromJson((String) metadataProperties.get(GraphPropertyEnum.ICONS.getProperty()), listTypeCat);
765                 category.setIcons(iconsfromJsonCat);
766                 categories.add(category);
767                 toscaElement.setCategories(categories);
768
769                 return TitanOperationStatus.OK;
770         }
771
772         private TopologyTemplate convertToTopologyTemplate(GraphVertex componentV) {
773
774                 TopologyTemplate topologyTemplate = super.convertToComponent(componentV);
775
776                 Map<String, CompositionDataDefinition> json = (Map<String, CompositionDataDefinition>) componentV.getJson();
777                 topologyTemplate.setCompositions(json);
778
779                 return topologyTemplate;
780         }
781
782         @Override
783         public Either<ToscaElement, StorageOperationStatus> deleteToscaElement(GraphVertex toscaElementVertex) {
784                 Either<ToscaElement, StorageOperationStatus> nodeType = getToscaElement(toscaElementVertex, new ComponentParametersView());
785                 if (nodeType.isRight()) {
786                         log.debug("Failed to fetch tosca element {} error {}", toscaElementVertex.getUniqueId(), nodeType.right().value());
787                         return nodeType;
788                 }
789                 TitanOperationStatus status = disassociateAndDeleteCommonElements(toscaElementVertex);
790                 if (status != TitanOperationStatus.OK) {
791                         Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
792                 }
793                 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.INST_ATTRIBUTES);
794                 if (status != TitanOperationStatus.OK) {
795                         log.debug("Failed to disassociate instances attributes for {} error {}", toscaElementVertex.getUniqueId(), status);
796                         Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
797                 }
798                 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.INST_PROPERTIES);
799                 if (status != TitanOperationStatus.OK) {
800                         log.debug("Failed to disassociate instances properties for {} error {}", toscaElementVertex.getUniqueId(), status);
801                         Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
802                 }
803
804                 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.INST_INPUTS);
805                 if (status != TitanOperationStatus.OK) {
806                         log.debug("Failed to disassociate instances inputs for {} error {}", toscaElementVertex.getUniqueId(), status);
807                         Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
808                 }
809
810                 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.GROUPS);
811                 if (status != TitanOperationStatus.OK) {
812                         log.debug("Failed to disassociate groups for {} error {}", toscaElementVertex.getUniqueId(), status);
813                         Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
814                 }
815                 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.INST_GROUPS);
816                 if (status != TitanOperationStatus.OK) {
817                         log.debug("Failed to disassociate instance groups for {} error {}", toscaElementVertex.getUniqueId(), status);
818                         Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
819                 }
820                 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.INPUTS);
821                 if (status != TitanOperationStatus.OK) {
822                         log.debug("Failed to disassociate inputs for {} error {}", toscaElementVertex.getUniqueId(), status);
823                         Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
824                 }
825                 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.INST_INPUTS);
826                 if (status != TitanOperationStatus.OK) {
827                         log.debug("Failed to disassociate instance inputs for {} error {}", toscaElementVertex.getUniqueId(), status);
828                         Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
829                 }
830                 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.CALCULATED_CAPABILITIES);
831                 if (status != TitanOperationStatus.OK) {
832                         log.debug("Failed to disassociate calculated capabiliites for {} error {}", toscaElementVertex.getUniqueId(), status);
833                         Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
834                 }
835                 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.FULLFILLED_CAPABILITIES);
836                 if (status != TitanOperationStatus.OK) {
837                         log.debug("Failed to disassociate fullfilled capabilities for {} error {}", toscaElementVertex.getUniqueId(), status);
838                         Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
839                 }
840                 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.CALCULATED_CAP_PROPERTIES);
841                 if (status != TitanOperationStatus.OK) {
842                         log.debug("Failed to disassociate calculated capabiliites properties for {} error {}", toscaElementVertex.getUniqueId(), status);
843                         Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
844                 }
845                 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.CALCULATED_REQUIREMENTS);
846                 if (status != TitanOperationStatus.OK) {
847                         log.debug("Failed to disassociate calculated requirements for {} error {}", toscaElementVertex.getUniqueId(), status);
848                         Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
849                 }
850                 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.FULLFILLED_REQUIREMENTS);
851                 if (status != TitanOperationStatus.OK) {
852                         log.debug("Failed to disassociate full filled requirements for {} error {}", toscaElementVertex.getUniqueId(), status);
853                         Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
854                 }
855                 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.INST_DEPLOYMENT_ARTIFACTS);
856                 if (status != TitanOperationStatus.OK) {
857                         log.debug("Failed to disassociate instance artifacts for {} error {}", toscaElementVertex.getUniqueId(), status);
858                         Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
859                 }
860                 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.SERVICE_API_ARTIFACTS);
861                 if (status != TitanOperationStatus.OK) {
862                         log.debug("Failed to disassociate service api artifacts for {} error {}", toscaElementVertex.getUniqueId(), status);
863                         Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
864                 }
865                 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.INSTANCE_ARTIFACTS);
866                 toscaElementVertex.getVertex().remove();
867                 log.trace("Tosca element vertex for {} was removed", toscaElementVertex.getUniqueId());
868
869                 return nodeType;
870         }
871
872         @SuppressWarnings("unchecked")
873         @Override
874         public Either<TopologyTemplate, StorageOperationStatus> createToscaElement(ToscaElement toscaElement) {
875                 return createTopologyTemplate((TopologyTemplate) toscaElement);
876         }
877
878         @Override
879         protected <T extends ToscaElement> TitanOperationStatus setCategoriesFromGraph(GraphVertex vertexComponent, T toscaElement) {
880                 return setTopologyTempalteCategoriesFromGraph(vertexComponent, toscaElement);
881         }
882
883         @Override
884         protected <T extends ToscaElement> StorageOperationStatus validateCategories(T toscaElementToUpdate, GraphVertex elementV) {
885                 // Product isn't supported now!!
886                 // TODO add for Product
887                 if (toscaElementToUpdate.getComponentType() == ComponentTypeEnum.SERVICE) {
888                         return validateServiceCategory(toscaElementToUpdate, elementV);
889                 } else {
890                         // Resource
891                         return validateResourceCategory(toscaElementToUpdate, elementV);
892                 }
893         }
894
895         @Override
896         protected <T extends ToscaElement> StorageOperationStatus updateDerived(T toscaElementToUpdate, GraphVertex updateElementV) {
897                 // not relevant now for topology template
898                 return StorageOperationStatus.OK;
899         }
900
901         @Override
902         public <T extends ToscaElement> void fillToscaElementVertexData(GraphVertex elementV, T toscaElementToUpdate, JsonParseFlagEnum flag) {
903                 fillMetadata(elementV, (TopologyTemplate) toscaElementToUpdate, flag);
904         }
905
906         private <T extends ToscaElement> StorageOperationStatus validateServiceCategory(T toscaElementToUpdate, GraphVertex elementV) {
907                 StorageOperationStatus status = StorageOperationStatus.OK;
908                 List<CategoryDefinition> newCategoryList = toscaElementToUpdate.getCategories();
909                 CategoryDefinition newCategory = newCategoryList.get(0);
910
911                 Either<GraphVertex, TitanOperationStatus> childVertex = titanDao.getChildVertex(elementV, EdgeLabelEnum.CATEGORY, JsonParseFlagEnum.NoParse);
912                 if (childVertex.isRight()) {
913                         log.debug("failed to fetch {} for tosca element with id {}, error {}", EdgeLabelEnum.CATEGORY, elementV.getUniqueId(), childVertex.right().value());
914                         return DaoStatusConverter.convertTitanStatusToStorageStatus(childVertex.right().value());
915                 }
916
917                 GraphVertex categoryV = childVertex.left().value();
918                 Map<GraphPropertyEnum, Object> metadataProperties = categoryV.getMetadataProperties();
919                 String categoryNameCurrent = (String) metadataProperties.get(GraphPropertyEnum.NAME);
920
921                 String newCategoryName = newCategory.getName();
922                 if (newCategoryName != null && false == newCategoryName.equals(categoryNameCurrent)) {
923                         // the category was changed
924                         Either<GraphVertex, StorageOperationStatus> getCategoryVertex = categoryOperation.getCategory(newCategoryName, VertexTypeEnum.SERVICE_CATEGORY);
925
926                         if (getCategoryVertex.isRight()) {
927                                 return getCategoryVertex.right().value();
928                         }
929                         GraphVertex newCategoryV = getCategoryVertex.left().value();
930                         status = moveCategoryEdge(elementV, newCategoryV);
931                         log.debug("Going to update the category of the resource from {} to {}. status is {}", categoryNameCurrent, newCategory, status);
932                 }
933                 return status;
934         }
935
936         public Either<List<GraphVertex>, TitanOperationStatus> getAllNotDeletedElements() {
937                 Map<GraphPropertyEnum, Object> propsHasNot = new HashMap<>();
938                 propsHasNot.put(GraphPropertyEnum.IS_DELETED, true);
939
940                 Either<List<GraphVertex>, TitanOperationStatus> byCriteria = titanDao.getByCriteria(VertexTypeEnum.TOPOLOGY_TEMPLATE, null, propsHasNot, JsonParseFlagEnum.ParseJson);
941                 if (byCriteria.isRight()) {
942                         log.debug("Failed to fetch all non marked topology templates , propsHasNot {}, error {}", propsHasNot, byCriteria.right().value());
943                         return Either.right(byCriteria.right().value());
944                 }
945                 return Either.left(byCriteria.left().value());
946         }
947
948         public boolean isInUse(GraphVertex elementV, List<GraphVertex> allNonDeleted) {
949                 for (GraphVertex containerV : allNonDeleted) {
950                         Map<String, CompositionDataDefinition> composition = (Map<String, CompositionDataDefinition>) containerV.getJson();
951                         if (composition != null) {
952                                 CompositionDataDefinition instances = composition.get(JsonConstantKeysEnum.COMPOSITION.getValue());
953                                 if (instances != null && instances.getComponentInstances() != null && !instances.getComponentInstances().isEmpty()) {
954                                         for (ComponentInstanceDataDefinition ci : instances.getComponentInstances().values()) {
955                                                 if (ci.getComponentUid().equals(elementV.getUniqueId())) {
956                                                         return true;
957                                                 }
958                                         }
959
960                                 }
961                         }
962                 }
963
964                 return false;
965         }
966
967         public boolean isInUse(String componentId, List<GraphVertex> allNonDeleted) {
968                 for (GraphVertex containerV : allNonDeleted) {
969                         Map<String, CompositionDataDefinition> composition = (Map<String, CompositionDataDefinition>) containerV.getJson();
970                         if (composition != null) {
971                                 CompositionDataDefinition instances = composition.get(JsonConstantKeysEnum.COMPOSITION.getValue());
972                                 if (instances != null && instances.getComponentInstances() != null && !instances.getComponentInstances().isEmpty()) {
973                                         for (ComponentInstanceDataDefinition ci : instances.getComponentInstances().values()) {
974                                                 if (ci.getComponentUid().equals(componentId)) {
975                                                         return true;
976                                                 }
977                                         }
978
979                                 }
980                         }
981                 }
982
983                 return false;
984         }
985
986         public Either<GraphVertex, StorageOperationStatus> updateDistributionStatus(String uniqueId, User user, DistributionStatusEnum distributionStatus) {
987
988                 Either<GraphVertex, StorageOperationStatus> result = null;
989                 String userId = user.getUserId();
990                 Either<GraphVertex, TitanOperationStatus> getRes = findUserVertex(userId);
991                 GraphVertex userVertex = null;
992                 GraphVertex serviceVertex = null;
993                 if (getRes.isRight()) {
994                         TitanOperationStatus status = getRes.right().value();
995                         CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Cannot find user {} in the graph. status is {}", userId, status);
996                         result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
997                 }
998                 if (result == null) {
999                         userVertex = getRes.left().value();
1000                         getRes = titanDao.getVertexById(uniqueId, JsonParseFlagEnum.ParseMetadata);
1001                         if (getRes.isRight()) {
1002                                 TitanOperationStatus status = getRes.right().value();
1003                                 log.error("Cannot find service {} in the graph. status is {}", uniqueId, status);
1004                                 result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
1005                         }
1006                 }
1007                 if (result == null) {
1008                         serviceVertex = getRes.left().value();
1009                         Iterator<Edge> edgeIterator = serviceVertex.getVertex().edges(Direction.IN, EdgeLabelEnum.LAST_DISTRIBUTION_STATE_MODIFIER.name());
1010                         if (edgeIterator.hasNext()) {
1011                                 log.debug("Remove existing edge from user to component {}. Edge type is {}", userId, uniqueId, EdgeLabelEnum.LAST_DISTRIBUTION_STATE_MODIFIER);
1012                                 edgeIterator.next().remove();
1013                         }
1014                 }
1015                 if (result == null) {
1016                         TitanOperationStatus status = titanDao.createEdge(userVertex, serviceVertex, EdgeLabelEnum.LAST_DISTRIBUTION_STATE_MODIFIER, null);
1017                         if (status != TitanOperationStatus.OK) {
1018                                 log.error("Failed to associate user {} to component {}. Edge type is {}", userId, uniqueId, EdgeLabelEnum.LAST_DISTRIBUTION_STATE_MODIFIER);
1019                                 result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
1020                         }
1021                 }
1022                 if (result == null) {
1023                         serviceVertex.addMetadataProperty(GraphPropertyEnum.DISTRIBUTION_STATUS, distributionStatus.name());
1024                         long lastUpdateDate = System.currentTimeMillis();
1025                         serviceVertex.setJsonMetadataField(JsonPresentationFields.LAST_UPDATE_DATE, lastUpdateDate);
1026                         Either<GraphVertex, TitanOperationStatus> updateRes = titanDao.updateVertex(serviceVertex);
1027                         if (updateRes.isRight()) {
1028                                 result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(updateRes.right().value()));
1029                         }
1030                 }
1031                 if (result == null) {
1032                         result = Either.left(serviceVertex);
1033                 }
1034                 return result;
1035         }
1036
1037 }