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