511185da10cf7257a9ac127a7de801a0d370ff4a
[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.util.List;
24 import java.util.UUID;
25 import javax.ws.rs.core.Response;
26 import javax.ws.rs.core.Response.Status;
27 import org.apache.commons.collections4.CollectionUtils;
28 import org.onap.policy.clamp.controlloop.common.exception.ControlLoopException;
29 import org.onap.policy.clamp.controlloop.common.exception.ControlLoopRuntimeException;
30 import org.onap.policy.clamp.controlloop.common.handler.ControlLoopHandler;
31 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoop;
32 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElement;
33 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopState;
34 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.Participant;
35 import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ControlLoopProvider;
36 import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ParticipantProvider;
37 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantControlLoopStateChange;
38 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantControlLoopUpdate;
39 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantMessageType;
40 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantStatus;
41 import org.onap.policy.clamp.controlloop.runtime.commissioning.CommissioningHandler;
42 import org.onap.policy.clamp.controlloop.runtime.main.parameters.ClRuntimeParameterGroup;
43 import org.onap.policy.clamp.controlloop.runtime.monitoring.MonitoringHandler;
44 import org.onap.policy.clamp.controlloop.runtime.monitoring.MonitoringProvider;
45 import org.onap.policy.clamp.controlloop.runtime.supervision.comm.ParticipantControlLoopStateChangePublisher;
46 import org.onap.policy.clamp.controlloop.runtime.supervision.comm.ParticipantControlLoopUpdatePublisher;
47 import org.onap.policy.clamp.controlloop.runtime.supervision.comm.ParticipantStateChangePublisher;
48 import org.onap.policy.clamp.controlloop.runtime.supervision.comm.ParticipantStatusListener;
49 import org.onap.policy.common.endpoints.event.comm.TopicSink;
50 import org.onap.policy.common.endpoints.listeners.MessageTypeDispatcher;
51 import org.onap.policy.common.utils.services.Registry;
52 import org.onap.policy.common.utils.services.ServiceManager;
53 import org.onap.policy.common.utils.services.ServiceManagerException;
54 import org.onap.policy.models.base.PfModelException;
55 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
56 import org.slf4j.Logger;
57 import org.slf4j.LoggerFactory;
58
59 /**
60  * This class handles supervision of control loop instances, so only one object of this type should be built at a time.
61  *
62  * <p/> It is effectively a singleton that is started at system start.
63  */
64 public class SupervisionHandler extends ControlLoopHandler {
65     private static final Logger LOGGER = LoggerFactory.getLogger(SupervisionHandler.class);
66
67     private static final String CONTROL_LOOP_CANNOT_TRANSITION_FROM_STATE = "Control loop can't transition from state ";
68     private static final String CONTROL_LOOP_IS_ALREADY_IN_STATE = "Control loop is already in state ";
69     private static final String TO_STATE = " to state ";
70     private static final String AND_TRANSITIONING_TO_STATE = " and transitioning to state ";
71
72     private ControlLoopProvider controlLoopProvider;
73     private ParticipantProvider participantProvider;
74     private MonitoringProvider monitoringProvider;
75
76     // Publishers for participant communication
77     private ParticipantStateChangePublisher stateChangePublisher;
78     private ParticipantControlLoopUpdatePublisher controlLoopUpdatePublisher;
79     private ParticipantControlLoopStateChangePublisher controlLoopStateChangePublisher;
80
81     private long supervisionScannerIntervalSec;
82     private long participantStateChangeIntervalSec;
83     private long participantClUpdateIntervalSec;
84     private long participantClStateChangeIntervalSec;
85
86     // Database scanner
87     private SupervisionScanner scanner;
88
89     /**
90      * Used to manage the services.
91      */
92     private ServiceManager manager;
93     private ServiceManager publisherManager;
94
95     /**
96      * Gets the SupervisionHandler.
97      *
98      * @return SupervisionHandler
99      */
100     public static SupervisionHandler getInstance() {
101         return Registry.get(SupervisionHandler.class.getName());
102     }
103
104     /**
105      * Create a handler.
106      *
107      * @param clRuntimeParameterGroup the parameters for the control loop runtime
108      */
109     public SupervisionHandler(ClRuntimeParameterGroup clRuntimeParameterGroup) {
110         super(clRuntimeParameterGroup.getDatabaseProviderParameters());
111         // @formatter:off
112         this.manager = new ServiceManager()
113                         .addAction("ControlLoop Provider",
114                             () -> controlLoopProvider = new ControlLoopProvider(getDatabaseProviderParameters()),
115                             () -> controlLoopProvider = null)
116                         .addAction("Participant Provider",
117                             () -> participantProvider = new ParticipantProvider(getDatabaseProviderParameters()),
118                             () -> participantProvider = null);
119         // @formatter:on
120
121         supervisionScannerIntervalSec = clRuntimeParameterGroup.getSupervisionScannerIntervalSec();
122         participantStateChangeIntervalSec = clRuntimeParameterGroup.getParticipantClStateChangeIntervalSec();
123         participantClUpdateIntervalSec = clRuntimeParameterGroup.getParticipantClUpdateIntervalSec();
124         participantClStateChangeIntervalSec = clRuntimeParameterGroup.getParticipantClStateChangeIntervalSec();
125
126     }
127
128     /**
129      * Supervision trigger called when a command is issued on control loops.
130      *
131      * </p> Causes supervision to start or continue supervision on the control loops in question.
132      *
133      * @param controlLoopIdentifierList the control loops for which the supervision command has been issued
134      * @throws ControlLoopException on supervision triggering exceptions
135      */
136     public void triggerControlLoopSupervision(List<ToscaConceptIdentifier> controlLoopIdentifierList)
137             throws ControlLoopException {
138
139         LOGGER.debug("triggering control loop supervision on control loops {}", controlLoopIdentifierList);
140
141         if (CollectionUtils.isEmpty(controlLoopIdentifierList)) {
142             // This is just to force throwing of the exception in certain circumstances.
143             exceptionOccured(Response.Status.NOT_ACCEPTABLE, "The list of control loops for supervision is empty");
144         }
145
146         for (ToscaConceptIdentifier controlLoopId : controlLoopIdentifierList) {
147             try {
148                 var controlLoop = controlLoopProvider.getControlLoop(controlLoopId);
149
150                 superviseControlLoop(controlLoop);
151
152                 controlLoopProvider.updateControlLoop(controlLoop);
153             } catch (PfModelException pfme) {
154                 throw new ControlLoopException(pfme.getErrorResponse().getResponseCode(), pfme.getMessage(), pfme);
155             }
156         }
157     }
158
159     @Override
160     public void startAndRegisterListeners(MessageTypeDispatcher msgDispatcher) {
161         msgDispatcher.register(ParticipantMessageType.PARTICIPANT_STATUS.name(), new ParticipantStatusListener());
162     }
163
164     @Override
165     public void startAndRegisterPublishers(List<TopicSink> topicSinks) {
166         // @formatter:off
167         this.publisherManager = new ServiceManager()
168                 .addAction("Supervision scanner",
169                         () -> scanner =
170                         new SupervisionScanner(controlLoopProvider, supervisionScannerIntervalSec),
171                         () -> scanner.close())
172                 .addAction("ControlLoopUpdate publisher",
173                         () -> controlLoopUpdatePublisher =
174                         new ParticipantControlLoopUpdatePublisher(topicSinks, participantClUpdateIntervalSec),
175                         () -> controlLoopUpdatePublisher.terminate())
176                 .addAction("StateChange Publisher",
177                         () -> stateChangePublisher =
178                         new ParticipantStateChangePublisher(topicSinks, participantStateChangeIntervalSec),
179                         () -> stateChangePublisher.terminate())
180                 .addAction("ControlLoopStateChange Publisher",
181                         () -> controlLoopStateChangePublisher =
182                         new ParticipantControlLoopStateChangePublisher(topicSinks, participantClStateChangeIntervalSec),
183                         () -> controlLoopStateChangePublisher.terminate());
184         // @formatter:on
185         try {
186             publisherManager.start();
187         } catch (final ServiceManagerException exp) {
188             throw new ControlLoopRuntimeException(Status.INTERNAL_SERVER_ERROR,
189                     "Supervision handler start of publishers or scanner failed", exp);
190         }
191     }
192
193     @Override
194     public void stopAndUnregisterPublishers() {
195         try {
196             publisherManager.stop();
197         } catch (final ServiceManagerException exp) {
198             throw new ControlLoopRuntimeException(Status.INTERNAL_SERVER_ERROR,
199                     "Supervision handler stop of publishers or scanner failed", exp);
200         }
201     }
202
203     @Override
204     public void stopAndUnregisterListeners(MessageTypeDispatcher msgDispatcher) {
205         msgDispatcher.unregister(ParticipantMessageType.PARTICIPANT_STATUS.name());
206     }
207
208     /**
209      * Handle a ParticipantStatus message from a participant.
210      *
211      * @param participantStatusMessage the ParticipantStatus message received from a participant
212      */
213     public void handleParticipantStatusMessage(ParticipantStatus participantStatusMessage) {
214         LOGGER.debug("Participant Status received {}", participantStatusMessage);
215
216         try {
217             superviseParticipant(participantStatusMessage);
218         } catch (PfModelException | ControlLoopException svExc) {
219             LOGGER.warn("error supervising participant {}", participantStatusMessage.getParticipantId(), svExc);
220             return;
221         }
222
223         try {
224             superviseControlLoops(participantStatusMessage);
225         } catch (PfModelException | ControlLoopException svExc) {
226             LOGGER.warn("error supervising participant {}", participantStatusMessage.getParticipantId(), svExc);
227         }
228     }
229
230     /**
231      * Supervise a control loop, performing whatever actions need to be performed on the control loop.
232      *
233      * @param controlLoop the control loop to supervises
234      * @throws PfModelException on accessing models in the database
235      * @throws ControlLoopException on supervision errors
236      */
237     private void superviseControlLoop(ControlLoop controlLoop) throws ControlLoopException, PfModelException {
238         switch (controlLoop.getOrderedState()) {
239             case UNINITIALISED:
240                 superviseControlLoopUninitialization(controlLoop);
241                 break;
242
243             case PASSIVE:
244                 superviseControlLoopPassivation(controlLoop);
245                 break;
246
247             case RUNNING:
248                 superviseControlLoopActivation(controlLoop);
249                 break;
250
251             default:
252                 exceptionOccured(Response.Status.NOT_ACCEPTABLE,
253                     "A control loop cannot be commanded to go into state " + controlLoop.getOrderedState().name());
254         }
255     }
256
257     /**
258      * Supervise a control loop uninitialisation, performing whatever actions need to be performed on the control loop,
259      * control loop ordered state is UNINITIALIZED.
260      *
261      * @param controlLoop the control loop to supervises
262      * @throws ControlLoopException on supervision errors
263      */
264     private void superviseControlLoopUninitialization(ControlLoop controlLoop) throws ControlLoopException {
265         switch (controlLoop.getState()) {
266             case UNINITIALISED:
267                 exceptionOccured(Response.Status.NOT_ACCEPTABLE,
268                         CONTROL_LOOP_IS_ALREADY_IN_STATE + controlLoop.getState().name());
269                 break;
270
271             case UNINITIALISED2PASSIVE:
272             case PASSIVE:
273                 controlLoop.setState(ControlLoopState.PASSIVE2UNINITIALISED);
274                 sendControlLoopStateChange(controlLoop);
275                 break;
276
277             case PASSIVE2UNINITIALISED:
278                 exceptionOccured(Response.Status.NOT_ACCEPTABLE, CONTROL_LOOP_IS_ALREADY_IN_STATE
279                         + controlLoop.getState().name() + AND_TRANSITIONING_TO_STATE + controlLoop.getOrderedState());
280                 break;
281
282             default:
283                 exceptionOccured(Response.Status.NOT_ACCEPTABLE, CONTROL_LOOP_CANNOT_TRANSITION_FROM_STATE
284                         + controlLoop.getState().name() + TO_STATE + controlLoop.getOrderedState());
285                 break;
286         }
287     }
288
289     private void superviseControlLoopPassivation(ControlLoop controlLoop)
290             throws ControlLoopException, PfModelException {
291         switch (controlLoop.getState()) {
292             case PASSIVE:
293                 exceptionOccured(Response.Status.NOT_ACCEPTABLE,
294                         CONTROL_LOOP_IS_ALREADY_IN_STATE + controlLoop.getState().name());
295                 break;
296             case UNINITIALISED:
297                 controlLoop.setState(ControlLoopState.UNINITIALISED2PASSIVE);
298                 sendControlLoopUpdate(controlLoop);
299                 break;
300
301             case UNINITIALISED2PASSIVE:
302             case RUNNING2PASSIVE:
303                 exceptionOccured(Response.Status.NOT_ACCEPTABLE, CONTROL_LOOP_IS_ALREADY_IN_STATE
304                         + controlLoop.getState().name() + AND_TRANSITIONING_TO_STATE + controlLoop.getOrderedState());
305                 break;
306
307             case RUNNING:
308                 controlLoop.setState(ControlLoopState.RUNNING2PASSIVE);
309                 sendControlLoopStateChange(controlLoop);
310                 break;
311
312             default:
313                 exceptionOccured(Response.Status.NOT_ACCEPTABLE, CONTROL_LOOP_CANNOT_TRANSITION_FROM_STATE
314                         + controlLoop.getState().name() + TO_STATE + controlLoop.getOrderedState());
315                 break;
316         }
317     }
318
319     private void superviseControlLoopActivation(ControlLoop controlLoop) throws ControlLoopException {
320         switch (controlLoop.getState()) {
321             case RUNNING:
322                 exceptionOccured(Response.Status.NOT_ACCEPTABLE,
323                         CONTROL_LOOP_IS_ALREADY_IN_STATE + controlLoop.getState().name());
324                 break;
325
326             case PASSIVE2RUNNING:
327                 exceptionOccured(Response.Status.NOT_ACCEPTABLE, CONTROL_LOOP_IS_ALREADY_IN_STATE
328                         + controlLoop.getState().name() + AND_TRANSITIONING_TO_STATE + controlLoop.getOrderedState());
329                 break;
330
331             case PASSIVE:
332                 controlLoop.setState(ControlLoopState.PASSIVE2RUNNING);
333                 sendControlLoopStateChange(controlLoop);
334                 break;
335
336             default:
337                 exceptionOccured(Response.Status.NOT_ACCEPTABLE, CONTROL_LOOP_CANNOT_TRANSITION_FROM_STATE
338                         + controlLoop.getState().name() + TO_STATE + controlLoop.getOrderedState());
339                 break;
340         }
341     }
342
343     private void sendControlLoopUpdate(ControlLoop controlLoop) throws PfModelException {
344         var pclu = new ParticipantControlLoopUpdate();
345         pclu.setControlLoopId(controlLoop.getKey().asIdentifier());
346         pclu.setControlLoop(controlLoop);
347         // TODO: We should look up the correct TOSCA node template here for the control loop
348         // Tiny hack implemented to return the tosca service template entry from the database and be passed onto dmaap
349         var commissioningProvider = CommissioningHandler.getInstance().getProvider();
350         pclu.setControlLoopDefinition(commissioningProvider.getToscaServiceTemplate(null, null));
351         controlLoopUpdatePublisher.send(pclu);
352     }
353
354     private void sendControlLoopStateChange(ControlLoop controlLoop) {
355         var clsc = new ParticipantControlLoopStateChange();
356         clsc.setControlLoopId(controlLoop.getKey().asIdentifier());
357         clsc.setMessageId(UUID.randomUUID());
358         clsc.setOrderedState(controlLoop.getOrderedState());
359
360         controlLoopStateChangePublisher.send(clsc);
361     }
362
363     private void superviseParticipant(ParticipantStatus participantStatusMessage)
364             throws PfModelException, ControlLoopException {
365         if (participantStatusMessage.getParticipantId() == null) {
366             exceptionOccured(Response.Status.NOT_FOUND, "Participant ID on PARTICIPANT_STATUS message is null");
367         }
368
369         List<Participant> participantList =
370                 participantProvider.getParticipants(participantStatusMessage.getParticipantId().getName(),
371                         participantStatusMessage.getParticipantId().getVersion());
372
373         if (CollectionUtils.isEmpty(participantList)) {
374             var participant = new Participant();
375             participant.setName(participantStatusMessage.getParticipantId().getName());
376             participant.setVersion(participantStatusMessage.getParticipantId().getVersion());
377             participant.setDefinition(new ToscaConceptIdentifier("unknown", "0.0.0"));
378             participant.setParticipantState(participantStatusMessage.getState());
379             participant.setHealthStatus(participantStatusMessage.getHealthStatus());
380
381             participantList.add(participant);
382             participantProvider.createParticipants(participantList);
383         } else {
384             for (Participant participant : participantList) {
385                 participant.setParticipantState(participantStatusMessage.getState());
386                 participant.setHealthStatus(participantStatusMessage.getHealthStatus());
387             }
388             participantProvider.updateParticipants(participantList);
389         }
390
391         monitoringProvider = MonitoringHandler.getInstance().getMonitoringProvider();
392         monitoringProvider.createParticipantStatistics(List.of(participantStatusMessage.getParticipantStatistics()));
393     }
394
395     private void superviseControlLoops(ParticipantStatus participantStatusMessage)
396             throws PfModelException, ControlLoopException {
397         if (CollectionUtils.isEmpty(participantStatusMessage.getControlLoops().getControlLoopList())) {
398             return;
399         }
400
401         for (ControlLoop controlLoop : participantStatusMessage.getControlLoops().getControlLoopList()) {
402             if (controlLoop == null) {
403                 exceptionOccured(Response.Status.NOT_FOUND,
404                     "PARTICIPANT_STATUS message references unknown control loop: " + controlLoop);
405             }
406
407             var dbControlLoop = controlLoopProvider
408                     .getControlLoop(new ToscaConceptIdentifier(controlLoop.getName(), controlLoop.getVersion()));
409             if (dbControlLoop == null) {
410                 exceptionOccured(Response.Status.NOT_FOUND,
411                     "PARTICIPANT_STATUS control loop not found in database: " + controlLoop);
412             }
413
414             for (ControlLoopElement element : controlLoop.getElements().values()) {
415                 ControlLoopElement dbElement = dbControlLoop.getElements().get(element.getId());
416
417                 if (dbElement == null) {
418                     exceptionOccured(Response.Status.NOT_FOUND,
419                             "PARTICIPANT_STATUS message references unknown control loop element: " + element);
420                 }
421
422                 // Replace element entry in the database
423                 dbControlLoop.getElements().put(element.getId(), element);
424             }
425             controlLoopProvider.updateControlLoop(dbControlLoop);
426         }
427
428         monitoringProvider = MonitoringHandler.getInstance().getMonitoringProvider();
429         for (ControlLoop controlLoop : participantStatusMessage.getControlLoops().getControlLoopList()) {
430             monitoringProvider.createClElementStatistics(controlLoop.getControlLoopElementStatisticsList(controlLoop));
431         }
432     }
433
434     @Override
435     public void startProviders() {
436         try {
437             manager.start();
438         } catch (final ServiceManagerException exp) {
439             throw new ControlLoopRuntimeException(Status.INTERNAL_SERVER_ERROR,
440                     "Supervision handler start of providers failed", exp);
441         }
442     }
443
444     @Override
445     public void stopProviders() {
446         try {
447             manager.stop();
448         } catch (final ServiceManagerException exp) {
449             throw new ControlLoopRuntimeException(Status.INTERNAL_SERVER_ERROR,
450                     "Supervision handler stop of providers failed", exp);
451         }
452     }
453
454     private void exceptionOccured(Response.Status status, String reason) throws ControlLoopException {
455         throw new ControlLoopException(status, reason);
456     }
457 }