Merge "Fix sonars in policy models"
[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  *  Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.models.sim.pdp.comm;
23
24 import java.util.List;
25 import java.util.Timer;
26 import java.util.TimerTask;
27 import org.onap.policy.common.endpoints.event.comm.TopicSink;
28 import org.onap.policy.common.endpoints.event.comm.client.TopicSinkClient;
29 import org.onap.policy.models.pdp.concepts.PdpStatus;
30 import org.onap.policy.models.sim.pdp.handler.PdpMessageHandler;
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 topicSinks the topic sinks
51      * @param interval time interval to send pdp status
52      */
53     public PdpStatusPublisher(final List<TopicSink> topicSinks, final long interval) {
54         this.topicSinkClient = new TopicSinkClient(topicSinks.get(0));
55         this.interval = interval;
56         timer = new Timer(false);
57         timer.scheduleAtFixedRate(this, 0, interval);
58     }
59
60     @Override
61     public void run() {
62         final var pdpStatus = new PdpMessageHandler().createPdpStatusFromContext();
63         topicSinkClient.send(pdpStatus);
64         LOGGER.debug("Sent heartbeat to PAP - {}", pdpStatus);
65     }
66
67     /**
68      * Terminates the current timer.
69      */
70     public void terminate() {
71         timer.cancel();
72         timer.purge();
73     }
74
75     /**
76      * Get the current time interval used by the timer task.
77      *
78      * @return interval the current time interval
79      */
80     public long getInterval() {
81         return interval;
82     }
83
84     /**
85      * Method to send pdp status message to pap on demand.
86      *
87      * @param pdpStatus the pdp status
88      */
89     public void send(final PdpStatus pdpStatus) {
90         topicSinkClient.send(pdpStatus);
91         LOGGER.debug("Sent pdp status message to PAP - {}", pdpStatus);
92     }
93 }