fix capabilities
[sdc.git] / catalog-model / src / main / java / org / openecomp / sdc / be / model / jsontitan / operations / NodeTypeOperation.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 fj.data.Either;
24 import java.util.stream.Collectors;
25
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.InterfaceDataDefinition;
35 import org.openecomp.sdc.be.datatypes.elements.ListCapabilityDataDefinition;
36 import org.openecomp.sdc.be.datatypes.elements.ListRequirementDataDefinition;
37 import org.openecomp.sdc.be.datatypes.elements.MapPropertiesDataDefinition;
38 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
39 import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum;
40 import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
41 import org.openecomp.sdc.be.datatypes.tosca.ToscaDataDefinition;
42 import org.openecomp.sdc.be.model.ComponentParametersView;
43 import org.openecomp.sdc.be.model.DerivedNodeTypeResolver;
44 import org.openecomp.sdc.be.model.LifecycleStateEnum;
45 import org.openecomp.sdc.be.model.jsontitan.datamodel.NodeType;
46 import org.openecomp.sdc.be.model.jsontitan.datamodel.ToscaElement;
47 import org.openecomp.sdc.be.model.jsontitan.datamodel.ToscaElementTypeEnum;
48 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
49 import org.openecomp.sdc.be.model.operations.impl.DaoStatusConverter;
50 import org.openecomp.sdc.be.model.operations.impl.UniqueIdBuilder;
51 import org.openecomp.sdc.common.jsongraph.util.CommonUtility;
52 import org.openecomp.sdc.common.jsongraph.util.CommonUtility.LogLevelEnum;
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
55 import org.springframework.beans.factory.annotation.Qualifier;
56
57 import java.util.ArrayList;
58 import java.util.Collection;
59 import java.util.Collections;
60 import java.util.EnumSet;
61 import java.util.HashMap;
62 import java.util.HashSet;
63 import java.util.List;
64 import java.util.Map;
65 import java.util.Set;
66 import java.util.regex.Pattern;
67
68 @org.springframework.stereotype.Component("node-type-operation")
69 public class NodeTypeOperation extends ToscaElementOperation {
70         public static Pattern uuidNewVersion = Pattern.compile("^\\d{1,}.1");
71         public static Pattern uuidNormativeNewVersion = Pattern.compile("^\\d{1,}.0");
72
73         private static Logger log = LoggerFactory.getLogger(NodeTypeOperation.class.getName());
74
75         private DerivedNodeTypeResolver derivedResourceResolver;
76
77         public NodeTypeOperation(@Qualifier("derived-resource-resolver") DerivedNodeTypeResolver derivedNodeTypeResolver) {
78                 this.derivedResourceResolver = derivedNodeTypeResolver;
79         }
80
81         public Either<NodeType, StorageOperationStatus> createNodeType(NodeType nodeType) {
82
83                 Either<NodeType, StorageOperationStatus> result = null;
84
85                 nodeType.generateUUID();
86
87                 nodeType = getResourceMetaDataFromResource(nodeType);
88                 String resourceUniqueId = nodeType.getUniqueId();
89                 if (resourceUniqueId == null) {
90                         resourceUniqueId = UniqueIdBuilder.buildResourceUniqueId();
91                         nodeType.setUniqueId(resourceUniqueId);
92                 }
93
94                 // get derived from resources
95                 List<GraphVertex> derivedResources = null;
96                 Either<List<GraphVertex>, StorageOperationStatus> derivedResourcesResult = findDerivedResources(nodeType);
97                 if (derivedResourcesResult.isRight()) {
98                         result = Either.right(derivedResourcesResult.right().value());
99                         return result;
100                 } else {
101                         derivedResources = derivedResourcesResult.left().value();
102                 }
103
104                 GraphVertex nodeTypeVertex = new GraphVertex(VertexTypeEnum.NODE_TYPE);
105                 fillToscaElementVertexData(nodeTypeVertex, nodeType, JsonParseFlagEnum.ParseAll);
106
107                 Either<GraphVertex, TitanOperationStatus> createdVertex = titanDao.createVertex(nodeTypeVertex);
108                 if (createdVertex.isRight()) {
109                         TitanOperationStatus status = createdVertex.right().value();
110                         log.error("Error returned after creating resource data node {}. status returned is ", nodeTypeVertex, status);
111                         result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
112                         return result;
113                 }
114                 nodeTypeVertex = createdVertex.left().value();
115
116                 StorageOperationStatus assosiateCommon = assosiateCommonForToscaElement(nodeTypeVertex, nodeType, derivedResources);
117                 if (assosiateCommon != StorageOperationStatus.OK) {
118                         result = Either.right(assosiateCommon);
119                         return result;
120                 }
121
122                 StorageOperationStatus associateDerived = assosiateToDerived(nodeTypeVertex, derivedResources);
123                 if (associateDerived != StorageOperationStatus.OK) {
124                         result = Either.right(associateDerived);
125                         return result;
126                 }
127                 StorageOperationStatus associateCategory = assosiateResourceMetadataToCategory(nodeTypeVertex, nodeType);
128                 if (associateCategory != StorageOperationStatus.OK) {
129                         result = Either.right(associateCategory);
130                         return result;
131                 }
132
133                 StorageOperationStatus associateAttributes = associateAttributesToResource(nodeTypeVertex, nodeType, derivedResources);
134                 if (associateAttributes != StorageOperationStatus.OK) {
135                         result = Either.right(associateAttributes);
136                         return result;
137                 }
138
139                 StorageOperationStatus associateRequirements = associateRequirementsToResource(nodeTypeVertex, nodeType, derivedResources);
140                 if (associateRequirements != StorageOperationStatus.OK) {
141                         result = Either.right(associateRequirements);
142                         return result;
143                 }
144
145                 StorageOperationStatus associateCapabilities = associateCapabilitiesToResource(nodeTypeVertex, nodeType, derivedResources);
146                 if (associateCapabilities != StorageOperationStatus.OK) {
147                         result = Either.right(associateCapabilities);
148                         return result;
149                 }
150                 StorageOperationStatus associateCapabilitiesProps = associateCapabilitiesPropertiesToResource(nodeTypeVertex, nodeType, derivedResources);
151                 if (associateCapabilitiesProps != StorageOperationStatus.OK) {
152                         result = Either.right(associateCapabilitiesProps);
153                         return result;
154                 }
155
156                 StorageOperationStatus associateInterfaces = associateInterfacesToResource(nodeTypeVertex, nodeType, derivedResources);
157                 if (associateInterfaces != StorageOperationStatus.OK) {
158                         result = Either.right(associateInterfaces);
159                         return result;
160                 }
161
162                 StorageOperationStatus addAdditionalInformation = addAdditionalInformationToResource(nodeTypeVertex, nodeType, derivedResources);
163                 if (addAdditionalInformation != StorageOperationStatus.OK) {
164                         result = Either.right(addAdditionalInformation);
165                         return result;
166                 }
167                 result = Either.left(nodeType);
168                 return result;
169
170         }
171
172         private StorageOperationStatus associateInterfacesToResource(GraphVertex nodeTypeVertex, NodeType nodeType, List<GraphVertex> derivedResources) {
173                 // Note : currently only one derived supported!!!!
174                 Either<Map<String, InterfaceDataDefinition>, StorageOperationStatus> dataFromDerived = getDataFromDerived(derivedResources, EdgeLabelEnum.INTERFACE_ARTIFACTS);
175                 if (dataFromDerived.isRight()) {
176                         return dataFromDerived.right().value();
177                 }
178                 Map<String, InterfaceDataDefinition> interfacArtsAll = dataFromDerived.left().value();
179
180                 Map<String, InterfaceDataDefinition> interfacArts = nodeType.getInterfaceArtifacts();
181                 if (interfacArts != null) {
182                         interfacArtsAll.putAll(interfacArts);
183                 }
184                 if (!interfacArtsAll.isEmpty()) {
185                         Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.INTERFACE_ARTIFACTS, EdgeLabelEnum.INTERFACE_ARTIFACTS, interfacArtsAll);
186                         if (assosiateElementToData.isRight()) {
187                                 return assosiateElementToData.right().value();
188                         }
189                 }
190                 return StorageOperationStatus.OK;
191         }
192
193         @Override
194         public Either<ToscaElement, StorageOperationStatus> getToscaElement(String uniqueId, ComponentParametersView componentParametersView) {
195
196                 Either<GraphVertex, StorageOperationStatus> componentByLabelAndId = getComponentByLabelAndId(uniqueId, ToscaElementTypeEnum.NodeType, JsonParseFlagEnum.ParseMetadata);
197                 if (componentByLabelAndId.isRight()) {
198                         return Either.right(componentByLabelAndId.right().value());
199                 }
200                 GraphVertex componentV = componentByLabelAndId.left().value();
201
202                 return getToscaElement(componentV, componentParametersView);
203
204         }
205
206         // -------------------------------------------------------------
207         @Override
208         public Either<ToscaElement, StorageOperationStatus> getToscaElement(GraphVertex componentV, ComponentParametersView componentParametersView) {
209                 NodeType toscaElement;
210                 toscaElement = convertToComponent(componentV);
211                 TitanOperationStatus status = null;
212                 if (false == componentParametersView.isIgnoreUsers()) {
213                         status = setCreatorFromGraph(componentV, toscaElement);
214                         if (status != TitanOperationStatus.OK) {
215                                 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
216                         }
217
218                         status = setLastModifierFromGraph(componentV, toscaElement);
219                         if (status != TitanOperationStatus.OK) {
220                                 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
221                         }
222                 }
223
224                 if (false == componentParametersView.isIgnoreProperties()) {
225                         status = setResourcePropertiesFromGraph(componentV, toscaElement);
226                         if (status != TitanOperationStatus.OK && status != TitanOperationStatus.NOT_FOUND) {
227                                 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
228                         }
229                 }
230
231                 if (false == componentParametersView.isIgnoreAttributesFrom()) {
232                         status = setResourceAttributesFromGraph(componentV, toscaElement);
233                         if (status != TitanOperationStatus.OK) {
234                                 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
235                         }
236                 }
237
238                 if (false == componentParametersView.isIgnoreDerivedFrom()) {
239                         status = setResourceDerivedFromGraph(componentV, toscaElement);
240                         if (status != TitanOperationStatus.OK) {
241                                 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
242                         }
243                 }
244
245                 if (false == componentParametersView.isIgnoreCategories()) {
246                         status = setResourceCategoryFromGraph(componentV, toscaElement);
247                         if (status != TitanOperationStatus.OK) {
248                                 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
249                         }
250                 }
251                 if (false == componentParametersView.isIgnoreRequirements()) {
252                         status = setResourceRequirementsFromGraph(componentV, toscaElement);
253                         if (status != TitanOperationStatus.OK) {
254                                 log.error("Failed to set requirement of resource {}. status is {}", componentV.getUniqueId(), status);
255                                 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
256                         }
257                 }
258                 if (false == componentParametersView.isIgnoreCapabilities()) {
259                         status = setResourceCapabilitiesFromGraph(componentV, toscaElement);
260                         if (status != TitanOperationStatus.OK) {
261                                 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
262                         }
263                 }
264
265                 if (false == componentParametersView.isIgnoreArtifacts()) {
266                         status = setArtifactsFromGraph(componentV, toscaElement);
267                         if (status != TitanOperationStatus.OK) {
268                                 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
269                         }
270                 }
271                 if (false == componentParametersView.isIgnoreAdditionalInformation()) {
272                         status = setAdditionalInformationFromGraph(componentV, toscaElement);
273                         if (status != TitanOperationStatus.OK) {
274                                 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
275                         }
276                 }
277                 if (false == componentParametersView.isIgnoreInterfaces()) {
278                         status = setInterfacesFromGraph(componentV, toscaElement);
279                         if (status != TitanOperationStatus.OK) {
280                                 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
281                         }
282                 }
283                 if (false == componentParametersView.isIgnoreAllVersions()) {
284                         status = setAllVersions(componentV, toscaElement);
285                         if (status != TitanOperationStatus.OK && status != TitanOperationStatus.NOT_FOUND) {
286                                 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
287                         }
288                 }
289
290                 if (false == componentParametersView.isIgnoreCapabiltyProperties()) {
291                         status = setComponentCapPropertiesFromGraph(componentV, toscaElement);
292                         if (status != TitanOperationStatus.OK) {
293                                 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
294
295                         }
296                 }
297                 return Either.left(toscaElement);
298         }
299
300         private TitanOperationStatus setComponentCapPropertiesFromGraph(GraphVertex componentV, NodeType toscaElement) {
301                 Either<Map<String, MapPropertiesDataDefinition>, TitanOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.CAPABILITIES_PROPERTIES);
302                 if (result.isLeft()) {
303                         toscaElement.setCapabiltiesProperties(result.left().value());
304                 } else {
305                         if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
306                                 return result.right().value();
307                         }
308                 }
309                 return TitanOperationStatus.OK;
310         }
311
312         private TitanOperationStatus setInterfacesFromGraph(GraphVertex componentV, NodeType toscaElement) {
313                 Either<Map<String, InterfaceDataDefinition>, TitanOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.INTERFACE_ARTIFACTS);
314                 if (result.isLeft()) {
315                         toscaElement.setInterfaceArtifacts(result.left().value());
316                 } else {
317                         if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
318                                 return result.right().value();
319                         }
320                 }
321                 return TitanOperationStatus.OK;
322         }
323
324         protected <T extends ToscaElement> TitanOperationStatus setCapabilitiesFromGraph(GraphVertex componentV, T toscaElement) {
325                 return setResourceCapabilitiesFromGraph(componentV, (NodeType) toscaElement);
326         }
327
328         private TitanOperationStatus setResourceCapabilitiesFromGraph(GraphVertex componentV, NodeType toscaElement) {
329                 Either<Map<String, ListCapabilityDataDefinition>, TitanOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.CAPABILITIES);
330                 if (result.isLeft()) {
331                         toscaElement.setCapabilties(result.left().value());
332                 } else {
333                         if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
334                                 return result.right().value();
335                         }
336                 }
337                 return TitanOperationStatus.OK;
338         }
339
340         private TitanOperationStatus setResourceDerivedFromGraph(GraphVertex componentV, NodeType toscaElement) {
341                 List<String> derivedFromList = new ArrayList<String>();
342
343                 TitanOperationStatus listFromGraphStatus = findResourcesPathRecursively(componentV, derivedFromList);
344                 if (TitanOperationStatus.OK != listFromGraphStatus) {
345                         return listFromGraphStatus;
346                 }
347
348                 if (false == derivedFromList.isEmpty()) {
349                         if (derivedFromList.size() > 1) {
350                                 List<String> lastDerivedFrom = new ArrayList<String>();
351                                 lastDerivedFrom.add(derivedFromList.get(1));
352                                 toscaElement.setDerivedFrom(lastDerivedFrom);
353                                 toscaElement.setDerivedList(derivedFromList);
354                         } else {
355                                 toscaElement.setDerivedFrom(null);
356                                 toscaElement.setDerivedList(derivedFromList);
357                         }
358
359                 }
360                 return TitanOperationStatus.OK;
361         }
362
363         protected TitanOperationStatus findResourcesPathRecursively(GraphVertex nodeTypeV, List<String> resourcesPathList) {
364                 Either<GraphVertex, TitanOperationStatus> parentResourceRes = titanDao.getChildVertex(nodeTypeV, EdgeLabelEnum.DERIVED_FROM, JsonParseFlagEnum.NoParse);
365                 resourcesPathList.add((String) nodeTypeV.getMetadataProperty(GraphPropertyEnum.TOSCA_RESOURCE_NAME));
366                 while (parentResourceRes.isLeft()) {
367
368                         GraphVertex parent = parentResourceRes.left().value();
369                         resourcesPathList.add((String) parent.getMetadataProperty(GraphPropertyEnum.TOSCA_RESOURCE_NAME));
370                         parentResourceRes = titanDao.getChildVertex(parent, EdgeLabelEnum.DERIVED_FROM, JsonParseFlagEnum.NoParse);
371                 }
372                 TitanOperationStatus operationStatus = parentResourceRes.right().value();
373
374                 if (operationStatus != TitanOperationStatus.NOT_FOUND) {
375                         return operationStatus;
376                 } else {
377                         return TitanOperationStatus.OK;
378                 }
379
380         }
381
382         protected <T extends ToscaElement> TitanOperationStatus setRequirementsFromGraph(GraphVertex componentV, T toscaElement) {
383                 return setResourceRequirementsFromGraph(componentV, (NodeType) toscaElement);
384         }
385
386         private TitanOperationStatus setResourceRequirementsFromGraph(GraphVertex componentV, NodeType toscaElement) {
387                 Either<Map<String, ListRequirementDataDefinition>, TitanOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.REQUIREMENTS);
388                 if (result.isLeft()) {
389                         toscaElement.setRequirements(result.left().value());
390                 } else {
391                         if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
392                                 return result.right().value();
393                         }
394                 }
395                 return TitanOperationStatus.OK;
396         }
397
398         private TitanOperationStatus setResourceAttributesFromGraph(GraphVertex componentV, NodeType toscaElement) {
399                 Either<Map<String, PropertyDataDefinition>, TitanOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.ATTRIBUTES);
400                 if (result.isLeft()) {
401                         toscaElement.setAttributes(result.left().value());
402                 } else {
403                         if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
404                                 return result.right().value();
405                         }
406                 }
407                 return TitanOperationStatus.OK;
408         }
409
410         private TitanOperationStatus setResourcePropertiesFromGraph(GraphVertex componentV, NodeType toscaElement) {
411                 Either<Map<String, PropertyDataDefinition>, TitanOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.PROPERTIES);
412                 if (result.isLeft()) {
413                         toscaElement.setProperties(result.left().value());
414                 } else {
415                         if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
416                                 return result.right().value();
417                         }
418                 }
419                 return TitanOperationStatus.OK;
420         }
421
422         private StorageOperationStatus assosiateToDerived(GraphVertex nodeTypeVertex, List<GraphVertex> derivedResources) {
423                 for (GraphVertex derivedV : derivedResources) {
424                         TitanOperationStatus createEdge = titanDao.createEdge(nodeTypeVertex, derivedV, EdgeLabelEnum.DERIVED_FROM, null);
425                         if (createEdge != TitanOperationStatus.OK) {
426                                 log.trace("Failed to associate resource {} to derived with id {}", nodeTypeVertex.getUniqueId(), derivedV.getUniqueId());
427                                 return DaoStatusConverter.convertTitanStatusToStorageStatus(createEdge);
428                         }
429                 }
430                 return StorageOperationStatus.OK;
431         }
432
433         private StorageOperationStatus addAdditionalInformationToResource(GraphVertex nodeTypeVertex, NodeType nodeType, List<GraphVertex> derivedResources) {
434                 // Note : currently only one derived supported!!!!
435                 Either<Map<String, AdditionalInfoParameterDataDefinition>, StorageOperationStatus> dataFromDerived = getDataFromDerived(derivedResources, EdgeLabelEnum.ADDITIONAL_INFORMATION);
436                 if (dataFromDerived.isRight()) {
437                         return dataFromDerived.right().value();
438                 }
439                 Map<String, AdditionalInfoParameterDataDefinition> addInformationAll = dataFromDerived.left().value();
440
441                 Map<String, AdditionalInfoParameterDataDefinition> addInformation = nodeType.getAdditionalInformation();
442                 if (addInformation != null) {
443                         ToscaDataDefinition.mergeDataMaps(addInformationAll, addInformation);
444                 }
445                 if (!addInformationAll.isEmpty()) {
446                         Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.ADDITIONAL_INFORMATION, EdgeLabelEnum.ADDITIONAL_INFORMATION, addInformationAll);
447                         if (assosiateElementToData.isRight()) {
448                                 return assosiateElementToData.right().value();
449                         }
450                 }
451                 return StorageOperationStatus.OK;
452         }
453
454         private StorageOperationStatus associateCapabilitiesToResource(GraphVertex nodeTypeVertex, NodeType nodeType, List<GraphVertex> derivedResources) {
455                 // Note : currently only one derived supported!!!!
456                 Either<Map<String, ListCapabilityDataDefinition>, StorageOperationStatus> dataFromDerived = getDataFromDerived(derivedResources, EdgeLabelEnum.CAPABILITIES);
457                 if (dataFromDerived.isRight()) {
458                         return dataFromDerived.right().value();
459                 }
460                 Map<String, ListCapabilityDataDefinition> capabiltiesAll = dataFromDerived.left().value();
461
462                 Map<String, ListCapabilityDataDefinition> capabilties = nodeType.getCapabilties();
463                 if (capabilties != null) {
464                         if (capabiltiesAll == null) {
465                                 capabiltiesAll = new HashMap<>();
466                         }
467                         capabilties.values().forEach(l -> {
468                                 l.getListToscaDataDefinition().stream().filter(p -> p.getUniqueId() == null).forEach(p -> {
469                                         String uid = UniqueIdBuilder.buildCapabilityUid(nodeTypeVertex.getUniqueId(), p.getName());
470                                         p.setUniqueId(uid);
471                                 });
472                         });
473
474                         ToscaDataDefinition.mergeDataMaps(capabiltiesAll, capabilties);
475                 }
476                 
477                 capabiltiesAll.values().forEach(l -> {
478             l.getListToscaDataDefinition().forEach(c -> {
479                   List<String> capabilitySources = c.getCapabilitySources();
480                   if ( capabilitySources == null ){
481                         capabilitySources = new ArrayList<>();
482                   }
483                   capabilitySources.add((String) nodeType.getMetadataValue(JsonPresentationFields.TOSCA_RESOURCE_NAME));
484                   c.setCapabilitySources(capabilitySources);
485             });
486       });
487
488                 
489                 if (!capabiltiesAll.isEmpty()) {
490                         Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.CAPABILTIES, EdgeLabelEnum.CAPABILITIES, capabiltiesAll);
491                         if (assosiateElementToData.isRight()) {
492                                 return assosiateElementToData.right().value();
493                         }
494                 }
495                 return StorageOperationStatus.OK;
496         }
497
498         private StorageOperationStatus associateRequirementsToResource(GraphVertex nodeTypeVertex, NodeType nodeType, List<GraphVertex> derivedResources) {
499                 // Note : currently only one derived supported!!!!
500                 Either<Map<String, ListRequirementDataDefinition>, StorageOperationStatus> dataFromDerived = getDataFromDerived(derivedResources, EdgeLabelEnum.REQUIREMENTS);
501                 if (dataFromDerived.isRight()) {
502                         return dataFromDerived.right().value();
503                 }
504                 Map<String, ListRequirementDataDefinition> requirementsAll = dataFromDerived.left().value();
505
506                 Map<String, ListRequirementDataDefinition> requirements = nodeType.getRequirements();
507                 if (requirements != null) {
508                         if (requirementsAll == null) {
509                                 requirementsAll = new HashMap<>();
510                         }
511                         requirements.values().forEach(l -> {
512                                 l.getListToscaDataDefinition().stream().filter(p -> p.getUniqueId() == null).forEach(p -> {
513                                         String uid = UniqueIdBuilder.buildRequirementUid(nodeTypeVertex.getUniqueId(), p.getName());
514                                         p.setUniqueId(uid);
515                                 });
516                         });
517                         
518                         ToscaDataDefinition.mergeDataMaps(requirementsAll, requirements);
519
520                 }
521                 if (!requirementsAll.isEmpty()) {
522                         Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.REQUIREMENTS, EdgeLabelEnum.REQUIREMENTS, requirementsAll);
523                         if (assosiateElementToData.isRight()) {
524                                 return assosiateElementToData.right().value();
525                         }
526                 }
527                 return StorageOperationStatus.OK;
528         }
529
530         private StorageOperationStatus associateAttributesToResource(GraphVertex nodeTypeVertex, NodeType nodeType, List<GraphVertex> derivedResources) {
531                 // Note : currently only one derived supported!!!!
532                 Either<Map<String, PropertyDataDefinition>, StorageOperationStatus> dataFromDerived = getDataFromDerived(derivedResources, EdgeLabelEnum.ATTRIBUTES);
533                 if (dataFromDerived.isRight()) {
534                         return dataFromDerived.right().value();
535                 }
536                 Map<String, PropertyDataDefinition> attributesAll = dataFromDerived.left().value();
537
538                 Map<String, PropertyDataDefinition> attributes = nodeType.getAttributes();
539                 if (attributes != null) {
540                         attributes.values().stream().filter(p -> p.getUniqueId() == null).forEach(p -> {
541                                 String uid = UniqueIdBuilder.buildAttributeUid(nodeTypeVertex.getUniqueId(), p.getName());
542                                 p.setUniqueId(uid);
543                         });
544                         ToscaDataDefinition.mergeDataMaps(attributesAll, attributes);
545                 }
546                 if (!attributesAll.isEmpty()) {
547                         Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.ATTRIBUTES, EdgeLabelEnum.ATTRIBUTES, attributesAll);
548                         if (assosiateElementToData.isRight()) {
549                                 return assosiateElementToData.right().value();
550                         }
551                 }
552                 return StorageOperationStatus.OK;
553         }
554
555         // TODO get from derived
556         private StorageOperationStatus associateCapabilitiesPropertiesToResource(GraphVertex nodeTypeVertex, NodeType nodeType, List<GraphVertex> derivedResources) {
557                 // // Note : currently only one derived supported!!!!
558                 Either<Map<String, MapPropertiesDataDefinition>, StorageOperationStatus> dataFromDerived = getDataFromDerived(derivedResources, EdgeLabelEnum.CAPABILITIES_PROPERTIES);
559                 if (dataFromDerived.isRight()) {
560                         return dataFromDerived.right().value();
561                 }
562                 Map<String, MapPropertiesDataDefinition> propertiesAll = dataFromDerived.left().value();
563                 Map<String, MapPropertiesDataDefinition> capabiltiesProps = nodeType.getCapabiltiesProperties();
564                 if (capabiltiesProps != null) {
565                         capabiltiesProps.values().forEach(l -> {
566                                 if (l.getMapToscaDataDefinition() != null && l.getMapToscaDataDefinition().values() != null) {
567                                         Collection<PropertyDataDefinition> mapToscaDataDefinition = l.getMapToscaDataDefinition().values();
568                                         mapToscaDataDefinition.stream().filter(p -> p != null && p.getUniqueId() == null).forEach(p -> {
569                                                 String uid = UniqueIdBuilder.buildRequirementUid(nodeTypeVertex.getUniqueId(), p.getName());
570                                                 p.setUniqueId(uid);
571                                         });
572                                 }
573                         });
574                         ToscaDataDefinition.mergeDataMaps(propertiesAll, capabiltiesProps);
575                 }
576                 if (!propertiesAll.isEmpty()) {
577                         Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.CAPABILITIES_PROPERTIES, EdgeLabelEnum.CAPABILITIES_PROPERTIES, propertiesAll);
578                         if (assosiateElementToData.isRight()) {
579                                 return assosiateElementToData.right().value();
580                         }
581                 }
582                 return StorageOperationStatus.OK;
583         }
584
585         public Either<List<GraphVertex>, StorageOperationStatus> findDerivedResources(NodeType nodeType) {
586
587                 List<GraphVertex> derivedResources = new ArrayList<GraphVertex>();
588                 List<String> derivedFromResources = nodeType.getDerivedFrom();
589                 if (derivedFromResources != null && false == derivedFromResources.isEmpty()) {
590
591                         for (String parentResource : derivedFromResources) {
592                                 Either<List<GraphVertex>, TitanOperationStatus> getParentResources = derivedResourceResolver.findDerivedResources(parentResource);
593                                 List<GraphVertex> resources = null;
594                                 if (getParentResources.isRight()) {
595                                         log.error("Cannot find parent resource by tosca resource name {} in the graph.", parentResource);
596                                         return Either.right(StorageOperationStatus.PARENT_RESOURCE_NOT_FOUND);
597
598                                 } else {
599                                         resources = getParentResources.left().value();
600                                         if (resources == null || resources.size() == 0) {
601                                                 log.error("Cannot find parent resource by tosca name {} in the graph. resources size is empty", parentResource);
602                                                 return Either.right(StorageOperationStatus.PARENT_RESOURCE_NOT_FOUND);
603                                         } else {
604                                                 if (resources.size() > 1) {
605                                                         return handleMultipleParent(parentResource, derivedResources, resources);
606                                                 } else {
607                                                         GraphVertex parentResourceData = resources.get(0);
608                                                         derivedResources.add(parentResourceData);
609                                                 }
610                                         }
611
612                                 }
613
614                         }
615                 }
616                 return Either.left(derivedResources);
617         }
618         
619         Either<List<GraphVertex>, StorageOperationStatus> handleMultipleParent(String parentResource, List<GraphVertex> derivedResource, List<GraphVertex> fetchedDerivedResources){
620                 
621                 Either<List<GraphVertex>, StorageOperationStatus> result = Either.left(derivedResource);
622                 try{
623                         fetchedDerivedResources.sort((d1,d2)->{
624                                 return new Double(Double.parseDouble((String)d1.getMetadataProperty(GraphPropertyEnum.VERSION)))
625                                                 .compareTo(Double.parseDouble((String)d2.getMetadataProperty(GraphPropertyEnum.VERSION)));
626                         });
627                         
628                         int actualHighestIndex = fetchedDerivedResources.size() - 1;
629                         derivedResource.add(fetchedDerivedResources.get(actualHighestIndex));
630                         fetchedDerivedResources.remove(actualHighestIndex);
631                         
632                         StorageOperationStatus status = fixMultipleParent(fetchedDerivedResources);
633                         if(status != StorageOperationStatus.OK){
634                                 result = Either.right(status);
635                         }
636                 } catch (Exception e){
637                         CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Exception occured during handle multiple parent {}. Exception is  {}",
638                                         parentResource, e.getMessage());
639                         result = Either.right(StorageOperationStatus.GENERAL_ERROR);
640                 }
641                 return result;
642         }
643
644         private StorageOperationStatus fixMultipleParent(List<GraphVertex> fetchedDerivedResources) {
645                 StorageOperationStatus result = StorageOperationStatus.OK;
646                 for(GraphVertex fetchedDerivedResource : fetchedDerivedResources){
647                         fetchedDerivedResource.addMetadataProperty(GraphPropertyEnum.IS_HIGHEST_VERSION, false);
648                         Either<GraphVertex, TitanOperationStatus> updateVertexRes = titanDao.updateVertex(fetchedDerivedResource);
649                         if (updateVertexRes.isRight()) {
650                                 TitanOperationStatus titatStatus = updateVertexRes.right().value();
651                                 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to set highest version of node type {} to false. Status is  {}",
652                                                 fetchedDerivedResource.getMetadataProperty(GraphPropertyEnum.TOSCA_RESOURCE_NAME), titatStatus);
653                                 result = DaoStatusConverter.convertTitanStatusToStorageStatus(titatStatus);
654                                 break;
655                         }
656                 }
657                 return result;
658         }
659
660         private GraphVertex fillMetadata(GraphVertex nodeTypeVertex, NodeType nodeType) {
661                 nodeTypeVertex.setLabel(VertexTypeEnum.NODE_TYPE);
662
663                 fillCommonMetadata(nodeTypeVertex, nodeType);
664
665                 return nodeTypeVertex;
666         }
667
668         @Override
669         public Either<ToscaElement, StorageOperationStatus> deleteToscaElement(GraphVertex toscaElementVertex) {
670                 Either<ToscaElement, StorageOperationStatus> nodeType = getToscaElement(toscaElementVertex, new ComponentParametersView());
671                 if (nodeType.isRight()) {
672                         log.debug("Failed to fetch tosca element {} error {}", toscaElementVertex.getUniqueId(), nodeType.right().value());
673                         return nodeType;
674                 }
675                 TitanOperationStatus status = disassociateAndDeleteCommonElements(toscaElementVertex);
676                 if (status != TitanOperationStatus.OK) {
677                         Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
678                 }
679                 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.CAPABILITIES);
680                 if (status != TitanOperationStatus.OK) {
681                         log.debug("Failed to disassociate capabilties for {} error {}", toscaElementVertex.getUniqueId(), status);
682                         Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
683                 }
684                 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.CAPABILITIES_PROPERTIES);
685                 if (status != TitanOperationStatus.OK) {
686                         log.debug("Failed to disassociate capabilties properties for {} error {}", toscaElementVertex.getUniqueId(), status);
687                         Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
688                 }
689                 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.REQUIREMENTS);
690                 if (status != TitanOperationStatus.OK) {
691                         log.debug("Failed to disassociate requirements for {} error {}", toscaElementVertex.getUniqueId(), status);
692                         Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
693                 }
694                 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.ATTRIBUTES);
695                 if (status != TitanOperationStatus.OK) {
696                         log.debug("Failed to disassociate attributes for {} error {}", toscaElementVertex.getUniqueId(), status);
697                         Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
698                 }
699                 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.INTERFACE_ARTIFACTS);
700                 if (status != TitanOperationStatus.OK) {
701                         log.debug("Failed to disassociate interface artifacts for {} error {}", toscaElementVertex.getUniqueId(), status);
702                         Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
703                 }
704                 toscaElementVertex.getVertex().remove();
705                 log.trace("Tosca element vertex for {} was removed", toscaElementVertex.getUniqueId());
706
707                 return nodeType;
708         }
709
710         @SuppressWarnings("unchecked")
711         @Override
712         public Either<NodeType, StorageOperationStatus> createToscaElement(ToscaElement toscaElement) {
713                 return createNodeType((NodeType) toscaElement);
714         }
715
716         @Override
717         protected <T extends ToscaElement> TitanOperationStatus setCategoriesFromGraph(GraphVertex vertexComponent, T toscaElement) {
718                 return setResourceCategoryFromGraph(vertexComponent, toscaElement);
719         }
720
721         @Override
722         protected <T extends ToscaElement> StorageOperationStatus validateCategories(T toscaElementToUpdate, GraphVertex elementV) {
723                 return validateResourceCategory(toscaElementToUpdate, elementV);
724         }
725
726         @Override
727         protected <T extends ToscaElement> StorageOperationStatus updateDerived(T toscaElementToUpdate, GraphVertex nodeTypeV) {
728
729                 NodeType nodeType = (NodeType) toscaElementToUpdate;
730                 List<GraphVertex> derivedResources = new ArrayList<>();
731
732                 List<String> derivedFromResources = nodeType.getDerivedFrom();
733
734                 // now supported only single derived from
735                 if (derivedFromResources != null && !derivedFromResources.isEmpty() && derivedFromResources.get(0) != null) {
736                         String firstDerived = derivedFromResources.get(0);
737                         boolean derivedFromGenericType = null != nodeType.getDerivedFromGenericType();
738                         Either<GraphVertex, TitanOperationStatus> childVertex = titanDao.getChildVertex(nodeTypeV, EdgeLabelEnum.DERIVED_FROM, JsonParseFlagEnum.NoParse);
739                         if (childVertex.isRight()) {
740                                 TitanOperationStatus getchieldError = childVertex.right().value();
741                                 log.debug("Failed to fetch derived resource for element {} error {}", nodeTypeV.getUniqueId(), getchieldError);
742                                 return DaoStatusConverter.convertTitanStatusToStorageStatus(getchieldError);
743                         }
744                         GraphVertex firstDerivedInChain = childVertex.left().value();
745
746                         String firstCurrentDerived = (String) firstDerivedInChain.getMetadataProperty(GraphPropertyEnum.TOSCA_RESOURCE_NAME);
747                         if (!firstDerived.equals(firstCurrentDerived) || derivedFromGenericType) {
748
749                                 Map<GraphPropertyEnum, Object> propertiesToMatch = new HashMap<GraphPropertyEnum, Object>();
750                                 propertiesToMatch.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
751
752                                 propertiesToMatch.put(GraphPropertyEnum.TOSCA_RESOURCE_NAME, firstDerived);
753                                 propertiesToMatch.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
754
755                                 Either<List<GraphVertex>, TitanOperationStatus> getParentResources = titanDao.getByCriteria(VertexTypeEnum.NODE_TYPE, propertiesToMatch, JsonParseFlagEnum.NoParse);
756
757                                 if (getParentResources.isRight()) {
758                                         TitanOperationStatus error = getParentResources.right().value();
759                                         CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to fetch derived by criteria {}. error {} ", propertiesToMatch, error);
760                                         return DaoStatusConverter.convertTitanStatusToStorageStatus(error);
761                                 }
762                                 // must be only one
763                                 GraphVertex newDerivedV = getParentResources.left().value().get(0);
764                                 return updateDerived(toscaElementToUpdate, nodeTypeV, firstDerivedInChain, newDerivedV, false);
765                         }
766                 }
767                 return StorageOperationStatus.OK;
768         }
769
770         /**
771          * 
772          * @param toscaElementToUpdate
773          * @param nodeTypeV
774          * @param preDerivedV
775          * @param newDerivedV
776          * @param mergeValues
777          * @return
778          */
779         protected <T extends ToscaElement> StorageOperationStatus updateDerived(T toscaElementToUpdate, GraphVertex nodeTypeV, GraphVertex preDerivedV, GraphVertex newDerivedV, boolean mergeValues) {
780                 Set<String> preDerivedChainIdList = new HashSet();
781                 preDerivedChainIdList.add(preDerivedV.getUniqueId());
782                 Either<GraphVertex, TitanOperationStatus> childVertex = titanDao.getChildVertex(preDerivedV, EdgeLabelEnum.DERIVED_FROM, JsonParseFlagEnum.NoParse);
783                 while (childVertex.isLeft()) {
784                         GraphVertex currentChield = childVertex.left().value();
785                         preDerivedChainIdList.add(currentChield.getUniqueId());
786                         childVertex = titanDao.getChildVertex(currentChield, EdgeLabelEnum.DERIVED_FROM, JsonParseFlagEnum.NoParse);
787                 }
788
789                 List<GraphVertex> derivedResources = new ArrayList<>();
790                 derivedResources.add(newDerivedV);
791                 StorageOperationStatus updateStatus = updateDataFromNewDerived(derivedResources, nodeTypeV, (NodeType) toscaElementToUpdate, mergeValues, preDerivedChainIdList);
792                 if (updateStatus != StorageOperationStatus.OK) {
793                         CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to update data for {} from new derived {} ", nodeTypeV.getUniqueId(), newDerivedV.getUniqueId(), updateStatus);
794                         return updateStatus;
795                 }
796
797                 Either<Edge, TitanOperationStatus> deleteEdge = titanDao.deleteEdge(nodeTypeV, preDerivedV, EdgeLabelEnum.DERIVED_FROM);
798                 if (deleteEdge.isRight()) {
799                         TitanOperationStatus deleteError = deleteEdge.right().value();
800                         log.debug("Failed to disassociate element {} from derived {} , error {}", nodeTypeV.getUniqueId(), preDerivedV.getUniqueId(), deleteError);
801                         return DaoStatusConverter.convertTitanStatusToStorageStatus(deleteError);
802                 }
803
804                 titanDao.createEdge(nodeTypeV, newDerivedV, EdgeLabelEnum.DERIVED_FROM, new HashMap<>());
805
806                 return StorageOperationStatus.OK;
807         }
808         
809         private StorageOperationStatus associateDerivedDataByType(EdgeLabelEnum edgeLabel, GraphVertex nodeTypeV, NodeType nodeToUpdate, List<GraphVertex> newDerived) {
810                 
811                 switch (edgeLabel) {
812                 case CAPABILITIES:
813                         return associateCapabilitiesToResource(nodeTypeV, nodeToUpdate, newDerived);
814                 case REQUIREMENTS:
815                         return associateRequirementsToResource(nodeTypeV, nodeToUpdate, newDerived);
816                 case PROPERTIES:
817                         return associatePropertiesToResource(nodeTypeV, nodeToUpdate, newDerived);
818                 case ATTRIBUTES:
819                         return associateAttributesToResource(nodeTypeV, nodeToUpdate, newDerived);
820                 case ADDITIONAL_INFORMATION:
821                         return addAdditionalInformationToResource(nodeTypeV, nodeToUpdate, newDerived);
822                 case CAPABILITIES_PROPERTIES:
823                         return associateCapabilitiesPropertiesToResource(nodeTypeV, nodeToUpdate, newDerived);
824                 default:
825                         return StorageOperationStatus.OK;
826                 }
827
828         }
829
830         private StorageOperationStatus updateDataFromNewDerived(List<GraphVertex> newDerived, GraphVertex nodeTypeV, NodeType nodeToUpdate, boolean mergeValues, Set<String> preDerivedChainIdList) {
831                 EnumSet<EdgeLabelEnum> edgeLabels = EnumSet.of(EdgeLabelEnum.CAPABILITIES, EdgeLabelEnum.REQUIREMENTS, EdgeLabelEnum.PROPERTIES, EdgeLabelEnum.ATTRIBUTES, EdgeLabelEnum.CAPABILITIES_PROPERTIES, EdgeLabelEnum.ADDITIONAL_INFORMATION);
832                 StorageOperationStatus status = null;
833                 for (EdgeLabelEnum edge : edgeLabels) {
834                         status = updateDataByType(newDerived, nodeTypeV, edge, nodeToUpdate, mergeValues, preDerivedChainIdList);
835                         if (status != StorageOperationStatus.OK) {
836                                 break;
837                         }
838                 }
839                 return status;
840
841         }
842
843         private <T extends ToscaDataDefinition> StorageOperationStatus updateDataByType(List<GraphVertex> newDerivedList, GraphVertex nodeTypeV, EdgeLabelEnum label, NodeType nodeElement, boolean mergeValues, Set<String> preDerivedChainIdList) {
844                 log.debug("Update data from derived for element {} type {}", nodeTypeV.getUniqueId(), label);
845                 Either<GraphVertex, TitanOperationStatus> dataFromGraph = getDataVertex(nodeTypeV, label);
846                 if (dataFromGraph.isRight()) {
847                         if (TitanOperationStatus.NOT_FOUND == dataFromGraph.right().value())
848                                 return associateDerivedDataByType(label, nodeTypeV, nodeElement, newDerivedList);
849                         return DaoStatusConverter.convertTitanStatusToStorageStatus(dataFromGraph.right().value());
850                 }
851                 GraphVertex dataV = dataFromGraph.left().value();
852
853                 Map<String, T> mapFromGraph = (Map<String, T>) dataV.getJson();
854                 Map<String, T> valuesFrmPrev = null;
855                 if (isSimpleHierarchy(label)) {
856                         if (mergeValues) {
857                                 valuesFrmPrev = mapFromGraph.entrySet().stream().filter(e -> e.getValue().getOwnerId() != null).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
858                         }
859                         mapFromGraph.entrySet().removeIf(e -> preDerivedChainIdList.contains(e.getValue().getOwnerId()));
860                 } else {
861                         final Map<String, T> valuesFrmPrevFinal = new HashMap<>();
862                         mapFromGraph.entrySet().stream().forEach(e -> {
863                                 T value = e.getValue();
864                                 value = ToscaDataDefinition.removeAndCollectByOwnerId(value, preDerivedChainIdList);
865                                 valuesFrmPrevFinal.put(e.getKey(), value);
866                         });
867                         valuesFrmPrev = valuesFrmPrevFinal;
868                         mapFromGraph.entrySet().removeIf(e->e.getValue().isEmpty());
869                 }
870
871                 Either<Map<String, T>, StorageOperationStatus> dataFromDerived = getDataFromDerived(newDerivedList, label);
872                 if (dataFromDerived.isRight()) {
873                         return dataFromDerived.right().value();
874                 }
875                 Map<String, T> dataFromDerivedAll = dataFromDerived.left().value();
876                 
877                 Either<Map<String, T>, String> merged = ToscaDataDefinition.mergeDataMaps(dataFromDerivedAll, mapFromGraph);
878                 if(merged.isRight()){
879                         log.debug("property {} cannot be overriden", merged.right().value());
880                         return StorageOperationStatus.INVALID_PROPERTY;
881                 }
882                 dataV.setJson(dataFromDerivedAll);
883                 Either<GraphVertex, TitanOperationStatus> updateDataV = updateOrCopyOnUpdate(dataV, nodeTypeV, label);
884                 if (updateDataV.isRight()) {
885                         return DaoStatusConverter.convertTitanStatusToStorageStatus(updateDataV.right().value());
886                 }
887                 return StorageOperationStatus.OK;
888         }
889         
890                 private boolean isSimpleHierarchy(EdgeLabelEnum label) {
891                 switch (label) {
892                 case PROPERTIES:
893                 case ATTRIBUTES:
894                 case ADDITIONAL_INFORMATION:
895                 case ARTIFACTS:
896                 case GROUPS:
897                 case INPUTS:
898                         return true;
899                 default:
900                         return false;
901                 }
902         }
903
904         
905         @Override
906         public <T extends ToscaElement> void fillToscaElementVertexData(GraphVertex elementV, T toscaElementToUpdate, JsonParseFlagEnum flag) {
907                 fillMetadata(elementV, (NodeType) toscaElementToUpdate);
908         }
909
910         public Either<ToscaElement, StorageOperationStatus> shouldUpdateDerivedVersion(ToscaElement toscaElementToUpdate, GraphVertex nodeTypeV) {
911                 NodeType nodeType = (NodeType) toscaElementToUpdate;
912
913                 Either<GraphVertex, TitanOperationStatus> childVertex = titanDao.getChildVertex(nodeTypeV, EdgeLabelEnum.DERIVED_FROM, JsonParseFlagEnum.NoParse);
914                 if (childVertex.isRight()) {
915                         TitanOperationStatus getchildError = childVertex.right().value();
916                         if (getchildError == TitanOperationStatus.NOT_FOUND) {
917                                 log.debug("derived resource for element {} not found", nodeTypeV.getUniqueId());
918                                 return Either.right(StorageOperationStatus.OK);
919                         }
920
921                         log.debug("Failed to fetch derived resource for element {} error {}", nodeTypeV.getUniqueId(), getchildError);
922                         return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(getchildError));
923                 }
924                 GraphVertex firstDerivedInChain = childVertex.left().value();
925
926                 String currentVersion = (String) firstDerivedInChain.getMetadataProperty(GraphPropertyEnum.VERSION);
927
928                 Map<GraphPropertyEnum, Object> props = new HashMap<>();
929                 props.put(GraphPropertyEnum.TOSCA_RESOURCE_NAME, nodeType.getDerivedFrom().get(0));
930                 props.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
931                 props.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
932
933                 Map<GraphPropertyEnum, Object> propsHasNot = new HashMap<>();
934                 propsHasNot.put(GraphPropertyEnum.IS_DELETED, true);
935                 Either<List<GraphVertex>, TitanOperationStatus> byCriteria = titanDao.getByCriteria(VertexTypeEnum.NODE_TYPE, props, propsHasNot, JsonParseFlagEnum.NoParse);
936                 if (byCriteria.isRight()) {
937                         log.debug("Failed to fetch derived by props {} error {}", props, byCriteria.right().value());
938                         return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(byCriteria.right().value()));
939                 }
940                 List<GraphVertex> lastDerived = byCriteria.left().value();
941                 // now supported only one derived!!! Change in future!(Evg)
942                 GraphVertex derivedFromHighest = lastDerived.get(0);
943                 String highestVersion = (String) derivedFromHighest.getMetadataProperty(GraphPropertyEnum.VERSION);
944                 if (!highestVersion.equals(currentVersion)) {
945
946                         // need to update to latest version of derived from
947                         StorageOperationStatus updateDerived = updateDerived(toscaElementToUpdate, nodeTypeV, firstDerivedInChain, derivedFromHighest, true);
948
949                         if (updateDerived != StorageOperationStatus.OK) {
950                                 log.debug("Failed to update {} to highest derived {} from error {}", nodeTypeV.getUniqueId(), derivedFromHighest.getUniqueId(), updateDerived);
951                                 return Either.right(updateDerived);
952                         }
953                         return getToscaElement(nodeTypeV.getUniqueId(), new ComponentParametersView());
954                 }
955                 // no version changes
956                 return Either.right(StorageOperationStatus.OK);
957         }
958
959 }