Improve test coverage
[sdc.git] / catalog-model / src / main / java / org / openecomp / sdc / be / model / jsonjanusgraph / operations / TopologyTemplateOperation.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.be.model.jsonjanusgraph.operations;
22
23 import com.google.gson.reflect.TypeToken;
24 import fj.data.Either;
25 import java.lang.reflect.Type;
26 import java.util.*;
27 import java.util.Map.Entry;
28 import java.util.stream.Collectors;
29 import org.apache.commons.collections.MapUtils;
30 import org.apache.tinkerpop.gremlin.structure.Direction;
31 import org.apache.tinkerpop.gremlin.structure.Edge;
32 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
33 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
34 import org.openecomp.sdc.be.dao.jsongraph.types.EdgeLabelEnum;
35 import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum;
36 import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum;
37 import org.openecomp.sdc.be.datatypes.category.MetadataKeyDataDefinition;
38 import org.openecomp.sdc.be.datatypes.elements.AttributeDataDefinition;
39 import org.openecomp.sdc.be.datatypes.elements.MapAttributesDataDefinition;
40 import org.openecomp.sdc.be.datatypes.elements.MapCapabilityProperty;
41 import org.openecomp.sdc.be.datatypes.elements.MapListCapabilityDataDefinition;
42 import org.openecomp.sdc.be.datatypes.elements.AdditionalInfoParameterDataDefinition;
43 import org.openecomp.sdc.be.datatypes.elements.ArtifactDataDefinition;
44 import org.openecomp.sdc.be.datatypes.elements.CINodeFilterDataDefinition;
45 import org.openecomp.sdc.be.datatypes.elements.ComponentInstanceDataDefinition;
46 import org.openecomp.sdc.be.datatypes.elements.CompositionDataDefinition;
47 import org.openecomp.sdc.be.datatypes.elements.DataTypeDataDefinition;
48 import org.openecomp.sdc.be.datatypes.elements.ForwardingPathDataDefinition;
49 import org.openecomp.sdc.be.datatypes.elements.GroupDataDefinition;
50 import org.openecomp.sdc.be.datatypes.elements.InterfaceDataDefinition;
51 import org.openecomp.sdc.be.datatypes.elements.ListCapabilityDataDefinition;
52 import org.openecomp.sdc.be.datatypes.elements.ListRequirementDataDefinition;
53 import org.openecomp.sdc.be.datatypes.elements.MapArtifactDataDefinition;
54 import org.openecomp.sdc.be.datatypes.elements.MapDataDefinition;
55 import org.openecomp.sdc.be.datatypes.elements.MapGroupsDataDefinition;
56 import org.openecomp.sdc.be.datatypes.elements.MapInterfaceDataDefinition;
57 import org.openecomp.sdc.be.datatypes.elements.MapListRequirementDataDefinition;
58 import org.openecomp.sdc.be.datatypes.elements.MapPropertiesDataDefinition;
59 import org.openecomp.sdc.be.datatypes.elements.PolicyDataDefinition;
60 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
61 import org.openecomp.sdc.be.datatypes.elements.SubstitutionFilterDataDefinition;
62 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
63 import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum;
64 import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
65 import org.openecomp.sdc.be.datatypes.enums.OriginTypeEnum;
66 import org.openecomp.sdc.be.datatypes.tosca.ToscaDataDefinition;
67 import org.openecomp.sdc.be.model.Component;
68 import org.openecomp.sdc.be.model.ComponentInstanceProperty;
69 import org.openecomp.sdc.be.model.ComponentParametersView;
70 import org.openecomp.sdc.be.model.DistributionStatusEnum;
71 import org.openecomp.sdc.be.model.GroupDefinition;
72 import org.openecomp.sdc.be.model.PolicyDefinition;
73 import org.openecomp.sdc.be.model.User;
74 import org.openecomp.sdc.be.model.category.CategoryDefinition;
75 import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.TopologyTemplate;
76 import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.ToscaElement;
77 import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.ToscaElementTypeEnum;
78 import org.openecomp.sdc.be.model.jsonjanusgraph.utils.CapabilityRequirementNameResolver;
79 import org.openecomp.sdc.be.model.jsonjanusgraph.utils.ModelConverter;
80 import org.openecomp.sdc.be.model.operations.StorageException;
81 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
82 import org.openecomp.sdc.be.model.operations.impl.DaoStatusConverter;
83 import org.openecomp.sdc.be.model.operations.impl.UniqueIdBuilder;
84 import org.openecomp.sdc.common.api.Constants;
85 import org.openecomp.sdc.common.jsongraph.util.CommonUtility;
86 import org.openecomp.sdc.common.jsongraph.util.CommonUtility.LogLevelEnum;
87 import org.openecomp.sdc.common.log.wrappers.Logger;
88 import org.openecomp.sdc.common.util.ValidationUtils;
89 import org.springframework.beans.factory.annotation.Autowired;
90
91
92 @org.springframework.stereotype.Component("topology-template-operation")
93 public class TopologyTemplateOperation extends ToscaElementOperation {
94
95     private static final Logger log = Logger.getLogger(TopologyTemplateOperation.class);
96
97     @Autowired
98     private ArchiveOperation archiveOperation;
99
100     public Either<TopologyTemplate, StorageOperationStatus> createTopologyTemplate(TopologyTemplate topologyTemplate) {
101         topologyTemplate.generateUUID();
102
103         topologyTemplate = getResourceMetaDataFromResource(topologyTemplate);
104         String resourceUniqueId = topologyTemplate.getUniqueId();
105         if (resourceUniqueId == null) {
106             resourceUniqueId = UniqueIdBuilder.buildResourceUniqueId();
107             topologyTemplate.setUniqueId(resourceUniqueId);
108         }
109
110         GraphVertex topologyTemplateVertex = new GraphVertex();
111         topologyTemplateVertex = fillMetadata(topologyTemplateVertex, topologyTemplate, JsonParseFlagEnum.ParseAll);
112
113         Either<GraphVertex, JanusGraphOperationStatus> createdVertex = janusGraphDao.createVertex(topologyTemplateVertex);
114         if (createdVertex.isRight()) {
115             JanusGraphOperationStatus status = createdVertex.right().value();
116             log.debug( "Error returned after creating topology template data node {}. status returned is ", topologyTemplateVertex, status);
117             return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
118         }
119
120         StorageOperationStatus assosiateCommon = assosiateCommonForToscaElement(topologyTemplateVertex, topologyTemplate);
121         if (assosiateCommon != StorageOperationStatus.OK) {
122             return Either.right(assosiateCommon);
123         }
124
125         StorageOperationStatus associateCategory = assosiateMetadataToCategory(topologyTemplateVertex, topologyTemplate);
126         if (associateCategory != StorageOperationStatus.OK) {
127             return Either.right(associateCategory);
128         }
129
130         StorageOperationStatus associateInputs = associateInputsToComponent(topologyTemplateVertex, topologyTemplate);
131         if (associateInputs != StorageOperationStatus.OK) {
132             return Either.right(associateInputs);
133         }
134         StorageOperationStatus associateGroups = associateGroupsToComponent(topologyTemplateVertex, topologyTemplate);
135         if (associateGroups != StorageOperationStatus.OK) {
136             return Either.right(associateGroups);
137         }
138         StorageOperationStatus associatePolicies = associatePoliciesToComponent(topologyTemplateVertex, topologyTemplate);
139         if (associatePolicies != StorageOperationStatus.OK) {
140             return Either.right(associatePolicies);
141         }
142         StorageOperationStatus associateInstAttr = associateInstAttributesToComponent(topologyTemplateVertex, topologyTemplate);
143         if (associateInstAttr != StorageOperationStatus.OK) {
144             return Either.right(associateInstAttr);
145         }
146         StorageOperationStatus associateInstProperties = associateInstPropertiesToComponent(topologyTemplateVertex, topologyTemplate);
147         if (associateInstProperties != StorageOperationStatus.OK) {
148             return Either.right(associateInstProperties);
149         }
150         StorageOperationStatus associateInstInputs = associateInstInputsToComponent(topologyTemplateVertex, topologyTemplate);
151         if (associateInstProperties != StorageOperationStatus.OK) {
152             return Either.right(associateInstInputs);
153         }
154         StorageOperationStatus associateInstGroups = associateInstGroupsToComponent(topologyTemplateVertex, topologyTemplate);
155         if (associateInstGroups != StorageOperationStatus.OK) {
156             return Either.right(associateInstInputs);
157         }
158
159         StorageOperationStatus associateRequirements = associateRequirementsToResource(topologyTemplateVertex, topologyTemplate);
160         if (associateRequirements != StorageOperationStatus.OK) {
161             return Either.right(associateRequirements);
162         }
163
164         StorageOperationStatus associateCapabilities = associateCapabilitiesToResource(topologyTemplateVertex, topologyTemplate);
165         if (associateCapabilities != StorageOperationStatus.OK) {
166             return Either.right(associateCapabilities);
167         }
168
169         StorageOperationStatus associateArtifacts = associateTopologyTemplateArtifactsToComponent(topologyTemplateVertex, topologyTemplate);
170         if (associateArtifacts != StorageOperationStatus.OK) {
171             return Either.right(associateArtifacts);
172         }
173
174         StorageOperationStatus addAdditionalInformation = addAdditionalInformationToResource(topologyTemplateVertex, topologyTemplate);
175         if (addAdditionalInformation != StorageOperationStatus.OK) {
176             return Either.right(addAdditionalInformation);
177         }
178         StorageOperationStatus associateCapProperties = associateCapPropertiesToResource(topologyTemplateVertex, topologyTemplate);
179         if (associateCapProperties != StorageOperationStatus.OK) {
180             return Either.right(associateCapProperties);
181         }
182
183         StorageOperationStatus associateInterfaces = associateInterfacesToComponent(topologyTemplateVertex, topologyTemplate);
184         if (associateInterfaces != StorageOperationStatus.OK) {
185             return Either.right(associateInterfaces);
186         }
187
188         StorageOperationStatus associatePathProperties = associateForwardingPathToResource(topologyTemplateVertex, topologyTemplate);
189         if (associateCapProperties != StorageOperationStatus.OK) {
190             return Either.right(associatePathProperties);
191         }
192         return Either.left(topologyTemplate);
193     }
194
195     private StorageOperationStatus associatePoliciesToComponent(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
196         return associatePoliciesToComponent(nodeTypeVertex, topologyTemplate.getPolicies());
197     }
198
199     private StorageOperationStatus associatePoliciesToComponent(GraphVertex nodeTypeVertex,    Map<String, PolicyDataDefinition> policies) {
200         if (policies != null && !policies.isEmpty()) {
201             policies.values().stream().filter(p -> p.getUniqueId() == null).forEach(p -> {
202                 String uid = UniqueIdBuilder.buildGroupingUid(nodeTypeVertex.getUniqueId(), p.getName());
203                 p.setUniqueId(uid);
204             });
205             Either<GraphVertex, StorageOperationStatus> assosiateElementToData = associateElementToData(nodeTypeVertex, VertexTypeEnum.POLICIES, EdgeLabelEnum.POLICIES, policies);
206             if (assosiateElementToData.isRight()) {
207                 return assosiateElementToData.right().value();
208             }
209         }
210         return StorageOperationStatus.OK;
211     }
212
213     private StorageOperationStatus associateForwardingPathToResource(GraphVertex topologyTemplateVertex, TopologyTemplate topologyTemplate) {
214         Map<String, ForwardingPathDataDefinition> forwardingPaths = topologyTemplate.getForwardingPaths();
215         return associateForwardingPathToComponent(topologyTemplateVertex,forwardingPaths);
216     }
217
218     private StorageOperationStatus associateCapPropertiesToResource(GraphVertex topologyTemplateVertex, TopologyTemplate topologyTemplate) {
219         Map<String, MapCapabilityProperty> calculatedCapProperties = topologyTemplate
220                 .getCalculatedCapabilitiesProperties();
221         if (MapUtils.isNotEmpty(calculatedCapProperties)) {
222             Either<GraphVertex, StorageOperationStatus> associateElementToData = associateElementToData
223                     (topologyTemplateVertex, VertexTypeEnum.CALCULATED_CAP_PROPERTIES,
224                             EdgeLabelEnum.CALCULATED_CAP_PROPERTIES, calculatedCapProperties);
225             if (associateElementToData.isRight()) {
226                 return associateElementToData.right().value();
227             }
228         }
229
230         Map<String, MapPropertiesDataDefinition> capabilitiesProperties = topologyTemplate.getCapabilitiesProperties();
231         if (MapUtils.isNotEmpty(capabilitiesProperties)) {
232             Either<GraphVertex, StorageOperationStatus> associateElementToData =
233                     associateElementToData(topologyTemplateVertex, VertexTypeEnum.CAPABILITIES_PROPERTIES,
234                             EdgeLabelEnum.CAPABILITIES_PROPERTIES, capabilitiesProperties);
235             if (associateElementToData.isRight()) {
236                 return associateElementToData.right().value();
237             }
238         }
239         return StorageOperationStatus.OK;
240     }
241
242     private StorageOperationStatus associateCapabilitiesToResource(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
243         Map<String, MapListCapabilityDataDefinition> calculatedCapabilities = topologyTemplate.getCalculatedCapabilities();
244         if (calculatedCapabilities != null && !calculatedCapabilities.isEmpty()) {
245             Either<GraphVertex, StorageOperationStatus> assosiateElementToData = associateElementToData(nodeTypeVertex, VertexTypeEnum.CALCULATED_CAPABILITIES, EdgeLabelEnum.CALCULATED_CAPABILITIES, calculatedCapabilities);
246             if (assosiateElementToData.isRight()) {
247                 return assosiateElementToData.right().value();
248             }
249         }
250         Map<String, MapListCapabilityDataDefinition> fullfilledCapabilities = topologyTemplate.getFullfilledCapabilities();
251         if (fullfilledCapabilities != null && !fullfilledCapabilities.isEmpty()) {
252             Either<GraphVertex, StorageOperationStatus> assosiateElementToData = associateElementToData(nodeTypeVertex, VertexTypeEnum.FULLFILLED_CAPABILITIES, EdgeLabelEnum.FULLFILLED_CAPABILITIES, fullfilledCapabilities);
253             if (assosiateElementToData.isRight()) {
254                 return assosiateElementToData.right().value();
255             }
256         }
257         Map<String, ListCapabilityDataDefinition> capabilities = topologyTemplate.getCapabilities();
258         if(MapUtils.isNotEmpty(capabilities)) {
259             Either<GraphVertex, StorageOperationStatus> associateElementToData =
260                     associateElementToData(nodeTypeVertex, VertexTypeEnum.CAPABILITIES,
261                             EdgeLabelEnum.CAPABILITIES, capabilities);
262             if (associateElementToData.isRight()) {
263                 return associateElementToData.right().value();
264             }
265         }
266         return StorageOperationStatus.OK;
267
268     }
269
270     private StorageOperationStatus associateRequirementsToResource(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
271         Map<String, MapListRequirementDataDefinition> calculatedRequirements = topologyTemplate.getCalculatedRequirements();
272         if (calculatedRequirements != null && !calculatedRequirements.isEmpty()) {
273             Either<GraphVertex, StorageOperationStatus> assosiateElementToData = associateElementToData(nodeTypeVertex, VertexTypeEnum.CALCULATED_REQUIREMENTS, EdgeLabelEnum.CALCULATED_REQUIREMENTS, calculatedRequirements);
274             if (assosiateElementToData.isRight()) {
275                 return assosiateElementToData.right().value();
276             }
277         }
278         Map<String, MapListRequirementDataDefinition> fullfilledRequirements = topologyTemplate.getFullfilledRequirements();
279         if (fullfilledRequirements != null && !fullfilledRequirements.isEmpty()) {
280             Either<GraphVertex, StorageOperationStatus> assosiateElementToData = associateElementToData(nodeTypeVertex, VertexTypeEnum.FULLFILLED_REQUIREMENTS, EdgeLabelEnum.FULLFILLED_REQUIREMENTS, fullfilledRequirements);
281             if (assosiateElementToData.isRight()) {
282                 return assosiateElementToData.right().value();
283             }
284         }
285         Map<String, ListRequirementDataDefinition> requirements = topologyTemplate.getRequirements();
286         if(MapUtils.isNotEmpty(requirements)) {
287             Either<GraphVertex, StorageOperationStatus> associateElementToData =
288                     associateElementToData(nodeTypeVertex, VertexTypeEnum.REQUIREMENTS,
289                             EdgeLabelEnum.REQUIREMENTS, requirements);
290             if (associateElementToData.isRight()) {
291                 return associateElementToData.right().value();
292             }
293         }
294         return StorageOperationStatus.OK;
295     }
296
297     private StorageOperationStatus associateTopologyTemplateArtifactsToComponent(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
298         Map<String, ArtifactDataDefinition> addInformation = topologyTemplate.getServiceApiArtifacts();
299
300         if (addInformation != null && !addInformation.isEmpty()) {
301             addInformation.values().stream().filter(a -> a.getUniqueId() == null).forEach(a -> {
302                 String uniqueId = UniqueIdBuilder.buildPropertyUniqueId(nodeTypeVertex.getUniqueId().toLowerCase(), a.getArtifactLabel().toLowerCase());
303                 a.setUniqueId(uniqueId);
304             });
305             Either<GraphVertex, StorageOperationStatus> assosiateElementToData = associateElementToData(nodeTypeVertex, VertexTypeEnum.SERVICE_API_ARTIFACTS, EdgeLabelEnum.SERVICE_API_ARTIFACTS, addInformation);
306             if (assosiateElementToData.isRight()) {
307                 return assosiateElementToData.right().value();
308             }
309         }
310         Map<String, MapArtifactDataDefinition> instArtifacts = topologyTemplate.getInstDeploymentArtifacts();
311
312         if (instArtifacts != null && !instArtifacts.isEmpty()) {
313             Either<GraphVertex, StorageOperationStatus> assosiateElementToData = associateElementToData(nodeTypeVertex, VertexTypeEnum.INST_DEPLOYMENT_ARTIFACTS, EdgeLabelEnum.INST_DEPLOYMENT_ARTIFACTS, instArtifacts);
314             if (assosiateElementToData.isRight()) {
315                 return assosiateElementToData.right().value();
316             }
317         }
318         Map<String, MapArtifactDataDefinition> instInfoArtifacts = topologyTemplate.getInstanceArtifacts();
319
320         if (instInfoArtifacts != null && !instInfoArtifacts.isEmpty()) {
321             Either<GraphVertex, StorageOperationStatus> assosiateElementToData = associateElementToData(nodeTypeVertex, VertexTypeEnum.INSTANCE_ARTIFACTS, EdgeLabelEnum.INSTANCE_ARTIFACTS, instInfoArtifacts);
322             if (assosiateElementToData.isRight()) {
323                 return assosiateElementToData.right().value();
324             }
325         }
326         Map<String, ListRequirementDataDefinition> requirements = topologyTemplate.getRequirements();
327         if(MapUtils.isNotEmpty(requirements)) {
328             Either<GraphVertex, StorageOperationStatus> associateElementToData =
329                     associateElementToData(nodeTypeVertex, VertexTypeEnum.REQUIREMENTS,
330                             EdgeLabelEnum.REQUIREMENTS, requirements);
331             if (associateElementToData.isRight()) {
332                 return associateElementToData.right().value();
333             }
334         }
335         return StorageOperationStatus.OK;
336     }
337
338     private StorageOperationStatus addAdditionalInformationToResource(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
339
340         Map<String, AdditionalInfoParameterDataDefinition> addInformation = topologyTemplate.getAdditionalInformation();
341
342         if (addInformation != null && !addInformation.isEmpty()) {
343             Either<GraphVertex, StorageOperationStatus> assosiateElementToData = associateElementToData(nodeTypeVertex, VertexTypeEnum.ADDITIONAL_INFORMATION, EdgeLabelEnum.ADDITIONAL_INFORMATION, addInformation);
344             if (assosiateElementToData.isRight()) {
345                 return assosiateElementToData.right().value();
346             }
347         }
348         return StorageOperationStatus.OK;
349     }
350
351     public StorageOperationStatus associateInstPropertiesToComponent(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
352         Map<String, MapPropertiesDataDefinition> instProps = topologyTemplate.getInstProperties();
353         return associateInstPropertiesToComponent(nodeTypeVertex, instProps);
354     }
355
356     public StorageOperationStatus associateInstInputsToComponent(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
357         Map<String, MapPropertiesDataDefinition> instProps = topologyTemplate.getInstInputs();
358         return associateInstInputsToComponent(nodeTypeVertex, instProps);
359     }
360
361     public StorageOperationStatus associateInstGroupsToComponent(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
362         Map<String, MapGroupsDataDefinition> instGroups = topologyTemplate.getInstGroups();
363         return associateInstGroupsToComponent(nodeTypeVertex, instGroups);
364     }
365
366
367     public StorageOperationStatus associateInstPropertiesToComponent(GraphVertex nodeTypeVertex, Map<String, MapPropertiesDataDefinition> instProps) {
368         if (instProps != null && !instProps.isEmpty()) {
369             Either<GraphVertex, StorageOperationStatus> assosiateElementToData = associateElementToData(nodeTypeVertex, VertexTypeEnum.INST_PROPERTIES, EdgeLabelEnum.INST_PROPERTIES, instProps);
370             if (assosiateElementToData.isRight()) {
371                 return assosiateElementToData.right().value();
372             }
373         }
374         return StorageOperationStatus.OK;
375     }
376
377     public StorageOperationStatus associateInstInputsToComponent(GraphVertex nodeTypeVertex, Map<String, MapPropertiesDataDefinition> instInputs) {
378         if (instInputs != null && !instInputs.isEmpty()) {
379             Either<GraphVertex, StorageOperationStatus> assosiateElementToData = associateElementToData(nodeTypeVertex, VertexTypeEnum.INST_INPUTS, EdgeLabelEnum.INST_INPUTS, instInputs);
380             if (assosiateElementToData.isRight()) {
381                 return assosiateElementToData.right().value();
382             }
383         }
384         return StorageOperationStatus.OK;
385     }
386
387     public StorageOperationStatus associateInstGroupsToComponent(GraphVertex nodeTypeVertex, Map<String, MapGroupsDataDefinition> instGroups) {
388         if (instGroups != null && !instGroups.isEmpty()) {
389             Either<GraphVertex, StorageOperationStatus> assosiateElementToData = associateElementToData(nodeTypeVertex, VertexTypeEnum.INST_GROUPS, EdgeLabelEnum.INST_GROUPS, instGroups);
390             if (assosiateElementToData.isRight()) {
391                 return assosiateElementToData.right().value();
392             }
393         }
394         return StorageOperationStatus.OK;
395     }
396
397
398     public StorageOperationStatus deleteInstInputsToComponent(GraphVertex nodeTypeVertex, Map<String, MapPropertiesDataDefinition> instInputs) {
399
400         if (instInputs != null && !instInputs.isEmpty()) {
401             instInputs.entrySet().forEach(i -> {
402                 List<String> uniqueKeys = new ArrayList<>(i.getValue().getMapToscaDataDefinition().keySet());
403                 List<String> pathKeys = new ArrayList<>();
404                 pathKeys.add(i.getKey());
405
406                 StorageOperationStatus status = deleteToscaDataDeepElements(nodeTypeVertex, EdgeLabelEnum.INST_INPUTS, VertexTypeEnum.INST_INPUTS, uniqueKeys, pathKeys, JsonPresentationFields.NAME);
407                 if (status != StorageOperationStatus.OK) {
408                     return;
409                 }
410             });
411         }
412
413         return StorageOperationStatus.OK;
414     }
415
416     public StorageOperationStatus addInstPropertiesToComponent(GraphVertex nodeTypeVertex, Map<String, MapPropertiesDataDefinition> instInputs) {
417
418         if (instInputs != null && !instInputs.isEmpty()) {
419             instInputs.entrySet().forEach(i -> {
420                 StorageOperationStatus status = addToscaDataDeepElementsBlockToToscaElement(nodeTypeVertex, EdgeLabelEnum.INST_PROPERTIES, VertexTypeEnum.INST_PROPERTIES, i.getValue(), i.getKey());
421                 if (status != StorageOperationStatus.OK) {
422                     return;
423                 }
424             });
425         }
426
427         return StorageOperationStatus.OK;
428     }
429
430     public StorageOperationStatus associateInstDeploymentArtifactsToComponent(GraphVertex nodeTypeVertex, Map<String, MapArtifactDataDefinition> instArtifacts) {
431         return associateInstanceArtifactsToComponent(nodeTypeVertex, instArtifacts, VertexTypeEnum.INST_DEPLOYMENT_ARTIFACTS, EdgeLabelEnum.INST_DEPLOYMENT_ARTIFACTS);
432     }
433
434     public StorageOperationStatus associateInstArtifactsToComponent(GraphVertex nodeTypeVertex, Map<String, MapArtifactDataDefinition> instArtifacts) {
435         return associateInstanceArtifactsToComponent(nodeTypeVertex, instArtifacts, VertexTypeEnum.INSTANCE_ARTIFACTS, EdgeLabelEnum.INSTANCE_ARTIFACTS);
436     }
437
438     private StorageOperationStatus associateInstanceArtifactsToComponent(GraphVertex nodeTypeVertex, Map<String, MapArtifactDataDefinition> instProps, VertexTypeEnum vertexType, EdgeLabelEnum edgeLabel) {
439         if (instProps != null && !instProps.isEmpty()) {
440             Either<GraphVertex, StorageOperationStatus> assosiateElementToData = associateElementToData(nodeTypeVertex, vertexType, edgeLabel, instProps);
441             if (assosiateElementToData.isRight()) {
442                 return assosiateElementToData.right().value();
443             }
444         }
445         return StorageOperationStatus.OK;
446     }
447
448     public StorageOperationStatus associateOrAddCalcCapReqToComponent(GraphVertex nodeTypeVertex, Map<String, MapListRequirementDataDefinition> calcRequirements, Map<String, MapListCapabilityDataDefinition> calcCapabilty, Map<String, MapCapabilityProperty> calculatedCapabilitiesProperties) {
449         if (!MapUtils.isEmpty(calcRequirements)) {
450             Either<GraphVertex, StorageOperationStatus> assosiateElementToData = associateOrAddElementToData(nodeTypeVertex, VertexTypeEnum.CALCULATED_REQUIREMENTS, EdgeLabelEnum.CALCULATED_REQUIREMENTS, calcRequirements);
451             if (assosiateElementToData.isRight()) {
452                 return assosiateElementToData.right().value();
453             }
454             Map<String, MapListRequirementDataDefinition> fullFilled = new HashMap<>();
455             assosiateElementToData = associateOrAddElementToData(nodeTypeVertex, VertexTypeEnum.FULLFILLED_REQUIREMENTS, EdgeLabelEnum.FULLFILLED_REQUIREMENTS, fullFilled);
456             if (assosiateElementToData.isRight()) {
457                 return assosiateElementToData.right().value();
458             }
459         }
460         if (!MapUtils.isEmpty(calcCapabilty)) {
461             Either<GraphVertex, StorageOperationStatus> assosiateElementToData = associateOrAddElementToData(nodeTypeVertex, VertexTypeEnum.CALCULATED_CAPABILITIES ,EdgeLabelEnum.CALCULATED_CAPABILITIES, calcCapabilty);
462             if (assosiateElementToData.isRight()) {
463                 return assosiateElementToData.right().value();
464             }
465             Map<String, MapListCapabilityDataDefinition> fullFilled = new HashMap<>();
466             assosiateElementToData = associateOrAddElementToData(nodeTypeVertex, VertexTypeEnum.FULLFILLED_CAPABILITIES, EdgeLabelEnum.FULLFILLED_CAPABILITIES, fullFilled);
467             if (assosiateElementToData.isRight()) {
468                 return assosiateElementToData.right().value();
469             }
470         }
471         if (!MapUtils.isEmpty(calculatedCapabilitiesProperties)){
472             return associateOrAddElementToData(nodeTypeVertex, VertexTypeEnum.CALCULATED_CAP_PROPERTIES,
473                     EdgeLabelEnum.CALCULATED_CAP_PROPERTIES, calculatedCapabilitiesProperties)
474                     .right()
475                     .on(v -> StorageOperationStatus.OK);
476         }
477         return StorageOperationStatus.OK;
478     }
479
480     private <T extends MapDataDefinition> Either<GraphVertex, StorageOperationStatus> associateOrAddElementToData(GraphVertex nodeTypeVertex, VertexTypeEnum vertexTypeEnum, EdgeLabelEnum edgeLabelEnum, Map<String, T> dataMap){
481         return janusGraphDao.getChildVertex(nodeTypeVertex, edgeLabelEnum, JsonParseFlagEnum.ParseJson)
482                 .either(dataVertex -> addElementsToComponent(nodeTypeVertex, dataVertex, vertexTypeEnum, edgeLabelEnum, dataMap),
483                         status -> associateElementToDataIfNotFound(status, nodeTypeVertex, vertexTypeEnum, edgeLabelEnum, dataMap));
484     }
485
486     private Either<GraphVertex, StorageOperationStatus> associateElementToDataIfNotFound(JanusGraphOperationStatus status, GraphVertex nodeTypeVertex, VertexTypeEnum vertexTypeEnum, EdgeLabelEnum edgeLabelEnum, Map<String, ? extends ToscaDataDefinition> dataMap) {
487         if (status == JanusGraphOperationStatus.NOT_FOUND) {
488             return associateElementToData(nodeTypeVertex, vertexTypeEnum, edgeLabelEnum, dataMap);
489         }
490         return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
491     }
492
493     private <T extends MapDataDefinition> Either<GraphVertex, StorageOperationStatus> addElementsToComponent(GraphVertex nodeTypeVertex, GraphVertex dataVertex, VertexTypeEnum vertexTypeEnum, EdgeLabelEnum edgeLabelEnum, Map<String, T> dataMap) {
494         Optional<StorageOperationStatus> error = dataMap.entrySet()
495                 .stream()
496                 .map(e -> addElementToComponent(nodeTypeVertex.getUniqueId(), vertexTypeEnum, edgeLabelEnum, e))
497                 .filter(s -> s != StorageOperationStatus.OK)
498                 .findFirst();
499         if(error.isPresent()){
500             return Either.right(error.get());
501         }
502         return Either.left(dataVertex);
503     }
504
505     private StorageOperationStatus associateInstAttributesToComponent(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
506         Map<String, MapAttributesDataDefinition> instAttr = topologyTemplate.getInstAttributes();
507         return associateInstAttributeToComponent(nodeTypeVertex, instAttr);
508     }
509
510     public StorageOperationStatus associateForwardingPathToComponent(GraphVertex nodeTypeVertex, Map<String, ForwardingPathDataDefinition> forwardingPathMap) {
511         if (forwardingPathMap != null && !forwardingPathMap.isEmpty()) {
512             Either<GraphVertex, StorageOperationStatus> assosiateElementToData = associateElementToData(nodeTypeVertex, VertexTypeEnum.FORWARDING_PATH, EdgeLabelEnum.FORWARDING_PATH, forwardingPathMap);
513             if (assosiateElementToData.isRight()) {
514                 return assosiateElementToData.right().value();
515             }
516         }
517         return StorageOperationStatus.OK;
518     }
519
520     public StorageOperationStatus associateInstAttributeToComponent(GraphVertex nodeTypeVertex, Map<String, MapAttributesDataDefinition> instAttr) {
521         if (instAttr != null && !instAttr.isEmpty()) {
522             Either<GraphVertex, StorageOperationStatus> assosiateElementToData = associateElementToData(nodeTypeVertex, VertexTypeEnum.INST_ATTRIBUTES, EdgeLabelEnum.INST_ATTRIBUTES, instAttr);
523             if (assosiateElementToData.isRight()) {
524                 return assosiateElementToData.right().value();
525             }
526         }
527         return StorageOperationStatus.OK;
528     }
529
530     public StorageOperationStatus associateGroupsToComponent(GraphVertex nodeTypeVertex, Map<String, GroupDataDefinition> groups) {
531
532         if (groups != null && !groups.isEmpty()) {
533             groups.values().stream().filter(p -> p.getUniqueId() == null).forEach(p -> {
534                 String uid = UniqueIdBuilder.buildGroupingUid(nodeTypeVertex.getUniqueId(), p.getName());
535                 p.setUniqueId(uid);
536             });
537             Either<GraphVertex, StorageOperationStatus> assosiateElementToData = associateElementToData(nodeTypeVertex, VertexTypeEnum.GROUPS, EdgeLabelEnum.GROUPS, groups);
538             if (assosiateElementToData.isRight()) {
539                 return assosiateElementToData.right().value();
540             }
541         }
542         return StorageOperationStatus.OK;
543     }
544
545     private StorageOperationStatus associateGroupsToComponent(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
546         return associateGroupsToComponent(nodeTypeVertex, topologyTemplate.getGroups());
547     }
548
549     public StorageOperationStatus associateInputsToComponent(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
550         Map<String, PropertyDataDefinition> inputs = topologyTemplate.getInputs();
551         return associateInputsToComponent(nodeTypeVertex, inputs, topologyTemplate.getUniqueId());
552     }
553
554     public StorageOperationStatus associateInputsToComponent(GraphVertex nodeTypeVertex, Map<String, PropertyDataDefinition> inputs, String id) {
555         if (inputs != null && !inputs.isEmpty()) {
556             inputs.values().stream().filter(e -> e.getUniqueId() == null).forEach(e -> e.setUniqueId(UniqueIdBuilder.buildPropertyUniqueId(id, e.getName())));
557
558             Either<GraphVertex, StorageOperationStatus> assosiateElementToData = associateElementToData(nodeTypeVertex, VertexTypeEnum.INPUTS, EdgeLabelEnum.INPUTS, inputs);
559             if (assosiateElementToData.isRight()) {
560                 return assosiateElementToData.right().value();
561             }
562         }
563         return StorageOperationStatus.OK;
564     }
565
566     private GraphVertex fillMetadata(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate, JsonParseFlagEnum flag) {
567         nodeTypeVertex.setLabel(VertexTypeEnum.TOPOLOGY_TEMPLATE);
568         fillCommonMetadata(nodeTypeVertex, topologyTemplate);
569         if (flag == JsonParseFlagEnum.ParseAll || flag == JsonParseFlagEnum.ParseJson) {
570             nodeTypeVertex.setJson(topologyTemplate.getCompositions());
571         }
572         nodeTypeVertex.addMetadataProperty(GraphPropertyEnum.CSAR_UUID, topologyTemplate.getMetadataValue(JsonPresentationFields.CSAR_UUID));
573         nodeTypeVertex.addMetadataProperty(GraphPropertyEnum.DISTRIBUTION_STATUS, topologyTemplate.getMetadataValue(JsonPresentationFields.DISTRIBUTION_STATUS));
574
575         return nodeTypeVertex;
576
577     }
578
579     private StorageOperationStatus assosiateMetadataToCategory(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
580         if (topologyTemplate.getResourceType() == null) {
581             // service
582             return associateServiceMetadataToCategory(nodeTypeVertex, topologyTemplate);
583         } else {
584             // VF
585             return assosiateResourceMetadataToCategory(nodeTypeVertex, topologyTemplate);
586         }
587     }
588
589     private StorageOperationStatus associateServiceMetadataToCategory(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
590         String categoryName = topologyTemplate.getCategories().get(0).getName();
591         Either<GraphVertex, StorageOperationStatus> category = categoryOperation.getCategory(categoryName, VertexTypeEnum.SERVICE_CATEGORY);
592         if (category.isRight()) {
593             log.trace("NO category {} for service {}", categoryName, topologyTemplate.getUniqueId());
594             return StorageOperationStatus.CATEGORY_NOT_FOUND;
595         }
596         GraphVertex categoryV = category.left().value();
597         JanusGraphOperationStatus createEdge = janusGraphDao.createEdge(nodeTypeVertex, categoryV, EdgeLabelEnum.CATEGORY, new HashMap<>());
598         if (createEdge != JanusGraphOperationStatus.OK) {
599             log.trace("Failed to associate resource {} to category {} with id {}", topologyTemplate.getUniqueId(), categoryName, categoryV.getUniqueId());
600             return DaoStatusConverter.convertJanusGraphStatusToStorageStatus(createEdge);
601         }
602         return StorageOperationStatus.OK;
603     }
604
605     @Override
606     public Either<ToscaElement, StorageOperationStatus> getToscaElement(String uniqueId, ComponentParametersView componentParametersView) {
607         JsonParseFlagEnum parseFlag = componentParametersView.detectParseFlag();
608
609         Either<GraphVertex, StorageOperationStatus> componentByLabelAndId = getComponentByLabelAndId(uniqueId, ToscaElementTypeEnum.TOPOLOGY_TEMPLATE, parseFlag);
610         if (componentByLabelAndId.isRight()) {
611             return Either.right(componentByLabelAndId.right().value());
612         }
613         GraphVertex componentV = componentByLabelAndId.left().value();
614
615         return getToscaElement(componentV, componentParametersView);
616
617     }
618     // -------------------------------------------------------------
619
620     public Either<ToscaElement, StorageOperationStatus> getToscaElement(GraphVertex componentV, ComponentParametersView componentParametersView) {
621         TopologyTemplate toscaElement;
622
623         toscaElement = convertToTopologyTemplate(componentV);
624         JanusGraphOperationStatus status;
625         if (!componentParametersView.isIgnoreUsers()) {
626             status = setCreatorFromGraph(componentV, toscaElement);
627             if (status != JanusGraphOperationStatus.OK) {
628                 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
629             }
630
631             status = setLastModifierFromGraph(componentV, toscaElement);
632             if (status != JanusGraphOperationStatus.OK) {
633                 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
634             }
635         }
636         if (!componentParametersView.isIgnoreCategories()) {
637             status = setTopologyTempalteCategoriesFromGraph(componentV, toscaElement);
638             if (status != JanusGraphOperationStatus.OK) {
639                 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
640
641             }
642         }
643         if (!componentParametersView.isIgnoreArtifacts()) {
644             JanusGraphOperationStatus storageStatus = setAllArtifactsFromGraph(componentV, toscaElement);
645             if (storageStatus != JanusGraphOperationStatus.OK) {
646                 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(storageStatus));
647             }
648         }
649         if (!componentParametersView.isIgnoreComponentInstancesProperties()) {
650             status = setComponentInstancesPropertiesFromGraph(componentV, toscaElement);
651             if (status != JanusGraphOperationStatus.OK) {
652                 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
653             }
654         }
655         if (!componentParametersView.isIgnoreComponentInstancesAttributes()) {
656             status = setComponentInstancesAttributesFromGraph(componentV, toscaElement);
657             if (status != JanusGraphOperationStatus.OK) {
658                 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
659             }
660         }
661         if (!componentParametersView.isIgnoreCapabilities()) {
662             status = setCapabilitiesFromGraph(componentV, toscaElement);
663             if (status != JanusGraphOperationStatus.OK) {
664                 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
665             }
666         }
667         if (!componentParametersView.isIgnoreRequirements()) {
668             status = setRequirementsFromGraph(componentV, toscaElement);
669             if (status != JanusGraphOperationStatus.OK) {
670                 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
671             }
672         }
673         if (!componentParametersView.isIgnoreAllVersions()) {
674             status = setAllVersions(componentV, toscaElement);
675             if (status != JanusGraphOperationStatus.OK) {
676                 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
677             }
678         }
679         if (!componentParametersView.isIgnoreAdditionalInformation()) {
680             status = setAdditionalInformationFromGraph(componentV, toscaElement);
681             if (status != JanusGraphOperationStatus.OK) {
682                 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
683             }
684         }
685
686         if (!componentParametersView.isIgnoreGroups()) {
687             status = setGroupsFromGraph(componentV, toscaElement);
688             if (status != JanusGraphOperationStatus.OK) {
689                 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
690             }
691
692         }
693         if (!componentParametersView.isIgnorePolicies()) {
694             status = setPoliciesFromGraph(componentV, toscaElement);
695             if (status != JanusGraphOperationStatus.OK) {
696                 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
697             }
698
699         }
700         if (!componentParametersView.isIgnoreComponentInstances()) {
701             status = setInstGroupsFromGraph(componentV, toscaElement);
702
703             //Mark all CIs that has archived origins
704             archiveOperation.setArchivedOriginsFlagInComponentInstances(componentV);
705
706             if (status != JanusGraphOperationStatus.OK) {
707                 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
708             }
709
710         }
711         if (!componentParametersView.isIgnoreInputs()) {
712             status = setInputsFromGraph(componentV, toscaElement);
713             if (status != JanusGraphOperationStatus.OK) {
714                 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
715             }
716
717         }
718
719         if (!componentParametersView.isIgnoreOutputs()) {
720             final JanusGraphOperationStatus storageStatus = setOutputsFromGraph(componentV, toscaElement);
721             if (storageStatus != JanusGraphOperationStatus.OK) {
722                 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(storageStatus));
723             }
724         }
725
726         if (!componentParametersView.isIgnoreProperties()) {
727             status = setPropertiesFromGraph(componentV, toscaElement);
728             if (status != JanusGraphOperationStatus.OK) {
729                 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
730             }
731
732         }
733
734         if (!componentParametersView.isIgnoreComponentInstancesInputs()) {
735             status = setComponentInstancesInputsFromGraph(componentV, toscaElement);
736             if (status != JanusGraphOperationStatus.OK) {
737                 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
738
739             }
740         }
741
742         if (!componentParametersView.isIgnoreCapabiltyProperties()) {
743             status = setComponentInstancesCapPropertiesFromGraph(componentV, toscaElement);
744             if (status != JanusGraphOperationStatus.OK) {
745                 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
746
747             }
748         }
749
750         if (!componentParametersView.isIgnoreServicePath()) {
751             status = setForwardingGraphPropertiesFromGraph(componentV, toscaElement);
752             if (status != JanusGraphOperationStatus.OK) {
753                 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
754
755             }
756         }
757
758         if (!componentParametersView.isIgnoreNodeFilter()) {
759             status = setNodeFilterComponentFromGraph(componentV, toscaElement);
760             if (status != JanusGraphOperationStatus.OK) {
761                 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
762
763             }
764         }
765
766         if (!componentParametersView.isIgnoreSubstitutionFilter()) {
767             status = setSubstitutionFilterComponentFromGraph(componentV, toscaElement);
768             if (status != JanusGraphOperationStatus.OK) {
769                 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
770
771             }
772         }
773
774         if (!componentParametersView.isIgnoreInterfaces()) {
775             JanusGraphOperationStatus storageStatus = setInterfacesFromGraph(componentV, toscaElement);
776             if (storageStatus != JanusGraphOperationStatus.OK) {
777                 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(storageStatus));
778
779             }
780         }
781
782         if (!componentParametersView.isIgnoreInterfaces()) {
783             JanusGraphOperationStatus storageStatus = setInterfcesFromGraph(componentV, toscaElement);
784             if (storageStatus != JanusGraphOperationStatus.OK) {
785                 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(storageStatus));
786
787             }
788         }
789
790         if (!componentParametersView.isIgnoreComponentInstancesInterfaces()) {
791             JanusGraphOperationStatus storageStatus =
792                     setComponentInstancesInterfacesFromGraph(componentV, toscaElement);
793             if (storageStatus != JanusGraphOperationStatus.OK) {
794                 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(storageStatus));
795             }
796         }
797
798         if (!componentParametersView.isIgnoreDataType()) {
799             JanusGraphOperationStatus storageStatus = setDataTypesFromGraph(componentV, toscaElement);
800             if (storageStatus != JanusGraphOperationStatus.OK) {
801                 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(storageStatus));
802             }
803         }
804
805         return Either.left(toscaElement);
806     }
807
808     private JanusGraphOperationStatus setDataTypesFromGraph(GraphVertex componentV, TopologyTemplate toscaElement) {
809         Either<Map<String, DataTypeDataDefinition>, JanusGraphOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.DATA_TYPES);
810         if (result.isLeft()) {
811             toscaElement.setDataTypes(result.left().value());
812         } else {
813             if (result.right().value() != JanusGraphOperationStatus.NOT_FOUND) {
814                 return result.right().value();
815             }
816         }
817         return JanusGraphOperationStatus.OK;
818     }
819
820     private JanusGraphOperationStatus setInterfcesFromGraph(GraphVertex componentV, TopologyTemplate topologyTemplate) {
821         Either<Map<String, InterfaceDataDefinition>, JanusGraphOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.INTERFACE);
822         if (result.isLeft()) {
823             topologyTemplate.setInterfaces(result.left().value());
824         } else {
825             if (result.right().value() != JanusGraphOperationStatus.NOT_FOUND) {
826                 return result.right().value();
827             }
828         }
829         return JanusGraphOperationStatus.OK;
830     }
831
832     private JanusGraphOperationStatus setPoliciesFromGraph(GraphVertex componentV, TopologyTemplate toscaElement) {
833         Either<Map<String, PolicyDataDefinition>, JanusGraphOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.POLICIES);
834         if (result.isLeft()) {
835             toscaElement.setPolicies(result.left().value());
836         } else {
837             if (result.right().value() != JanusGraphOperationStatus.NOT_FOUND) {
838                 return result.right().value();
839             }
840         }
841         return JanusGraphOperationStatus.OK;
842     }
843
844     private JanusGraphOperationStatus setInterfacesFromGraph(GraphVertex componentV, TopologyTemplate topologyTemplate) {
845         Either<Map<String, InterfaceDataDefinition>, JanusGraphOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.INTERFACE);
846         if (result.isLeft()) {
847             topologyTemplate.setInterfaces(result.left().value());
848         } else {
849             if (result.right().value() != JanusGraphOperationStatus.NOT_FOUND) {
850                 return result.right().value();
851             }
852         }
853         return JanusGraphOperationStatus.OK;
854     }
855
856
857     private JanusGraphOperationStatus setComponentInstancesInterfacesFromGraph(GraphVertex componentV,
858                                                                           TopologyTemplate topologyTemplate) {
859         Either<Map<String, MapInterfaceDataDefinition>, JanusGraphOperationStatus> result =
860                 getDataFromGraph(componentV, EdgeLabelEnum.INST_INTERFACES);
861         if (result.isLeft()) {
862             result.left().value().entrySet().forEach(entry -> topologyTemplate
863                     .addComponentInstanceInterfaceMap(entry.getKey(), entry.getValue()));
864         } else {
865             if (result.right().value() != JanusGraphOperationStatus.NOT_FOUND) {
866                 return result.right().value();
867             }
868         }
869         return JanusGraphOperationStatus.OK;
870     }
871
872     private StorageOperationStatus associateInterfacesToComponent(GraphVertex topologyTemplateVertex, TopologyTemplate topologyTemplate) {
873         Map<String, InterfaceDataDefinition> interfaceMap = topologyTemplate.getInterfaces();
874         if (interfaceMap != null && !interfaceMap.isEmpty()) {
875             Either<GraphVertex, StorageOperationStatus> assosiateElementToData = associateElementToData(topologyTemplateVertex, VertexTypeEnum.INTERFACE, EdgeLabelEnum.INTERFACE, interfaceMap);
876             if (assosiateElementToData.isRight()) {
877                 return assosiateElementToData.right().value();
878             }
879         }
880         return StorageOperationStatus.OK;
881     }
882
883     private JanusGraphOperationStatus setForwardingGraphPropertiesFromGraph(GraphVertex componentV, TopologyTemplate topologyTemplate) {
884         Either<Map<String, ForwardingPathDataDefinition>, JanusGraphOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.FORWARDING_PATH);
885         if (result.isLeft()) {
886             topologyTemplate.setForwardingPaths(result.left().value());
887         } else {
888             if (result.right().value() != JanusGraphOperationStatus.NOT_FOUND) {
889                 return result.right().value();
890             }
891         }
892         return JanusGraphOperationStatus.OK;
893     }
894
895
896     private JanusGraphOperationStatus setComponentInstancesCapPropertiesFromGraph(GraphVertex componentV, TopologyTemplate topologyTemplate) {
897         Either<Map<String, MapCapabilityProperty>, JanusGraphOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.CALCULATED_CAP_PROPERTIES);
898         if (result.isLeft()) {
899             topologyTemplate.setCalculatedCapabilitiesProperties(result.left().value());
900         } else {
901             if (result.right().value() != JanusGraphOperationStatus.NOT_FOUND) {
902                 return result.right().value();
903             }
904         }
905         Either<Map<String, MapPropertiesDataDefinition>, JanusGraphOperationStatus> capPropResult =
906                 getDataFromGraph(componentV, EdgeLabelEnum.CAPABILITIES_PROPERTIES);
907         if (capPropResult.isLeft()) {
908             topologyTemplate.setCapabilitiesProperties(capPropResult.left().value());
909         } else {
910             if (capPropResult.right().value() != JanusGraphOperationStatus.NOT_FOUND) {
911                 return capPropResult.right().value();
912             }
913         }
914
915         return JanusGraphOperationStatus.OK;
916     }
917
918     private JanusGraphOperationStatus setPropertiesFromGraph(GraphVertex componentV, TopologyTemplate toscaElement) {
919         Either<Map<String, PropertyDataDefinition>, JanusGraphOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.PROPERTIES);
920         if (result.isLeft()) {
921             toscaElement.setProperties(result.left().value());
922         } else {
923             if (result.right().value() != JanusGraphOperationStatus.NOT_FOUND) {
924                 return result.right().value();
925             }
926         }
927         return JanusGraphOperationStatus.OK;
928     }
929
930     private JanusGraphOperationStatus setInstGroupsFromGraph(GraphVertex componentV, TopologyTemplate topologyTemplate) {
931         Either<Map<String, MapGroupsDataDefinition>, JanusGraphOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.INST_GROUPS);
932         if (result.isLeft()) {
933             topologyTemplate.setInstGroups(result.left().value());
934         } else {
935             if (result.right().value() != JanusGraphOperationStatus.NOT_FOUND) {
936                 return result.right().value();
937             }
938         }
939         return JanusGraphOperationStatus.OK;
940     }
941
942     private JanusGraphOperationStatus setComponentInstancesPropertiesFromGraph(GraphVertex componentV, TopologyTemplate topologyTemplate) {
943         Either<Map<String, MapPropertiesDataDefinition>, JanusGraphOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.INST_PROPERTIES);
944         if (result.isLeft()) {
945             topologyTemplate.setInstProperties(result.left().value());
946         } else {
947             if (result.right().value() != JanusGraphOperationStatus.NOT_FOUND) {
948                 return result.right().value();
949             }
950         }
951         return JanusGraphOperationStatus.OK;
952     }
953
954     private JanusGraphOperationStatus setComponentInstancesAttributesFromGraph(final GraphVertex componentV,
955                                                                                final TopologyTemplate topologyTemplate) {
956         final Either<Map<String, MapAttributesDataDefinition>, JanusGraphOperationStatus> result =
957             getDataFromGraph(componentV, EdgeLabelEnum.INST_ATTRIBUTES);
958         if (result.isLeft()) {
959             topologyTemplate.setInstAttributes(result.left().value());
960         } else if (result.right().value() != JanusGraphOperationStatus.NOT_FOUND) {
961             return result.right().value();
962         }
963         return JanusGraphOperationStatus.OK;
964     }
965
966     private JanusGraphOperationStatus setComponentInstancesInputsFromGraph(GraphVertex componentV, TopologyTemplate topologyTemplate) {
967         Either<Map<String, MapPropertiesDataDefinition>, JanusGraphOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.INST_INPUTS);
968         if (result.isLeft()) {
969             topologyTemplate.setInstInputs(result.left().value());
970         } else {
971             if (result.right().value() != JanusGraphOperationStatus.NOT_FOUND) {
972                 return result.right().value();
973             }
974         }
975         return JanusGraphOperationStatus.OK;
976     }
977
978     private JanusGraphOperationStatus setNodeFilterComponentFromGraph(final GraphVertex componentV,
979                                                                       final TopologyTemplate topologyTemplate) {
980
981         final Either<Map<String, CINodeFilterDataDefinition>, JanusGraphOperationStatus> result =
982             getDataFromGraph(componentV, EdgeLabelEnum.NODE_FILTER_TEMPLATE);
983         if (result.isLeft()) {
984             topologyTemplate.setNodeFilterComponents(result.left().value());
985         } else {
986             if (result.right().value() != JanusGraphOperationStatus.NOT_FOUND) {
987                 return result.right().value();
988             }
989         }
990         return JanusGraphOperationStatus.OK;
991     }
992
993     private JanusGraphOperationStatus setSubstitutionFilterComponentFromGraph(final GraphVertex componentV,
994                                                                               final TopologyTemplate topologyTemplate) {
995
996         final Either<Map<String, SubstitutionFilterDataDefinition>, JanusGraphOperationStatus> result =
997             getDataFromGraph(componentV, EdgeLabelEnum.SUBSTITUTION_FILTER_TEMPLATE);
998         if (result.isLeft()) {
999             topologyTemplate.setSubstitutionFilterDataDefinitionMap(result.left().value());
1000         } else {
1001             if (result.right().value() != JanusGraphOperationStatus.NOT_FOUND) {
1002                 return result.right().value();
1003             }
1004         }
1005         return JanusGraphOperationStatus.OK;
1006     }
1007
1008
1009     @Override
1010     protected <T extends ToscaElement> JanusGraphOperationStatus setRequirementsFromGraph(GraphVertex componentV, T toscaElement) {
1011         Either<Map<String, MapListRequirementDataDefinition>, JanusGraphOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.CALCULATED_REQUIREMENTS);
1012         if (result.isLeft()) {
1013             ((TopologyTemplate) toscaElement).setCalculatedRequirements(result.left().value());
1014         } else {
1015             if (result.right().value() != JanusGraphOperationStatus.NOT_FOUND) {
1016                 return result.right().value();
1017             }
1018         }
1019         result = getDataFromGraph(componentV, EdgeLabelEnum.FULLFILLED_REQUIREMENTS);
1020         if (result.isLeft()) {
1021             ((TopologyTemplate) toscaElement).setFullfilledRequirements(result.left().value());
1022         } else {
1023             if (result.right().value() != JanusGraphOperationStatus.NOT_FOUND) {
1024                 return result.right().value();
1025             }
1026         }
1027         Either<Map<String, ListRequirementDataDefinition>, JanusGraphOperationStatus> requirementResult =
1028                 getDataFromGraph(componentV, EdgeLabelEnum.REQUIREMENTS);
1029         if (requirementResult.isLeft()) {
1030             toscaElement.setRequirements(requirementResult.left().value());
1031         } else {
1032             if (requirementResult.right().value() != JanusGraphOperationStatus.NOT_FOUND) {
1033                 return requirementResult.right().value();
1034             }
1035         }
1036         return JanusGraphOperationStatus.OK;
1037
1038     }
1039
1040     protected <T extends ToscaElement> JanusGraphOperationStatus setCapabilitiesFromGraph(GraphVertex componentV, T toscaElement) {
1041         Either<Map<String, MapListCapabilityDataDefinition>, JanusGraphOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.CALCULATED_CAPABILITIES);
1042         if (result.isLeft()) {
1043             ((TopologyTemplate) toscaElement).setCalculatedCapabilities(result.left().value());
1044         } else {
1045             if (result.right().value() != JanusGraphOperationStatus.NOT_FOUND) {
1046                 return result.right().value();
1047             }
1048         }
1049         result = getDataFromGraph(componentV, EdgeLabelEnum.FULLFILLED_CAPABILITIES);
1050         if (result.isLeft()) {
1051             ((TopologyTemplate) toscaElement).setFullfilledCapabilities(result.left().value());
1052         } else {
1053             if (result.right().value() != JanusGraphOperationStatus.NOT_FOUND) {
1054                 return result.right().value();
1055             }
1056         }
1057         Either<Map<String, ListCapabilityDataDefinition>, JanusGraphOperationStatus> capabilitiesResult =
1058                 getDataFromGraph(componentV, EdgeLabelEnum.CAPABILITIES);
1059         if (capabilitiesResult.isLeft()) {
1060             toscaElement.setCapabilities(capabilitiesResult.left().value());
1061         } else {
1062             if (capabilitiesResult.right().value() != JanusGraphOperationStatus.NOT_FOUND) {
1063                 return capabilitiesResult.right().value();
1064             }
1065         }
1066         return JanusGraphOperationStatus.OK;
1067     }
1068
1069     private JanusGraphOperationStatus setAllArtifactsFromGraph(GraphVertex componentV, TopologyTemplate toscaElement) {
1070         JanusGraphOperationStatus storageStatus = setArtifactsFromGraph(componentV, toscaElement);
1071         if (storageStatus != JanusGraphOperationStatus.OK) {
1072             return storageStatus;
1073         }
1074         Either<Map<String, ArtifactDataDefinition>, JanusGraphOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.SERVICE_API_ARTIFACTS);
1075         if (result.isLeft()) {
1076             toscaElement.setServiceApiArtifacts(result.left().value());
1077         } else {
1078             if (result.right().value() != JanusGraphOperationStatus.NOT_FOUND) {
1079                 return result.right().value();
1080             }
1081         }
1082         Either<Map<String, MapArtifactDataDefinition>, JanusGraphOperationStatus> resultInstArt = getDataFromGraph(componentV, EdgeLabelEnum.INST_DEPLOYMENT_ARTIFACTS);
1083         if (resultInstArt.isLeft()) {
1084             toscaElement.setInstDeploymentArtifacts(resultInstArt.left().value());
1085         } else {
1086             if (resultInstArt.right().value() != JanusGraphOperationStatus.NOT_FOUND) {
1087                 return resultInstArt.right().value();
1088             }
1089         }
1090         Either<Map<String, MapArtifactDataDefinition>, JanusGraphOperationStatus> instanceArt = getDataFromGraph(componentV, EdgeLabelEnum.INSTANCE_ARTIFACTS);
1091         if (instanceArt.isLeft()) {
1092             toscaElement.setInstanceArtifacts(instanceArt.left().value());
1093         } else {
1094             if (instanceArt.right().value() != JanusGraphOperationStatus.NOT_FOUND) {
1095                 return instanceArt.right().value();
1096             }
1097         }
1098         return JanusGraphOperationStatus.OK;
1099     }
1100
1101     private JanusGraphOperationStatus setInputsFromGraph(GraphVertex componentV, TopologyTemplate toscaElement) {
1102         Either<Map<String, PropertyDataDefinition>, JanusGraphOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.INPUTS);
1103         if (result.isLeft()) {
1104             toscaElement.setInputs(result.left().value());
1105         } else {
1106             if (result.right().value() != JanusGraphOperationStatus.NOT_FOUND) {
1107                 return result.right().value();
1108             }
1109         }
1110         Either<Map<String, ListCapabilityDataDefinition>, JanusGraphOperationStatus> capabilitiesResult =
1111                 getDataFromGraph(componentV, EdgeLabelEnum.CAPABILITIES);
1112         if (capabilitiesResult.isLeft()) {
1113             toscaElement.setCapabilities(capabilitiesResult.left().value());
1114         } else {
1115             if (capabilitiesResult.right().value() != JanusGraphOperationStatus.NOT_FOUND) {
1116                 return capabilitiesResult.right().value();
1117             }
1118         }
1119         return JanusGraphOperationStatus.OK;
1120     }
1121
1122     private JanusGraphOperationStatus setOutputsFromGraph(final GraphVertex componentV,
1123                                                           final TopologyTemplate toscaElement) {
1124         final Either<Map<String, AttributeDataDefinition>, JanusGraphOperationStatus> result =
1125             getDataFromGraph(componentV, EdgeLabelEnum.OUTPUTS);
1126         if (result.isLeft()) {
1127             toscaElement.setOutputs(result.left().value());
1128         } else {
1129             if (result.right().value() != JanusGraphOperationStatus.NOT_FOUND) {
1130                 return result.right().value();
1131             }
1132         }
1133         return JanusGraphOperationStatus.OK;
1134     }
1135
1136     private JanusGraphOperationStatus setGroupsFromGraph(GraphVertex componentV, TopologyTemplate toscaElement) {
1137         Either<Map<String, GroupDataDefinition>, JanusGraphOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.GROUPS);
1138         if (result.isLeft()) {
1139             toscaElement.setGroups(result.left().value());
1140         } else {
1141             if (result.right().value() != JanusGraphOperationStatus.NOT_FOUND) {
1142                 return result.right().value();
1143             }
1144         }
1145         return JanusGraphOperationStatus.OK;
1146     }
1147
1148     private JanusGraphOperationStatus setTopologyTempalteCategoriesFromGraph(GraphVertex componentV, ToscaElement toscaElement) {
1149         List<CategoryDefinition> categories = new ArrayList<>();
1150
1151         switch (componentV.getType()) {
1152         case RESOURCE:
1153             return setResourceCategoryFromGraph(componentV, toscaElement);
1154         case SERVICE:
1155             return setServiceCategoryFromGraph(componentV, toscaElement, categories);
1156
1157         default:
1158             log.debug("Not supported component type {} ", componentV.getType());
1159             break;
1160         }
1161         return JanusGraphOperationStatus.OK;
1162     }
1163
1164     private JanusGraphOperationStatus setServiceCategoryFromGraph(GraphVertex componentV, ToscaElement toscaElement, List<CategoryDefinition> categories) {
1165         Either<GraphVertex, JanusGraphOperationStatus> childVertex = janusGraphDao.getChildVertex(componentV, EdgeLabelEnum.CATEGORY, JsonParseFlagEnum.NoParse);
1166         if (childVertex.isRight()) {
1167             log.debug("failed to fetch {} for tosca element with id {}, error {}", EdgeLabelEnum.CATEGORY, componentV.getUniqueId(), childVertex.right().value());
1168             return childVertex.right().value();
1169         }
1170         GraphVertex categoryV = childVertex.left().value();
1171         Map<GraphPropertyEnum, Object> metadataProperties = categoryV.getMetadataProperties();
1172         CategoryDefinition category = new CategoryDefinition();
1173         category.setUniqueId(categoryV.getUniqueId());
1174         category.setNormalizedName((String) metadataProperties.get(GraphPropertyEnum.NORMALIZED_NAME));
1175         category.setName((String) metadataProperties.get(GraphPropertyEnum.NAME));
1176         final Boolean useServiceSubstitutionForNestedServices = (Boolean)metadataProperties.get(GraphPropertyEnum.USE_SUBSTITUTION_FOR_NESTED_SERVICES);
1177         category.setUseServiceSubstitutionForNestedServices(useServiceSubstitutionForNestedServices == null ? false : useServiceSubstitutionForNestedServices);
1178
1179         Type listTypeCat = new TypeToken<List<String>>() {}.getType();
1180         List<String> iconsfromJsonCat = getGson().fromJson((String) metadataProperties.get(GraphPropertyEnum.ICONS.getProperty()), listTypeCat);
1181         category.setIcons(iconsfromJsonCat);
1182         
1183         final Type metadataKeysTypeCat = new TypeToken<List<MetadataKeyDataDefinition>>() {}.getType();
1184         final List<MetadataKeyDataDefinition> metadataKeysfromJsonCat = getGson().fromJson((String) metadataProperties.get(GraphPropertyEnum.METADATA_KEYS), metadataKeysTypeCat);
1185         category.setMetadataKeys(metadataKeysfromJsonCat);
1186         
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 }