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