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