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