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.ParticipantDeregister;
47 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantDeregisterAck;
48 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantMessage;
49 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantRegister;
50 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantRegisterAck;
51 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantStatus;
52 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantStatusReq;
53 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantUpdate;
54 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantUpdateAck;
55 import org.onap.policy.clamp.controlloop.participant.intermediary.comm.MessageSender;
56 import org.onap.policy.clamp.controlloop.participant.intermediary.comm.ParticipantMessagePublisher;
57 import org.onap.policy.clamp.controlloop.participant.intermediary.parameters.ParticipantParameters;
58 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
59 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
60 import org.slf4j.Logger;
61 import org.slf4j.LoggerFactory;
62 import org.springframework.stereotype.Component;
65 * This class is responsible for managing the state of a participant.
69 public class ParticipantHandler implements Closeable {
70 private static final Logger LOGGER = LoggerFactory.getLogger(ParticipantHandler.class);
72 private final ToscaConceptIdentifier participantType;
73 private final ToscaConceptIdentifier participantId;
74 private final MessageSender sender;
75 private final ControlLoopHandler controlLoopHandler;
76 private final ParticipantStatistics participantStatistics;
79 private ParticipantState state = ParticipantState.UNKNOWN;
82 private ParticipantHealthStatus healthStatus = ParticipantHealthStatus.UNKNOWN;
84 private List<ControlLoopElementDefinition> clElementDefsOnThisParticipant =
87 public ToscaServiceTemplate toscaServiceTemplate = new ToscaServiceTemplate();
90 * Constructor, set the participant ID and sender.
92 * @param parameters the parameters of the participant
93 * @param publisher the publisher for sending responses to messages
95 public ParticipantHandler(ParticipantParameters parameters, ParticipantMessagePublisher publisher) {
96 this.participantType = parameters.getIntermediaryParameters().getParticipantType();
97 this.participantId = parameters.getIntermediaryParameters().getParticipantId();
99 new MessageSender(this, publisher,
100 parameters.getIntermediaryParameters().getReportingTimeIntervalMs());
101 this.controlLoopHandler = new ControlLoopHandler(parameters.getIntermediaryParameters(), sender);
102 this.participantStatistics = new ParticipantStatistics();
103 this.participantStatistics.setParticipantId(participantId);
104 this.participantStatistics.setState(state);
105 this.participantStatistics.setHealthStatus(healthStatus);
106 this.participantStatistics.setTimeStamp(Instant.now());
110 public void close() {
115 * Method which handles a participant health check event from clamp.
117 * @param participantStatusReqMsg participant participantStatusReq message
119 public void handleParticipantStatusReq(final ParticipantStatusReq participantStatusReqMsg) {
120 sender.sendParticipantStatus(makeHeartbeat(true));
124 * Handle a control loop update message.
126 * @param updateMsg the update message
128 public void handleControlLoopUpdate(ControlLoopUpdate updateMsg) {
129 controlLoopHandler.handleControlLoopUpdate(updateMsg, clElementDefsOnThisParticipant);
133 * Handle a control loop state change message.
135 * @param stateChangeMsg the state change message
137 public void handleControlLoopStateChange(ControlLoopStateChange stateChangeMsg) {
138 controlLoopHandler.handleControlLoopStateChange(stateChangeMsg);
141 private void handleStateChange(ParticipantState newParticipantState, ParticipantUpdateAck response) {
142 if (state.equals(newParticipantState)) {
143 response.setResult(false);
144 response.setMessage("Participant already in state " + newParticipantState);
146 response.setResult(true);
147 response.setMessage("Participant state changed from " + state + " to " + newParticipantState);
148 state = newParticipantState;
153 * Method to update participant state.
155 * @param definition participant definition
156 * @param participantState participant state
157 * @return the participant
159 public Participant updateParticipantState(ToscaConceptIdentifier definition, ParticipantState participantState) {
160 if (!Objects.equals(definition, participantId)) {
161 LOGGER.debug("No participant with this ID {}", definition.getName());
165 var participantUpdateAck = new ParticipantUpdateAck();
166 handleStateChange(participantState, participantUpdateAck);
167 sender.sendParticipantUpdateAck(participantUpdateAck);
168 return getParticipant(definition.getName(), definition.getVersion());
172 * Get participants as a {@link Participant} class.
174 * @param name the participant name to get
175 * @param version the version of the participant to get
176 * @return the participant
178 public Participant getParticipant(String name, String version) {
179 if (participantId.getName().equals(name)) {
180 var participant = new Participant();
181 participant.setDefinition(participantId);
182 participant.setParticipantState(state);
183 participant.setHealthStatus(healthStatus);
190 * Check if a participant message applies to this participant handler.
192 * @param participantMsg the message to check
193 * @return true if it applies, false otherwise
195 public boolean appliesTo(ParticipantMessage participantMsg) {
196 return participantMsg.appliesTo(participantType, participantId);
200 * Method to send ParticipantRegister message to controlloop runtime.
202 public void sendParticipantRegister() {
203 var participantRegister = new ParticipantRegister();
204 participantRegister.setParticipantId(participantId);
205 participantRegister.setParticipantType(participantType);
207 sender.sendParticipantRegister(participantRegister);
211 * Handle a participantRegister Ack message.
213 * @param participantRegisterAckMsg the participantRegisterAck message
215 public void handleParticipantRegisterAck(ParticipantRegisterAck participantRegisterAckMsg) {
216 LOGGER.debug("ParticipantRegisterAck message received as responseTo {}",
217 participantRegisterAckMsg.getResponseTo());
221 * Method to send ParticipantDeregister message to controlloop runtime.
223 public void sendParticipantDeregister() {
224 var participantDeregister = new ParticipantDeregister();
225 participantDeregister.setParticipantId(participantId);
226 participantDeregister.setParticipantType(participantType);
228 sender.sendParticipantDeregister(participantDeregister);
232 * Handle a participantDeregister Ack message.
234 * @param participantDeregisterAckMsg the participantDeregisterAck message
236 public void handleParticipantDeregisterAck(ParticipantDeregisterAck participantDeregisterAckMsg) {
237 LOGGER.debug("ParticipantDeregisterAck message received as responseTo {}",
238 participantDeregisterAckMsg.getResponseTo());
242 * Handle a ParticipantUpdate message.
244 * @param participantUpdateMsg the ParticipantUpdate message
246 public void handleParticipantUpdate(ParticipantUpdate participantUpdateMsg) {
247 LOGGER.debug("ParticipantUpdate message received for participantId {}",
248 participantUpdateMsg.getParticipantId());
250 if (!participantUpdateMsg.appliesTo(participantType, participantId)) {
254 toscaServiceTemplate = participantUpdateMsg.getToscaServiceTemplate();
255 if (toscaServiceTemplate != null) {
256 // This message is to commission the controlloop
257 for (ParticipantDefinition participantDefinition : participantUpdateMsg.getParticipantDefinitionUpdates()) {
258 if (participantDefinition.getParticipantId().equals(participantType)) {
259 clElementDefsOnThisParticipant = participantDefinition.getControlLoopElementDefinitionList();
264 // This message is to decommision the controlloop
265 clElementDefsOnThisParticipant.clear();
267 sendParticipantUpdateAck(participantUpdateMsg.getMessageId());
271 * Method to send ParticipantUpdateAck message to controlloop runtime.
273 public void sendParticipantUpdateAck(UUID messageId) {
274 var participantUpdateAck = new ParticipantUpdateAck();
275 participantUpdateAck.setResponseTo(messageId);
276 participantUpdateAck.setMessage("Participant Update Ack message");
277 participantUpdateAck.setResult(true);
278 participantUpdateAck.setParticipantId(participantId);
279 participantUpdateAck.setParticipantType(participantType);
281 sender.sendParticipantUpdateAck(participantUpdateAck);
285 * Method to send heartbeat to controlloop runtime.
287 public ParticipantStatus makeHeartbeat(boolean responseToParticipantStatusReq) {
288 this.participantStatistics.setState(state);
289 this.participantStatistics.setHealthStatus(healthStatus);
290 this.participantStatistics.setTimeStamp(Instant.now());
292 var heartbeat = new ParticipantStatus();
293 heartbeat.setParticipantId(participantId);
294 heartbeat.setParticipantStatistics(participantStatistics);
295 heartbeat.setParticipantType(participantType);
296 heartbeat.setHealthStatus(healthStatus);
297 heartbeat.setState(state);
298 heartbeat.setControlLoopInfoList(getControlLoopInfoList());
300 if (responseToParticipantStatusReq) {
301 List<ParticipantDefinition> participantDefinitionUpdates = new ArrayList<>();
302 ParticipantDefinition participantDefinition = new ParticipantDefinition();
303 participantDefinition.setParticipantId(participantId);
304 participantDefinition.setControlLoopElementDefinitionList(clElementDefsOnThisParticipant);
305 participantDefinitionUpdates.add(participantDefinition);
306 heartbeat.setParticipantDefinitionUpdates(participantDefinitionUpdates);
312 private List<ControlLoopInfo> getControlLoopInfoList() {
313 List<ControlLoopInfo> controlLoopInfoList = new ArrayList<>();
314 for (Map.Entry<ToscaConceptIdentifier, ControlLoop> entry :
315 controlLoopHandler.getControlLoopMap().entrySet()) {
316 ControlLoopInfo clInfo = new ControlLoopInfo();
317 clInfo.setControlLoopId(entry.getKey());
318 ControlLoopStatistics clStatitistics = new ControlLoopStatistics();
319 clStatitistics.setControlLoopId(entry.getKey());
320 ClElementStatisticsList clElementStatisticsList = new ClElementStatisticsList();
321 clElementStatisticsList.setClElementStatistics(
322 entry.getValue().getControlLoopElementStatisticsList(entry.getValue()));
323 clStatitistics.setClElementStatisticsList(clElementStatisticsList);
324 clInfo.setControlLoopStatistics(clStatitistics);
325 clInfo.setState(entry.getValue().getState());
327 return controlLoopInfoList;