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