c160c1daca6d0c250e92b4bc4fd3a8b6b34fc850
[policy/xacml-pdp.git] / main / src / main / java / org / onap / policy / pdpx / main / comm / XacmlPdpHearbeatPublisher.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
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.pdpx.main.comm;
22
23 import java.util.Timer;
24 import java.util.TimerTask;
25 import lombok.Getter;
26 import org.onap.policy.common.endpoints.event.comm.client.TopicSinkClient;
27 import org.onap.policy.models.pdp.concepts.PdpStatus;
28 import org.onap.policy.pdpx.main.XacmlState;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 public class XacmlPdpHearbeatPublisher extends TimerTask {
33     public static final int DEFAULT_INTERVAL_MS = 60000;
34
35     private static final Logger LOGGER = LoggerFactory.getLogger(XacmlPdpHearbeatPublisher.class);
36
37     private final TopicSinkClient topicSinkClient;
38
39     /**
40      * Tracks the state of this PDP.
41      */
42     private final XacmlState currentState;
43
44     /**
45      * Current timer interval, in milliseconds.
46      */
47     @Getter
48     private long intervalMs = DEFAULT_INTERVAL_MS;
49
50     private Timer timer = null;
51
52
53     /**
54      * Constructor for instantiating XacmlPdpPublisher.
55      *
56      * @param topicSinkClient used to send heart beat message
57      * @param state tracks the state of this PDP
58      */
59     public XacmlPdpHearbeatPublisher(TopicSinkClient topicSinkClient, XacmlState state) {
60         this.topicSinkClient = topicSinkClient;
61         this.currentState = state;
62     }
63
64     @Override
65     public void run() {
66         PdpStatus message = currentState.genHeartbeat();
67         LOGGER.info("Sending Xacml PDP heartbeat to the PAP - {}", message);
68
69         topicSinkClient.send(message);
70     }
71
72     /**
73      * Method to terminate the heart beat.
74      */
75     public synchronized void terminate() {
76         if (timer != null) {
77             timer.cancel();
78             timer.purge();
79             timer = null;
80         }
81     }
82
83     /**
84      * Restarts the timer if the interval has changed. If the timer is not currently
85      * running, then it updates the interval, but does not start the timer.
86      *
87      * @param intervalMs desired interval, or {@code null} to leave it unchanged
88      */
89     public synchronized void restart(Long intervalMs) {
90         if (intervalMs != null && intervalMs > 0 && intervalMs != this.intervalMs) {
91             this.intervalMs = intervalMs;
92
93             if (timer != null) {
94                 terminate();
95                 start();
96             }
97         }
98     }
99
100     /**
101      * Starts the timer.
102      */
103     public synchronized void start() {
104         if (timer == null) {
105             timer = makeTimer();
106             timer.scheduleAtFixedRate(this, 0, this.intervalMs);
107         }
108     }
109
110     // these may be overridden by junit tests
111
112     protected Timer makeTimer() {
113         return new Timer(true);
114     }
115 }