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