3128f1eaa42bbce4ec5e18f8b301f59e62026e81
[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.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;
43
44 /**
45  * This class sends messages from participants to CLAMP.
46  */
47 public class MessageSender extends TimerTask implements Closeable {
48     private static final Logger LOGGER = LoggerFactory.getLogger(MessageSender.class);
49
50     private final ParticipantHandler participantHandler;
51     private final ParticipantStatusPublisher publisher;
52     private ScheduledExecutorService timerPool;
53
54     /**
55      * Constructor, set the publisher.
56      *
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
60      */
61     public MessageSender(ParticipantHandler participantHandler, ParticipantStatusPublisher publisher,
62             long interval) {
63         this.participantHandler = participantHandler;
64         this.publisher = publisher;
65
66         // Kick off the timer
67         timerPool = makeTimerPool();
68         timerPool.scheduleAtFixedRate(this, 0, interval, TimeUnit.SECONDS);
69     }
70
71     @Override
72     public void run() {
73         LOGGER.debug("Sent heartbeat to CLAMP");
74
75         ParticipantResponseDetails response = new ParticipantResponseDetails();
76
77         response.setResponseTo(null);
78         response.setResponseStatus(ParticipantResponseStatus.PERIODIC);
79         response.setResponseMessage("Periodic response from participant");
80     }
81
82     @Override
83     public void close() {
84         timerPool.shutdown();
85     }
86
87     /**
88      * Send a response message for this participant.
89      *
90      * @param response the details to include in the response message
91      */
92     public void sendResponse(ParticipantResponseDetails response) {
93         sendResponse(null, response);
94     }
95
96     /**
97      * Dispatch a response message for this participant.
98      *
99      * @param controlLoopId the control loop to which this message is a response
100      * @param response the details to include in the response message
101      */
102     public void sendResponse(ToscaConceptIdentifier controlLoopId, ParticipantResponseDetails response) {
103         ParticipantStatus status = new ParticipantStatus();
104
105         // Participant related fields
106         status.setParticipantType(participantHandler.getParticipantType());
107         status.setParticipantId(participantHandler.getParticipantId());
108         status.setState(participantHandler.getState());
109         status.setHealthStatus(participantHandler.getHealthStatus());
110
111         // Control loop related fields
112         ControlLoops controlLoops = participantHandler.getControlLoopHandler().getControlLoops();
113         status.setControlLoopId(controlLoopId);
114         status.setControlLoops(controlLoops);
115         status.setResponse(response);
116
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);
123
124         for (ControlLoopElementListener clElementListener :
125             participantHandler.getControlLoopHandler().getListeners()) {
126             updateClElementStatistics(controlLoops, clElementListener);
127         }
128
129         status.setControlLoops(controlLoops);
130
131         publisher.send(status);
132     }
133
134     /**
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.
138      *
139      * @param controlLoops the control loops
140      * @param clElementListener control loop element listener
141      */
142     public void updateClElementStatistics(ControlLoops controlLoops, ControlLoopElementListener clElementListener) {
143         for (ControlLoop controlLoop : controlLoops.getControlLoopList()) {
144             for (ControlLoopElement element : controlLoop.getElements().values()) {
145                 try {
146                     clElementListener.handleStatistics(element.getId());
147                 } catch (PfModelException e) {
148                     LOGGER.debug("Getting statistics for Control loop element failed");
149                 }
150             }
151         }
152     }
153
154     /**
155      * Makes a new timer pool.
156      *
157      * @return a new timer pool
158      */
159     protected ScheduledExecutorService makeTimerPool() {
160         return Executors.newScheduledThreadPool(1);
161     }
162 }