20490f81dfbe49cc4b079cb99167616023971153
[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.messages.dmaap.participant.ParticipantResponseDetails;
29 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantResponseStatus;
30 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantStatus;
31 import org.onap.policy.clamp.controlloop.participant.intermediary.handler.ParticipantHandler;
32 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 /**
37  * This class sends messages from participants to CLAMP.
38  */
39 public class MessageSender extends TimerTask implements Closeable {
40     private static final Logger LOGGER = LoggerFactory.getLogger(MessageSender.class);
41
42     private final ParticipantHandler participantHandler;
43     private final ParticipantStatusPublisher publisher;
44     private ScheduledExecutorService timerPool;
45
46     /**
47      * Constructor, set the publisher.
48      *
49      * @param participantHandler the participant handler to use for gathering information
50      * @param publisher the publisher to use for sending messages
51      * @param interval time interval to send Participant Status periodic messages
52      */
53     public MessageSender(ParticipantHandler participantHandler, ParticipantStatusPublisher publisher,
54             long interval) {
55         this.participantHandler = participantHandler;
56         this.publisher = publisher;
57
58         // Kick off the timer
59         timerPool = makeTimerPool();
60         timerPool.scheduleAtFixedRate(this, 0, interval, TimeUnit.SECONDS);
61     }
62
63     @Override
64     public void run() {
65         LOGGER.debug("Sent heartbeat to CLAMP");
66
67         ParticipantResponseDetails response = new ParticipantResponseDetails();
68
69         response.setResponseTo(null);
70         response.setResponseStatus(ParticipantResponseStatus.PERIODIC);
71         response.setResponseMessage("Periodic response from participant");
72     }
73
74     @Override
75     public void close() {
76         timerPool.shutdown();
77     }
78
79     /**
80      * Send a response message for this participant.
81      *
82      * @param response the details to include in the response message
83      */
84     public void sendResponse(ParticipantResponseDetails response) {
85         sendResponse(null, response);
86     }
87
88     /**
89      * Send a response message for this participant.
90      *
91      * @param controlLoopId the control loop to which this message is a response
92      * @param response the details to include in the response message
93      */
94     public void sendResponse(ToscaConceptIdentifier controlLoopId, ParticipantResponseDetails response) {
95         ParticipantStatus status = new ParticipantStatus();
96
97         // Participant related fields
98         status.setParticipantId(participantHandler.getParticipantId());
99         status.setState(participantHandler.getState());
100         status.setHealthStatus(participantHandler.getHealthStatus());
101
102         // Control loop related fields
103         status.setControlLoopId(controlLoopId);
104         status.setControlLoops(participantHandler.getControlLoopHandler().getControlLoops());
105         status.setResponse(response);
106
107         publisher.send(status);
108     }
109
110     /**
111      * Makes a new timer pool.
112      *
113      * @return a new timer pool
114      */
115     protected ScheduledExecutorService makeTimerPool() {
116         return Executors.newScheduledThreadPool(1);
117     }
118 }