989707bf0cbbcd3cb8a91f09b784ba2a0b0f7929
[sdc.git] /
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 associateInstDeploymentArtifactsToComponent(GraphVertex nodeTypeVertex, Map<String, MapArtifactDataDefinition> instArtifacts) {
353                 return associateInstanceArtifactsToComponent(nodeTypeVertex, instArtifacts, VertexTypeEnum.INST_DEPLOYMENT_ARTIFACTS, EdgeLabelEnum.INST_DEPLOYMENT_ARTIFACTS);
354         }
355         
356         public StorageOperationStatus associateInstArtifactsToComponent(GraphVertex nodeTypeVertex, Map<String, MapArtifactDataDefinition> instArtifacts) {
357                 return associateInstanceArtifactsToComponent(nodeTypeVertex, instArtifacts, VertexTypeEnum.INSTANCE_ARTIFACTS, EdgeLabelEnum.INSTANCE_ARTIFACTS);
358         }
359
360         private StorageOperationStatus associateInstanceArtifactsToComponent(GraphVertex nodeTypeVertex, Map<String, MapArtifactDataDefinition> instProps, VertexTypeEnum vertexType, EdgeLabelEnum edgeLabel) {
361                 if (instProps != null && !instProps.isEmpty()) {
362                         Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, vertexType, edgeLabel, instProps);
363                         if (assosiateElementToData.isRight()) {
364                                 return assosiateElementToData.right().value();
365                         }
366                 }
367                 return StorageOperationStatus.OK;
368         }
369
370         public StorageOperationStatus associateCalcCapReqToComponent(GraphVertex nodeTypeVertex, Map<String, MapListRequirementDataDefinition> calcRequirements, Map<String, MapListCapabiltyDataDefinition> calcCapabilty, Map<String, MapCapabiltyProperty> calculatedCapabilitiesProperties) {
371                 if (calcRequirements != null && !calcRequirements.isEmpty()) {
372                         Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.CALCULATED_REQUIREMENTS, EdgeLabelEnum.CALCULATED_REQUIREMENTS, calcRequirements);
373                         if (assosiateElementToData.isRight()) {
374                                 return assosiateElementToData.right().value();
375                         }
376                         Map<String, MapListRequirementDataDefinition> fullFilled = new HashMap<>();
377                         assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.FULLFILLED_REQUIREMENTS, EdgeLabelEnum.FULLFILLED_REQUIREMENTS, fullFilled);
378                         if (assosiateElementToData.isRight()) {
379                                 return assosiateElementToData.right().value();
380                         }
381                 }
382                 if (calcCapabilty != null && !calcCapabilty.isEmpty()) {
383                         Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.CALCULATED_CAPABILITIES, EdgeLabelEnum.CALCULATED_CAPABILITIES, calcCapabilty);
384                         if (assosiateElementToData.isRight()) {
385                                 return assosiateElementToData.right().value();
386                         }
387                         Map<String, MapListCapabiltyDataDefinition> fullFilled = new HashMap<>();
388                         assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.FULLFILLED_CAPABILITIES, EdgeLabelEnum.FULLFILLED_CAPABILITIES, fullFilled);
389                         if (assosiateElementToData.isRight()) {
390                                 return assosiateElementToData.right().value();
391                         }
392                 }
393                 if ( calculatedCapabilitiesProperties != null && !calculatedCapabilitiesProperties.isEmpty() ){
394                         Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.CALCULATED_CAP_PROPERTIES, EdgeLabelEnum.CALCULATED_CAP_PROPERTIES, calculatedCapabilitiesProperties);
395                         if (assosiateElementToData.isRight()) {
396                                 return assosiateElementToData.right().value();
397                         }
398                 }
399                 return StorageOperationStatus.OK;
400         }
401
402         private StorageOperationStatus associateInstAttributesToComponent(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
403                 Map<String, MapPropertiesDataDefinition> instAttr = topologyTemplate.getInstAttributes();
404                 return associateInstAttributeToComponent(nodeTypeVertex, instAttr);
405         }
406
407         public StorageOperationStatus associateInstAttributeToComponent(GraphVertex nodeTypeVertex, Map<String, MapPropertiesDataDefinition> instAttr) {
408                 if (instAttr != null && !instAttr.isEmpty()) {
409                         Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.INST_ATTRIBUTES, EdgeLabelEnum.INST_ATTRIBUTES, instAttr);
410                         if (assosiateElementToData.isRight()) {
411                                 return assosiateElementToData.right().value();
412                         }
413                 }
414                 return StorageOperationStatus.OK;
415         }
416
417         public StorageOperationStatus associateGroupsToComponent(GraphVertex nodeTypeVertex, Map<String, GroupDataDefinition> groups) {
418
419                 if (groups != null && !groups.isEmpty()) {
420                         groups.values().stream().filter(p -> p.getUniqueId() == null).forEach(p -> {
421                                 String uid = UniqueIdBuilder.buildGroupingUid(nodeTypeVertex.getUniqueId(), p.getName());
422                                 p.setUniqueId(uid);
423                         });
424                         Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.GROUPS, EdgeLabelEnum.GROUPS, groups);
425                         if (assosiateElementToData.isRight()) {
426                                 return assosiateElementToData.right().value();
427                         }
428                 }
429                 return StorageOperationStatus.OK;
430         }
431
432         private StorageOperationStatus associateGroupsToComponent(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
433                 return associateGroupsToComponent(nodeTypeVertex, topologyTemplate.getGroups());
434         }
435
436         public StorageOperationStatus associateInputsToComponent(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
437                 Map<String, PropertyDataDefinition> inputs = topologyTemplate.getInputs();
438                 return associateInputsToComponent(nodeTypeVertex, inputs, topologyTemplate.getUniqueId());
439         }
440
441         public StorageOperationStatus associateInputsToComponent(GraphVertex nodeTypeVertex, Map<String, PropertyDataDefinition> inputs, String id) {
442                 if (inputs != null && !inputs.isEmpty()) {
443                         inputs.values().stream().filter(e -> e.getUniqueId() == null).forEach(e -> e.setUniqueId(UniqueIdBuilder.buildPropertyUniqueId(id, e.getName())));
444
445                         Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.INPUTS, EdgeLabelEnum.INPUTS, inputs);
446                         if (assosiateElementToData.isRight()) {
447                                 return assosiateElementToData.right().value();
448                         }
449                 }
450                 return StorageOperationStatus.OK;
451         }
452
453         private GraphVertex fillMetadata(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate, JsonParseFlagEnum flag) {
454                 nodeTypeVertex.setLabel(VertexTypeEnum.TOPOLOGY_TEMPLATE);
455                 fillCommonMetadata(nodeTypeVertex, topologyTemplate);
456                 if (flag == JsonParseFlagEnum.ParseAll || flag == JsonParseFlagEnum.ParseJson) {
457                         nodeTypeVertex.setJson(topologyTemplate.getCompositions());
458                 }
459                 nodeTypeVertex.addMetadataProperty(GraphPropertyEnum.CSAR_UUID, topologyTemplate.getMetadataValue(JsonPresentationFields.CSAR_UUID));
460                 nodeTypeVertex.addMetadataProperty(GraphPropertyEnum.DISTRIBUTION_STATUS, topologyTemplate.getMetadataValue(JsonPresentationFields.DISTRIBUTION_STATUS));
461                 
462                 return nodeTypeVertex;
463
464         }
465
466         private StorageOperationStatus assosiateMetadataToCategory(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
467                 if (topologyTemplate.getResourceType() == null) {
468                         // service
469                         return associateServiceMetadataToCategory(nodeTypeVertex, topologyTemplate);
470                 } else {
471                         // VF
472                         return assosiateResourceMetadataToCategory(nodeTypeVertex, topologyTemplate);
473                 }
474         }
475
476         private StorageOperationStatus associateServiceMetadataToCategory(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
477                 String categoryName = topologyTemplate.getCategories().get(0).getName();
478                 Either<GraphVertex, StorageOperationStatus> category = categoryOperation.getCategory(categoryName, VertexTypeEnum.SERVICE_CATEGORY);
479                 if (category.isRight()) {
480                         log.trace("NO category {} for service {}", categoryName, topologyTemplate.getUniqueId());
481                         return StorageOperationStatus.CATEGORY_NOT_FOUND;
482                 }
483                 GraphVertex categoryV = category.left().value();
484                 TitanOperationStatus createEdge = titanDao.createEdge(nodeTypeVertex, categoryV, EdgeLabelEnum.CATEGORY, new HashMap<>());
485                 if (createEdge != TitanOperationStatus.OK) {
486                         log.trace("Failed to associate resource {} to category {} with id {}", topologyTemplate.getUniqueId(), categoryName, categoryV.getUniqueId());
487                         return DaoStatusConverter.convertTitanStatusToStorageStatus(createEdge);
488                 }
489                 return StorageOperationStatus.OK;
490         }
491
492         @Override
493         public Either<ToscaElement, StorageOperationStatus> getToscaElement(String uniqueId, ComponentParametersView componentParametersView) {
494                 JsonParseFlagEnum parseFlag = componentParametersView.detectParseFlag();
495
496                 Either<GraphVertex, StorageOperationStatus> componentByLabelAndId = getComponentByLabelAndId(uniqueId, ToscaElementTypeEnum.TopologyTemplate, parseFlag);
497                 if (componentByLabelAndId.isRight()) {
498                         return Either.right(componentByLabelAndId.right().value());
499                 }
500                 GraphVertex componentV = componentByLabelAndId.left().value();
501
502                 return getToscaElement(componentV, componentParametersView);
503
504         }
505         // -------------------------------------------------------------
506
507         public Either<ToscaElement, StorageOperationStatus> getToscaElement(GraphVertex componentV, ComponentParametersView componentParametersView) {
508                 TopologyTemplate toscaElement;
509
510                 toscaElement = convertToTopologyTemplate(componentV);
511                 TitanOperationStatus status = null;
512                 if (false == componentParametersView.isIgnoreUsers()) {
513                         status = setCreatorFromGraph(componentV, toscaElement);
514                         if (status != TitanOperationStatus.OK) {
515                                 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
516                         }
517
518                         status = setLastModifierFromGraph(componentV, toscaElement);
519                         if (status != TitanOperationStatus.OK) {
520                                 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
521                         }
522                 }
523                 if (false == componentParametersView.isIgnoreCategories()) {
524                         status = setTopologyTempalteCategoriesFromGraph(componentV, toscaElement);
525                         if (status != TitanOperationStatus.OK) {
526                                 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
527
528                         }
529                 }
530                 if (false == componentParametersView.isIgnoreArtifacts()) {
531                         TitanOperationStatus storageStatus = setAllArtifactsFromGraph(componentV, toscaElement);
532                         if (storageStatus != TitanOperationStatus.OK) {
533                                 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(storageStatus));
534                         }
535                 }
536                 if (false == componentParametersView.isIgnoreComponentInstancesProperties()) {
537                         status = setComponentInstancesPropertiesFromGraph(componentV, toscaElement);
538                         if (status != TitanOperationStatus.OK) {
539                                 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
540                         }
541                 }
542                 if (false == componentParametersView.isIgnoreCapabilities()) {
543                         status = setCapabilitiesFromGraph(componentV, toscaElement);
544                         if (status != TitanOperationStatus.OK) {
545                                 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
546                         }
547                 }
548                 if (false == componentParametersView.isIgnoreRequirements()) {
549                         status = setRequirementsFromGraph(componentV, toscaElement);
550                         if (status != TitanOperationStatus.OK) {
551                                 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
552                         }
553                 }
554                 if (false == componentParametersView.isIgnoreAllVersions()) {
555                         status = setAllVersions(componentV, toscaElement);
556                         if (status != TitanOperationStatus.OK) {
557                                 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
558                         }
559                 }
560                 if (false == componentParametersView.isIgnoreAdditionalInformation()) {
561                         status = setAdditionalInformationFromGraph(componentV, toscaElement);
562                         if (status != TitanOperationStatus.OK) {
563                                 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
564                         }
565                 }
566
567                 if (false == componentParametersView.isIgnoreGroups()) {
568                         status = setGroupsFromGraph(componentV, toscaElement);
569                         if (status != TitanOperationStatus.OK) {
570                                 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
571                         }
572
573                 }
574                 if (false == componentParametersView.isIgnoreComponentInstances()) {
575                         status = setInstGroupsFromGraph(componentV, toscaElement);
576                         if (status != TitanOperationStatus.OK) {
577                                 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
578                         }
579
580                 }
581                 if (false == componentParametersView.isIgnoreInputs()) {
582                         status = setInputsFromGraph(componentV, toscaElement);
583                         if (status != TitanOperationStatus.OK) {
584                                 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
585                         }
586
587                 }
588                 if (false == componentParametersView.isIgnoreProperties()) {
589                         status = setPropertiesFromGraph(componentV, toscaElement);
590                         if (status != TitanOperationStatus.OK) {
591                                 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
592                         }
593
594                 }
595
596                 if (false == componentParametersView.isIgnoreComponentInstancesInputs()) {
597                         status = setComponentInstancesInputsFromGraph(componentV, toscaElement);
598                         if (status != TitanOperationStatus.OK) {
599                                 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
600
601                         }
602                 }
603
604                 if (false == componentParametersView.isIgnoreCapabiltyProperties()) {
605                         status = setComponentInstancesCapPropertiesFromGraph(componentV, toscaElement);
606                         if (status != TitanOperationStatus.OK) {
607                                 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
608
609                         }
610                 }
611                 return Either.left(toscaElement);
612         }
613
614         private TitanOperationStatus setComponentInstancesCapPropertiesFromGraph(GraphVertex componentV, TopologyTemplate topologyTemplate) {
615                 Either<Map<String, MapCapabiltyProperty>, TitanOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.CALCULATED_CAP_PROPERTIES);
616                 if (result.isLeft()) {
617                         topologyTemplate.setCalculatedCapabilitiesProperties(result.left().value());
618                 } else {
619                         if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
620                                 return result.right().value();
621                         }
622                 }
623                 return TitanOperationStatus.OK;
624         }
625
626         private TitanOperationStatus setPropertiesFromGraph(GraphVertex componentV, TopologyTemplate toscaElement) {
627                 Either<Map<String, PropertyDataDefinition>, TitanOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.PROPERTIES);
628                 if (result.isLeft()) {
629                         toscaElement.setProperties(result.left().value());
630                 } else {
631                         if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
632                                 return result.right().value();
633                         }
634                 }
635                 return TitanOperationStatus.OK;
636         }
637
638         private TitanOperationStatus setInstGroupsFromGraph(GraphVertex componentV, TopologyTemplate topologyTemplate) {
639                 Either<Map<String, MapGroupsDataDefinition>, TitanOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.INST_GROUPS);
640                 if (result.isLeft()) {
641                         topologyTemplate.setInstGroups(result.left().value());
642                 } else {
643                         if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
644                                 return result.right().value();
645                         }
646                 }
647                 return TitanOperationStatus.OK;
648         }
649
650         private TitanOperationStatus setComponentInstancesPropertiesFromGraph(GraphVertex componentV, TopologyTemplate topologyTemplate) {
651                 Either<Map<String, MapPropertiesDataDefinition>, TitanOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.INST_PROPERTIES);
652                 if (result.isLeft()) {
653                         topologyTemplate.setInstProperties(result.left().value());
654                 } else {
655                         if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
656                                 return result.right().value();
657                         }
658                 }
659                 return TitanOperationStatus.OK;
660         }
661
662         private TitanOperationStatus setComponentInstancesInputsFromGraph(GraphVertex componentV, TopologyTemplate topologyTemplate) {
663                 Either<Map<String, MapPropertiesDataDefinition>, TitanOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.INST_INPUTS);
664                 if (result.isLeft()) {
665                         topologyTemplate.setInstInputs(result.left().value());
666                 } else {
667                         if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
668                                 return result.right().value();
669                         }
670                 }
671                 return TitanOperationStatus.OK;
672         }
673
674         @Override
675         protected <T extends ToscaElement> TitanOperationStatus setRequirementsFromGraph(GraphVertex componentV, T toscaElement) {
676                 Either<Map<String, MapListRequirementDataDefinition>, TitanOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.CALCULATED_REQUIREMENTS);
677                 if (result.isLeft()) {
678                         ((TopologyTemplate) toscaElement).setCalculatedRequirements(result.left().value());
679                 } else {
680                         if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
681                                 return result.right().value();
682                         }
683                 }
684                 result = getDataFromGraph(componentV, EdgeLabelEnum.FULLFILLED_REQUIREMENTS);
685                 if (result.isLeft()) {
686                         ((TopologyTemplate) toscaElement).setFullfilledRequirements(result.left().value());
687                 } else {
688                         if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
689                                 return result.right().value();
690                         }
691                 }
692                 return TitanOperationStatus.OK;
693
694         }
695
696         protected <T extends ToscaElement> TitanOperationStatus setCapabilitiesFromGraph(GraphVertex componentV, T toscaElement) {
697                 Either<Map<String, MapListCapabiltyDataDefinition>, TitanOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.CALCULATED_CAPABILITIES);
698                 if (result.isLeft()) {
699                         ((TopologyTemplate) toscaElement).setCalculatedCapabilities(result.left().value());
700                 } else {
701                         if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
702                                 return result.right().value();
703                         }
704                 }
705                 result = getDataFromGraph(componentV, EdgeLabelEnum.FULLFILLED_CAPABILITIES);
706                 if (result.isLeft()) {
707                         ((TopologyTemplate) toscaElement).setFullfilledCapabilities(result.left().value());
708                 } else {
709                         if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
710                                 return result.right().value();
711                         }
712                 }
713                 return TitanOperationStatus.OK;
714         }
715
716         private TitanOperationStatus setAllArtifactsFromGraph(GraphVertex componentV, TopologyTemplate toscaElement) {
717                 TitanOperationStatus storageStatus = setArtifactsFromGraph(componentV, toscaElement);
718                 if (storageStatus != TitanOperationStatus.OK) {
719                         return storageStatus;
720                 }
721                 Either<Map<String, ArtifactDataDefinition>, TitanOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.SERVICE_API_ARTIFACTS);
722                 if (result.isLeft()) {
723                         toscaElement.setServiceApiArtifacts(result.left().value());
724                 } else {
725                         if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
726                                 return result.right().value();
727                         }
728                 }
729                 Either<Map<String, MapArtifactDataDefinition>, TitanOperationStatus> resultInstArt = getDataFromGraph(componentV, EdgeLabelEnum.INST_DEPLOYMENT_ARTIFACTS);
730                 if (resultInstArt.isLeft()) {
731                         toscaElement.setInstDeploymentArtifacts(resultInstArt.left().value());
732                 } else {
733                         if (resultInstArt.right().value() != TitanOperationStatus.NOT_FOUND) {
734                                 return resultInstArt.right().value();
735                         }
736                 }
737                 Either<Map<String, MapArtifactDataDefinition>, TitanOperationStatus> instanceArt = getDataFromGraph(componentV, EdgeLabelEnum.INSTANCE_ARTIFACTS);
738                 if (instanceArt.isLeft()) {
739                         toscaElement.setInstanceArtifacts(instanceArt.left().value());
740                 } else {
741                         if (instanceArt.right().value() != TitanOperationStatus.NOT_FOUND) {
742                                 return instanceArt.right().value();
743                         }
744                 }
745                 return TitanOperationStatus.OK;
746         }
747
748         private TitanOperationStatus setInputsFromGraph(GraphVertex componentV, TopologyTemplate toscaElement) {
749                 Either<Map<String, PropertyDataDefinition>, TitanOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.INPUTS);
750                 if (result.isLeft()) {
751                         toscaElement.setInputs(result.left().value());
752                 } else {
753                         if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
754                                 return result.right().value();
755                         }
756                 }
757                 return TitanOperationStatus.OK;
758         }
759
760         private TitanOperationStatus setGroupsFromGraph(GraphVertex componentV, TopologyTemplate toscaElement) {
761                 Either<Map<String, GroupDataDefinition>, TitanOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.GROUPS);
762                 if (result.isLeft()) {
763                         toscaElement.setGroups(result.left().value());
764                 } else {
765                         if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
766                                 return result.right().value();
767                         }
768                 }
769                 return TitanOperationStatus.OK;
770         }
771
772         private TitanOperationStatus setTopologyTempalteCategoriesFromGraph(GraphVertex componentV, ToscaElement toscaElement) {
773                 List<CategoryDefinition> categories = new ArrayList<>();
774
775                 switch (componentV.getType()) {
776                 case RESOURCE:
777                         return setResourceCategoryFromGraph(componentV, toscaElement);
778                 case SERVICE:
779                         return setServiceCategoryFromGraph(componentV, toscaElement, categories);
780
781                 default:
782                         log.debug("Not supported component type {} ", componentV.getType());
783                         break;
784                 }
785                 return TitanOperationStatus.OK;
786         }
787
788         private TitanOperationStatus setServiceCategoryFromGraph(GraphVertex componentV, ToscaElement toscaElement, List<CategoryDefinition> categories) {
789                 Either<GraphVertex, TitanOperationStatus> childVertex = titanDao.getChildVertex(componentV, EdgeLabelEnum.CATEGORY, JsonParseFlagEnum.NoParse);
790                 if (childVertex.isRight()) {
791                         log.debug("failed to fetch {} for tosca element with id {}, error {}", EdgeLabelEnum.CATEGORY, componentV.getUniqueId(), childVertex.right().value());
792                         return childVertex.right().value();
793                 }
794                 GraphVertex categoryV = childVertex.left().value();
795                 Map<GraphPropertyEnum, Object> metadataProperties = categoryV.getMetadataProperties();
796                 CategoryDefinition category = new CategoryDefinition();
797                 category.setUniqueId(categoryV.getUniqueId());
798                 category.setNormalizedName((String) metadataProperties.get(GraphPropertyEnum.NORMALIZED_NAME));
799                 category.setName((String) metadataProperties.get(GraphPropertyEnum.NAME));
800
801                 Type listTypeCat = new TypeToken<List<String>>() {}.getType();
802                 List<String> iconsfromJsonCat = getGson().fromJson((String) metadataProperties.get(GraphPropertyEnum.ICONS.getProperty()), listTypeCat);
803                 category.setIcons(iconsfromJsonCat);
804                 categories.add(category);
805                 toscaElement.setCategories(categories);
806
807                 return TitanOperationStatus.OK;
808         }
809
810         private TopologyTemplate convertToTopologyTemplate(GraphVertex componentV) {
811
812                 TopologyTemplate topologyTemplate = super.convertToComponent(componentV);
813
814                 Map<String, CompositionDataDefinition> json = (Map<String, CompositionDataDefinition>) componentV.getJson();
815                 topologyTemplate.setCompositions(json);
816
817                 return topologyTemplate;
818         }
819
820         @Override
821         public Either<ToscaElement, StorageOperationStatus> deleteToscaElement(GraphVertex toscaElementVertex) {
822                 Either<ToscaElement, StorageOperationStatus> nodeType = getToscaElement(toscaElementVertex, new ComponentParametersView());
823                 if (nodeType.isRight()) {
824                         log.debug("Failed to fetch tosca element {} error {}", toscaElementVertex.getUniqueId(), nodeType.right().value());
825                         return nodeType;
826                 }
827                 TitanOperationStatus status = disassociateAndDeleteCommonElements(toscaElementVertex);
828                 if (status != TitanOperationStatus.OK) {
829                         Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
830                 }
831                 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.INST_ATTRIBUTES);
832                 if (status != TitanOperationStatus.OK) {
833                         log.debug("Failed to disassociate instances attributes for {} error {}", toscaElementVertex.getUniqueId(), status);
834                         Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
835                 }
836                 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.INST_PROPERTIES);
837                 if (status != TitanOperationStatus.OK) {
838                         log.debug("Failed to disassociate instances properties for {} error {}", toscaElementVertex.getUniqueId(), status);
839                         Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
840                 }
841
842                 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.INST_INPUTS);
843                 if (status != TitanOperationStatus.OK) {
844                         log.debug("Failed to disassociate instances inputs for {} error {}", toscaElementVertex.getUniqueId(), status);
845                         Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
846                 }
847
848                 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.GROUPS);
849                 if (status != TitanOperationStatus.OK) {
850                         log.debug("Failed to disassociate groups for {} error {}", toscaElementVertex.getUniqueId(), status);
851                         Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
852                 }
853                 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.INST_GROUPS);
854                 if (status != TitanOperationStatus.OK) {
855                         log.debug("Failed to disassociate instance groups for {} error {}", toscaElementVertex.getUniqueId(), status);
856                         Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
857                 }
858                 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.INPUTS);
859                 if (status != TitanOperationStatus.OK) {
860                         log.debug("Failed to disassociate inputs for {} error {}", toscaElementVertex.getUniqueId(), status);
861                         Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
862                 }
863                 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.INST_INPUTS);
864                 if (status != TitanOperationStatus.OK) {
865                         log.debug("Failed to disassociate instance inputs for {} error {}", toscaElementVertex.getUniqueId(), status);
866                         Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
867                 }
868                 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.CALCULATED_CAPABILITIES);
869                 if (status != TitanOperationStatus.OK) {
870                         log.debug("Failed to disassociate calculated capabiliites for {} error {}", toscaElementVertex.getUniqueId(), status);
871                         Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
872                 }
873                 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.FULLFILLED_CAPABILITIES);
874                 if (status != TitanOperationStatus.OK) {
875                         log.debug("Failed to disassociate fullfilled capabilities for {} error {}", toscaElementVertex.getUniqueId(), status);
876                         Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
877                 }
878                 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.CALCULATED_CAP_PROPERTIES);
879                 if (status != TitanOperationStatus.OK) {
880                         log.debug("Failed to disassociate calculated capabiliites properties for {} error {}", toscaElementVertex.getUniqueId(), status);
881                         Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
882                 }
883                 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.CALCULATED_REQUIREMENTS);
884                 if (status != TitanOperationStatus.OK) {
885                         log.debug("Failed to disassociate calculated requirements for {} error {}", toscaElementVertex.getUniqueId(), status);
886                         Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
887                 }
888                 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.FULLFILLED_REQUIREMENTS);
889                 if (status != TitanOperationStatus.OK) {
890                         log.debug("Failed to disassociate full filled requirements for {} error {}", toscaElementVertex.getUniqueId(), status);
891                         Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
892                 }
893                 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.INST_DEPLOYMENT_ARTIFACTS);
894                 if (status != TitanOperationStatus.OK) {
895                         log.debug("Failed to disassociate instance artifacts for {} error {}", toscaElementVertex.getUniqueId(), status);
896                         Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
897                 }
898                 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.SERVICE_API_ARTIFACTS);
899                 if (status != TitanOperationStatus.OK) {
900                         log.debug("Failed to disassociate service api artifacts for {} error {}", toscaElementVertex.getUniqueId(), status);
901                         Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
902                 }
903                 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.INSTANCE_ARTIFACTS);
904                 toscaElementVertex.getVertex().remove();
905                 log.trace("Tosca element vertex for {} was removed", toscaElementVertex.getUniqueId());
906
907                 return nodeType;
908         }
909
910         @SuppressWarnings("unchecked")
911         @Override
912         public Either<TopologyTemplate, StorageOperationStatus> createToscaElement(ToscaElement toscaElement) {
913                 return createTopologyTemplate((TopologyTemplate) toscaElement);
914         }
915
916         @Override
917         protected <T extends ToscaElement> TitanOperationStatus setCategoriesFromGraph(GraphVertex vertexComponent, T toscaElement) {
918                 return setTopologyTempalteCategoriesFromGraph(vertexComponent, toscaElement);
919         }
920
921         @Override
922         protected <T extends ToscaElement> StorageOperationStatus validateCategories(T toscaElementToUpdate, GraphVertex elementV) {
923                 // Product isn't supported now!!
924                 // TODO add for Product
925                 if (toscaElementToUpdate.getComponentType() == ComponentTypeEnum.SERVICE) {
926                         return validateServiceCategory(toscaElementToUpdate, elementV);
927                 } else {
928                         // Resource
929                         return validateResourceCategory(toscaElementToUpdate, elementV);
930                 }
931         }
932
933         @Override
934         protected <T extends ToscaElement> StorageOperationStatus updateDerived(T toscaElementToUpdate, GraphVertex updateElementV) {
935                 // not relevant now for topology template
936                 return StorageOperationStatus.OK;
937         }
938
939         @Override
940         public <T extends ToscaElement> void fillToscaElementVertexData(GraphVertex elementV, T toscaElementToUpdate, JsonParseFlagEnum flag) {
941                 fillMetadata(elementV, (TopologyTemplate) toscaElementToUpdate, flag);
942         }
943
944         private <T extends ToscaElement> StorageOperationStatus validateServiceCategory(T toscaElementToUpdate, GraphVertex elementV) {
945                 StorageOperationStatus status = StorageOperationStatus.OK;
946                 List<CategoryDefinition> newCategoryList = toscaElementToUpdate.getCategories();
947                 CategoryDefinition newCategory = newCategoryList.get(0);
948
949                 Either<GraphVertex, TitanOperationStatus> childVertex = titanDao.getChildVertex(elementV, EdgeLabelEnum.CATEGORY, JsonParseFlagEnum.NoParse);
950                 if (childVertex.isRight()) {
951                         log.debug("failed to fetch {} for tosca element with id {}, error {}", EdgeLabelEnum.CATEGORY, elementV.getUniqueId(), childVertex.right().value());
952                         return DaoStatusConverter.convertTitanStatusToStorageStatus(childVertex.right().value());
953                 }
954
955                 GraphVertex categoryV = childVertex.left().value();
956                 Map<GraphPropertyEnum, Object> metadataProperties = categoryV.getMetadataProperties();
957                 String categoryNameCurrent = (String) metadataProperties.get(GraphPropertyEnum.NAME);
958
959                 String newCategoryName = newCategory.getName();
960                 if (newCategoryName != null && false == newCategoryName.equals(categoryNameCurrent)) {
961                         // the category was changed
962                         Either<GraphVertex, StorageOperationStatus> getCategoryVertex = categoryOperation.getCategory(newCategoryName, VertexTypeEnum.SERVICE_CATEGORY);
963
964                         if (getCategoryVertex.isRight()) {
965                                 return getCategoryVertex.right().value();
966                         }
967                         GraphVertex newCategoryV = getCategoryVertex.left().value();
968                         status = moveCategoryEdge(elementV, newCategoryV);
969                         log.debug("Going to update the category of the resource from {} to {}. status is {}", categoryNameCurrent, newCategory, status);
970                 }
971                 return status;
972         }
973
974         public Either<List<GraphVertex>, TitanOperationStatus> getAllNotDeletedElements() {
975                 Map<GraphPropertyEnum, Object> propsHasNot = new HashMap<>();
976                 propsHasNot.put(GraphPropertyEnum.IS_DELETED, true);
977
978                 Either<List<GraphVertex>, TitanOperationStatus> byCriteria = titanDao.getByCriteria(VertexTypeEnum.TOPOLOGY_TEMPLATE, null, propsHasNot, JsonParseFlagEnum.ParseJson);
979                 if (byCriteria.isRight()) {
980                         log.debug("Failed to fetch all non marked topology templates , propsHasNot {}, error {}", propsHasNot, byCriteria.right().value());
981                         return Either.right(byCriteria.right().value());
982                 }
983                 return Either.left(byCriteria.left().value());
984         }
985
986         public boolean isInUse(GraphVertex elementV, List<GraphVertex> allNonDeleted) {
987                 for (GraphVertex containerV : allNonDeleted) {
988                         Map<String, CompositionDataDefinition> composition = (Map<String, CompositionDataDefinition>) containerV.getJson();
989                         if (composition != null) {
990                                 CompositionDataDefinition instances = composition.get(JsonConstantKeysEnum.COMPOSITION.getValue());
991                                 if (instances != null && instances.getComponentInstances() != null && !instances.getComponentInstances().isEmpty()) {
992                                         for (ComponentInstanceDataDefinition ci : instances.getComponentInstances().values()) {
993                                                 if (ci.getComponentUid().equals(elementV.getUniqueId())) {
994                                                         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());
995                                                         return true;
996                                                 }
997                                         }
998
999                                 }
1000                         }
1001                 }
1002
1003                 return false;
1004         }
1005
1006         public boolean isInUse(String componentId, List<GraphVertex> allNonDeleted) {
1007                 for (GraphVertex containerV : allNonDeleted) {
1008                         Map<String, CompositionDataDefinition> composition = (Map<String, CompositionDataDefinition>) containerV.getJson();
1009                         if (composition != null) {
1010                                 CompositionDataDefinition instances = composition.get(JsonConstantKeysEnum.COMPOSITION.getValue());
1011                                 if (instances != null && instances.getComponentInstances() != null && !instances.getComponentInstances().isEmpty()) {
1012                                         for (ComponentInstanceDataDefinition ci : instances.getComponentInstances().values()) {
1013                                                 if (ci.getComponentUid().equals(componentId)) {
1014                                                         return true;
1015                                                 }
1016                                         }
1017
1018                                 }
1019                         }
1020                 }
1021
1022                 return false;
1023         }
1024
1025         public Either<GraphVertex, StorageOperationStatus> updateDistributionStatus(String uniqueId, User user, DistributionStatusEnum distributionStatus) {
1026
1027                 Either<GraphVertex, StorageOperationStatus> result = null;
1028                 String userId = user.getUserId();
1029                 Either<GraphVertex, TitanOperationStatus> getRes = findUserVertex(userId);
1030                 GraphVertex userVertex = null;
1031                 GraphVertex serviceVertex = null;
1032                 if (getRes.isRight()) {
1033                         TitanOperationStatus status = getRes.right().value();
1034                         CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Cannot find user {} in the graph. status is {}", userId, status);
1035                         result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
1036                 }
1037                 if (result == null) {
1038                         userVertex = getRes.left().value();
1039                         getRes = titanDao.getVertexById(uniqueId, JsonParseFlagEnum.ParseMetadata);
1040                         if (getRes.isRight()) {
1041                                 TitanOperationStatus status = getRes.right().value();
1042                                 log.error("Cannot find service {} in the graph. status is {}", uniqueId, status);
1043                                 result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
1044                         }
1045                 }
1046                 if (result == null) {
1047                         serviceVertex = getRes.left().value();
1048                         Iterator<Edge> edgeIterator = serviceVertex.getVertex().edges(Direction.IN, EdgeLabelEnum.LAST_DISTRIBUTION_STATE_MODIFIER.name());
1049                         if (edgeIterator.hasNext()) {
1050                                 log.debug("Remove existing edge from user to component {}. Edge type is {}", userId, uniqueId, EdgeLabelEnum.LAST_DISTRIBUTION_STATE_MODIFIER);
1051                                 edgeIterator.next().remove();
1052                         }
1053                 }
1054                 if (result == null) {
1055                         TitanOperationStatus status = titanDao.createEdge(userVertex, serviceVertex, EdgeLabelEnum.LAST_DISTRIBUTION_STATE_MODIFIER, null);
1056                         if (status != TitanOperationStatus.OK) {
1057                                 log.error("Failed to associate user {} to component {}. Edge type is {}", userId, uniqueId, EdgeLabelEnum.LAST_DISTRIBUTION_STATE_MODIFIER);
1058                                 result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
1059                         }
1060                 }
1061                 if (result == null) {
1062                         serviceVertex.addMetadataProperty(GraphPropertyEnum.DISTRIBUTION_STATUS, distributionStatus.name());
1063                         long lastUpdateDate = System.currentTimeMillis();
1064                         serviceVertex.setJsonMetadataField(JsonPresentationFields.LAST_UPDATE_DATE, lastUpdateDate);
1065                         Either<GraphVertex, TitanOperationStatus> updateRes = titanDao.updateVertex(serviceVertex);
1066                         if (updateRes.isRight()) {
1067                                 result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(updateRes.right().value()));
1068                         }
1069                 }
1070                 if (result == null) {
1071                         result = Either.left(serviceVertex);
1072                 }
1073                 return result;
1074         }
1075
1076 }