Service Consumption BE
[sdc.git] / catalog-model / src / main / java / org / openecomp / sdc / be / model / jsontitan / operations / TopologyTemplateOperation.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.be.model.jsontitan.operations;
22
23 import com.google.gson.reflect.TypeToken;
24 import fj.data.Either;
25 import java.lang.reflect.Type;
26 import java.util.ArrayList;
27 import java.util.Arrays;
28 import java.util.HashMap;
29 import java.util.HashSet;
30 import java.util.Iterator;
31 import java.util.List;
32 import java.util.Map;
33 import java.util.Map.Entry;
34 import java.util.Optional;
35 import java.util.Set;
36 import java.util.stream.Collectors;
37 import org.apache.commons.collections.MapUtils;
38 import org.apache.tinkerpop.gremlin.structure.Direction;
39 import org.apache.tinkerpop.gremlin.structure.Edge;
40 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
41 import org.openecomp.sdc.be.dao.jsongraph.types.EdgeLabelEnum;
42 import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum;
43 import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum;
44 import org.openecomp.sdc.be.dao.titan.TitanOperationStatus;
45 import org.openecomp.sdc.be.datatypes.elements.AdditionalInfoParameterDataDefinition;
46 import org.openecomp.sdc.be.datatypes.elements.ArtifactDataDefinition;
47 import org.openecomp.sdc.be.datatypes.elements.CINodeFilterDataDefinition;
48 import org.openecomp.sdc.be.datatypes.elements.ComponentInstanceDataDefinition;
49 import org.openecomp.sdc.be.datatypes.elements.CompositionDataDefinition;
50 import org.openecomp.sdc.be.datatypes.elements.ForwardingPathDataDefinition;
51 import org.openecomp.sdc.be.datatypes.elements.GroupDataDefinition;
52 import org.openecomp.sdc.be.datatypes.elements.InterfaceDataDefinition;
53 import org.openecomp.sdc.be.datatypes.elements.ListCapabilityDataDefinition;
54 import org.openecomp.sdc.be.datatypes.elements.ListRequirementDataDefinition;
55 import org.openecomp.sdc.be.datatypes.elements.MapArtifactDataDefinition;
56 import org.openecomp.sdc.be.datatypes.elements.MapCapabilityProperty;
57 import org.openecomp.sdc.be.datatypes.elements.MapDataDefinition;
58 import org.openecomp.sdc.be.datatypes.elements.MapGroupsDataDefinition;
59 import org.openecomp.sdc.be.datatypes.elements.MapInterfaceDataDefinition;
60 import org.openecomp.sdc.be.datatypes.elements.MapListCapabilityDataDefinition;
61 import org.openecomp.sdc.be.datatypes.elements.MapListRequirementDataDefinition;
62 import org.openecomp.sdc.be.datatypes.elements.MapPropertiesDataDefinition;
63 import org.openecomp.sdc.be.datatypes.elements.PolicyDataDefinition;
64 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
65 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
66 import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum;
67 import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
68 import org.openecomp.sdc.be.datatypes.enums.OriginTypeEnum;
69 import org.openecomp.sdc.be.datatypes.tosca.ToscaDataDefinition;
70 import org.openecomp.sdc.be.model.Component;
71 import org.openecomp.sdc.be.model.ComponentInstanceProperty;
72 import org.openecomp.sdc.be.model.ComponentParametersView;
73 import org.openecomp.sdc.be.model.DistributionStatusEnum;
74 import org.openecomp.sdc.be.model.GroupDefinition;
75
76 import org.openecomp.sdc.be.model.PolicyDefinition;
77 import org.openecomp.sdc.be.model.User;
78 import org.openecomp.sdc.be.model.category.CategoryDefinition;
79 import org.openecomp.sdc.be.model.jsontitan.datamodel.TopologyTemplate;
80 import org.openecomp.sdc.be.model.jsontitan.datamodel.ToscaElement;
81 import org.openecomp.sdc.be.model.jsontitan.datamodel.ToscaElementTypeEnum;
82 import org.openecomp.sdc.be.model.jsontitan.utils.CapabilityRequirementNameResolver;
83 import org.openecomp.sdc.be.model.jsontitan.utils.ModelConverter;
84 import org.openecomp.sdc.be.model.operations.StorageException;
85 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
86 import org.openecomp.sdc.be.model.operations.impl.DaoStatusConverter;
87 import org.openecomp.sdc.be.model.operations.impl.UniqueIdBuilder;
88 import org.openecomp.sdc.common.api.Constants;
89 import org.openecomp.sdc.common.jsongraph.util.CommonUtility;
90 import org.openecomp.sdc.common.jsongraph.util.CommonUtility.LogLevelEnum;
91 import org.openecomp.sdc.common.log.wrappers.Logger;
92 import org.openecomp.sdc.common.util.ValidationUtils;
93 import org.springframework.beans.factory.annotation.Autowired;
94
95 @org.springframework.stereotype.Component("topology-template-operation")
96 public class TopologyTemplateOperation extends ToscaElementOperation {
97
98     private static final Logger log = Logger.getLogger(TopologyTemplateOperation.class);
99     private Set<OriginTypeEnum> nodeTypeSet = new HashSet<>(Arrays.asList(OriginTypeEnum.VFC, OriginTypeEnum.CP, OriginTypeEnum.VL, OriginTypeEnum.Configuration, OriginTypeEnum.VFCMT));
100
101     @Autowired
102     private ArchiveOperation archiveOperation;
103
104     public Either<TopologyTemplate, StorageOperationStatus> createTopologyTemplate(TopologyTemplate topologyTemplate) {
105         Either<TopologyTemplate, StorageOperationStatus> result = null;
106
107         topologyTemplate.generateUUID();
108
109         topologyTemplate = getResourceMetaDataFromResource(topologyTemplate);
110         String resourceUniqueId = topologyTemplate.getUniqueId();
111         if (resourceUniqueId == null) {
112             resourceUniqueId = UniqueIdBuilder.buildResourceUniqueId();
113             topologyTemplate.setUniqueId(resourceUniqueId);
114         }
115
116         GraphVertex topologyTemplateVertex = new GraphVertex();
117         topologyTemplateVertex = fillMetadata(topologyTemplateVertex, topologyTemplate, JsonParseFlagEnum.ParseAll);
118
119         Either<GraphVertex, TitanOperationStatus> createdVertex = titanDao.createVertex(topologyTemplateVertex);
120         if (createdVertex.isRight()) {
121             TitanOperationStatus status = createdVertex.right().value();
122             log.debug("Error returned after creating topology template data node {}. status returned is ", topologyTemplateVertex, status);
123             result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
124             return result;
125         }
126
127         topologyTemplateVertex = createdVertex.left().value();
128
129         StorageOperationStatus assosiateCommon = assosiateCommonForToscaElement(topologyTemplateVertex, topologyTemplate, null);
130         if (assosiateCommon != StorageOperationStatus.OK) {
131             result = Either.right(assosiateCommon);
132             return result;
133         }
134
135         StorageOperationStatus associateCategory = assosiateMetadataToCategory(topologyTemplateVertex, topologyTemplate);
136         if (associateCategory != StorageOperationStatus.OK) {
137             result = Either.right(associateCategory);
138             return result;
139         }
140
141         StorageOperationStatus associateInputs = associateInputsToComponent(topologyTemplateVertex, topologyTemplate);
142         if (associateInputs != StorageOperationStatus.OK) {
143             result = Either.right(associateInputs);
144             return result;
145         }
146         StorageOperationStatus associateGroups = associateGroupsToComponent(topologyTemplateVertex, topologyTemplate);
147         if (associateGroups != StorageOperationStatus.OK) {
148             result = Either.right(associateGroups);
149             return result;
150         }
151         StorageOperationStatus associatePolicies = associatePoliciesToComponent(topologyTemplateVertex, topologyTemplate);
152         if (associatePolicies != StorageOperationStatus.OK) {
153             result = Either.right(associatePolicies);
154             return result;
155         }
156         StorageOperationStatus associateInstAttr = associateInstAttributesToComponent(topologyTemplateVertex, topologyTemplate);
157         if (associateInstAttr != StorageOperationStatus.OK) {
158             result = Either.right(associateInstAttr);
159             return result;
160         }
161         StorageOperationStatus associateInstProperties = associateInstPropertiesToComponent(topologyTemplateVertex, topologyTemplate);
162         if (associateInstProperties != StorageOperationStatus.OK) {
163             result = Either.right(associateInstProperties);
164             return result;
165         }
166         StorageOperationStatus associateInstInputs = associateInstInputsToComponent(topologyTemplateVertex, topologyTemplate);
167         if (associateInstProperties != StorageOperationStatus.OK) {
168             result = Either.right(associateInstInputs);
169             return result;
170         }
171         StorageOperationStatus associateInstGroups = associateInstGroupsToComponent(topologyTemplateVertex, topologyTemplate);
172         if (associateInstGroups != StorageOperationStatus.OK) {
173             result = Either.right(associateInstInputs);
174             return result;
175         }
176
177         StorageOperationStatus associateRequirements = associateRequirementsToResource(topologyTemplateVertex, topologyTemplate);
178         if (associateRequirements != StorageOperationStatus.OK) {
179             result = Either.right(associateRequirements);
180             return result;
181         }
182
183         StorageOperationStatus associateCapabilities = associateCapabilitiesToResource(topologyTemplateVertex, topologyTemplate);
184         if (associateCapabilities != StorageOperationStatus.OK) {
185             result = Either.right(associateCapabilities);
186             return result;
187         }
188
189         StorageOperationStatus associateArtifacts = associateTopologyTemplateArtifactsToComponent(topologyTemplateVertex, topologyTemplate);
190         if (associateArtifacts != StorageOperationStatus.OK) {
191             result = Either.right(associateArtifacts);
192             return result;
193         }
194
195         StorageOperationStatus addAdditionalInformation = addAdditionalInformationToResource(topologyTemplateVertex, topologyTemplate);
196         if (addAdditionalInformation != StorageOperationStatus.OK) {
197             result = Either.right(addAdditionalInformation);
198             return result;
199         }
200         StorageOperationStatus associateCapProperties = associateCapPropertiesToResource(topologyTemplateVertex, topologyTemplate);
201         if (associateCapProperties != StorageOperationStatus.OK) {
202             result = Either.right(associateCapProperties);
203             return result;
204         }
205
206         StorageOperationStatus associateInterfaces = associateInterfacesToComponent(topologyTemplateVertex, topologyTemplate);
207         if (associateInterfaces != StorageOperationStatus.OK) {
208             result = Either.right(associateInterfaces);
209             return result;
210         }
211
212         StorageOperationStatus associatePathProperties = associateForwardingPathToResource(topologyTemplateVertex, topologyTemplate);
213         if (associateCapProperties != StorageOperationStatus.OK) {
214             result = Either.right(associatePathProperties);
215             return result;
216         }
217
218
219         return Either.left(topologyTemplate);
220
221     }
222
223     private StorageOperationStatus associatePoliciesToComponent(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
224         return associatePoliciesToComponent(nodeTypeVertex, topologyTemplate.getPolicies());
225     }
226
227     private StorageOperationStatus associatePoliciesToComponent(GraphVertex nodeTypeVertex, Map<String, PolicyDataDefinition> policies) {
228         if (policies != null && !policies.isEmpty()) {
229             policies.values().stream().filter(p -> p.getUniqueId() == null).forEach(p -> {
230                 String uid = UniqueIdBuilder.buildGroupingUid(nodeTypeVertex.getUniqueId(), p.getName());
231                 p.setUniqueId(uid);
232             });
233             Either<GraphVertex, StorageOperationStatus> assosiateElementToData = associateElementToData(nodeTypeVertex, VertexTypeEnum.POLICIES, EdgeLabelEnum.POLICIES, policies);
234             if (assosiateElementToData.isRight()) {
235                 return assosiateElementToData.right().value();
236             }
237         }
238         return StorageOperationStatus.OK;
239     }
240
241     private StorageOperationStatus associateForwardingPathToResource(GraphVertex topologyTemplateVertex, TopologyTemplate topologyTemplate) {
242         Map<String, ForwardingPathDataDefinition> forwardingPaths = topologyTemplate.getForwardingPaths();
243         return associateForwardingPathToComponent(topologyTemplateVertex, forwardingPaths);
244     }
245
246     private StorageOperationStatus associateNodeFilterToResource(GraphVertex topologyTemplateVertex,
247                                                                  TopologyTemplate topologyTemplate) {
248         Map<String, CINodeFilterDataDefinition> nodeFilters =
249                 topologyTemplate.getNodeFilterComponents();
250         return associateNodeFiltersToComponent(topologyTemplateVertex, nodeFilters);
251     }
252
253     private StorageOperationStatus associateCapPropertiesToResource(GraphVertex topologyTemplateVertex, TopologyTemplate topologyTemplate) {
254         Map<String, MapCapabilityProperty> calculatedCapProperties = topologyTemplate.getCalculatedCapabilitiesProperties();
255         if (calculatedCapProperties != null && !calculatedCapProperties.isEmpty()) {
256             Either<GraphVertex, StorageOperationStatus> assosiateElementToData = associateElementToData(topologyTemplateVertex, VertexTypeEnum.CALCULATED_CAP_PROPERTIES, EdgeLabelEnum.CALCULATED_CAP_PROPERTIES, calculatedCapProperties);
257             if (assosiateElementToData.isRight()) {
258                 return assosiateElementToData.right().value();
259             }
260         }
261         return StorageOperationStatus.OK;
262     }
263
264     private StorageOperationStatus associateCapabilitiesToResource(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
265         Map<String, MapListCapabilityDataDefinition> calculatedCapabilities = topologyTemplate.getCalculatedCapabilities();
266         if (calculatedCapabilities != null && !calculatedCapabilities.isEmpty()) {
267             Either<GraphVertex, StorageOperationStatus> assosiateElementToData = associateElementToData(nodeTypeVertex, VertexTypeEnum.CALCULATED_CAPABILITIES, EdgeLabelEnum.CALCULATED_CAPABILITIES, calculatedCapabilities);
268             if (assosiateElementToData.isRight()) {
269                 return assosiateElementToData.right().value();
270             }
271         }
272         Map<String, MapListCapabilityDataDefinition> fullfilledCapabilities = topologyTemplate.getFullfilledCapabilities();
273         if (fullfilledCapabilities != null && !fullfilledCapabilities.isEmpty()) {
274             Either<GraphVertex, StorageOperationStatus> assosiateElementToData = associateElementToData(nodeTypeVertex, VertexTypeEnum.FULLFILLED_CAPABILITIES, EdgeLabelEnum.FULLFILLED_CAPABILITIES, fullfilledCapabilities);
275             if (assosiateElementToData.isRight()) {
276                 return assosiateElementToData.right().value();
277             }
278         }
279         Map<String, ListCapabilityDataDefinition> capabilities = topologyTemplate.getCapabilities();
280         if(MapUtils.isNotEmpty(capabilities)) {
281             Either<GraphVertex, StorageOperationStatus> associateElementToData =
282                     associateElementToData(nodeTypeVertex, VertexTypeEnum.CAPABILITIES,
283                             EdgeLabelEnum.CAPABILITIES, capabilities);
284             if (associateElementToData.isRight()) {
285                 return associateElementToData.right().value();
286             }
287         }
288         return StorageOperationStatus.OK;
289
290     }
291
292     private StorageOperationStatus associateRequirementsToResource(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
293         Map<String, MapListRequirementDataDefinition> calculatedRequirements = topologyTemplate.getCalculatedRequirements();
294         if (calculatedRequirements != null && !calculatedRequirements.isEmpty()) {
295             Either<GraphVertex, StorageOperationStatus> assosiateElementToData = associateElementToData(nodeTypeVertex, VertexTypeEnum.CALCULATED_REQUIREMENTS, EdgeLabelEnum.CALCULATED_REQUIREMENTS, calculatedRequirements);
296             if (assosiateElementToData.isRight()) {
297                 return assosiateElementToData.right().value();
298             }
299         }
300         Map<String, MapListRequirementDataDefinition> fullfilledRequirements = topologyTemplate.getFullfilledRequirements();
301         if (fullfilledRequirements != null && !fullfilledRequirements.isEmpty()) {
302             Either<GraphVertex, StorageOperationStatus> assosiateElementToData = associateElementToData(nodeTypeVertex, VertexTypeEnum.FULLFILLED_REQUIREMENTS, EdgeLabelEnum.FULLFILLED_REQUIREMENTS, fullfilledRequirements);
303             if (assosiateElementToData.isRight()) {
304                 return assosiateElementToData.right().value();
305             }
306         }
307         Map<String, ListRequirementDataDefinition> requirements = topologyTemplate.getRequirements();
308         if(MapUtils.isNotEmpty(requirements)) {
309             Either<GraphVertex, StorageOperationStatus> associateElementToData =
310                     associateElementToData(nodeTypeVertex, VertexTypeEnum.REQUIREMENTS,
311                             EdgeLabelEnum.REQUIREMENTS, requirements);
312             if (associateElementToData.isRight()) {
313                 return associateElementToData.right().value();
314             }
315         }
316         return StorageOperationStatus.OK;
317     }
318
319     private StorageOperationStatus associateTopologyTemplateArtifactsToComponent(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
320         Map<String, ArtifactDataDefinition> addInformation = topologyTemplate.getServiceApiArtifacts();
321
322         if (addInformation != null && !addInformation.isEmpty()) {
323             addInformation.values().stream().filter(a -> a.getUniqueId() == null).forEach(a -> {
324                 String uniqueId = UniqueIdBuilder.buildPropertyUniqueId(nodeTypeVertex.getUniqueId().toLowerCase(), a.getArtifactLabel().toLowerCase());
325                 a.setUniqueId(uniqueId);
326             });
327             Either<GraphVertex, StorageOperationStatus> assosiateElementToData = associateElementToData(nodeTypeVertex, VertexTypeEnum.SERVICE_API_ARTIFACTS, EdgeLabelEnum.SERVICE_API_ARTIFACTS, addInformation);
328             if (assosiateElementToData.isRight()) {
329                 return assosiateElementToData.right().value();
330             }
331         }
332         Map<String, MapArtifactDataDefinition> instArtifacts = topologyTemplate.getInstDeploymentArtifacts();
333
334         if (instArtifacts != null && !instArtifacts.isEmpty()) {
335             Either<GraphVertex, StorageOperationStatus> assosiateElementToData = associateElementToData(nodeTypeVertex, VertexTypeEnum.INST_DEPLOYMENT_ARTIFACTS, EdgeLabelEnum.INST_DEPLOYMENT_ARTIFACTS, instArtifacts);
336             if (assosiateElementToData.isRight()) {
337                 return assosiateElementToData.right().value();
338             }
339         }
340         Map<String, MapArtifactDataDefinition> instInfoArtifacts = topologyTemplate.getInstanceArtifacts();
341
342         if (instInfoArtifacts != null && !instInfoArtifacts.isEmpty()) {
343             Either<GraphVertex, StorageOperationStatus> assosiateElementToData = associateElementToData(nodeTypeVertex, VertexTypeEnum.INSTANCE_ARTIFACTS, EdgeLabelEnum.INSTANCE_ARTIFACTS, instInfoArtifacts);
344             if (assosiateElementToData.isRight()) {
345                 return assosiateElementToData.right().value();
346             }
347         }
348         return StorageOperationStatus.OK;
349     }
350
351     private StorageOperationStatus addAdditionalInformationToResource(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
352
353         Map<String, AdditionalInfoParameterDataDefinition> addInformation = topologyTemplate.getAdditionalInformation();
354
355         if (addInformation != null && !addInformation.isEmpty()) {
356             Either<GraphVertex, StorageOperationStatus> assosiateElementToData = associateElementToData(nodeTypeVertex, VertexTypeEnum.ADDITIONAL_INFORMATION, EdgeLabelEnum.ADDITIONAL_INFORMATION, addInformation);
357             if (assosiateElementToData.isRight()) {
358                 return assosiateElementToData.right().value();
359             }
360         }
361         return StorageOperationStatus.OK;
362     }
363
364     public StorageOperationStatus associateInstPropertiesToComponent(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
365         Map<String, MapPropertiesDataDefinition> instProps = topologyTemplate.getInstProperties();
366         return associateInstPropertiesToComponent(nodeTypeVertex, instProps);
367     }
368
369     public StorageOperationStatus associateInstInputsToComponent(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
370         Map<String, MapPropertiesDataDefinition> instProps = topologyTemplate.getInstInputs();
371         return associateInstInputsToComponent(nodeTypeVertex, instProps);
372     }
373
374     public StorageOperationStatus associateInstGroupsToComponent(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
375         Map<String, MapGroupsDataDefinition> instGroups = topologyTemplate.getInstGroups();
376         return associateInstGroupsToComponent(nodeTypeVertex, instGroups);
377     }
378
379
380     public StorageOperationStatus associateInstPropertiesToComponent(GraphVertex nodeTypeVertex, Map<String, MapPropertiesDataDefinition> instProps) {
381         if (instProps != null && !instProps.isEmpty()) {
382             Either<GraphVertex, StorageOperationStatus> assosiateElementToData = associateElementToData(nodeTypeVertex, VertexTypeEnum.INST_PROPERTIES, EdgeLabelEnum.INST_PROPERTIES, instProps);
383             if (assosiateElementToData.isRight()) {
384                 return assosiateElementToData.right().value();
385             }
386         }
387         return StorageOperationStatus.OK;
388     }
389
390     public StorageOperationStatus associateInstInputsToComponent(GraphVertex nodeTypeVertex, Map<String, MapPropertiesDataDefinition> instInputs) {
391         if (instInputs != null && !instInputs.isEmpty()) {
392             Either<GraphVertex, StorageOperationStatus> assosiateElementToData = associateElementToData(nodeTypeVertex, VertexTypeEnum.INST_INPUTS, EdgeLabelEnum.INST_INPUTS, instInputs);
393             if (assosiateElementToData.isRight()) {
394                 return assosiateElementToData.right().value();
395             }
396         }
397         return StorageOperationStatus.OK;
398     }
399
400     public StorageOperationStatus associateInstGroupsToComponent(GraphVertex nodeTypeVertex, Map<String, MapGroupsDataDefinition> instGroups) {
401         if (instGroups != null && !instGroups.isEmpty()) {
402             Either<GraphVertex, StorageOperationStatus> assosiateElementToData = associateElementToData(nodeTypeVertex, VertexTypeEnum.INST_GROUPS, EdgeLabelEnum.INST_GROUPS, instGroups);
403             if (assosiateElementToData.isRight()) {
404                 return assosiateElementToData.right().value();
405             }
406         }
407         return StorageOperationStatus.OK;
408     }
409
410
411     public StorageOperationStatus deleteInstInputsToComponent(GraphVertex nodeTypeVertex, Map<String, MapPropertiesDataDefinition> instInputs) {
412
413         if (instInputs != null && !instInputs.isEmpty()) {
414             instInputs.entrySet().forEach(i -> {
415                 List<String> uniqueKeys = new ArrayList<>(i.getValue().getMapToscaDataDefinition().keySet());
416                 List<String> pathKeys = new ArrayList<>();
417                 pathKeys.add(i.getKey());
418
419                 StorageOperationStatus status = deleteToscaDataDeepElements(nodeTypeVertex, EdgeLabelEnum.INST_INPUTS, VertexTypeEnum.INST_INPUTS, uniqueKeys, pathKeys, JsonPresentationFields.NAME);
420                 if (status != StorageOperationStatus.OK) {
421                     return;
422                 }
423             });
424         }
425
426         return StorageOperationStatus.OK;
427     }
428
429     public StorageOperationStatus addInstPropertiesToComponent(GraphVertex nodeTypeVertex, Map<String, MapPropertiesDataDefinition> instInputs) {
430
431         if (instInputs != null && !instInputs.isEmpty()) {
432             instInputs.entrySet().forEach(i -> {
433                 StorageOperationStatus status = addToscaDataDeepElementsBlockToToscaElement(nodeTypeVertex, EdgeLabelEnum.INST_PROPERTIES, VertexTypeEnum.INST_PROPERTIES, i.getValue(), i.getKey());
434                 if (status != StorageOperationStatus.OK) {
435                     return;
436                 }
437             });
438         }
439
440         return StorageOperationStatus.OK;
441     }
442
443     public StorageOperationStatus associateInstDeploymentArtifactsToComponent(GraphVertex nodeTypeVertex, Map<String, MapArtifactDataDefinition> instArtifacts) {
444         return associateInstanceArtifactsToComponent(nodeTypeVertex, instArtifacts, VertexTypeEnum.INST_DEPLOYMENT_ARTIFACTS, EdgeLabelEnum.INST_DEPLOYMENT_ARTIFACTS);
445     }
446
447     public StorageOperationStatus associateInstArtifactsToComponent(GraphVertex nodeTypeVertex, Map<String, MapArtifactDataDefinition> instArtifacts) {
448         return associateInstanceArtifactsToComponent(nodeTypeVertex, instArtifacts, VertexTypeEnum.INSTANCE_ARTIFACTS, EdgeLabelEnum.INSTANCE_ARTIFACTS);
449     }
450
451     private StorageOperationStatus associateInstanceArtifactsToComponent(GraphVertex nodeTypeVertex, Map<String, MapArtifactDataDefinition> instProps, VertexTypeEnum vertexType, EdgeLabelEnum edgeLabel) {
452         if (instProps != null && !instProps.isEmpty()) {
453             Either<GraphVertex, StorageOperationStatus> assosiateElementToData = associateElementToData(nodeTypeVertex, vertexType, edgeLabel, instProps);
454             if (assosiateElementToData.isRight()) {
455                 return assosiateElementToData.right().value();
456             }
457         }
458         return StorageOperationStatus.OK;
459     }
460
461     public StorageOperationStatus associateOrAddCalcCapReqToComponent(GraphVertex nodeTypeVertex, Map<String, MapListRequirementDataDefinition> calcRequirements, Map<String, MapListCapabilityDataDefinition> calcCapabilty, Map<String, MapCapabilityProperty> calculatedCapabilitiesProperties) {
462         if (calcRequirements != null && !calcRequirements.isEmpty()) {
463             Either<GraphVertex, StorageOperationStatus> assosiateElementToData = associateOrAddElementToData(nodeTypeVertex, VertexTypeEnum.CALCULATED_REQUIREMENTS, EdgeLabelEnum.CALCULATED_REQUIREMENTS, calcRequirements);
464             if (assosiateElementToData.isRight()) {
465                 return assosiateElementToData.right().value();
466             }
467             Map<String, MapListRequirementDataDefinition> fullFilled = new HashMap<>();
468             assosiateElementToData = associateOrAddElementToData(nodeTypeVertex, VertexTypeEnum.FULLFILLED_REQUIREMENTS, EdgeLabelEnum.FULLFILLED_REQUIREMENTS, fullFilled);
469             if (assosiateElementToData.isRight()) {
470                 return assosiateElementToData.right().value();
471             }
472         }
473         if (calcCapabilty != null && !calcCapabilty.isEmpty()) {
474             Either<GraphVertex, StorageOperationStatus> assosiateElementToData = associateOrAddElementToData(nodeTypeVertex, VertexTypeEnum.CALCULATED_CAPABILITIES, EdgeLabelEnum.CALCULATED_CAPABILITIES, calcCapabilty);
475             if (assosiateElementToData.isRight()) {
476                 return assosiateElementToData.right().value();
477             }
478             Map<String, MapListCapabilityDataDefinition> fullFilled = new HashMap<>();
479             assosiateElementToData = associateOrAddElementToData(nodeTypeVertex, VertexTypeEnum.FULLFILLED_CAPABILITIES, EdgeLabelEnum.FULLFILLED_CAPABILITIES, fullFilled);
480             if (assosiateElementToData.isRight()) {
481                 return assosiateElementToData.right().value();
482             }
483         }
484         if (calculatedCapabilitiesProperties != null && !calculatedCapabilitiesProperties.isEmpty()) {
485             return associateOrAddElementToData(nodeTypeVertex, VertexTypeEnum.CALCULATED_CAP_PROPERTIES,
486                     EdgeLabelEnum.CALCULATED_CAP_PROPERTIES, calculatedCapabilitiesProperties)
487                     .right()
488                     .on(v -> StorageOperationStatus.OK);
489         }
490         return StorageOperationStatus.OK;
491     }
492
493     private <T extends MapDataDefinition> Either<GraphVertex, StorageOperationStatus> associateOrAddElementToData(GraphVertex nodeTypeVertex, VertexTypeEnum vertexTypeEnum, EdgeLabelEnum edgeLabelEnum, Map<String, T> dataMap) {
494         return titanDao.getChildVertex(nodeTypeVertex, edgeLabelEnum, JsonParseFlagEnum.ParseJson)
495                 .either(dataVertex -> addElementsToComponent(nodeTypeVertex, dataVertex, vertexTypeEnum, edgeLabelEnum, dataMap),
496                         status -> associateElementToDataIfNotFound(status, nodeTypeVertex, vertexTypeEnum, edgeLabelEnum, dataMap));
497     }
498
499     private Either<GraphVertex, StorageOperationStatus> associateElementToDataIfNotFound(TitanOperationStatus status, GraphVertex nodeTypeVertex, VertexTypeEnum vertexTypeEnum, EdgeLabelEnum edgeLabelEnum, Map<String, ? extends ToscaDataDefinition> dataMap) {
500         if (status == TitanOperationStatus.NOT_FOUND) {
501             return associateElementToData(nodeTypeVertex, vertexTypeEnum, edgeLabelEnum, dataMap);
502         }
503         return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
504     }
505
506     private <T extends MapDataDefinition> Either<GraphVertex, StorageOperationStatus> addElementsToComponent(GraphVertex nodeTypeVertex, GraphVertex dataVertex, VertexTypeEnum vertexTypeEnum, EdgeLabelEnum edgeLabelEnum, Map<String, T> dataMap) {
507         Optional<StorageOperationStatus> error = dataMap.entrySet()
508                 .stream()
509                 .map(e -> addElementToComponent(nodeTypeVertex.getUniqueId(), vertexTypeEnum, edgeLabelEnum, e))
510                 .filter(s -> s != StorageOperationStatus.OK)
511                 .findFirst();
512         if (error.isPresent()) {
513             return Either.right(error.get());
514         }
515         return Either.left(dataVertex);
516     }
517
518     private StorageOperationStatus associateInstAttributesToComponent(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
519         Map<String, MapPropertiesDataDefinition> instAttr = topologyTemplate.getInstAttributes();
520         return associateInstAttributeToComponent(nodeTypeVertex, instAttr);
521     }
522
523     public StorageOperationStatus associateForwardingPathToComponent(GraphVertex nodeTypeVertex, Map<String, ForwardingPathDataDefinition> forwardingPathMap) {
524         if (forwardingPathMap != null && !forwardingPathMap.isEmpty()) {
525             Either<GraphVertex, StorageOperationStatus> assosiateElementToData = associateElementToData(nodeTypeVertex, VertexTypeEnum.FORWARDING_PATH, EdgeLabelEnum.FORWARDING_PATH, forwardingPathMap);
526             if (assosiateElementToData.isRight()) {
527                 return assosiateElementToData.right().value();
528             }
529         }
530         return StorageOperationStatus.OK;
531     }
532
533     public StorageOperationStatus associateInstAttributeToComponent(GraphVertex nodeTypeVertex, Map<String, MapPropertiesDataDefinition> instAttr) {
534         if (instAttr != null && !instAttr.isEmpty()) {
535             Either<GraphVertex, StorageOperationStatus> assosiateElementToData = associateElementToData(nodeTypeVertex, VertexTypeEnum.INST_ATTRIBUTES, EdgeLabelEnum.INST_ATTRIBUTES, instAttr);
536             if (assosiateElementToData.isRight()) {
537                 return assosiateElementToData.right().value();
538             }
539         }
540         return StorageOperationStatus.OK;
541     }
542
543     public StorageOperationStatus associateGroupsToComponent(GraphVertex nodeTypeVertex, Map<String, GroupDataDefinition> groups) {
544
545         if (groups != null && !groups.isEmpty()) {
546             groups.values().stream().filter(p -> p.getUniqueId() == null).forEach(p -> {
547                 String uid = UniqueIdBuilder.buildGroupingUid(nodeTypeVertex.getUniqueId(), p.getName());
548                 p.setUniqueId(uid);
549             });
550             Either<GraphVertex, StorageOperationStatus> assosiateElementToData = associateElementToData(nodeTypeVertex, VertexTypeEnum.GROUPS, EdgeLabelEnum.GROUPS, groups);
551             if (assosiateElementToData.isRight()) {
552                 return assosiateElementToData.right().value();
553             }
554         }
555         return StorageOperationStatus.OK;
556     }
557
558     private StorageOperationStatus associateGroupsToComponent(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
559         return associateGroupsToComponent(nodeTypeVertex, topologyTemplate.getGroups());
560     }
561
562     public StorageOperationStatus associateInputsToComponent(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
563         Map<String, PropertyDataDefinition> inputs = topologyTemplate.getInputs();
564         return associateInputsToComponent(nodeTypeVertex, inputs, topologyTemplate.getUniqueId());
565     }
566
567     public StorageOperationStatus associateInputsToComponent(GraphVertex nodeTypeVertex, Map<String, PropertyDataDefinition> inputs, String id) {
568         if (inputs != null && !inputs.isEmpty()) {
569             inputs.values().stream().filter(e -> e.getUniqueId() == null).forEach(e -> e.setUniqueId(UniqueIdBuilder.buildPropertyUniqueId(id, e.getName())));
570
571             Either<GraphVertex, StorageOperationStatus> assosiateElementToData = associateElementToData(nodeTypeVertex, VertexTypeEnum.INPUTS, EdgeLabelEnum.INPUTS, inputs);
572             if (assosiateElementToData.isRight()) {
573                 return assosiateElementToData.right().value();
574             }
575         }
576         return StorageOperationStatus.OK;
577     }
578
579     private GraphVertex fillMetadata(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate, JsonParseFlagEnum flag) {
580         nodeTypeVertex.setLabel(VertexTypeEnum.TOPOLOGY_TEMPLATE);
581         fillCommonMetadata(nodeTypeVertex, topologyTemplate);
582         if (flag == JsonParseFlagEnum.ParseAll || flag == JsonParseFlagEnum.ParseJson) {
583             nodeTypeVertex.setJson(topologyTemplate.getCompositions());
584         }
585         nodeTypeVertex.addMetadataProperty(GraphPropertyEnum.CSAR_UUID, topologyTemplate.getMetadataValue(JsonPresentationFields.CSAR_UUID));
586         nodeTypeVertex.addMetadataProperty(GraphPropertyEnum.DISTRIBUTION_STATUS, topologyTemplate.getMetadataValue(JsonPresentationFields.DISTRIBUTION_STATUS));
587
588         return nodeTypeVertex;
589
590     }
591
592     private StorageOperationStatus assosiateMetadataToCategory(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
593         if (topologyTemplate.getResourceType() == null) {
594             // service
595             return associateServiceMetadataToCategory(nodeTypeVertex, topologyTemplate);
596         } else {
597             // VF
598             return assosiateResourceMetadataToCategory(nodeTypeVertex, topologyTemplate);
599         }
600     }
601
602     private StorageOperationStatus associateServiceMetadataToCategory(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
603         String categoryName = topologyTemplate.getCategories().get(0).getName();
604         Either<GraphVertex, StorageOperationStatus> category = categoryOperation.getCategory(categoryName, VertexTypeEnum.SERVICE_CATEGORY);
605         if (category.isRight()) {
606             log.trace("NO category {} for service {}", categoryName, topologyTemplate.getUniqueId());
607             return StorageOperationStatus.CATEGORY_NOT_FOUND;
608         }
609         GraphVertex categoryV = category.left().value();
610         TitanOperationStatus createEdge = titanDao.createEdge(nodeTypeVertex, categoryV, EdgeLabelEnum.CATEGORY, new HashMap<>());
611         if (createEdge != TitanOperationStatus.OK) {
612             log.trace("Failed to associate resource {} to category {} with id {}", topologyTemplate.getUniqueId(), categoryName, categoryV.getUniqueId());
613             return DaoStatusConverter.convertTitanStatusToStorageStatus(createEdge);
614         }
615         return StorageOperationStatus.OK;
616     }
617
618     @Override
619     public Either<ToscaElement, StorageOperationStatus> getToscaElement(String uniqueId, ComponentParametersView componentParametersView) {
620         JsonParseFlagEnum parseFlag = componentParametersView.detectParseFlag();
621
622         Either<GraphVertex, StorageOperationStatus> componentByLabelAndId = getComponentByLabelAndId(uniqueId, ToscaElementTypeEnum.TOPOLOGY_TEMPLATE, parseFlag);
623         if (componentByLabelAndId.isRight()) {
624             return Either.right(componentByLabelAndId.right().value());
625         }
626         GraphVertex componentV = componentByLabelAndId.left().value();
627
628         return getToscaElement(componentV, componentParametersView);
629
630     }
631     // -------------------------------------------------------------
632
633     public Either<ToscaElement, StorageOperationStatus> getToscaElement(GraphVertex componentV, ComponentParametersView componentParametersView) {
634         TopologyTemplate toscaElement;
635
636         toscaElement = convertToTopologyTemplate(componentV);
637         TitanOperationStatus status;
638         if (!componentParametersView.isIgnoreUsers()) {
639             status = setCreatorFromGraph(componentV, toscaElement);
640             if (status != TitanOperationStatus.OK) {
641                 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
642             }
643
644             status = setLastModifierFromGraph(componentV, toscaElement);
645             if (status != TitanOperationStatus.OK) {
646                 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
647             }
648         }
649         if (!componentParametersView.isIgnoreCategories()) {
650             status = setTopologyTempalteCategoriesFromGraph(componentV, toscaElement);
651             if (status != TitanOperationStatus.OK) {
652                 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
653
654             }
655         }
656         if (!componentParametersView.isIgnoreArtifacts()) {
657             TitanOperationStatus storageStatus = setAllArtifactsFromGraph(componentV, toscaElement);
658             if (storageStatus != TitanOperationStatus.OK) {
659                 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(storageStatus));
660             }
661         }
662         if (!componentParametersView.isIgnoreComponentInstancesProperties()) {
663             status = setComponentInstancesPropertiesFromGraph(componentV, toscaElement);
664             if (status != TitanOperationStatus.OK) {
665                 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
666             }
667         }
668         if (!componentParametersView.isIgnoreCapabilities()) {
669             status = setCapabilitiesFromGraph(componentV, toscaElement);
670             if (status != TitanOperationStatus.OK) {
671                 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
672             }
673         }
674         if (!componentParametersView.isIgnoreRequirements()) {
675             status = setRequirementsFromGraph(componentV, toscaElement);
676             if (status != TitanOperationStatus.OK) {
677                 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
678             }
679         }
680         if (!componentParametersView.isIgnoreAllVersions()) {
681             status = setAllVersions(componentV, toscaElement);
682             if (status != TitanOperationStatus.OK) {
683                 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
684             }
685         }
686         if (!componentParametersView.isIgnoreAdditionalInformation()) {
687             status = setAdditionalInformationFromGraph(componentV, toscaElement);
688             if (status != TitanOperationStatus.OK) {
689                 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
690             }
691         }
692
693         if (!componentParametersView.isIgnoreGroups()) {
694             status = setGroupsFromGraph(componentV, toscaElement);
695             if (status != TitanOperationStatus.OK) {
696                 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
697             }
698
699         }
700         if (!componentParametersView.isIgnorePolicies()) {
701             status = setPoliciesFromGraph(componentV, toscaElement);
702             if (status != TitanOperationStatus.OK) {
703                 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
704             }
705
706         }
707         if (!componentParametersView.isIgnoreComponentInstances()) {
708             status = setInstGroupsFromGraph(componentV, toscaElement);
709
710             //Mark all CIs that has archived origins
711             archiveOperation.setArchivedOriginsFlagInComponentInstances(componentV);
712
713             if (status != TitanOperationStatus.OK) {
714                 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
715             }
716
717         }
718         if (!componentParametersView.isIgnoreInputs()) {
719             status = setInputsFromGraph(componentV, toscaElement);
720             if (status != TitanOperationStatus.OK) {
721                 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
722             }
723
724         }
725         if (!componentParametersView.isIgnoreProperties()) {
726             status = setPropertiesFromGraph(componentV, toscaElement);
727             if (status != TitanOperationStatus.OK) {
728                 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
729             }
730
731         }
732
733         if (!componentParametersView.isIgnoreComponentInstancesInputs()) {
734             status = setComponentInstancesInputsFromGraph(componentV, toscaElement);
735             if (status != TitanOperationStatus.OK) {
736                 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
737
738             }
739         }
740
741         if (!componentParametersView.isIgnoreCapabiltyProperties()) {
742             status = setComponentInstancesCapPropertiesFromGraph(componentV, toscaElement);
743             if (status != TitanOperationStatus.OK) {
744                 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
745
746             }
747         }
748
749         if (!componentParametersView.isIgnoreForwardingPath()) {
750             status = setForwardingGraphPropertiesFromGraph(componentV, toscaElement);
751             if (status != TitanOperationStatus.OK) {
752                 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
753
754             }
755         }
756
757         if (!componentParametersView.isIgnoreNodeFilter()) {
758             status = setNodeFilterComponentFromGraph(componentV, toscaElement);
759             if (status != TitanOperationStatus.OK) {
760                 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
761
762             }
763         }
764
765         if (!componentParametersView.isIgnoreInterfaces()) {
766             TitanOperationStatus storageStatus = setInterfacesFromGraph(componentV, toscaElement);
767             if (storageStatus != TitanOperationStatus.OK) {
768                 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(storageStatus));
769
770             }
771         }
772
773         if (!componentParametersView.isIgnoreComponentInstancesInterfaces()) {
774             TitanOperationStatus storageStatus =
775                     setComponentInstancesInterfacesFromGraph(componentV, toscaElement);
776             if (storageStatus != TitanOperationStatus.OK) {
777                 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(storageStatus));
778             }
779         }
780         return Either.left(toscaElement);
781     }
782
783     private TitanOperationStatus setPoliciesFromGraph(GraphVertex componentV, TopologyTemplate toscaElement) {
784         Either<Map<String, PolicyDataDefinition>, TitanOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.POLICIES);
785         if (result.isLeft()) {
786             toscaElement.setPolicies(result.left().value());
787         } else {
788             if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
789                 return result.right().value();
790             }
791         }
792         return TitanOperationStatus.OK;
793     }
794
795     private TitanOperationStatus setInterfacesFromGraph(GraphVertex componentV, TopologyTemplate topologyTemplate) {
796         Either<Map<String, InterfaceDataDefinition>, TitanOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.INTERFACE);
797         if (result.isLeft()) {
798             topologyTemplate.setInterfaces(result.left().value());
799         } else {
800             if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
801                 return result.right().value();
802             }
803         }
804         return TitanOperationStatus.OK;
805     }
806
807
808     private TitanOperationStatus setComponentInstancesInterfacesFromGraph(GraphVertex componentV,
809                                                                           TopologyTemplate topologyTemplate) {
810         Either<Map<String, MapInterfaceDataDefinition>, TitanOperationStatus> result =
811                 getDataFromGraph(componentV, EdgeLabelEnum.INST_INTERFACES);
812         if (result.isLeft()) {
813             result.left().value().entrySet().forEach(entry -> topologyTemplate
814                     .addComponentInstanceInterfaceMap(entry.getKey(), entry.getValue()));
815         } else {
816             if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
817                 return result.right().value();
818             }
819         }
820         return TitanOperationStatus.OK;
821     }
822
823     private StorageOperationStatus associateInterfacesToComponent(GraphVertex topologyTemplateVertex, TopologyTemplate topologyTemplate) {
824         Map<String, InterfaceDataDefinition> interfaceMap = topologyTemplate.getInterfaces();
825         if (interfaceMap != null && !interfaceMap.isEmpty()) {
826             Either<GraphVertex, StorageOperationStatus> assosiateElementToData = associateElementToData(topologyTemplateVertex, VertexTypeEnum.INTERFACE, EdgeLabelEnum.INTERFACE, interfaceMap);
827             if (assosiateElementToData.isRight()) {
828                 return assosiateElementToData.right().value();
829             }
830         }
831         return StorageOperationStatus.OK;
832     }
833
834     public StorageOperationStatus associateNodeFiltersToComponent(GraphVertex nodeTypeVertex,
835                                                                   Map<String, CINodeFilterDataDefinition> filterMaps) {
836         if (filterMaps != null && !filterMaps.isEmpty()) {
837             Either<GraphVertex, StorageOperationStatus> assosiateElementToData = associateElementToData
838                     (nodeTypeVertex, VertexTypeEnum.NODE_FILTER_TEMPLATE,
839                             EdgeLabelEnum.NODE_FILTER_TEMPLATE, filterMaps);
840             if (assosiateElementToData.isRight()) {
841                 return assosiateElementToData.right().value();
842             }
843         }
844         return StorageOperationStatus.OK;
845     }
846
847     private TitanOperationStatus setForwardingGraphPropertiesFromGraph(GraphVertex componentV, TopologyTemplate topologyTemplate) {
848         Either<Map<String, ForwardingPathDataDefinition>, TitanOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.FORWARDING_PATH);
849         if (result.isLeft()) {
850             topologyTemplate.setForwardingPaths(result.left().value());
851         } else {
852             if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
853                 return result.right().value();
854             }
855         }
856         return TitanOperationStatus.OK;
857     }
858
859
860     private TitanOperationStatus setComponentInstancesCapPropertiesFromGraph(GraphVertex componentV, TopologyTemplate topologyTemplate) {
861         Either<Map<String, MapCapabilityProperty>, TitanOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.CALCULATED_CAP_PROPERTIES);
862         if (result.isLeft()) {
863             topologyTemplate.setCalculatedCapabilitiesProperties(result.left().value());
864         } else {
865             if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
866                 return result.right().value();
867             }
868         }
869         return TitanOperationStatus.OK;
870     }
871
872     private TitanOperationStatus setPropertiesFromGraph(GraphVertex componentV, TopologyTemplate toscaElement) {
873         Either<Map<String, PropertyDataDefinition>, TitanOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.PROPERTIES);
874         if (result.isLeft()) {
875             toscaElement.setProperties(result.left().value());
876         } else {
877             if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
878                 return result.right().value();
879             }
880         }
881         return TitanOperationStatus.OK;
882     }
883
884     private TitanOperationStatus setInstGroupsFromGraph(GraphVertex componentV, TopologyTemplate topologyTemplate) {
885         Either<Map<String, MapGroupsDataDefinition>, TitanOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.INST_GROUPS);
886         if (result.isLeft()) {
887             topologyTemplate.setInstGroups(result.left().value());
888         } else {
889             if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
890                 return result.right().value();
891             }
892         }
893         return TitanOperationStatus.OK;
894     }
895
896     private TitanOperationStatus setComponentInstancesPropertiesFromGraph(GraphVertex componentV, TopologyTemplate topologyTemplate) {
897         Either<Map<String, MapPropertiesDataDefinition>, TitanOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.INST_PROPERTIES);
898         if (result.isLeft()) {
899             topologyTemplate.setInstProperties(result.left().value());
900         } else {
901             if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
902                 return result.right().value();
903             }
904         }
905         return TitanOperationStatus.OK;
906     }
907
908     private TitanOperationStatus setComponentInstancesInputsFromGraph(GraphVertex componentV, TopologyTemplate topologyTemplate) {
909         Either<Map<String, MapPropertiesDataDefinition>, TitanOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.INST_INPUTS);
910         if (result.isLeft()) {
911             topologyTemplate.setInstInputs(result.left().value());
912         } else {
913             if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
914                 return result.right().value();
915             }
916         }
917         return TitanOperationStatus.OK;
918     }
919
920     private TitanOperationStatus setNodeFilterComponentFromGraph(GraphVertex componentV,
921                                                                  TopologyTemplate topologyTemplate) {
922         Either<Map<String, CINodeFilterDataDefinition>, TitanOperationStatus> result =
923                 getDataFromGraph(componentV,
924                         EdgeLabelEnum.NODE_FILTER_TEMPLATE);
925         if (result.isLeft()) {
926             topologyTemplate.setNodeFilterComponents(result.left().value());
927         } else {
928             if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
929                 return result.right().value();
930             }
931         }
932         return TitanOperationStatus.OK;
933     }
934
935     @Override
936     protected <T extends ToscaElement> TitanOperationStatus setRequirementsFromGraph(GraphVertex componentV, T toscaElement) {
937         Either<Map<String, MapListRequirementDataDefinition>, TitanOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.CALCULATED_REQUIREMENTS);
938         if (result.isLeft()) {
939             ((TopologyTemplate) toscaElement).setCalculatedRequirements(result.left().value());
940         } else {
941             if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
942                 return result.right().value();
943             }
944         }
945         result = getDataFromGraph(componentV, EdgeLabelEnum.FULLFILLED_REQUIREMENTS);
946         if (result.isLeft()) {
947             ((TopologyTemplate) toscaElement).setFullfilledRequirements(result.left().value());
948         } else {
949             if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
950                 return result.right().value();
951             }
952         }
953         Either<Map<String, ListRequirementDataDefinition>, TitanOperationStatus> requirementResult =
954                 getDataFromGraph(componentV, EdgeLabelEnum.REQUIREMENTS);
955         if (requirementResult.isLeft()) {
956             toscaElement.setRequirements(requirementResult.left().value());
957         } else {
958             if (requirementResult.right().value() != TitanOperationStatus.NOT_FOUND) {
959                 return requirementResult.right().value();
960             }
961         }
962         return TitanOperationStatus.OK;
963
964     }
965
966     protected <T extends ToscaElement> TitanOperationStatus setCapabilitiesFromGraph(GraphVertex componentV, T toscaElement) {
967         Either<Map<String, MapListCapabilityDataDefinition>, TitanOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.CALCULATED_CAPABILITIES);
968         if (result.isLeft()) {
969             ((TopologyTemplate) toscaElement).setCalculatedCapabilities(result.left().value());
970         } else {
971             if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
972                 return result.right().value();
973             }
974         }
975         result = getDataFromGraph(componentV, EdgeLabelEnum.FULLFILLED_CAPABILITIES);
976         if (result.isLeft()) {
977             ((TopologyTemplate) toscaElement).setFullfilledCapabilities(result.left().value());
978         } else {
979             if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
980                 return result.right().value();
981             }
982         }
983         Either<Map<String, ListCapabilityDataDefinition>, TitanOperationStatus> capabilitiesResult =
984                 getDataFromGraph(componentV, EdgeLabelEnum.CAPABILITIES);
985         if (capabilitiesResult.isLeft()) {
986             toscaElement.setCapabilities(capabilitiesResult.left().value());
987         } else {
988             if (capabilitiesResult.right().value() != TitanOperationStatus.NOT_FOUND) {
989                 return capabilitiesResult.right().value();
990             }
991         }
992         return TitanOperationStatus.OK;
993     }
994
995     private TitanOperationStatus setAllArtifactsFromGraph(GraphVertex componentV, TopologyTemplate toscaElement) {
996         TitanOperationStatus storageStatus = setArtifactsFromGraph(componentV, toscaElement);
997         if (storageStatus != TitanOperationStatus.OK) {
998             return storageStatus;
999         }
1000         Either<Map<String, ArtifactDataDefinition>, TitanOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.SERVICE_API_ARTIFACTS);
1001         if (result.isLeft()) {
1002             toscaElement.setServiceApiArtifacts(result.left().value());
1003         } else {
1004             if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
1005                 return result.right().value();
1006             }
1007         }
1008         Either<Map<String, MapArtifactDataDefinition>, TitanOperationStatus> resultInstArt = getDataFromGraph(componentV, EdgeLabelEnum.INST_DEPLOYMENT_ARTIFACTS);
1009         if (resultInstArt.isLeft()) {
1010             toscaElement.setInstDeploymentArtifacts(resultInstArt.left().value());
1011         } else {
1012             if (resultInstArt.right().value() != TitanOperationStatus.NOT_FOUND) {
1013                 return resultInstArt.right().value();
1014             }
1015         }
1016         Either<Map<String, MapArtifactDataDefinition>, TitanOperationStatus> instanceArt = getDataFromGraph(componentV, EdgeLabelEnum.INSTANCE_ARTIFACTS);
1017         if (instanceArt.isLeft()) {
1018             toscaElement.setInstanceArtifacts(instanceArt.left().value());
1019         } else {
1020             if (instanceArt.right().value() != TitanOperationStatus.NOT_FOUND) {
1021                 return instanceArt.right().value();
1022             }
1023         }
1024         return TitanOperationStatus.OK;
1025     }
1026
1027     private TitanOperationStatus setInputsFromGraph(GraphVertex componentV, TopologyTemplate toscaElement) {
1028         Either<Map<String, PropertyDataDefinition>, TitanOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.INPUTS);
1029         if (result.isLeft()) {
1030             toscaElement.setInputs(result.left().value());
1031         } else {
1032             if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
1033                 return result.right().value();
1034             }
1035         }
1036         return TitanOperationStatus.OK;
1037     }
1038
1039     private TitanOperationStatus setGroupsFromGraph(GraphVertex componentV, TopologyTemplate toscaElement) {
1040         Either<Map<String, GroupDataDefinition>, TitanOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.GROUPS);
1041         if (result.isLeft()) {
1042             toscaElement.setGroups(result.left().value());
1043         } else {
1044             if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
1045                 return result.right().value();
1046             }
1047         }
1048         return TitanOperationStatus.OK;
1049     }
1050
1051     private TitanOperationStatus setTopologyTempalteCategoriesFromGraph(GraphVertex componentV, ToscaElement toscaElement) {
1052         List<CategoryDefinition> categories = new ArrayList<>();
1053
1054         switch (componentV.getType()) {
1055             case RESOURCE:
1056                 return setResourceCategoryFromGraph(componentV, toscaElement);
1057             case SERVICE:
1058                 return setServiceCategoryFromGraph(componentV, toscaElement, categories);
1059
1060             default:
1061                 log.debug("Not supported component type {} ", componentV.getType());
1062                 break;
1063         }
1064         return TitanOperationStatus.OK;
1065     }
1066
1067     private TitanOperationStatus setServiceCategoryFromGraph(GraphVertex componentV, ToscaElement toscaElement, List<CategoryDefinition> categories) {
1068         Either<GraphVertex, TitanOperationStatus> childVertex = titanDao.getChildVertex(componentV, EdgeLabelEnum.CATEGORY, JsonParseFlagEnum.NoParse);
1069         if (childVertex.isRight()) {
1070             log.debug("failed to fetch {} for tosca element with id {}, error {}", EdgeLabelEnum.CATEGORY, componentV.getUniqueId(), childVertex.right().value());
1071             return childVertex.right().value();
1072         }
1073         GraphVertex categoryV = childVertex.left().value();
1074         Map<GraphPropertyEnum, Object> metadataProperties = categoryV.getMetadataProperties();
1075         CategoryDefinition category = new CategoryDefinition();
1076         category.setUniqueId(categoryV.getUniqueId());
1077         category.setNormalizedName((String) metadataProperties.get(GraphPropertyEnum.NORMALIZED_NAME));
1078         category.setName((String) metadataProperties.get(GraphPropertyEnum.NAME));
1079
1080         Type listTypeCat = new TypeToken<List<String>>() {
1081         }.getType();
1082         List<String> iconsfromJsonCat = getGson().fromJson((String) metadataProperties.get(GraphPropertyEnum.ICONS.getProperty()), listTypeCat);
1083         category.setIcons(iconsfromJsonCat);
1084         categories.add(category);
1085         toscaElement.setCategories(categories);
1086
1087         return TitanOperationStatus.OK;
1088     }
1089
1090     @SuppressWarnings("unchecked")
1091     private TopologyTemplate convertToTopologyTemplate(GraphVertex componentV) {
1092
1093         TopologyTemplate topologyTemplate = super.convertToComponent(componentV);
1094
1095         Map<String, CompositionDataDefinition> json = (Map<String, CompositionDataDefinition>) componentV.getJson();
1096         topologyTemplate.setCompositions(json);
1097
1098         return topologyTemplate;
1099     }
1100
1101     @Override
1102     public Either<ToscaElement, StorageOperationStatus> deleteToscaElement(GraphVertex toscaElementVertex) {
1103         Either<ToscaElement, StorageOperationStatus> nodeType = getToscaElement(toscaElementVertex, new ComponentParametersView());
1104         if (nodeType.isRight()) {
1105             log.debug("Failed to fetch tosca element {} error {}", toscaElementVertex.getUniqueId(), nodeType.right().value());
1106             return nodeType;
1107         }
1108         TitanOperationStatus status = disassociateAndDeleteCommonElements(toscaElementVertex);
1109         if (status != TitanOperationStatus.OK) {
1110             return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
1111         }
1112         status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.INST_ATTRIBUTES);
1113         if (status != TitanOperationStatus.OK) {
1114             log.debug("Failed to disassociate instances attributes for {} error {}", toscaElementVertex.getUniqueId(), status);
1115             return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
1116         }
1117         status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.INST_PROPERTIES);
1118         if (status != TitanOperationStatus.OK) {
1119             log.debug("Failed to disassociate instances properties for {} error {}", toscaElementVertex.getUniqueId(), status);
1120             return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
1121         }
1122
1123         status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.INST_INPUTS);
1124         if (status != TitanOperationStatus.OK) {
1125             log.debug("Failed to disassociate instances inputs for {} error {}", toscaElementVertex.getUniqueId(), status);
1126             return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
1127         }
1128
1129         status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.GROUPS);
1130         if (status != TitanOperationStatus.OK) {
1131             log.debug("Failed to disassociate groups for {} error {}", toscaElementVertex.getUniqueId(), status);
1132             return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
1133         }
1134         status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.POLICIES);
1135         if (status != TitanOperationStatus.OK) {
1136             log.debug("Failed to disassociate policies for {} error {}", toscaElementVertex.getUniqueId(), status);
1137             return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
1138         }
1139         status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.INST_GROUPS);
1140         if (status != TitanOperationStatus.OK) {
1141             log.debug("Failed to disassociate instance groups for {} error {}", toscaElementVertex.getUniqueId(), status);
1142             return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
1143         }
1144         status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.INPUTS);
1145         if (status != TitanOperationStatus.OK) {
1146             log.debug("Failed to disassociate inputs for {} error {}", toscaElementVertex.getUniqueId(), status);
1147             return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
1148         }
1149         status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.INST_INPUTS);
1150         if (status != TitanOperationStatus.OK) {
1151             log.debug("Failed to disassociate instance inputs for {} error {}", toscaElementVertex.getUniqueId(), status);
1152             return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
1153         }
1154         status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.CALCULATED_CAPABILITIES);
1155         if (status != TitanOperationStatus.OK) {
1156             log.debug("Failed to disassociate calculated capabiliites for {} error {}", toscaElementVertex.getUniqueId(), status);
1157             return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
1158         }
1159         status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.FULLFILLED_CAPABILITIES);
1160         if (status != TitanOperationStatus.OK) {
1161             log.debug("Failed to disassociate fullfilled capabilities for {} error {}", toscaElementVertex.getUniqueId(), status);
1162             return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
1163         }
1164         status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.CALCULATED_CAP_PROPERTIES);
1165         if (status != TitanOperationStatus.OK) {
1166             log.debug("Failed to disassociate calculated capabiliites properties for {} error {}", toscaElementVertex.getUniqueId(), status);
1167             return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
1168         }
1169         status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.CALCULATED_REQUIREMENTS);
1170         if (status != TitanOperationStatus.OK) {
1171             log.debug("Failed to disassociate calculated requirements for {} error {}", toscaElementVertex.getUniqueId(), status);
1172             return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
1173         }
1174         status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.FULLFILLED_REQUIREMENTS);
1175         if (status != TitanOperationStatus.OK) {
1176             log.debug("Failed to disassociate full filled requirements for {} error {}", toscaElementVertex.getUniqueId(), status);
1177             return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
1178         }
1179         status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.INST_DEPLOYMENT_ARTIFACTS);
1180         if (status != TitanOperationStatus.OK) {
1181             log.debug("Failed to disassociate instance artifacts for {} error {}", toscaElementVertex.getUniqueId(), status);
1182             return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
1183         }
1184         status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.SERVICE_API_ARTIFACTS);
1185         if (status != TitanOperationStatus.OK) {
1186             log.debug("Failed to disassociate service api artifacts for {} error {}", toscaElementVertex.getUniqueId(), status);
1187             return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
1188         }
1189         status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.FORWARDING_PATH);
1190         if (status != TitanOperationStatus.OK) {
1191             log.debug("Failed to disassociate service api artifacts for {} error {}", toscaElementVertex.getUniqueId(), status);
1192             return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
1193         }
1194         status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.INTERFACE);
1195         if (status != TitanOperationStatus.OK) {
1196             log.debug("Failed to disassociate interfaces for {} error {}", toscaElementVertex.getUniqueId(), status);
1197             return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
1198         }
1199         status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.INSTANCE_ARTIFACTS);
1200         if (status != TitanOperationStatus.OK) {
1201             log.debug("Failed to disassociate instance artifact for {} error {}", toscaElementVertex.getUniqueId(), status);
1202             return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
1203         }
1204
1205         status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT,
1206                 EdgeLabelEnum.REQUIREMENTS);
1207         if (status != TitanOperationStatus.OK) {
1208             log.debug("Failed to disassociate requirements for {} error {}",
1209                     toscaElementVertex.getUniqueId(), status);
1210             return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
1211         }
1212         status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT,
1213                 EdgeLabelEnum.CAPABILITIES);
1214         if (status != TitanOperationStatus.OK) {
1215             log.debug("Failed to disassociate capabilities for {} error {}",
1216                     toscaElementVertex.getUniqueId(), status);
1217             return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
1218         }
1219         status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.INST_INTERFACES);
1220         if (status != TitanOperationStatus.OK) {
1221             log.debug("Failed to disassociate instances interfaces for {} error {}", toscaElementVertex.getUniqueId(), status);
1222             return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
1223         }
1224         toscaElementVertex.getVertex().remove();
1225         log.trace("Tosca element vertex for {} was removed", toscaElementVertex.getUniqueId());
1226
1227         return nodeType;
1228     }
1229
1230     @SuppressWarnings("unchecked")
1231     @Override
1232     public Either<TopologyTemplate, StorageOperationStatus> createToscaElement(ToscaElement toscaElement) {
1233         return createTopologyTemplate((TopologyTemplate) toscaElement);
1234     }
1235
1236     @Override
1237     protected <T extends ToscaElement> TitanOperationStatus setCategoriesFromGraph(GraphVertex vertexComponent, T toscaElement) {
1238         return setTopologyTempalteCategoriesFromGraph(vertexComponent, toscaElement);
1239     }
1240
1241     @Override
1242     protected <T extends ToscaElement> StorageOperationStatus validateCategories(T toscaElementToUpdate, GraphVertex elementV) {
1243         // Product isn't supported now!!
1244         // TODO add for Product
1245         if (toscaElementToUpdate.getComponentType() == ComponentTypeEnum.SERVICE) {
1246             return validateServiceCategory(toscaElementToUpdate, elementV);
1247         } else {
1248             // Resource
1249             return validateResourceCategory(toscaElementToUpdate, elementV);
1250         }
1251     }
1252
1253     @Override
1254     protected <T extends ToscaElement> StorageOperationStatus updateDerived(T toscaElementToUpdate, GraphVertex updateElementV) {
1255         // not relevant now for topology template
1256         return StorageOperationStatus.OK;
1257     }
1258
1259     @Override
1260     public <T extends ToscaElement> void fillToscaElementVertexData(GraphVertex elementV, T toscaElementToUpdate, JsonParseFlagEnum flag) {
1261         fillMetadata(elementV, (TopologyTemplate) toscaElementToUpdate, flag);
1262     }
1263
1264     private <T extends ToscaElement> StorageOperationStatus validateServiceCategory(T toscaElementToUpdate, GraphVertex elementV) {
1265         StorageOperationStatus status = StorageOperationStatus.OK;
1266         List<CategoryDefinition> newCategoryList = toscaElementToUpdate.getCategories();
1267         CategoryDefinition newCategory = newCategoryList.get(0);
1268
1269         Either<GraphVertex, TitanOperationStatus> childVertex = titanDao.getChildVertex(elementV, EdgeLabelEnum.CATEGORY, JsonParseFlagEnum.NoParse);
1270         if (childVertex.isRight()) {
1271             log.debug("failed to fetch {} for tosca element with id {}, error {}", EdgeLabelEnum.CATEGORY, elementV.getUniqueId(), childVertex.right().value());
1272             return DaoStatusConverter.convertTitanStatusToStorageStatus(childVertex.right().value());
1273         }
1274
1275         GraphVertex categoryV = childVertex.left().value();
1276         Map<GraphPropertyEnum, Object> metadataProperties = categoryV.getMetadataProperties();
1277         String categoryNameCurrent = (String) metadataProperties.get(GraphPropertyEnum.NAME);
1278
1279         String newCategoryName = newCategory.getName();
1280         if (newCategoryName != null && !newCategoryName.equals(categoryNameCurrent)) {
1281             // the category was changed
1282             Either<GraphVertex, StorageOperationStatus> getCategoryVertex = categoryOperation.getCategory(newCategoryName, VertexTypeEnum.SERVICE_CATEGORY);
1283
1284             if (getCategoryVertex.isRight()) {
1285                 return getCategoryVertex.right().value();
1286             }
1287             GraphVertex newCategoryV = getCategoryVertex.left().value();
1288             status = moveCategoryEdge(elementV, newCategoryV);
1289             log.debug("Going to update the category of the resource from {} to {}. status is {}", categoryNameCurrent, newCategory, status);
1290         }
1291         return status;
1292     }
1293
1294     public Either<GraphVertex, StorageOperationStatus> updateDistributionStatus(String uniqueId, User user, DistributionStatusEnum distributionStatus) {
1295
1296         Either<GraphVertex, StorageOperationStatus> result = null;
1297         String userId = user.getUserId();
1298         Either<GraphVertex, TitanOperationStatus> getRes = findUserVertex(userId);
1299         GraphVertex userVertex = null;
1300         GraphVertex serviceVertex = null;
1301         if (getRes.isRight()) {
1302             TitanOperationStatus status = getRes.right().value();
1303             CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Cannot find user {} in the graph. status is {}", userId, status);
1304             result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
1305         }
1306         if (result == null) {
1307             userVertex = getRes.left().value();
1308             getRes = titanDao.getVertexById(uniqueId, JsonParseFlagEnum.ParseMetadata);
1309             if (getRes.isRight()) {
1310                 TitanOperationStatus status = getRes.right().value();
1311                 log.debug("Cannot find service {} in the graph. status is {}", uniqueId, status);
1312                 result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
1313             }
1314         }
1315         if (result == null) {
1316             serviceVertex = getRes.left().value();
1317             Iterator<Edge> edgeIterator = serviceVertex.getVertex().edges(Direction.IN, EdgeLabelEnum.LAST_DISTRIBUTION_STATE_MODIFIER.name());
1318             if (edgeIterator.hasNext()) {
1319                 log.debug("Remove existing edge from user to component {}. Edge type is {}", userId, uniqueId, EdgeLabelEnum.LAST_DISTRIBUTION_STATE_MODIFIER);
1320                 edgeIterator.next().remove();
1321             }
1322         }
1323         if (result == null) {
1324             TitanOperationStatus status = titanDao.createEdge(userVertex, serviceVertex, EdgeLabelEnum.LAST_DISTRIBUTION_STATE_MODIFIER, null);
1325             if (status != TitanOperationStatus.OK) {
1326                 log.debug("Failed to associate user {} to component {}. Edge type is {}", userId, uniqueId, EdgeLabelEnum.LAST_DISTRIBUTION_STATE_MODIFIER);
1327                 result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
1328             }
1329         }
1330         if (result == null) {
1331             serviceVertex.addMetadataProperty(GraphPropertyEnum.DISTRIBUTION_STATUS, distributionStatus.name());
1332             long lastUpdateDate = System.currentTimeMillis();
1333             serviceVertex.setJsonMetadataField(JsonPresentationFields.LAST_UPDATE_DATE, lastUpdateDate);
1334             Either<GraphVertex, TitanOperationStatus> updateRes = titanDao.updateVertex(serviceVertex);
1335             if (updateRes.isRight()) {
1336                 result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(updateRes.right().value()));
1337             }
1338         }
1339         if (result == null) {
1340             result = Either.left(serviceVertex);
1341         }
1342         return result;
1343     }
1344
1345     /**
1346      * Returns list of ComponentInstanceProperty belonging to component instance capability specified by name, type and ownerId
1347      *
1348      * @param componentId
1349      * @param instanceId
1350      * @param capabilityName
1351      * @param capabilityType
1352      * @param ownerId
1353      * @return
1354      */
1355     public Either<List<ComponentInstanceProperty>, StorageOperationStatus> getComponentInstanceCapabilityProperties(String componentId, String instanceId, String capabilityName, String capabilityType, String ownerId) {
1356
1357         Either<List<ComponentInstanceProperty>, StorageOperationStatus> result = null;
1358         Map<String, MapCapabilityProperty> mapPropertiesDataDefinition = null;
1359         Either<GraphVertex, StorageOperationStatus> componentByLabelAndId = getComponentByLabelAndId(componentId, ToscaElementTypeEnum.TOPOLOGY_TEMPLATE, JsonParseFlagEnum.NoParse);
1360         if (componentByLabelAndId.isRight()) {
1361             result = Either.right(componentByLabelAndId.right().value());
1362         }
1363         if (componentByLabelAndId.isLeft()) {
1364             Either<Map<String, MapCapabilityProperty>, TitanOperationStatus> getDataRes = getDataFromGraph(componentByLabelAndId.left().value(), EdgeLabelEnum.CALCULATED_CAP_PROPERTIES);
1365             if (getDataRes.isRight()) {
1366                 result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(getDataRes.right().value()));
1367             } else {
1368                 mapPropertiesDataDefinition = getDataRes.left().value();
1369             }
1370         }
1371         if (isNotEmptyMapOfProperties(instanceId, mapPropertiesDataDefinition)) {
1372             result = Either.left(findComponentInstanceCapabilityProperties(instanceId, capabilityName, capabilityType, ownerId, mapPropertiesDataDefinition.get(instanceId).getMapToscaDataDefinition()));
1373         }
1374         return result;
1375     }
1376
1377     public StorageOperationStatus updateComponentInstanceCapabilityProperties(Component containerComponent, String componentInstanceId, MapCapabilityProperty instanceProperties) {
1378         return updateToscaDataDeepElementsBlockToToscaElement(containerComponent.getUniqueId(), EdgeLabelEnum.CALCULATED_CAP_PROPERTIES, instanceProperties, componentInstanceId);
1379     }
1380
1381     public StorageOperationStatus updateComponentInstanceInterfaces(Component containerComponent,
1382                                                                     String componentInstanceId,
1383                                                                     MapInterfaceDataDefinition instanceInterfaces) {
1384         if (MapUtils.isNotEmpty(instanceInterfaces.getMapToscaDataDefinition())) {
1385             return updateToscaDataDeepElementsBlockToToscaElement(containerComponent.getUniqueId(),
1386                     EdgeLabelEnum.INST_INTERFACES, instanceInterfaces, componentInstanceId);
1387         }
1388         return StorageOperationStatus.OK;
1389     }
1390
1391
1392     private boolean isNotEmptyMapOfProperties(String instanceId, Map<String, MapCapabilityProperty> mapPropertiesDataDefinition) {
1393         return MapUtils.isNotEmpty(mapPropertiesDataDefinition) &&
1394                 mapPropertiesDataDefinition.get(instanceId) != null &&
1395                 MapUtils.isNotEmpty(mapPropertiesDataDefinition.get(instanceId).getMapToscaDataDefinition());
1396     }
1397
1398     private List<ComponentInstanceProperty> findComponentInstanceCapabilityProperties(String instanceId, String capabilityName, String capabilityType, String ownerId, Map<String, MapPropertiesDataDefinition> propertiesMap) {
1399         List<ComponentInstanceProperty> capPropsList = null;
1400         for (Entry<String, MapPropertiesDataDefinition> capProp : propertiesMap.entrySet()) {
1401             if (isBelongingPropertyMap(instanceId, capabilityName, capabilityType, ownerId, capProp)) {
1402                 Map<String, PropertyDataDefinition> capMap = capProp.getValue().getMapToscaDataDefinition();
1403                 if (capMap != null && !capMap.isEmpty()) {
1404                     capPropsList = capMap.values().stream().map(ComponentInstanceProperty::new).collect(Collectors.toList());
1405                     break;
1406                 }
1407             }
1408         }
1409         if (capPropsList == null) {
1410             capPropsList = new ArrayList<>();
1411         }
1412         return capPropsList;
1413     }
1414
1415     private boolean isBelongingPropertyMap(String instanceId, String capabilityName, String capabilityType, String ownerId, Entry<String, MapPropertiesDataDefinition> capProp) {
1416         if (capProp != null) {
1417             String[] path = capProp.getKey().split(ModelConverter.CAP_PROP_DELIM);
1418             if (path.length < 4) {
1419                 log.debug("wrong key format for capabilty, key {}", capProp);
1420                 return false;
1421             }
1422             return path[path.length - 2].equals(capabilityType) && path[path.length - 1].equals(capabilityName) && path[1].equals(ownerId) && path[0].equals(instanceId);
1423         }
1424         return false;
1425     }
1426
1427     public StorageOperationStatus addPolicyToToscaElement(GraphVertex componentV, PolicyDefinition policyDefinition, int counter) {
1428         fillPolicyDefinition(componentV, policyDefinition, counter);
1429         return addToscaDataToToscaElement(componentV, EdgeLabelEnum.POLICIES, VertexTypeEnum.POLICIES, policyDefinition, JsonPresentationFields.UNIQUE_ID);
1430     }
1431
1432     public StorageOperationStatus addPoliciesToToscaElement(GraphVertex componentV, List<PolicyDefinition> policies) {
1433         return addToscaDataToToscaElement(componentV, EdgeLabelEnum.POLICIES, VertexTypeEnum.POLICIES, policies, JsonPresentationFields.UNIQUE_ID);
1434     }
1435
1436     public StorageOperationStatus updatePolicyOfToscaElement(GraphVertex componentV, PolicyDefinition policyDefinition) {
1437         return updateToscaDataOfToscaElement(componentV, EdgeLabelEnum.POLICIES, VertexTypeEnum.POLICIES, policyDefinition, JsonPresentationFields.UNIQUE_ID);
1438     }
1439
1440     public StorageOperationStatus updatePoliciesOfToscaElement(GraphVertex componentV, List<PolicyDefinition> policiesDefinitions) {
1441         return updateToscaDataOfToscaElement(componentV, EdgeLabelEnum.POLICIES, VertexTypeEnum.POLICIES, policiesDefinitions, JsonPresentationFields.UNIQUE_ID);
1442     }
1443
1444     public StorageOperationStatus removePolicyFromToscaElement(GraphVertex componentV, String policyId) {
1445         return deleteToscaDataElement(componentV, EdgeLabelEnum.POLICIES, VertexTypeEnum.POLICIES, policyId, JsonPresentationFields.UNIQUE_ID);
1446     }
1447
1448     public StorageOperationStatus updateGroupOfToscaElement(GraphVertex componentV, GroupDefinition groupDefinition) {
1449         return updateToscaDataOfToscaElement(componentV, EdgeLabelEnum.GROUPS, VertexTypeEnum.GROUPS, groupDefinition, JsonPresentationFields.NAME);
1450     }
1451
1452     private void fillPolicyDefinition(GraphVertex componentV, PolicyDefinition policyDefinition, int counter) {
1453         String policyName = buildSubComponentName((String) componentV.getJsonMetadataField(JsonPresentationFields.NAME), policyDefinition.getPolicyTypeName(), counter);
1454         policyDefinition.setName(policyName);
1455         policyDefinition.setInvariantName(policyName);
1456         policyDefinition.setComponentName((String) componentV.getJsonMetadataField(JsonPresentationFields.NAME));
1457         policyDefinition.setUniqueId(UniqueIdBuilder.buildPolicyUniqueId(componentV.getUniqueId(), policyName));
1458         policyDefinition.setInvariantUUID(UniqueIdBuilder.buildInvariantUUID());
1459         policyDefinition.setPolicyUUID(UniqueIdBuilder.generateUUID());
1460     }
1461
1462     public static String buildSubComponentName(String componentName, String subComponentTypeName, int counter) {
1463         String normalizedComponentName = ValidationUtils.normalizeComponentInstanceName(componentName);
1464         String typeSuffix = subComponentTypeName.substring(subComponentTypeName.lastIndexOf('.') + 1, subComponentTypeName.length());
1465         return normalizedComponentName + Constants.GROUP_POLICY_NAME_DELIMETER + typeSuffix + Constants.GROUP_POLICY_NAME_DELIMETER + counter;
1466     }
1467
1468     void revertNamesOfCalculatedCapabilitiesRequirements(String componentId, TopologyTemplate toscaElement) {
1469         if (MapUtils.isNotEmpty(toscaElement.getComponentInstances()) || MapUtils.isNotEmpty(toscaElement.getGroups())) {
1470             GraphVertex toscaElementV = titanDao.getVertexById(componentId, JsonParseFlagEnum.NoParse)
1471                     .left()
1472                     .on(this::throwStorageException);
1473             if (MapUtils.isNotEmpty(toscaElement.getComponentInstances())) {
1474                 toscaElement.getComponentInstances().values().forEach(i -> CapabilityRequirementNameResolver.revertNamesOfCalculatedCapabilitiesRequirements(toscaElement, i.getUniqueId(), this::getOriginToscaElement));
1475             }
1476             if (MapUtils.isNotEmpty(toscaElement.getGroups())) {
1477                 toscaElement.getGroups().values().forEach(g -> CapabilityRequirementNameResolver.revertNamesOfCalculatedCapabilitiesRequirements(toscaElement, g.getUniqueId(), this::getOriginToscaElement));
1478             }
1479             topologyTemplateOperation.updateFullToscaData(toscaElementV, EdgeLabelEnum.CALCULATED_CAPABILITIES, VertexTypeEnum.CALCULATED_CAPABILITIES, toscaElement.getCalculatedCapabilities());
1480             topologyTemplateOperation.updateFullToscaData(toscaElementV, EdgeLabelEnum.CALCULATED_REQUIREMENTS, VertexTypeEnum.CALCULATED_REQUIREMENTS, toscaElement.getCalculatedRequirements());
1481             topologyTemplateOperation.updateFullToscaData(toscaElementV, EdgeLabelEnum.CALCULATED_CAP_PROPERTIES, VertexTypeEnum.CALCULATED_CAP_PROPERTIES, toscaElement.getCalculatedCapabilitiesProperties());
1482         }
1483     }
1484
1485     public void updateNamesOfCalculatedCapabilitiesRequirements(String componentId, TopologyTemplate toscaElement) {
1486         if (MapUtils.isNotEmpty(toscaElement.getComponentInstances()) || MapUtils.isNotEmpty(toscaElement.getGroups())) {
1487             GraphVertex toscaElementV = titanDao.getVertexById(componentId, JsonParseFlagEnum.NoParse)
1488                     .left()
1489                     .on(this::throwStorageException);
1490             if (MapUtils.isNotEmpty(toscaElement.getComponentInstances())) {
1491                 toscaElement.getComponentInstances().values().forEach(i -> CapabilityRequirementNameResolver.updateNamesOfCalculatedCapabilitiesRequirements(toscaElement, i.getUniqueId(), i.getNormalizedName(), this::getOriginToscaElement));
1492             }
1493             if (MapUtils.isNotEmpty(toscaElement.getGroups())) {
1494                 toscaElement.getGroups().values().forEach(g -> CapabilityRequirementNameResolver.updateNamesOfCalculatedCapabilitiesRequirements(toscaElement, g.getUniqueId(), g.getName(), this::getOriginToscaElement));
1495             }
1496             topologyTemplateOperation.updateFullToscaData(toscaElementV, EdgeLabelEnum.CALCULATED_CAPABILITIES, VertexTypeEnum.CALCULATED_CAPABILITIES, toscaElement.getCalculatedCapabilities());
1497             topologyTemplateOperation.updateFullToscaData(toscaElementV, EdgeLabelEnum.CALCULATED_REQUIREMENTS, VertexTypeEnum.CALCULATED_REQUIREMENTS, toscaElement.getCalculatedRequirements());
1498             topologyTemplateOperation.updateFullToscaData(toscaElementV, EdgeLabelEnum.CALCULATED_CAP_PROPERTIES, VertexTypeEnum.CALCULATED_CAP_PROPERTIES, toscaElement.getCalculatedCapabilitiesProperties());
1499         }
1500     }
1501
1502     private GraphVertex throwStorageException(TitanOperationStatus status) {
1503         throw new StorageException(status);
1504     }
1505
1506     private ToscaElement getOriginToscaElement(ComponentInstanceDataDefinition instance) {
1507         log.debug("#getOriginToscaElement - origin name: {}", instance.getComponentName());
1508         ToscaElementTypeEnum elementType = detectToscaType(instance.getOriginType());
1509         Either<ToscaElement, StorageOperationStatus> getOriginRes;
1510         if (elementType == ToscaElementTypeEnum.TOPOLOGY_TEMPLATE) {
1511             getOriginRes = this.getToscaElement(CapabilityRequirementNameResolver.getActualComponentUid(instance), getFilter());
1512
1513         } else {
1514             getOriginRes = nodeTypeOperation.getToscaElement(CapabilityRequirementNameResolver.getActualComponentUid(instance), getFilter());
1515         }
1516         if (getOriginRes.isRight()) {
1517             log.debug("Failed to get an origin component with uniqueId {}", CapabilityRequirementNameResolver.getActualComponentUid(instance));
1518             throw new StorageException(getOriginRes.right().value());
1519         }
1520         return getOriginRes.left().value();
1521     }
1522
1523     private ToscaElementTypeEnum detectToscaType(OriginTypeEnum originType) {
1524         log.debug("#detectToscaType - type: {}", originType);
1525         if (nodeTypeSet.contains(originType)){
1526             return ToscaElementTypeEnum.NODE_TYPE;
1527         } else {
1528             return ToscaElementTypeEnum.TOPOLOGY_TEMPLATE;
1529         }
1530     }
1531
1532     private ComponentParametersView getFilter() {
1533         ComponentParametersView filter = new ComponentParametersView();
1534         filter.setIgnoreCapabilities(false);
1535         filter.setIgnoreCapabiltyProperties(false);
1536         filter.setIgnoreRequirements(false);
1537         return filter;
1538     }
1539     public void updateCapReqOwnerId(String componentId, TopologyTemplate toscaElement) {
1540         GraphVertex toscaElementV = titanDao.getVertexById(componentId, JsonParseFlagEnum.NoParse)
1541                 .left().on(this::throwStorageException);
1542         updateCapOwnerId(toscaElement, componentId);
1543         updateReqOwnerId(toscaElement, componentId);
1544         topologyTemplateOperation
1545
1546                 .updateFullToscaData(toscaElementV, EdgeLabelEnum.CAPABILITIES,
1547                         VertexTypeEnum.CAPABILITIES, toscaElement.getCapabilities());
1548         topologyTemplateOperation
1549                 .updateFullToscaData(toscaElementV, EdgeLabelEnum.REQUIREMENTS,
1550                         VertexTypeEnum.REQUIREMENTS, toscaElement.getRequirements());
1551     }
1552
1553     private void updateCapOwnerId(ToscaElement toscaElement, String ownerId) {
1554         if(MapUtils.isNotEmpty(toscaElement.getCapabilities())) {
1555             toscaElement.getCapabilities().values().stream().flatMap(listCapDef -> listCapDef.getListToscaDataDefinition().stream())
1556                     .forEach(capabilityDefinition -> capabilityDefinition.setOwnerId(ownerId));
1557         }
1558     }
1559
1560     private void updateReqOwnerId(ToscaElement toscaElement, String ownerId) {
1561         if(MapUtils.isNotEmpty(toscaElement.getRequirements())) {
1562             toscaElement.getRequirements().values().stream().flatMap(listReqDef -> listReqDef.getListToscaDataDefinition().stream())
1563                     .forEach(requirementDefinition -> requirementDefinition.setOwnerId(ownerId));
1564         }
1565     }
1566
1567 }