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