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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.clamp.controlloop.runtime.supervision;
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;
54 * This class handles supervision of control loop instances, so only one object of this type should be built at a time.
57 * It is effectively a singleton that is started at system start.
61 public class SupervisionHandler {
62 private static final Logger LOGGER = LoggerFactory.getLogger(SupervisionHandler.class);
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 ";
69 private final ControlLoopProvider controlLoopProvider;
70 private final ParticipantProvider participantProvider;
71 private final MonitoringProvider monitoringProvider;
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;
81 * Supervision trigger called when a command is issued on control loops.
84 * Causes supervision to start or continue supervision on the control loops in question.
86 * @param controlLoopIdentifierList the control loops for which the supervision command has been issued
87 * @throws ControlLoopException on supervision triggering exceptions
89 public void triggerControlLoopSupervision(List<ToscaConceptIdentifier> controlLoopIdentifierList)
90 throws ControlLoopException {
92 LOGGER.debug("triggering control loop supervision on control loops {}", controlLoopIdentifierList);
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");
99 for (ToscaConceptIdentifier controlLoopId : controlLoopIdentifierList) {
101 var controlLoop = controlLoopProvider.getControlLoop(controlLoopId);
103 superviseControlLoop(controlLoop);
105 controlLoopProvider.updateControlLoop(controlLoop);
106 } catch (PfModelException pfme) {
107 throw new ControlLoopException(pfme.getErrorResponse().getResponseCode(), pfme.getMessage(), pfme);
113 * Handle a ParticipantStatus message from a participant.
115 * @param participantStatusMessage the ParticipantStatus message received from a participant
118 public void handleParticipantMessage(ParticipantStatus participantStatusMessage) {
119 LOGGER.debug("Participant Status received {}", participantStatusMessage);
121 superviseParticipant(participantStatusMessage);
122 } catch (PfModelException | ControlLoopException svExc) {
123 LOGGER.warn("error supervising participant {}", participantStatusMessage.getParticipantId(), svExc);
128 superviseControlLoops(participantStatusMessage);
129 } catch (PfModelException | ControlLoopException svExc) {
130 LOGGER.warn("error supervising participant {}", participantStatusMessage.getParticipantId(), svExc);
135 * Handle a ParticipantRegister message from a participant.
137 * @param participantRegisterMessage the ParticipantRegister message received from a participant
140 public void handleParticipantMessage(ParticipantRegister participantRegisterMessage) {
141 LOGGER.debug("Participant Register received {}", participantRegisterMessage);
143 participantRegisterAckPublisher.send(participantRegisterMessage.getMessageId(),
144 participantRegisterMessage.getParticipantId(), participantRegisterMessage.getParticipantType());
146 participantUpdatePublisher.send(participantRegisterMessage.getParticipantId(),
147 participantRegisterMessage.getParticipantType(), true);
151 * Handle a ParticipantDeregister message from a participant.
153 * @param participantDeregisterMessage the ParticipantDeregister message received from a participant
156 public void handleParticipantMessage(ParticipantDeregister participantDeregisterMessage) {
157 LOGGER.debug("Participant Deregister received {}", participantDeregisterMessage);
158 participantDeregisterAckPublisher.send(participantDeregisterMessage.getMessageId());
162 * Handle a ParticipantUpdateAck message from a participant.
164 * @param participantUpdateAckMessage the ParticipantUpdateAck message received from a participant
167 public void handleParticipantMessage(ParticipantUpdateAck participantUpdateAckMessage) {
168 LOGGER.debug("Participant Update Ack received {}", participantUpdateAckMessage);
172 * Send commissioning update message to dmaap.
174 * @param participantUpdateMessage the ParticipantUpdate message to send
176 public void handleSendCommissionMessage(ParticipantUpdate participantUpdateMessage) {
177 LOGGER.debug("Participant update message being sent {}", participantUpdateMessage);
179 participantUpdatePublisher.send(participantUpdateMessage.getParticipantId(),
180 participantUpdateMessage.getParticipantType(), true);
184 * Send decommissioning update message to dmaap.
186 * @param participantUpdateMessage the ParticipantUpdate message to send
188 public void handleSendDeCommissionMessage(ParticipantUpdate participantUpdateMessage) {
189 LOGGER.debug("Participant update message being sent {}", participantUpdateMessage);
191 participantUpdatePublisher.send(participantUpdateMessage.getParticipantId(),
192 participantUpdateMessage.getParticipantType(), false);
196 * Handle a ControlLoop update acknowledge message from a participant.
198 * @param controlLoopAckMessage the ControlLoopAck message received from a participant
201 public void handleControlLoopUpdateAckMessage(ControlLoopAck controlLoopAckMessage) {
202 LOGGER.debug("ControlLoop Update Ack message received {}", controlLoopAckMessage);
206 * Handle a ControlLoop statechange acknowledge message from a participant.
208 * @param controlLoopAckMessage the ControlLoopAck message received from a participant
211 public void handleControlLoopStateChangeAckMessage(ControlLoopAck controlLoopAckMessage) {
212 LOGGER.debug("ControlLoop StateChange Ack message received {}", controlLoopAckMessage);
216 * Supervise a control loop, performing whatever actions need to be performed on the control loop.
218 * @param controlLoop the control loop to supervises
219 * @throws ControlLoopException on supervision errors
221 private void superviseControlLoop(ControlLoop controlLoop) throws ControlLoopException {
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);