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.time.Instant;
24 import java.util.LinkedHashMap;
25 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.ControlLoopElement;
34 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElementDefinition;
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.ParticipantControlLoopStateChange;
40 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantControlLoopUpdate;
41 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantDeregister;
42 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantDeregisterAck;
43 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantMessageType;
44 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantRegister;
45 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantRegisterAck;
46 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantStatus;
47 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantUpdate;
48 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantUpdateAck;
49 import org.onap.policy.clamp.controlloop.runtime.commissioning.CommissioningProvider;
50 import org.onap.policy.clamp.controlloop.runtime.main.parameters.ClRuntimeParameterGroup;
51 import org.onap.policy.clamp.controlloop.runtime.monitoring.MonitoringProvider;
52 import org.onap.policy.clamp.controlloop.runtime.supervision.comm.ParticipantControlLoopStateChangePublisher;
53 import org.onap.policy.clamp.controlloop.runtime.supervision.comm.ParticipantControlLoopUpdatePublisher;
54 import org.onap.policy.clamp.controlloop.runtime.supervision.comm.ParticipantDeregisterAckPublisher;
55 import org.onap.policy.clamp.controlloop.runtime.supervision.comm.ParticipantRegisterAckPublisher;
56 import org.onap.policy.clamp.controlloop.runtime.supervision.comm.ParticipantStateChangePublisher;
57 import org.onap.policy.clamp.controlloop.runtime.supervision.comm.ParticipantStatusListener;
58 import org.onap.policy.clamp.controlloop.runtime.supervision.comm.ParticipantUpdatePublisher;
59 import org.onap.policy.common.endpoints.event.comm.TopicSink;
60 import org.onap.policy.common.endpoints.listeners.MessageTypeDispatcher;
61 import org.onap.policy.common.utils.services.ServiceManager;
62 import org.onap.policy.common.utils.services.ServiceManagerException;
63 import org.onap.policy.models.base.PfModelException;
64 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
65 import org.slf4j.Logger;
66 import org.slf4j.LoggerFactory;
67 import org.springframework.stereotype.Component;
70 * This class handles supervision of control loop instances, so only one object of this type should be built at a time.
73 * It is effectively a singleton that is started at system start.
77 public class SupervisionHandler {
78 private static final Logger LOGGER = LoggerFactory.getLogger(SupervisionHandler.class);
80 private static final String CONTROL_LOOP_CANNOT_TRANSITION_FROM_STATE = "Control loop can't transition from state ";
81 private static final String CONTROL_LOOP_IS_ALREADY_IN_STATE = "Control loop is already in state ";
82 private static final String TO_STATE = " to state ";
83 private static final String AND_TRANSITIONING_TO_STATE = " and transitioning to state ";
85 private final ControlLoopProvider controlLoopProvider;
86 private final ParticipantProvider participantProvider;
87 private final MonitoringProvider monitoringProvider;
88 private final CommissioningProvider commissioningProvider;
90 // Publishers for participant communication
91 private final ParticipantControlLoopUpdatePublisher controlLoopUpdatePublisher;
92 private final ParticipantControlLoopStateChangePublisher controlLoopStateChangePublisher;
93 private final ParticipantRegisterAckPublisher participantRegisterAckPublisher;
94 private final ParticipantDeregisterAckPublisher participantDeregisterAckPublisher;
95 private final ParticipantUpdatePublisher participantUpdatePublisher;
98 * Supervision trigger called when a command is issued on control loops.
101 * Causes supervision to start or continue supervision on the control loops in question.
103 * @param controlLoopIdentifierList the control loops for which the supervision command has been issued
104 * @throws ControlLoopException on supervision triggering exceptions
106 public void triggerControlLoopSupervision(List<ToscaConceptIdentifier> controlLoopIdentifierList)
107 throws ControlLoopException {
109 LOGGER.debug("triggering control loop supervision on control loops {}", controlLoopIdentifierList);
111 if (CollectionUtils.isEmpty(controlLoopIdentifierList)) {
112 // This is just to force throwing of the exception in certain circumstances.
113 exceptionOccured(Response.Status.NOT_ACCEPTABLE, "The list of control loops for supervision is empty");
116 for (ToscaConceptIdentifier controlLoopId : controlLoopIdentifierList) {
118 var controlLoop = controlLoopProvider.getControlLoop(controlLoopId);
120 superviseControlLoop(controlLoop);
122 controlLoopProvider.updateControlLoop(controlLoop);
123 } catch (PfModelException pfme) {
124 throw new ControlLoopException(pfme.getErrorResponse().getResponseCode(), pfme.getMessage(), pfme);
130 * Handle a ParticipantStatus message from a participant.
132 * @param participantStatusMessage the ParticipantStatus message received from a participant
134 public void handleParticipantMessage(ParticipantStatus participantStatusMessage) {
135 LOGGER.debug("Participant Status received {}", participantStatusMessage);
137 superviseParticipant(participantStatusMessage);
138 } catch (PfModelException | ControlLoopException svExc) {
139 LOGGER.warn("error supervising participant {}", participantStatusMessage.getParticipantId(), svExc);
144 superviseControlLoops(participantStatusMessage);
145 } catch (PfModelException | ControlLoopException svExc) {
146 LOGGER.warn("error supervising participant {}", participantStatusMessage.getParticipantId(), svExc);
151 * Handle a ParticipantRegister message from a participant.
153 * @param participantRegisterMessage the ParticipantRegister message received from a participant
155 public void handleParticipantMessage(ParticipantRegister participantRegisterMessage) {
156 LOGGER.debug("Participant Register received {}", participantRegisterMessage);
157 sendParticipantAckMessage(participantRegisterMessage);
158 sendParticipantUpdate(participantRegisterMessage);
162 * Handle a ParticipantDeregister message from a participant.
164 * @param participantDeregisterMessage the ParticipantDeregister message received from a participant
166 public void handleParticipantMessage(ParticipantDeregister participantDeregisterMessage) {
167 LOGGER.debug("Participant Deregister received {}", participantDeregisterMessage);
168 sendParticipantAckMessage(participantDeregisterMessage);
172 * Handle a ParticipantUpdateAck message from a participant.
174 * @param participantUpdateAckMessage the ParticipantUpdateAck message received from a participant
176 public void handleParticipantMessage(ParticipantUpdateAck participantUpdateAckMessage) {
177 LOGGER.debug("Participant Update Ack received {}", participantUpdateAckMessage);
181 * Supervise a control loop, performing whatever actions need to be performed on the control loop.
183 * @param controlLoop the control loop to supervises
184 * @throws PfModelException on accessing models in the database
185 * @throws ControlLoopException on supervision errors
187 private void superviseControlLoop(ControlLoop controlLoop) throws ControlLoopException, PfModelException {
188 switch (controlLoop.getOrderedState()) {
190 superviseControlLoopUninitialization(controlLoop);
194 superviseControlLoopPassivation(controlLoop);
198 superviseControlLoopActivation(controlLoop);
202 exceptionOccured(Response.Status.NOT_ACCEPTABLE,
203 "A control loop cannot be commanded to go into state " + controlLoop.getOrderedState().name());
208 * Supervise a control loop uninitialisation, performing whatever actions need to be performed on the control loop,
209 * control loop ordered state is UNINITIALIZED.
211 * @param controlLoop the control loop to supervises
212 * @throws ControlLoopException on supervision errors
214 private void superviseControlLoopUninitialization(ControlLoop controlLoop) throws ControlLoopException {
215 switch (controlLoop.getState()) {
217 exceptionOccured(Response.Status.NOT_ACCEPTABLE,
218 CONTROL_LOOP_IS_ALREADY_IN_STATE + controlLoop.getState().name());
221 case UNINITIALISED2PASSIVE:
223 controlLoop.setState(ControlLoopState.PASSIVE2UNINITIALISED);
224 controlLoopStateChangePublisher.send(controlLoop);
227 case PASSIVE2UNINITIALISED:
228 exceptionOccured(Response.Status.NOT_ACCEPTABLE, CONTROL_LOOP_IS_ALREADY_IN_STATE
229 + controlLoop.getState().name() + AND_TRANSITIONING_TO_STATE + controlLoop.getOrderedState());
233 exceptionOccured(Response.Status.NOT_ACCEPTABLE, CONTROL_LOOP_CANNOT_TRANSITION_FROM_STATE
234 + controlLoop.getState().name() + TO_STATE + controlLoop.getOrderedState());
239 private void superviseControlLoopPassivation(ControlLoop controlLoop)
240 throws ControlLoopException, PfModelException {
241 switch (controlLoop.getState()) {
243 exceptionOccured(Response.Status.NOT_ACCEPTABLE,
244 CONTROL_LOOP_IS_ALREADY_IN_STATE + controlLoop.getState().name());
247 controlLoop.setState(ControlLoopState.UNINITIALISED2PASSIVE);
248 controlLoopUpdatePublisher.send(controlLoop);
251 case UNINITIALISED2PASSIVE:
252 case RUNNING2PASSIVE:
253 exceptionOccured(Response.Status.NOT_ACCEPTABLE, CONTROL_LOOP_IS_ALREADY_IN_STATE
254 + controlLoop.getState().name() + AND_TRANSITIONING_TO_STATE + controlLoop.getOrderedState());
258 controlLoop.setState(ControlLoopState.RUNNING2PASSIVE);
259 controlLoopStateChangePublisher.send(controlLoop);
263 exceptionOccured(Response.Status.NOT_ACCEPTABLE, CONTROL_LOOP_CANNOT_TRANSITION_FROM_STATE
264 + controlLoop.getState().name() + TO_STATE + controlLoop.getOrderedState());
269 private void superviseControlLoopActivation(ControlLoop controlLoop) throws ControlLoopException {
270 switch (controlLoop.getState()) {
272 exceptionOccured(Response.Status.NOT_ACCEPTABLE,
273 CONTROL_LOOP_IS_ALREADY_IN_STATE + controlLoop.getState().name());
276 case PASSIVE2RUNNING:
277 exceptionOccured(Response.Status.NOT_ACCEPTABLE, CONTROL_LOOP_IS_ALREADY_IN_STATE
278 + controlLoop.getState().name() + AND_TRANSITIONING_TO_STATE + controlLoop.getOrderedState());
282 controlLoop.setState(ControlLoopState.PASSIVE2RUNNING);
283 controlLoopStateChangePublisher.send(controlLoop);
287 exceptionOccured(Response.Status.NOT_ACCEPTABLE, CONTROL_LOOP_CANNOT_TRANSITION_FROM_STATE
288 + controlLoop.getState().name() + TO_STATE + controlLoop.getOrderedState());
293 private void sendControlLoopUpdate(ControlLoop controlLoop) throws PfModelException {
294 var pclu = new ParticipantControlLoopUpdate();
295 pclu.setControlLoopId(controlLoop.getKey().asIdentifier());
296 pclu.setControlLoop(controlLoop);
297 // TODO: We should look up the correct TOSCA node template here for the control loop
298 // Tiny hack implemented to return the tosca service template entry from the database and be passed onto dmaap
299 pclu.setControlLoopDefinition(commissioningProvider.getToscaServiceTemplate(null, null));
300 controlLoopUpdatePublisher.send(pclu);
303 private void sendControlLoopStateChange(ControlLoop controlLoop) {
304 var clsc = new ParticipantControlLoopStateChange();
305 clsc.setControlLoopId(controlLoop.getKey().asIdentifier());
306 clsc.setMessageId(UUID.randomUUID());
307 clsc.setOrderedState(controlLoop.getOrderedState());
308 controlLoopStateChangePublisher.send(clsc);
311 private void sendParticipantUpdate(ParticipantRegister participantRegisterMessage) {
312 var message = new ParticipantUpdate();
313 message.setParticipantId(participantRegisterMessage.getParticipantId());
314 message.setParticipantType(participantRegisterMessage.getParticipantType());
315 message.setTimestamp(Instant.now());
317 ControlLoopElementDefinition clDefinition = new ControlLoopElementDefinition();
318 clDefinition.setId(UUID.randomUUID());
321 clDefinition.setControlLoopElementToscaServiceTemplate(commissioningProvider
322 .getToscaServiceTemplate(null, null));
323 } catch (PfModelException pfme) {
324 LOGGER.warn("Get of tosca service template failed, cannot send participantupdate", pfme);
328 Map<UUID, ControlLoopElementDefinition> controlLoopElementDefinitionMap = new LinkedHashMap<>();
329 controlLoopElementDefinitionMap.put(UUID.randomUUID(), clDefinition);
331 Map<ToscaConceptIdentifier, Map<UUID, ControlLoopElementDefinition>>
332 participantDefinitionUpdateMap = new LinkedHashMap<>();
333 participantDefinitionUpdateMap.put(participantRegisterMessage.getParticipantId(),
334 controlLoopElementDefinitionMap);
335 message.setParticipantDefinitionUpdateMap(participantDefinitionUpdateMap);
337 LOGGER.debug("Participant Update sent", message);
338 participantUpdatePublisher.send(message);
341 private void sendParticipantAckMessage(ParticipantRegister participantRegisterMessage) {
342 var message = new ParticipantRegisterAck();
343 message.setResponseTo(participantRegisterMessage.getMessageId());
344 message.setMessage("Participant Register Ack");
345 message.setResult(true);
346 participantRegisterAckPublisher.send(message);
349 private void sendParticipantAckMessage(ParticipantDeregister participantDeregisterMessage) {
350 var message = new ParticipantDeregisterAck();
351 message.setResponseTo(participantDeregisterMessage.getMessageId());
352 message.setMessage("Participant Deregister Ack");
353 message.setResult(true);
354 participantDeregisterAckPublisher.send(message);
357 private void superviseParticipant(ParticipantStatus participantStatusMessage)
358 throws PfModelException, ControlLoopException {
359 if (participantStatusMessage.getParticipantId() == null) {
360 exceptionOccured(Response.Status.NOT_FOUND, "Participant ID on PARTICIPANT_STATUS message is null");
363 List<Participant> participantList =
364 participantProvider.getParticipants(participantStatusMessage.getParticipantId().getName(),
365 participantStatusMessage.getParticipantId().getVersion());
367 if (CollectionUtils.isEmpty(participantList)) {
368 var participant = new Participant();
369 participant.setName(participantStatusMessage.getParticipantId().getName());
370 participant.setVersion(participantStatusMessage.getParticipantId().getVersion());
371 participant.setDefinition(new ToscaConceptIdentifier("unknown", "0.0.0"));
372 participant.setParticipantState(participantStatusMessage.getState());
373 participant.setHealthStatus(participantStatusMessage.getHealthStatus());
375 participantList.add(participant);
376 participantProvider.createParticipants(participantList);
378 for (Participant participant : participantList) {
379 participant.setParticipantState(participantStatusMessage.getState());
380 participant.setHealthStatus(participantStatusMessage.getHealthStatus());
382 participantProvider.updateParticipants(participantList);
385 monitoringProvider.createParticipantStatistics(List.of(participantStatusMessage.getParticipantStatistics()));
388 private void superviseControlLoops(ParticipantStatus participantStatusMessage)
389 throws PfModelException, ControlLoopException {
390 if (CollectionUtils.isEmpty(participantStatusMessage.getControlLoops().getControlLoopList())) {
394 for (ControlLoop controlLoop : participantStatusMessage.getControlLoops().getControlLoopList()) {
395 if (controlLoop == null) {
396 exceptionOccured(Response.Status.NOT_FOUND,
397 "PARTICIPANT_STATUS message references unknown control loop: " + controlLoop);
400 var dbControlLoop = controlLoopProvider
401 .getControlLoop(new ToscaConceptIdentifier(controlLoop.getName(), controlLoop.getVersion()));
402 if (dbControlLoop == null) {
403 exceptionOccured(Response.Status.NOT_FOUND,
404 "PARTICIPANT_STATUS control loop not found in database: " + controlLoop);
407 for (ControlLoopElement element : controlLoop.getElements().values()) {
408 ControlLoopElement dbElement = dbControlLoop.getElements().get(element.getId());
410 if (dbElement == null) {
411 exceptionOccured(Response.Status.NOT_FOUND,
412 "PARTICIPANT_STATUS message references unknown control loop element: " + element);
415 // Replace element entry in the database
416 dbControlLoop.getElements().put(element.getId(), element);
418 controlLoopProvider.updateControlLoop(dbControlLoop);
421 for (ControlLoop controlLoop : participantStatusMessage.getControlLoops().getControlLoopList()) {
422 monitoringProvider.createClElementStatistics(controlLoop.getControlLoopElementStatisticsList(controlLoop));
426 private void exceptionOccured(Response.Status status, String reason) throws ControlLoopException {
427 throw new ControlLoopException(status, reason);