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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.clamp.controlloop.runtime.supervision;
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;
53 * This class handles supervision of control loop instances, so only one object of this type should be built at a time.
56 * It is effectively a singleton that is started at system start.
60 public class SupervisionHandler {
61 private static final Logger LOGGER = LoggerFactory.getLogger(SupervisionHandler.class);
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 ";
68 private final ControlLoopProvider controlLoopProvider;
69 private final ParticipantProvider participantProvider;
70 private final MonitoringProvider monitoringProvider;
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;
80 * Supervision trigger called when a command is issued on control loops.
83 * Causes supervision to start or continue supervision on the control loops in question.
85 * @param controlLoopIdentifierList the control loops for which the supervision command has been issued
86 * @throws ControlLoopException on supervision triggering exceptions
88 public void triggerControlLoopSupervision(List<ToscaConceptIdentifier> controlLoopIdentifierList)
89 throws ControlLoopException {
91 LOGGER.debug("triggering control loop supervision on control loops {}", controlLoopIdentifierList);
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");
98 for (ToscaConceptIdentifier controlLoopId : controlLoopIdentifierList) {
100 var controlLoop = controlLoopProvider.getControlLoop(controlLoopId);
102 superviseControlLoop(controlLoop);
104 controlLoopProvider.updateControlLoop(controlLoop);
105 } catch (PfModelException pfme) {
106 throw new ControlLoopException(pfme.getErrorResponse().getResponseCode(), pfme.getMessage(), pfme);
112 * Handle a ParticipantStatus message from a participant.
114 * @param participantStatusMessage the ParticipantStatus message received from a participant
117 public void handleParticipantMessage(ParticipantStatus participantStatusMessage) {
118 LOGGER.debug("Participant Status received {}", participantStatusMessage);
120 superviseParticipant(participantStatusMessage);
121 } catch (PfModelException | ControlLoopException svExc) {
122 LOGGER.warn("error supervising participant {}", participantStatusMessage.getParticipantId(), svExc);
127 superviseControlLoops(participantStatusMessage);
128 } catch (PfModelException | ControlLoopException svExc) {
129 LOGGER.warn("error supervising participant {}", participantStatusMessage.getParticipantId(), svExc);
134 * Handle a ParticipantRegister message from a participant.
136 * @param participantRegisterMessage the ParticipantRegister message received from a participant
139 public void handleParticipantMessage(ParticipantRegister participantRegisterMessage) {
140 LOGGER.debug("Participant Register received {}", participantRegisterMessage);
142 participantRegisterAckPublisher.send(participantRegisterMessage.getMessageId(),
143 participantRegisterMessage.getParticipantId(), participantRegisterMessage.getParticipantType());
145 participantUpdatePublisher.send(participantRegisterMessage.getParticipantId(),
146 participantRegisterMessage.getParticipantType(), true);
150 * Handle a ParticipantDeregister message from a participant.
152 * @param participantDeregisterMessage the ParticipantDeregister message received from a participant
155 public void handleParticipantMessage(ParticipantDeregister participantDeregisterMessage) {
156 LOGGER.debug("Participant Deregister received {}", participantDeregisterMessage);
157 participantDeregisterAckPublisher.send(participantDeregisterMessage.getMessageId());
161 * Handle a ParticipantUpdateAck message from a participant.
163 * @param participantUpdateAckMessage the ParticipantUpdateAck message received from a participant
166 public void handleParticipantMessage(ParticipantUpdateAck participantUpdateAckMessage) {
167 LOGGER.debug("Participant Update Ack received {}", participantUpdateAckMessage);
171 * Send commissioning update message to dmaap.
173 * @param participantUpdateMessage the ParticipantUpdate message to send
175 public void handleSendCommissionMessage(ParticipantUpdate participantUpdateMessage) {
176 LOGGER.debug("Participant update message being sent {}", participantUpdateMessage);
178 participantUpdatePublisher.send(participantUpdateMessage.getParticipantId(),
179 participantUpdateMessage.getParticipantType(), true);
183 * Send decommissioning update message to dmaap.
185 * @param participantUpdateMessage the ParticipantUpdate message to send
187 public void handleSendDeCommissionMessage(ParticipantUpdate participantUpdateMessage) {
188 LOGGER.debug("Participant update message being sent {}", participantUpdateMessage);
190 participantUpdatePublisher.send(participantUpdateMessage.getParticipantId(),
191 participantUpdateMessage.getParticipantType(), false);
195 * Handle a ControlLoop update acknowledge message from a participant.
197 * @param controlLoopAckMessage the ControlLoopAck message received from a participant
200 public void handleControlLoopUpdateAckMessage(ControlLoopAck controlLoopAckMessage) {
201 LOGGER.debug("ControlLoop Update Ack message received {}", controlLoopAckMessage);
205 * Handle a ControlLoop statechange acknowledge message from a participant.
207 * @param controlLoopAckMessage the ControlLoopAck message received from a participant
210 public void handleControlLoopStateChangeAckMessage(ControlLoopAck controlLoopAckMessage) {
211 LOGGER.debug("ControlLoop StateChange Ack message received {}", controlLoopAckMessage);
215 * Supervise a control loop, performing whatever actions need to be performed on the control loop.
217 * @param controlLoop the control loop to supervises
218 * @throws PfModelException on accessing models in the database
219 * @throws ControlLoopException on supervision errors
221 private void superviseControlLoop(ControlLoop controlLoop) throws ControlLoopException, PfModelException {
222 switch (controlLoop.getOrderedState()) {
224 superviseControlLoopUninitialization(controlLoop);
228 superviseControlLoopPassivation(controlLoop);
232 superviseControlLoopActivation(controlLoop);
236 exceptionOccured(Response.Status.NOT_ACCEPTABLE,
237 "A control loop cannot be commanded to go into state " + controlLoop.getOrderedState().name());
242 * Supervise a control loop uninitialisation, performing whatever actions need to be performed on the control loop,
243 * control loop ordered state is UNINITIALIZED.
245 * @param controlLoop the control loop to supervises
246 * @throws ControlLoopException on supervision errors
248 private void superviseControlLoopUninitialization(ControlLoop controlLoop) throws ControlLoopException {
249 switch (controlLoop.getState()) {
251 exceptionOccured(Response.Status.NOT_ACCEPTABLE,
252 CONTROL_LOOP_IS_ALREADY_IN_STATE + controlLoop.getState().name());
255 case UNINITIALISED2PASSIVE:
257 controlLoop.setState(ControlLoopState.PASSIVE2UNINITIALISED);
258 controlLoopStateChangePublisher.send(controlLoop);
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());
267 exceptionOccured(Response.Status.NOT_ACCEPTABLE, CONTROL_LOOP_CANNOT_TRANSITION_FROM_STATE
268 + controlLoop.getState().name() + TO_STATE + controlLoop.getOrderedState());
273 private void superviseControlLoopPassivation(ControlLoop controlLoop)
274 throws ControlLoopException {
275 switch (controlLoop.getState()) {
277 exceptionOccured(Response.Status.NOT_ACCEPTABLE,
278 CONTROL_LOOP_IS_ALREADY_IN_STATE + controlLoop.getState().name());
281 controlLoop.setState(ControlLoopState.UNINITIALISED2PASSIVE);
282 controlLoopUpdatePublisher.send(controlLoop);
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());
292 controlLoop.setState(ControlLoopState.RUNNING2PASSIVE);
293 controlLoopStateChangePublisher.send(controlLoop);
297 exceptionOccured(Response.Status.NOT_ACCEPTABLE, CONTROL_LOOP_CANNOT_TRANSITION_FROM_STATE
298 + controlLoop.getState().name() + TO_STATE + controlLoop.getOrderedState());
303 private void superviseControlLoopActivation(ControlLoop controlLoop) throws ControlLoopException {
304 switch (controlLoop.getState()) {
306 exceptionOccured(Response.Status.NOT_ACCEPTABLE,
307 CONTROL_LOOP_IS_ALREADY_IN_STATE + controlLoop.getState().name());
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());
316 controlLoop.setState(ControlLoopState.PASSIVE2RUNNING);
317 controlLoopStateChangePublisher.send(controlLoop);
321 exceptionOccured(Response.Status.NOT_ACCEPTABLE, CONTROL_LOOP_CANNOT_TRANSITION_FROM_STATE
322 + controlLoop.getState().name() + TO_STATE + controlLoop.getOrderedState());
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");
333 List<Participant> participantList =
334 participantProvider.getParticipants(participantStatusMessage.getParticipantId().getName(),
335 participantStatusMessage.getParticipantId().getVersion());
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());
345 participantList.add(participant);
346 participantProvider.createParticipants(participantList);
348 for (Participant participant : participantList) {
349 participant.setParticipantState(participantStatusMessage.getState());
350 participant.setHealthStatus(participantStatusMessage.getHealthStatus());
352 participantProvider.updateParticipants(participantList);
355 monitoringProvider.createParticipantStatistics(List.of(participantStatusMessage.getParticipantStatistics()));
358 private void superviseControlLoops(ParticipantStatus participantStatusMessage)
359 throws PfModelException, ControlLoopException {
360 if (participantStatusMessage.getControlLoopInfoList() != null) {
361 for (ControlLoopInfo clEntry : participantStatusMessage.getControlLoopInfoList()) {
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());
368 dbControlLoop.setState(clEntry.getState());
369 monitoringProvider.createClElementStatistics(
370 clEntry.getControlLoopStatistics().getClElementStatisticsList().getClElementStatistics());
375 private void exceptionOccured(Response.Status status, String reason) throws ControlLoopException {
376 throw new ControlLoopException(status, reason);