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