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