diff options
author | emaclee <lee.anjella.macabuhay@est.tech> | 2024-12-11 14:58:25 +0000 |
---|---|---|
committer | Lee Anjella Macabuhay <lee.anjella.macabuhay@est.tech> | 2025-01-07 12:02:29 +0000 |
commit | 0d61c432b2604f0459dea95bbd328c279b04fbef (patch) | |
tree | 3cd35b93ed02048699006082ad1086c986c0990f /cps-ncmp-service/src/main/java | |
parent | ffac9f27f7183d8e80aa74a7b113f68dcadb3f87 (diff) |
Add gauge metric for NCMP "cmhandle states"
Issue-ID: CPS-2456
Change-Id: I1aebcc68dfdc9c48c230c74376742d67b05c0615
Signed-off-by: emaclee <lee.anjella.macabuhay@est.tech>
Diffstat (limited to 'cps-ncmp-service/src/main/java')
3 files changed, 153 insertions, 2 deletions
diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/cache/AdminCacheConfig.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/cache/AdminCacheConfig.java new file mode 100644 index 0000000000..a29799b13f --- /dev/null +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/cache/AdminCacheConfig.java @@ -0,0 +1,49 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2025 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.impl.cache; + +import com.hazelcast.config.MapConfig; +import com.hazelcast.map.IMap; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class AdminCacheConfig extends HazelcastCacheConfig { + + private static final MapConfig adminCacheMapConfig = createMapConfig("adminCacheMapConfig"); + + /** + * Distributed instance admin cache map for cm handles by state for use of gauge metrics. + * + * @return configured map of cm handles by state. + */ + @Bean + public IMap<String, Integer> cmHandlesByState() { + final IMap<String, Integer> cmHandlesByState = getOrCreateHazelcastInstance(adminCacheMapConfig).getMap( + "cmHandlesByState"); + cmHandlesByState.putIfAbsent("advisedCmHandlesCount", 0); + cmHandlesByState.putIfAbsent("readyCmHandlesCount", 0); + cmHandlesByState.putIfAbsent("lockedCmHandlesCount", 0); + cmHandlesByState.putIfAbsent("deletingCmHandlesCount", 0); + cmHandlesByState.putIfAbsent("deletedCmHandlesCount", 0); + return cmHandlesByState; + } +} diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/sync/lcm/CmHandleStateMonitor.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/sync/lcm/CmHandleStateMonitor.java new file mode 100644 index 0000000000..4fd752c0d1 --- /dev/null +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/sync/lcm/CmHandleStateMonitor.java @@ -0,0 +1,100 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2025 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.impl.inventory.sync.lcm; + +import com.hazelcast.map.EntryProcessor; +import com.hazelcast.map.IMap; +import java.util.Collection; +import java.util.Map; +import lombok.RequiredArgsConstructor; +import org.onap.cps.ncmp.api.inventory.models.CompositeState; +import org.onap.cps.ncmp.impl.inventory.models.CmHandleState; +import org.onap.cps.ncmp.impl.inventory.sync.lcm.LcmEventsCmHandleStateHandlerImpl.CmHandleTransitionPair; +import org.springframework.scheduling.annotation.Async; +import org.springframework.stereotype.Component; + +@Component +@RequiredArgsConstructor +public class CmHandleStateMonitor { + + private static final String METRIC_POSTFIX = "CmHandlesCount"; + final IMap<String, Integer> cmHandlesByState; + + /** + * Asynchronously update the cm handle state metrics. + * + * @param cmHandleTransitionPairs cm handle transition pairs + */ + @Async + public void updateCmHandleStateMetrics(final Collection<CmHandleTransitionPair> + cmHandleTransitionPairs) { + cmHandleTransitionPairs.forEach(this::updateMetricWithStateChange); + } + + private void updateMetricWithStateChange(final CmHandleTransitionPair cmHandleTransitionPair) { + final CmHandleState targetCmHandleState = cmHandleTransitionPair.getTargetYangModelCmHandle() + .getCompositeState().getCmHandleState(); + if (isNew(cmHandleTransitionPair.getCurrentYangModelCmHandle().getCompositeState())) { + updateTargetStateCount(targetCmHandleState); + } else { + final CmHandleState previousCmHandleState = cmHandleTransitionPair.getCurrentYangModelCmHandle() + .getCompositeState().getCmHandleState(); + updatePreviousStateCount(previousCmHandleState); + updateTargetStateCount(targetCmHandleState); + } + } + + private void updatePreviousStateCount(final CmHandleState previousCmHandleState) { + final String keyName = previousCmHandleState.name().toLowerCase() + METRIC_POSTFIX; + cmHandlesByState.executeOnKey(keyName, new DecreasingEntryProcessor()); + } + + private void updateTargetStateCount(final CmHandleState targetCmHandleState) { + final String keyName = targetCmHandleState.name().toLowerCase() + METRIC_POSTFIX; + cmHandlesByState.executeOnKey(keyName, new IncreasingEntryProcessor()); + } + + private boolean isNew(final CompositeState existingCompositeState) { + return (existingCompositeState == null); + } + + static class DecreasingEntryProcessor implements EntryProcessor<String, Integer, Void> { + @Override + public Void process(final Map.Entry<String, Integer> entry) { + final int currentValue = entry.getValue(); + if (currentValue > 0) { + entry.setValue(currentValue - 1); + } + return null; + } + } + + static class IncreasingEntryProcessor implements EntryProcessor<String, Integer, Void> { + @Override + public Void process(final Map.Entry<String, Integer> entry) { + final int currentValue = entry.getValue(); + entry.setValue(currentValue + 1); + return null; + } + } + + +}
\ No newline at end of file diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/sync/lcm/LcmEventsCmHandleStateHandlerImpl.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/sync/lcm/LcmEventsCmHandleStateHandlerImpl.java index e9bd37219a..3e6597ea39 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/sync/lcm/LcmEventsCmHandleStateHandlerImpl.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/sync/lcm/LcmEventsCmHandleStateHandlerImpl.java @@ -1,6 +1,6 @@ /* * ============LICENSE_START======================================================= - * Copyright (C) 2022-2024 Nordix Foundation + * Copyright (C) 2022-2025 Nordix Foundation * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -51,6 +51,7 @@ public class LcmEventsCmHandleStateHandlerImpl implements LcmEventsCmHandleState private final InventoryPersistence inventoryPersistence; private final LcmEventsCmHandleStateHandlerAsyncHelper lcmEventsCmHandleStateHandlerAsyncHelper; + private final CmHandleStateMonitor cmHandleStateMonitor; @Override @Timed(value = "cps.ncmp.cmhandle.state.update.batch", @@ -60,6 +61,7 @@ public class LcmEventsCmHandleStateHandlerImpl implements LcmEventsCmHandleState prepareCmHandleTransitionBatch(cmHandleStatePerCmHandle); persistCmHandleBatch(cmHandleTransitionPairs); lcmEventsCmHandleStateHandlerAsyncHelper.publishLcmEventBatchAsynchronously(cmHandleTransitionPairs); + cmHandleStateMonitor.updateCmHandleStateMetrics(cmHandleTransitionPairs); } @Override @@ -168,7 +170,7 @@ public class LcmEventsCmHandleStateHandlerImpl implements LcmEventsCmHandleState @Getter @Setter @NoArgsConstructor - static class CmHandleTransitionPair { + public static class CmHandleTransitionPair { private YangModelCmHandle currentYangModelCmHandle; private YangModelCmHandle targetYangModelCmHandle; |