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