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