2cc0f94e2bbd41ddfb446f7a7154cf0859c7d63b
[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
144         participantUpdatePublisher.send(participantRegisterMessage.getParticipantId(),
145                 participantRegisterMessage.getParticipantType(), true);
146     }
147
148     /**
149      * Handle a ParticipantDeregister message from a participant.
150      *
151      * @param participantDeregisterMessage the ParticipantDeregister message received from a participant
152      */
153     @MessageIntercept
154     public void handleParticipantMessage(ParticipantDeregister participantDeregisterMessage) {
155         LOGGER.debug("Participant Deregister received {}", participantDeregisterMessage);
156         participantDeregisterAckPublisher.send(participantDeregisterMessage.getMessageId());
157     }
158
159     /**
160      * Handle a ParticipantUpdateAck message from a participant.
161      *
162      * @param participantUpdateAckMessage the ParticipantUpdateAck message received from a participant
163      */
164     @MessageIntercept
165     public void handleParticipantMessage(ParticipantUpdateAck participantUpdateAckMessage) {
166         LOGGER.debug("Participant Update Ack received {}", participantUpdateAckMessage);
167     }
168
169     /**
170      * Send commissioning update message to dmaap.
171      *
172      * @param participantUpdateMessage the ParticipantUpdate message to send
173      */
174     public void handleSendCommissionMessage(ParticipantUpdate participantUpdateMessage) {
175         LOGGER.debug("Participant update message being sent {}", participantUpdateMessage);
176
177         participantUpdatePublisher.send(participantUpdateMessage.getParticipantId(),
178             participantUpdateMessage.getParticipantType(), true);
179     }
180
181     /**
182      * Send decommissioning update message to dmaap.
183      *
184      * @param participantUpdateMessage the ParticipantUpdate message to send
185      */
186     public void handleSendDeCommissionMessage(ParticipantUpdate participantUpdateMessage) {
187         LOGGER.debug("Participant update message being sent {}", participantUpdateMessage);
188
189         participantUpdatePublisher.send(participantUpdateMessage.getParticipantId(),
190             participantUpdateMessage.getParticipantType(), false);
191     }
192
193     /**
194      * Handle a ControlLoop update acknowledge message from a participant.
195      *
196      * @param controlLoopAckMessage the ControlLoopAck message received from a participant
197      */
198     @MessageIntercept
199     public void handleControlLoopUpdateAckMessage(ControlLoopAck controlLoopAckMessage) {
200         LOGGER.debug("ControlLoop Update Ack message received {}", controlLoopAckMessage);
201     }
202
203     /**
204      * Handle a ControlLoop statechange acknowledge message from a participant.
205      *
206      * @param controlLoopAckMessage the ControlLoopAck message received from a participant
207      */
208     @MessageIntercept
209     public void handleControlLoopStateChangeAckMessage(ControlLoopAck controlLoopAckMessage) {
210         LOGGER.debug("ControlLoop StateChange Ack message received {}", controlLoopAckMessage);
211     }
212
213     /**
214      * Supervise a control loop, performing whatever actions need to be performed on the control loop.
215      *
216      * @param controlLoop the control loop to supervises
217      * @throws PfModelException on accessing models in the database
218      * @throws ControlLoopException on supervision errors
219      */
220     private void superviseControlLoop(ControlLoop controlLoop) throws ControlLoopException, PfModelException {
221         switch (controlLoop.getOrderedState()) {
222             case UNINITIALISED:
223                 superviseControlLoopUninitialization(controlLoop);
224                 break;
225
226             case PASSIVE:
227                 superviseControlLoopPassivation(controlLoop);
228                 break;
229
230             case RUNNING:
231                 superviseControlLoopActivation(controlLoop);
232                 break;
233
234             default:
235                 exceptionOccured(Response.Status.NOT_ACCEPTABLE,
236                         "A control loop cannot be commanded to go into state " + controlLoop.getOrderedState().name());
237         }
238     }
239
240     /**
241      * Supervise a control loop uninitialisation, performing whatever actions need to be performed on the control loop,
242      * control loop ordered state is UNINITIALIZED.
243      *
244      * @param controlLoop the control loop to supervises
245      * @throws ControlLoopException on supervision errors
246      */
247     private void superviseControlLoopUninitialization(ControlLoop controlLoop) throws ControlLoopException {
248         switch (controlLoop.getState()) {
249             case UNINITIALISED:
250                 exceptionOccured(Response.Status.NOT_ACCEPTABLE,
251                         CONTROL_LOOP_IS_ALREADY_IN_STATE + controlLoop.getState().name());
252                 break;
253
254             case UNINITIALISED2PASSIVE:
255             case PASSIVE:
256                 controlLoop.setState(ControlLoopState.PASSIVE2UNINITIALISED);
257                 controlLoopStateChangePublisher.send(controlLoop);
258                 break;
259
260             case PASSIVE2UNINITIALISED:
261                 exceptionOccured(Response.Status.NOT_ACCEPTABLE, CONTROL_LOOP_IS_ALREADY_IN_STATE
262                         + controlLoop.getState().name() + AND_TRANSITIONING_TO_STATE + controlLoop.getOrderedState());
263                 break;
264
265             default:
266                 exceptionOccured(Response.Status.NOT_ACCEPTABLE, CONTROL_LOOP_CANNOT_TRANSITION_FROM_STATE
267                         + controlLoop.getState().name() + TO_STATE + controlLoop.getOrderedState());
268                 break;
269         }
270     }
271
272     private void superviseControlLoopPassivation(ControlLoop controlLoop)
273             throws ControlLoopException {
274         switch (controlLoop.getState()) {
275             case PASSIVE:
276                 exceptionOccured(Response.Status.NOT_ACCEPTABLE,
277                         CONTROL_LOOP_IS_ALREADY_IN_STATE + controlLoop.getState().name());
278                 break;
279             case UNINITIALISED:
280                 controlLoop.setState(ControlLoopState.UNINITIALISED2PASSIVE);
281                 controlLoopUpdatePublisher.send(controlLoop);
282                 break;
283
284             case UNINITIALISED2PASSIVE:
285             case RUNNING2PASSIVE:
286                 exceptionOccured(Response.Status.NOT_ACCEPTABLE, CONTROL_LOOP_IS_ALREADY_IN_STATE
287                         + controlLoop.getState().name() + AND_TRANSITIONING_TO_STATE + controlLoop.getOrderedState());
288                 break;
289
290             case RUNNING:
291                 controlLoop.setState(ControlLoopState.RUNNING2PASSIVE);
292                 controlLoopStateChangePublisher.send(controlLoop);
293                 break;
294
295             default:
296                 exceptionOccured(Response.Status.NOT_ACCEPTABLE, CONTROL_LOOP_CANNOT_TRANSITION_FROM_STATE
297                         + controlLoop.getState().name() + TO_STATE + controlLoop.getOrderedState());
298                 break;
299         }
300     }
301
302     private void superviseControlLoopActivation(ControlLoop controlLoop) throws ControlLoopException {
303         switch (controlLoop.getState()) {
304             case RUNNING:
305                 exceptionOccured(Response.Status.NOT_ACCEPTABLE,
306                         CONTROL_LOOP_IS_ALREADY_IN_STATE + controlLoop.getState().name());
307                 break;
308
309             case PASSIVE2RUNNING:
310                 exceptionOccured(Response.Status.NOT_ACCEPTABLE, CONTROL_LOOP_IS_ALREADY_IN_STATE
311                         + controlLoop.getState().name() + AND_TRANSITIONING_TO_STATE + controlLoop.getOrderedState());
312                 break;
313
314             case PASSIVE:
315                 controlLoop.setState(ControlLoopState.PASSIVE2RUNNING);
316                 controlLoopStateChangePublisher.send(controlLoop);
317                 break;
318
319             default:
320                 exceptionOccured(Response.Status.NOT_ACCEPTABLE, CONTROL_LOOP_CANNOT_TRANSITION_FROM_STATE
321                         + controlLoop.getState().name() + TO_STATE + controlLoop.getOrderedState());
322                 break;
323         }
324     }
325
326     private void superviseParticipant(ParticipantStatus participantStatusMessage)
327             throws PfModelException, ControlLoopException {
328         if (participantStatusMessage.getParticipantId() == null) {
329             exceptionOccured(Response.Status.NOT_FOUND, "Participant ID on PARTICIPANT_STATUS message is null");
330         }
331
332         List<Participant> participantList =
333                 participantProvider.getParticipants(participantStatusMessage.getParticipantId().getName(),
334                         participantStatusMessage.getParticipantId().getVersion());
335
336         if (CollectionUtils.isEmpty(participantList)) {
337             var participant = new Participant();
338             participant.setName(participantStatusMessage.getParticipantId().getName());
339             participant.setVersion(participantStatusMessage.getParticipantId().getVersion());
340             participant.setDefinition(new ToscaConceptIdentifier("unknown", "0.0.0"));
341             participant.setParticipantState(participantStatusMessage.getState());
342             participant.setHealthStatus(participantStatusMessage.getHealthStatus());
343
344             participantList.add(participant);
345             participantProvider.createParticipants(participantList);
346         } else {
347             for (Participant participant : participantList) {
348                 participant.setParticipantState(participantStatusMessage.getState());
349                 participant.setHealthStatus(participantStatusMessage.getHealthStatus());
350             }
351             participantProvider.updateParticipants(participantList);
352         }
353
354         monitoringProvider.createParticipantStatistics(List.of(participantStatusMessage.getParticipantStatistics()));
355     }
356
357     private void superviseControlLoops(ParticipantStatus participantStatusMessage)
358             throws PfModelException, ControlLoopException {
359         if (participantStatusMessage.getControlLoopInfoList() != null) {
360             for (ControlLoopInfo clEntry : participantStatusMessage.getControlLoopInfoList()) {
361                 var dbControlLoop = controlLoopProvider.getControlLoop(
362                         new ToscaConceptIdentifier(clEntry.getControlLoopId()));
363                 if (dbControlLoop == null) {
364                     exceptionOccured(Response.Status.NOT_FOUND,
365                             "PARTICIPANT_STATUS control loop not found in database: " + clEntry.getControlLoopId());
366                 }
367                 dbControlLoop.setState(clEntry.getState());
368                 monitoringProvider.createClElementStatistics(clEntry.getControlLoopStatistics()
369                         .getClElementStatisticsList().getClElementStatistics());
370             }
371         }
372     }
373
374     private void exceptionOccured(Response.Status status, String reason) throws ControlLoopException {
375         throw new ControlLoopException(status, reason);
376     }
377 }