2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 Nordix Foundation.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.clamp.controlloop.runtime.supervision;
23 import java.util.HashMap;
24 import java.util.HashSet;
29 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoop;
30 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElement;
31 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopState;
32 import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ControlLoopProvider;
33 import org.onap.policy.clamp.controlloop.runtime.main.parameters.ClRuntimeParameterGroup;
34 import org.onap.policy.clamp.controlloop.runtime.supervision.comm.ControlLoopStateChangePublisher;
35 import org.onap.policy.clamp.controlloop.runtime.supervision.comm.ControlLoopUpdatePublisher;
36 import org.onap.policy.models.base.PfModelException;
37 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40 import org.springframework.stereotype.Component;
43 * This class is used to scan the control loops in the database and check if they are in the correct state.
46 public class SupervisionScanner {
47 private static final Logger LOGGER = LoggerFactory.getLogger(SupervisionScanner.class);
51 static class HandleCounter {
52 private int maxRetryCount;
53 private long maxWaitMs;
54 private Map<ToscaConceptIdentifier, Integer> mapCounter = new HashMap<>();
55 private Set<ToscaConceptIdentifier> mapFault = new HashSet<>();
57 public void clear(ToscaConceptIdentifier id) {
58 mapCounter.put(id, 0);
62 public void setFault(ToscaConceptIdentifier id) {
63 mapCounter.put(id, 0);
67 public boolean count(ToscaConceptIdentifier id) {
68 int counter = mapCounter.getOrDefault(id, 0) + 1;
69 if (counter <= maxRetryCount) {
70 mapCounter.put(id, counter);
76 public boolean isFault(ToscaConceptIdentifier id) {
77 return mapFault.contains(id);
80 public int getCounter(ToscaConceptIdentifier id) {
81 return mapCounter.getOrDefault(id, 0);
85 private HandleCounter stateChange = new HandleCounter();
87 private final ControlLoopProvider controlLoopProvider;
88 private final ControlLoopStateChangePublisher controlLoopStateChangePublisher;
89 private final ControlLoopUpdatePublisher controlLoopUpdatePublisher;
92 * Constructor for instantiating SupervisionScanner.
94 * @param controlLoopProvider the provider to use to read control loops from the database
95 * @param controlLoopStateChangePublisher the ControlLoopStateChange Publisher
96 * @param clRuntimeParameterGroup the parameters for the control loop runtime
98 public SupervisionScanner(final ControlLoopProvider controlLoopProvider,
99 final ControlLoopStateChangePublisher controlLoopStateChangePublisher,
100 ControlLoopUpdatePublisher controlLoopUpdatePublisher,
101 final ClRuntimeParameterGroup clRuntimeParameterGroup) {
102 this.controlLoopProvider = controlLoopProvider;
103 this.controlLoopStateChangePublisher = controlLoopStateChangePublisher;
104 this.controlLoopUpdatePublisher = controlLoopUpdatePublisher;
106 stateChange.setMaxRetryCount(
107 clRuntimeParameterGroup.getParticipantParameters().getUpdateParameters().getMaxRetryCount());
108 stateChange.setMaxWaitMs(
109 clRuntimeParameterGroup.getParticipantParameters().getUpdateParameters().getMaxWaitMs());
115 * @param counterCheck if true activate counter and retry
117 public void run(boolean counterCheck) {
118 LOGGER.debug("Scanning control loops in the database . . .");
121 for (ControlLoop controlLoop : controlLoopProvider.getControlLoops(null, null)) {
122 scanControlLoop(controlLoop, counterCheck);
124 } catch (PfModelException pfme) {
125 LOGGER.warn("error reading control loops from database", pfme);
128 LOGGER.debug("Control loop scan complete . . .");
131 private void scanControlLoop(final ControlLoop controlLoop, boolean counterCheck) throws PfModelException {
132 LOGGER.debug("scanning control loop {} . . .", controlLoop.getKey().asIdentifier());
134 if (controlLoop.getState().equals(controlLoop.getOrderedState().asState())) {
135 LOGGER.debug("control loop {} scanned, OK", controlLoop.getKey().asIdentifier());
137 // Clear missed report counter on Control Loop
138 clearFaultAndCounter(controlLoop);
142 boolean completed = true;
143 for (ControlLoopElement element : controlLoop.getElements().values()) {
144 if (!element.getState().equals(element.getOrderedState().asState())) {
151 LOGGER.debug("control loop scan: transition from state {} to {} completed", controlLoop.getState(),
152 controlLoop.getOrderedState());
154 controlLoop.setState(controlLoop.getOrderedState().asState());
155 controlLoopProvider.updateControlLoop(controlLoop);
157 // Clear missed report counter on Control Loop
158 clearFaultAndCounter(controlLoop);
160 LOGGER.debug("control loop scan: transition from state {} to {} not completed", controlLoop.getState(),
161 controlLoop.getOrderedState());
163 handleCounter(controlLoop);
168 private void clearFaultAndCounter(ControlLoop controlLoop) {
169 stateChange.clear(controlLoop.getKey().asIdentifier());
172 private void handleCounter(ControlLoop controlLoop) {
173 ToscaConceptIdentifier id = controlLoop.getKey().asIdentifier();
174 if (stateChange.isFault(id)) {
175 LOGGER.debug("report ControlLoop fault");
179 if (stateChange.count(id)) {
180 if (ControlLoopState.UNINITIALISED2PASSIVE.equals(controlLoop.getState())) {
181 LOGGER.debug("retry message ControlLoopUpdate");
182 controlLoopUpdatePublisher.send(controlLoop);
184 LOGGER.debug("retry message ControlLoopStateChange");
185 controlLoopStateChangePublisher.send(controlLoop);
188 LOGGER.debug("report ControlLoop fault");
189 stateChange.setFault(id);