diff options
author | 2023-10-23 14:40:25 +0100 | |
---|---|---|
committer | 2023-10-23 16:19:50 +0100 | |
commit | 4266fbbaad4ab3741accf1b3911278835ca19795 (patch) | |
tree | 8bef45b9fd2c3e15a40e455d1ef586eaee2c339c /cps-ncmp-service/src/main | |
parent | 3ccd62ed40e96dbe2970de1433b2ebdbe014d4aa (diff) |
Introduce and use new Hazelcast map pt. 2
- modified ModuleSyncService
- created ModuleSetTagCacheConfig and tests
- ensure both Create and Upgrade cm Handle use cases work
- Moved inventory pkg to its right patha as production code.
Issue-ID: CPS-1859
Signed-off-by: leventecsanyi <levente.csanyi@est.tech>
Change-Id: I173dcd81fe2e74d86d85365b2ead461302cad974
Signed-off-by: sourabh_sourabh <sourabh.sourabh@est.tech>
Diffstat (limited to 'cps-ncmp-service/src/main')
7 files changed, 113 insertions, 63 deletions
diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/NetworkCmProxyDataService.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/NetworkCmProxyDataService.java index e7ffaa624..0c1287553 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/NetworkCmProxyDataService.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/NetworkCmProxyDataService.java @@ -171,7 +171,7 @@ public interface NetworkCmProxyDataService { * @param cmHandleId cm handle id * @param dataSyncEnabled data sync enabled flag */ - void setDataSyncEnabled(String cmHandleId, boolean dataSyncEnabled); + void setDataSyncEnabled(String cmHandleId, Boolean dataSyncEnabled); /** * Get all cm handle IDs by DMI plugin identifier. diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/NetworkCmProxyDataServiceImpl.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/NetworkCmProxyDataServiceImpl.java index b2c71941c..febd1cf9c 100755 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/NetworkCmProxyDataServiceImpl.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/NetworkCmProxyDataServiceImpl.java @@ -218,26 +218,29 @@ public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService * based on the data sync enabled boolean for the cm handle id provided. * * @param cmHandleId cm handle id - * @param dataSyncEnabled data sync enabled flag + * @param dataSyncEnabledTargetValue data sync enabled flag */ @Override - public void setDataSyncEnabled(final String cmHandleId, final boolean dataSyncEnabled) { + public void setDataSyncEnabled(final String cmHandleId, final Boolean dataSyncEnabledTargetValue) { final CompositeState compositeState = inventoryPersistence.getCmHandleState(cmHandleId); - if (compositeState.getDataSyncEnabled().equals(dataSyncEnabled)) { - log.info("Data-Sync Enabled flag is already: {} ", dataSyncEnabled); - } else if (compositeState.getCmHandleState() != CmHandleState.READY) { - throw new CpsException("State mismatch exception.", "Cm-Handle not in READY state. Cm handle state is: " - + compositeState.getCmHandleState()); - } else { + if (dataSyncEnabledTargetValue.equals(compositeState.getDataSyncEnabled())) { + log.info("Data-Sync Enabled flag is already: {} ", dataSyncEnabledTargetValue); + return; + } + if (CmHandleState.READY.equals(compositeState.getCmHandleState())) { final DataStoreSyncState dataStoreSyncState = compositeState.getDataStores() .getOperationalDataStore().getDataStoreSyncState(); - if (!dataSyncEnabled && dataStoreSyncState == DataStoreSyncState.SYNCHRONIZED) { + if (Boolean.FALSE.equals(dataSyncEnabledTargetValue) + && DataStoreSyncState.SYNCHRONIZED.equals(dataStoreSyncState)) { + // TODO : This is hard-coded for onap dmi that need to be addressed cpsDataService.deleteDataNode(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, cmHandleId, "/netconf-state", OffsetDateTime.now()); } - CompositeStateUtils.setDataSyncEnabledFlagWithDataSyncState(dataSyncEnabled, compositeState); - inventoryPersistence.saveCmHandleState(cmHandleId, - compositeState); + CompositeStateUtils.setDataSyncEnabledFlagWithDataSyncState(dataSyncEnabledTargetValue, compositeState); + inventoryPersistence.saveCmHandleState(cmHandleId, compositeState); + } else { + throw new CpsException("State mismatch exception.", "Cm-Handle not in READY state. Cm handle state is: " + + compositeState.getCmHandleState()); } } diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/config/embeddedcache/ModuleSetTagCacheConfig.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/config/embeddedcache/ModuleSetTagCacheConfig.java new file mode 100644 index 000000000..a791a3bbd --- /dev/null +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/config/embeddedcache/ModuleSetTagCacheConfig.java @@ -0,0 +1,46 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2023 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.onap.cps.ncmp.api.impl.config.embeddedcache; + +import com.hazelcast.config.MapConfig; +import java.util.Collection; +import java.util.Map; +import org.onap.cps.cache.HazelcastCacheConfig; +import org.onap.cps.spi.model.ModuleReference; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class ModuleSetTagCacheConfig extends HazelcastCacheConfig { + + private static final MapConfig moduleSetTagCacheMapConfig = createMapConfig("moduleSetTagCacheMapConfig"); + + /** + * Map instance for cached ModulesSetTags. + * + * @return configured map of ModuleSetTags + */ + @Bean + public Map<String, Collection<ModuleReference>> moduleSetTagCache() { + return createHazelcastInstance("moduleSetTags", moduleSetTagCacheMapConfig) + .getMap("moduleSetTagCache"); + } +} diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/config/embeddedcache/SynchronizationCacheConfig.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/config/embeddedcache/SynchronizationCacheConfig.java index 8b28717db..62a380ca5 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/config/embeddedcache/SynchronizationCacheConfig.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/config/embeddedcache/SynchronizationCacheConfig.java @@ -23,7 +23,6 @@ package org.onap.cps.ncmp.api.impl.config.embeddedcache; import com.hazelcast.config.MapConfig; import com.hazelcast.config.QueueConfig; import com.hazelcast.map.IMap; -import java.util.Set; import java.util.concurrent.BlockingQueue; import lombok.extern.slf4j.Slf4j; import org.onap.cps.cache.HazelcastCacheConfig; @@ -45,8 +44,6 @@ public class SynchronizationCacheConfig extends HazelcastCacheConfig { private static final MapConfig moduleSyncStartedConfig = createMapConfig("moduleSyncStartedConfig"); private static final MapConfig dataSyncSemaphoresConfig = createMapConfig("dataSyncSemaphoresConfig"); - private static final MapConfig moduleSetTagCacheMapConfig = createMapConfig("moduleSetTagCacheMapConfig"); - /** * Module Sync Distributed Queue Instance. * @@ -77,15 +74,4 @@ public class SynchronizationCacheConfig extends HazelcastCacheConfig { public IMap<String, Boolean> dataSyncSemaphores() { return createHazelcastInstance("dataSyncSemaphores", dataSyncSemaphoresConfig).getMap("dataSyncSemaphores"); } - - /** - * IMap instance for cached ModulesSetTags. - * - * @return configured map of ModuleSetTags - */ - @Bean - public IMap<String, Set<String>> moduleSetTagCache() { - return createHazelcastInstance("moduleSetTags", moduleSetTagCacheMapConfig) - .getMap("moduleSetTagCache"); - } } diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/inventory/sync/ModuleSyncService.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/inventory/sync/ModuleSyncService.java index 8e17ab916..2ea39e8dc 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/inventory/sync/ModuleSyncService.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/inventory/sync/ModuleSyncService.java @@ -26,7 +26,6 @@ import static org.onap.cps.ncmp.api.impl.ncmppersistence.NcmpPersistence.NCMP_DM import static org.onap.cps.ncmp.api.impl.ncmppersistence.NcmpPersistence.NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME; import java.time.OffsetDateTime; -import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.HashMap; @@ -65,51 +64,65 @@ public class ModuleSyncService { private final CmHandleQueries cmHandleQueries; private final CpsDataService cpsDataService; private final JsonObjectMapper jsonObjectMapper; + private final Map<String, Collection<ModuleReference>> moduleSetTagCache; + private static final Map<String, String> NO_NEW_MODULES = Collections.emptyMap(); /** * This method registers a cm handle and initiates modules sync. * - * @param upgradedCmHandle the yang model of cm handle. + * @param yangModelCmHandle the yang model of cm handle. */ - public void syncAndCreateOrUpgradeSchemaSetAndAnchor(final YangModelCmHandle upgradedCmHandle) { + public void syncAndCreateOrUpgradeSchemaSetAndAnchor(final YangModelCmHandle yangModelCmHandle) { - final String moduleSetTag = extractModuleSetTag(upgradedCmHandle.getCompositeState()); - final Optional<DataNode> existingCmHandleWithSameModuleSetTag - = getFirstReadyDataNodeWithModuleSetTag(moduleSetTag); + final String moduleSetTag; + final String cmHandleId = yangModelCmHandle.getId(); + final CompositeState compositeState = yangModelCmHandle.getCompositeState(); + final boolean inUpgrade = isInUpgrade(compositeState); - if (existingCmHandleWithSameModuleSetTag.isPresent()) { - upgradeUsingModuleSetTag(upgradedCmHandle, moduleSetTag); + if (inUpgrade) { + moduleSetTag = extractModuleSetTag(compositeState); } else { - syncAndCreateSchemaSetAndAnchor(upgradedCmHandle); + moduleSetTag = yangModelCmHandle.getModuleSetTag(); } - setCmHandleModuleSetTag(upgradedCmHandle, moduleSetTag); + + final Collection<ModuleReference> moduleReferencesFromCache = moduleSetTagCache.get(moduleSetTag); + + if (moduleReferencesFromCache == null) { + final Optional<DataNode> optionalExistingCmHandleWithSameModuleSetTag + = getFirstReadyDataNodeWithModuleSetTag(moduleSetTag); + + if (optionalExistingCmHandleWithSameModuleSetTag.isPresent()) { + final String existingCmHandleAnchorName + = optionalExistingCmHandleWithSameModuleSetTag.get().getAnchorName(); + createOrUpgradeSchemaSetUsingModuleSetTag(cmHandleId, moduleSetTag, existingCmHandleAnchorName); + } else { + syncAndCreateSchemaSet(yangModelCmHandle, moduleSetTag); + } + } else { + cpsModuleService.createOrUpgradeSchemaSetFromModules(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, + cmHandleId, NO_NEW_MODULES, moduleReferencesFromCache); + } + if (!inUpgrade) { + cpsAdminService.createAnchor(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, cmHandleId, cmHandleId); + } + setCmHandleModuleSetTag(yangModelCmHandle, moduleSetTag); } - private void syncAndCreateSchemaSetAndAnchor(final YangModelCmHandle yangModelCmHandle) { + private void syncAndCreateSchemaSet(final YangModelCmHandle yangModelCmHandle, final String moduleSetTag) { final Collection<ModuleReference> allModuleReferencesFromCmHandle = dmiModelOperations.getModuleReferences(yangModelCmHandle); - final Collection<ModuleReference> identifiedNewModuleReferencesFromCmHandle = cpsModuleService .identifyNewModuleReferences(allModuleReferencesFromCmHandle); - final Map<String, String> newModuleNameToContentMap; if (identifiedNewModuleReferencesFromCmHandle.isEmpty()) { - newModuleNameToContentMap = Collections.emptyMap(); + newModuleNameToContentMap = NO_NEW_MODULES; } else { newModuleNameToContentMap = dmiModelOperations.getNewYangResourcesFromDmi(yangModelCmHandle, identifiedNewModuleReferencesFromCmHandle); } - createSchemaSetAndAnchor(yangModelCmHandle, newModuleNameToContentMap, allModuleReferencesFromCmHandle); - } - - private void createSchemaSetAndAnchor(final YangModelCmHandle yangModelCmHandle, - final Map<String, String> newModuleNameToContentMap, - final Collection<ModuleReference> allModuleReferencesFromCmHandle) { - final String schemaSetAndAnchorName = yangModelCmHandle.getId(); cpsModuleService.createOrUpgradeSchemaSetFromModules(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, - schemaSetAndAnchorName, newModuleNameToContentMap, allModuleReferencesFromCmHandle); - cpsAdminService.createAnchor(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, schemaSetAndAnchorName, - schemaSetAndAnchorName); + yangModelCmHandle.getId(), newModuleNameToContentMap, allModuleReferencesFromCmHandle); + moduleSetTagCache.put(moduleSetTag, allModuleReferencesFromCmHandle); } /** @@ -147,21 +160,24 @@ public class ModuleSyncService { jsonObjectMapper.asJsonString(dmiRegistryProperties), OffsetDateTime.now()); } - private void upgradeUsingModuleSetTag(final YangModelCmHandle upgradedCmHandle, final String moduleSetTag) { + private void createOrUpgradeSchemaSetUsingModuleSetTag(final String schemaSetName, + final String moduleSetTag, + final String existingCmHandleAnchorName) { log.info("Found cm handle having module set tag: {}", moduleSetTag); final Collection<ModuleReference> moduleReferencesFromExistingCmHandle = - cpsModuleService.getYangResourcesModuleReferences(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR); - final String upgradedSchemaSetAndAnchorName = upgradedCmHandle.getId(); - final Map<String, String> noNewModules = Collections.emptyMap(); + cpsModuleService.getYangResourcesModuleReferences(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, + existingCmHandleAnchorName); cpsModuleService.createOrUpgradeSchemaSetFromModules(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, - upgradedSchemaSetAndAnchorName, noNewModules, moduleReferencesFromExistingCmHandle); + schemaSetName, NO_NEW_MODULES, moduleReferencesFromExistingCmHandle); + moduleSetTagCache.put(moduleSetTag, moduleReferencesFromExistingCmHandle); } private static String extractModuleSetTag(final CompositeState compositeState) { - return compositeState.getLockReason() != null && compositeState.getLockReason().getLockReasonCategory() - == LockReasonCategory.MODULE_UPGRADE - ? Arrays.stream(compositeState.getLockReason().getDetails().split(":")).toList().get(1).trim() - : StringUtils.EMPTY; + return compositeState.getLockReason().getDetails().split(":")[1].trim(); } + private static boolean isInUpgrade(final CompositeState compositeState) { + return compositeState.getLockReason() != null && LockReasonCategory.MODULE_UPGRADE.equals( + compositeState.getLockReason().getLockReasonCategory()); + } } diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/inventory/sync/ModuleSyncWatchdog.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/inventory/sync/ModuleSyncWatchdog.java index 75781eb1b..bf005051f 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/inventory/sync/ModuleSyncWatchdog.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/inventory/sync/ModuleSyncWatchdog.java @@ -25,7 +25,6 @@ import com.hazelcast.map.IMap; import java.util.Collection; import java.util.HashSet; import java.util.List; -import java.util.Set; import java.util.concurrent.BlockingQueue; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; @@ -49,7 +48,6 @@ public class ModuleSyncWatchdog { private final IMap<String, Object> moduleSyncStartedOnCmHandles; private final ModuleSyncTasks moduleSyncTasks; private final AsyncTaskExecutor asyncTaskExecutor; - private final IMap<String, Set<String>> moduleSetTagCache; private static final int MODULE_SYNC_BATCH_SIZE = 100; private static final long PREVENT_CPU_BURN_WAIT_TIME_MILLIS = 10; private static final String VALUE_FOR_HAZELCAST_IN_PROGRESS_MAP = "Started"; diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/yangmodels/YangModelCmHandle.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/yangmodels/YangModelCmHandle.java index d148f371b..f930d5b71 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/yangmodels/YangModelCmHandle.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/yangmodels/YangModelCmHandle.java @@ -34,6 +34,7 @@ import lombok.EqualsAndHashCode; import lombok.Getter; import lombok.NoArgsConstructor; import lombok.Setter; +import org.apache.commons.lang3.StringUtils; import org.onap.cps.ncmp.api.impl.inventory.CompositeState; import org.onap.cps.ncmp.api.impl.operations.RequiredDmiService; import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle; @@ -112,7 +113,7 @@ public class YangModelCmHandle { yangModelCmHandle.setDmiServiceName(dmiServiceName); yangModelCmHandle.setDmiDataServiceName(dmiDataServiceName); yangModelCmHandle.setDmiModelServiceName(dmiModelServiceName); - yangModelCmHandle.setModuleSetTag(moduleSetTag); + yangModelCmHandle.setModuleSetTag(moduleSetTag == null ? StringUtils.EMPTY : moduleSetTag); yangModelCmHandle.setDmiProperties(asYangModelCmHandleProperties(ncmpServiceCmHandle.getDmiProperties())); yangModelCmHandle.setPublicProperties(asYangModelCmHandleProperties( ncmpServiceCmHandle.getPublicProperties())); |