0ccfddff3467826f7e0a4f2376141c97594e67f7
[policy/clamp.git] /
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.clamp.controlloop.runtime.supervision;
22
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;
41
42 /**
43  * This class is used to scan the control loops in the database and check if they are in the correct state.
44  */
45 public class SupervisionScanner implements Runnable, Closeable {
46     private static final Logger LOGGER = LoggerFactory.getLogger(SupervisionScanner.class);
47
48     private ControlLoopProvider controlLoopProvider;
49     private ScheduledExecutorService timerPool;
50
51     /**
52      * Constructor for instantiating SupervisionScanner.
53      *
54      * @param controlLoopProvider the provider to use to read control loops from the database
55      * @param interval time interval to perform scans
56      */
57     public SupervisionScanner(final ControlLoopProvider controlLoopProvider, final long interval) {
58         this.controlLoopProvider = controlLoopProvider;
59
60         // Kick off the timer
61         timerPool = makeTimerPool();
62         timerPool.scheduleAtFixedRate(this, 0, interval, TimeUnit.SECONDS);
63     }
64
65     @Override
66     public void run() {
67         LOGGER.debug("Scanning control loops in the database . . .");
68
69         try {
70             for (ControlLoop controlLoop : controlLoopProvider.getControlLoops(null, null)) {
71                 scanControlLoop(controlLoop);
72             }
73         } catch (PfModelException pfme) {
74             LOGGER.warn("error reading control loops from database", pfme);
75         }
76
77         LOGGER.debug("Control loop scan complete . . .");
78     }
79
80     @Override
81     public void close() {
82         timerPool.shutdown();
83     }
84
85     private void scanControlLoop(final ControlLoop controlLoop) throws PfModelException {
86         LOGGER.debug("scanning control loop {} . . .", controlLoop.getKey().asIdentifier());
87
88         if (controlLoop.getState().equals(controlLoop.getOrderedState().asState())) {
89             LOGGER.debug("control loop {} scanned, OK", controlLoop.getKey().asIdentifier());
90             return;
91         }
92
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());
97                 return;
98             }
99         }
100
101         LOGGER.debug("control loop scan: transition from state {} to {} completed", controlLoop.getState(),
102                 controlLoop.getOrderedState());
103
104         controlLoop.setState(controlLoop.getOrderedState().asState());
105         controlLoopProvider.updateControlLoop(controlLoop);
106     }
107
108     /**
109      * Makes a new timer pool.
110      *
111      * @return a new timer pool
112      */
113     protected ScheduledExecutorService makeTimerPool() {
114         return Executors.newScheduledThreadPool(1);
115     }
116 }