Filter categories by model name
[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,
613             VertexTypeEnum.SERVICE_CATEGORY);
614         if (category.isRight()) {
615             log.trace("NO category {} for service {}", categoryName, topologyTemplate.getUniqueId());
616             return StorageOperationStatus.CATEGORY_NOT_FOUND;
617         }
618         GraphVertex categoryV = category.left().value();
619         JanusGraphOperationStatus createEdge = janusGraphDao.createEdge(nodeTypeVertex, categoryV, EdgeLabelEnum.CATEGORY, new HashMap<>());
620         if (createEdge != JanusGraphOperationStatus.OK) {
621             log.trace("Failed to associate resource {} to category {} with id {}", topologyTemplate.getUniqueId(), categoryName,
622                 categoryV.getUniqueId());
623             return DaoStatusConverter.convertJanusGraphStatusToStorageStatus(createEdge);
624         }
625         return StorageOperationStatus.OK;
626     }
627
628     @Override
629     public Either<ToscaElement, StorageOperationStatus> getToscaElement(String uniqueId, ComponentParametersView componentParametersView) {
630         JsonParseFlagEnum parseFlag = componentParametersView.detectParseFlag();
631         Either<GraphVertex, StorageOperationStatus> componentByLabelAndId = getComponentByLabelAndId(uniqueId, ToscaElementTypeEnum.TOPOLOGY_TEMPLATE,
632             parseFlag);
633         if (componentByLabelAndId.isRight()) {
634             return Either.right(componentByLabelAndId.right().value());
635         }
636         GraphVertex componentV = componentByLabelAndId.left().value();
637         return getToscaElement(componentV, componentParametersView);
638     }
639
640     // -------------------------------------------------------------
641     public Either<ToscaElement, StorageOperationStatus> getToscaElement(GraphVertex componentV, ComponentParametersView componentParametersView) {
642         TopologyTemplate toscaElement;
643         toscaElement = convertToTopologyTemplate(componentV);
644         JanusGraphOperationStatus status;
645         if (!componentParametersView.isIgnoreUsers()) {
646             status = setCreatorFromGraph(componentV, toscaElement);
647             if (status != JanusGraphOperationStatus.OK) {
648                 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
649             }
650             status = setLastModifierFromGraph(componentV, toscaElement);
651             if (status != JanusGraphOperationStatus.OK) {
652                 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
653             }
654         }
655         if (!componentParametersView.isIgnoreCategories()) {
656             status = setTopologyTempalteCategoriesFromGraph(componentV, toscaElement);
657             if (status != JanusGraphOperationStatus.OK) {
658                 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
659             }
660         }
661         if (!componentParametersView.isIgnoreArtifacts()) {
662             JanusGraphOperationStatus storageStatus = setAllArtifactsFromGraph(componentV, toscaElement);
663             if (storageStatus != JanusGraphOperationStatus.OK) {
664                 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(storageStatus));
665             }
666         }
667         if (!componentParametersView.isIgnoreComponentInstancesProperties()) {
668             status = setComponentInstancesPropertiesFromGraph(componentV, toscaElement);
669             if (status != JanusGraphOperationStatus.OK) {
670                 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
671             }
672         }
673         if (!componentParametersView.isIgnoreComponentInstancesAttributes()) {
674             status = setComponentInstancesAttributesFromGraph(componentV, toscaElement);
675             if (status != JanusGraphOperationStatus.OK) {
676                 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
677             }
678         }
679         if (!componentParametersView.isIgnoreCapabilities()) {
680             status = setCapabilitiesFromGraph(componentV, toscaElement);
681             if (status != JanusGraphOperationStatus.OK) {
682                 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
683             }
684         }
685         if (!componentParametersView.isIgnoreRequirements()) {
686             status = setRequirementsFromGraph(componentV, toscaElement);
687             if (status != JanusGraphOperationStatus.OK) {
688                 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
689             }
690         }
691         if (!componentParametersView.isIgnoreAllVersions()) {
692             status = setAllVersions(componentV, toscaElement);
693             if (status != JanusGraphOperationStatus.OK) {
694                 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
695             }
696         }
697         if (!componentParametersView.isIgnoreAdditionalInformation()) {
698             status = setAdditionalInformationFromGraph(componentV, toscaElement);
699             if (status != JanusGraphOperationStatus.OK) {
700                 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
701             }
702         }
703         if (!componentParametersView.isIgnoreGroups()) {
704             status = setGroupsFromGraph(componentV, toscaElement);
705             if (status != JanusGraphOperationStatus.OK) {
706                 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
707             }
708         }
709         if (!componentParametersView.isIgnorePolicies()) {
710             status = setPoliciesFromGraph(componentV, toscaElement);
711             if (status != JanusGraphOperationStatus.OK) {
712                 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
713             }
714         }
715         if (!componentParametersView.isIgnoreComponentInstances()) {
716             status = setInstGroupsFromGraph(componentV, toscaElement);
717             //Mark all CIs that has archived origins
718             archiveOperation.setArchivedOriginsFlagInComponentInstances(componentV);
719             if (status != JanusGraphOperationStatus.OK) {
720                 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
721             }
722         }
723         if (!componentParametersView.isIgnoreInputs()) {
724             status = setInputsFromGraph(componentV, toscaElement);
725             if (status != JanusGraphOperationStatus.OK) {
726                 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
727             }
728         }
729         if (!componentParametersView.isIgnoreOutputs()) {
730             final JanusGraphOperationStatus storageStatus = setOutputsFromGraph(componentV, toscaElement);
731             if (storageStatus != JanusGraphOperationStatus.OK) {
732                 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(storageStatus));
733             }
734         }
735         if (!componentParametersView.isIgnoreProperties()) {
736             status = setPropertiesFromGraph(componentV, toscaElement);
737             if (status != JanusGraphOperationStatus.OK) {
738                 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
739             }
740         }
741         if (!componentParametersView.isIgnoreComponentInstancesInputs()) {
742             status = setComponentInstancesInputsFromGraph(componentV, toscaElement);
743             if (status != JanusGraphOperationStatus.OK) {
744                 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
745             }
746         }
747         if (!componentParametersView.isIgnoreCapabiltyProperties()) {
748             status = setComponentInstancesCapPropertiesFromGraph(componentV, toscaElement);
749             if (status != JanusGraphOperationStatus.OK) {
750                 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
751             }
752         }
753         if (!componentParametersView.isIgnoreServicePath()) {
754             status = setForwardingGraphPropertiesFromGraph(componentV, toscaElement);
755             if (status != JanusGraphOperationStatus.OK) {
756                 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
757             }
758         }
759         if (!componentParametersView.isIgnoreNodeFilter()) {
760             status = setNodeFilterComponentFromGraph(componentV, toscaElement);
761             if (status != JanusGraphOperationStatus.OK) {
762                 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
763             }
764         }
765         if (!componentParametersView.isIgnoreSubstitutionFilter()) {
766             status = setSubstitutionFilterComponentFromGraph(componentV, toscaElement);
767             if (status != JanusGraphOperationStatus.OK) {
768                 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
769             }
770         }
771         if (!componentParametersView.isIgnoreInterfaces()) {
772             JanusGraphOperationStatus storageStatus = setInterfacesFromGraph(componentV, toscaElement);
773             if (storageStatus != JanusGraphOperationStatus.OK) {
774                 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(storageStatus));
775             }
776         }
777         if (!componentParametersView.isIgnoreInterfaces()) {
778             JanusGraphOperationStatus storageStatus = setInterfcesFromGraph(componentV, toscaElement);
779             if (storageStatus != JanusGraphOperationStatus.OK) {
780                 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(storageStatus));
781             }
782         }
783         if (!componentParametersView.isIgnoreComponentInstancesInterfaces()) {
784             JanusGraphOperationStatus storageStatus = setComponentInstancesInterfacesFromGraph(componentV, toscaElement);
785             if (storageStatus != JanusGraphOperationStatus.OK) {
786                 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(storageStatus));
787             }
788         }
789         if (!componentParametersView.isIgnoreDataType()) {
790             JanusGraphOperationStatus storageStatus = setDataTypesFromGraph(componentV, toscaElement);
791             if (storageStatus != JanusGraphOperationStatus.OK) {
792                 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(storageStatus));
793             }
794         }
795         return Either.left(toscaElement);
796     }
797
798     private JanusGraphOperationStatus setDataTypesFromGraph(GraphVertex componentV, TopologyTemplate toscaElement) {
799         Either<Map<String, DataTypeDataDefinition>, JanusGraphOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.DATA_TYPES);
800         if (result.isLeft()) {
801             toscaElement.setDataTypes(result.left().value());
802         } else {
803             if (result.right().value() != JanusGraphOperationStatus.NOT_FOUND) {
804                 return result.right().value();
805             }
806         }
807         return JanusGraphOperationStatus.OK;
808     }
809
810     private JanusGraphOperationStatus setInterfcesFromGraph(GraphVertex componentV, TopologyTemplate topologyTemplate) {
811         Either<Map<String, InterfaceDataDefinition>, JanusGraphOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.INTERFACE);
812         if (result.isLeft()) {
813             topologyTemplate.setInterfaces(result.left().value());
814         } else {
815             if (result.right().value() != JanusGraphOperationStatus.NOT_FOUND) {
816                 return result.right().value();
817             }
818         }
819         return JanusGraphOperationStatus.OK;
820     }
821
822     private JanusGraphOperationStatus setPoliciesFromGraph(GraphVertex componentV, TopologyTemplate toscaElement) {
823         Either<Map<String, PolicyDataDefinition>, JanusGraphOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.POLICIES);
824         if (result.isLeft()) {
825             toscaElement.setPolicies(result.left().value());
826         } else {
827             if (result.right().value() != JanusGraphOperationStatus.NOT_FOUND) {
828                 return result.right().value();
829             }
830         }
831         return JanusGraphOperationStatus.OK;
832     }
833
834     private JanusGraphOperationStatus setInterfacesFromGraph(GraphVertex componentV, TopologyTemplate topologyTemplate) {
835         Either<Map<String, InterfaceDataDefinition>, JanusGraphOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.INTERFACE);
836         if (result.isLeft()) {
837             topologyTemplate.setInterfaces(result.left().value());
838         } else {
839             if (result.right().value() != JanusGraphOperationStatus.NOT_FOUND) {
840                 return result.right().value();
841             }
842         }
843         return JanusGraphOperationStatus.OK;
844     }
845
846     private JanusGraphOperationStatus setComponentInstancesInterfacesFromGraph(GraphVertex componentV, TopologyTemplate topologyTemplate) {
847         Either<Map<String, MapInterfaceDataDefinition>, JanusGraphOperationStatus> result = getDataFromGraph(componentV,
848             EdgeLabelEnum.INST_INTERFACES);
849         if (result.isLeft()) {
850             result.left().value().entrySet().forEach(entry -> topologyTemplate.addComponentInstanceInterfaceMap(entry.getKey(), entry.getValue()));
851         } else {
852             if (result.right().value() != JanusGraphOperationStatus.NOT_FOUND) {
853                 return result.right().value();
854             }
855         }
856         return JanusGraphOperationStatus.OK;
857     }
858
859     private StorageOperationStatus associateInterfacesToComponent(GraphVertex topologyTemplateVertex, TopologyTemplate topologyTemplate) {
860         Map<String, InterfaceDataDefinition> interfaceMap = topologyTemplate.getInterfaces();
861         if (interfaceMap != null && !interfaceMap.isEmpty()) {
862             Either<GraphVertex, StorageOperationStatus> assosiateElementToData = associateElementToData(topologyTemplateVertex,
863                 VertexTypeEnum.INTERFACE, EdgeLabelEnum.INTERFACE, interfaceMap);
864             if (assosiateElementToData.isRight()) {
865                 return assosiateElementToData.right().value();
866             }
867         }
868         return StorageOperationStatus.OK;
869     }
870
871     private JanusGraphOperationStatus setForwardingGraphPropertiesFromGraph(GraphVertex componentV, TopologyTemplate topologyTemplate) {
872         Either<Map<String, ForwardingPathDataDefinition>, JanusGraphOperationStatus> result = getDataFromGraph(componentV,
873             EdgeLabelEnum.FORWARDING_PATH);
874         if (result.isLeft()) {
875             topologyTemplate.setForwardingPaths(result.left().value());
876         } else {
877             if (result.right().value() != JanusGraphOperationStatus.NOT_FOUND) {
878                 return result.right().value();
879             }
880         }
881         return JanusGraphOperationStatus.OK;
882     }
883
884     private JanusGraphOperationStatus setComponentInstancesCapPropertiesFromGraph(GraphVertex componentV, TopologyTemplate topologyTemplate) {
885         Either<Map<String, MapCapabilityProperty>, JanusGraphOperationStatus> result = getDataFromGraph(componentV,
886             EdgeLabelEnum.CALCULATED_CAP_PROPERTIES);
887         if (result.isLeft()) {
888             topologyTemplate.setCalculatedCapabilitiesProperties(result.left().value());
889         } else {
890             if (result.right().value() != JanusGraphOperationStatus.NOT_FOUND) {
891                 return result.right().value();
892             }
893         }
894         Either<Map<String, MapPropertiesDataDefinition>, JanusGraphOperationStatus> capPropResult = getDataFromGraph(componentV,
895             EdgeLabelEnum.CAPABILITIES_PROPERTIES);
896         if (capPropResult.isLeft()) {
897             topologyTemplate.setCapabilitiesProperties(capPropResult.left().value());
898         } else {
899             if (capPropResult.right().value() != JanusGraphOperationStatus.NOT_FOUND) {
900                 return capPropResult.right().value();
901             }
902         }
903         return JanusGraphOperationStatus.OK;
904     }
905
906     private JanusGraphOperationStatus setPropertiesFromGraph(GraphVertex componentV, TopologyTemplate toscaElement) {
907         Either<Map<String, PropertyDataDefinition>, JanusGraphOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.PROPERTIES);
908         if (result.isLeft()) {
909             toscaElement.setProperties(result.left().value());
910         } else {
911             if (result.right().value() != JanusGraphOperationStatus.NOT_FOUND) {
912                 return result.right().value();
913             }
914         }
915         return JanusGraphOperationStatus.OK;
916     }
917
918     private JanusGraphOperationStatus setInstGroupsFromGraph(GraphVertex componentV, TopologyTemplate topologyTemplate) {
919         Either<Map<String, MapGroupsDataDefinition>, JanusGraphOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.INST_GROUPS);
920         if (result.isLeft()) {
921             topologyTemplate.setInstGroups(result.left().value());
922         } else {
923             if (result.right().value() != JanusGraphOperationStatus.NOT_FOUND) {
924                 return result.right().value();
925             }
926         }
927         return JanusGraphOperationStatus.OK;
928     }
929
930     private JanusGraphOperationStatus setComponentInstancesPropertiesFromGraph(GraphVertex componentV, TopologyTemplate topologyTemplate) {
931         Either<Map<String, MapPropertiesDataDefinition>, JanusGraphOperationStatus> result = getDataFromGraph(componentV,
932             EdgeLabelEnum.INST_PROPERTIES);
933         if (result.isLeft()) {
934             topologyTemplate.setInstProperties(result.left().value());
935         } else {
936             if (result.right().value() != JanusGraphOperationStatus.NOT_FOUND) {
937                 return result.right().value();
938             }
939         }
940         return JanusGraphOperationStatus.OK;
941     }
942
943     private JanusGraphOperationStatus setComponentInstancesAttributesFromGraph(final GraphVertex componentV,
944                                                                                final TopologyTemplate topologyTemplate) {
945         final Either<Map<String, MapAttributesDataDefinition>, JanusGraphOperationStatus> result = getDataFromGraph(componentV,
946             EdgeLabelEnum.INST_ATTRIBUTES);
947         if (result.isLeft()) {
948             topologyTemplate.setInstAttributes(result.left().value());
949         } else if (result.right().value() != JanusGraphOperationStatus.NOT_FOUND) {
950             return result.right().value();
951         }
952         return JanusGraphOperationStatus.OK;
953     }
954
955     private JanusGraphOperationStatus setComponentInstancesInputsFromGraph(GraphVertex componentV, TopologyTemplate topologyTemplate) {
956         Either<Map<String, MapPropertiesDataDefinition>, JanusGraphOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.INST_INPUTS);
957         if (result.isLeft()) {
958             topologyTemplate.setInstInputs(result.left().value());
959         } else {
960             if (result.right().value() != JanusGraphOperationStatus.NOT_FOUND) {
961                 return result.right().value();
962             }
963         }
964         return JanusGraphOperationStatus.OK;
965     }
966
967     private JanusGraphOperationStatus setNodeFilterComponentFromGraph(final GraphVertex componentV, final TopologyTemplate topologyTemplate) {
968         final Either<Map<String, CINodeFilterDataDefinition>, JanusGraphOperationStatus> result = getDataFromGraph(componentV,
969             EdgeLabelEnum.NODE_FILTER_TEMPLATE);
970         if (result.isLeft()) {
971             topologyTemplate.setNodeFilterComponents(result.left().value());
972         } else {
973             if (result.right().value() != JanusGraphOperationStatus.NOT_FOUND) {
974                 return result.right().value();
975             }
976         }
977         return JanusGraphOperationStatus.OK;
978     }
979
980     private JanusGraphOperationStatus setSubstitutionFilterComponentFromGraph(final GraphVertex componentV, final TopologyTemplate topologyTemplate) {
981         final Either<Map<String, SubstitutionFilterDataDefinition>, JanusGraphOperationStatus> result = getDataFromGraph(componentV,
982             EdgeLabelEnum.SUBSTITUTION_FILTER_TEMPLATE);
983         if (result.isLeft()) {
984             final Map<String, SubstitutionFilterDataDefinition> filters = result.left().value();
985             if (MapUtils.isEmpty(filters)) {
986                 return JanusGraphOperationStatus.OK;
987             }
988             if(filters.values().size() > 1) {
989                 log.error(EcompLoggerErrorCode.DATA_ERROR, TopologyTemplateOperation.class.getName(),
990                     (ErrorLogOptionalData) null, "Only a single substitution filter is expected, but got '{}'", filters.values().size());
991                 return JanusGraphOperationStatus.GENERAL_ERROR;
992             }
993             topologyTemplate.setSubstitutionFilters(new SubstitutionFilterDataDefinition(filters.values().iterator().next()));
994         } else {
995             if (result.right().value() != JanusGraphOperationStatus.NOT_FOUND) {
996                 return result.right().value();
997             }
998         }
999         return JanusGraphOperationStatus.OK;
1000     }
1001
1002     @Override
1003     protected <T extends ToscaElement> JanusGraphOperationStatus setRequirementsFromGraph(GraphVertex componentV, T toscaElement) {
1004         Either<Map<String, MapListRequirementDataDefinition>, JanusGraphOperationStatus> result = getDataFromGraph(componentV,
1005             EdgeLabelEnum.CALCULATED_REQUIREMENTS);
1006         if (result.isLeft()) {
1007             ((TopologyTemplate) toscaElement).setCalculatedRequirements(result.left().value());
1008         } else {
1009             if (result.right().value() != JanusGraphOperationStatus.NOT_FOUND) {
1010                 return result.right().value();
1011             }
1012         }
1013         result = getDataFromGraph(componentV, EdgeLabelEnum.FULLFILLED_REQUIREMENTS);
1014         if (result.isLeft()) {
1015             ((TopologyTemplate) toscaElement).setFullfilledRequirements(result.left().value());
1016         } else {
1017             if (result.right().value() != JanusGraphOperationStatus.NOT_FOUND) {
1018                 return result.right().value();
1019             }
1020         }
1021         Either<Map<String, ListRequirementDataDefinition>, JanusGraphOperationStatus> requirementResult = getDataFromGraph(componentV,
1022             EdgeLabelEnum.REQUIREMENTS);
1023         if (requirementResult.isLeft()) {
1024             toscaElement.setRequirements(requirementResult.left().value());
1025         } else {
1026             if (requirementResult.right().value() != JanusGraphOperationStatus.NOT_FOUND) {
1027                 return requirementResult.right().value();
1028             }
1029         }
1030         return JanusGraphOperationStatus.OK;
1031     }
1032
1033     protected <T extends ToscaElement> JanusGraphOperationStatus setCapabilitiesFromGraph(GraphVertex componentV, T toscaElement) {
1034         Either<Map<String, MapListCapabilityDataDefinition>, JanusGraphOperationStatus> result = getDataFromGraph(componentV,
1035             EdgeLabelEnum.CALCULATED_CAPABILITIES);
1036         if (result.isLeft()) {
1037             ((TopologyTemplate) toscaElement).setCalculatedCapabilities(result.left().value());
1038         } else {
1039             if (result.right().value() != JanusGraphOperationStatus.NOT_FOUND) {
1040                 return result.right().value();
1041             }
1042         }
1043         result = getDataFromGraph(componentV, EdgeLabelEnum.FULLFILLED_CAPABILITIES);
1044         if (result.isLeft()) {
1045             ((TopologyTemplate) toscaElement).setFullfilledCapabilities(result.left().value());
1046         } else {
1047             if (result.right().value() != JanusGraphOperationStatus.NOT_FOUND) {
1048                 return result.right().value();
1049             }
1050         }
1051         Either<Map<String, ListCapabilityDataDefinition>, JanusGraphOperationStatus> capabilitiesResult = getDataFromGraph(componentV,
1052             EdgeLabelEnum.CAPABILITIES);
1053         if (capabilitiesResult.isLeft()) {
1054             toscaElement.setCapabilities(capabilitiesResult.left().value());
1055         } else {
1056             if (capabilitiesResult.right().value() != JanusGraphOperationStatus.NOT_FOUND) {
1057                 return capabilitiesResult.right().value();
1058             }
1059         }
1060         return JanusGraphOperationStatus.OK;
1061     }
1062
1063     private JanusGraphOperationStatus setAllArtifactsFromGraph(GraphVertex componentV, TopologyTemplate toscaElement) {
1064         JanusGraphOperationStatus storageStatus = setArtifactsFromGraph(componentV, toscaElement);
1065         if (storageStatus != JanusGraphOperationStatus.OK) {
1066             return storageStatus;
1067         }
1068         Either<Map<String, ArtifactDataDefinition>, JanusGraphOperationStatus> result = getDataFromGraph(componentV,
1069             EdgeLabelEnum.SERVICE_API_ARTIFACTS);
1070         if (result.isLeft()) {
1071             toscaElement.setServiceApiArtifacts(result.left().value());
1072         } else {
1073             if (result.right().value() != JanusGraphOperationStatus.NOT_FOUND) {
1074                 return result.right().value();
1075             }
1076         }
1077         Either<Map<String, MapArtifactDataDefinition>, JanusGraphOperationStatus> resultInstArt = getDataFromGraph(componentV,
1078             EdgeLabelEnum.INST_DEPLOYMENT_ARTIFACTS);
1079         if (resultInstArt.isLeft()) {
1080             toscaElement.setInstDeploymentArtifacts(resultInstArt.left().value());
1081         } else {
1082             if (resultInstArt.right().value() != JanusGraphOperationStatus.NOT_FOUND) {
1083                 return resultInstArt.right().value();
1084             }
1085         }
1086         Either<Map<String, MapArtifactDataDefinition>, JanusGraphOperationStatus> instanceArt = getDataFromGraph(componentV,
1087             EdgeLabelEnum.INSTANCE_ARTIFACTS);
1088         if (instanceArt.isLeft()) {
1089             toscaElement.setInstanceArtifacts(instanceArt.left().value());
1090         } else {
1091             if (instanceArt.right().value() != JanusGraphOperationStatus.NOT_FOUND) {
1092                 return instanceArt.right().value();
1093             }
1094         }
1095         return JanusGraphOperationStatus.OK;
1096     }
1097
1098     private JanusGraphOperationStatus setInputsFromGraph(GraphVertex componentV, TopologyTemplate toscaElement) {
1099         Either<Map<String, PropertyDataDefinition>, JanusGraphOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.INPUTS);
1100         if (result.isLeft()) {
1101             toscaElement.setInputs(result.left().value());
1102         } else {
1103             if (result.right().value() != JanusGraphOperationStatus.NOT_FOUND) {
1104                 return result.right().value();
1105             }
1106         }
1107         Either<Map<String, ListCapabilityDataDefinition>, JanusGraphOperationStatus> capabilitiesResult = getDataFromGraph(componentV,
1108             EdgeLabelEnum.CAPABILITIES);
1109         if (capabilitiesResult.isLeft()) {
1110             toscaElement.setCapabilities(capabilitiesResult.left().value());
1111         } else {
1112             if (capabilitiesResult.right().value() != JanusGraphOperationStatus.NOT_FOUND) {
1113                 return capabilitiesResult.right().value();
1114             }
1115         }
1116         return JanusGraphOperationStatus.OK;
1117     }
1118
1119     private JanusGraphOperationStatus setOutputsFromGraph(final GraphVertex componentV, final TopologyTemplate toscaElement) {
1120         final Either<Map<String, AttributeDataDefinition>, JanusGraphOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.OUTPUTS);
1121         if (result.isLeft()) {
1122             toscaElement.setOutputs(result.left().value());
1123         } else {
1124             if (result.right().value() != JanusGraphOperationStatus.NOT_FOUND) {
1125                 return result.right().value();
1126             }
1127         }
1128         return JanusGraphOperationStatus.OK;
1129     }
1130
1131     private JanusGraphOperationStatus setGroupsFromGraph(GraphVertex componentV, TopologyTemplate toscaElement) {
1132         Either<Map<String, GroupDataDefinition>, JanusGraphOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.GROUPS);
1133         if (result.isLeft()) {
1134             toscaElement.setGroups(result.left().value());
1135         } else {
1136             if (result.right().value() != JanusGraphOperationStatus.NOT_FOUND) {
1137                 return result.right().value();
1138             }
1139         }
1140         return JanusGraphOperationStatus.OK;
1141     }
1142
1143     private JanusGraphOperationStatus setTopologyTempalteCategoriesFromGraph(GraphVertex componentV, ToscaElement toscaElement) {
1144         List<CategoryDefinition> categories = new ArrayList<>();
1145         switch (componentV.getType()) {
1146             case RESOURCE:
1147                 return setResourceCategoryFromGraph(componentV, toscaElement);
1148             case SERVICE:
1149                 return setServiceCategoryFromGraph(componentV, toscaElement, categories);
1150             default:
1151                 log.debug("Not supported component type {} ", componentV.getType());
1152                 break;
1153         }
1154         return JanusGraphOperationStatus.OK;
1155     }
1156
1157     private JanusGraphOperationStatus setServiceCategoryFromGraph(GraphVertex componentV, ToscaElement toscaElement,
1158                                                                   List<CategoryDefinition> categories) {
1159         Either<GraphVertex, JanusGraphOperationStatus> childVertex = janusGraphDao
1160             .getChildVertex(componentV, EdgeLabelEnum.CATEGORY, JsonParseFlagEnum.NoParse);
1161         if (childVertex.isRight()) {
1162             log.debug("failed to fetch {} for tosca element with id {}, error {}", EdgeLabelEnum.CATEGORY, componentV.getUniqueId(),
1163                 childVertex.right().value());
1164             return childVertex.right().value();
1165         }
1166         GraphVertex categoryV = childVertex.left().value();
1167         Map<GraphPropertyEnum, Object> metadataProperties = categoryV.getMetadataProperties();
1168         CategoryDefinition category = new CategoryDefinition();
1169         category.setUniqueId(categoryV.getUniqueId());
1170         category.setNormalizedName((String) metadataProperties.get(GraphPropertyEnum.NORMALIZED_NAME));
1171         category.setName((String) metadataProperties.get(GraphPropertyEnum.NAME));
1172         final Boolean useServiceSubstitutionForNestedServices = (Boolean) metadataProperties
1173             .get(GraphPropertyEnum.USE_SUBSTITUTION_FOR_NESTED_SERVICES);
1174         category.setUseServiceSubstitutionForNestedServices(
1175             useServiceSubstitutionForNestedServices == null ? false : useServiceSubstitutionForNestedServices);
1176         Type listTypeCat = new TypeToken<List<String>>() {
1177         }.getType();
1178         List<String> iconsfromJsonCat = getGson().fromJson((String) metadataProperties.get(GraphPropertyEnum.ICONS.getProperty()), listTypeCat);
1179         category.setIcons(iconsfromJsonCat);
1180         category.setModels((getGson().fromJson((String) metadataProperties.get(GraphPropertyEnum.MODEL.getProperty()), listTypeCat)));
1181         final Type metadataKeysTypeCat = new TypeToken<List<MetadataKeyDataDefinition>>() {
1182         }.getType();
1183         final List<MetadataKeyDataDefinition> metadataKeysfromJsonCat = getGson()
1184             .fromJson((String) metadataProperties.get(GraphPropertyEnum.METADATA_KEYS), metadataKeysTypeCat);
1185         category.setMetadataKeys(metadataKeysfromJsonCat);
1186         categories.add(category);
1187         toscaElement.setCategories(categories);
1188         return JanusGraphOperationStatus.OK;
1189     }
1190
1191     @SuppressWarnings("unchecked")
1192     private TopologyTemplate convertToTopologyTemplate(GraphVertex componentV) {
1193         TopologyTemplate topologyTemplate = super.convertToComponent(componentV);
1194         Map<String, CompositionDataDefinition> json = (Map<String, CompositionDataDefinition>) componentV.getJson();
1195         topologyTemplate.setCompositions(json);
1196         return topologyTemplate;
1197     }
1198
1199     @Override
1200     public Either<ToscaElement, StorageOperationStatus> deleteToscaElement(GraphVertex toscaElementVertex) {
1201         Either<ToscaElement, StorageOperationStatus> nodeType = getToscaElement(toscaElementVertex, new ComponentParametersView());
1202         if (nodeType.isRight()) {
1203             log.debug("Failed to fetch tosca element {} error {}", toscaElementVertex.getUniqueId(), nodeType.right().value());
1204             return nodeType;
1205         }
1206         JanusGraphOperationStatus status = disassociateAndDeleteCommonElements(toscaElementVertex);
1207         if (status != JanusGraphOperationStatus.OK) {
1208             Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
1209         }
1210         status = janusGraphDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.INST_ATTRIBUTES);
1211         if (status != JanusGraphOperationStatus.OK) {
1212             log.debug("Failed to disassociate instances attributes for {} error {}", toscaElementVertex.getUniqueId(), status);
1213             Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
1214         }
1215         status = janusGraphDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.INST_PROPERTIES);
1216         if (status != JanusGraphOperationStatus.OK) {
1217             log.debug("Failed to disassociate instances properties for {} error {}", toscaElementVertex.getUniqueId(), status);
1218             Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
1219         }
1220         status = janusGraphDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.INST_INPUTS);
1221         if (status != JanusGraphOperationStatus.OK) {
1222             log.debug("Failed to disassociate instances inputs for {} error {}", toscaElementVertex.getUniqueId(), status);
1223             Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
1224         }
1225         status = janusGraphDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.GROUPS);
1226         if (status != JanusGraphOperationStatus.OK) {
1227             log.debug("Failed to disassociate groups for {} error {}", toscaElementVertex.getUniqueId(), status);
1228             Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
1229         }
1230         status = janusGraphDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.POLICIES);
1231         if (status != JanusGraphOperationStatus.OK) {
1232             log.debug("Failed to disassociate policies for {} error {}", toscaElementVertex.getUniqueId(), status);
1233             Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
1234         }
1235         status = janusGraphDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.INST_GROUPS);
1236         if (status != JanusGraphOperationStatus.OK) {
1237             log.debug("Failed to disassociate instance groups for {} error {}", toscaElementVertex.getUniqueId(), status);
1238             Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
1239         }
1240         status = janusGraphDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.INPUTS);
1241         if (status != JanusGraphOperationStatus.OK) {
1242             log.debug("Failed to disassociate inputs for {} error {}", toscaElementVertex.getUniqueId(), status);
1243             Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
1244         }
1245         status = janusGraphDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.INST_INPUTS);
1246         if (status != JanusGraphOperationStatus.OK) {
1247             log.debug("Failed to disassociate instance inputs for {} error {}", toscaElementVertex.getUniqueId(), status);
1248             Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
1249         }
1250         status = janusGraphDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.CAPABILITIES_PROPERTIES);
1251         if (status != JanusGraphOperationStatus.OK) {
1252             log.debug("Failed to disassociate capabilities properties for {} error {}", toscaElementVertex.getUniqueId(), status);
1253             Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
1254         }
1255         status = janusGraphDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.CALCULATED_CAPABILITIES);
1256         if (status != JanusGraphOperationStatus.OK) {
1257             log.debug("Failed to disassociate calculated capabiliites for {} error {}", toscaElementVertex.getUniqueId(), status);
1258             Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
1259         }
1260         status = janusGraphDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.FULLFILLED_CAPABILITIES);
1261         if (status != JanusGraphOperationStatus.OK) {
1262             log.debug("Failed to disassociate fullfilled capabilities for {} error {}", toscaElementVertex.getUniqueId(), status);
1263             Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
1264         }
1265         status = janusGraphDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.CALCULATED_CAP_PROPERTIES);
1266         if (status != JanusGraphOperationStatus.OK) {
1267             log.debug("Failed to disassociate calculated capabiliites properties for {} error {}", toscaElementVertex.getUniqueId(), status);
1268             Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
1269         }
1270         status = janusGraphDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.CALCULATED_REQUIREMENTS);
1271         if (status != JanusGraphOperationStatus.OK) {
1272             log.debug("Failed to disassociate calculated requirements for {} error {}", toscaElementVertex.getUniqueId(), status);
1273             Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
1274         }
1275         status = janusGraphDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.FULLFILLED_REQUIREMENTS);
1276         if (status != JanusGraphOperationStatus.OK) {
1277             log.debug("Failed to disassociate full filled requirements for {} error {}", toscaElementVertex.getUniqueId(), status);
1278             Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
1279         }
1280         status = janusGraphDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.INST_DEPLOYMENT_ARTIFACTS);
1281         if (status != JanusGraphOperationStatus.OK) {
1282             log.debug("Failed to disassociate instance artifacts for {} error {}", toscaElementVertex.getUniqueId(), status);
1283             Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
1284         }
1285         status = janusGraphDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.SERVICE_API_ARTIFACTS);
1286         if (status != JanusGraphOperationStatus.OK) {
1287             log.debug("Failed to disassociate service api artifacts for {} error {}", toscaElementVertex.getUniqueId(), status);
1288             Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
1289         }
1290         status = janusGraphDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.FORWARDING_PATH);
1291         if (status != JanusGraphOperationStatus.OK) {
1292             log.debug("Failed to disassociate service api artifacts for {} error {}", toscaElementVertex.getUniqueId(), status);
1293             Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
1294         }
1295         status = janusGraphDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.INTERFACE);
1296         if (status != JanusGraphOperationStatus.OK) {
1297             log.debug("Failed to disassociate interfaces for {} error {}", toscaElementVertex.getUniqueId(), status);
1298             return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
1299         }
1300         status = janusGraphDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.INSTANCE_ARTIFACTS);
1301         if (status != JanusGraphOperationStatus.OK) {
1302             log.debug("Failed to disassociate instance artifact for {} error {}", toscaElementVertex.getUniqueId(), status);
1303             return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
1304         }
1305         status = janusGraphDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.REQUIREMENTS);
1306         if (status != JanusGraphOperationStatus.OK) {
1307             log.debug("Failed to disassociate requirements for {} error {}", toscaElementVertex.getUniqueId(), status);
1308             return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
1309         }
1310         status = janusGraphDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.CAPABILITIES);
1311         if (status != JanusGraphOperationStatus.OK) {
1312             log.debug("Failed to disassociate capabilities for {} error {}", toscaElementVertex.getUniqueId(), status);
1313             return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
1314         }
1315         status = janusGraphDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.INST_INTERFACES);
1316         if (status != JanusGraphOperationStatus.OK) {
1317             log.debug("Failed to disassociate instances interfaces for {} error {}", toscaElementVertex.getUniqueId(), status);
1318             return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
1319         }
1320         toscaElementVertex.getVertex().remove();
1321         log.trace("Tosca element vertex for {} was removed", toscaElementVertex.getUniqueId());
1322         return nodeType;
1323     }
1324
1325     @SuppressWarnings("unchecked")
1326     @Override
1327     public Either<TopologyTemplate, StorageOperationStatus> createToscaElement(ToscaElement toscaElement) {
1328         return createTopologyTemplate((TopologyTemplate) toscaElement);
1329     }
1330
1331     @Override
1332     protected <T extends ToscaElement> JanusGraphOperationStatus setCategoriesFromGraph(GraphVertex vertexComponent, T toscaElement) {
1333         return setTopologyTempalteCategoriesFromGraph(vertexComponent, toscaElement);
1334     }
1335
1336     @Override
1337     protected <T extends ToscaElement> StorageOperationStatus validateCategories(T toscaElementToUpdate, GraphVertex elementV) {
1338         // Product isn't supported now!!
1339
1340         // TODO add for Product
1341         if (toscaElementToUpdate.getComponentType() == ComponentTypeEnum.SERVICE) {
1342             return validateServiceCategory(toscaElementToUpdate, elementV);
1343         } else {
1344             // Resource
1345             return validateResourceCategory(toscaElementToUpdate, elementV);
1346         }
1347     }
1348
1349     @Override
1350     protected <T extends ToscaElement> StorageOperationStatus updateDerived(T toscaElementToUpdate, GraphVertex updateElementV) {
1351         // not relevant now for topology template
1352         return StorageOperationStatus.OK;
1353     }
1354
1355     @Override
1356     public <T extends ToscaElement> void fillToscaElementVertexData(GraphVertex elementV, T toscaElementToUpdate, JsonParseFlagEnum flag) {
1357         fillMetadata(elementV, (TopologyTemplate) toscaElementToUpdate, flag);
1358     }
1359
1360     private <T extends ToscaElement> StorageOperationStatus validateServiceCategory(T toscaElementToUpdate, GraphVertex elementV) {
1361         StorageOperationStatus status = StorageOperationStatus.OK;
1362         List<CategoryDefinition> newCategoryList = toscaElementToUpdate.getCategories();
1363         CategoryDefinition newCategory = newCategoryList.get(0);
1364         Either<GraphVertex, JanusGraphOperationStatus> childVertex = janusGraphDao
1365             .getChildVertex(elementV, EdgeLabelEnum.CATEGORY, JsonParseFlagEnum.NoParse);
1366         if (childVertex.isRight()) {
1367             log.debug("failed to fetch {} for tosca element with id {}, error {}", EdgeLabelEnum.CATEGORY, elementV.getUniqueId(),
1368                 childVertex.right().value());
1369             return DaoStatusConverter.convertJanusGraphStatusToStorageStatus(childVertex.right().value());
1370         }
1371         GraphVertex categoryV = childVertex.left().value();
1372         Map<GraphPropertyEnum, Object> metadataProperties = categoryV.getMetadataProperties();
1373         String categoryNameCurrent = (String) metadataProperties.get(GraphPropertyEnum.NAME);
1374         String newCategoryName = newCategory.getName();
1375         if (newCategoryName != null && !newCategoryName.equals(categoryNameCurrent)) {
1376             // the category was changed
1377             Either<GraphVertex, StorageOperationStatus> getCategoryVertex = categoryOperation
1378                 .getCategory(newCategoryName, VertexTypeEnum.SERVICE_CATEGORY);
1379             if (getCategoryVertex.isRight()) {
1380                 return getCategoryVertex.right().value();
1381             }
1382             GraphVertex newCategoryV = getCategoryVertex.left().value();
1383             status = moveCategoryEdge(elementV, newCategoryV);
1384             log.debug("Going to update the category of the resource from {} to {}. status is {}", categoryNameCurrent, newCategory, status);
1385         }
1386         return status;
1387     }
1388
1389     public Either<GraphVertex, StorageOperationStatus> updateDistributionStatus(String uniqueId, User user,
1390                                                                                 DistributionStatusEnum distributionStatus) {
1391         Either<GraphVertex, StorageOperationStatus> result = null;
1392         String userId = user.getUserId();
1393         Either<GraphVertex, JanusGraphOperationStatus> getRes = findUserVertex(userId);
1394         GraphVertex userVertex = null;
1395         GraphVertex serviceVertex = null;
1396         if (getRes.isRight()) {
1397             JanusGraphOperationStatus status = getRes.right().value();
1398             CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Cannot find user {} in the graph. status is {}", userId, status);
1399             result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
1400         }
1401         if (result == null) {
1402             userVertex = getRes.left().value();
1403             getRes = janusGraphDao.getVertexById(uniqueId, JsonParseFlagEnum.ParseMetadata);
1404             if (getRes.isRight()) {
1405                 JanusGraphOperationStatus status = getRes.right().value();
1406                 log.debug("Cannot find service {} in the graph. status is {}", uniqueId, status);
1407                 result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
1408             }
1409         }
1410         if (result == null) {
1411             serviceVertex = getRes.left().value();
1412             Iterator<Edge> edgeIterator = serviceVertex.getVertex().edges(Direction.IN, EdgeLabelEnum.LAST_DISTRIBUTION_STATE_MODIFIER.name());
1413             if (edgeIterator.hasNext()) {
1414                 log.debug("Remove existing edge from user to component {}. Edge type is {}", userId, uniqueId,
1415                     EdgeLabelEnum.LAST_DISTRIBUTION_STATE_MODIFIER);
1416                 edgeIterator.next().remove();
1417             }
1418         }
1419         if (result == null) {
1420             JanusGraphOperationStatus status = janusGraphDao
1421                 .createEdge(userVertex, serviceVertex, EdgeLabelEnum.LAST_DISTRIBUTION_STATE_MODIFIER, null);
1422             if (status != JanusGraphOperationStatus.OK) {
1423                 log.debug("Failed to associate user {} to component {}. Edge type is {}", userId, uniqueId,
1424                     EdgeLabelEnum.LAST_DISTRIBUTION_STATE_MODIFIER);
1425                 result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
1426             }
1427         }
1428         if (result == null) {
1429             serviceVertex.addMetadataProperty(GraphPropertyEnum.DISTRIBUTION_STATUS, distributionStatus.name());
1430             long lastUpdateDate = System.currentTimeMillis();
1431             serviceVertex.setJsonMetadataField(JsonPresentationFields.LAST_UPDATE_DATE, lastUpdateDate);
1432             Either<GraphVertex, JanusGraphOperationStatus> updateRes = janusGraphDao.updateVertex(serviceVertex);
1433             if (updateRes.isRight()) {
1434                 result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(updateRes.right().value()));
1435             }
1436         }
1437         if (result == null) {
1438             result = Either.left(serviceVertex);
1439         }
1440         return result;
1441     }
1442
1443     /**
1444      * Returns list of ComponentInstanceProperty belonging to component instance capability specified by name, type and ownerId
1445      *
1446      * @param componentId
1447      * @param instanceId
1448      * @param capabilityName
1449      * @param capabilityType
1450      * @param ownerId
1451      * @return
1452      */
1453     public Either<List<ComponentInstanceProperty>, StorageOperationStatus> getComponentInstanceCapabilityProperties(String componentId,
1454                                                                                                                     String instanceId,
1455                                                                                                                     String capabilityName,
1456                                                                                                                     String capabilityType,
1457                                                                                                                     String ownerId) {
1458         Either<List<ComponentInstanceProperty>, StorageOperationStatus> result = null;
1459         Map<String, MapCapabilityProperty> mapPropertiesDataDefinition = null;
1460         Either<GraphVertex, StorageOperationStatus> componentByLabelAndId = getComponentByLabelAndId(componentId,
1461             ToscaElementTypeEnum.TOPOLOGY_TEMPLATE, JsonParseFlagEnum.NoParse);
1462         if (componentByLabelAndId.isRight()) {
1463             result = Either.right(componentByLabelAndId.right().value());
1464         }
1465         if (componentByLabelAndId.isLeft()) {
1466             Either<Map<String, MapCapabilityProperty>, JanusGraphOperationStatus> getDataRes = getDataFromGraph(componentByLabelAndId.left().value(),
1467                 EdgeLabelEnum.CALCULATED_CAP_PROPERTIES);
1468             if (getDataRes.isRight()) {
1469                 result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getDataRes.right().value()));
1470             } else {
1471                 mapPropertiesDataDefinition = getDataRes.left().value();
1472             }
1473         }
1474         if (isNotEmptyMapOfProperties(instanceId, mapPropertiesDataDefinition)) {
1475             result = Either.left(findComponentInstanceCapabilityProperties(instanceId, capabilityName, capabilityType, ownerId,
1476                 mapPropertiesDataDefinition.get(instanceId).getMapToscaDataDefinition()));
1477         }
1478         return result;
1479     }
1480
1481     public StorageOperationStatus updateComponentInstanceCapabilityProperties(Component containerComponent, String componentInstanceId,
1482                                                                               MapCapabilityProperty instanceProperties) {
1483         return updateToscaDataDeepElementsBlockToToscaElement(containerComponent.getUniqueId(), EdgeLabelEnum.CALCULATED_CAP_PROPERTIES,
1484             instanceProperties, componentInstanceId);
1485     }
1486
1487     public StorageOperationStatus updateComponentInstanceInterfaces(Component containerComponent, String componentInstanceId,
1488                                                                     MapInterfaceDataDefinition instanceInterfaces) {
1489         if (MapUtils.isNotEmpty(instanceInterfaces.getMapToscaDataDefinition())) {
1490             return updateToscaDataDeepElementsBlockToToscaElement(containerComponent.getUniqueId(), EdgeLabelEnum.INST_INTERFACES, instanceInterfaces,
1491                 componentInstanceId);
1492         }
1493         return StorageOperationStatus.OK;
1494     }
1495
1496     private boolean isNotEmptyMapOfProperties(String instanceId, Map<String, MapCapabilityProperty> mapPropertiesDataDefinition) {
1497         return MapUtils.isNotEmpty(mapPropertiesDataDefinition) && instanceId != null && mapPropertiesDataDefinition.get(instanceId) != null
1498             && MapUtils.isNotEmpty(mapPropertiesDataDefinition.get(instanceId).getMapToscaDataDefinition());
1499     }
1500
1501     private List<ComponentInstanceProperty> findComponentInstanceCapabilityProperties(String instanceId, String capabilityName, String capabilityType,
1502                                                                                       String ownerId,
1503                                                                                       Map<String, MapPropertiesDataDefinition> propertiesMap) {
1504         List<ComponentInstanceProperty> capPropsList = null;
1505         for (Entry<String, MapPropertiesDataDefinition> capProp : propertiesMap.entrySet()) {
1506             if (isBelongingPropertyMap(instanceId, capabilityName, capabilityType, ownerId, capProp)) {
1507                 Map<String, PropertyDataDefinition> capMap = capProp.getValue().getMapToscaDataDefinition();
1508                 if (capMap != null && !capMap.isEmpty()) {
1509                     capPropsList = capMap.values().stream().map(ComponentInstanceProperty::new).collect(Collectors.toList());
1510                     break;
1511                 }
1512             }
1513         }
1514         if (capPropsList == null) {
1515             capPropsList = new ArrayList<>();
1516         }
1517         return capPropsList;
1518     }
1519
1520     private boolean isBelongingPropertyMap(String instanceId, String capabilityName, String capabilityType, String ownerId,
1521                                            Entry<String, MapPropertiesDataDefinition> capProp) {
1522         if (capProp != null) {
1523             String[] path = capProp.getKey().split(ModelConverter.CAP_PROP_DELIM);
1524             if (path.length < 4) {
1525                 log.debug("wrong key format for capabilty, key {}", capProp);
1526                 return false;
1527             }
1528             return path[path.length - 2].equals(capabilityType) && path[path.length - 1].equals(capabilityName) && path[1].equals(ownerId) && path[0]
1529                 .equals(instanceId);
1530         }
1531         return false;
1532     }
1533
1534     public StorageOperationStatus addPolicyToToscaElement(GraphVertex componentV, PolicyDefinition policyDefinition, int counter) {
1535         fillPolicyDefinition(componentV, policyDefinition, counter);
1536         return addToscaDataToToscaElement(componentV, EdgeLabelEnum.POLICIES, VertexTypeEnum.POLICIES, policyDefinition,
1537             JsonPresentationFields.UNIQUE_ID);
1538     }
1539
1540     public StorageOperationStatus addPoliciesToToscaElement(GraphVertex componentV, List<PolicyDefinition> policies) {
1541         return addToscaDataToToscaElement(componentV, EdgeLabelEnum.POLICIES, VertexTypeEnum.POLICIES, policies, JsonPresentationFields.UNIQUE_ID);
1542     }
1543
1544     public StorageOperationStatus updatePolicyOfToscaElement(GraphVertex componentV, PolicyDefinition policyDefinition) {
1545         return updateToscaDataOfToscaElement(componentV, EdgeLabelEnum.POLICIES, VertexTypeEnum.POLICIES, policyDefinition,
1546             JsonPresentationFields.UNIQUE_ID);
1547     }
1548
1549     public StorageOperationStatus updatePoliciesOfToscaElement(GraphVertex componentV, List<PolicyDefinition> policiesDefinitions) {
1550         return updateToscaDataOfToscaElement(componentV, EdgeLabelEnum.POLICIES, VertexTypeEnum.POLICIES, policiesDefinitions,
1551             JsonPresentationFields.UNIQUE_ID);
1552     }
1553
1554     public StorageOperationStatus removePolicyFromToscaElement(GraphVertex componentV, String policyId) {
1555         return deleteToscaDataElement(componentV, EdgeLabelEnum.POLICIES, VertexTypeEnum.POLICIES, policyId, JsonPresentationFields.UNIQUE_ID);
1556     }
1557
1558     public StorageOperationStatus updateGroupOfToscaElement(GraphVertex componentV, GroupDefinition groupDefinition) {
1559         return updateToscaDataOfToscaElement(componentV, EdgeLabelEnum.GROUPS, VertexTypeEnum.GROUPS, groupDefinition,
1560             JsonPresentationFields.CI_INVARIANT_NAME);
1561     }
1562
1563     private void fillPolicyDefinition(GraphVertex componentV, PolicyDefinition policyDefinition, int counter) {
1564         String policyName = buildSubComponentName((String) componentV.getJsonMetadataField(JsonPresentationFields.NAME),
1565             policyDefinition.getPolicyTypeName(), counter);
1566         policyDefinition.setName(policyName);
1567         policyDefinition.setInvariantName(policyName);
1568         policyDefinition.setComponentName((String) componentV.getJsonMetadataField(JsonPresentationFields.NAME));
1569         policyDefinition.setUniqueId(UniqueIdBuilder.buildPolicyUniqueId(componentV.getUniqueId(), policyName));
1570         policyDefinition.setInvariantUUID(UniqueIdBuilder.buildInvariantUUID());
1571         policyDefinition.setPolicyUUID(UniqueIdBuilder.generateUUID());
1572     }
1573
1574     void revertNamesOfCalculatedCapabilitiesRequirements(String componentId, TopologyTemplate toscaElement) {
1575         if (MapUtils.isNotEmpty(toscaElement.getComponentInstances()) || MapUtils.isNotEmpty(toscaElement.getGroups())) {
1576             GraphVertex toscaElementV = janusGraphDao.getVertexById(componentId, JsonParseFlagEnum.NoParse).left().on(this::throwStorageException);
1577             if (MapUtils.isNotEmpty(toscaElement.getComponentInstances())) {
1578                 toscaElement.getComponentInstances().values().forEach(i -> CapabilityRequirementNameResolver
1579                     .revertNamesOfCalculatedCapabilitiesRequirements(toscaElement, i.getUniqueId(), this::getOriginToscaElement));
1580             }
1581             if (MapUtils.isNotEmpty(toscaElement.getGroups())) {
1582                 toscaElement.getGroups().values().forEach(g -> CapabilityRequirementNameResolver
1583                     .revertNamesOfCalculatedCapabilitiesRequirements(toscaElement, g.getUniqueId(), this::getOriginToscaElement));
1584             }
1585             topologyTemplateOperation
1586                 .updateFullToscaData(toscaElementV, EdgeLabelEnum.CALCULATED_CAPABILITIES, VertexTypeEnum.CALCULATED_CAPABILITIES,
1587                     toscaElement.getCalculatedCapabilities());
1588             topologyTemplateOperation
1589                 .updateFullToscaData(toscaElementV, EdgeLabelEnum.CALCULATED_REQUIREMENTS, VertexTypeEnum.CALCULATED_REQUIREMENTS,
1590                     toscaElement.getCalculatedRequirements());
1591             topologyTemplateOperation
1592                 .updateFullToscaData(toscaElementV, EdgeLabelEnum.CALCULATED_CAP_PROPERTIES, VertexTypeEnum.CALCULATED_CAP_PROPERTIES,
1593                     toscaElement.getCalculatedCapabilitiesProperties());
1594         }
1595     }
1596
1597     public void updateNamesOfCalculatedCapabilitiesRequirements(String componentId, TopologyTemplate toscaElement) {
1598         if (MapUtils.isNotEmpty(toscaElement.getComponentInstances()) || MapUtils.isNotEmpty(toscaElement.getGroups())) {
1599             GraphVertex toscaElementV = janusGraphDao.getVertexById(componentId, JsonParseFlagEnum.NoParse).left().on(this::throwStorageException);
1600             if (MapUtils.isNotEmpty(toscaElement.getComponentInstances())) {
1601                 toscaElement.getComponentInstances().values().forEach(i -> CapabilityRequirementNameResolver
1602                     .updateNamesOfCalculatedCapabilitiesRequirements(toscaElement, i.getUniqueId(), i.getNormalizedName(),
1603                         this::getOriginToscaElement));
1604             }
1605             if (MapUtils.isNotEmpty(toscaElement.getGroups())) {
1606                 toscaElement.getGroups().values().forEach(g -> CapabilityRequirementNameResolver
1607                     .updateNamesOfCalculatedCapabilitiesRequirements(toscaElement, g.getUniqueId(), g.getName(), this::getOriginToscaElement));
1608             }
1609             topologyTemplateOperation
1610                 .updateFullToscaData(toscaElementV, EdgeLabelEnum.CALCULATED_CAPABILITIES, VertexTypeEnum.CALCULATED_CAPABILITIES,
1611                     toscaElement.getCalculatedCapabilities());
1612             topologyTemplateOperation
1613                 .updateFullToscaData(toscaElementV, EdgeLabelEnum.CALCULATED_REQUIREMENTS, VertexTypeEnum.CALCULATED_REQUIREMENTS,
1614                     toscaElement.getCalculatedRequirements());
1615             topologyTemplateOperation
1616                 .updateFullToscaData(toscaElementV, EdgeLabelEnum.CALCULATED_CAP_PROPERTIES, VertexTypeEnum.CALCULATED_CAP_PROPERTIES,
1617                     toscaElement.getCalculatedCapabilitiesProperties());
1618         }
1619     }
1620
1621     private ToscaElement getOriginToscaElement(ComponentInstanceDataDefinition instance) {
1622         log.debug("#getOriginToscaElement - origin name: {}", instance.getComponentName());
1623         ToscaElementTypeEnum elementType = detectToscaType(instance.getOriginType());
1624         Either<ToscaElement, StorageOperationStatus> getOriginRes;
1625         if (elementType == ToscaElementTypeEnum.TOPOLOGY_TEMPLATE) {
1626             getOriginRes = this.getToscaElement(CapabilityRequirementNameResolver.getActualComponentUid(instance), getFilter());
1627         } else {
1628             getOriginRes = nodeTypeOperation.getToscaElement(CapabilityRequirementNameResolver.getActualComponentUid(instance), getFilter());
1629         }
1630         if (getOriginRes.isRight()) {
1631             log.debug("Failed to get an origin component with uniqueId {}", CapabilityRequirementNameResolver.getActualComponentUid(instance));
1632             throw new StorageException(getOriginRes.right().value());
1633         }
1634         return getOriginRes.left().value();
1635     }
1636
1637     private ToscaElementTypeEnum detectToscaType(OriginTypeEnum originType) {
1638         log.debug("#detectToscaType - type: {}", originType);
1639         if (originType == OriginTypeEnum.VFC || originType == OriginTypeEnum.CP || originType == OriginTypeEnum.VL
1640             || originType == OriginTypeEnum.Configuration || originType == OriginTypeEnum.VFCMT) {
1641             return ToscaElementTypeEnum.NODE_TYPE;
1642         } else {
1643             return ToscaElementTypeEnum.TOPOLOGY_TEMPLATE;
1644         }
1645     }
1646
1647     private ComponentParametersView getFilter() {
1648         ComponentParametersView filter = new ComponentParametersView();
1649         filter.setIgnoreCapabilities(false);
1650         filter.setIgnoreCapabiltyProperties(false);
1651         filter.setIgnoreRequirements(false);
1652         return filter;
1653     }
1654
1655     public void updateCapReqPropertiesOwnerId(String componentId, TopologyTemplate toscaElement) {
1656         GraphVertex toscaElementV = janusGraphDao.getVertexById(componentId, JsonParseFlagEnum.NoParse).left().on(this::throwStorageException);
1657         updateCapOwnerId(toscaElement, componentId);
1658         updateReqOwnerId(toscaElement, componentId);
1659         updatePropertiesOwnerId(toscaElement, componentId);
1660         topologyTemplateOperation
1661             .updateFullToscaData(toscaElementV, EdgeLabelEnum.CAPABILITIES, VertexTypeEnum.CAPABILITIES, toscaElement.getCapabilities());
1662         topologyTemplateOperation
1663             .updateFullToscaData(toscaElementV, EdgeLabelEnum.REQUIREMENTS, VertexTypeEnum.REQUIREMENTS, toscaElement.getRequirements());
1664         topologyTemplateOperation
1665             .updateFullToscaData(toscaElementV, EdgeLabelEnum.PROPERTIES, VertexTypeEnum.PROPERTIES, toscaElement.getProperties());
1666     }
1667
1668     private void updateCapOwnerId(ToscaElement toscaElement, String ownerId) {
1669         if (MapUtils.isNotEmpty(toscaElement.getCapabilities())) {
1670             toscaElement.getCapabilities().values().stream().flatMap(listCapDef -> listCapDef.getListToscaDataDefinition().stream())
1671                 .forEach(capabilityDefinition -> capabilityDefinition.setOwnerId(ownerId));
1672         }
1673     }
1674
1675     private void updateReqOwnerId(ToscaElement toscaElement, String ownerId) {
1676         if (MapUtils.isNotEmpty(toscaElement.getRequirements())) {
1677             toscaElement.getRequirements().values().stream().flatMap(listReqDef -> listReqDef.getListToscaDataDefinition().stream())
1678                 .forEach(requirementDefinition -> requirementDefinition.setOwnerId(ownerId));
1679         }
1680     }
1681
1682     private void updatePropertiesOwnerId(ToscaElement toscaElement, String ownerId) {
1683         Map<String, PropertyDataDefinition> properties = toscaElement.getProperties();
1684         if (MapUtils.isNotEmpty(properties)) {
1685             properties.values().forEach(propertyDataDefinition -> propertyDataDefinition.setParentUniqueId(ownerId));
1686         }
1687     }
1688 }