/*
- * ============LICENSE_START=======================================================
+ * ============LICENSE_START=======================================================
* Copyright (C) 2022 Nordix Foundation
* Modifications Copyright (C) 2022 Bell Canada
* ================================================================================
package org.onap.cps.ncmp.api.inventory.sync;
-import static org.onap.ncmp.cmhandle.lcm.event.Event.Operation.CREATE;
-
import java.util.List;
+import java.util.concurrent.ConcurrentMap;
+import java.util.function.Consumer;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
-import org.onap.cps.ncmp.api.impl.event.NcmpEventsService;
import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle;
import org.onap.cps.ncmp.api.inventory.CmHandleState;
import org.onap.cps.ncmp.api.inventory.CompositeState;
-import org.onap.cps.ncmp.api.inventory.CompositeState.LockReason;
+import org.onap.cps.ncmp.api.inventory.DataStoreSyncState;
import org.onap.cps.ncmp.api.inventory.InventoryPersistence;
import org.onap.cps.ncmp.api.inventory.LockReasonCategory;
import org.springframework.scheduling.annotation.Scheduled;
private final ModuleSyncService moduleSyncService;
- private final NcmpEventsService ncmpEventsService;
+ private final ConcurrentMap<String, Boolean> moduleSyncSemaphoreMap;
/**
* Execute Cm Handle poll which changes the cm handle state from 'ADVISED' to 'READY'.
- * Also publish the LCM Create Event when cm handle state is moved to 'READY'.
*/
@Scheduled(fixedDelayString = "${timers.advised-modules-sync.sleep-time-ms:30000}")
public void executeAdvisedCmHandlePoll() {
- YangModelCmHandle advisedCmHandle = syncUtils.getAnAdvisedCmHandle();
- while (advisedCmHandle != null) {
+ syncUtils.getAdvisedCmHandles().forEach(advisedCmHandle -> {
final String cmHandleId = advisedCmHandle.getId();
- final CompositeState compositeState = inventoryPersistence.getCmHandleState(cmHandleId);
- try {
- moduleSyncService.syncAndCreateSchemaSetAndAnchor(advisedCmHandle);
- compositeState.setCmHandleState(CmHandleState.READY);
- } catch (final Exception e) {
- compositeState.setCmHandleState(CmHandleState.LOCKED);
- syncUtils.updateLockReasonDetailsAndAttempts(compositeState,
- LockReasonCategory.LOCKED_MISBEHAVING,
- e.getMessage());
- }
- compositeState.setLastUpdateTimeNow();
- inventoryPersistence.saveCmHandleState(cmHandleId, compositeState);
- log.info("{} is now in {} state", cmHandleId,
- advisedCmHandle.getCompositeState().getCmHandleState());
- if (compositeState.getCmHandleState() == CmHandleState.READY) {
- log.debug("Publishing LCM Create Event for cmHandleId : {}", cmHandleId);
- ncmpEventsService.publishNcmpEvent(cmHandleId, CREATE);
+ if (hasPushedIntoSemaphoreMap(cmHandleId)) {
+ log.debug("executing module sync on {}", cmHandleId);
+ final CompositeState compositeState = inventoryPersistence.getCmHandleState(cmHandleId);
+ try {
+ moduleSyncService.deleteSchemaSetIfExists(advisedCmHandle);
+ moduleSyncService.syncAndCreateSchemaSetAndAnchor(advisedCmHandle);
+ setCompositeStateToReadyWithInitialDataStoreSyncState().accept(compositeState);
+ updateModuleSyncSemaphoreMap(cmHandleId);
+ } catch (final Exception e) {
+ setCompositeStateToLocked().accept(compositeState);
+ syncUtils.updateLockReasonDetailsAndAttempts(compositeState,
+ LockReasonCategory.LOCKED_MODULE_SYNC_FAILED, e.getMessage());
+ }
+ inventoryPersistence.saveCmHandleState(cmHandleId, compositeState);
+ log.debug("{} is now in {} state", cmHandleId, compositeState.getCmHandleState().name());
+ } else {
+ log.debug("{} already processed by another instance", cmHandleId);
}
- advisedCmHandle = syncUtils.getAnAdvisedCmHandle();
- }
+ });
log.debug("No Cm-Handles currently found in an ADVISED state");
}
* Execute Cm Handle poll which changes the cm handle state from 'LOCKED' to 'ADVISED'.
*/
@Scheduled(fixedDelayString = "${timers.locked-modules-sync.sleep-time-ms:300000}")
- public void executeLockedMisbehavingCmHandlePoll() {
- final List<YangModelCmHandle> lockedMisbehavingCmHandles = syncUtils.getLockedMisbehavingCmHandles();
- for (final YangModelCmHandle lockedMisbehavingModelCmHandle: lockedMisbehavingCmHandles) {
- final CompositeState updatedCompositeState = lockedMisbehavingModelCmHandle.getCompositeState();
- updatedCompositeState.setCmHandleState(CmHandleState.ADVISED);
- updatedCompositeState.setLastUpdateTimeNow();
- updatedCompositeState.setLockReason(LockReason.builder()
- .details(updatedCompositeState.getLockReason().getDetails()).build());
- log.debug("Locked misbehaving cm handle {} is being recycled", lockedMisbehavingModelCmHandle.getId());
- inventoryPersistence.saveCmHandleState(lockedMisbehavingModelCmHandle.getId(), updatedCompositeState);
+ public void executeLockedCmHandlePoll() {
+ final List<YangModelCmHandle> lockedCmHandles = syncUtils.getModuleSyncFailedCmHandles();
+ for (final YangModelCmHandle lockedCmHandle : lockedCmHandles) {
+ final CompositeState compositeState = lockedCmHandle.getCompositeState();
+ final boolean isReadyForRetry = syncUtils.isReadyForRetry(compositeState);
+ if (isReadyForRetry) {
+ setCompositeStateToAdvisedAndRetainOldLockReasonDetails(compositeState);
+ log.debug("Locked cm handle {} is being re-synced", lockedCmHandle.getId());
+ inventoryPersistence.saveCmHandleState(lockedCmHandle.getId(), compositeState);
+ }
}
}
+
+ private Consumer<CompositeState> setCompositeStateToLocked() {
+ return compositeState -> {
+ compositeState.setCmHandleState(CmHandleState.LOCKED);
+ compositeState.setLastUpdateTimeNow();
+ };
+ }
+
+ private Consumer<CompositeState> setCompositeStateToReadyWithInitialDataStoreSyncState() {
+ return compositeState -> {
+ compositeState.setDataSyncEnabled(false);
+ compositeState.setCmHandleState(CmHandleState.READY);
+ final CompositeState.Operational operational = getDataStoreSyncState();
+ final CompositeState.DataStores dataStores = CompositeState.DataStores.builder()
+ .operationalDataStore(operational)
+ .build();
+ compositeState.setDataStores(dataStores);
+ };
+ }
+
+ private void setCompositeStateToAdvisedAndRetainOldLockReasonDetails(final CompositeState compositeState) {
+ compositeState.setCmHandleState(CmHandleState.ADVISED);
+ compositeState.setLastUpdateTimeNow();
+ final String oldLockReasonDetails = compositeState.getLockReason().getDetails();
+ final CompositeState.LockReason lockReason = CompositeState.LockReason.builder()
+ .details(oldLockReasonDetails).build();
+ compositeState.setLockReason(lockReason);
+ }
+
+ private CompositeState.Operational getDataStoreSyncState() {
+ final DataStoreSyncState dataStoreSyncState = DataStoreSyncState.NONE_REQUESTED;
+ return CompositeState.Operational.builder().dataStoreSyncState(dataStoreSyncState).build();
+ }
+
+ private void updateModuleSyncSemaphoreMap(final String cmHandleId) {
+ moduleSyncSemaphoreMap.replace(cmHandleId, true);
+ }
+
+ private boolean hasPushedIntoSemaphoreMap(final String cmHandleId) {
+ return moduleSyncSemaphoreMap.putIfAbsent(cmHandleId, false) == null;
+ }
}