211fe13165284aa801f74599f7dee5679b4b7120
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 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.apex.services.onappf.comm;
22
23 import java.util.List;
24 import java.util.Timer;
25 import java.util.TimerTask;
26
27 import org.onap.policy.apex.services.onappf.handler.PdpMessageHandler;
28 import org.onap.policy.common.endpoints.event.comm.TopicSink;
29 import org.onap.policy.common.endpoints.event.comm.client.TopicSinkClient;
30 import org.onap.policy.models.pdp.concepts.PdpStatus;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 /**
35  * This class is used to send pdp status messages to pap using TopicSinkClient.
36  *
37  * @author Ajith Sreekumar (ajith.sreekumar@est.tech)
38  */
39 public class PdpStatusPublisher extends TimerTask {
40
41     private static final Logger LOGGER = LoggerFactory.getLogger(PdpStatusPublisher.class);
42
43     private TopicSinkClient topicSinkClient;
44     private Timer timer;
45     private long interval;
46
47     /**
48      * Constructor for instantiating PdpStatusPublisher
49      *
50      * @param pdpStatus
51      * @param topicSinks
52      * @param apexStarterParameterGroup
53      */
54     public PdpStatusPublisher(final List<TopicSink> topicSinks, final long interval) {
55         this.topicSinkClient = new TopicSinkClient(topicSinks.get(0));
56         this.interval = interval;
57         timer = new Timer(false);
58         timer.scheduleAtFixedRate(this, 0, interval);
59     }
60
61     @Override
62     public void run() {
63         final PdpStatus pdpStatus = new PdpMessageHandler().createPdpStatusFromContext();
64         topicSinkClient.send(pdpStatus);
65         LOGGER.debug("Sent heartbeat to PAP - {}", pdpStatus);
66     }
67
68     /**
69      * Terminates the current timer.
70      */
71     public void terminate() {
72         timer.cancel();
73         timer.purge();
74     }
75
76     /**
77      * Get the current time interval used by the timer task.
78      *
79      * @return interval
80      */
81     public long getInterval() {
82         return interval;
83     }
84
85     /**
86      * Method to send pdp status message to pap on demand.
87      */
88     public void send(final PdpStatus pdpStatus) {
89         topicSinkClient.send(pdpStatus);
90         LOGGER.debug("Sent pdp status message to PAP - {}", pdpStatus);
91     }
92 }