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