960cb43045d0054b969c0124a2551bf8ad18d7d4
[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.ParticipantMessage;
44 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantRegister;
45 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantStatus;
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 boolean handleParticipantMessage(ParticipantRegister participantRegisterMessage) {
147         LOGGER.debug("Participant Register received {}", participantRegisterMessage);
148         try {
149             checkParticipant(participantRegisterMessage, ParticipantState.UNKNOWN, ParticipantHealthStatus.UNKNOWN);
150         } catch (PfModelException | ControlLoopException svExc) {
151             LOGGER.warn("error saving participant {}", participantRegisterMessage.getParticipantId(), svExc);
152         }
153
154         var isCommissioning = participantUpdatePublisher.sendCommissioning(null, null,
155                 participantRegisterMessage.getParticipantId(), participantRegisterMessage.getParticipantType());
156
157         participantRegisterAckPublisher.send(participantRegisterMessage.getMessageId(),
158                 participantRegisterMessage.getParticipantId(), participantRegisterMessage.getParticipantType());
159         return isCommissioning;
160     }
161
162     /**
163      * Handle a ParticipantDeregister message from a participant.
164      *
165      * @param participantDeregisterMessage the ParticipantDeregister message received from a participant
166      */
167     @MessageIntercept
168     public void handleParticipantMessage(ParticipantDeregister participantDeregisterMessage) {
169         LOGGER.debug("Participant Deregister received {}", participantDeregisterMessage);
170         try {
171             var participantList =
172                     participantProvider.getParticipants(participantDeregisterMessage.getParticipantId().getName(),
173                             participantDeregisterMessage.getParticipantId().getVersion());
174
175             if (participantList != null) {
176                 for (Participant participant : participantList) {
177                     participant.setParticipantState(ParticipantState.TERMINATED);
178                     participant.setHealthStatus(ParticipantHealthStatus.OFF_LINE);
179                 }
180                 participantProvider.updateParticipants(participantList);
181             }
182         } catch (PfModelException pfme) {
183             LOGGER.warn("Model exception occured with participant id {}",
184                     participantDeregisterMessage.getParticipantId());
185         }
186
187         participantDeregisterAckPublisher.send(participantDeregisterMessage.getMessageId());
188     }
189
190     /**
191      * Handle a ParticipantUpdateAck message from a participant.
192      *
193      * @param participantUpdateAckMessage the ParticipantUpdateAck message received from a participant
194      */
195     @MessageIntercept
196     public void handleParticipantMessage(ParticipantUpdateAck participantUpdateAckMessage) {
197         LOGGER.debug("Participant Update Ack received {}", participantUpdateAckMessage);
198         try {
199             var participantList =
200                     participantProvider.getParticipants(participantUpdateAckMessage.getParticipantId().getName(),
201                             participantUpdateAckMessage.getParticipantId().getVersion());
202
203             if (participantList != null) {
204                 for (Participant participant : participantList) {
205                     participant.setParticipantState(participantUpdateAckMessage.getState());
206                 }
207                 participantProvider.updateParticipants(participantList);
208             } else {
209                 LOGGER.warn("Participant not found in database {}", participantUpdateAckMessage.getParticipantId());
210             }
211         } catch (PfModelException pfme) {
212             LOGGER.warn("Model exception occured with participant id {}",
213                     participantUpdateAckMessage.getParticipantId());
214         }
215     }
216
217     /**
218      * Send commissioning update message to dmaap.
219      *
220      * @param name the ToscaServiceTemplate name
221      * @param version the ToscaServiceTemplate version
222      */
223     public void handleSendCommissionMessage(String name, String version) {
224         LOGGER.debug("Participant update message with serviveTemplate {} {} being sent to all participants", name,
225                 version);
226         participantUpdatePublisher.sendComissioningBroadcast(name, version);
227     }
228
229     /**
230      * Send decommissioning update message to dmaap.
231      *
232      */
233     public void handleSendDeCommissionMessage() {
234         LOGGER.debug("Participant update message being sent");
235         participantUpdatePublisher.sendDecomisioning();
236     }
237
238     /**
239      * Handle a ControlLoop update acknowledge message from a participant.
240      *
241      * @param controlLoopAckMessage the ControlLoopAck message received from a participant
242      */
243     @MessageIntercept
244     public void handleControlLoopUpdateAckMessage(ControlLoopAck controlLoopAckMessage) {
245         LOGGER.debug("ControlLoop Update Ack message received {}", controlLoopAckMessage);
246         setClElementStateInDb(controlLoopAckMessage);
247     }
248
249     /**
250      * Handle a ControlLoop statechange acknowledge message from a participant.
251      *
252      * @param controlLoopAckMessage the ControlLoopAck message received from a participant
253      */
254     @MessageIntercept
255     public void handleControlLoopStateChangeAckMessage(ControlLoopAck controlLoopAckMessage) {
256         LOGGER.debug("ControlLoop StateChange Ack message received {}", controlLoopAckMessage);
257         setClElementStateInDb(controlLoopAckMessage);
258     }
259
260     private void setClElementStateInDb(ControlLoopAck controlLoopAckMessage) {
261         if (controlLoopAckMessage.getControlLoopResultMap() != null) {
262             try {
263                 var controlLoop = controlLoopProvider.getControlLoop(controlLoopAckMessage.getControlLoopId());
264                 if (controlLoop != null) {
265                     var updated = updateState(controlLoop, controlLoopAckMessage.getControlLoopResultMap().entrySet());
266                     updated |= setPrimed(controlLoop);
267                     if (updated) {
268                         controlLoopProvider.updateControlLoop(controlLoop);
269                     }
270                 } else {
271                     LOGGER.warn("ControlLoop not found in database {}", controlLoopAckMessage.getControlLoopId());
272                 }
273             } catch (PfModelException pfme) {
274                 LOGGER.warn("Model exception occured with ControlLoop Id {}", controlLoopAckMessage.getControlLoopId());
275             }
276         }
277     }
278
279     private boolean updateState(ControlLoop controlLoop,
280             Set<Map.Entry<UUID, ControlLoopElementAck>> controlLoopResultSet) {
281         var updated = false;
282         for (var clElementAck : controlLoopResultSet) {
283             var element = controlLoop.getElements().get(clElementAck.getKey());
284             if (element != null) {
285                 element.setState(clElementAck.getValue().getState());
286                 updated = true;
287             }
288         }
289         return updated;
290     }
291
292     private boolean setPrimed(ControlLoop controlLoop) {
293         var clElements = controlLoop.getElements().values();
294         if (clElements != null) {
295             Boolean primedFlag = true;
296             var checkOpt = controlLoop.getElements().values().stream()
297                     .filter(clElement -> (!clElement.getState().equals(ControlLoopState.PASSIVE)
298                             || !clElement.getState().equals(ControlLoopState.RUNNING)))
299                     .findAny();
300             if (checkOpt.isEmpty()) {
301                 primedFlag = false;
302             }
303             controlLoop.setPrimed(primedFlag);
304             return true;
305         }
306
307         return false;
308     }
309
310     /**
311      * Supervise a control loop, performing whatever actions need to be performed on the control loop.
312      *
313      * @param controlLoop the control loop to supervises
314      * @throws ControlLoopException on supervision errors
315      */
316     private void superviseControlLoop(ControlLoop controlLoop) throws ControlLoopException {
317         switch (controlLoop.getOrderedState()) {
318             case UNINITIALISED:
319                 superviseControlLoopUninitialization(controlLoop);
320                 break;
321
322             case PASSIVE:
323                 superviseControlLoopPassivation(controlLoop);
324                 break;
325
326             case RUNNING:
327                 superviseControlLoopActivation(controlLoop);
328                 break;
329
330             default:
331                 exceptionOccured(Response.Status.NOT_ACCEPTABLE,
332                         "A control loop cannot be commanded to go into state " + controlLoop.getOrderedState().name());
333         }
334     }
335
336     /**
337      * Supervise a control loop uninitialisation, performing whatever actions need to be performed on the control loop,
338      * control loop ordered state is UNINITIALIZED.
339      *
340      * @param controlLoop the control loop to supervises
341      * @throws ControlLoopException on supervision errors
342      */
343     private void superviseControlLoopUninitialization(ControlLoop controlLoop) throws ControlLoopException {
344         switch (controlLoop.getState()) {
345             case UNINITIALISED:
346                 exceptionOccured(Response.Status.NOT_ACCEPTABLE,
347                         CONTROL_LOOP_IS_ALREADY_IN_STATE + controlLoop.getState().name());
348                 break;
349
350             case UNINITIALISED2PASSIVE:
351             case PASSIVE:
352                 controlLoop.setState(ControlLoopState.PASSIVE2UNINITIALISED);
353                 controlLoopStateChangePublisher.send(controlLoop);
354                 break;
355
356             case PASSIVE2UNINITIALISED:
357                 exceptionOccured(Response.Status.NOT_ACCEPTABLE, CONTROL_LOOP_IS_ALREADY_IN_STATE
358                         + controlLoop.getState().name() + AND_TRANSITIONING_TO_STATE + controlLoop.getOrderedState());
359                 break;
360
361             default:
362                 exceptionOccured(Response.Status.NOT_ACCEPTABLE, CONTROL_LOOP_CANNOT_TRANSITION_FROM_STATE
363                         + controlLoop.getState().name() + TO_STATE + controlLoop.getOrderedState());
364                 break;
365         }
366     }
367
368     private void superviseControlLoopPassivation(ControlLoop controlLoop) throws ControlLoopException {
369         switch (controlLoop.getState()) {
370             case PASSIVE:
371                 exceptionOccured(Response.Status.NOT_ACCEPTABLE,
372                         CONTROL_LOOP_IS_ALREADY_IN_STATE + controlLoop.getState().name());
373                 break;
374             case UNINITIALISED:
375                 controlLoop.setState(ControlLoopState.UNINITIALISED2PASSIVE);
376                 controlLoopUpdatePublisher.send(controlLoop);
377                 break;
378
379             case UNINITIALISED2PASSIVE:
380             case RUNNING2PASSIVE:
381                 exceptionOccured(Response.Status.NOT_ACCEPTABLE, CONTROL_LOOP_IS_ALREADY_IN_STATE
382                         + controlLoop.getState().name() + AND_TRANSITIONING_TO_STATE + controlLoop.getOrderedState());
383                 break;
384
385             case RUNNING:
386                 controlLoop.setState(ControlLoopState.RUNNING2PASSIVE);
387                 controlLoopStateChangePublisher.send(controlLoop);
388                 break;
389
390             default:
391                 exceptionOccured(Response.Status.NOT_ACCEPTABLE, CONTROL_LOOP_CANNOT_TRANSITION_FROM_STATE
392                         + controlLoop.getState().name() + TO_STATE + controlLoop.getOrderedState());
393                 break;
394         }
395     }
396
397     private void superviseControlLoopActivation(ControlLoop controlLoop) throws ControlLoopException {
398         switch (controlLoop.getState()) {
399             case RUNNING:
400                 exceptionOccured(Response.Status.NOT_ACCEPTABLE,
401                         CONTROL_LOOP_IS_ALREADY_IN_STATE + controlLoop.getState().name());
402                 break;
403
404             case PASSIVE2RUNNING:
405                 exceptionOccured(Response.Status.NOT_ACCEPTABLE, CONTROL_LOOP_IS_ALREADY_IN_STATE
406                         + controlLoop.getState().name() + AND_TRANSITIONING_TO_STATE + controlLoop.getOrderedState());
407                 break;
408
409             case PASSIVE:
410                 controlLoop.setState(ControlLoopState.PASSIVE2RUNNING);
411                 controlLoopStateChangePublisher.send(controlLoop);
412                 break;
413
414             default:
415                 exceptionOccured(Response.Status.NOT_ACCEPTABLE, CONTROL_LOOP_CANNOT_TRANSITION_FROM_STATE
416                         + controlLoop.getState().name() + TO_STATE + controlLoop.getOrderedState());
417                 break;
418         }
419     }
420
421     private void checkParticipant(ParticipantMessage participantMessage, ParticipantState participantState,
422             ParticipantHealthStatus healthStatus) throws ControlLoopException, PfModelException {
423         if (participantMessage.getParticipantId() == null) {
424             exceptionOccured(Response.Status.NOT_FOUND, "Participant ID on PARTICIPANT_STATUS message is null");
425         }
426         List<Participant> participantList = participantProvider.getParticipants(
427                 participantMessage.getParticipantId().getName(), participantMessage.getParticipantId().getVersion());
428
429         if (CollectionUtils.isEmpty(participantList)) {
430             var participant = new Participant();
431             participant.setName(participantMessage.getParticipantId().getName());
432             participant.setVersion(participantMessage.getParticipantId().getVersion());
433             participant.setDefinition(participantMessage.getParticipantId());
434             participant.setParticipantType(participantMessage.getParticipantType());
435             participant.setParticipantState(participantState);
436             participant.setHealthStatus(healthStatus);
437
438             participantList.add(participant);
439             participantProvider.createParticipants(participantList);
440         } else {
441             for (Participant participant : participantList) {
442                 participant.setParticipantState(participantState);
443                 participant.setHealthStatus(healthStatus);
444             }
445             participantProvider.updateParticipants(participantList);
446         }
447     }
448
449     private void superviseParticipant(ParticipantStatus participantStatusMessage)
450             throws PfModelException, ControlLoopException {
451
452         checkParticipant(participantStatusMessage, participantStatusMessage.getState(),
453                 participantStatusMessage.getHealthStatus());
454
455         monitoringProvider.createParticipantStatistics(List.of(participantStatusMessage.getParticipantStatistics()));
456     }
457
458     private void superviseControlLoops(ParticipantStatus participantStatusMessage)
459             throws PfModelException, ControlLoopException {
460         if (participantStatusMessage.getControlLoopInfoList() != null) {
461             for (ControlLoopInfo clEntry : participantStatusMessage.getControlLoopInfoList()) {
462                 var dbControlLoop =
463                         controlLoopProvider.getControlLoop(new ToscaConceptIdentifier(clEntry.getControlLoopId()));
464                 if (dbControlLoop == null) {
465                     exceptionOccured(Response.Status.NOT_FOUND,
466                             "PARTICIPANT_STATUS control loop not found in database: " + clEntry.getControlLoopId());
467                 }
468                 dbControlLoop.setState(clEntry.getState());
469                 monitoringProvider.createClElementStatistics(
470                         clEntry.getControlLoopStatistics().getClElementStatisticsList().getClElementStatistics());
471             }
472         }
473     }
474
475     private void exceptionOccured(Response.Status status, String reason) throws ControlLoopException {
476         throw new ControlLoopException(status, reason);
477     }
478 }