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