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.participant.intermediary.handler;
23 import java.io.Closeable;
24 import java.util.LinkedHashMap;
26 import java.util.Objects;
27 import java.util.UUID;
30 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElementDefinition;
31 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.Participant;
32 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantHealthStatus;
33 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantState;
34 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantStatistics;
35 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ControlLoopUpdate;
36 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantControlLoopStateChange;
37 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantDeregister;
38 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantDeregisterAck;
39 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantHealthCheck;
40 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantMessage;
41 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantRegister;
42 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantRegisterAck;
43 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantResponseDetails;
44 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantResponseStatus;
45 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantStatus;
46 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantUpdate;
47 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantUpdateAck;
48 import org.onap.policy.clamp.controlloop.participant.intermediary.comm.MessageSender;
49 import org.onap.policy.clamp.controlloop.participant.intermediary.comm.ParticipantMessagePublisher;
50 import org.onap.policy.clamp.controlloop.participant.intermediary.parameters.ParticipantParameters;
51 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
54 import org.springframework.stereotype.Component;
57 * This class is responsible for managing the state of a participant.
61 public class ParticipantHandler implements Closeable {
62 private static final Logger LOGGER = LoggerFactory.getLogger(ParticipantHandler.class);
64 private final ToscaConceptIdentifier participantType;
65 private final ToscaConceptIdentifier participantId;
66 private final MessageSender sender;
67 private final ControlLoopHandler controlLoopHandler;
68 private final ParticipantStatistics participantStatistics;
71 private ParticipantState state = ParticipantState.UNKNOWN;
74 private ParticipantHealthStatus healthStatus = ParticipantHealthStatus.UNKNOWN;
76 private final Map<UUID, ControlLoopElementDefinition> clElementDefsOnThisParticipant = new LinkedHashMap<>();
79 * Constructor, set the participant ID and sender.
81 * @param parameters the parameters of the participant
82 * @param publisher the publisher for sending responses to messages
84 public ParticipantHandler(ParticipantParameters parameters, ParticipantMessagePublisher publisher) {
85 this.participantType = parameters.getIntermediaryParameters().getParticipantType();
86 this.participantId = parameters.getIntermediaryParameters().getParticipantId();
88 new MessageSender(this, publisher,
89 parameters.getIntermediaryParameters().getReportingTimeIntervalMs());
90 this.controlLoopHandler = new ControlLoopHandler(parameters.getIntermediaryParameters(), sender);
91 this.participantStatistics = new ParticipantStatistics();
100 * Method which handles a participant health check event from clamp.
102 * @param healthCheckMsg participant health check message
104 public void handleParticipantHealthCheck(final ParticipantHealthCheck healthCheckMsg) {
105 var response = new ParticipantResponseDetails(healthCheckMsg);
106 response.setResponseStatus(ParticipantResponseStatus.SUCCESS);
107 response.setResponseMessage(healthStatus.toString());
109 sender.sendResponse(response);
113 * Handle a control loop update message.
115 * @param updateMsg the update message
117 public void handleControlLoopUpdate(ControlLoopUpdate updateMsg) {
118 controlLoopHandler.handleControlLoopUpdate(updateMsg);
122 * Handle a control loop state change message.
124 * @param stateChangeMsg the state change message
126 public void handleControlLoopStateChange(ParticipantControlLoopStateChange stateChangeMsg) {
127 controlLoopHandler.handleControlLoopStateChange(stateChangeMsg);
130 private void handleStateChange(ParticipantState newParticipantState, ParticipantResponseDetails response) {
131 if (state.equals(newParticipantState)) {
132 response.setResponseStatus(ParticipantResponseStatus.SUCCESS);
133 response.setResponseMessage("Participant already in state " + newParticipantState);
135 response.setResponseStatus(ParticipantResponseStatus.SUCCESS);
136 response.setResponseMessage("Participant state changed from " + state + " to " + newParticipantState);
137 state = newParticipantState;
142 * Method to update participant state.
144 * @param definition participant definition
145 * @param participantState participant state
146 * @return the participant
148 public Participant updateParticipantState(ToscaConceptIdentifier definition, ParticipantState participantState) {
149 if (!Objects.equals(definition, participantId)) {
150 LOGGER.debug("No participant with this ID {}", definition.getName());
153 var response = new ParticipantResponseDetails();
154 handleStateChange(participantState, response);
155 sender.sendResponse(response);
156 return getParticipant(definition.getName(), definition.getVersion());
160 * Get participants as a {@link Participant} class.
162 * @param name the participant name to get
163 * @param version the version of the participant to get
164 * @return the participant
166 public Participant getParticipant(String name, String version) {
167 if (participantId.getName().equals(name)) {
168 var participant = new Participant();
169 participant.setDefinition(participantId);
170 participant.setParticipantState(state);
171 participant.setHealthStatus(healthStatus);
178 * Check if a participant message applies to this participant handler.
180 * @param participantMsg the message to check
181 * @return true if it applies, false otherwise
183 public boolean appliesTo(ParticipantMessage participantMsg) {
184 return participantMsg.appliesTo(participantType, participantId);
188 * Method to send ParticipantRegister message to controlloop runtime.
190 public void sendParticipantRegister() {
191 var participantRegister = new ParticipantRegister();
192 participantRegister.setParticipantId(participantId);
193 participantRegister.setParticipantType(participantType);
195 sender.sendParticipantRegister(participantRegister);
199 * Handle a participantRegister Ack message.
201 * @param participantRegisterAckMsg the participantRegisterAck message
203 public void handleParticipantRegisterAck(ParticipantRegisterAck participantRegisterAckMsg) {
204 LOGGER.debug("ParticipantRegisterAck message received as responseTo {}",
205 participantRegisterAckMsg.getResponseTo());
209 * Method to send ParticipantDeregister message to controlloop runtime.
211 public void sendParticipantDeregister() {
212 var participantDeregister = new ParticipantDeregister();
213 participantDeregister.setParticipantId(participantId);
214 participantDeregister.setParticipantType(participantType);
216 sender.sendParticipantDeregister(participantDeregister);
220 * Handle a participantDeregister Ack message.
222 * @param participantDeregisterAckMsg the participantDeregisterAck message
224 public void handleParticipantDeregisterAck(ParticipantDeregisterAck participantDeregisterAckMsg) {
225 LOGGER.debug("ParticipantDeregisterAck message received as responseTo {}",
226 participantDeregisterAckMsg.getResponseTo());
230 * Handle a ParticipantUpdate message.
232 * @param participantUpdateMsg the ParticipantUpdate message
234 public void handleParticipantUpdate(ParticipantUpdate participantUpdateMsg) {
235 LOGGER.debug("ParticipantUpdate message received for participantId {}",
236 participantUpdateMsg.getParticipantId());
238 if (!participantUpdateMsg.appliesTo(participantType, participantId)) {
242 Map<UUID, ControlLoopElementDefinition> clDefinitionMap =
243 participantUpdateMsg.getParticipantDefinitionUpdateMap().get(participantUpdateMsg.getParticipantId());
245 for (ControlLoopElementDefinition element : clDefinitionMap.values()) {
246 clElementDefsOnThisParticipant.put(element.getId(), element);
249 sendParticipantUpdateAck(participantUpdateMsg.getMessageId());
253 * Method to send ParticipantUpdateAck message to controlloop runtime.
255 public void sendParticipantUpdateAck(UUID messageId) {
256 var participantUpdateAck = new ParticipantUpdateAck();
257 participantUpdateAck.setResponseTo(messageId);
258 participantUpdateAck.setMessage("Participant Update Ack message");
259 participantUpdateAck.setResult(true);
261 sender.sendParticipantUpdateAck(participantUpdateAck);
265 * Method to send heartbeat to controlloop runtime.
267 public ParticipantStatus makeHeartbeat() {
268 ParticipantStatus heartbeat = new ParticipantStatus();
269 heartbeat.setParticipantId(participantId);
270 heartbeat.setParticipantStatistics(participantStatistics);
271 heartbeat.setParticipantType(participantType);
272 heartbeat.setHealthStatus(healthStatus);
273 heartbeat.setMessage("Participant heartbeat message sent from -> " + participantId.getName());