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