diff options
Diffstat (limited to 'catalog-model/src')
3 files changed, 72 insertions, 25 deletions
diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/CapabilityTypeOperation.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/CapabilityTypeOperation.java index f4073baa3d..ff4dc0aabd 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/CapabilityTypeOperation.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/CapabilityTypeOperation.java @@ -114,10 +114,12 @@ public class CapabilityTypeOperation extends AbstractOperation implements ICapab public Either<CapabilityTypeDefinition, StorageOperationStatus> validateUpdateProperties(CapabilityTypeDefinition capabilityTypeDefinition) { JanusGraphOperationStatus error = null; if (MapUtils.isNotEmpty(capabilityTypeDefinition.getProperties()) && capabilityTypeDefinition.getDerivedFrom() != null) { - final Either<CapabilityTypeData, JanusGraphOperationStatus> derivedFromNode = janusGraphGenericDao.getNode(GraphPropertiesDictionary.TYPE.getProperty(), + final Either<CapabilityTypeData, JanusGraphOperationStatus> derivedFromNode = janusGraphGenericDao.getNode( + GraphPropertiesDictionary.TYPE.getProperty(), capabilityTypeDefinition.getDerivedFrom(), CapabilityTypeData.class, capabilityTypeDefinition.getModel()); if (derivedFromNode.isRight()) { - log.error(BUSINESS_PROCESS_ERROR, "Failed to find the derived from type for {}. status is {}", capabilityTypeDefinition.getType(), derivedFromNode.right().value()); + log.error(BUSINESS_PROCESS_ERROR, "Failed to find the derived from type for {}. status is {}", capabilityTypeDefinition.getType(), + derivedFromNode.right().value()); return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(derivedFromNode.right().value())); } Either<Map<String, PropertyDefinition>, JanusGraphOperationStatus> allPropertiesRes = getAllCapabilityTypePropertiesFromAllDerivedFrom( @@ -178,15 +180,15 @@ public class CapabilityTypeOperation extends AbstractOperation implements ICapab log.error("Failed add properties {} to capability {}", propertiesMap, capabilityTypeDefinition.getType()); return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(addPropertiesToCapablityType.right().value())); } - + final Either<GraphRelation, StorageOperationStatus> modelRelationship = addCapabilityTypeToModel(capabilityTypeDefinition); if (modelRelationship.isRight()) { return Either.right(modelRelationship.right().value()); - } - + } + return addDerivedFromRelation(capabilityTypeDefinition, ctUniqueId).left().map(updatedDerivedFrom -> createCTResult.left().value()); } - + private Either<GraphRelation, StorageOperationStatus> addCapabilityTypeToModel(final CapabilityTypeDefinition capabilityTypeDefinition) { final String model = capabilityTypeDefinition.getModel(); if (model == null) { @@ -195,7 +197,8 @@ public class CapabilityTypeOperation extends AbstractOperation implements ICapab final GraphNode from = new UniqueIdData(NodeTypeEnum.Model, UniqueIdBuilder.buildModelUid(model)); final GraphNode to = new UniqueIdData(NodeTypeEnum.CapabilityType, capabilityTypeDefinition.getUniqueId()); log.info("Connecting model {} to type {}", from, to); - return janusGraphGenericDao.createRelation(from , to, GraphEdgeLabels.MODEL_ELEMENT, Collections.emptyMap()).right().map(DaoStatusConverter::convertJanusGraphStatusToStorageStatus); + return janusGraphGenericDao.createRelation(from, to, GraphEdgeLabels.MODEL_ELEMENT, Collections.emptyMap()).right() + .map(DaoStatusConverter::convertJanusGraphStatusToStorageStatus); } private CapabilityTypeData buildCapabilityTypeData(CapabilityTypeDefinition capabilityTypeDefinition, String ctUniqueId) { @@ -237,7 +240,7 @@ public class CapabilityTypeOperation extends AbstractOperation implements ICapab // Optimization: In case of Capability Type its unique ID is the same as type return getCapabilityTypeByUid(capabilityType); } - + public Either<CapabilityTypeDefinition, JanusGraphOperationStatus> getCapabilityTypeByType(final String capabilityType, final String model) { final Either<CapabilityTypeData, JanusGraphOperationStatus> capabilityTypesRes = janusGraphGenericDao .getNode(GraphPropertiesDictionary.TYPE.getProperty(), capabilityType, CapabilityTypeData.class, model); @@ -256,7 +259,6 @@ public class CapabilityTypeOperation extends AbstractOperation implements ICapab * @return */ public Either<CapabilityTypeDefinition, JanusGraphOperationStatus> getCapabilityTypeByUid(String uniqueId) { - Either<CapabilityTypeDefinition, JanusGraphOperationStatus> result = null; Either<CapabilityTypeData, JanusGraphOperationStatus> capabilityTypesRes = janusGraphGenericDao .getNode(UniqueIdBuilder.getKeyByNodeType(NodeTypeEnum.CapabilityType), uniqueId, CapabilityTypeData.class); if (capabilityTypesRes.isRight()) { @@ -266,7 +268,7 @@ public class CapabilityTypeOperation extends AbstractOperation implements ICapab } return getCapabilityTypeDefinition(capabilityTypesRes.left().value()); } - + private Either<CapabilityTypeDefinition, JanusGraphOperationStatus> getCapabilityTypeDefinition(final CapabilityTypeData ctData) { CapabilityTypeDefinition capabilityTypeDefinition = new CapabilityTypeDefinition(ctData.getCapabilityTypeDataDefinition()); Either<Map<String, PropertyDefinition>, JanusGraphOperationStatus> propertiesStatus = OperationUtils @@ -285,7 +287,8 @@ public class CapabilityTypeOperation extends AbstractOperation implements ICapab if (parentNode.isRight()) { JanusGraphOperationStatus janusGraphOperationStatus = parentNode.right().value(); if (janusGraphOperationStatus != JanusGraphOperationStatus.NOT_FOUND) { - log.error(BUSINESS_PROCESS_ERROR, "Failed to find the parent capability of capability type {}. status is {}", ctData.getUniqueId(), janusGraphOperationStatus); + log.error(BUSINESS_PROCESS_ERROR, "Failed to find the parent capability of capability type {}. status is {}", ctData.getUniqueId(), + janusGraphOperationStatus); return Either.right(janusGraphOperationStatus); } } else { @@ -294,14 +297,14 @@ public class CapabilityTypeOperation extends AbstractOperation implements ICapab CapabilityTypeData parentCT = immutablePair.getKey(); capabilityTypeDefinition.setDerivedFrom(parentCT.getCapabilityTypeDataDefinition().getType()); } - + final Either<ImmutablePair<ModelData, GraphEdge>, JanusGraphOperationStatus> model = janusGraphGenericDao.getParentNode( - UniqueIdBuilder.getKeyByNodeType(NodeTypeEnum.CapabilityType), ctData.getUniqueId(), GraphEdgeLabels.MODEL_ELEMENT, + UniqueIdBuilder.getKeyByNodeType(NodeTypeEnum.CapabilityType), ctData.getUniqueId(), GraphEdgeLabels.MODEL_ELEMENT, NodeTypeEnum.Model, ModelData.class); if (model.isLeft()) { capabilityTypeDefinition.setModel(model.left().value().getLeft().getName()); } - + return Either.left(capabilityTypeDefinition); } diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/DataTypeOperation.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/DataTypeOperation.java index a0963c9030..f75e3cfb17 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/DataTypeOperation.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/DataTypeOperation.java @@ -24,17 +24,22 @@ import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Optional; import org.apache.commons.collections.CollectionUtils; import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource; import org.apache.tinkerpop.gremlin.structure.Vertex; import org.janusgraph.core.JanusGraph; import org.openecomp.sdc.be.config.BeEcompErrorManager; import org.openecomp.sdc.be.config.BeEcompErrorManager.ErrorSeverity; +import org.openecomp.sdc.be.dao.api.ActionStatus; import org.openecomp.sdc.be.dao.janusgraph.HealingJanusGraphGenericDao; import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus; import org.openecomp.sdc.be.dao.neo4j.GraphEdgeLabels; import org.openecomp.sdc.be.dao.neo4j.GraphPropertiesDictionary; +import org.openecomp.sdc.be.datatypes.elements.DataTypeDataDefinition; import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum; +import org.openecomp.sdc.be.model.jsonjanusgraph.operations.exception.OperationException; +import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus; import org.openecomp.sdc.be.resources.data.DataTypeData; import org.openecomp.sdc.common.log.enums.EcompLoggerErrorCode; import org.slf4j.Logger; @@ -71,38 +76,38 @@ public class DataTypeOperation extends AbstractOperation { } final List<DataTypeData> allDataTypeNodesWithModel = getAllDataTypesWithModel(); - if(CollectionUtils.isNotEmpty(allDataTypeNodesWithModel)) { + if (CollectionUtils.isNotEmpty(allDataTypeNodesWithModel)) { dataTypesFound.addAll(allDataTypeNodesWithModel); } return dataTypesFound; } - + public Map<String, List<String>> getAllDataTypeUidsToModels() { final Map<String, List<String>> dataTypesFound = new HashMap<>(); final Either<List<DataTypeData>, JanusGraphOperationStatus> getAllDataTypesWithNullModel = janusGraphGenericDao.getByCriteria(NodeTypeEnum.DataType, null, DataTypeData.class); final var dataTypesValidated = validateDataType(getAllDataTypesWithNullModel, null); - - for (DataTypeData dataType: dataTypesValidated) { - if (!dataTypesFound.containsKey(dataType.getUniqueId())){ + + for (DataTypeData dataType : dataTypesValidated) { + if (!dataTypesFound.containsKey(dataType.getUniqueId())) { dataTypesFound.put(dataType.getUniqueId(), new ArrayList<>()); } dataTypesFound.get(dataType.getUniqueId()).add(null); } - + modelOperation.findAllModels() .forEach(model -> { - for (DataTypeData dataType: getAllDataTypesWithModel(model.getName())) { - if (!dataTypesFound.containsKey(dataType.getUniqueId())){ + for (DataTypeData dataType : getAllDataTypesWithModel(model.getName())) { + if (!dataTypesFound.containsKey(dataType.getUniqueId())) { dataTypesFound.put(dataType.getUniqueId(), new ArrayList<>()); } dataTypesFound.get(dataType.getUniqueId()).add(model.getName()); } - }); + }); return dataTypesFound; } - + private List<DataTypeData> getAllDataTypesWithModel(final String modelName) { final Either<List<DataTypeData>, JanusGraphOperationStatus> getAllDataTypesByModel = janusGraphGenericDao .getByCriteriaForModel(NodeTypeEnum.DataType, null, modelName, DataTypeData.class); @@ -129,7 +134,7 @@ public class DataTypeOperation extends AbstractOperation { if (getDataTypes.isRight()) { final var status = getDataTypes.right().value(); if (LOGGER.isErrorEnabled()) { - final var errorMsg= String.format("Failed to fetch data types from database with model %s. Status is %s", modelName, status); + final var errorMsg = String.format("Failed to fetch data types from database with model %s. Status is %s", modelName, status); LOGGER.error(String.valueOf(EcompLoggerErrorCode.UNKNOWN_ERROR), DataTypeOperation.class.getName(), errorMsg); BeEcompErrorManager.getInstance().logInternalConnectionError(DataTypeOperation.class.getName(), errorMsg, ErrorSeverity.ERROR); } @@ -152,4 +157,19 @@ public class DataTypeOperation extends AbstractOperation { }); } + public Optional<DataTypeDataDefinition> getDataTypeByUid(final String uniqueId) { + final Either<DataTypeData, JanusGraphOperationStatus> dataTypeEither = janusGraphGenericDao + .getNode(UniqueIdBuilder.getKeyByNodeType(NodeTypeEnum.DataType), uniqueId, DataTypeData.class); + if (dataTypeEither.isRight()) { + if (JanusGraphOperationStatus.NOT_FOUND.equals(dataTypeEither.right().value())) { + return Optional.empty(); + } + final StorageOperationStatus storageOperationStatus + = DaoStatusConverter.convertJanusGraphStatusToStorageStatus(dataTypeEither.right().value()); + LOGGER.warn("Failed to fetch data type '{}' from JanusGraph. Status is: {}", uniqueId, storageOperationStatus); + throw new OperationException(ActionStatus.GENERAL_ERROR, + String.format("Failed to fetch data type '%s' from JanusGraph. Status is: %s", uniqueId, storageOperationStatus)); + } + return Optional.of(dataTypeEither.left().value().getDataTypeDataDefinition()); + } } diff --git a/catalog-model/src/test/java/org/openecomp/sdc/be/model/operations/impl/DataTypeOperationTest.java b/catalog-model/src/test/java/org/openecomp/sdc/be/model/operations/impl/DataTypeOperationTest.java index 1580fd34b8..0efb751124 100644 --- a/catalog-model/src/test/java/org/openecomp/sdc/be/model/operations/impl/DataTypeOperationTest.java +++ b/catalog-model/src/test/java/org/openecomp/sdc/be/model/operations/impl/DataTypeOperationTest.java @@ -19,6 +19,11 @@ package org.openecomp.sdc.be.model.operations.impl; import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.when; import fj.data.Either; @@ -27,6 +32,7 @@ import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Optional; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.mockito.InjectMocks; @@ -34,6 +40,7 @@ import org.mockito.Mock; import org.mockito.MockitoAnnotations; import org.openecomp.sdc.be.dao.janusgraph.HealingJanusGraphGenericDao; import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus; +import org.openecomp.sdc.be.datatypes.elements.DataTypeDataDefinition; import org.openecomp.sdc.be.datatypes.enums.ModelTypeEnum; import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum; import org.openecomp.sdc.be.model.DataTypeDefinition; @@ -110,6 +117,23 @@ class DataTypeOperationTest { assertThat(dataTypesFound).isEmpty(); } + @Test + void getDataTypeByUidTest_Success() { + doReturn(Either.left(createDataTypeData("test.data.type99", "test.data.type00099", 888L, 999L, modelName))) + .when(janusGraphGenericDao).getNode(eq("uid"), eq("dataType"), any()); + final Optional<DataTypeDataDefinition> dataType = dataTypeOperation.getDataTypeByUid("dataType"); + assertTrue(dataType.isPresent()); + assertEquals("test.data.type99", dataType.get().getName()); + assertEquals("test.data.type00099", dataType.get().getUniqueId()); + assertEquals(modelName, dataType.get().getModel()); + } + + @Test + void getDataTypeByUidTest_Fail() { + doReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND)).when(janusGraphGenericDao).getNode(eq("uid"), eq("dataType"), any()); + Optional<DataTypeDataDefinition> result = dataTypeOperation.getDataTypeByUid("dataType"); + assertTrue(result.isEmpty()); + } private void initTestData() { model = new Model(modelName, ModelTypeEnum.NORMATIVE); |