new unit tests for sdc-dao
[sdc.git] / catalog-model / src / main / java / org / openecomp / sdc / be / model / jsontitan / 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.jsontitan.operations;
22
23 import java.lang.reflect.Type;
24 import java.util.ArrayList;
25 import java.util.HashMap;
26 import java.util.Iterator;
27 import java.util.List;
28 import java.util.Map;
29 import java.util.Map.Entry;
30 import java.util.stream.Collectors;
31
32 import org.apache.commons.collections.MapUtils;
33 import org.apache.tinkerpop.gremlin.structure.Direction;
34 import org.apache.tinkerpop.gremlin.structure.Edge;
35 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
36 import org.openecomp.sdc.be.dao.jsongraph.types.EdgeLabelEnum;
37 import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum;
38 import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum;
39 import org.openecomp.sdc.be.dao.titan.TitanOperationStatus;
40 import org.openecomp.sdc.be.datatypes.elements.AdditionalInfoParameterDataDefinition;
41 import org.openecomp.sdc.be.datatypes.elements.ArtifactDataDefinition;
42 import org.openecomp.sdc.be.datatypes.elements.ComponentInstanceDataDefinition;
43 import org.openecomp.sdc.be.datatypes.elements.CompositionDataDefinition;
44 import org.openecomp.sdc.be.datatypes.elements.ForwardingPathDataDefinition;
45 import org.openecomp.sdc.be.datatypes.elements.GroupDataDefinition;
46 import org.openecomp.sdc.be.datatypes.elements.InterfaceDataDefinition;
47 import org.openecomp.sdc.be.datatypes.elements.MapArtifactDataDefinition;
48 import org.openecomp.sdc.be.datatypes.elements.MapCapabiltyProperty;
49 import org.openecomp.sdc.be.datatypes.elements.MapGroupsDataDefinition;
50 import org.openecomp.sdc.be.datatypes.elements.MapListCapabiltyDataDefinition;
51 import org.openecomp.sdc.be.datatypes.elements.MapListRequirementDataDefinition;
52 import org.openecomp.sdc.be.datatypes.elements.MapPropertiesDataDefinition;
53 import org.openecomp.sdc.be.datatypes.elements.PolicyDataDefinition;
54 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
55 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
56 import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum;
57 import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
58 import org.openecomp.sdc.be.model.Component;
59 import org.openecomp.sdc.be.model.ComponentInstanceProperty;
60 import org.openecomp.sdc.be.model.ComponentParametersView;
61 import org.openecomp.sdc.be.model.DistributionStatusEnum;
62 import org.openecomp.sdc.be.model.PolicyDefinition;
63 import org.openecomp.sdc.be.model.User;
64 import org.openecomp.sdc.be.model.category.CategoryDefinition;
65 import org.openecomp.sdc.be.model.jsontitan.datamodel.TopologyTemplate;
66 import org.openecomp.sdc.be.model.jsontitan.datamodel.ToscaElement;
67 import org.openecomp.sdc.be.model.jsontitan.datamodel.ToscaElementTypeEnum;
68 import org.openecomp.sdc.be.model.jsontitan.enums.JsonConstantKeysEnum;
69 import org.openecomp.sdc.be.model.jsontitan.utils.ModelConverter;
70 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
71 import org.openecomp.sdc.be.model.operations.impl.DaoStatusConverter;
72 import org.openecomp.sdc.be.model.operations.impl.UniqueIdBuilder;
73 import org.openecomp.sdc.common.api.Constants;
74 import org.openecomp.sdc.common.jsongraph.util.CommonUtility;
75 import org.openecomp.sdc.common.jsongraph.util.CommonUtility.LogLevelEnum;
76 import org.slf4j.Logger;
77 import org.slf4j.LoggerFactory;
78
79 import com.google.gson.reflect.TypeToken;
80
81 import fj.data.Either;
82
83 @org.springframework.stereotype.Component("topology-template-operation")
84 public class TopologyTemplateOperation extends ToscaElementOperation {
85
86         private static Logger log = LoggerFactory.getLogger(TopologyTemplateOperation.class.getName());
87
88         @Override
89         public Either<ToscaElement, StorageOperationStatus> getToscaElement(String uniqueId) {
90                 return super.getToscaElement(uniqueId);
91         }
92
93         public Either<TopologyTemplate, StorageOperationStatus> createTopologyTemplate(TopologyTemplate topologyTemplate) {
94                 Either<TopologyTemplate, StorageOperationStatus> result = null;
95
96                 topologyTemplate.generateUUID();
97
98                 topologyTemplate = (TopologyTemplate) getResourceMetaDataFromResource(topologyTemplate);
99                 String resourceUniqueId = topologyTemplate.getUniqueId();
100                 if (resourceUniqueId == null) {
101                         resourceUniqueId = UniqueIdBuilder.buildResourceUniqueId();
102                         topologyTemplate.setUniqueId(resourceUniqueId);
103                 }
104
105                 GraphVertex topologyTemplateVertex = new GraphVertex();
106                 topologyTemplateVertex = fillMetadata(topologyTemplateVertex, topologyTemplate, JsonParseFlagEnum.ParseAll);
107
108                 Either<GraphVertex, TitanOperationStatus> createdVertex = titanDao.createVertex(topologyTemplateVertex);
109                 if (createdVertex.isRight()) {
110                         TitanOperationStatus status = createdVertex.right().value();
111                         log.error("Error returned after creating topology template data node {}. status returned is ", topologyTemplateVertex, status);
112                         result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
113                         return result;
114                 }
115
116                 topologyTemplateVertex = createdVertex.left().value();
117
118                 StorageOperationStatus assosiateCommon = assosiateCommonForToscaElement(topologyTemplateVertex, topologyTemplate, null);
119                 if (assosiateCommon != StorageOperationStatus.OK) {
120                         result = Either.right(assosiateCommon);
121                         return result;
122                 }
123
124                 StorageOperationStatus associateCategory = assosiateMetadataToCategory(topologyTemplateVertex, topologyTemplate);
125                 if (associateCategory != StorageOperationStatus.OK) {
126                         result = Either.right(associateCategory);
127                         return result;
128                 }
129
130                 StorageOperationStatus associateInputs = associateInputsToComponent(topologyTemplateVertex, topologyTemplate);
131                 if (associateInputs != StorageOperationStatus.OK) {
132                         result = Either.right(associateInputs);
133                         return result;
134                 }
135                 StorageOperationStatus associateGroups = associateGroupsToComponent(topologyTemplateVertex, topologyTemplate);
136                 if (associateGroups != StorageOperationStatus.OK) {
137                         result = Either.right(associateGroups);
138                         return result;
139                 }
140                 StorageOperationStatus associatePolicies = associatePoliciesToComponent(topologyTemplateVertex, topologyTemplate);
141                 if (associatePolicies != StorageOperationStatus.OK) {
142                         result = Either.right(associatePolicies);
143                         return result;
144                 }
145                 StorageOperationStatus associateInstAttr = associateInstAttributesToComponent(topologyTemplateVertex, topologyTemplate);
146                 if (associateInstAttr != StorageOperationStatus.OK) {
147                         result = Either.right(associateInstAttr);
148                         return result;
149                 }
150                 StorageOperationStatus associateInstProperties = associateInstPropertiesToComponent(topologyTemplateVertex, topologyTemplate);
151                 if (associateInstProperties != StorageOperationStatus.OK) {
152                         result = Either.right(associateInstProperties);
153                         return result;
154                 }
155                 StorageOperationStatus associateInstInputs = associateInstInputsToComponent(topologyTemplateVertex, topologyTemplate);
156                 if (associateInstProperties != StorageOperationStatus.OK) {
157                         result = Either.right(associateInstInputs);
158                         return result;
159                 }
160                 StorageOperationStatus associateInstGroups = associateInstGroupsToComponent(topologyTemplateVertex, topologyTemplate);
161                 if (associateInstGroups != StorageOperationStatus.OK) {
162                         result = Either.right(associateInstInputs);
163                         return result;
164                 }
165                 
166                 StorageOperationStatus associateRequirements = associateRequirementsToResource(topologyTemplateVertex, topologyTemplate);
167                 if (associateRequirements != StorageOperationStatus.OK) {
168                         result = Either.right(associateRequirements);
169                         return result;
170                 }
171
172                 StorageOperationStatus associateCapabilities = associateCapabilitiesToResource(topologyTemplateVertex, topologyTemplate);
173                 if (associateCapabilities != StorageOperationStatus.OK) {
174                         result = Either.right(associateCapabilities);
175                         return result;
176                 }
177
178                 StorageOperationStatus associateArtifacts = associateTopologyTemplateArtifactsToComponent(topologyTemplateVertex, topologyTemplate);
179                 if (associateArtifacts != StorageOperationStatus.OK) {
180                         result = Either.right(associateArtifacts);
181                         return result;
182                 }
183
184                 StorageOperationStatus addAdditionalInformation = addAdditionalInformationToResource(topologyTemplateVertex, topologyTemplate);
185                 if (addAdditionalInformation != StorageOperationStatus.OK) {
186                         result = Either.right(addAdditionalInformation);
187                         return result;
188                 }
189                 StorageOperationStatus associateCapProperties = associateCapPropertiesToResource(topologyTemplateVertex, topologyTemplate);
190                 if (associateCapProperties != StorageOperationStatus.OK) {
191                         result = Either.right(associateCapProperties);
192                         return result;
193                 }
194
195                 StorageOperationStatus associateInterfaces = associateInterfacesToResource(topologyTemplateVertex, topologyTemplate);
196                 if (associateInterfaces != StorageOperationStatus.OK) {
197                         result = Either.right(associateInterfaces);
198                         return result;
199                 }
200
201                 StorageOperationStatus associatePathProperties = associateForwardingPathToResource(topologyTemplateVertex, topologyTemplate);
202                 if (associateCapProperties != StorageOperationStatus.OK) {
203                         result = Either.right(associatePathProperties);
204                         return result;
205                 }
206
207
208                 return Either.left(topologyTemplate);
209
210         }
211
212         private StorageOperationStatus associatePoliciesToComponent(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
213                 return associatePoliciesToComponent(nodeTypeVertex, topologyTemplate.getPolicies());
214         }
215
216         private StorageOperationStatus associatePoliciesToComponent(GraphVertex nodeTypeVertex, Map<String, PolicyDataDefinition> policies) {
217                 if (policies != null && !policies.isEmpty()) {
218                         policies.values().stream().filter(p -> p.getUniqueId() == null).forEach(p -> {
219                                 String uid = UniqueIdBuilder.buildGroupingUid(nodeTypeVertex.getUniqueId(), p.getName());
220                                 p.setUniqueId(uid);
221                         });
222                         Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.POLICIES, EdgeLabelEnum.POLICIES, policies);
223                         if (assosiateElementToData.isRight()) {
224                                 return assosiateElementToData.right().value();
225                         }
226                 }
227                 return StorageOperationStatus.OK;
228         }
229
230         private StorageOperationStatus associateForwardingPathToResource(GraphVertex topologyTemplateVertex, TopologyTemplate topologyTemplate) {
231                 Map<String, ForwardingPathDataDefinition> forwardingPaths = topologyTemplate.getForwardingPaths();
232                 return associateForwardingPathToComponent(topologyTemplateVertex,forwardingPaths);
233         }
234
235         private StorageOperationStatus associateCapPropertiesToResource(GraphVertex topologyTemplateVertex, TopologyTemplate topologyTemplate) {
236                 Map<String, MapCapabiltyProperty> calculatedCapProperties = topologyTemplate.getCalculatedCapabilitiesProperties();
237                 if (calculatedCapProperties != null && !calculatedCapProperties.isEmpty()) {
238                         Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(topologyTemplateVertex, VertexTypeEnum.CALCULATED_CAP_PROPERTIES, EdgeLabelEnum.CALCULATED_CAP_PROPERTIES, calculatedCapProperties);
239                         if (assosiateElementToData.isRight()) {
240                                 return assosiateElementToData.right().value();
241                         }
242                 }
243                 return StorageOperationStatus.OK;
244         }
245
246         private StorageOperationStatus associateCapabilitiesToResource(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
247                 Map<String, MapListCapabiltyDataDefinition> calculatedCapabilities = topologyTemplate.getCalculatedCapabilities();
248                 if (calculatedCapabilities != null && !calculatedCapabilities.isEmpty()) {
249                         Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.CALCULATED_CAPABILITIES, EdgeLabelEnum.CALCULATED_CAPABILITIES, calculatedCapabilities);
250                         if (assosiateElementToData.isRight()) {
251                                 return assosiateElementToData.right().value();
252                         }
253                 }
254                 Map<String, MapListCapabiltyDataDefinition> fullfilledCapabilities = topologyTemplate.getFullfilledCapabilities();
255                 if (fullfilledCapabilities != null && !fullfilledCapabilities.isEmpty()) {
256                         Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.FULLFILLED_CAPABILITIES, EdgeLabelEnum.FULLFILLED_CAPABILITIES, fullfilledCapabilities);
257                         if (assosiateElementToData.isRight()) {
258                                 return assosiateElementToData.right().value();
259                         }
260                 }
261                 return StorageOperationStatus.OK;
262
263         }
264
265         private StorageOperationStatus associateRequirementsToResource(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
266                 Map<String, MapListRequirementDataDefinition> calculatedRequirements = topologyTemplate.getCalculatedRequirements();
267                 if (calculatedRequirements != null && !calculatedRequirements.isEmpty()) {
268                         Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.CALCULATED_REQUIREMENTS, EdgeLabelEnum.CALCULATED_REQUIREMENTS, calculatedRequirements);
269                         if (assosiateElementToData.isRight()) {
270                                 return assosiateElementToData.right().value();
271                         }
272                 }
273                 Map<String, MapListRequirementDataDefinition> fullfilledRequirements = topologyTemplate.getFullfilledRequirements();
274                 if (fullfilledRequirements != null && !fullfilledRequirements.isEmpty()) {
275                         Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.FULLFILLED_REQUIREMENTS, EdgeLabelEnum.FULLFILLED_REQUIREMENTS, fullfilledRequirements);
276                         if (assosiateElementToData.isRight()) {
277                                 return assosiateElementToData.right().value();
278                         }
279                 }
280                 return StorageOperationStatus.OK;
281         }
282
283         private StorageOperationStatus associateTopologyTemplateArtifactsToComponent(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
284                 Map<String, ArtifactDataDefinition> addInformation = topologyTemplate.getServiceApiArtifacts();
285
286                 if (addInformation != null && !addInformation.isEmpty()) {
287                         addInformation.values().stream().filter(a -> a.getUniqueId() == null).forEach(a -> {
288                                 String uniqueId = UniqueIdBuilder.buildPropertyUniqueId(nodeTypeVertex.getUniqueId().toLowerCase(), a.getArtifactLabel().toLowerCase());
289                                 a.setUniqueId(uniqueId);
290                         });
291                         Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.SERVICE_API_ARTIFACTS, EdgeLabelEnum.SERVICE_API_ARTIFACTS, addInformation);
292                         if (assosiateElementToData.isRight()) {
293                                 return assosiateElementToData.right().value();
294                         }
295                 }
296                 Map<String, MapArtifactDataDefinition> instArtifacts = topologyTemplate.getInstDeploymentArtifacts();
297
298                 if (instArtifacts != null && !instArtifacts.isEmpty()) {
299                         Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.INST_DEPLOYMENT_ARTIFACTS, EdgeLabelEnum.INST_DEPLOYMENT_ARTIFACTS, instArtifacts);
300                         if (assosiateElementToData.isRight()) {
301                                 return assosiateElementToData.right().value();
302                         }
303                 }
304                 Map<String, MapArtifactDataDefinition> instInfoArtifacts = topologyTemplate.getInstanceArtifacts();
305
306                 if (instInfoArtifacts != null && !instInfoArtifacts.isEmpty()) {
307                         Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.INSTANCE_ARTIFACTS, EdgeLabelEnum.INSTANCE_ARTIFACTS, instInfoArtifacts);
308                         if (assosiateElementToData.isRight()) {
309                                 return assosiateElementToData.right().value();
310                         }
311                 }
312                 return StorageOperationStatus.OK;
313         }
314
315         private StorageOperationStatus addAdditionalInformationToResource(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
316
317                 Map<String, AdditionalInfoParameterDataDefinition> addInformation = topologyTemplate.getAdditionalInformation();
318
319                 if (addInformation != null && !addInformation.isEmpty()) {
320                         Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.ADDITIONAL_INFORMATION, EdgeLabelEnum.ADDITIONAL_INFORMATION, addInformation);
321                         if (assosiateElementToData.isRight()) {
322                                 return assosiateElementToData.right().value();
323                         }
324                 }
325                 return StorageOperationStatus.OK;
326         }
327
328         public StorageOperationStatus associateInstPropertiesToComponent(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
329                 Map<String, MapPropertiesDataDefinition> instProps = topologyTemplate.getInstProperties();
330                 return associateInstPropertiesToComponent(nodeTypeVertex, instProps);
331         }
332
333         public StorageOperationStatus associateInstInputsToComponent(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
334                 Map<String, MapPropertiesDataDefinition> instProps = topologyTemplate.getInstInputs();
335                 return associateInstInputsToComponent(nodeTypeVertex, instProps);
336         }
337         
338         public StorageOperationStatus associateInstGroupsToComponent(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
339                 Map<String, MapGroupsDataDefinition> instGroups = topologyTemplate.getInstGroups();
340                 return associateInstGroupsToComponent(nodeTypeVertex, instGroups);
341         }
342         
343
344         public StorageOperationStatus associateInstPropertiesToComponent(GraphVertex nodeTypeVertex, Map<String, MapPropertiesDataDefinition> instProps) {
345                 if (instProps != null && !instProps.isEmpty()) {
346                         Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.INST_PROPERTIES, EdgeLabelEnum.INST_PROPERTIES, instProps);
347                         if (assosiateElementToData.isRight()) {
348                                 return assosiateElementToData.right().value();
349                         }
350                 }
351                 return StorageOperationStatus.OK;
352         }
353
354         public StorageOperationStatus associateInstInputsToComponent(GraphVertex nodeTypeVertex, Map<String, MapPropertiesDataDefinition> instInputs) {
355                 if (instInputs != null && !instInputs.isEmpty()) {
356                         Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.INST_INPUTS, EdgeLabelEnum.INST_INPUTS, instInputs);
357                         if (assosiateElementToData.isRight()) {
358                                 return assosiateElementToData.right().value();
359                         }
360                 }
361                 return StorageOperationStatus.OK;
362         }
363         
364         public StorageOperationStatus associateInstGroupsToComponent(GraphVertex nodeTypeVertex, Map<String, MapGroupsDataDefinition> instGroups) {
365                 if (instGroups != null && !instGroups.isEmpty()) {
366                         Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.INST_GROUPS, EdgeLabelEnum.INST_GROUPS, instGroups);
367                         if (assosiateElementToData.isRight()) {
368                                 return assosiateElementToData.right().value();
369                         }
370                 }
371                 return StorageOperationStatus.OK;
372         }
373
374
375         public StorageOperationStatus deleteInstInputsToComponent(GraphVertex nodeTypeVertex, Map<String, MapPropertiesDataDefinition> instInputs) {
376
377                 if (instInputs != null && !instInputs.isEmpty()) {
378                         instInputs.entrySet().forEach(i -> {
379                                 List<String> uniqueKeys = new ArrayList<String>(i.getValue().getMapToscaDataDefinition().keySet());
380                                 List<String> pathKeys = new ArrayList<String>();
381                                 pathKeys.add(i.getKey());
382
383                                 StorageOperationStatus status = deleteToscaDataDeepElements(nodeTypeVertex, EdgeLabelEnum.INST_INPUTS, VertexTypeEnum.INST_INPUTS, uniqueKeys, pathKeys, JsonPresentationFields.NAME);
384                                 if (status != StorageOperationStatus.OK) {
385                                         return;
386                                 }
387                         });
388                 }
389
390                 return StorageOperationStatus.OK;
391         }
392
393         public StorageOperationStatus addInstPropertiesToComponent(GraphVertex nodeTypeVertex, Map<String, MapPropertiesDataDefinition> instInputs) {
394
395                 if (instInputs != null && !instInputs.isEmpty()) {
396                         instInputs.entrySet().forEach(i -> {
397                                 StorageOperationStatus status = addToscaDataDeepElementsBlockToToscaElement(nodeTypeVertex, EdgeLabelEnum.INST_PROPERTIES, VertexTypeEnum.INST_PROPERTIES, i.getValue(), i.getKey());
398                                 if (status != StorageOperationStatus.OK) {
399                                         return;
400                                 }
401                         });
402                 }
403
404                 return StorageOperationStatus.OK;
405         }
406
407         public StorageOperationStatus associateInstDeploymentArtifactsToComponent(GraphVertex nodeTypeVertex, Map<String, MapArtifactDataDefinition> instArtifacts) {
408                 return associateInstanceArtifactsToComponent(nodeTypeVertex, instArtifacts, VertexTypeEnum.INST_DEPLOYMENT_ARTIFACTS, EdgeLabelEnum.INST_DEPLOYMENT_ARTIFACTS);
409         }
410         
411         public StorageOperationStatus associateInstArtifactsToComponent(GraphVertex nodeTypeVertex, Map<String, MapArtifactDataDefinition> instArtifacts) {
412                 return associateInstanceArtifactsToComponent(nodeTypeVertex, instArtifacts, VertexTypeEnum.INSTANCE_ARTIFACTS, EdgeLabelEnum.INSTANCE_ARTIFACTS);
413         }
414
415         private StorageOperationStatus associateInstanceArtifactsToComponent(GraphVertex nodeTypeVertex, Map<String, MapArtifactDataDefinition> instProps, VertexTypeEnum vertexType, EdgeLabelEnum edgeLabel) {
416                 if (instProps != null && !instProps.isEmpty()) {
417                         Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, vertexType, edgeLabel, instProps);
418                         if (assosiateElementToData.isRight()) {
419                                 return assosiateElementToData.right().value();
420                         }
421                 }
422                 return StorageOperationStatus.OK;
423         }
424
425         public StorageOperationStatus associateCalcCapReqToComponent(GraphVertex nodeTypeVertex, Map<String, MapListRequirementDataDefinition> calcRequirements, Map<String, MapListCapabiltyDataDefinition> calcCapabilty, Map<String, MapCapabiltyProperty> calculatedCapabilitiesProperties) {
426                 if (calcRequirements != null && !calcRequirements.isEmpty()) {
427                         Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.CALCULATED_REQUIREMENTS, EdgeLabelEnum.CALCULATED_REQUIREMENTS, calcRequirements);
428                         if (assosiateElementToData.isRight()) {
429                                 return assosiateElementToData.right().value();
430                         }
431                         Map<String, MapListRequirementDataDefinition> fullFilled = new HashMap<>();
432                         assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.FULLFILLED_REQUIREMENTS, EdgeLabelEnum.FULLFILLED_REQUIREMENTS, fullFilled);
433                         if (assosiateElementToData.isRight()) {
434                                 return assosiateElementToData.right().value();
435                         }
436                 }
437                 if (calcCapabilty != null && !calcCapabilty.isEmpty()) {
438                         Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.CALCULATED_CAPABILITIES, EdgeLabelEnum.CALCULATED_CAPABILITIES, calcCapabilty);
439                         if (assosiateElementToData.isRight()) {
440                                 return assosiateElementToData.right().value();
441                         }
442                         Map<String, MapListCapabiltyDataDefinition> fullFilled = new HashMap<>();
443                         assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.FULLFILLED_CAPABILITIES, EdgeLabelEnum.FULLFILLED_CAPABILITIES, fullFilled);
444                         if (assosiateElementToData.isRight()) {
445                                 return assosiateElementToData.right().value();
446                         }
447                 }
448                 if ( calculatedCapabilitiesProperties != null && !calculatedCapabilitiesProperties.isEmpty() ){
449                         Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.CALCULATED_CAP_PROPERTIES, EdgeLabelEnum.CALCULATED_CAP_PROPERTIES, calculatedCapabilitiesProperties);
450                         if (assosiateElementToData.isRight()) {
451                                 return assosiateElementToData.right().value();
452                         }
453                 }
454                 return StorageOperationStatus.OK;
455         }
456
457         private StorageOperationStatus associateInstAttributesToComponent(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
458                 Map<String, MapPropertiesDataDefinition> instAttr = topologyTemplate.getInstAttributes();
459                 return associateInstAttributeToComponent(nodeTypeVertex, instAttr);
460         }
461
462         public StorageOperationStatus associateForwardingPathToComponent(GraphVertex nodeTypeVertex, Map<String, ForwardingPathDataDefinition> forwardingPathMap) {
463                 if (forwardingPathMap != null && !forwardingPathMap.isEmpty()) {
464                         Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.FORWARDING_PATH, EdgeLabelEnum.FORWARDING_PATH, forwardingPathMap);
465                         if (assosiateElementToData.isRight()) {
466                                 return assosiateElementToData.right().value();
467                         }
468                 }
469                 return StorageOperationStatus.OK;
470         }
471
472         public StorageOperationStatus associateInstAttributeToComponent(GraphVertex nodeTypeVertex, Map<String, MapPropertiesDataDefinition> instAttr) {
473                 if (instAttr != null && !instAttr.isEmpty()) {
474                         Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.INST_ATTRIBUTES, EdgeLabelEnum.INST_ATTRIBUTES, instAttr);
475                         if (assosiateElementToData.isRight()) {
476                                 return assosiateElementToData.right().value();
477                         }
478                 }
479                 return StorageOperationStatus.OK;
480         }
481
482         public StorageOperationStatus associateGroupsToComponent(GraphVertex nodeTypeVertex, Map<String, GroupDataDefinition> groups) {
483
484                 if (groups != null && !groups.isEmpty()) {
485                         groups.values().stream().filter(p -> p.getUniqueId() == null).forEach(p -> {
486                                 String uid = UniqueIdBuilder.buildGroupingUid(nodeTypeVertex.getUniqueId(), p.getName());
487                                 p.setUniqueId(uid);
488                         });
489                         Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.GROUPS, EdgeLabelEnum.GROUPS, groups);
490                         if (assosiateElementToData.isRight()) {
491                                 return assosiateElementToData.right().value();
492                         }
493                 }
494                 return StorageOperationStatus.OK;
495         }
496
497         private StorageOperationStatus associateGroupsToComponent(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
498                 return associateGroupsToComponent(nodeTypeVertex, topologyTemplate.getGroups());
499         }
500
501         public StorageOperationStatus associateInputsToComponent(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
502                 Map<String, PropertyDataDefinition> inputs = topologyTemplate.getInputs();
503                 return associateInputsToComponent(nodeTypeVertex, inputs, topologyTemplate.getUniqueId());
504         }
505
506         public StorageOperationStatus associateInputsToComponent(GraphVertex nodeTypeVertex, Map<String, PropertyDataDefinition> inputs, String id) {
507                 if (inputs != null && !inputs.isEmpty()) {
508                         inputs.values().stream().filter(e -> e.getUniqueId() == null).forEach(e -> e.setUniqueId(UniqueIdBuilder.buildPropertyUniqueId(id, e.getName())));
509
510                         Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.INPUTS, EdgeLabelEnum.INPUTS, inputs);
511                         if (assosiateElementToData.isRight()) {
512                                 return assosiateElementToData.right().value();
513                         }
514                 }
515                 return StorageOperationStatus.OK;
516         }
517
518         private GraphVertex fillMetadata(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate, JsonParseFlagEnum flag) {
519                 nodeTypeVertex.setLabel(VertexTypeEnum.TOPOLOGY_TEMPLATE);
520                 fillCommonMetadata(nodeTypeVertex, topologyTemplate);
521                 if (flag == JsonParseFlagEnum.ParseAll || flag == JsonParseFlagEnum.ParseJson) {
522                         nodeTypeVertex.setJson(topologyTemplate.getCompositions());
523                 }
524                 nodeTypeVertex.addMetadataProperty(GraphPropertyEnum.CSAR_UUID, topologyTemplate.getMetadataValue(JsonPresentationFields.CSAR_UUID));
525                 nodeTypeVertex.addMetadataProperty(GraphPropertyEnum.DISTRIBUTION_STATUS, topologyTemplate.getMetadataValue(JsonPresentationFields.DISTRIBUTION_STATUS));
526                 
527                 return nodeTypeVertex;
528
529         }
530
531         private StorageOperationStatus assosiateMetadataToCategory(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
532                 if (topologyTemplate.getResourceType() == null) {
533                         // service
534                         return associateServiceMetadataToCategory(nodeTypeVertex, topologyTemplate);
535                 } else {
536                         // VF
537                         return assosiateResourceMetadataToCategory(nodeTypeVertex, topologyTemplate);
538                 }
539         }
540
541         private StorageOperationStatus associateServiceMetadataToCategory(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
542                 String categoryName = topologyTemplate.getCategories().get(0).getName();
543                 Either<GraphVertex, StorageOperationStatus> category = categoryOperation.getCategory(categoryName, VertexTypeEnum.SERVICE_CATEGORY);
544                 if (category.isRight()) {
545                         log.trace("NO category {} for service {}", categoryName, topologyTemplate.getUniqueId());
546                         return StorageOperationStatus.CATEGORY_NOT_FOUND;
547                 }
548                 GraphVertex categoryV = category.left().value();
549                 TitanOperationStatus createEdge = titanDao.createEdge(nodeTypeVertex, categoryV, EdgeLabelEnum.CATEGORY, new HashMap<>());
550                 if (createEdge != TitanOperationStatus.OK) {
551                         log.trace("Failed to associate resource {} to category {} with id {}", topologyTemplate.getUniqueId(), categoryName, categoryV.getUniqueId());
552                         return DaoStatusConverter.convertTitanStatusToStorageStatus(createEdge);
553                 }
554                 return StorageOperationStatus.OK;
555         }
556
557         @Override
558         public Either<ToscaElement, StorageOperationStatus> getToscaElement(String uniqueId, ComponentParametersView componentParametersView) {
559                 JsonParseFlagEnum parseFlag = componentParametersView.detectParseFlag();
560
561                 Either<GraphVertex, StorageOperationStatus> componentByLabelAndId = getComponentByLabelAndId(uniqueId, ToscaElementTypeEnum.TopologyTemplate, parseFlag);
562                 if (componentByLabelAndId.isRight()) {
563                         return Either.right(componentByLabelAndId.right().value());
564                 }
565                 GraphVertex componentV = componentByLabelAndId.left().value();
566
567                 return getToscaElement(componentV, componentParametersView);
568
569         }
570         // -------------------------------------------------------------
571
572         public Either<ToscaElement, StorageOperationStatus> getToscaElement(GraphVertex componentV, ComponentParametersView componentParametersView) {
573                 TopologyTemplate toscaElement;
574
575                 toscaElement = convertToTopologyTemplate(componentV);
576                 TitanOperationStatus status;
577                 if (!componentParametersView.isIgnoreUsers()) {
578                         status = setCreatorFromGraph(componentV, toscaElement);
579                         if (status != TitanOperationStatus.OK) {
580                                 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
581                         }
582
583                         status = setLastModifierFromGraph(componentV, toscaElement);
584                         if (status != TitanOperationStatus.OK) {
585                                 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
586                         }
587                 }
588                 if (!componentParametersView.isIgnoreCategories()) {
589                         status = setTopologyTempalteCategoriesFromGraph(componentV, toscaElement);
590                         if (status != TitanOperationStatus.OK) {
591                                 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
592
593                         }
594                 }
595                 if (!componentParametersView.isIgnoreArtifacts()) {
596                         TitanOperationStatus storageStatus = setAllArtifactsFromGraph(componentV, toscaElement);
597                         if (storageStatus != TitanOperationStatus.OK) {
598                                 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(storageStatus));
599                         }
600                 }
601                 if (!componentParametersView.isIgnoreComponentInstancesProperties()) {
602                         status = setComponentInstancesPropertiesFromGraph(componentV, toscaElement);
603                         if (status != TitanOperationStatus.OK) {
604                                 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
605                         }
606                 }
607                 if (!componentParametersView.isIgnoreCapabilities()) {
608                         status = setCapabilitiesFromGraph(componentV, toscaElement);
609                         if (status != TitanOperationStatus.OK) {
610                                 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
611                         }
612                 }
613                 if (!componentParametersView.isIgnoreRequirements()) {
614                         status = setRequirementsFromGraph(componentV, toscaElement);
615                         if (status != TitanOperationStatus.OK) {
616                                 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
617                         }
618                 }
619                 if (!componentParametersView.isIgnoreAllVersions()) {
620                         status = setAllVersions(componentV, toscaElement);
621                         if (status != TitanOperationStatus.OK) {
622                                 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
623                         }
624                 }
625                 if (!componentParametersView.isIgnoreAdditionalInformation()) {
626                         status = setAdditionalInformationFromGraph(componentV, toscaElement);
627                         if (status != TitanOperationStatus.OK) {
628                                 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
629                         }
630                 }
631
632                 if (!componentParametersView.isIgnoreGroups()) {
633                         status = setGroupsFromGraph(componentV, toscaElement);
634                         if (status != TitanOperationStatus.OK) {
635                                 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
636                         }
637
638                 }
639                 if (!componentParametersView.isIgnorePolicies()) {
640                         status = setPoliciesFromGraph(componentV, toscaElement);
641                         if (status != TitanOperationStatus.OK) {
642                                 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
643                         }
644
645                 }
646                 if (!componentParametersView.isIgnoreComponentInstances()) {
647                         status = setInstGroupsFromGraph(componentV, toscaElement);
648                         if (status != TitanOperationStatus.OK) {
649                                 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
650                         }
651
652                 }
653                 if (!componentParametersView.isIgnoreInputs()) {
654                         status = setInputsFromGraph(componentV, toscaElement);
655                         if (status != TitanOperationStatus.OK) {
656                                 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
657                         }
658
659                 }
660                 if (!componentParametersView.isIgnoreProperties()) {
661                         status = setPropertiesFromGraph(componentV, toscaElement);
662                         if (status != TitanOperationStatus.OK) {
663                                 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
664                         }
665
666                 }
667
668                 if (!componentParametersView.isIgnoreComponentInstancesInputs()) {
669                         status = setComponentInstancesInputsFromGraph(componentV, toscaElement);
670                         if (status != TitanOperationStatus.OK) {
671                                 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
672
673                         }
674                 }
675
676                 if (!componentParametersView.isIgnoreCapabiltyProperties()) {
677                         status = setComponentInstancesCapPropertiesFromGraph(componentV, toscaElement);
678                         if (status != TitanOperationStatus.OK) {
679                                 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
680
681                         }
682                 }
683
684                 if (!componentParametersView.isIgnoreForwardingPath()) {
685                         status = setForwardingGraphPropertiesFromGraph(componentV, toscaElement);
686                         if (status != TitanOperationStatus.OK) {
687                                 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
688
689                         }
690                 }
691                 if (!componentParametersView.isIgnoreInterfaces()) {
692                         TitanOperationStatus storageStatus = setInterfcesFromGraph(componentV, toscaElement);
693                         if (storageStatus != TitanOperationStatus.OK) {
694                                 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(storageStatus));
695                         }
696                 }
697
698                 return Either.left(toscaElement);
699         }
700
701         private TitanOperationStatus setInterfcesFromGraph(GraphVertex componentV,
702                                                                                                            TopologyTemplate topologyTemplate) {
703                 Either<Map<String, InterfaceDataDefinition>, TitanOperationStatus> result = getDataFromGraph
704                                 (componentV,
705                                                 EdgeLabelEnum.INTERFACE_ARTIFACTS);
706                 if (result.isLeft()) {
707                         topologyTemplate.setInterfaces(result.left().value());
708                 } else {
709                         if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
710                                 return result.right().value();
711                         }
712                 }
713                 return TitanOperationStatus.OK;
714         }
715
716         private StorageOperationStatus associateInterfacesToResource(GraphVertex topologyTemplateVertex,
717                                                                                                                                  TopologyTemplate topologyTemplate) {
718                 Map<String, InterfaceDataDefinition> interfaces = topologyTemplate.getInterfaces();
719                 return associateInterfacesToComponent(topologyTemplateVertex,interfaces);
720         }
721
722         public StorageOperationStatus associateInterfacesToComponent(GraphVertex nodeTypeVertex,
723                                                                                                                                  Map<String, InterfaceDataDefinition>
724                                                                                                                                                  interfaceMap) {
725                 if (interfaceMap != null && !interfaceMap.isEmpty()) {
726                         Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData
727                                         (nodeTypeVertex, VertexTypeEnum.INTERFACE_ARTIFACTS, EdgeLabelEnum.INTERFACE_ARTIFACTS, interfaceMap);
728                         if (assosiateElementToData.isRight()) {
729                                 return assosiateElementToData.right().value();
730                         }
731                 }
732                 return StorageOperationStatus.OK;
733         }
734
735
736         private TitanOperationStatus setPoliciesFromGraph(GraphVertex componentV, TopologyTemplate toscaElement) {
737                 Either<Map<String, PolicyDataDefinition>, TitanOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.POLICIES);
738                 if (result.isLeft()) {
739                         toscaElement.setPolicies(result.left().value());
740                 } else {
741                         if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
742                                 return result.right().value();
743                         }
744                 }
745                 return TitanOperationStatus.OK;
746         }
747
748         private TitanOperationStatus setForwardingGraphPropertiesFromGraph(GraphVertex componentV, TopologyTemplate topologyTemplate) {
749                 Either<Map<String, ForwardingPathDataDefinition>, TitanOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.FORWARDING_PATH);
750                 if (result.isLeft()) {
751                         topologyTemplate.setForwardingPaths(result.left().value());
752                 } else {
753                         if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
754                                 return result.right().value();
755                         }
756                 }
757                 return TitanOperationStatus.OK;
758         }
759
760
761         private TitanOperationStatus setComponentInstancesCapPropertiesFromGraph(GraphVertex componentV, TopologyTemplate topologyTemplate) {
762                 Either<Map<String, MapCapabiltyProperty>, TitanOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.CALCULATED_CAP_PROPERTIES);
763                 if (result.isLeft()) {
764                         topologyTemplate.setCalculatedCapabilitiesProperties(result.left().value());
765                 } else {
766                         if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
767                                 return result.right().value();
768                         }
769                 }
770                 return TitanOperationStatus.OK;
771         }
772
773         private TitanOperationStatus setPropertiesFromGraph(GraphVertex componentV, TopologyTemplate toscaElement) {
774                 Either<Map<String, PropertyDataDefinition>, TitanOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.PROPERTIES);
775                 if (result.isLeft()) {
776                         toscaElement.setProperties(result.left().value());
777                 } else {
778                         if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
779                                 return result.right().value();
780                         }
781                 }
782                 return TitanOperationStatus.OK;
783         }
784
785         private TitanOperationStatus setInstGroupsFromGraph(GraphVertex componentV, TopologyTemplate topologyTemplate) {
786                 Either<Map<String, MapGroupsDataDefinition>, TitanOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.INST_GROUPS);
787                 if (result.isLeft()) {
788                         topologyTemplate.setInstGroups(result.left().value());
789                 } else {
790                         if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
791                                 return result.right().value();
792                         }
793                 }
794                 return TitanOperationStatus.OK;
795         }
796
797         private TitanOperationStatus setComponentInstancesPropertiesFromGraph(GraphVertex componentV, TopologyTemplate topologyTemplate) {
798                 Either<Map<String, MapPropertiesDataDefinition>, TitanOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.INST_PROPERTIES);
799                 if (result.isLeft()) {
800                         topologyTemplate.setInstProperties(result.left().value());
801                 } else {
802                         if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
803                                 return result.right().value();
804                         }
805                 }
806                 return TitanOperationStatus.OK;
807         }
808
809         private TitanOperationStatus setComponentInstancesInputsFromGraph(GraphVertex componentV, TopologyTemplate topologyTemplate) {
810                 Either<Map<String, MapPropertiesDataDefinition>, TitanOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.INST_INPUTS);
811                 if (result.isLeft()) {
812                         topologyTemplate.setInstInputs(result.left().value());
813                 } else {
814                         if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
815                                 return result.right().value();
816                         }
817                 }
818                 return TitanOperationStatus.OK;
819         }
820
821         @Override
822         protected <T extends ToscaElement> TitanOperationStatus setRequirementsFromGraph(GraphVertex componentV, T toscaElement) {
823                 Either<Map<String, MapListRequirementDataDefinition>, TitanOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.CALCULATED_REQUIREMENTS);
824                 if (result.isLeft()) {
825                         ((TopologyTemplate) toscaElement).setCalculatedRequirements(result.left().value());
826                 } else {
827                         if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
828                                 return result.right().value();
829                         }
830                 }
831                 result = getDataFromGraph(componentV, EdgeLabelEnum.FULLFILLED_REQUIREMENTS);
832                 if (result.isLeft()) {
833                         ((TopologyTemplate) toscaElement).setFullfilledRequirements(result.left().value());
834                 } else {
835                         if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
836                                 return result.right().value();
837                         }
838                 }
839                 return TitanOperationStatus.OK;
840
841         }
842
843         protected <T extends ToscaElement> TitanOperationStatus setCapabilitiesFromGraph(GraphVertex componentV, T toscaElement) {
844                 Either<Map<String, MapListCapabiltyDataDefinition>, TitanOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.CALCULATED_CAPABILITIES);
845                 if (result.isLeft()) {
846                         ((TopologyTemplate) toscaElement).setCalculatedCapabilities(result.left().value());
847                 } else {
848                         if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
849                                 return result.right().value();
850                         }
851                 }
852                 result = getDataFromGraph(componentV, EdgeLabelEnum.FULLFILLED_CAPABILITIES);
853                 if (result.isLeft()) {
854                         ((TopologyTemplate) toscaElement).setFullfilledCapabilities(result.left().value());
855                 } else {
856                         if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
857                                 return result.right().value();
858                         }
859                 }
860                 return TitanOperationStatus.OK;
861         }
862
863         private TitanOperationStatus setAllArtifactsFromGraph(GraphVertex componentV, TopologyTemplate toscaElement) {
864                 TitanOperationStatus storageStatus = setArtifactsFromGraph(componentV, toscaElement);
865                 if (storageStatus != TitanOperationStatus.OK) {
866                         return storageStatus;
867                 }
868                 Either<Map<String, ArtifactDataDefinition>, TitanOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.SERVICE_API_ARTIFACTS);
869                 if (result.isLeft()) {
870                         toscaElement.setServiceApiArtifacts(result.left().value());
871                 } else {
872                         if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
873                                 return result.right().value();
874                         }
875                 }
876                 Either<Map<String, MapArtifactDataDefinition>, TitanOperationStatus> resultInstArt = getDataFromGraph(componentV, EdgeLabelEnum.INST_DEPLOYMENT_ARTIFACTS);
877                 if (resultInstArt.isLeft()) {
878                         toscaElement.setInstDeploymentArtifacts(resultInstArt.left().value());
879                 } else {
880                         if (resultInstArt.right().value() != TitanOperationStatus.NOT_FOUND) {
881                                 return resultInstArt.right().value();
882                         }
883                 }
884                 Either<Map<String, MapArtifactDataDefinition>, TitanOperationStatus> instanceArt = getDataFromGraph(componentV, EdgeLabelEnum.INSTANCE_ARTIFACTS);
885                 if (instanceArt.isLeft()) {
886                         toscaElement.setInstanceArtifacts(instanceArt.left().value());
887                 } else {
888                         if (instanceArt.right().value() != TitanOperationStatus.NOT_FOUND) {
889                                 return instanceArt.right().value();
890                         }
891                 }
892                 return TitanOperationStatus.OK;
893         }
894
895         private TitanOperationStatus setInputsFromGraph(GraphVertex componentV, TopologyTemplate toscaElement) {
896                 Either<Map<String, PropertyDataDefinition>, TitanOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.INPUTS);
897                 if (result.isLeft()) {
898                         toscaElement.setInputs(result.left().value());
899                 } else {
900                         if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
901                                 return result.right().value();
902                         }
903                 }
904                 return TitanOperationStatus.OK;
905         }
906
907         private TitanOperationStatus setGroupsFromGraph(GraphVertex componentV, TopologyTemplate toscaElement) {
908                 Either<Map<String, GroupDataDefinition>, TitanOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.GROUPS);
909                 if (result.isLeft()) {
910                         toscaElement.setGroups(result.left().value());
911                 } else {
912                         if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
913                                 return result.right().value();
914                         }
915                 }
916                 return TitanOperationStatus.OK;
917         }
918
919         private TitanOperationStatus setTopologyTempalteCategoriesFromGraph(GraphVertex componentV, ToscaElement toscaElement) {
920                 List<CategoryDefinition> categories = new ArrayList<>();
921
922                 switch (componentV.getType()) {
923                 case RESOURCE:
924                         return setResourceCategoryFromGraph(componentV, toscaElement);
925                 case SERVICE:
926                         return setServiceCategoryFromGraph(componentV, toscaElement, categories);
927
928                 default:
929                         log.debug("Not supported component type {} ", componentV.getType());
930                         break;
931                 }
932                 return TitanOperationStatus.OK;
933         }
934
935         private TitanOperationStatus setServiceCategoryFromGraph(GraphVertex componentV, ToscaElement toscaElement, List<CategoryDefinition> categories) {
936                 Either<GraphVertex, TitanOperationStatus> childVertex = titanDao.getChildVertex(componentV, EdgeLabelEnum.CATEGORY, JsonParseFlagEnum.NoParse);
937                 if (childVertex.isRight()) {
938                         log.debug("failed to fetch {} for tosca element with id {}, error {}", EdgeLabelEnum.CATEGORY, componentV.getUniqueId(), childVertex.right().value());
939                         return childVertex.right().value();
940                 }
941                 GraphVertex categoryV = childVertex.left().value();
942                 Map<GraphPropertyEnum, Object> metadataProperties = categoryV.getMetadataProperties();
943                 CategoryDefinition category = new CategoryDefinition();
944                 category.setUniqueId(categoryV.getUniqueId());
945                 category.setNormalizedName((String) metadataProperties.get(GraphPropertyEnum.NORMALIZED_NAME));
946                 category.setName((String) metadataProperties.get(GraphPropertyEnum.NAME));
947
948                 Type listTypeCat = new TypeToken<List<String>>() {}.getType();
949                 List<String> iconsfromJsonCat = getGson().fromJson((String) metadataProperties.get(GraphPropertyEnum.ICONS.getProperty()), listTypeCat);
950                 category.setIcons(iconsfromJsonCat);
951                 categories.add(category);
952                 toscaElement.setCategories(categories);
953
954                 return TitanOperationStatus.OK;
955         }
956
957         @SuppressWarnings("unchecked")
958         private TopologyTemplate convertToTopologyTemplate(GraphVertex componentV) {
959
960                 TopologyTemplate topologyTemplate = super.convertToComponent(componentV);
961
962                 Map<String, CompositionDataDefinition> json = (Map<String, CompositionDataDefinition>) componentV.getJson();
963                 topologyTemplate.setCompositions(json);
964
965                 return topologyTemplate;
966         }
967
968         @Override
969         public Either<ToscaElement, StorageOperationStatus> deleteToscaElement(GraphVertex toscaElementVertex) {
970                 Either<ToscaElement, StorageOperationStatus> nodeType = getToscaElement(toscaElementVertex, new ComponentParametersView());
971                 if (nodeType.isRight()) {
972                         log.debug("Failed to fetch tosca element {} error {}", toscaElementVertex.getUniqueId(), nodeType.right().value());
973                         return nodeType;
974                 }
975                 TitanOperationStatus status = disassociateAndDeleteCommonElements(toscaElementVertex);
976                 if (status != TitanOperationStatus.OK) {
977                         Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
978                 }
979                 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.INST_ATTRIBUTES);
980                 if (status != TitanOperationStatus.OK) {
981                         log.debug("Failed to disassociate instances attributes for {} error {}", toscaElementVertex.getUniqueId(), status);
982                         Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
983                 }
984                 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.INST_PROPERTIES);
985                 if (status != TitanOperationStatus.OK) {
986                         log.debug("Failed to disassociate instances properties for {} error {}", toscaElementVertex.getUniqueId(), status);
987                         Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
988                 }
989
990                 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.INST_INPUTS);
991                 if (status != TitanOperationStatus.OK) {
992                         log.debug("Failed to disassociate instances inputs for {} error {}", toscaElementVertex.getUniqueId(), status);
993                         Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
994                 }
995
996                 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.GROUPS);
997                 if (status != TitanOperationStatus.OK) {
998                         log.debug("Failed to disassociate groups for {} error {}", toscaElementVertex.getUniqueId(), status);
999                         Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
1000                 }
1001                 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.INST_GROUPS);
1002                 if (status != TitanOperationStatus.OK) {
1003                         log.debug("Failed to disassociate instance groups for {} error {}", toscaElementVertex.getUniqueId(), status);
1004                         Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
1005                 }
1006                 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.INPUTS);
1007                 if (status != TitanOperationStatus.OK) {
1008                         log.debug("Failed to disassociate inputs for {} error {}", toscaElementVertex.getUniqueId(), status);
1009                         Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
1010                 }
1011                 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.INST_INPUTS);
1012                 if (status != TitanOperationStatus.OK) {
1013                         log.debug("Failed to disassociate instance inputs for {} error {}", toscaElementVertex.getUniqueId(), status);
1014                         Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
1015                 }
1016                 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.CALCULATED_CAPABILITIES);
1017                 if (status != TitanOperationStatus.OK) {
1018                         log.debug("Failed to disassociate calculated capabiliites for {} error {}", toscaElementVertex.getUniqueId(), status);
1019                         Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
1020                 }
1021                 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.FULLFILLED_CAPABILITIES);
1022                 if (status != TitanOperationStatus.OK) {
1023                         log.debug("Failed to disassociate fullfilled capabilities for {} error {}", toscaElementVertex.getUniqueId(), status);
1024                         Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
1025                 }
1026                 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.CALCULATED_CAP_PROPERTIES);
1027                 if (status != TitanOperationStatus.OK) {
1028                         log.debug("Failed to disassociate calculated capabiliites properties for {} error {}", toscaElementVertex.getUniqueId(), status);
1029                         Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
1030                 }
1031                 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.CALCULATED_REQUIREMENTS);
1032                 if (status != TitanOperationStatus.OK) {
1033                         log.debug("Failed to disassociate calculated requirements for {} error {}", toscaElementVertex.getUniqueId(), status);
1034                         Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
1035                 }
1036                 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.FULLFILLED_REQUIREMENTS);
1037                 if (status != TitanOperationStatus.OK) {
1038                         log.debug("Failed to disassociate full filled requirements for {} error {}", toscaElementVertex.getUniqueId(), status);
1039                         Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
1040                 }
1041                 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.INST_DEPLOYMENT_ARTIFACTS);
1042                 if (status != TitanOperationStatus.OK) {
1043                         log.debug("Failed to disassociate instance artifacts for {} error {}", toscaElementVertex.getUniqueId(), status);
1044                         Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
1045                 }
1046                 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.SERVICE_API_ARTIFACTS);
1047                 if (status != TitanOperationStatus.OK) {
1048                         log.debug("Failed to disassociate service api artifacts for {} error {}", toscaElementVertex.getUniqueId(), status);
1049                         Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
1050                 }
1051                 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.FORWARDING_PATH);
1052                 if (status != TitanOperationStatus.OK) {
1053                         log.debug("Failed to disassociate service api artifacts for {} error {}", toscaElementVertex.getUniqueId(), status);
1054                         Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
1055                 }
1056
1057                 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.INSTANCE_ARTIFACTS);
1058                 toscaElementVertex.getVertex().remove();
1059                 log.trace("Tosca element vertex for {} was removed", toscaElementVertex.getUniqueId());
1060
1061                 return nodeType;
1062         }
1063
1064         @SuppressWarnings("unchecked")
1065         @Override
1066         public Either<TopologyTemplate, StorageOperationStatus> createToscaElement(ToscaElement toscaElement) {
1067                 return createTopologyTemplate((TopologyTemplate) toscaElement);
1068         }
1069
1070         @Override
1071         protected <T extends ToscaElement> TitanOperationStatus setCategoriesFromGraph(GraphVertex vertexComponent, T toscaElement) {
1072                 return setTopologyTempalteCategoriesFromGraph(vertexComponent, toscaElement);
1073         }
1074
1075         @Override
1076         protected <T extends ToscaElement> StorageOperationStatus validateCategories(T toscaElementToUpdate, GraphVertex elementV) {
1077                 // Product isn't supported now!!
1078                 // TODO add for Product
1079                 if (toscaElementToUpdate.getComponentType() == ComponentTypeEnum.SERVICE) {
1080                         return validateServiceCategory(toscaElementToUpdate, elementV);
1081                 } else {
1082                         // Resource
1083                         return validateResourceCategory(toscaElementToUpdate, elementV);
1084                 }
1085         }
1086
1087         @Override
1088         protected <T extends ToscaElement> StorageOperationStatus updateDerived(T toscaElementToUpdate, GraphVertex updateElementV) {
1089                 // not relevant now for topology template
1090                 return StorageOperationStatus.OK;
1091         }
1092
1093         @Override
1094         public <T extends ToscaElement> void fillToscaElementVertexData(GraphVertex elementV, T toscaElementToUpdate, JsonParseFlagEnum flag) {
1095                 fillMetadata(elementV, (TopologyTemplate) toscaElementToUpdate, flag);
1096         }
1097
1098         private <T extends ToscaElement> StorageOperationStatus validateServiceCategory(T toscaElementToUpdate, GraphVertex elementV) {
1099                 StorageOperationStatus status = StorageOperationStatus.OK;
1100                 List<CategoryDefinition> newCategoryList = toscaElementToUpdate.getCategories();
1101                 CategoryDefinition newCategory = newCategoryList.get(0);
1102
1103                 Either<GraphVertex, TitanOperationStatus> childVertex = titanDao.getChildVertex(elementV, EdgeLabelEnum.CATEGORY, JsonParseFlagEnum.NoParse);
1104                 if (childVertex.isRight()) {
1105                         log.debug("failed to fetch {} for tosca element with id {}, error {}", EdgeLabelEnum.CATEGORY, elementV.getUniqueId(), childVertex.right().value());
1106                         return DaoStatusConverter.convertTitanStatusToStorageStatus(childVertex.right().value());
1107                 }
1108
1109                 GraphVertex categoryV = childVertex.left().value();
1110                 Map<GraphPropertyEnum, Object> metadataProperties = categoryV.getMetadataProperties();
1111                 String categoryNameCurrent = (String) metadataProperties.get(GraphPropertyEnum.NAME);
1112
1113                 String newCategoryName = newCategory.getName();
1114                 if (newCategoryName != null && false == newCategoryName.equals(categoryNameCurrent)) {
1115                         // the category was changed
1116                         Either<GraphVertex, StorageOperationStatus> getCategoryVertex = categoryOperation.getCategory(newCategoryName, VertexTypeEnum.SERVICE_CATEGORY);
1117
1118                         if (getCategoryVertex.isRight()) {
1119                                 return getCategoryVertex.right().value();
1120                         }
1121                         GraphVertex newCategoryV = getCategoryVertex.left().value();
1122                         status = moveCategoryEdge(elementV, newCategoryV);
1123                         log.debug("Going to update the category of the resource from {} to {}. status is {}", categoryNameCurrent, newCategory, status);
1124                 }
1125                 return status;
1126         }
1127
1128         public Either<List<GraphVertex>, TitanOperationStatus> getAllNotDeletedElements() {
1129                 Map<GraphPropertyEnum, Object> propsHasNot = new HashMap<>();
1130                 propsHasNot.put(GraphPropertyEnum.IS_DELETED, true);
1131
1132                 Either<List<GraphVertex>, TitanOperationStatus> byCriteria = titanDao.getByCriteria(VertexTypeEnum.TOPOLOGY_TEMPLATE, null, propsHasNot, JsonParseFlagEnum.ParseJson);
1133                 if (byCriteria.isRight()) {
1134                         log.debug("Failed to fetch all non marked topology templates , propsHasNot {}, error {}", propsHasNot, byCriteria.right().value());
1135                         return Either.right(byCriteria.right().value());
1136                 }
1137                 return Either.left(byCriteria.left().value());
1138         }
1139
1140         @SuppressWarnings("unchecked")
1141         public boolean isInUse(GraphVertex elementV, List<GraphVertex> allNonDeleted) {
1142                 for (GraphVertex containerV : allNonDeleted) {
1143                         Map<String, CompositionDataDefinition> composition = (Map<String, CompositionDataDefinition>) containerV.getJson();
1144                         if (composition != null) {
1145                                 CompositionDataDefinition instances = composition.get(JsonConstantKeysEnum.COMPOSITION.getValue());
1146                                 if (instances != null && instances.getComponentInstances() != null && !instances.getComponentInstances().isEmpty()) {
1147                                         for (ComponentInstanceDataDefinition ci : instances.getComponentInstances().values()) {
1148                                                 if (ci.getComponentUid().equals(elementV.getUniqueId())) {
1149                                                         log.debug("The resource {} failed to delete cause in use as component instance UniqueID = {} in {} with UniqueID {}", elementV.getUniqueId(), ci.getUniqueId(), containerV.getType(), containerV.getUniqueId());
1150                                                         return true;
1151                                                 }
1152                                         }
1153
1154                                 }
1155                         }
1156                 }
1157
1158                 return false;
1159         }
1160
1161         @SuppressWarnings("unchecked")
1162         public boolean isInUse(String componentId, List<GraphVertex> allNonDeleted) {
1163                 for (GraphVertex containerV : allNonDeleted) {
1164                         Map<String, CompositionDataDefinition> composition = (Map<String, CompositionDataDefinition>) containerV.getJson();
1165                         if (composition != null) {
1166                                 CompositionDataDefinition instances = composition.get(JsonConstantKeysEnum.COMPOSITION.getValue());
1167                                 if (instances != null && instances.getComponentInstances() != null && !instances.getComponentInstances().isEmpty()) {
1168                                         for (ComponentInstanceDataDefinition ci : instances.getComponentInstances().values()) {
1169                                                 if (ci.getComponentUid().equals(componentId)) {
1170                                                         return true;
1171                                                 }
1172                                         }
1173
1174                                 }
1175                         }
1176                 }
1177
1178                 return false;
1179         }
1180
1181         public Either<GraphVertex, StorageOperationStatus> updateDistributionStatus(String uniqueId, User user, DistributionStatusEnum distributionStatus) {
1182
1183                 Either<GraphVertex, StorageOperationStatus> result = null;
1184                 String userId = user.getUserId();
1185                 Either<GraphVertex, TitanOperationStatus> getRes = findUserVertex(userId);
1186                 GraphVertex userVertex = null;
1187                 GraphVertex serviceVertex = null;
1188                 if (getRes.isRight()) {
1189                         TitanOperationStatus status = getRes.right().value();
1190                         CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Cannot find user {} in the graph. status is {}", userId, status);
1191                         result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
1192                 }
1193                 if (result == null) {
1194                         userVertex = getRes.left().value();
1195                         getRes = titanDao.getVertexById(uniqueId, JsonParseFlagEnum.ParseMetadata);
1196                         if (getRes.isRight()) {
1197                                 TitanOperationStatus status = getRes.right().value();
1198                                 log.error("Cannot find service {} in the graph. status is {}", uniqueId, status);
1199                                 result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
1200                         }
1201                 }
1202                 if (result == null) {
1203                         serviceVertex = getRes.left().value();
1204                         Iterator<Edge> edgeIterator = serviceVertex.getVertex().edges(Direction.IN, EdgeLabelEnum.LAST_DISTRIBUTION_STATE_MODIFIER.name());
1205                         if (edgeIterator.hasNext()) {
1206                                 log.debug("Remove existing edge from user to component {}. Edge type is {}", userId, uniqueId, EdgeLabelEnum.LAST_DISTRIBUTION_STATE_MODIFIER);
1207                                 edgeIterator.next().remove();
1208                         }
1209                 }
1210                 if (result == null) {
1211                         TitanOperationStatus status = titanDao.createEdge(userVertex, serviceVertex, EdgeLabelEnum.LAST_DISTRIBUTION_STATE_MODIFIER, null);
1212                         if (status != TitanOperationStatus.OK) {
1213                                 log.error("Failed to associate user {} to component {}. Edge type is {}", userId, uniqueId, EdgeLabelEnum.LAST_DISTRIBUTION_STATE_MODIFIER);
1214                                 result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
1215                         }
1216                 }
1217                 if (result == null) {
1218                         serviceVertex.addMetadataProperty(GraphPropertyEnum.DISTRIBUTION_STATUS, distributionStatus.name());
1219                         long lastUpdateDate = System.currentTimeMillis();
1220                         serviceVertex.setJsonMetadataField(JsonPresentationFields.LAST_UPDATE_DATE, lastUpdateDate);
1221                         Either<GraphVertex, TitanOperationStatus> updateRes = titanDao.updateVertex(serviceVertex);
1222                         if (updateRes.isRight()) {
1223                                 result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(updateRes.right().value()));
1224                         }
1225                 }
1226                 if (result == null) {
1227                         result = Either.left(serviceVertex);
1228                 }
1229                 return result;
1230         }
1231         /**
1232          * Returns list of ComponentInstanceProperty belonging to component instance capability specified by name, type and ownerId
1233          * @param componentId
1234          * @param instanceId
1235          * @param capabilityName
1236          * @param capabilityType
1237          * @param ownerId 
1238          * @return
1239          */
1240         public Either<List<ComponentInstanceProperty>, StorageOperationStatus> getComponentInstanceCapabilityProperties(String componentId, String instanceId, String capabilityName, String capabilityType, String ownerId) {
1241
1242                 Either<List<ComponentInstanceProperty>, StorageOperationStatus> result = null;
1243                 Map<String, MapCapabiltyProperty> mapPropertiesDataDefinition = null;
1244                 Either<GraphVertex, StorageOperationStatus> componentByLabelAndId = getComponentByLabelAndId(componentId, ToscaElementTypeEnum.TopologyTemplate, JsonParseFlagEnum.NoParse);
1245                 if (componentByLabelAndId.isRight()) {
1246                         result = Either.right(componentByLabelAndId.right().value());
1247                 }
1248                 if(componentByLabelAndId.isLeft()){
1249                         Either<Map<String, MapCapabiltyProperty>, TitanOperationStatus> getDataRes = getDataFromGraph(componentByLabelAndId.left().value(), EdgeLabelEnum.CALCULATED_CAP_PROPERTIES);
1250                         if (getDataRes.isRight()) {
1251                                 result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(getDataRes.right().value()));
1252                         } else {
1253                                 mapPropertiesDataDefinition = getDataRes.left().value();
1254                         }
1255                 }
1256                 if(isNotEmptyMapOfProperties(instanceId, mapPropertiesDataDefinition)){
1257                         result = Either.left(findComponentInstanceCapabilityProperties(instanceId, capabilityName, capabilityType, ownerId, mapPropertiesDataDefinition.get(instanceId).getMapToscaDataDefinition()));
1258                 }
1259                 return result;
1260         }
1261
1262         public StorageOperationStatus updateComponentInstanceCapabilityProperties(Component containerComponent, String componentInstanceId, MapCapabiltyProperty instanceProperties) {
1263                 return updateToscaDataDeepElementsBlockToToscaElement(containerComponent.getUniqueId(), EdgeLabelEnum.CALCULATED_CAP_PROPERTIES, instanceProperties, componentInstanceId);
1264         }
1265
1266
1267         private boolean isNotEmptyMapOfProperties(String instanceId, Map<String, MapCapabiltyProperty> mapPropertiesDataDefinition) {
1268                 return  MapUtils.isNotEmpty(mapPropertiesDataDefinition) &&
1269                                 mapPropertiesDataDefinition.get(instanceId) != null &&
1270                                 MapUtils.isNotEmpty(mapPropertiesDataDefinition.get(instanceId).getMapToscaDataDefinition());
1271         }
1272
1273         private List<ComponentInstanceProperty> findComponentInstanceCapabilityProperties(String instanceId, String capabilityName, String capabilityType, String ownerId, Map<String, MapPropertiesDataDefinition> propertiesMap) {
1274                 List<ComponentInstanceProperty> capPropsList = null;
1275                 for(Entry<String, MapPropertiesDataDefinition> capProp : propertiesMap.entrySet()){
1276                         if (isBelongingPropertyMap(instanceId, capabilityName, capabilityType, ownerId, capProp)) {
1277                                 Map<String, PropertyDataDefinition> capMap = capProp.getValue().getMapToscaDataDefinition();
1278                                 if (capMap != null && !capMap.isEmpty()) {
1279                                         capPropsList = capMap.values().stream().map(o -> new ComponentInstanceProperty(o)).collect(Collectors.toList());
1280                                         break;
1281                                 }
1282                         }
1283                 }
1284                 if(capPropsList == null){
1285                         capPropsList = new ArrayList<>();
1286                 }
1287                 return capPropsList;
1288         }
1289
1290         private boolean isBelongingPropertyMap(String instanceId, String capabilityName, String capabilityType, String ownerId, Entry<String, MapPropertiesDataDefinition> capProp) {
1291                 if (capProp != null) {
1292                         String[] path = capProp.getKey().split(ModelConverter.CAP_PROP_DELIM );
1293                         if (path.length < 4) {
1294                                 log.debug("wrong key format for capabilty, key {}", capProp);
1295                                 return false;
1296                         }
1297                         return path[path.length - 2].equals(capabilityType) && path[path.length - 1].equals(capabilityName) && path[1].equals(ownerId) && path[0].equals(instanceId);
1298                 }
1299                 return false;
1300         }
1301
1302         public StorageOperationStatus addPolicyToToscaElement(GraphVertex componentV, PolicyDefinition policyDefinition, int counter) {
1303                 fillPolicyDefinition(componentV, policyDefinition, counter);
1304                 return addToscaDataToToscaElement(componentV, EdgeLabelEnum.POLICIES, VertexTypeEnum.POLICIES, policyDefinition, JsonPresentationFields.UNIQUE_ID);
1305         }
1306         
1307         public StorageOperationStatus updatePolicyOfToscaElement(GraphVertex componentV, PolicyDefinition policyDefinition) {
1308                 return updateToscaDataOfToscaElement(componentV, EdgeLabelEnum.POLICIES, VertexTypeEnum.POLICIES, policyDefinition, JsonPresentationFields.UNIQUE_ID);
1309         }
1310
1311         public StorageOperationStatus updatePoliciesOfToscaElement(GraphVertex componentV, List<PolicyDefinition> policiesDefinitions) {
1312                 return updateToscaDataOfToscaElement(componentV, EdgeLabelEnum.POLICIES, VertexTypeEnum.POLICIES, policiesDefinitions, JsonPresentationFields.UNIQUE_ID);
1313         }
1314         
1315         public StorageOperationStatus removePolicyFromToscaElement(GraphVertex componentV, String policyId) {
1316                 return  deleteToscaDataElement(componentV, EdgeLabelEnum.POLICIES, VertexTypeEnum.POLICIES, policyId, JsonPresentationFields.UNIQUE_ID);
1317         }
1318
1319         private void fillPolicyDefinition(GraphVertex componentV, PolicyDefinition policyDefinition, int counter) {
1320                 String policyName = buildSubComponentName((String) componentV.getJsonMetadataField(JsonPresentationFields.NAME), policyDefinition.getPolicyTypeName(), counter);
1321                 policyDefinition.setName(policyName);
1322                 policyDefinition.setInvariantName(policyName);
1323                 policyDefinition.setComponentName((String) componentV.getJsonMetadataField(JsonPresentationFields.NAME));
1324                 policyDefinition.setUniqueId(UniqueIdBuilder.buildPolicyUniqueId(componentV.getUniqueId(), policyName));
1325                 policyDefinition.setInvariantUUID(UniqueIdBuilder.buildInvariantUUID());
1326                 policyDefinition.setPolicyUUID(UniqueIdBuilder.generateUUID());
1327         }
1328         
1329         public static String buildSubComponentName(String componentName, String subComponentTypeName, int counter) {
1330                 String typeSuffix = subComponentTypeName.substring(subComponentTypeName.lastIndexOf('.') + 1, subComponentTypeName.length());
1331                 return componentName + Constants.GROUP_POLICY_NAME_DELIMETER + typeSuffix + Constants.GROUP_POLICY_NAME_DELIMETER + counter;
1332         }
1333
1334 }