680acd27668c5c0eaa8ee9d08c8a8e7f0e33204b
[policy/clamp.git] /
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.clamp.controlloop.participant.intermediary.comm;
22
23 import java.io.Closeable;
24 import java.util.TimerTask;
25 import java.util.concurrent.Executors;
26 import java.util.concurrent.ScheduledExecutorService;
27 import java.util.concurrent.TimeUnit;
28 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoop;
29 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElement;
30 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoops;
31 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ControlLoopAck;
32 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantDeregister;
33 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantRegister;
34 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantStatus;
35 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantUpdateAck;
36 import org.onap.policy.clamp.controlloop.participant.intermediary.api.ControlLoopElementListener;
37 import org.onap.policy.clamp.controlloop.participant.intermediary.handler.ParticipantHandler;
38 import org.onap.policy.models.base.PfModelException;
39 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42
43 /**
44  * This class sends messages from participants to CLAMP.
45  */
46 public class MessageSender extends TimerTask implements Closeable {
47     private static final Logger LOGGER = LoggerFactory.getLogger(MessageSender.class);
48
49     private final ParticipantHandler participantHandler;
50     private final ParticipantMessagePublisher publisher;
51     private ScheduledExecutorService timerPool;
52
53     /**
54      * Constructor, set the publisher.
55      *
56      * @param participantHandler the participant handler to use for gathering information
57      * @param publisher the publisher to use for sending messages
58      * @param interval time interval to send Participant Status periodic messages
59      */
60     public MessageSender(ParticipantHandler participantHandler, ParticipantMessagePublisher publisher,
61             long interval) {
62         this.participantHandler = participantHandler;
63         this.publisher = publisher;
64
65         // Kick off the timer
66         timerPool = makeTimerPool();
67         timerPool.scheduleAtFixedRate(this, interval, interval, TimeUnit.MILLISECONDS);
68     }
69
70     @Override
71     public void run() {
72         LOGGER.debug("Sent heartbeat to CLAMP");
73         this.sendHeartbeat();
74     }
75
76     @Override
77     public void close() {
78         timerPool.shutdown();
79     }
80
81     /**
82      * Send a response message for this participant.
83      *
84      * @param ackMessage the details to include in the response message
85      */
86     public void sendAckResponse(ControlLoopAck ackMessage) {
87         sendAckResponse(null, ackMessage);
88     }
89
90     /**
91      * Dispatch a response message for this participant.
92      *
93      * @param controlLoopId the control loop to which this message is a response
94      * @param ackMessage the details to include in the response message
95      */
96     public void sendAckResponse(ToscaConceptIdentifier controlLoopId, ControlLoopAck ackMessage) {
97         // Participant related fields
98         ackMessage.setParticipantType(participantHandler.getParticipantType());
99         ackMessage.setParticipantId(participantHandler.getParticipantId());
100         publisher.sendControlLoopAck(ackMessage);
101     }
102
103     /**
104      * Send a ParticipantRegister message for this participant.
105      *
106      * @param message the participantRegister message
107      */
108     public void sendParticipantRegister(ParticipantRegister message) {
109         publisher.sendParticipantRegister(message);
110     }
111
112     /**
113      * Send a ParticipantDeregister message for this participant.
114      *
115      * @param message the participantDeRegister message
116      */
117     public void sendParticipantDeregister(ParticipantDeregister message) {
118         publisher.sendParticipantDeregister(message);
119     }
120
121     /**
122      * Send a ParticipantUpdateAck message for this participant update.
123      *
124      * @param message the participantUpdateAck message
125      */
126     public void sendParticipantUpdateAck(ParticipantUpdateAck message) {
127         publisher.sendParticipantUpdateAck(message);
128     }
129
130     /**
131      * Send a ParticipantStatus message for this participant.
132      *
133      * @param participantStatus the ParticipantStatus message
134      */
135     public void sendParticipantStatus(ParticipantStatus participantStatus) {
136         var controlLoops = participantHandler.getControlLoopHandler().getControlLoops();
137         for (ControlLoopElementListener clElementListener :
138             participantHandler.getControlLoopHandler().getListeners()) {
139             updateClElementStatistics(controlLoops, clElementListener);
140         }
141
142         publisher.sendParticipantStatus(participantStatus);
143     }
144
145     /**
146      * Dispatch a heartbeat for this participant.
147      */
148     public void sendHeartbeat() {
149         publisher.sendHeartbeat(participantHandler.makeHeartbeat(false));
150     }
151
152     /**
153      * Update ControlLoopElement statistics. The control loop elements listening will be
154      * notified to retrieve statistics from respective controlloop elements, and controlloopelements
155      * data on the handler will be updated.
156      *
157      * @param controlLoops the control loops
158      * @param clElementListener control loop element listener
159      */
160     public void updateClElementStatistics(ControlLoops controlLoops, ControlLoopElementListener clElementListener) {
161         for (ControlLoop controlLoop : controlLoops.getControlLoopList()) {
162             for (ControlLoopElement element : controlLoop.getElements().values()) {
163                 try {
164                     clElementListener.handleStatistics(element.getId());
165                 } catch (PfModelException e) {
166                     LOGGER.debug("Getting statistics for Control loop element failed");
167                 }
168             }
169         }
170     }
171
172     /**
173      * Makes a new timer pool.
174      *
175      * @return a new timer pool
176      */
177     protected ScheduledExecutorService makeTimerPool() {
178         return Executors.newScheduledThreadPool(1);
179     }
180 }