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