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