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