From b600063182fce15ac3060e4526feaea715e7ef08 Mon Sep 17 00:00:00 2001 From: MichaelMorris Date: Thu, 2 Sep 2021 16:22:12 +0100 Subject: Fix data type cache issue for model extensions Signed-off-by: MichaelMorris Issue-ID: SDC-3713 Change-Id: I56b8de3065848942a4ba8dd8486a7d87e7d8029e --- .../model/operations/impl/DataTypeOperation.java | 59 +++++++++++++--------- .../model/operations/impl/PropertyOperation.java | 27 ++++++---- .../operations/impl/DataTypeOperationTest.java | 8 --- 3 files changed, 50 insertions(+), 44 deletions(-) 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 158cb2569b..d6cca3f824 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,6 +24,7 @@ import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.stream.Collectors; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections4.MapUtils; import org.openecomp.sdc.be.config.BeEcompErrorManager; @@ -69,8 +70,40 @@ public class DataTypeOperation extends AbstractOperation { } return dataTypesFound; } + + public Map> getAllDataTypeUidsToModels() { + final Map> dataTypesFound = new HashMap<>(); + final Either, JanusGraphOperationStatus> getAllDataTypesWithNullModel = + janusGraphGenericDao.getByCriteria(NodeTypeEnum.DataType, null, DataTypeData.class); - public List getAllDataTypesWithModel() { + final var dataTypesValidated = validateDataType(getAllDataTypesWithNullModel, null); + + 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())){ + dataTypesFound.put(dataType.getUniqueId(), new ArrayList<>()); + } + dataTypesFound.get(dataType.getUniqueId()).add(model.getName()); + } + }); + return dataTypesFound; + } + + private List getAllDataTypesWithModel(final String modelName) { + final Either, JanusGraphOperationStatus> getAllDataTypesByModel = janusGraphGenericDao + .getByCriteriaForModel(NodeTypeEnum.DataType, null, modelName, DataTypeData.class); + return validateDataType(getAllDataTypesByModel, modelName); + } + + private List getAllDataTypesWithModel() { final List dataTypesWithModel = new ArrayList<>(); modelOperation.findAllModels() .forEach(model -> { @@ -99,28 +132,4 @@ public class DataTypeOperation extends AbstractOperation { return getDataTypes.left().value(); } - public Map> mapDataTypesDefinitionByModel(final Map allDataTypes) { - final Map dataTypesWithNullModelMap = new HashMap<>(); - final Map dataTypesWithModelMap = new HashMap<>(); - final Map> dataTypesMappedByModel = new HashMap<>(); - allDataTypes.values().stream().forEach(dataTypeDefinition -> { - final var model = dataTypeDefinition.getModel(); - final var dataTypeDefinitionName = dataTypeDefinition.getName(); - if (model == null) { - dataTypesWithNullModelMap.put(dataTypeDefinitionName, dataTypeDefinition); - } else { - dataTypesWithModelMap.put(dataTypeDefinitionName, dataTypeDefinition); - if (dataTypesMappedByModel.containsKey(model)) { - dataTypesMappedByModel.get(model).put(dataTypeDefinitionName, dataTypeDefinition); - } else { - dataTypesMappedByModel.put(model, dataTypesWithModelMap); - } - } - }); - if(MapUtils.isNotEmpty(dataTypesWithNullModelMap)) { - dataTypesMappedByModel.put(null, dataTypesWithNullModelMap); - } - return dataTypesMappedByModel; - } - } diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/PropertyOperation.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/PropertyOperation.java index 726efcf847..01dbb07fb4 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/PropertyOperation.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/PropertyOperation.java @@ -1514,17 +1514,15 @@ public class PropertyOperation extends AbstractOperation implements IPropertyOpe final Map> dataTypes = new HashMap<>(); Either>, JanusGraphOperationStatus> result = Either.left(dataTypes); final Map allDataTypesFound = new HashMap<>(); - final List list = dataTypeOperation.getAllDataTypeNodes(); - if (list != null) { - log.trace("Number of data types to load is {}", list.size()); - List collect = list.stream().map(p -> p.getDataTypeDataDefinition().getName()).collect(Collectors.toList()); - log.trace("The data types to load are {}", collect); - for (DataTypeData dataTypeData : list) { - final String dataTypeName = dataTypeData.getDataTypeDataDefinition().getName(); - final String dataTypeUniqueId = dataTypeData.getUniqueId(); - log.trace("Going to fetch data type {}. uid is {}", dataTypeName, dataTypeUniqueId); + + final Map> dataTypeUidstoModels = dataTypeOperation.getAllDataTypeUidsToModels(); + + if (dataTypeUidstoModels != null) { + log.trace("Number of data types to load is {}", dataTypeUidstoModels.size()); + for (Map.Entry> entry : dataTypeUidstoModels.entrySet()) { + log.trace("Going to fetch data type with uid {}", entry.getKey()); Either dataTypeByUid = this - .getAndAddDataTypeByUid(dataTypeUniqueId, allDataTypesFound); + .getAndAddDataTypeByUid(entry.getKey(), allDataTypesFound); if (dataTypeByUid.isRight()) { JanusGraphOperationStatus status = dataTypeByUid.right().value(); if (status == JanusGraphOperationStatus.NOT_FOUND) { @@ -1532,8 +1530,15 @@ public class PropertyOperation extends AbstractOperation implements IPropertyOpe } return Either.right(status); } - result = Either.left(dataTypeOperation.mapDataTypesDefinitionByModel(allDataTypesFound)); + for (final String model: entry.getValue()) { + if (!dataTypes.containsKey(model)) { + dataTypes.put(model, new HashMap()); + } + DataTypeDefinition dataTypeDefinition = allDataTypesFound.get(entry.getKey()); + dataTypes.get(model).put(dataTypeDefinition.getName(), dataTypeDefinition); + } } + } if (log.isTraceEnabled()) { if (result.isRight()) { 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 c8a2b03fb3..d93b7986f8 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 @@ -113,14 +113,6 @@ class DataTypeOperationTest { assertThat(dataTypesFound).isEmpty(); } - @Test - void mapDataTypesDefinitionByModelTest() { - final var allDataTypesMappedByModel = - dataTypeOperation.mapDataTypesDefinitionByModel(allDataTypesFoundDefinitionMap); - assertThat(allDataTypesMappedByModel.get(modelName).size()).isEqualTo(2); - assertThat(allDataTypesMappedByModel.get(null)).isNotEmpty(); - } - private void initTestData() { model = new Model(modelName, ModelTypeEnum.NORMATIVE); -- cgit 1.2.3-korg