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