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