c4ca568f773b92c997e2657f42aa2586a4bf920b
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 Nordix Foundation.
4  *  Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.clamp.controlloop.runtime.supervision;
23
24 import java.util.List;
25 import java.util.Map;
26 import java.util.Set;
27 import java.util.UUID;
28 import javax.ws.rs.core.Response;
29 import lombok.AllArgsConstructor;
30 import org.apache.commons.collections4.CollectionUtils;
31 import org.onap.policy.clamp.controlloop.common.exception.ControlLoopException;
32 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoop;
33 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElementAck;
34 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopInfo;
35 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopState;
36 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.Participant;
37 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantHealthStatus;
38 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantState;
39 import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ControlLoopProvider;
40 import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ParticipantProvider;
41 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ControlLoopAck;
42 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantDeregister;
43 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantRegister;
44 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantStatus;
45 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantUpdate;
46 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantUpdateAck;
47 import org.onap.policy.clamp.controlloop.runtime.monitoring.MonitoringProvider;
48 import org.onap.policy.clamp.controlloop.runtime.supervision.comm.ControlLoopStateChangePublisher;
49 import org.onap.policy.clamp.controlloop.runtime.supervision.comm.ControlLoopUpdatePublisher;
50 import org.onap.policy.clamp.controlloop.runtime.supervision.comm.ParticipantDeregisterAckPublisher;
51 import org.onap.policy.clamp.controlloop.runtime.supervision.comm.ParticipantRegisterAckPublisher;
52 import org.onap.policy.clamp.controlloop.runtime.supervision.comm.ParticipantUpdatePublisher;
53 import org.onap.policy.models.base.PfModelException;
54 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
55 import org.slf4j.Logger;
56 import org.slf4j.LoggerFactory;
57 import org.springframework.stereotype.Component;
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/>
63  * It is effectively a singleton that is started at system start.
64  */
65 @Component
66 @AllArgsConstructor
67 public class SupervisionHandler {
68     private static final Logger LOGGER = LoggerFactory.getLogger(SupervisionHandler.class);
69
70     private static final String CONTROL_LOOP_CANNOT_TRANSITION_FROM_STATE = "Control loop can't transition from state ";
71     private static final String CONTROL_LOOP_IS_ALREADY_IN_STATE = "Control loop is already in state ";
72     private static final String TO_STATE = " to state ";
73     private static final String AND_TRANSITIONING_TO_STATE = " and transitioning to state ";
74
75     private final ControlLoopProvider controlLoopProvider;
76     private final ParticipantProvider participantProvider;
77     private final MonitoringProvider monitoringProvider;
78
79     // Publishers for participant communication
80     private final ControlLoopUpdatePublisher controlLoopUpdatePublisher;
81     private final ControlLoopStateChangePublisher controlLoopStateChangePublisher;
82     private final ParticipantRegisterAckPublisher participantRegisterAckPublisher;
83     private final ParticipantDeregisterAckPublisher participantDeregisterAckPublisher;
84     private final ParticipantUpdatePublisher participantUpdatePublisher;
85
86     /**
87      * Supervision trigger called when a command is issued on control loops.
88      *
89      * <p/>
90      * Causes supervision to start or continue supervision on the control loops in question.
91      *
92      * @param controlLoopIdentifierList the control loops for which the supervision command has been issued
93      * @throws ControlLoopException on supervision triggering exceptions
94      */
95     public void triggerControlLoopSupervision(List<ToscaConceptIdentifier> controlLoopIdentifierList)
96             throws ControlLoopException {
97
98         LOGGER.debug("triggering control loop supervision on control loops {}", controlLoopIdentifierList);
99
100         if (CollectionUtils.isEmpty(controlLoopIdentifierList)) {
101             // This is just to force throwing of the exception in certain circumstances.
102             exceptionOccured(Response.Status.NOT_ACCEPTABLE, "The list of control loops for supervision is empty");
103         }
104
105         for (ToscaConceptIdentifier controlLoopId : controlLoopIdentifierList) {
106             try {
107                 var controlLoop = controlLoopProvider.getControlLoop(controlLoopId);
108
109                 superviseControlLoop(controlLoop);
110
111                 controlLoopProvider.updateControlLoop(controlLoop);
112             } catch (PfModelException pfme) {
113                 throw new ControlLoopException(pfme.getErrorResponse().getResponseCode(), pfme.getMessage(), pfme);
114             }
115         }
116     }
117
118     /**
119      * Handle a ParticipantStatus message from a participant.
120      *
121      * @param participantStatusMessage the ParticipantStatus message received from a participant
122      */
123     @MessageIntercept
124     public void handleParticipantMessage(ParticipantStatus participantStatusMessage) {
125         LOGGER.debug("Participant Status received {}", participantStatusMessage);
126         try {
127             superviseParticipant(participantStatusMessage);
128         } catch (PfModelException | ControlLoopException svExc) {
129             LOGGER.warn("error supervising participant {}", participantStatusMessage.getParticipantId(), svExc);
130             return;
131         }
132
133         try {
134             superviseControlLoops(participantStatusMessage);
135         } catch (PfModelException | ControlLoopException svExc) {
136             LOGGER.warn("error supervising participant {}", participantStatusMessage.getParticipantId(), svExc);
137         }
138     }
139
140     /**
141      * Handle a ParticipantRegister message from a participant.
142      *
143      * @param participantRegisterMessage the ParticipantRegister message received from a participant
144      */
145     @MessageIntercept
146     public void handleParticipantMessage(ParticipantRegister participantRegisterMessage) {
147         LOGGER.debug("Participant Register received {}", participantRegisterMessage);
148
149         participantRegisterAckPublisher.send(participantRegisterMessage.getMessageId(),
150                 participantRegisterMessage.getParticipantId(), participantRegisterMessage.getParticipantType());
151
152         participantUpdatePublisher.send(participantRegisterMessage.getParticipantId(),
153                 participantRegisterMessage.getParticipantType(), true);
154     }
155
156     /**
157      * Handle a ParticipantDeregister message from a participant.
158      *
159      * @param participantDeregisterMessage the ParticipantDeregister message received from a participant
160      */
161     @MessageIntercept
162     public void handleParticipantMessage(ParticipantDeregister participantDeregisterMessage) {
163         LOGGER.debug("Participant Deregister received {}", participantDeregisterMessage);
164         try {
165             var participantList =
166                 participantProvider.getParticipants(participantDeregisterMessage.getParticipantId().getName(),
167                     participantDeregisterMessage.getParticipantId().getVersion());
168
169             if (participantList != null) {
170                 for (Participant participant : participantList) {
171                     participant.setParticipantState(ParticipantState.TERMINATED);
172                     participant.setHealthStatus(ParticipantHealthStatus.OFF_LINE);
173                 }
174                 participantProvider.updateParticipants(participantList);
175             }
176         } catch (PfModelException pfme) {
177             LOGGER.warn("Model exception occured {}", participantDeregisterMessage.getParticipantId());
178         }
179
180         participantDeregisterAckPublisher.send(participantDeregisterMessage.getMessageId());
181     }
182
183     /**
184      * Handle a ParticipantUpdateAck message from a participant.
185      *
186      * @param participantUpdateAckMessage the ParticipantUpdateAck message received from a participant
187      */
188     @MessageIntercept
189     public void handleParticipantMessage(ParticipantUpdateAck participantUpdateAckMessage) {
190         LOGGER.debug("Participant Update Ack received {}", participantUpdateAckMessage);
191         try {
192             var participantList =
193                 participantProvider.getParticipants(participantUpdateAckMessage.getParticipantId().getName(),
194                     participantUpdateAckMessage.getParticipantId().getVersion());
195
196             if (participantList != null) {
197                 for (Participant participant : participantList) {
198                     participant.setParticipantState(participantUpdateAckMessage.getState());
199                 }
200                 participantProvider.updateParticipants(participantList);
201             } else {
202                 LOGGER.warn("Participant not found in database {}", participantUpdateAckMessage.getParticipantId());
203             }
204         } catch (PfModelException pfme) {
205             LOGGER.warn("Model exception occured {}", participantUpdateAckMessage.getParticipantId());
206         }
207     }
208
209     /**
210      * Send commissioning update message to dmaap.
211      *
212      * @param participantUpdateMessage the ParticipantUpdate message to send
213      */
214     public void handleSendCommissionMessage(ParticipantUpdate participantUpdateMessage) {
215         LOGGER.debug("Participant update message being sent {}", participantUpdateMessage);
216
217         participantUpdatePublisher.send(participantUpdateMessage.getParticipantId(),
218             participantUpdateMessage.getParticipantType(), true);
219     }
220
221     /**
222      * Send decommissioning update message to dmaap.
223      *
224      * @param participantUpdateMessage the ParticipantUpdate message to send
225      */
226     public void handleSendDeCommissionMessage(ParticipantUpdate participantUpdateMessage) {
227         LOGGER.debug("Participant update message being sent {}", participantUpdateMessage);
228
229         participantUpdatePublisher.send(participantUpdateMessage.getParticipantId(),
230             participantUpdateMessage.getParticipantType(), false);
231     }
232
233     /**
234      * Handle a ControlLoop update acknowledge message from a participant.
235      *
236      * @param controlLoopAckMessage the ControlLoopAck message received from a participant
237      */
238     @MessageIntercept
239     public void handleControlLoopUpdateAckMessage(ControlLoopAck controlLoopAckMessage) {
240         LOGGER.debug("ControlLoop Update Ack message received {}", controlLoopAckMessage);
241         setClElementStateInDb(controlLoopAckMessage);
242     }
243
244     /**
245      * Handle a ControlLoop statechange acknowledge message from a participant.
246      *
247      * @param controlLoopAckMessage the ControlLoopAck message received from a participant
248      */
249     @MessageIntercept
250     public void handleControlLoopStateChangeAckMessage(ControlLoopAck controlLoopAckMessage) {
251         LOGGER.debug("ControlLoop StateChange Ack message received {}", controlLoopAckMessage);
252         setClElementStateInDb(controlLoopAckMessage);
253     }
254
255     private void setClElementStateInDb(ControlLoopAck controlLoopAckMessage) {
256         if (controlLoopAckMessage.getControlLoopResultMap() != null) {
257             try {
258                 var controlLoop = controlLoopProvider.getControlLoop(controlLoopAckMessage.getControlLoopId());
259                 if (controlLoop != null) {
260                     var updated = updateState(controlLoop, controlLoopAckMessage
261                                     .getControlLoopResultMap().entrySet())
262                                     || setPrimed(controlLoop);
263                     if (updated) {
264                         controlLoopProvider.updateControlLoop(controlLoop);
265                     }
266                 } else {
267                     LOGGER.warn("ControlLoop not found in database {}", controlLoopAckMessage.getControlLoopId());
268                 }
269             } catch (PfModelException pfme) {
270                 LOGGER.warn("Model exception occured {}", controlLoopAckMessage.getControlLoopId());
271             }
272         }
273     }
274
275     private boolean updateState(ControlLoop controlLoop, Set<Map.Entry<UUID, ControlLoopElementAck>>
276             controlLoopResultSet) {
277         var updated = false;
278         for (var clElementAck : controlLoopResultSet) {
279             var element = controlLoop.getElements().get(clElementAck.getKey());
280             if (element != null) {
281                 element.setState(clElementAck.getValue().getState());
282                 updated = true;
283             }
284         }
285         return updated;
286     }
287
288     private boolean setPrimed(ControlLoop controlLoop) {
289         var clElements = controlLoop.getElements().values();
290         if (clElements != null) {
291             Boolean primedFlag = true;
292             var checkOpt = controlLoop.getElements().values().stream()
293                             .filter(clElement -> (!clElement.getState().equals(ControlLoopState.PASSIVE)
294                                     || !clElement.getState().equals(ControlLoopState.RUNNING))).findAny();
295             if (checkOpt.isEmpty()) {
296                 primedFlag = false;
297             }
298             controlLoop.setPrimed(primedFlag);
299             return true;
300         }
301
302         return false;
303     }
304
305     /**
306      * Supervise a control loop, performing whatever actions need to be performed on the control loop.
307      *
308      * @param controlLoop the control loop to supervises
309      * @throws ControlLoopException on supervision errors
310      */
311     private void superviseControlLoop(ControlLoop controlLoop) throws ControlLoopException {
312         switch (controlLoop.getOrderedState()) {
313             case UNINITIALISED:
314                 superviseControlLoopUninitialization(controlLoop);
315                 break;
316
317             case PASSIVE:
318                 superviseControlLoopPassivation(controlLoop);
319                 break;
320
321             case RUNNING:
322                 superviseControlLoopActivation(controlLoop);
323                 break;
324
325             default:
326                 exceptionOccured(Response.Status.NOT_ACCEPTABLE,
327                         "A control loop cannot be commanded to go into state " + controlLoop.getOrderedState().name());
328         }
329     }
330
331     /**
332      * Supervise a control loop uninitialisation, performing whatever actions need to be performed on the control loop,
333      * control loop ordered state is UNINITIALIZED.
334      *
335      * @param controlLoop the control loop to supervises
336      * @throws ControlLoopException on supervision errors
337      */
338     private void superviseControlLoopUninitialization(ControlLoop controlLoop) throws ControlLoopException {
339         switch (controlLoop.getState()) {
340             case UNINITIALISED:
341                 exceptionOccured(Response.Status.NOT_ACCEPTABLE,
342                         CONTROL_LOOP_IS_ALREADY_IN_STATE + controlLoop.getState().name());
343                 break;
344
345             case UNINITIALISED2PASSIVE:
346             case PASSIVE:
347                 controlLoop.setState(ControlLoopState.PASSIVE2UNINITIALISED);
348                 controlLoopStateChangePublisher.send(controlLoop);
349                 break;
350
351             case PASSIVE2UNINITIALISED:
352                 exceptionOccured(Response.Status.NOT_ACCEPTABLE, CONTROL_LOOP_IS_ALREADY_IN_STATE
353                         + controlLoop.getState().name() + AND_TRANSITIONING_TO_STATE + controlLoop.getOrderedState());
354                 break;
355
356             default:
357                 exceptionOccured(Response.Status.NOT_ACCEPTABLE, CONTROL_LOOP_CANNOT_TRANSITION_FROM_STATE
358                         + controlLoop.getState().name() + TO_STATE + controlLoop.getOrderedState());
359                 break;
360         }
361     }
362
363     private void superviseControlLoopPassivation(ControlLoop controlLoop)
364             throws ControlLoopException {
365         switch (controlLoop.getState()) {
366             case PASSIVE:
367                 exceptionOccured(Response.Status.NOT_ACCEPTABLE,
368                         CONTROL_LOOP_IS_ALREADY_IN_STATE + controlLoop.getState().name());
369                 break;
370             case UNINITIALISED:
371                 controlLoop.setState(ControlLoopState.UNINITIALISED2PASSIVE);
372                 controlLoopUpdatePublisher.send(controlLoop);
373                 break;
374
375             case UNINITIALISED2PASSIVE:
376             case RUNNING2PASSIVE:
377                 exceptionOccured(Response.Status.NOT_ACCEPTABLE, CONTROL_LOOP_IS_ALREADY_IN_STATE
378                         + controlLoop.getState().name() + AND_TRANSITIONING_TO_STATE + controlLoop.getOrderedState());
379                 break;
380
381             case RUNNING:
382                 controlLoop.setState(ControlLoopState.RUNNING2PASSIVE);
383                 controlLoopStateChangePublisher.send(controlLoop);
384                 break;
385
386             default:
387                 exceptionOccured(Response.Status.NOT_ACCEPTABLE, CONTROL_LOOP_CANNOT_TRANSITION_FROM_STATE
388                         + controlLoop.getState().name() + TO_STATE + controlLoop.getOrderedState());
389                 break;
390         }
391     }
392
393     private void superviseControlLoopActivation(ControlLoop controlLoop) throws ControlLoopException {
394         switch (controlLoop.getState()) {
395             case RUNNING:
396                 exceptionOccured(Response.Status.NOT_ACCEPTABLE,
397                         CONTROL_LOOP_IS_ALREADY_IN_STATE + controlLoop.getState().name());
398                 break;
399
400             case PASSIVE2RUNNING:
401                 exceptionOccured(Response.Status.NOT_ACCEPTABLE, CONTROL_LOOP_IS_ALREADY_IN_STATE
402                         + controlLoop.getState().name() + AND_TRANSITIONING_TO_STATE + controlLoop.getOrderedState());
403                 break;
404
405             case PASSIVE:
406                 controlLoop.setState(ControlLoopState.PASSIVE2RUNNING);
407                 controlLoopStateChangePublisher.send(controlLoop);
408                 break;
409
410             default:
411                 exceptionOccured(Response.Status.NOT_ACCEPTABLE, CONTROL_LOOP_CANNOT_TRANSITION_FROM_STATE
412                         + controlLoop.getState().name() + TO_STATE + controlLoop.getOrderedState());
413                 break;
414         }
415     }
416
417     private void superviseParticipant(ParticipantStatus participantStatusMessage)
418             throws PfModelException, ControlLoopException {
419         if (participantStatusMessage.getParticipantId() == null) {
420             exceptionOccured(Response.Status.NOT_FOUND, "Participant ID on PARTICIPANT_STATUS message is null");
421         }
422
423         List<Participant> participantList =
424                 participantProvider.getParticipants(participantStatusMessage.getParticipantId().getName(),
425                         participantStatusMessage.getParticipantId().getVersion());
426
427         if (CollectionUtils.isEmpty(participantList)) {
428             var participant = new Participant();
429             participant.setName(participantStatusMessage.getParticipantId().getName());
430             participant.setVersion(participantStatusMessage.getParticipantId().getVersion());
431             participant.setDefinition(participantStatusMessage.getParticipantId());
432             participant.setParticipantState(participantStatusMessage.getState());
433             participant.setHealthStatus(participantStatusMessage.getHealthStatus());
434
435             participantList.add(participant);
436             participantProvider.createParticipants(participantList);
437         } else {
438             for (Participant participant : participantList) {
439                 participant.setParticipantState(participantStatusMessage.getState());
440                 participant.setHealthStatus(participantStatusMessage.getHealthStatus());
441             }
442             participantProvider.updateParticipants(participantList);
443         }
444
445         monitoringProvider.createParticipantStatistics(List.of(participantStatusMessage.getParticipantStatistics()));
446     }
447
448     private void superviseControlLoops(ParticipantStatus participantStatusMessage)
449             throws PfModelException, ControlLoopException {
450         if (participantStatusMessage.getControlLoopInfoList() != null) {
451             for (ControlLoopInfo clEntry : participantStatusMessage.getControlLoopInfoList()) {
452                 var dbControlLoop =
453                         controlLoopProvider.getControlLoop(new ToscaConceptIdentifier(clEntry.getControlLoopId()));
454                 if (dbControlLoop == null) {
455                     exceptionOccured(Response.Status.NOT_FOUND,
456                             "PARTICIPANT_STATUS control loop not found in database: " + clEntry.getControlLoopId());
457                 }
458                 dbControlLoop.setState(clEntry.getState());
459                 monitoringProvider.createClElementStatistics(
460                         clEntry.getControlLoopStatistics().getClElementStatisticsList().getClElementStatistics());
461             }
462         }
463     }
464
465     private void exceptionOccured(Response.Status status, String reason) throws ControlLoopException {
466         throw new ControlLoopException(status, reason);
467     }
468 }