789e4fa92e2a7df24ad7a0963f9694a0d3278c59
[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 javax.ws.rs.core.Response;
26 import lombok.AllArgsConstructor;
27 import org.apache.commons.collections4.CollectionUtils;
28 import org.onap.policy.clamp.controlloop.common.exception.ControlLoopException;
29 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoop;
30 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopInfo;
31 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopState;
32 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.Participant;
33 import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ControlLoopProvider;
34 import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ParticipantProvider;
35 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ControlLoopAck;
36 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantDeregister;
37 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantRegister;
38 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantStatus;
39 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantUpdate;
40 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantUpdateAck;
41 import org.onap.policy.clamp.controlloop.runtime.monitoring.MonitoringProvider;
42 import org.onap.policy.clamp.controlloop.runtime.supervision.comm.ControlLoopStateChangePublisher;
43 import org.onap.policy.clamp.controlloop.runtime.supervision.comm.ControlLoopUpdatePublisher;
44 import org.onap.policy.clamp.controlloop.runtime.supervision.comm.ParticipantDeregisterAckPublisher;
45 import org.onap.policy.clamp.controlloop.runtime.supervision.comm.ParticipantRegisterAckPublisher;
46 import org.onap.policy.clamp.controlloop.runtime.supervision.comm.ParticipantUpdatePublisher;
47 import org.onap.policy.models.base.PfModelException;
48 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
51 import org.springframework.stereotype.Component;
52
53 /**
54  * This class handles supervision of control loop instances, so only one object of this type should be built at a time.
55  *
56  * <p/>
57  * It is effectively a singleton that is started at system start.
58  */
59 @Component
60 @AllArgsConstructor
61 public class SupervisionHandler {
62     private static final Logger LOGGER = LoggerFactory.getLogger(SupervisionHandler.class);
63
64     private static final String CONTROL_LOOP_CANNOT_TRANSITION_FROM_STATE = "Control loop can't transition from state ";
65     private static final String CONTROL_LOOP_IS_ALREADY_IN_STATE = "Control loop is already in state ";
66     private static final String TO_STATE = " to state ";
67     private static final String AND_TRANSITIONING_TO_STATE = " and transitioning to state ";
68
69     private final ControlLoopProvider controlLoopProvider;
70     private final ParticipantProvider participantProvider;
71     private final MonitoringProvider monitoringProvider;
72
73     // Publishers for participant communication
74     private final ControlLoopUpdatePublisher controlLoopUpdatePublisher;
75     private final ControlLoopStateChangePublisher controlLoopStateChangePublisher;
76     private final ParticipantRegisterAckPublisher participantRegisterAckPublisher;
77     private final ParticipantDeregisterAckPublisher participantDeregisterAckPublisher;
78     private final ParticipantUpdatePublisher participantUpdatePublisher;
79
80     /**
81      * Supervision trigger called when a command is issued on control loops.
82      *
83      * <p/>
84      * Causes supervision to start or continue supervision on the control loops in question.
85      *
86      * @param controlLoopIdentifierList the control loops for which the supervision command has been issued
87      * @throws ControlLoopException on supervision triggering exceptions
88      */
89     public void triggerControlLoopSupervision(List<ToscaConceptIdentifier> controlLoopIdentifierList)
90             throws ControlLoopException {
91
92         LOGGER.debug("triggering control loop supervision on control loops {}", controlLoopIdentifierList);
93
94         if (CollectionUtils.isEmpty(controlLoopIdentifierList)) {
95             // This is just to force throwing of the exception in certain circumstances.
96             exceptionOccured(Response.Status.NOT_ACCEPTABLE, "The list of control loops for supervision is empty");
97         }
98
99         for (ToscaConceptIdentifier controlLoopId : controlLoopIdentifierList) {
100             try {
101                 var controlLoop = controlLoopProvider.getControlLoop(controlLoopId);
102
103                 superviseControlLoop(controlLoop);
104
105                 controlLoopProvider.updateControlLoop(controlLoop);
106             } catch (PfModelException pfme) {
107                 throw new ControlLoopException(pfme.getErrorResponse().getResponseCode(), pfme.getMessage(), pfme);
108             }
109         }
110     }
111
112     /**
113      * Handle a ParticipantStatus message from a participant.
114      *
115      * @param participantStatusMessage the ParticipantStatus message received from a participant
116      */
117     @MessageIntercept
118     public void handleParticipantMessage(ParticipantStatus participantStatusMessage) {
119         LOGGER.debug("Participant Status received {}", participantStatusMessage);
120         try {
121             superviseParticipant(participantStatusMessage);
122         } catch (PfModelException | ControlLoopException svExc) {
123             LOGGER.warn("error supervising participant {}", participantStatusMessage.getParticipantId(), svExc);
124             return;
125         }
126
127         try {
128             superviseControlLoops(participantStatusMessage);
129         } catch (PfModelException | ControlLoopException svExc) {
130             LOGGER.warn("error supervising participant {}", participantStatusMessage.getParticipantId(), svExc);
131         }
132     }
133
134     /**
135      * Handle a ParticipantRegister message from a participant.
136      *
137      * @param participantRegisterMessage the ParticipantRegister message received from a participant
138      */
139     @MessageIntercept
140     public void handleParticipantMessage(ParticipantRegister participantRegisterMessage) {
141         LOGGER.debug("Participant Register received {}", participantRegisterMessage);
142
143         participantRegisterAckPublisher.send(participantRegisterMessage.getMessageId(),
144                 participantRegisterMessage.getParticipantId(), participantRegisterMessage.getParticipantType());
145
146         participantUpdatePublisher.send(participantRegisterMessage.getParticipantId(),
147                 participantRegisterMessage.getParticipantType(), true);
148     }
149
150     /**
151      * Handle a ParticipantDeregister message from a participant.
152      *
153      * @param participantDeregisterMessage the ParticipantDeregister message received from a participant
154      */
155     @MessageIntercept
156     public void handleParticipantMessage(ParticipantDeregister participantDeregisterMessage) {
157         LOGGER.debug("Participant Deregister received {}", participantDeregisterMessage);
158         participantDeregisterAckPublisher.send(participantDeregisterMessage.getMessageId());
159     }
160
161     /**
162      * Handle a ParticipantUpdateAck message from a participant.
163      *
164      * @param participantUpdateAckMessage the ParticipantUpdateAck message received from a participant
165      */
166     @MessageIntercept
167     public void handleParticipantMessage(ParticipantUpdateAck participantUpdateAckMessage) {
168         LOGGER.debug("Participant Update Ack received {}", participantUpdateAckMessage);
169     }
170
171     /**
172      * Send commissioning update message to dmaap.
173      *
174      * @param participantUpdateMessage the ParticipantUpdate message to send
175      */
176     public void handleSendCommissionMessage(ParticipantUpdate participantUpdateMessage) {
177         LOGGER.debug("Participant update message being sent {}", participantUpdateMessage);
178
179         participantUpdatePublisher.send(participantUpdateMessage.getParticipantId(),
180             participantUpdateMessage.getParticipantType(), true);
181     }
182
183     /**
184      * Send decommissioning update message to dmaap.
185      *
186      * @param participantUpdateMessage the ParticipantUpdate message to send
187      */
188     public void handleSendDeCommissionMessage(ParticipantUpdate participantUpdateMessage) {
189         LOGGER.debug("Participant update message being sent {}", participantUpdateMessage);
190
191         participantUpdatePublisher.send(participantUpdateMessage.getParticipantId(),
192             participantUpdateMessage.getParticipantType(), false);
193     }
194
195     /**
196      * Handle a ControlLoop update acknowledge message from a participant.
197      *
198      * @param controlLoopAckMessage the ControlLoopAck message received from a participant
199      */
200     @MessageIntercept
201     public void handleControlLoopUpdateAckMessage(ControlLoopAck controlLoopAckMessage) {
202         LOGGER.debug("ControlLoop Update Ack message received {}", controlLoopAckMessage);
203     }
204
205     /**
206      * Handle a ControlLoop statechange acknowledge message from a participant.
207      *
208      * @param controlLoopAckMessage the ControlLoopAck message received from a participant
209      */
210     @MessageIntercept
211     public void handleControlLoopStateChangeAckMessage(ControlLoopAck controlLoopAckMessage) {
212         LOGGER.debug("ControlLoop StateChange Ack message received {}", controlLoopAckMessage);
213     }
214
215     /**
216      * Supervise a control loop, performing whatever actions need to be performed on the control loop.
217      *
218      * @param controlLoop the control loop to supervises
219      * @throws ControlLoopException on supervision errors
220      */
221     private void superviseControlLoop(ControlLoop controlLoop) throws ControlLoopException {
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 }