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.participant.intermediary.handler;
25 import java.io.Closeable;
26 import java.time.Instant;
27 import java.util.ArrayList;
28 import java.util.List;
30 import java.util.Objects;
31 import java.util.UUID;
34 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ClElementStatisticsList;
35 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoop;
36 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElementDefinition;
37 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopInfo;
38 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopStatistics;
39 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.Participant;
40 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantDefinition;
41 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantHealthStatus;
42 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantState;
43 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantStatistics;
44 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ControlLoopStateChange;
45 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ControlLoopUpdate;
46 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantAckMessage;
47 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantDeregister;
48 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantDeregisterAck;
49 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantMessage;
50 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantRegister;
51 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantRegisterAck;
52 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantStatus;
53 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantStatusReq;
54 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantUpdate;
55 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantUpdateAck;
56 import org.onap.policy.clamp.controlloop.participant.intermediary.comm.MessageSender;
57 import org.onap.policy.clamp.controlloop.participant.intermediary.comm.ParticipantMessagePublisher;
58 import org.onap.policy.clamp.controlloop.participant.intermediary.parameters.ParticipantParameters;
59 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
60 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
61 import org.slf4j.Logger;
62 import org.slf4j.LoggerFactory;
63 import org.springframework.stereotype.Component;
66 * This class is responsible for managing the state of a participant.
70 public class ParticipantHandler implements Closeable {
71 private static final Logger LOGGER = LoggerFactory.getLogger(ParticipantHandler.class);
73 private final ToscaConceptIdentifier participantType;
74 private final ToscaConceptIdentifier participantId;
75 private final MessageSender sender;
76 private final ControlLoopHandler controlLoopHandler;
77 private final ParticipantStatistics participantStatistics;
80 private ParticipantState state = ParticipantState.UNKNOWN;
83 private ParticipantHealthStatus healthStatus = ParticipantHealthStatus.UNKNOWN;
85 private List<ControlLoopElementDefinition> clElementDefsOnThisParticipant =
88 public ToscaServiceTemplate toscaServiceTemplate = new ToscaServiceTemplate();
91 * Constructor, set the participant ID and sender.
93 * @param parameters the parameters of the participant
94 * @param publisher the publisher for sending responses to messages
96 public ParticipantHandler(ParticipantParameters parameters, ParticipantMessagePublisher publisher) {
97 this.participantType = parameters.getIntermediaryParameters().getParticipantType();
98 this.participantId = parameters.getIntermediaryParameters().getParticipantId();
100 new MessageSender(this, publisher,
101 parameters.getIntermediaryParameters().getReportingTimeIntervalMs());
102 this.controlLoopHandler = new ControlLoopHandler(parameters.getIntermediaryParameters(), sender);
103 this.participantStatistics = new ParticipantStatistics();
104 this.participantStatistics.setParticipantId(participantId);
105 this.participantStatistics.setState(state);
106 this.participantStatistics.setHealthStatus(healthStatus);
107 this.participantStatistics.setTimeStamp(Instant.now());
111 public void close() {
116 * Method which handles a participant health check event from clamp.
118 * @param participantStatusReqMsg participant participantStatusReq message
120 public void handleParticipantStatusReq(final ParticipantStatusReq participantStatusReqMsg) {
121 sender.sendParticipantStatus(makeHeartbeat(true));
125 * Handle a control loop update message.
127 * @param updateMsg the update message
129 public void handleControlLoopUpdate(ControlLoopUpdate updateMsg) {
130 controlLoopHandler.handleControlLoopUpdate(updateMsg, clElementDefsOnThisParticipant);
134 * Handle a control loop state change message.
136 * @param stateChangeMsg the state change message
138 public void handleControlLoopStateChange(ControlLoopStateChange stateChangeMsg) {
139 controlLoopHandler.handleControlLoopStateChange(stateChangeMsg);
142 private void handleStateChange(ParticipantState newParticipantState, ParticipantUpdateAck response) {
143 if (state.equals(newParticipantState)) {
144 response.setResult(false);
145 response.setMessage("Participant already in state " + newParticipantState);
147 response.setResult(true);
148 response.setMessage("Participant state changed from " + state + " to " + newParticipantState);
149 state = newParticipantState;
154 * Method to update participant state.
156 * @param definition participant definition
157 * @param participantState participant state
158 * @return the participant
160 public Participant updateParticipantState(ToscaConceptIdentifier definition, ParticipantState participantState) {
161 if (!Objects.equals(definition, participantId)) {
162 LOGGER.debug("No participant with this ID {}", definition.getName());
166 var participantUpdateAck = new ParticipantUpdateAck();
167 handleStateChange(participantState, participantUpdateAck);
168 sender.sendParticipantUpdateAck(participantUpdateAck);
169 return getParticipant(definition.getName(), definition.getVersion());
173 * Get participants as a {@link Participant} class.
175 * @param name the participant name to get
176 * @param version the version of the participant to get
177 * @return the participant
179 public Participant getParticipant(String name, String version) {
180 if (participantId.getName().equals(name)) {
181 var participant = new Participant();
182 participant.setDefinition(participantId);
183 participant.setParticipantState(state);
184 participant.setHealthStatus(healthStatus);
191 * Check if a participant message applies to this participant handler.
193 * @param participantMsg the message to check
194 * @return true if it applies, false otherwise
196 public boolean appliesTo(ParticipantMessage participantMsg) {
197 return participantMsg.appliesTo(participantType, participantId);
201 * Check if a participant message applies to this participant handler.
203 * @param participantMsg the message to check
204 * @return true if it applies, false otherwise
206 public boolean appliesTo(ParticipantAckMessage participantMsg) {
207 return participantMsg.appliesTo(participantType, participantId);
211 * Method to send ParticipantRegister message to controlloop runtime.
213 public void sendParticipantRegister() {
214 var participantRegister = new ParticipantRegister();
215 participantRegister.setParticipantId(participantId);
216 participantRegister.setParticipantType(participantType);
218 sender.sendParticipantRegister(participantRegister);
222 * Handle a participantRegister Ack message.
224 * @param participantRegisterAckMsg the participantRegisterAck message
226 public void handleParticipantRegisterAck(ParticipantRegisterAck participantRegisterAckMsg) {
227 LOGGER.debug("ParticipantRegisterAck message received as responseTo {}",
228 participantRegisterAckMsg.getResponseTo());
232 * Method to send ParticipantDeregister message to controlloop runtime.
234 public void sendParticipantDeregister() {
235 var participantDeregister = new ParticipantDeregister();
236 participantDeregister.setParticipantId(participantId);
237 participantDeregister.setParticipantType(participantType);
239 sender.sendParticipantDeregister(participantDeregister);
243 * Handle a participantDeregister Ack message.
245 * @param participantDeregisterAckMsg the participantDeregisterAck message
247 public void handleParticipantDeregisterAck(ParticipantDeregisterAck participantDeregisterAckMsg) {
248 LOGGER.debug("ParticipantDeregisterAck message received as responseTo {}",
249 participantDeregisterAckMsg.getResponseTo());
253 * Handle a ParticipantUpdate message.
255 * @param participantUpdateMsg the ParticipantUpdate message
257 public void handleParticipantUpdate(ParticipantUpdate participantUpdateMsg) {
258 LOGGER.debug("ParticipantUpdate message received for participantId {}",
259 participantUpdateMsg.getParticipantId());
261 if (!participantUpdateMsg.appliesTo(participantType, participantId)) {
265 toscaServiceTemplate = participantUpdateMsg.getToscaServiceTemplate();
266 if (toscaServiceTemplate != null) {
267 // This message is to commission the controlloop
268 for (ParticipantDefinition participantDefinition : participantUpdateMsg.getParticipantDefinitionUpdates()) {
269 if (participantDefinition.getParticipantId().equals(participantType)) {
270 clElementDefsOnThisParticipant = participantDefinition.getControlLoopElementDefinitionList();
275 // This message is to decommision the controlloop
276 clElementDefsOnThisParticipant.clear();
278 sendParticipantUpdateAck(participantUpdateMsg.getMessageId());
282 * Method to send ParticipantUpdateAck message to controlloop runtime.
284 public void sendParticipantUpdateAck(UUID messageId) {
285 var participantUpdateAck = new ParticipantUpdateAck();
286 participantUpdateAck.setResponseTo(messageId);
287 participantUpdateAck.setMessage("Participant Update Ack message");
288 participantUpdateAck.setResult(true);
289 participantUpdateAck.setParticipantId(participantId);
290 participantUpdateAck.setParticipantType(participantType);
292 sender.sendParticipantUpdateAck(participantUpdateAck);
296 * Method to send heartbeat to controlloop runtime.
298 public ParticipantStatus makeHeartbeat(boolean responseToParticipantStatusReq) {
299 this.participantStatistics.setState(state);
300 this.participantStatistics.setHealthStatus(healthStatus);
301 this.participantStatistics.setTimeStamp(Instant.now());
303 var heartbeat = new ParticipantStatus();
304 heartbeat.setParticipantId(participantId);
305 heartbeat.setParticipantStatistics(participantStatistics);
306 heartbeat.setParticipantType(participantType);
307 heartbeat.setHealthStatus(healthStatus);
308 heartbeat.setState(state);
309 heartbeat.setControlLoopInfoList(getControlLoopInfoList());
311 if (responseToParticipantStatusReq) {
312 List<ParticipantDefinition> participantDefinitionUpdates = new ArrayList<>();
313 ParticipantDefinition participantDefinition = new ParticipantDefinition();
314 participantDefinition.setParticipantId(participantId);
315 participantDefinition.setControlLoopElementDefinitionList(clElementDefsOnThisParticipant);
316 participantDefinitionUpdates.add(participantDefinition);
317 heartbeat.setParticipantDefinitionUpdates(participantDefinitionUpdates);
323 private List<ControlLoopInfo> getControlLoopInfoList() {
324 List<ControlLoopInfo> controlLoopInfoList = new ArrayList<>();
325 for (Map.Entry<ToscaConceptIdentifier, ControlLoop> entry :
326 controlLoopHandler.getControlLoopMap().entrySet()) {
327 ControlLoopInfo clInfo = new ControlLoopInfo();
328 clInfo.setControlLoopId(entry.getKey());
329 ControlLoopStatistics clStatitistics = new ControlLoopStatistics();
330 clStatitistics.setControlLoopId(entry.getKey());
331 ClElementStatisticsList clElementStatisticsList = new ClElementStatisticsList();
332 clElementStatisticsList.setClElementStatistics(
333 entry.getValue().getControlLoopElementStatisticsList(entry.getValue()));
334 clStatitistics.setClElementStatisticsList(clElementStatisticsList);
335 clInfo.setControlLoopStatistics(clStatitistics);
336 clInfo.setState(entry.getValue().getState());
338 return controlLoopInfoList;