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