2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 Nordix Foundation.
4 * ================================================================================
5 * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.clamp.controlloop.runtime.supervision;
25 import java.util.List;
26 import javax.ws.rs.core.Response;
27 import lombok.AllArgsConstructor;
28 import org.apache.commons.collections4.CollectionUtils;
29 import org.onap.policy.clamp.controlloop.common.exception.ControlLoopException;
30 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoop;
31 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopInfo;
32 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopState;
33 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.Participant;
34 import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ControlLoopProvider;
35 import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ParticipantProvider;
36 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ControlLoopAck;
37 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantDeregister;
38 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantRegister;
39 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantStatus;
40 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantUpdate;
41 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantUpdateAck;
42 import org.onap.policy.clamp.controlloop.runtime.monitoring.MonitoringProvider;
43 import org.onap.policy.clamp.controlloop.runtime.supervision.comm.ControlLoopStateChangePublisher;
44 import org.onap.policy.clamp.controlloop.runtime.supervision.comm.ControlLoopUpdatePublisher;
45 import org.onap.policy.clamp.controlloop.runtime.supervision.comm.ParticipantDeregisterAckPublisher;
46 import org.onap.policy.clamp.controlloop.runtime.supervision.comm.ParticipantRegisterAckPublisher;
47 import org.onap.policy.clamp.controlloop.runtime.supervision.comm.ParticipantUpdatePublisher;
48 import org.onap.policy.models.base.PfModelException;
49 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
52 import org.springframework.stereotype.Component;
55 * This class handles supervision of control loop instances, so only one object of this type should be built at a time.
58 * It is effectively a singleton that is started at system start.
62 public class SupervisionHandler {
63 private static final Logger LOGGER = LoggerFactory.getLogger(SupervisionHandler.class);
65 private static final String CONTROL_LOOP_CANNOT_TRANSITION_FROM_STATE = "Control loop can't transition from state ";
66 private static final String CONTROL_LOOP_IS_ALREADY_IN_STATE = "Control loop is already in state ";
67 private static final String TO_STATE = " to state ";
68 private static final String AND_TRANSITIONING_TO_STATE = " and transitioning to state ";
70 private final ControlLoopProvider controlLoopProvider;
71 private final ParticipantProvider participantProvider;
72 private final MonitoringProvider monitoringProvider;
74 // Publishers for participant communication
75 private final ControlLoopUpdatePublisher controlLoopUpdatePublisher;
76 private final ControlLoopStateChangePublisher controlLoopStateChangePublisher;
77 private final ParticipantRegisterAckPublisher participantRegisterAckPublisher;
78 private final ParticipantDeregisterAckPublisher participantDeregisterAckPublisher;
79 private final ParticipantUpdatePublisher participantUpdatePublisher;
82 * Supervision trigger called when a command is issued on control loops.
85 * Causes supervision to start or continue supervision on the control loops in question.
87 * @param controlLoopIdentifierList the control loops for which the supervision command has been issued
88 * @throws ControlLoopException on supervision triggering exceptions
90 public void triggerControlLoopSupervision(List<ToscaConceptIdentifier> controlLoopIdentifierList)
91 throws ControlLoopException {
93 LOGGER.debug("triggering control loop supervision on control loops {}", controlLoopIdentifierList);
95 if (CollectionUtils.isEmpty(controlLoopIdentifierList)) {
96 // This is just to force throwing of the exception in certain circumstances.
97 exceptionOccured(Response.Status.NOT_ACCEPTABLE, "The list of control loops for supervision is empty");
100 for (ToscaConceptIdentifier controlLoopId : controlLoopIdentifierList) {
102 var controlLoop = controlLoopProvider.getControlLoop(controlLoopId);
104 superviseControlLoop(controlLoop);
106 controlLoopProvider.updateControlLoop(controlLoop);
107 } catch (PfModelException pfme) {
108 throw new ControlLoopException(pfme.getErrorResponse().getResponseCode(), pfme.getMessage(), pfme);
114 * Handle a ParticipantStatus message from a participant.
116 * @param participantStatusMessage the ParticipantStatus message received from a participant
119 public void handleParticipantMessage(ParticipantStatus participantStatusMessage) {
120 LOGGER.debug("Participant Status received {}", participantStatusMessage);
122 superviseParticipant(participantStatusMessage);
123 } catch (PfModelException | ControlLoopException svExc) {
124 LOGGER.warn("error supervising participant {}", participantStatusMessage.getParticipantId(), svExc);
129 superviseControlLoops(participantStatusMessage);
130 } catch (PfModelException | ControlLoopException svExc) {
131 LOGGER.warn("error supervising participant {}", participantStatusMessage.getParticipantId(), svExc);
136 * Handle a ParticipantRegister message from a participant.
138 * @param participantRegisterMessage the ParticipantRegister message received from a participant
141 public void handleParticipantMessage(ParticipantRegister participantRegisterMessage) {
142 LOGGER.debug("Participant Register received {}", participantRegisterMessage);
144 participantRegisterAckPublisher.send(participantRegisterMessage.getMessageId(),
145 participantRegisterMessage.getParticipantId(), participantRegisterMessage.getParticipantType());
147 participantUpdatePublisher.send(participantRegisterMessage.getParticipantId(),
148 participantRegisterMessage.getParticipantType(), true);
152 * Handle a ParticipantDeregister message from a participant.
154 * @param participantDeregisterMessage the ParticipantDeregister message received from a participant
157 public void handleParticipantMessage(ParticipantDeregister participantDeregisterMessage) {
158 LOGGER.debug("Participant Deregister received {}", participantDeregisterMessage);
159 participantDeregisterAckPublisher.send(participantDeregisterMessage.getMessageId());
163 * Handle a ParticipantUpdateAck message from a participant.
165 * @param participantUpdateAckMessage the ParticipantUpdateAck message received from a participant
168 public void handleParticipantMessage(ParticipantUpdateAck participantUpdateAckMessage) {
169 LOGGER.debug("Participant Update Ack received {}", participantUpdateAckMessage);
173 * Send commissioning update message to dmaap.
175 * @param participantUpdateMessage the ParticipantUpdate message to send
177 public void handleSendCommissionMessage(ParticipantUpdate participantUpdateMessage) {
178 LOGGER.debug("Participant update message being sent {}", participantUpdateMessage);
180 participantUpdatePublisher.send(participantUpdateMessage.getParticipantId(),
181 participantUpdateMessage.getParticipantType(), true);
185 * Send decommissioning update message to dmaap.
187 * @param participantUpdateMessage the ParticipantUpdate message to send
189 public void handleSendDeCommissionMessage(ParticipantUpdate participantUpdateMessage) {
190 LOGGER.debug("Participant update message being sent {}", participantUpdateMessage);
192 participantUpdatePublisher.send(participantUpdateMessage.getParticipantId(),
193 participantUpdateMessage.getParticipantType(), false);
197 * Handle a ControlLoop update acknowledge message from a participant.
199 * @param controlLoopAckMessage the ControlLoopAck message received from a participant
202 public void handleControlLoopUpdateAckMessage(ControlLoopAck controlLoopAckMessage) {
203 LOGGER.debug("ControlLoop Update Ack message received {}", controlLoopAckMessage);
207 * Handle a ControlLoop statechange acknowledge message from a participant.
209 * @param controlLoopAckMessage the ControlLoopAck message received from a participant
212 public void handleControlLoopStateChangeAckMessage(ControlLoopAck controlLoopAckMessage) {
213 LOGGER.debug("ControlLoop StateChange Ack message received {}", controlLoopAckMessage);
217 * Supervise a control loop, performing whatever actions need to be performed on the control loop.
219 * @param controlLoop the control loop to supervises
220 * @throws PfModelException on accessing models in the database
221 * @throws ControlLoopException on supervision errors
223 private void superviseControlLoop(ControlLoop controlLoop) throws ControlLoopException, PfModelException {
224 switch (controlLoop.getOrderedState()) {
226 superviseControlLoopUninitialization(controlLoop);
230 superviseControlLoopPassivation(controlLoop);
234 superviseControlLoopActivation(controlLoop);
238 exceptionOccured(Response.Status.NOT_ACCEPTABLE,
239 "A control loop cannot be commanded to go into state " + controlLoop.getOrderedState().name());
244 * Supervise a control loop uninitialisation, performing whatever actions need to be performed on the control loop,
245 * control loop ordered state is UNINITIALIZED.
247 * @param controlLoop the control loop to supervises
248 * @throws ControlLoopException on supervision errors
250 private void superviseControlLoopUninitialization(ControlLoop controlLoop) throws ControlLoopException {
251 switch (controlLoop.getState()) {
253 exceptionOccured(Response.Status.NOT_ACCEPTABLE,
254 CONTROL_LOOP_IS_ALREADY_IN_STATE + controlLoop.getState().name());
257 case UNINITIALISED2PASSIVE:
259 controlLoop.setState(ControlLoopState.PASSIVE2UNINITIALISED);
260 controlLoopStateChangePublisher.send(controlLoop);
263 case PASSIVE2UNINITIALISED:
264 exceptionOccured(Response.Status.NOT_ACCEPTABLE, CONTROL_LOOP_IS_ALREADY_IN_STATE
265 + controlLoop.getState().name() + AND_TRANSITIONING_TO_STATE + controlLoop.getOrderedState());
269 exceptionOccured(Response.Status.NOT_ACCEPTABLE, CONTROL_LOOP_CANNOT_TRANSITION_FROM_STATE
270 + controlLoop.getState().name() + TO_STATE + controlLoop.getOrderedState());
275 private void superviseControlLoopPassivation(ControlLoop controlLoop)
276 throws ControlLoopException {
277 switch (controlLoop.getState()) {
279 exceptionOccured(Response.Status.NOT_ACCEPTABLE,
280 CONTROL_LOOP_IS_ALREADY_IN_STATE + controlLoop.getState().name());
283 controlLoop.setState(ControlLoopState.UNINITIALISED2PASSIVE);
284 controlLoopUpdatePublisher.send(controlLoop);
287 case UNINITIALISED2PASSIVE:
288 case RUNNING2PASSIVE:
289 exceptionOccured(Response.Status.NOT_ACCEPTABLE, CONTROL_LOOP_IS_ALREADY_IN_STATE
290 + controlLoop.getState().name() + AND_TRANSITIONING_TO_STATE + controlLoop.getOrderedState());
294 controlLoop.setState(ControlLoopState.RUNNING2PASSIVE);
295 controlLoopStateChangePublisher.send(controlLoop);
299 exceptionOccured(Response.Status.NOT_ACCEPTABLE, CONTROL_LOOP_CANNOT_TRANSITION_FROM_STATE
300 + controlLoop.getState().name() + TO_STATE + controlLoop.getOrderedState());
305 private void superviseControlLoopActivation(ControlLoop controlLoop) throws ControlLoopException {
306 switch (controlLoop.getState()) {
308 exceptionOccured(Response.Status.NOT_ACCEPTABLE,
309 CONTROL_LOOP_IS_ALREADY_IN_STATE + controlLoop.getState().name());
312 case PASSIVE2RUNNING:
313 exceptionOccured(Response.Status.NOT_ACCEPTABLE, CONTROL_LOOP_IS_ALREADY_IN_STATE
314 + controlLoop.getState().name() + AND_TRANSITIONING_TO_STATE + controlLoop.getOrderedState());
318 controlLoop.setState(ControlLoopState.PASSIVE2RUNNING);
319 controlLoopStateChangePublisher.send(controlLoop);
323 exceptionOccured(Response.Status.NOT_ACCEPTABLE, CONTROL_LOOP_CANNOT_TRANSITION_FROM_STATE
324 + controlLoop.getState().name() + TO_STATE + controlLoop.getOrderedState());
329 private void superviseParticipant(ParticipantStatus participantStatusMessage)
330 throws PfModelException, ControlLoopException {
331 if (participantStatusMessage.getParticipantId() == null) {
332 exceptionOccured(Response.Status.NOT_FOUND, "Participant ID on PARTICIPANT_STATUS message is null");
335 List<Participant> participantList =
336 participantProvider.getParticipants(participantStatusMessage.getParticipantId().getName(),
337 participantStatusMessage.getParticipantId().getVersion());
339 if (CollectionUtils.isEmpty(participantList)) {
340 var participant = new Participant();
341 participant.setName(participantStatusMessage.getParticipantId().getName());
342 participant.setVersion(participantStatusMessage.getParticipantId().getVersion());
343 participant.setDefinition(new ToscaConceptIdentifier("unknown", "0.0.0"));
344 participant.setParticipantState(participantStatusMessage.getState());
345 participant.setHealthStatus(participantStatusMessage.getHealthStatus());
347 participantList.add(participant);
348 participantProvider.createParticipants(participantList);
350 for (Participant participant : participantList) {
351 participant.setParticipantState(participantStatusMessage.getState());
352 participant.setHealthStatus(participantStatusMessage.getHealthStatus());
354 participantProvider.updateParticipants(participantList);
357 monitoringProvider.createParticipantStatistics(List.of(participantStatusMessage.getParticipantStatistics()));
360 private void superviseControlLoops(ParticipantStatus participantStatusMessage)
361 throws PfModelException, ControlLoopException {
362 if (participantStatusMessage.getControlLoopInfoList() != null) {
363 for (ControlLoopInfo clEntry : participantStatusMessage.getControlLoopInfoList()) {
365 controlLoopProvider.getControlLoop(new ToscaConceptIdentifier(clEntry.getControlLoopId()));
366 if (dbControlLoop == null) {
367 exceptionOccured(Response.Status.NOT_FOUND,
368 "PARTICIPANT_STATUS control loop not found in database: " + clEntry.getControlLoopId());
370 dbControlLoop.setState(clEntry.getState());
371 monitoringProvider.createClElementStatistics(
372 clEntry.getControlLoopStatistics().getClElementStatisticsList().getClElementStatistics());
377 private void exceptionOccured(Response.Status status, String reason) throws ControlLoopException {
378 throw new ControlLoopException(status, reason);