diff options
author | aribeiro <anderson.ribeiro@est.tech> | 2021-07-15 09:18:37 +0100 |
---|---|---|
committer | aribeiro <anderson.ribeiro@est.tech> | 2021-08-06 17:22:20 +0100 |
commit | 47bcc63a9daff1f310125fed006f27c93656fa83 (patch) | |
tree | 3c61670b039c0e63d14880f909e6047d54776492 /catalog-model | |
parent | 0ed125f98ae8ea9e80c855a640c328654660d081 (diff) |
Retrieve data types based on component model
Issue-ID: SDC-3660
Signed-off-by: aribeiro <anderson.ribeiro@est.tech>
Change-Id: I09c533eb39277532b29e581e4dd57e9df952e8e6
Diffstat (limited to 'catalog-model')
11 files changed, 459 insertions, 129 deletions
diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/DataTypeDefinition.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/DataTypeDefinition.java index 01f6cd1b40..bf9f4d79a6 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/DataTypeDefinition.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/DataTypeDefinition.java @@ -36,7 +36,6 @@ public class DataTypeDefinition extends DataTypeDataDefinition { private DataTypeDefinition derivedFrom; private List<PropertyConstraint> constraints; private List<PropertyDefinition> properties; - private String model; public DataTypeDefinition(final DataTypeDataDefinition dataTypeDataDefinition) { super(dataTypeDataDefinition); diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/cache/ApplicationCache.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/cache/ApplicationCache.java index d0a071909c..bfb20a2f14 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/cache/ApplicationCache.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/cache/ApplicationCache.java @@ -22,10 +22,11 @@ package org.openecomp.sdc.be.model.cache; import fj.data.Either; import java.util.Map; import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus; +import org.openecomp.sdc.be.model.DataTypeDefinition; public interface ApplicationCache<T> { - Either<Map<String, T>, JanusGraphOperationStatus> getAll(); + Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> getAll(final String model); - Either<T, JanusGraphOperationStatus> get(String uniqueId); + Either<T, JanusGraphOperationStatus> get(final String model, final String uniqueId); } diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/cache/ApplicationDataTypeCache.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/cache/ApplicationDataTypeCache.java index a1bafbc6f2..070ebf72e3 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/cache/ApplicationDataTypeCache.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/cache/ApplicationDataTypeCache.java @@ -20,6 +20,7 @@ package org.openecomp.sdc.be.model.cache; import fj.data.Either; +import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.List; @@ -43,6 +44,7 @@ import org.openecomp.sdc.be.config.Configuration.ApplicationL1CacheInfo; import org.openecomp.sdc.be.config.ConfigurationManager; import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus; import org.openecomp.sdc.be.model.DataTypeDefinition; +import org.openecomp.sdc.be.model.operations.impl.DataTypeOperation; import org.openecomp.sdc.be.model.operations.impl.PropertyOperation; import org.openecomp.sdc.be.resources.data.DataTypeData; import org.openecomp.sdc.common.log.enums.EcompLoggerErrorCode; @@ -64,13 +66,16 @@ public class ApplicationDataTypeCache implements ApplicationCache<DataTypeDefini private final ScheduledExecutorService scheduledPollingService; @Getter(AccessLevel.PACKAGE) private ScheduledFuture<?> scheduledFuture = null; - private Map<String, DataTypeDefinition> dataTypesCacheMap = new HashMap<>(); + private Map<String, Map<String, DataTypeDefinition>> dataTypesByModelCacheMap = new HashMap<>(); + private final DataTypeOperation dataTypeOperation; private int firstRunDelayInSec = 30; private int pollingIntervalInSec = 60; - public ApplicationDataTypeCache(final PropertyOperation propertyOperation, final ApplicationEventPublisher applicationEventPublisher) { + public ApplicationDataTypeCache(final PropertyOperation propertyOperation, final ApplicationEventPublisher applicationEventPublisher, + final DataTypeOperation dataTypeOperation) { this.propertyOperation = propertyOperation; this.applicationEventPublisher = applicationEventPublisher; + this.dataTypeOperation = dataTypeOperation; scheduledPollingService = Executors .newScheduledThreadPool(1, new BasicThreadFactory.Builder().namingPattern("ApplicationDataTypeCacheThread-%d").build()); } @@ -156,32 +161,34 @@ public class ApplicationDataTypeCache implements ApplicationCache<DataTypeDefini } } - private Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> getAllDataTypesFromGraph() { + private Either<Map<String, Map<String, DataTypeDefinition>>, JanusGraphOperationStatus> getAllDataTypesFromGraph() { return propertyOperation.getAllDataTypes(); } - @Override - public Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> getAll() { + public Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> getAll(final String model) { try { readWriteLock.readLock().lock(); - if (MapUtils.isEmpty(dataTypesCacheMap)) { - return getAllDataTypesFromGraph(); + if (MapUtils.isEmpty(dataTypesByModelCacheMap)) { + final var dataTypesFound = getAllDataTypesFromGraph(); + if (dataTypesFound.isRight()) { + return Either.right(dataTypesFound.right().value()); + } + dataTypesByModelCacheMap = dataTypesFound.left().value(); } - return Either.left(new HashMap<>(dataTypesCacheMap)); + return Either.left(getDataTypeDefinitionMapByModel(model)); } finally { readWriteLock.readLock().unlock(); } } @Override - public Either<DataTypeDefinition, JanusGraphOperationStatus> get(String uniqueId) { + public Either<DataTypeDefinition, JanusGraphOperationStatus> get(final String model, final String uniqueId) { try { readWriteLock.readLock().lock(); - if (MapUtils.isEmpty(dataTypesCacheMap)) { + if (MapUtils.isEmpty(dataTypesByModelCacheMap)) { return propertyOperation.getDataTypeByUid(uniqueId); } - - final Optional<DataTypeDefinition> dataTypeDefinition = dataTypesCacheMap.values().stream() + final Optional<DataTypeDefinition> dataTypeDefinition = getDataTypeDefinitionMapByModel(model).values().stream() .filter(p -> p.getUniqueId().equals(uniqueId)).findFirst(); if (dataTypeDefinition.isEmpty()) { return propertyOperation.getDataTypeByUid(uniqueId); @@ -192,6 +199,10 @@ public class ApplicationDataTypeCache implements ApplicationCache<DataTypeDefini } } + private Map<String, DataTypeDefinition> getDataTypeDefinitionMapByModel(final String model) { + return dataTypesByModelCacheMap.containsKey(model) ? dataTypesByModelCacheMap.get(model) : new HashMap<>(); + } + @Override public void run() { try { @@ -218,10 +229,9 @@ public class ApplicationDataTypeCache implements ApplicationCache<DataTypeDefini private boolean hasDataTypesChanged() { final List<DataTypeData> dataTypeListFromDatabase = findAllDataTypesLazy(); - final Map<String, DataTypeDefinition> dataTypesCacheCopyMap = copyDataTypeCache(); - - if (dataTypeListFromDatabase.size() != dataTypesCacheCopyMap.size()) { - log.debug("Total of cached data types '{}' differs from the actual '{}'", dataTypeListFromDatabase.size(), dataTypesCacheCopyMap.size()); + final int dataTypesCacheCopyMap = dataTypesCacheMapSize(); + if (dataTypeListFromDatabase.size() != dataTypesCacheCopyMap) { + log.debug("Total of cached data types '{}' differs from the actual '{}'", dataTypeListFromDatabase.size(), dataTypesCacheCopyMap); return true; } @@ -230,13 +240,23 @@ public class ApplicationDataTypeCache implements ApplicationCache<DataTypeDefini return false; } - return hasDataTypesChanged(dataTypeListFromDatabase, dataTypesCacheCopyMap); + return hasDataTypesChanged(dataTypeListFromDatabase, copyDataTypeCache()); } - private boolean hasDataTypesChanged(final List<DataTypeData> dataTypeListFromDatabase, final Map<String, DataTypeDefinition> dataTypesCacheCopyMap) { + private int dataTypesCacheMapSize() { + var count = 0; + for (var i = 0; i < copyDataTypeCache().size(); i++) { + count += new ArrayList<>(copyDataTypeCache().values()).get(i).size(); + + } + return count; + } + + private boolean hasDataTypesChanged(final List<DataTypeData> dataTypeListFromDatabase, final Map<String, Map<String, DataTypeDefinition>> dataTypesCacheCopyMap) { return dataTypeListFromDatabase.stream().map(DataTypeData::getDataTypeDataDefinition).anyMatch(actualDataTypeDefinition -> { final String dataTypeName = actualDataTypeDefinition.getName(); - final DataTypeDefinition cachedDataTypeDefinition = dataTypesCacheCopyMap.get(dataTypeName); + final String model = actualDataTypeDefinition.getModel(); + final DataTypeDefinition cachedDataTypeDefinition = dataTypesCacheCopyMap.get(model).get(dataTypeName); if (cachedDataTypeDefinition == null) { log.debug("Datatype '{}' is not present in the cache. ", dataTypeName); return true; @@ -245,8 +265,8 @@ public class ApplicationDataTypeCache implements ApplicationCache<DataTypeDefini final long cachedCreationTime = cachedDataTypeDefinition.getCreationTime() == null ? 0 : cachedDataTypeDefinition.getCreationTime(); final long actualCreationTime = actualDataTypeDefinition.getCreationTime() == null ? 0 : actualDataTypeDefinition.getCreationTime(); if (cachedCreationTime != actualCreationTime) { - log.debug("Datatype '{}' was updated. Cache/database creation time '{}'/'{}'.", - dataTypeName, cachedCreationTime, actualCreationTime); + log.debug("Datatype '{}' with model '{}' was updated. Cache/database creation time '{}'/'{}'.", + dataTypeName, model, cachedCreationTime, actualCreationTime); return true; } final long cachedModificationTime = @@ -263,23 +283,23 @@ public class ApplicationDataTypeCache implements ApplicationCache<DataTypeDefini }); } - private Map<String, DataTypeDefinition> copyDataTypeCache() { + private Map<String, Map<String, DataTypeDefinition>> copyDataTypeCache() { try { readWriteLock.readLock().lock(); - return new HashMap<>(this.dataTypesCacheMap); + return new HashMap<>(this.dataTypesByModelCacheMap); } finally { readWriteLock.readLock().unlock(); } } private void refreshDataTypesCache() { - final Map<String, DataTypeDefinition> dataTypesDefinitionMap = findAllDataTypesEager(); + final Map<String, Map<String, DataTypeDefinition>> dataTypesDefinitionMap = findAllDataTypesEager(); if (dataTypesDefinitionMap.isEmpty()) { return; } try { readWriteLock.writeLock().lock(); - dataTypesCacheMap = dataTypesDefinitionMap; + dataTypesByModelCacheMap = dataTypesDefinitionMap; onDataChangeEventEmit(); BeEcompErrorManager.getInstance() .logInternalFlowError("ReplaceDataTypesCache", "Succeed to replace the data types cache", ErrorSeverity.INFO); @@ -288,10 +308,10 @@ public class ApplicationDataTypeCache implements ApplicationCache<DataTypeDefini } } - private Map<String, DataTypeDefinition> findAllDataTypesEager() { + private Map<String, Map<String, DataTypeDefinition>> findAllDataTypesEager() { log.trace("Fetching data types from database, eager mode"); final long startTime = System.currentTimeMillis(); - final Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> allDataTypes = propertyOperation.getAllDataTypes(); + final Either<Map<String, Map<String, DataTypeDefinition>>, JanusGraphOperationStatus> allDataTypes = propertyOperation.getAllDataTypes(); log.trace("Finish fetching data types from database. Took {}ms", (System.currentTimeMillis() - startTime)); if (allDataTypes.isRight()) { final JanusGraphOperationStatus status = allDataTypes.right().value(); @@ -306,16 +326,9 @@ public class ApplicationDataTypeCache implements ApplicationCache<DataTypeDefini private List<DataTypeData> findAllDataTypesLazy() { log.trace("Fetching data types from database, lazy mode"); final long startTime = System.currentTimeMillis(); - final Either<List<DataTypeData>, JanusGraphOperationStatus> allDataTypes = propertyOperation.getAllDataTypeNodes(); + final List<DataTypeData> allDataTypes = dataTypeOperation.getAllDataTypeNodes(); log.trace("Finish fetching data types from database. Took {}ms", (System.currentTimeMillis() - startTime)); - if (allDataTypes.isRight()) { - final JanusGraphOperationStatus status = allDataTypes.right().value(); - var errorMsg= String.format("Failed to fetch data types from database. Status is %s", status); - log.error(EcompLoggerErrorCode.UNKNOWN_ERROR, ApplicationDataTypeCache.class.getName(), errorMsg); - BeEcompErrorManager.getInstance().logInternalConnectionError(APPLICATION_DATA_TYPES_CACHE, errorMsg, ErrorSeverity.ERROR); - return Collections.emptyList(); - } - return allDataTypes.left().value(); + return allDataTypes; } private void onDataChangeEventEmit() { @@ -329,9 +342,9 @@ public class ApplicationDataTypeCache implements ApplicationCache<DataTypeDefini public static class DataTypesCacheChangedEvent extends ApplicationEvent { @Getter - private final Map<String, DataTypeDefinition> newData; + private final Map<String, Map<String, DataTypeDefinition>> newData; - public DataTypesCacheChangedEvent(final Object source, final Map<String, DataTypeDefinition> newData) { + public DataTypesCacheChangedEvent(final Object source, final Map<String, Map<String, DataTypeDefinition>> newData) { super(source); this.newData = newData; } diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/ComponentInstanceOperation.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/ComponentInstanceOperation.java index 1097e5d204..97dd3ea718 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/ComponentInstanceOperation.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/ComponentInstanceOperation.java @@ -70,7 +70,7 @@ public class ComponentInstanceOperation extends AbstractOperation { @Autowired private IInputsOperation inputOperation; @Autowired - private ApplicationDataTypeCache dataTypeCache; + private ApplicationDataTypeCache applicationDataTypeCache; public ComponentInstanceOperation() { super(); @@ -408,7 +408,8 @@ public class ComponentInstanceOperation extends AbstractOperation { innerType = propDef.getType(); } log.debug("Before validateAndUpdatePropertyValue"); - Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> allDataTypes = dataTypeCache.getAll(); + Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> allDataTypes = + applicationDataTypeCache.getAll(resourceInstanceInput.getModel()); if (allDataTypes.isRight()) { JanusGraphOperationStatus status = allDataTypes.right().value(); BeEcompErrorManager.getInstance().logInternalFlowError("UpdatePropertyValueOnComponentInstance", 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 new file mode 100644 index 0000000000..158cb2569b --- /dev/null +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/DataTypeOperation.java @@ -0,0 +1,126 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2021 Nordix Foundation + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ +package org.openecomp.sdc.be.model.operations.impl; + +import fj.data.Either; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import org.apache.commons.collections.CollectionUtils; +import org.apache.commons.collections4.MapUtils; +import org.openecomp.sdc.be.config.BeEcompErrorManager; +import org.openecomp.sdc.be.config.BeEcompErrorManager.ErrorSeverity; +import org.openecomp.sdc.be.dao.janusgraph.HealingJanusGraphGenericDao; +import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus; +import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum; +import org.openecomp.sdc.be.model.DataTypeDefinition; +import org.openecomp.sdc.be.resources.data.DataTypeData; +import org.openecomp.sdc.common.log.enums.EcompLoggerErrorCode; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +@Component("dataType-operation") +public class DataTypeOperation extends AbstractOperation { + + private static final Logger LOGGER = LoggerFactory.getLogger(DataTypeOperation.class); + + private final ModelOperation modelOperation; + + @Autowired + public DataTypeOperation(final HealingJanusGraphGenericDao janusGraphGenericDao, + final ModelOperation modelOperation) { + this.janusGraphGenericDao = janusGraphGenericDao; + this.modelOperation = modelOperation; + } + + public List<DataTypeData> getAllDataTypeNodes() { + final List<DataTypeData> dataTypesFound = new ArrayList<>(); + final Either<List<DataTypeData>, JanusGraphOperationStatus> getAllDataTypesWithNullModel = + janusGraphGenericDao.getByCriteria(NodeTypeEnum.DataType, null, DataTypeData.class); + + final var dataTypesValidated = validateDataType(getAllDataTypesWithNullModel, null); + if (CollectionUtils.isNotEmpty(dataTypesValidated)) { + dataTypesFound.addAll(dataTypesValidated); + } + + final List<DataTypeData> allDataTypeNodesWithModel = getAllDataTypesWithModel(); + if(CollectionUtils.isNotEmpty(allDataTypeNodesWithModel)) { + dataTypesFound.addAll(allDataTypeNodesWithModel); + } + return dataTypesFound; + } + + public List<DataTypeData> getAllDataTypesWithModel() { + final List<DataTypeData> dataTypesWithModel = new ArrayList<>(); + modelOperation.findAllModels() + .forEach(model -> { + final var modelName = model.getName(); + final Either<List<DataTypeData>, JanusGraphOperationStatus> getAllDataTypesByModel = janusGraphGenericDao + .getByCriteriaForModel(NodeTypeEnum.DataType, null, modelName, DataTypeData.class); + final var dataTypesValidated = validateDataType(getAllDataTypesByModel, modelName); + dataTypesWithModel.addAll(dataTypesValidated); + }); + return dataTypesWithModel; + } + + private List<DataTypeData> validateDataType(final Either<List<DataTypeData>, JanusGraphOperationStatus> getDataTypes, final String modelName) { + if (getDataTypes.isRight() && getDataTypes.right().value() == JanusGraphOperationStatus.NOT_FOUND) { + return Collections.emptyList(); + } + 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); + LOGGER.error(String.valueOf(EcompLoggerErrorCode.UNKNOWN_ERROR), DataTypeOperation.class.getName(), errorMsg); + BeEcompErrorManager.getInstance().logInternalConnectionError(DataTypeOperation.class.getName(), errorMsg, ErrorSeverity.ERROR); + } + return Collections.emptyList(); + } + return getDataTypes.left().value(); + } + + public Map<String, Map<String, DataTypeDefinition>> mapDataTypesDefinitionByModel(final Map<String, DataTypeDefinition> allDataTypes) { + final Map<String, DataTypeDefinition> dataTypesWithNullModelMap = new HashMap<>(); + final Map<String, DataTypeDefinition> dataTypesWithModelMap = new HashMap<>(); + final Map<String, Map<String, DataTypeDefinition>> 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/GroupInstanceOperation.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/GroupInstanceOperation.java index 45df37c4b8..c463af691c 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/GroupInstanceOperation.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/GroupInstanceOperation.java @@ -76,7 +76,7 @@ public class GroupInstanceOperation extends AbstractOperation implements IGroupI @Autowired PropertyOperation propertyOperation; @javax.annotation.Resource - private ApplicationDataTypeCache dataTypeCache; + private ApplicationDataTypeCache applicationDataTypeCache; public Either<List<GroupInstance>, StorageOperationStatus> getAllGroupInstances(String parentId, NodeTypeEnum parentType) { Either<List<GroupInstance>, StorageOperationStatus> result = null; @@ -285,7 +285,8 @@ public class GroupInstanceOperation extends AbstractOperation implements IGroupI innerType = propDef.getType(); } log.debug("Before validateAndUpdatePropertyValue"); - Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> allDataTypes = dataTypeCache.getAll(); + Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> allDataTypes = + applicationDataTypeCache.getAll(groupInstanceProperty.getModel()); if (allDataTypes.isRight()) { JanusGraphOperationStatus status = allDataTypes.right().value(); BeEcompErrorManager.getInstance() @@ -388,7 +389,8 @@ public class GroupInstanceOperation extends AbstractOperation implements IGroupI innerType = propDef.getType(); } // Specific Update Logic - Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> allDataTypes = dataTypeCache.getAll(); + Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> allDataTypes = + applicationDataTypeCache.getAll(groupInstanceProerty.getModel()); if (allDataTypes.isRight()) { JanusGraphOperationStatus status = allDataTypes.right().value(); BeEcompErrorManager.getInstance() diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/GroupOperation.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/GroupOperation.java index effdbebf4c..b9378b8eca 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/GroupOperation.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/GroupOperation.java @@ -72,15 +72,15 @@ public class GroupOperation extends AbstractOperation implements IGroupOperation private final TopologyTemplateOperation topologyTemplateOperation; private final PropertyOperation propertyOperation; private final GroupTypeOperation groupTypeOperation; - private final ApplicationDataTypeCache dataTypeCache; + private final ApplicationDataTypeCache applicationDataTypeCache; public GroupOperation(JanusGraphDao janusGraphDao, TopologyTemplateOperation topologyTemplateOperation, PropertyOperation propertyOperation, - GroupTypeOperation groupTypeOperation, ApplicationDataTypeCache dataTypeCache) { + GroupTypeOperation groupTypeOperation, ApplicationDataTypeCache applicationDataTypeCache) { this.janusGraphDao = janusGraphDao; this.topologyTemplateOperation = topologyTemplateOperation; this.propertyOperation = propertyOperation; this.groupTypeOperation = groupTypeOperation; - this.dataTypeCache = dataTypeCache; + this.applicationDataTypeCache = applicationDataTypeCache; } private GroupDefinition convertGroupDataToGroupDefinition(GroupData groupData) { @@ -485,7 +485,7 @@ public class GroupOperation extends AbstractOperation implements IGroupOperation StorageOperationStatus result = null; String innerType = property.getSchema() == null ? null : property.getSchema().getProperty() == null ? null : property.getSchema().getProperty().getType(); - Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> allDataTypes = dataTypeCache.getAll(); + Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> allDataTypes = applicationDataTypeCache.getAll(property.getModel()); Either<Object, Boolean> isValid = null; if (allDataTypes.isRight()) { JanusGraphOperationStatus status = allDataTypes.right().value(); @@ -493,8 +493,8 @@ public class GroupOperation extends AbstractOperation implements IGroupOperation result = DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status); } if (result == null) { - isValid = propertyOperation - .validateAndUpdatePropertyValue(property.getType(), property.getValue(), innerType, allDataTypes.left().value()); + isValid = propertyOperation.validateAndUpdatePropertyValue(property.getType(), property.getValue(), innerType, + allDataTypes.left().value()); if (isValid.isRight()) { log.debug("Failed to validate property value {}. Status is {}. ", property.getValue(), StorageOperationStatus.INVALID_PROPERTY); result = StorageOperationStatus.INVALID_PROPERTY; 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 1f4fca0685..726efcf847 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 @@ -21,6 +21,20 @@ package org.openecomp.sdc.be.model.operations.impl; import static org.openecomp.sdc.common.log.enums.EcompLoggerErrorCode.BUSINESS_PROCESS_ERROR; +import com.fasterxml.jackson.core.ObjectCodec; +import com.fasterxml.jackson.databind.DeserializationContext; +import com.fasterxml.jackson.databind.JsonNode; +import com.google.common.collect.Maps; +import com.google.gson.JsonArray; +import com.google.gson.JsonDeserializationContext; +import com.google.gson.JsonDeserializer; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonParseException; +import com.google.gson.JsonParser; +import com.google.gson.JsonSerializationContext; +import com.google.gson.JsonSerializer; +import fj.data.Either; import java.io.IOException; import java.lang.reflect.Type; import java.util.ArrayList; @@ -82,6 +96,7 @@ import org.openecomp.sdc.be.model.tosca.constraints.ValidValuesConstraint; import org.openecomp.sdc.be.model.tosca.converters.PropertyValueConverter; import org.openecomp.sdc.be.resources.data.ComponentInstanceData; import org.openecomp.sdc.be.resources.data.DataTypeData; +import org.openecomp.sdc.be.resources.data.ModelData; import org.openecomp.sdc.be.resources.data.PropertyData; import org.openecomp.sdc.be.resources.data.PropertyValueData; import org.openecomp.sdc.be.resources.data.ResourceMetadataData; @@ -89,20 +104,6 @@ import org.openecomp.sdc.be.resources.data.UniqueIdData; import org.openecomp.sdc.common.log.wrappers.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; -import com.fasterxml.jackson.core.ObjectCodec; -import com.fasterxml.jackson.databind.DeserializationContext; -import com.fasterxml.jackson.databind.JsonNode; -import com.google.common.collect.Maps; -import com.google.gson.JsonArray; -import com.google.gson.JsonDeserializationContext; -import com.google.gson.JsonDeserializer; -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; -import com.google.gson.JsonParseException; -import com.google.gson.JsonParser; -import com.google.gson.JsonSerializationContext; -import com.google.gson.JsonSerializer; -import fj.data.Either; @Component("property-operation") public class PropertyOperation extends AbstractOperation implements IPropertyOperation { @@ -120,11 +121,14 @@ public class PropertyOperation extends AbstractOperation implements IPropertyOpe private static final String UPDATE_DATA_TYPE = "UpdateDataType"; private static Logger log = Logger.getLogger(PropertyOperation.class.getName()); private DerivedFromOperation derivedFromOperation; + private DataTypeOperation dataTypeOperation; @Autowired - public PropertyOperation(HealingJanusGraphGenericDao janusGraphGenericDao, DerivedFromOperation derivedFromOperation) { + public PropertyOperation(HealingJanusGraphGenericDao janusGraphGenericDao, DerivedFromOperation derivedFromOperation, + DataTypeOperation dataTypeOperation) { this.janusGraphGenericDao = janusGraphGenericDao; this.derivedFromOperation = derivedFromOperation; + this.dataTypeOperation = dataTypeOperation; } public PropertyDefinition convertPropertyDataToPropertyDefinition(PropertyData propertyDataResult, String propertyName, String resourceId) { @@ -1506,28 +1510,21 @@ public class PropertyOperation extends AbstractOperation implements IPropertyOpe return Either.left(true); } - public Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> getAllDataTypes() { - Map<String, DataTypeDefinition> dataTypes = new HashMap<>(); - Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> result = Either.left(dataTypes); - Either<List<DataTypeData>, JanusGraphOperationStatus> getAllDataTypes = janusGraphGenericDao - .getByCriteria(NodeTypeEnum.DataType, null, DataTypeData.class); - if (getAllDataTypes.isRight()) { - JanusGraphOperationStatus status = getAllDataTypes.right().value(); - if (status != JanusGraphOperationStatus.NOT_FOUND) { - return Either.right(status); - } else { - return result; - } - } - List<DataTypeData> list = getAllDataTypes.left().value(); + public Either<Map<String, Map<String, DataTypeDefinition>>, JanusGraphOperationStatus> getAllDataTypes() { + final Map<String, Map<String, DataTypeDefinition>> dataTypes = new HashMap<>(); + Either<Map<String, Map<String, DataTypeDefinition>>, JanusGraphOperationStatus> result = Either.left(dataTypes); + final Map<String, DataTypeDefinition> allDataTypesFound = new HashMap<>(); + final List<DataTypeData> list = dataTypeOperation.getAllDataTypeNodes(); if (list != null) { log.trace("Number of data types to load is {}", list.size()); List<String> collect = list.stream().map(p -> p.getDataTypeDataDefinition().getName()).collect(Collectors.toList()); log.trace("The data types to load are {}", collect); for (DataTypeData dataTypeData : list) { - log.trace("Going to fetch data type {}. uid is {}", dataTypeData.getDataTypeDataDefinition().getName(), dataTypeData.getUniqueId()); + final String dataTypeName = dataTypeData.getDataTypeDataDefinition().getName(); + final String dataTypeUniqueId = dataTypeData.getUniqueId(); + log.trace("Going to fetch data type {}. uid is {}", dataTypeName, dataTypeUniqueId); Either<DataTypeDefinition, JanusGraphOperationStatus> dataTypeByUid = this - .getAndAddDataTypeByUid(dataTypeData.getUniqueId(), dataTypes); + .getAndAddDataTypeByUid(dataTypeUniqueId, allDataTypesFound); if (dataTypeByUid.isRight()) { JanusGraphOperationStatus status = dataTypeByUid.right().value(); if (status == JanusGraphOperationStatus.NOT_FOUND) { @@ -1535,13 +1532,14 @@ public class PropertyOperation extends AbstractOperation implements IPropertyOpe } return Either.right(status); } + result = Either.left(dataTypeOperation.mapDataTypesDefinitionByModel(allDataTypesFound)); } } if (log.isTraceEnabled()) { if (result.isRight()) { log.trace("After fetching all data types {}", result); } else { - Map<String, DataTypeDefinition> map = result.left().value(); + Map<String, Map<String, DataTypeDefinition>> map = result.left().value(); if (map != null) { String types = map.keySet().stream().collect(Collectors.joining(",", "[", "]")); log.trace("After fetching all data types {} ", types); @@ -1577,7 +1575,7 @@ public class PropertyOperation extends AbstractOperation implements IPropertyOpe log.error(FAILED_TO_FETCH_PROPERTIES_OF_DATA_TYPE, uniqueId); return Either.right(propertiesStatus); } - allDataTypes.put(dataTypeDefinition.getName(), dataTypeDefinition); + allDataTypes.put(dataTypeDefinition.getUniqueId(), dataTypeDefinition); String derivedFrom = dataTypeDefinition.getDerivedFromName(); if (allDataTypes.containsKey(derivedFrom)) { DataTypeDefinition parentDataTypeDefinition = allDataTypes.get(derivedFrom); @@ -1606,26 +1604,28 @@ public class PropertyOperation extends AbstractOperation implements IPropertyOpe } DataTypeDefinition parentDataTypeDefinition = dataTypeByUid.left().value(); dataTypeDefinition.setDerivedFrom(parentDataTypeDefinition); + final var model = getModel(uniqueId); + if (StringUtils.isNotEmpty(model)) { + dataTypeDefinition.setModel(model); + } } result = Either.left(dataTypeDefinition); return result; } + private String getModel(final String uniqueId) { + final Either<ImmutablePair<ModelData, GraphEdge>, JanusGraphOperationStatus> model = janusGraphGenericDao.getParentNode( + UniqueIdBuilder.getKeyByNodeType(NodeTypeEnum.DataType), uniqueId, GraphEdgeLabels.MODEL_ELEMENT, + NodeTypeEnum.Model, ModelData.class); + return model.isLeft() ? model.left().value().getLeft().getName() : StringUtils.EMPTY; + } + public Either<String, JanusGraphOperationStatus> checkInnerType(PropertyDataDefinition propDataDef) { String propertyType = propDataDef.getType(); ToscaPropertyType type = ToscaPropertyType.isValidType(propertyType); return getInnerType(type, propDataDef::getSchema); } - public Either<List<DataTypeData>, JanusGraphOperationStatus> getAllDataTypeNodes() { - final Either<List<DataTypeData>, JanusGraphOperationStatus> getAllDataTypes = - janusGraphGenericDao.getByCriteria(NodeTypeEnum.DataType, null, DataTypeData.class); - if (getAllDataTypes.isRight() && getAllDataTypes.right().value() == JanusGraphOperationStatus.NOT_FOUND) { - return Either.left(Collections.emptyList()); - } - return getAllDataTypes; - } - public Either<Object, Boolean> validateAndUpdatePropertyValue(String propertyType, String value, boolean isValidate, String innerType, Map<String, DataTypeDefinition> dataTypes) { log.trace("Going to validate property value and its type. type = {}, value = {}", propertyType, value); diff --git a/catalog-model/src/test/java/org/openecomp/sdc/be/model/cache/ApplicationDataTypeCacheTest.java b/catalog-model/src/test/java/org/openecomp/sdc/be/model/cache/ApplicationDataTypeCacheTest.java index 9126b64659..75a47db634 100644 --- a/catalog-model/src/test/java/org/openecomp/sdc/be/model/cache/ApplicationDataTypeCacheTest.java +++ b/catalog-model/src/test/java/org/openecomp/sdc/be/model/cache/ApplicationDataTypeCacheTest.java @@ -49,6 +49,7 @@ import org.openecomp.sdc.be.config.ConfigurationManager; import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus; import org.openecomp.sdc.be.datatypes.elements.DataTypeDataDefinition; import org.openecomp.sdc.be.model.DataTypeDefinition; +import org.openecomp.sdc.be.model.operations.impl.DataTypeOperation; import org.openecomp.sdc.be.model.operations.impl.PropertyOperation; import org.openecomp.sdc.be.resources.data.DataTypeData; import org.springframework.context.ApplicationEventPublisher; @@ -59,6 +60,9 @@ class ApplicationDataTypeCacheTest { private PropertyOperation propertyOperation; @Mock + private DataTypeOperation dataTypeOperation; + + @Mock private ApplicationEventPublisher applicationEventPublisher; @InjectMocks @@ -150,18 +154,19 @@ class ApplicationDataTypeCacheTest { final DataTypeDefinition testDataType1 = createDataTypeDefinition("test.data.type1", "test.data.type1", 101L, 1000L); final DataTypeDefinition testDataType2 = createDataTypeDefinition("test.data.type2", "test.data.type2", 101L, 1002L); - final Map<String, DataTypeDefinition> modifiedDataTypeDefinitionMap = - Map.of(testDataType1.getName(), testDataType1, testDataType2.getName(), testDataType2); + final Map<String, DataTypeDefinition> dataTypeDefinitionMap = Map.of(testDataType1.getName(), testDataType1, testDataType2.getName(), testDataType2); + final Map<String, Map<String, DataTypeDefinition>> modifiedDataTypeDefinitionMap = new HashMap<>(); + modifiedDataTypeDefinitionMap.put(null, dataTypeDefinitionMap); when(propertyOperation.getAllDataTypes()).thenReturn(Either.left(modifiedDataTypeDefinitionMap)); final DataTypeData dataTypeData1 = createDataTypeData("test.data.type1", "test.data.type1", 101L, 101L); final DataTypeData dataTypeData2 = createDataTypeData("test.data.type2", "test.data.type2", 101L, 1002L); - when(propertyOperation.getAllDataTypeNodes()).thenReturn(Either.left(List.of(dataTypeData1, dataTypeData2))); + when(dataTypeOperation.getAllDataTypeNodes()).thenReturn(List.of(dataTypeData1, dataTypeData2)); await().atMost(Duration.ofSeconds(schedulerPollIntervalInSec + 1)).until(() -> scheduledFuture.getDelay(TimeUnit.SECONDS) == 0); await().atMost(Duration.ofSeconds(schedulerPollIntervalInSec + 1)).until(() -> scheduledFuture.getDelay(TimeUnit.SECONDS) != 0); - assertDataTypeCache(modifiedDataTypeDefinitionMap); + assertDataTypeCache(modifiedDataTypeDefinitionMap.get(null)); } @Test @@ -172,17 +177,19 @@ class ApplicationDataTypeCacheTest { await().until(() -> scheduledFuture.getDelay(TimeUnit.SECONDS) != 0); assertDataTypeCache(dataTypeDefinitionMap); + final Map<String, Map<String, DataTypeDefinition>> dataTypesMappedByModel = new HashMap<>(); final Map<String, DataTypeDefinition> modifiedDataTypeDefinitionMap = new HashMap<>(); final DataTypeDefinition testDataType1 = createDataTypeDefinition("test.data.type1", "test.data.type1", 1L, 1L); modifiedDataTypeDefinitionMap.put(testDataType1.getName(), testDataType1); final DataTypeDefinition testDataType3 = createDataTypeDefinition("test.data.type3", "test.data.type3", 1L, 1L); modifiedDataTypeDefinitionMap.put(testDataType3.getName(), testDataType3); - when(propertyOperation.getAllDataTypes()).thenReturn(Either.left(modifiedDataTypeDefinitionMap)); + dataTypesMappedByModel.put(null, modifiedDataTypeDefinitionMap); + when(propertyOperation.getAllDataTypes()).thenReturn(Either.left(dataTypesMappedByModel)); final DataTypeData dataTypeData1 = createDataTypeData("test.data.type1", "test.data.type1", 1L, 1L); final DataTypeData dataTypeData3 = createDataTypeData("test.data.type3", "test.data.type3", 1L, 1L); - when(propertyOperation.getAllDataTypeNodes()).thenReturn(Either.left(List.of(dataTypeData1, dataTypeData3))); + when(dataTypeOperation.getAllDataTypeNodes()).thenReturn(List.of(dataTypeData1, dataTypeData3)); await().atMost(Duration.ofSeconds(schedulerPollIntervalInSec + 1)).until(() -> scheduledFuture.getDelay(TimeUnit.SECONDS) == 0); await().atMost(Duration.ofSeconds(schedulerPollIntervalInSec + 1)).until(() -> scheduledFuture.getDelay(TimeUnit.SECONDS) != 0); @@ -191,9 +198,9 @@ class ApplicationDataTypeCacheTest { @Test void testGetAllWithNoInitialization() { - final Map<String, DataTypeDefinition> dataTypeDefinitionMap = new HashMap<>(); + final Map<String, Map<String, DataTypeDefinition>> dataTypeDefinitionMap = new HashMap<>(); when(propertyOperation.getAllDataTypes()).thenReturn(Either.left(dataTypeDefinitionMap)); - final Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> response = applicationDataTypeCache.getAll(); + final Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> response = applicationDataTypeCache.getAll(null); assertNotNull(response); assertTrue(response.isLeft()); } @@ -202,7 +209,7 @@ class ApplicationDataTypeCacheTest { void testGetWhenCacheIsEmpty() { var dataTypeDefinition = new DataTypeDefinition(); when(propertyOperation.getDataTypeByUid("uniqueId")).thenReturn(Either.left(dataTypeDefinition)); - final Either<DataTypeDefinition, JanusGraphOperationStatus> dataTypeEither = applicationDataTypeCache.get("uniqueId"); + final Either<DataTypeDefinition, JanusGraphOperationStatus> dataTypeEither = applicationDataTypeCache.get(null, "uniqueId"); assertNotNull(dataTypeEither); assertTrue(dataTypeEither.isLeft()); assertEquals(dataTypeDefinition, dataTypeEither.left().value()); @@ -213,7 +220,8 @@ class ApplicationDataTypeCacheTest { defaultInit(); final ScheduledFuture<?> scheduledFuture = applicationDataTypeCache.getScheduledFuture(); await().atMost(Duration.ofSeconds(schedulerPollIntervalInSec + 1)).until(() -> scheduledFuture.getDelay(TimeUnit.SECONDS) != 0); - final Either<DataTypeDefinition, JanusGraphOperationStatus> dataTypeEither = applicationDataTypeCache.get("test.data.type1"); + final Either<DataTypeDefinition, JanusGraphOperationStatus> dataTypeEither = + applicationDataTypeCache.get(null, "test.data.type1"); assertNotNull(dataTypeEither); assertTrue(dataTypeEither.isLeft()); final DataTypeDefinition actualDataTypeDefinition = dataTypeEither.left().value(); @@ -236,12 +244,14 @@ class ApplicationDataTypeCacheTest { dataTypeDefinitionMap.put(testDataType1.getName(), testDataType1); final DataTypeDefinition testDataType2 = createDataTypeDefinition("test.data.type2", "test.data.type2", 101L, 1001L); dataTypeDefinitionMap.put(testDataType2.getName(), testDataType2); - when(propertyOperation.getAllDataTypes()).thenReturn(Either.left(dataTypeDefinitionMap)); + final Map<String, Map<String, DataTypeDefinition>> dataTypesMappedByModel = new HashMap<>(); + dataTypesMappedByModel.put(null, dataTypeDefinitionMap); + when(propertyOperation.getAllDataTypes()).thenReturn(Either.left(dataTypesMappedByModel)); final DataTypeData dataTypeData1 = createDataTypeData("test.data.type1", testDataType1.getName(), 100L, 1000L); final DataTypeData dataTypeData2 = createDataTypeData("test.data.type2", testDataType2.getName(), 101L, 1001L); - when(propertyOperation.getAllDataTypeNodes()).thenReturn(Either.left(List.of(dataTypeData1, dataTypeData2))); + when(dataTypeOperation.getAllDataTypeNodes()).thenReturn(List.of(dataTypeData1, dataTypeData2)); applicationDataTypeCache.init(); } @@ -286,7 +296,7 @@ class ApplicationDataTypeCacheTest { } public void assertDataTypeCache(final Map<String, DataTypeDefinition> expectedDataTypeCache) { - Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> dataTypeCacheMapEither = applicationDataTypeCache.getAll(); + Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> dataTypeCacheMapEither = applicationDataTypeCache.getAll(null); assertNotNull(dataTypeCacheMapEither); assertTrue(dataTypeCacheMapEither.isLeft()); final Map<String, DataTypeDefinition> actualDataTypeMap = dataTypeCacheMapEither.left().value(); 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 new file mode 100644 index 0000000000..015aedc6a3 --- /dev/null +++ b/catalog-model/src/test/java/org/openecomp/sdc/be/model/operations/impl/DataTypeOperationTest.java @@ -0,0 +1,174 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2021 Nordix Foundation + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ +package org.openecomp.sdc.be.model.operations.impl; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.when; + +import fj.data.Either; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; +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.enums.NodeTypeEnum; +import org.openecomp.sdc.be.model.DataTypeDefinition; +import org.openecomp.sdc.be.model.Model; +import org.openecomp.sdc.be.model.cache.ApplicationDataTypeCache; +import org.openecomp.sdc.be.resources.data.DataTypeData; +import org.springframework.test.context.ContextConfiguration; + +@ContextConfiguration("classpath:application-context-test.xml") +class DataTypeOperationTest { + + @InjectMocks + private DataTypeOperation dataTypeOperation; + @Mock + private ModelOperation modelOperation; + @Mock + private HealingJanusGraphGenericDao janusGraphGenericDao; + @Mock + private ApplicationDataTypeCache applicationDataTypeCache; + + private final String modelName = "ETSI-SDC-MODEL-TEST"; + private final List<DataTypeData> dataTypesWithoutModel = new ArrayList<>(); + private final List<DataTypeData> dataTypesWithModel = new ArrayList<>(); + final Map<String, Map<String, DataTypeDefinition>> dataTypesMappedByModel = new HashMap<>(); + final Map<String, DataTypeDefinition> allDataTypesFoundDefinitionMap = new HashMap<>(); + private Model model; + + + @BeforeEach + void beforeEachInit() { + MockitoAnnotations.openMocks(this); + initTestData(); + } + + @Test + void getAllDataTypeNodesTest() { + when(janusGraphGenericDao.getByCriteria(NodeTypeEnum.DataType, null, DataTypeData.class)) + .thenReturn(Either.left(dataTypesWithoutModel)); + when(modelOperation.findAllModels()).thenReturn(Collections.singletonList(model)); + when(janusGraphGenericDao.getByCriteriaForModel(NodeTypeEnum.DataType, null, modelName, DataTypeData.class)) + .thenReturn(Either.left(dataTypesWithModel)); + final var dataTypesFound = dataTypeOperation.getAllDataTypeNodes(); + assertThat(dataTypesFound.size()).isEqualTo(4); + assertThat(dataTypesFound.containsAll(dataTypesWithoutModel)).isTrue(); + assertThat(dataTypesFound.containsAll(dataTypesWithModel)).isTrue(); + } + + @Test + void getAllDataTypesWithModelTest() { + when(janusGraphGenericDao.getByCriteria(NodeTypeEnum.DataType, null, DataTypeData.class)) + .thenReturn(Either.left(Collections.emptyList())); + when(modelOperation.findAllModels()).thenReturn(Collections.singletonList(model)); + when(janusGraphGenericDao.getByCriteriaForModel(NodeTypeEnum.DataType, null, modelName, DataTypeData.class)) + .thenReturn(Either.left(dataTypesWithModel)); + final var dataTypesFound = dataTypeOperation.getAllDataTypeNodes(); + assertThat(dataTypesFound.size()).isEqualTo(2); + assertThat(dataTypesFound.containsAll(dataTypesWithModel)).isTrue(); + assertThat(dataTypesFound.containsAll(dataTypesWithoutModel)).isFalse(); + } + + @Test + void getAllDataTypeNodesWithValidationErrorTest() { + when(janusGraphGenericDao.getByCriteria(NodeTypeEnum.DataType, null, DataTypeData.class)) + .thenReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND)); + final var dataTypesFound = dataTypeOperation.getAllDataTypeNodes(); + assertThat(dataTypesFound.isEmpty()).isTrue(); + } + + @Test + void getAllDataTypesWithModelWithValidationErrorTest() { + when(janusGraphGenericDao.getByCriteria(NodeTypeEnum.DataType, null, DataTypeData.class)) + .thenReturn(Either.left(Collections.emptyList())); + when(modelOperation.findAllModels()).thenReturn(Collections.singletonList(model)); + when(janusGraphGenericDao.getByCriteriaForModel(NodeTypeEnum.DataType, null, modelName, DataTypeData.class)) + .thenReturn(Either.right(JanusGraphOperationStatus.GENERAL_ERROR)); + final var dataTypesFound = dataTypeOperation.getAllDataTypeNodes(); + 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); + final String TEST_DATA_TYPE_001 = "test.data.type001"; + final String TEST_DATA_TYPE_002 = "test.data.type002"; + final String TEST_DATA_TYPE_003 = "test.data.type003"; + final String TEST_DATA_TYPE_004 = "test.data.type004"; + final DataTypeData dataTypeData1 = createDataTypeData("test.data.type1", TEST_DATA_TYPE_001, 101L, + 101L, null); + final DataTypeData dataTypeData2 = createDataTypeData("test.data.type2", TEST_DATA_TYPE_002, 101L, + 1002L, null); + dataTypesWithoutModel.add(dataTypeData1); + dataTypesWithoutModel.add(dataTypeData2); + + final DataTypeData dataTypeWithModel1 = createDataTypeData("test.data.type1", TEST_DATA_TYPE_003, 101L, + 101L, modelName); + final DataTypeData dataTypeWithModel2 = createDataTypeData("test.data.type2", TEST_DATA_TYPE_004, 101L, + 1002L, modelName); + dataTypesWithModel.add(dataTypeWithModel1); + dataTypesWithModel.add(dataTypeWithModel2); + + allDataTypesFoundDefinitionMap.put(TEST_DATA_TYPE_001, createDataTypeDefinition("test.data.type1", TEST_DATA_TYPE_001, + 101L, 101L, null)); + allDataTypesFoundDefinitionMap.put(TEST_DATA_TYPE_002, createDataTypeDefinition("test.data.type2", TEST_DATA_TYPE_002, + 101L, 101L, null)); + allDataTypesFoundDefinitionMap.put(TEST_DATA_TYPE_003, createDataTypeDefinition("test.data.type1", TEST_DATA_TYPE_003, + 101L, 101L, modelName)); + allDataTypesFoundDefinitionMap.put(TEST_DATA_TYPE_004, createDataTypeDefinition("test.data.type2", TEST_DATA_TYPE_004, + 101L, 101L, modelName)); + + dataTypesMappedByModel.put(null, allDataTypesFoundDefinitionMap); + } + + private DataTypeData createDataTypeData(final String name, final String uniqueId, final long creationTime, final long modificationTime, + final String model) { + final DataTypeData dataTypeData = new DataTypeData(); + dataTypeData.setDataTypeDataDefinition(createDataTypeDefinition(name, uniqueId, creationTime, modificationTime, model)); + return dataTypeData; + } + + private DataTypeDefinition createDataTypeDefinition(final String name, final String uniqueId, final long creationTime, + final long modificationTime, String model) { + final DataTypeDefinition dataTypeDefinition = new DataTypeDefinition(); + dataTypeDefinition.setName(name); + dataTypeDefinition.setUniqueId(uniqueId); + dataTypeDefinition.setCreationTime(creationTime); + dataTypeDefinition.setModificationTime(modificationTime); + dataTypeDefinition.setModel(model); + return dataTypeDefinition; + } + +} diff --git a/catalog-model/src/test/java/org/openecomp/sdc/be/model/operations/impl/PropertyOperationTest.java b/catalog-model/src/test/java/org/openecomp/sdc/be/model/operations/impl/PropertyOperationTest.java index 6812b44b41..793af71bbd 100644 --- a/catalog-model/src/test/java/org/openecomp/sdc/be/model/operations/impl/PropertyOperationTest.java +++ b/catalog-model/src/test/java/org/openecomp/sdc/be/model/operations/impl/PropertyOperationTest.java @@ -20,14 +20,27 @@ package org.openecomp.sdc.be.model.operations.impl; -import org.janusgraph.core.JanusGraphVertex; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; + import fj.data.Either; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; import org.apache.commons.lang3.tuple.ImmutablePair; +import org.janusgraph.core.JanusGraphVertex; import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.mockito.Mockito; -import org.openecomp.sdc.be.dao.graph.datatype.GraphEdge; import org.openecomp.sdc.be.dao.graph.datatype.GraphRelation; import org.openecomp.sdc.be.dao.janusgraph.HealingJanusGraphGenericDao; import org.openecomp.sdc.be.dao.janusgraph.JanusGraphClient; @@ -38,7 +51,12 @@ import org.openecomp.sdc.be.dao.neo4j.GraphPropertiesDictionary; import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition; import org.openecomp.sdc.be.datatypes.elements.PropertyRule; import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum; -import org.openecomp.sdc.be.model.*; +import org.openecomp.sdc.be.model.ComponentInstanceProperty; +import org.openecomp.sdc.be.model.DataTypeDefinition; +import org.openecomp.sdc.be.model.IComplexDefaultValue; +import org.openecomp.sdc.be.model.ModelTestBase; +import org.openecomp.sdc.be.model.PropertyConstraint; +import org.openecomp.sdc.be.model.PropertyDefinition; import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus; import org.openecomp.sdc.be.model.tosca.ToscaPropertyType; import org.openecomp.sdc.be.model.tosca.ToscaType; @@ -48,16 +66,14 @@ import org.openecomp.sdc.be.model.tosca.constraints.LessOrEqualConstraint; import org.openecomp.sdc.be.resources.data.DataTypeData; import org.openecomp.sdc.be.resources.data.PropertyData; import org.openecomp.sdc.be.resources.data.PropertyValueData; -import java.util.*; - -import static org.junit.Assert.*; -import static org.mockito.Mockito.mock; public class PropertyOperationTest extends ModelTestBase { HealingJanusGraphGenericDao janusGraphGenericDao = mock(HealingJanusGraphGenericDao.class); - PropertyOperation propertyOperation = new PropertyOperation(janusGraphGenericDao, null); + final DataTypeOperation dataTypeOperation = mock(DataTypeOperation.class); + + PropertyOperation propertyOperation = new PropertyOperation(janusGraphGenericDao, null, dataTypeOperation); @Before public void setup() { @@ -441,7 +457,7 @@ public class PropertyOperationTest extends ModelTestBase { } private PropertyOperation createTestSubject() { - return new PropertyOperation(new HealingJanusGraphGenericDao(new JanusGraphClient()), null); + return new PropertyOperation(new HealingJanusGraphGenericDao(new JanusGraphClient()), null, dataTypeOperation); } @Test @@ -937,7 +953,7 @@ public class PropertyOperationTest extends ModelTestBase { @Test public void testGetAllDataTypes() throws Exception { PropertyOperation testSubject; - Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> result; + Either<Map<String, Map<String, DataTypeDefinition>>, JanusGraphOperationStatus> result; // default test testSubject = createTestSubject(); @@ -956,18 +972,6 @@ public class PropertyOperationTest extends ModelTestBase { result = testSubject.checkInnerType(propDataDef); } - - @Test - public void testGetAllDataTypeNodes() throws Exception { - PropertyOperation testSubject; - Either<List<DataTypeData>, JanusGraphOperationStatus> result; - - // default test - testSubject = createTestSubject(); - result = testSubject.getAllDataTypeNodes(); - } - - @Test public void testValidateAndUpdatePropertyValue() throws Exception { PropertyOperation testSubject; |