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;
27 import java.util.UUID;
28 import javax.ws.rs.core.Response;
29 import lombok.AllArgsConstructor;
30 import org.apache.commons.collections4.CollectionUtils;
31 import org.onap.policy.clamp.controlloop.common.exception.ControlLoopException;
32 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoop;
33 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElementAck;
34 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopInfo;
35 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopState;
36 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.Participant;
37 import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ControlLoopProvider;
38 import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ParticipantProvider;
39 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ControlLoopAck;
40 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantDeregister;
41 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantRegister;
42 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantStatus;
43 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantUpdate;
44 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantUpdateAck;
45 import org.onap.policy.clamp.controlloop.runtime.monitoring.MonitoringProvider;
46 import org.onap.policy.clamp.controlloop.runtime.supervision.comm.ControlLoopStateChangePublisher;
47 import org.onap.policy.clamp.controlloop.runtime.supervision.comm.ControlLoopUpdatePublisher;
48 import org.onap.policy.clamp.controlloop.runtime.supervision.comm.ParticipantDeregisterAckPublisher;
49 import org.onap.policy.clamp.controlloop.runtime.supervision.comm.ParticipantRegisterAckPublisher;
50 import org.onap.policy.clamp.controlloop.runtime.supervision.comm.ParticipantUpdatePublisher;
51 import org.onap.policy.models.base.PfModelException;
52 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
55 import org.springframework.stereotype.Component;
58 * This class handles supervision of control loop instances, so only one object of this type should be built at a time.
61 * It is effectively a singleton that is started at system start.
65 public class SupervisionHandler {
66 private static final Logger LOGGER = LoggerFactory.getLogger(SupervisionHandler.class);
68 private static final String CONTROL_LOOP_CANNOT_TRANSITION_FROM_STATE = "Control loop can't transition from state ";
69 private static final String CONTROL_LOOP_IS_ALREADY_IN_STATE = "Control loop is already in state ";
70 private static final String TO_STATE = " to state ";
71 private static final String AND_TRANSITIONING_TO_STATE = " and transitioning to state ";
73 private final ControlLoopProvider controlLoopProvider;
74 private final ParticipantProvider participantProvider;
75 private final MonitoringProvider monitoringProvider;
77 // Publishers for participant communication
78 private final ControlLoopUpdatePublisher controlLoopUpdatePublisher;
79 private final ControlLoopStateChangePublisher controlLoopStateChangePublisher;
80 private final ParticipantRegisterAckPublisher participantRegisterAckPublisher;
81 private final ParticipantDeregisterAckPublisher participantDeregisterAckPublisher;
82 private final ParticipantUpdatePublisher participantUpdatePublisher;
85 * Supervision trigger called when a command is issued on control loops.
88 * Causes supervision to start or continue supervision on the control loops in question.
90 * @param controlLoopIdentifierList the control loops for which the supervision command has been issued
91 * @throws ControlLoopException on supervision triggering exceptions
93 public void triggerControlLoopSupervision(List<ToscaConceptIdentifier> controlLoopIdentifierList)
94 throws ControlLoopException {
96 LOGGER.debug("triggering control loop supervision on control loops {}", controlLoopIdentifierList);
98 if (CollectionUtils.isEmpty(controlLoopIdentifierList)) {
99 // This is just to force throwing of the exception in certain circumstances.
100 exceptionOccured(Response.Status.NOT_ACCEPTABLE, "The list of control loops for supervision is empty");
103 for (ToscaConceptIdentifier controlLoopId : controlLoopIdentifierList) {
105 var controlLoop = controlLoopProvider.getControlLoop(controlLoopId);
107 superviseControlLoop(controlLoop);
109 controlLoopProvider.updateControlLoop(controlLoop);
110 } catch (PfModelException pfme) {
111 throw new ControlLoopException(pfme.getErrorResponse().getResponseCode(), pfme.getMessage(), pfme);
117 * Handle a ParticipantStatus message from a participant.
119 * @param participantStatusMessage the ParticipantStatus message received from a participant
122 public void handleParticipantMessage(ParticipantStatus participantStatusMessage) {
123 LOGGER.debug("Participant Status received {}", participantStatusMessage);
125 superviseParticipant(participantStatusMessage);
126 } catch (PfModelException | ControlLoopException svExc) {
127 LOGGER.warn("error supervising participant {}", participantStatusMessage.getParticipantId(), svExc);
132 superviseControlLoops(participantStatusMessage);
133 } catch (PfModelException | ControlLoopException svExc) {
134 LOGGER.warn("error supervising participant {}", participantStatusMessage.getParticipantId(), svExc);
139 * Handle a ParticipantRegister message from a participant.
141 * @param participantRegisterMessage the ParticipantRegister message received from a participant
144 public void handleParticipantMessage(ParticipantRegister participantRegisterMessage) {
145 LOGGER.debug("Participant Register received {}", participantRegisterMessage);
147 participantRegisterAckPublisher.send(participantRegisterMessage.getMessageId(),
148 participantRegisterMessage.getParticipantId(), participantRegisterMessage.getParticipantType());
150 participantUpdatePublisher.send(participantRegisterMessage.getParticipantId(),
151 participantRegisterMessage.getParticipantType(), true);
155 * Handle a ParticipantDeregister message from a participant.
157 * @param participantDeregisterMessage the ParticipantDeregister message received from a participant
160 public void handleParticipantMessage(ParticipantDeregister participantDeregisterMessage) {
161 LOGGER.debug("Participant Deregister received {}", participantDeregisterMessage);
162 participantDeregisterAckPublisher.send(participantDeregisterMessage.getMessageId());
166 * Handle a ParticipantUpdateAck message from a participant.
168 * @param participantUpdateAckMessage the ParticipantUpdateAck message received from a participant
171 public void handleParticipantMessage(ParticipantUpdateAck participantUpdateAckMessage) {
172 LOGGER.debug("Participant Update Ack received {}", participantUpdateAckMessage);
174 var participantList =
175 participantProvider.getParticipants(participantUpdateAckMessage.getParticipantId().getName(),
176 participantUpdateAckMessage.getParticipantId().getVersion());
178 if (participantList != null) {
179 for (Participant participant : participantList) {
180 participant.setParticipantState(participantUpdateAckMessage.getState());
182 participantProvider.updateParticipants(participantList);
184 LOGGER.warn("Participant not found in database {}", participantUpdateAckMessage.getParticipantId());
186 } catch (PfModelException pfme) {
187 LOGGER.warn("Model exception occured {}", participantUpdateAckMessage.getParticipantId());
192 * Send commissioning update message to dmaap.
194 * @param participantUpdateMessage the ParticipantUpdate message to send
196 public void handleSendCommissionMessage(ParticipantUpdate participantUpdateMessage) {
197 LOGGER.debug("Participant update message being sent {}", participantUpdateMessage);
199 participantUpdatePublisher.send(participantUpdateMessage.getParticipantId(),
200 participantUpdateMessage.getParticipantType(), true);
204 * Send decommissioning update message to dmaap.
206 * @param participantUpdateMessage the ParticipantUpdate message to send
208 public void handleSendDeCommissionMessage(ParticipantUpdate participantUpdateMessage) {
209 LOGGER.debug("Participant update message being sent {}", participantUpdateMessage);
211 participantUpdatePublisher.send(participantUpdateMessage.getParticipantId(),
212 participantUpdateMessage.getParticipantType(), false);
216 * Handle a ControlLoop update acknowledge message from a participant.
218 * @param controlLoopAckMessage the ControlLoopAck message received from a participant
221 public void handleControlLoopUpdateAckMessage(ControlLoopAck controlLoopAckMessage) {
222 LOGGER.debug("ControlLoop Update Ack message received {}", controlLoopAckMessage);
223 setClElementStateInDb(controlLoopAckMessage);
227 * Handle a ControlLoop statechange acknowledge message from a participant.
229 * @param controlLoopAckMessage the ControlLoopAck message received from a participant
232 public void handleControlLoopStateChangeAckMessage(ControlLoopAck controlLoopAckMessage) {
233 LOGGER.debug("ControlLoop StateChange Ack message received {}", controlLoopAckMessage);
234 setClElementStateInDb(controlLoopAckMessage);
237 private void setClElementStateInDb(ControlLoopAck controlLoopAckMessage) {
238 if (controlLoopAckMessage.getControlLoopResultMap() != null) {
240 var controlLoop = controlLoopProvider.getControlLoop(controlLoopAckMessage.getControlLoopId());
241 if (controlLoop != null) {
242 var updated = updateState(controlLoop, controlLoopAckMessage
243 .getControlLoopResultMap().entrySet());
245 controlLoopProvider.updateControlLoop(controlLoop);
248 LOGGER.warn("ControlLoop not found in database {}", controlLoopAckMessage.getControlLoopId());
250 } catch (PfModelException pfme) {
251 LOGGER.warn("Model exception occured {}", controlLoopAckMessage.getControlLoopId());
256 private boolean updateState(ControlLoop controlLoop, Set<Map.Entry<UUID, ControlLoopElementAck>>
257 controlLoopResultSet) {
259 for (var clElementAck : controlLoopResultSet) {
260 var element = controlLoop.getElements().get(clElementAck.getKey());
261 if (element != null) {
262 element.setState(clElementAck.getValue().getState());
270 * Supervise a control loop, performing whatever actions need to be performed on the control loop.
272 * @param controlLoop the control loop to supervises
273 * @throws ControlLoopException on supervision errors
275 private void superviseControlLoop(ControlLoop controlLoop) throws ControlLoopException {
276 switch (controlLoop.getOrderedState()) {
278 superviseControlLoopUninitialization(controlLoop);
282 superviseControlLoopPassivation(controlLoop);
286 superviseControlLoopActivation(controlLoop);
290 exceptionOccured(Response.Status.NOT_ACCEPTABLE,
291 "A control loop cannot be commanded to go into state " + controlLoop.getOrderedState().name());
296 * Supervise a control loop uninitialisation, performing whatever actions need to be performed on the control loop,
297 * control loop ordered state is UNINITIALIZED.
299 * @param controlLoop the control loop to supervises
300 * @throws ControlLoopException on supervision errors
302 private void superviseControlLoopUninitialization(ControlLoop controlLoop) throws ControlLoopException {
303 switch (controlLoop.getState()) {
305 exceptionOccured(Response.Status.NOT_ACCEPTABLE,
306 CONTROL_LOOP_IS_ALREADY_IN_STATE + controlLoop.getState().name());
309 case UNINITIALISED2PASSIVE:
311 controlLoop.setState(ControlLoopState.PASSIVE2UNINITIALISED);
312 controlLoopStateChangePublisher.send(controlLoop);
315 case PASSIVE2UNINITIALISED:
316 exceptionOccured(Response.Status.NOT_ACCEPTABLE, CONTROL_LOOP_IS_ALREADY_IN_STATE
317 + controlLoop.getState().name() + AND_TRANSITIONING_TO_STATE + controlLoop.getOrderedState());
321 exceptionOccured(Response.Status.NOT_ACCEPTABLE, CONTROL_LOOP_CANNOT_TRANSITION_FROM_STATE
322 + controlLoop.getState().name() + TO_STATE + controlLoop.getOrderedState());
327 private void superviseControlLoopPassivation(ControlLoop controlLoop)
328 throws ControlLoopException {
329 switch (controlLoop.getState()) {
331 exceptionOccured(Response.Status.NOT_ACCEPTABLE,
332 CONTROL_LOOP_IS_ALREADY_IN_STATE + controlLoop.getState().name());
335 controlLoop.setState(ControlLoopState.UNINITIALISED2PASSIVE);
336 controlLoopUpdatePublisher.send(controlLoop);
339 case UNINITIALISED2PASSIVE:
340 case RUNNING2PASSIVE:
341 exceptionOccured(Response.Status.NOT_ACCEPTABLE, CONTROL_LOOP_IS_ALREADY_IN_STATE
342 + controlLoop.getState().name() + AND_TRANSITIONING_TO_STATE + controlLoop.getOrderedState());
346 controlLoop.setState(ControlLoopState.RUNNING2PASSIVE);
347 controlLoopStateChangePublisher.send(controlLoop);
351 exceptionOccured(Response.Status.NOT_ACCEPTABLE, CONTROL_LOOP_CANNOT_TRANSITION_FROM_STATE
352 + controlLoop.getState().name() + TO_STATE + controlLoop.getOrderedState());
357 private void superviseControlLoopActivation(ControlLoop controlLoop) throws ControlLoopException {
358 switch (controlLoop.getState()) {
360 exceptionOccured(Response.Status.NOT_ACCEPTABLE,
361 CONTROL_LOOP_IS_ALREADY_IN_STATE + controlLoop.getState().name());
364 case PASSIVE2RUNNING:
365 exceptionOccured(Response.Status.NOT_ACCEPTABLE, CONTROL_LOOP_IS_ALREADY_IN_STATE
366 + controlLoop.getState().name() + AND_TRANSITIONING_TO_STATE + controlLoop.getOrderedState());
370 controlLoop.setState(ControlLoopState.PASSIVE2RUNNING);
371 controlLoopStateChangePublisher.send(controlLoop);
375 exceptionOccured(Response.Status.NOT_ACCEPTABLE, CONTROL_LOOP_CANNOT_TRANSITION_FROM_STATE
376 + controlLoop.getState().name() + TO_STATE + controlLoop.getOrderedState());
381 private void superviseParticipant(ParticipantStatus participantStatusMessage)
382 throws PfModelException, ControlLoopException {
383 if (participantStatusMessage.getParticipantId() == null) {
384 exceptionOccured(Response.Status.NOT_FOUND, "Participant ID on PARTICIPANT_STATUS message is null");
387 List<Participant> participantList =
388 participantProvider.getParticipants(participantStatusMessage.getParticipantId().getName(),
389 participantStatusMessage.getParticipantId().getVersion());
391 if (CollectionUtils.isEmpty(participantList)) {
392 var participant = new Participant();
393 participant.setName(participantStatusMessage.getParticipantId().getName());
394 participant.setVersion(participantStatusMessage.getParticipantId().getVersion());
395 participant.setDefinition(new ToscaConceptIdentifier("unknown", "0.0.0"));
396 participant.setParticipantState(participantStatusMessage.getState());
397 participant.setHealthStatus(participantStatusMessage.getHealthStatus());
399 participantList.add(participant);
400 participantProvider.createParticipants(participantList);
402 for (Participant participant : participantList) {
403 participant.setParticipantState(participantStatusMessage.getState());
404 participant.setHealthStatus(participantStatusMessage.getHealthStatus());
406 participantProvider.updateParticipants(participantList);
409 monitoringProvider.createParticipantStatistics(List.of(participantStatusMessage.getParticipantStatistics()));
412 private void superviseControlLoops(ParticipantStatus participantStatusMessage)
413 throws PfModelException, ControlLoopException {
414 if (participantStatusMessage.getControlLoopInfoList() != null) {
415 for (ControlLoopInfo clEntry : participantStatusMessage.getControlLoopInfoList()) {
417 controlLoopProvider.getControlLoop(new ToscaConceptIdentifier(clEntry.getControlLoopId()));
418 if (dbControlLoop == null) {
419 exceptionOccured(Response.Status.NOT_FOUND,
420 "PARTICIPANT_STATUS control loop not found in database: " + clEntry.getControlLoopId());
422 dbControlLoop.setState(clEntry.getState());
423 monitoringProvider.createClElementStatistics(
424 clEntry.getControlLoopStatistics().getClElementStatisticsList().getClElementStatistics());
429 private void exceptionOccured(Response.Status status, String reason) throws ControlLoopException {
430 throw new ControlLoopException(status, reason);