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