6926bc30baea3732b53dd359a4afb0bb08196053
[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.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.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 ParticipantStatusPublisher 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, ParticipantStatusPublisher 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, 0, interval, TimeUnit.SECONDS);
68     }
69
70     @Override
71     public void run() {
72         LOGGER.debug("Sent heartbeat to CLAMP");
73
74         var response = new ParticipantResponseDetails();
75
76         response.setResponseTo(null);
77         response.setResponseStatus(ParticipantResponseStatus.PERIODIC);
78         response.setResponseMessage("Periodic response from participant");
79     }
80
81     @Override
82     public void close() {
83         timerPool.shutdown();
84     }
85
86     /**
87      * Send a response message for this participant.
88      *
89      * @param response the details to include in the response message
90      */
91     public void sendResponse(ParticipantResponseDetails response) {
92         sendResponse(null, response);
93     }
94
95     /**
96      * Dispatch a response message for this participant.
97      *
98      * @param controlLoopId the control loop to which this message is a response
99      * @param response the details to include in the response message
100      */
101     public void sendResponse(ToscaConceptIdentifier controlLoopId, ParticipantResponseDetails response) {
102         var status = new ParticipantStatus();
103
104         // Participant related fields
105         status.setParticipantType(participantHandler.getParticipantType());
106         status.setParticipantId(participantHandler.getParticipantId());
107         status.setState(participantHandler.getState());
108         status.setHealthStatus(participantHandler.getHealthStatus());
109
110         // Control loop related fields
111         var controlLoops = participantHandler.getControlLoopHandler().getControlLoops();
112         status.setControlLoopId(controlLoopId);
113         status.setControlLoops(controlLoops);
114         status.setResponse(response);
115
116         var participantStatistics = new ParticipantStatistics();
117         participantStatistics.setTimeStamp(Instant.now());
118         participantStatistics.setParticipantId(participantHandler.getParticipantId());
119         participantStatistics.setHealthStatus(participantHandler.getHealthStatus());
120         participantStatistics.setState(participantHandler.getState());
121         status.setParticipantStatistics(participantStatistics);
122
123         for (ControlLoopElementListener clElementListener :
124             participantHandler.getControlLoopHandler().getListeners()) {
125             updateClElementStatistics(controlLoops, clElementListener);
126         }
127
128         status.setControlLoops(controlLoops);
129
130         publisher.send(status);
131     }
132
133     /**
134      * Update ControlLoopElement statistics. The control loop elements listening will be 
135      * notified to retrieve statistics from respective controlloop elements, and controlloopelements
136      * data on the handler will be updated.
137      *
138      * @param controlLoops the control loops
139      * @param clElementListener control loop element listener
140      */
141     public void updateClElementStatistics(ControlLoops controlLoops, ControlLoopElementListener clElementListener) {
142         for (ControlLoop controlLoop : controlLoops.getControlLoopList()) {
143             for (ControlLoopElement element : controlLoop.getElements().values()) {
144                 try {
145                     clElementListener.handleStatistics(element.getId());
146                 } catch (PfModelException e) {
147                     LOGGER.debug("Getting statistics for Control loop element failed");
148                 }
149             }
150         }
151     }
152
153     /**
154      * Makes a new timer pool.
155      *
156      * @return a new timer pool
157      */
158     protected ScheduledExecutorService makeTimerPool() {
159         return Executors.newScheduledThreadPool(1);
160     }
161 }