2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.be.model.jsontitan.operations;
23 import fj.data.Either;
24 import java.util.stream.Collectors;
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;
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;
66 import java.util.regex.Pattern;
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");
73 private static Logger log = LoggerFactory.getLogger(NodeTypeOperation.class.getName());
75 private DerivedNodeTypeResolver derivedResourceResolver;
77 public NodeTypeOperation(@Qualifier("derived-resource-resolver") DerivedNodeTypeResolver derivedNodeTypeResolver) {
78 this.derivedResourceResolver = derivedNodeTypeResolver;
81 public Either<NodeType, StorageOperationStatus> createNodeType(NodeType nodeType) {
83 Either<NodeType, StorageOperationStatus> result = null;
85 nodeType.generateUUID();
87 nodeType = getResourceMetaDataFromResource(nodeType);
88 String resourceUniqueId = nodeType.getUniqueId();
89 if (resourceUniqueId == null) {
90 resourceUniqueId = UniqueIdBuilder.buildResourceUniqueId();
91 nodeType.setUniqueId(resourceUniqueId);
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());
101 derivedResources = derivedResourcesResult.left().value();
104 GraphVertex nodeTypeVertex = new GraphVertex(VertexTypeEnum.NODE_TYPE);
105 fillToscaElementVertexData(nodeTypeVertex, nodeType, JsonParseFlagEnum.ParseAll);
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));
114 nodeTypeVertex = createdVertex.left().value();
116 StorageOperationStatus assosiateCommon = assosiateCommonForToscaElement(nodeTypeVertex, nodeType, derivedResources);
117 if (assosiateCommon != StorageOperationStatus.OK) {
118 result = Either.right(assosiateCommon);
122 StorageOperationStatus associateDerived = assosiateToDerived(nodeTypeVertex, derivedResources);
123 if (associateDerived != StorageOperationStatus.OK) {
124 result = Either.right(associateDerived);
127 StorageOperationStatus associateCategory = assosiateResourceMetadataToCategory(nodeTypeVertex, nodeType);
128 if (associateCategory != StorageOperationStatus.OK) {
129 result = Either.right(associateCategory);
133 StorageOperationStatus associateAttributes = associateAttributesToResource(nodeTypeVertex, nodeType, derivedResources);
134 if (associateAttributes != StorageOperationStatus.OK) {
135 result = Either.right(associateAttributes);
139 StorageOperationStatus associateRequirements = associateRequirementsToResource(nodeTypeVertex, nodeType, derivedResources);
140 if (associateRequirements != StorageOperationStatus.OK) {
141 result = Either.right(associateRequirements);
145 StorageOperationStatus associateCapabilities = associateCapabilitiesToResource(nodeTypeVertex, nodeType, derivedResources);
146 if (associateCapabilities != StorageOperationStatus.OK) {
147 result = Either.right(associateCapabilities);
150 StorageOperationStatus associateCapabilitiesProps = associateCapabilitiesPropertiesToResource(nodeTypeVertex, nodeType, derivedResources);
151 if (associateCapabilitiesProps != StorageOperationStatus.OK) {
152 result = Either.right(associateCapabilitiesProps);
156 StorageOperationStatus associateInterfaces = associateInterfacesToResource(nodeTypeVertex, nodeType, derivedResources);
157 if (associateInterfaces != StorageOperationStatus.OK) {
158 result = Either.right(associateInterfaces);
162 StorageOperationStatus addAdditionalInformation = addAdditionalInformationToResource(nodeTypeVertex, nodeType, derivedResources);
163 if (addAdditionalInformation != StorageOperationStatus.OK) {
164 result = Either.right(addAdditionalInformation);
167 result = Either.left(nodeType);
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();
178 Map<String, InterfaceDataDefinition> interfacArtsAll = dataFromDerived.left().value();
180 Map<String, InterfaceDataDefinition> interfacArts = nodeType.getInterfaceArtifacts();
181 if (interfacArts != null) {
182 interfacArtsAll.putAll(interfacArts);
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();
190 return StorageOperationStatus.OK;
194 public Either<ToscaElement, StorageOperationStatus> getToscaElement(String uniqueId, ComponentParametersView componentParametersView) {
196 Either<GraphVertex, StorageOperationStatus> componentByLabelAndId = getComponentByLabelAndId(uniqueId, ToscaElementTypeEnum.NodeType, JsonParseFlagEnum.ParseMetadata);
197 if (componentByLabelAndId.isRight()) {
198 return Either.right(componentByLabelAndId.right().value());
200 GraphVertex componentV = componentByLabelAndId.left().value();
202 return getToscaElement(componentV, componentParametersView);
206 // -------------------------------------------------------------
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));
218 status = setLastModifierFromGraph(componentV, toscaElement);
219 if (status != TitanOperationStatus.OK) {
220 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
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));
231 if (false == componentParametersView.isIgnoreAttributesFrom()) {
232 status = setResourceAttributesFromGraph(componentV, toscaElement);
233 if (status != TitanOperationStatus.OK) {
234 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
238 if (false == componentParametersView.isIgnoreDerivedFrom()) {
239 status = setResourceDerivedFromGraph(componentV, toscaElement);
240 if (status != TitanOperationStatus.OK) {
241 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
245 if (false == componentParametersView.isIgnoreCategories()) {
246 status = setResourceCategoryFromGraph(componentV, toscaElement);
247 if (status != TitanOperationStatus.OK) {
248 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
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));
258 if (false == componentParametersView.isIgnoreCapabilities()) {
259 status = setResourceCapabilitiesFromGraph(componentV, toscaElement);
260 if (status != TitanOperationStatus.OK) {
261 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
265 if (false == componentParametersView.isIgnoreArtifacts()) {
266 status = setArtifactsFromGraph(componentV, toscaElement);
267 if (status != TitanOperationStatus.OK) {
268 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
271 if (false == componentParametersView.isIgnoreAdditionalInformation()) {
272 status = setAdditionalInformationFromGraph(componentV, toscaElement);
273 if (status != TitanOperationStatus.OK) {
274 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
277 if (false == componentParametersView.isIgnoreInterfaces()) {
278 status = setInterfacesFromGraph(componentV, toscaElement);
279 if (status != TitanOperationStatus.OK) {
280 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
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));
290 if (false == componentParametersView.isIgnoreCapabiltyProperties()) {
291 status = setComponentCapPropertiesFromGraph(componentV, toscaElement);
292 if (status != TitanOperationStatus.OK) {
293 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
297 return Either.left(toscaElement);
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());
305 if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
306 return result.right().value();
309 return TitanOperationStatus.OK;
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());
317 if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
318 return result.right().value();
321 return TitanOperationStatus.OK;
324 protected <T extends ToscaElement> TitanOperationStatus setCapabilitiesFromGraph(GraphVertex componentV, T toscaElement) {
325 return setResourceCapabilitiesFromGraph(componentV, (NodeType) toscaElement);
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());
333 if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
334 return result.right().value();
337 return TitanOperationStatus.OK;
340 private TitanOperationStatus setResourceDerivedFromGraph(GraphVertex componentV, NodeType toscaElement) {
341 List<String> derivedFromList = new ArrayList<String>();
343 TitanOperationStatus listFromGraphStatus = findResourcesPathRecursively(componentV, derivedFromList);
344 if (TitanOperationStatus.OK != listFromGraphStatus) {
345 return listFromGraphStatus;
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);
355 toscaElement.setDerivedFrom(null);
356 toscaElement.setDerivedList(derivedFromList);
360 return TitanOperationStatus.OK;
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()) {
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);
372 TitanOperationStatus operationStatus = parentResourceRes.right().value();
374 if (operationStatus != TitanOperationStatus.NOT_FOUND) {
375 return operationStatus;
377 return TitanOperationStatus.OK;
382 protected <T extends ToscaElement> TitanOperationStatus setRequirementsFromGraph(GraphVertex componentV, T toscaElement) {
383 return setResourceRequirementsFromGraph(componentV, (NodeType) toscaElement);
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());
391 if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
392 return result.right().value();
395 return TitanOperationStatus.OK;
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());
403 if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
404 return result.right().value();
407 return TitanOperationStatus.OK;
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());
415 if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
416 return result.right().value();
419 return TitanOperationStatus.OK;
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);
430 return StorageOperationStatus.OK;
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();
439 Map<String, AdditionalInfoParameterDataDefinition> addInformationAll = dataFromDerived.left().value();
441 Map<String, AdditionalInfoParameterDataDefinition> addInformation = nodeType.getAdditionalInformation();
442 if (addInformation != null) {
443 ToscaDataDefinition.mergeDataMaps(addInformationAll, addInformation);
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();
451 return StorageOperationStatus.OK;
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();
460 Map<String, ListCapabilityDataDefinition> capabiltiesAll = dataFromDerived.left().value();
462 Map<String, ListCapabilityDataDefinition> capabilties = nodeType.getCapabilties();
463 if (capabilties != null) {
464 if (capabiltiesAll == null) {
465 capabiltiesAll = new HashMap<>();
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());
474 ToscaDataDefinition.mergeDataMaps(capabiltiesAll, capabilties);
477 capabiltiesAll.values().forEach(l -> {
478 l.getListToscaDataDefinition().forEach(c -> {
479 List<String> capabilitySources = c.getCapabilitySources();
480 if ( capabilitySources == null ){
481 capabilitySources = new ArrayList<>();
483 capabilitySources.add((String) nodeType.getMetadataValue(JsonPresentationFields.TOSCA_RESOURCE_NAME));
484 c.setCapabilitySources(capabilitySources);
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();
495 return StorageOperationStatus.OK;
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();
504 Map<String, ListRequirementDataDefinition> requirementsAll = dataFromDerived.left().value();
506 Map<String, ListRequirementDataDefinition> requirements = nodeType.getRequirements();
507 if (requirements != null) {
508 if (requirementsAll == null) {
509 requirementsAll = new HashMap<>();
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());
518 ToscaDataDefinition.mergeDataMaps(requirementsAll, requirements);
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();
527 return StorageOperationStatus.OK;
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();
536 Map<String, PropertyDataDefinition> attributesAll = dataFromDerived.left().value();
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());
544 ToscaDataDefinition.mergeDataMaps(attributesAll, attributes);
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();
552 return StorageOperationStatus.OK;
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();
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());
574 ToscaDataDefinition.mergeDataMaps(propertiesAll, capabiltiesProps);
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();
582 return StorageOperationStatus.OK;
585 public Either<List<GraphVertex>, StorageOperationStatus> findDerivedResources(NodeType nodeType) {
587 List<GraphVertex> derivedResources = new ArrayList<GraphVertex>();
588 List<String> derivedFromResources = nodeType.getDerivedFrom();
589 if (derivedFromResources != null && false == derivedFromResources.isEmpty()) {
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);
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);
604 if (resources.size() > 1) {
605 return handleMultipleParent(parentResource, derivedResources, resources);
607 GraphVertex parentResourceData = resources.get(0);
608 derivedResources.add(parentResourceData);
616 return Either.left(derivedResources);
619 Either<List<GraphVertex>, StorageOperationStatus> handleMultipleParent(String parentResource, List<GraphVertex> derivedResource, List<GraphVertex> fetchedDerivedResources){
621 Either<List<GraphVertex>, StorageOperationStatus> result = Either.left(derivedResource);
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)));
628 int actualHighestIndex = fetchedDerivedResources.size() - 1;
629 derivedResource.add(fetchedDerivedResources.get(actualHighestIndex));
630 fetchedDerivedResources.remove(actualHighestIndex);
632 StorageOperationStatus status = fixMultipleParent(fetchedDerivedResources);
633 if(status != StorageOperationStatus.OK){
634 result = Either.right(status);
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);
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);
660 private GraphVertex fillMetadata(GraphVertex nodeTypeVertex, NodeType nodeType) {
661 nodeTypeVertex.setLabel(VertexTypeEnum.NODE_TYPE);
663 fillCommonMetadata(nodeTypeVertex, nodeType);
665 return nodeTypeVertex;
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());
675 TitanOperationStatus status = disassociateAndDeleteCommonElements(toscaElementVertex);
676 if (status != TitanOperationStatus.OK) {
677 Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
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));
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));
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));
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));
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));
704 toscaElementVertex.getVertex().remove();
705 log.trace("Tosca element vertex for {} was removed", toscaElementVertex.getUniqueId());
710 @SuppressWarnings("unchecked")
712 public Either<NodeType, StorageOperationStatus> createToscaElement(ToscaElement toscaElement) {
713 return createNodeType((NodeType) toscaElement);
717 protected <T extends ToscaElement> TitanOperationStatus setCategoriesFromGraph(GraphVertex vertexComponent, T toscaElement) {
718 return setResourceCategoryFromGraph(vertexComponent, toscaElement);
722 protected <T extends ToscaElement> StorageOperationStatus validateCategories(T toscaElementToUpdate, GraphVertex elementV) {
723 return validateResourceCategory(toscaElementToUpdate, elementV);
727 protected <T extends ToscaElement> StorageOperationStatus updateDerived(T toscaElementToUpdate, GraphVertex nodeTypeV) {
729 NodeType nodeType = (NodeType) toscaElementToUpdate;
730 List<GraphVertex> derivedResources = new ArrayList<>();
732 List<String> derivedFromResources = nodeType.getDerivedFrom();
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);
744 GraphVertex firstDerivedInChain = childVertex.left().value();
746 String firstCurrentDerived = (String) firstDerivedInChain.getMetadataProperty(GraphPropertyEnum.TOSCA_RESOURCE_NAME);
747 if (!firstDerived.equals(firstCurrentDerived) || derivedFromGenericType) {
749 Map<GraphPropertyEnum, Object> propertiesToMatch = new HashMap<GraphPropertyEnum, Object>();
750 propertiesToMatch.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
752 propertiesToMatch.put(GraphPropertyEnum.TOSCA_RESOURCE_NAME, firstDerived);
753 propertiesToMatch.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
755 Either<List<GraphVertex>, TitanOperationStatus> getParentResources = titanDao.getByCriteria(VertexTypeEnum.NODE_TYPE, propertiesToMatch, JsonParseFlagEnum.NoParse);
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);
763 GraphVertex newDerivedV = getParentResources.left().value().get(0);
764 return updateDerived(toscaElementToUpdate, nodeTypeV, firstDerivedInChain, newDerivedV, false);
767 return StorageOperationStatus.OK;
772 * @param toscaElementToUpdate
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);
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);
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);
804 titanDao.createEdge(nodeTypeV, newDerivedV, EdgeLabelEnum.DERIVED_FROM, new HashMap<>());
806 return StorageOperationStatus.OK;
809 private StorageOperationStatus associateDerivedDataByType(EdgeLabelEnum edgeLabel, GraphVertex nodeTypeV, NodeType nodeToUpdate, List<GraphVertex> newDerived) {
813 return associateCapabilitiesToResource(nodeTypeV, nodeToUpdate, newDerived);
815 return associateRequirementsToResource(nodeTypeV, nodeToUpdate, newDerived);
817 return associatePropertiesToResource(nodeTypeV, nodeToUpdate, newDerived);
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);
825 return StorageOperationStatus.OK;
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) {
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());
851 GraphVertex dataV = dataFromGraph.left().value();
853 Map<String, T> mapFromGraph = (Map<String, T>) dataV.getJson();
854 Map<String, T> valuesFrmPrev = null;
855 if (isSimpleHierarchy(label)) {
857 valuesFrmPrev = mapFromGraph.entrySet().stream().filter(e -> e.getValue().getOwnerId() != null).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
859 mapFromGraph.entrySet().removeIf(e -> preDerivedChainIdList.contains(e.getValue().getOwnerId()));
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);
867 valuesFrmPrev = valuesFrmPrevFinal;
868 mapFromGraph.entrySet().removeIf(e->e.getValue().isEmpty());
871 Either<Map<String, T>, StorageOperationStatus> dataFromDerived = getDataFromDerived(newDerivedList, label);
872 if (dataFromDerived.isRight()) {
873 return dataFromDerived.right().value();
875 Map<String, T> dataFromDerivedAll = dataFromDerived.left().value();
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;
882 dataV.setJson(dataFromDerivedAll);
883 Either<GraphVertex, TitanOperationStatus> updateDataV = updateOrCopyOnUpdate(dataV, nodeTypeV, label);
884 if (updateDataV.isRight()) {
885 return DaoStatusConverter.convertTitanStatusToStorageStatus(updateDataV.right().value());
887 return StorageOperationStatus.OK;
890 private boolean isSimpleHierarchy(EdgeLabelEnum label) {
894 case ADDITIONAL_INFORMATION:
906 public <T extends ToscaElement> void fillToscaElementVertexData(GraphVertex elementV, T toscaElementToUpdate, JsonParseFlagEnum flag) {
907 fillMetadata(elementV, (NodeType) toscaElementToUpdate);
910 public Either<ToscaElement, StorageOperationStatus> shouldUpdateDerivedVersion(ToscaElement toscaElementToUpdate, GraphVertex nodeTypeV) {
911 NodeType nodeType = (NodeType) toscaElementToUpdate;
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);
921 log.debug("Failed to fetch derived resource for element {} error {}", nodeTypeV.getUniqueId(), getchildError);
922 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(getchildError));
924 GraphVertex firstDerivedInChain = childVertex.left().value();
926 String currentVersion = (String) firstDerivedInChain.getMetadataProperty(GraphPropertyEnum.VERSION);
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);
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()));
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)) {
946 // need to update to latest version of derived from
947 StorageOperationStatus updateDerived = updateDerived(toscaElementToUpdate, nodeTypeV, firstDerivedInChain, derivedFromHighest, true);
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);
953 return getToscaElement(nodeTypeV.getUniqueId(), new ComponentParametersView());
955 // no version changes
956 return Either.right(StorageOperationStatus.OK);