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.Collection;
25 import java.util.List;
26 import java.util.TimerTask;
27 import java.util.concurrent.Callable;
28 import java.util.concurrent.ExecutionException;
29 import java.util.concurrent.Executors;
30 import java.util.concurrent.Future;
31 import java.util.concurrent.ScheduledExecutorService;
32 import java.util.concurrent.ScheduledFuture;
33 import java.util.concurrent.TimeUnit;
34 import java.util.concurrent.TimeoutException;
35 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoop;
36 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElement;
37 import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ControlLoopProvider;
38 import org.onap.policy.models.base.PfModelException;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
43 * This class is used to scan the control loops in the database and check if they are in the correct state.
45 public class SupervisionScanner implements Runnable, Closeable {
46 private static final Logger LOGGER = LoggerFactory.getLogger(SupervisionScanner.class);
48 private ControlLoopProvider controlLoopProvider;
49 private ScheduledExecutorService timerPool;
52 * Constructor for instantiating SupervisionScanner.
54 * @param controlLoopProvider the provider to use to read control loops from the database
55 * @param interval time interval to perform scans
57 public SupervisionScanner(final ControlLoopProvider controlLoopProvider, final long interval) {
58 this.controlLoopProvider = controlLoopProvider;
61 timerPool = makeTimerPool();
62 timerPool.scheduleAtFixedRate(this, 0, interval, TimeUnit.SECONDS);
67 LOGGER.debug("Scanning control loops in the database . . .");
70 for (ControlLoop controlLoop : controlLoopProvider.getControlLoops(null, null)) {
71 scanControlLoop(controlLoop);
73 } catch (PfModelException pfme) {
74 LOGGER.warn("error reading control loops from database", pfme);
77 LOGGER.debug("Control loop scan complete . . .");
85 private void scanControlLoop(final ControlLoop controlLoop) throws PfModelException {
86 LOGGER.debug("scanning control loop {} . . .", controlLoop.getKey().asIdentifier());
88 if (controlLoop.getState().equals(controlLoop.getOrderedState().asState())) {
89 LOGGER.debug("control loop {} scanned, OK", controlLoop.getKey().asIdentifier());
93 for (ControlLoopElement element : controlLoop.getElements().values()) {
94 if (!element.getState().equals(element.getOrderedState().asState())) {
95 LOGGER.debug("control loop scan: transitioning from state {} to {}", controlLoop.getState(),
96 controlLoop.getOrderedState());
101 LOGGER.debug("control loop scan: transition from state {} to {} completed", controlLoop.getState(),
102 controlLoop.getOrderedState());
104 controlLoop.setState(controlLoop.getOrderedState().asState());
105 controlLoopProvider.updateControlLoop(controlLoop);
109 * Makes a new timer pool.
111 * @return a new timer pool
113 protected ScheduledExecutorService makeTimerPool() {
114 return Executors.newScheduledThreadPool(1);