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