4f3faf8af087f35458756954d8c6515be1a37f08
[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.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;
33
34 /**
35  * This class is used to scan the control loops in the database and check if they are in the correct state.
36  */
37 public class SupervisionScanner implements Runnable, Closeable {
38     private static final Logger LOGGER = LoggerFactory.getLogger(SupervisionScanner.class);
39
40     private ControlLoopProvider controlLoopProvider;
41     private ScheduledExecutorService timerPool;
42
43     /**
44      * Constructor for instantiating SupervisionScanner.
45      *
46      * @param controlLoopProvider the provider to use to read control loops from the database
47      * @param interval time interval to perform scans
48      */
49     public SupervisionScanner(final ControlLoopProvider controlLoopProvider, final long interval) {
50         this.controlLoopProvider = controlLoopProvider;
51
52         // Kick off the timer
53         timerPool = makeTimerPool();
54         timerPool.scheduleAtFixedRate(this, 0, interval, TimeUnit.SECONDS);
55     }
56
57     @Override
58     public void run() {
59         LOGGER.debug("Scanning control loops in the database . . .");
60
61         try {
62             for (ControlLoop controlLoop : controlLoopProvider.getControlLoops(null, null)) {
63                 scanControlLoop(controlLoop);
64             }
65         } catch (PfModelException pfme) {
66             LOGGER.warn("error reading control loops from database", pfme);
67         }
68
69         LOGGER.debug("Control loop scan complete . . .");
70     }
71
72     @Override
73     public void close() {
74         timerPool.shutdown();
75     }
76
77     private void scanControlLoop(final ControlLoop controlLoop) throws PfModelException {
78         LOGGER.debug("scanning control loop {} . . .", controlLoop.getKey().asIdentifier());
79
80         if (controlLoop.getState().equals(controlLoop.getOrderedState().asState())) {
81             LOGGER.debug("control loop {} scanned, OK", controlLoop.getKey().asIdentifier());
82             return;
83         }
84
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());
89                 return;
90             }
91         }
92
93         LOGGER.debug("control loop scan: transition from state {} to {} completed", controlLoop.getState(),
94                 controlLoop.getOrderedState());
95
96         controlLoop.setState(controlLoop.getOrderedState().asState());
97         controlLoopProvider.updateControlLoop(controlLoop);
98     }
99
100     /**
101      * Makes a new timer pool.
102      *
103      * @return a new timer pool
104      */
105     protected ScheduledExecutorService makeTimerPool() {
106         return Executors.newScheduledThreadPool(1);
107     }
108 }