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.comm;
23 import java.io.Closeable;
24 import java.time.Instant;
25 import java.util.TimerTask;
26 import java.util.concurrent.Executors;
27 import java.util.concurrent.ScheduledExecutorService;
28 import java.util.concurrent.TimeUnit;
29 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoop;
30 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElement;
31 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoops;
32 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantStatistics;
33 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantResponseDetails;
34 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantResponseStatus;
35 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantStatus;
36 import org.onap.policy.clamp.controlloop.participant.intermediary.api.ControlLoopElementListener;
37 import org.onap.policy.clamp.controlloop.participant.intermediary.comm.ParticipantStatusPublisher;
38 import org.onap.policy.clamp.controlloop.participant.intermediary.handler.ParticipantHandler;
39 import org.onap.policy.models.base.PfModelException;
40 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
45 * This class sends messages from participants to CLAMP.
47 public class MessageSender extends TimerTask implements Closeable {
48 private static final Logger LOGGER = LoggerFactory.getLogger(MessageSender.class);
50 private final ParticipantHandler participantHandler;
51 private final ParticipantStatusPublisher publisher;
52 private ScheduledExecutorService timerPool;
55 * Constructor, set the publisher.
57 * @param participantHandler the participant handler to use for gathering information
58 * @param publisher the publisher to use for sending messages
59 * @param interval time interval to send Participant Status periodic messages
61 public MessageSender(ParticipantHandler participantHandler, ParticipantStatusPublisher publisher,
63 this.participantHandler = participantHandler;
64 this.publisher = publisher;
67 timerPool = makeTimerPool();
68 timerPool.scheduleAtFixedRate(this, 0, interval, TimeUnit.SECONDS);
73 LOGGER.debug("Sent heartbeat to CLAMP");
75 ParticipantResponseDetails response = new ParticipantResponseDetails();
77 response.setResponseTo(null);
78 response.setResponseStatus(ParticipantResponseStatus.PERIODIC);
79 response.setResponseMessage("Periodic response from participant");
88 * Send a response message for this participant.
90 * @param response the details to include in the response message
92 public void sendResponse(ParticipantResponseDetails response) {
93 sendResponse(null, response);
97 * Dispatch a response message for this participant.
99 * @param controlLoopId the control loop to which this message is a response
100 * @param response the details to include in the response message
102 public void sendResponse(ToscaConceptIdentifier controlLoopId, ParticipantResponseDetails response) {
103 ParticipantStatus status = new ParticipantStatus();
105 // Participant related fields
106 status.setParticipantType(participantHandler.getParticipantType());
107 status.setParticipantId(participantHandler.getParticipantId());
108 status.setState(participantHandler.getState());
109 status.setHealthStatus(participantHandler.getHealthStatus());
111 // Control loop related fields
112 ControlLoops controlLoops = participantHandler.getControlLoopHandler().getControlLoops();
113 status.setControlLoopId(controlLoopId);
114 status.setControlLoops(controlLoops);
115 status.setResponse(response);
117 ParticipantStatistics participantStatistics = new ParticipantStatistics();
118 participantStatistics.setTimeStamp(Instant.now());
119 participantStatistics.setParticipantId(participantHandler.getParticipantId());
120 participantStatistics.setHealthStatus(participantHandler.getHealthStatus());
121 participantStatistics.setState(participantHandler.getState());
122 status.setParticipantStatistics(participantStatistics);
124 for (ControlLoopElementListener clElementListener :
125 participantHandler.getControlLoopHandler().getListeners()) {
126 updateClElementStatistics(controlLoops, clElementListener);
129 status.setControlLoops(controlLoops);
131 publisher.send(status);
135 * Update ControlLoopElement statistics. The control loop elements listening will be
136 * notified to retrieve statistics from respective controlloop elements, and controlloopelements
137 * data on the handler will be updated.
139 * @param controlLoops the control loops
140 * @param clElementListener control loop element listener
142 public void updateClElementStatistics(ControlLoops controlLoops, ControlLoopElementListener clElementListener) {
143 for (ControlLoop controlLoop : controlLoops.getControlLoopList()) {
144 for (ControlLoopElement element : controlLoop.getElements().values()) {
146 clElementListener.handleStatistics(element.getId());
147 } catch (PfModelException e) {
148 LOGGER.debug("Getting statistics for Control loop element failed");
155 * Makes a new timer pool.
157 * @return a new timer pool
159 protected ScheduledExecutorService makeTimerPool() {
160 return Executors.newScheduledThreadPool(1);