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