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.io.Closeable;
24 import java.util.concurrent.Executors;
25 import java.util.concurrent.ScheduledExecutorService;
26 import java.util.concurrent.TimeUnit;
27 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoop;
28 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElement;
29 import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ControlLoopProvider;
30 import org.onap.policy.models.base.PfModelException;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
35 * This class is used to scan the control loops in the database and check if they are in the correct state.
37 public class SupervisionScanner implements Runnable, Closeable {
38 private static final Logger LOGGER = LoggerFactory.getLogger(SupervisionScanner.class);
40 private ControlLoopProvider controlLoopProvider;
41 private ScheduledExecutorService timerPool;
44 * Constructor for instantiating SupervisionScanner.
46 * @param controlLoopProvider the provider to use to read control loops from the database
47 * @param interval time interval to perform scans
49 public SupervisionScanner(final ControlLoopProvider controlLoopProvider, final long interval) {
50 this.controlLoopProvider = controlLoopProvider;
53 timerPool = makeTimerPool();
54 timerPool.scheduleAtFixedRate(this, 0, interval, TimeUnit.SECONDS);
59 LOGGER.debug("Scanning control loops in the database . . .");
62 for (ControlLoop controlLoop : controlLoopProvider.getControlLoops(null, null)) {
63 scanControlLoop(controlLoop);
65 } catch (PfModelException pfme) {
66 LOGGER.warn("error reading control loops from database", pfme);
69 LOGGER.debug("Control loop scan complete . . .");
77 private void scanControlLoop(final ControlLoop controlLoop) throws PfModelException {
78 LOGGER.debug("scanning control loop {} . . .", controlLoop.getKey().asIdentifier());
80 if (controlLoop.getState().equals(controlLoop.getOrderedState().asState())) {
81 LOGGER.debug("control loop {} scanned, OK", controlLoop.getKey().asIdentifier());
85 for (ControlLoopElement element : controlLoop.getElements().values()) {
86 if (!element.getState().equals(element.getOrderedState().asState())) {
87 LOGGER.debug("control loop scan: transitioning from state {} to {}", controlLoop.getState(),
88 controlLoop.getOrderedState());
93 LOGGER.debug("control loop scan: transition from state {} to {} completed", controlLoop.getState(),
94 controlLoop.getOrderedState());
96 controlLoop.setState(controlLoop.getOrderedState().asState());
97 controlLoopProvider.updateControlLoop(controlLoop);
101 * Makes a new timer pool.
103 * @return a new timer pool
105 protected ScheduledExecutorService makeTimerPool() {
106 return Executors.newScheduledThreadPool(1);