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