Changes for checkstyle 8.32
[policy/apex-pdp.git] / services / services-engine / src / main / java / org / onap / policy / apex / service / engine / event / ApexPeriodicEventGenerator.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. 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.apex.service.engine.event;
22
23 import java.util.HashMap;
24 import java.util.Map;
25 import java.util.Timer;
26 import java.util.TimerTask;
27 import org.onap.policy.apex.service.engine.runtime.EngineServiceEventInterface;
28 import org.slf4j.ext.XLogger;
29 import org.slf4j.ext.XLoggerFactory;
30
31 /**
32  * This class is used to generate periodic events into an Apex engine service. It is used to trigger
33  * policies that perform housekeeping operations.
34  *
35  * @author eeilfn
36  */
37 public class ApexPeriodicEventGenerator extends TimerTask {
38     private static final XLogger LOGGER = XLoggerFactory.getXLogger(ApexPeriodicEventGenerator.class);
39
40     /** The name of the periodic event. */
41     public static final String PERIODIC_EVENT_NAME = "PERIODIC_EVENT";
42
43     /** The version of the periodic event. */
44     public static final String PERIODIC_EVENT_VERSION = "0.0.1";
45
46     /** The name space of the periodic event. */
47     public static final String PERIODIC_EVENT_NAMESPACE = "org.onap.policy.apex.service.engine.event";
48
49     /** The source of the periodic event. */
50     public static final String PERIODIC_EVENT_SOURCE = "internal";
51
52     /** The target of the periodic event. */
53     public static final String PERIODIC_EVENT_TARGET = PERIODIC_EVENT_SOURCE;
54
55     /**
56      * The field name in the periodic event for the delay between occurrences of the periodic event.
57      */
58     public static final String PERIODIC_DELAY = "PERIODIC_DELAY";
59
60     /**
61      * The field name in the periodic event for the time at which the first periodic event will
62      * occur.
63      */
64     public static final String PERIODIC_FIRST_TIME = "PERIODIC_FIRST_TIME";
65
66     /**
67      * The field name in the periodic event for the time at which the last periodic event will
68      * occur.
69      */
70     public static final String PERIODIC_LAST_TIME = "PERIODIC_LAST_TIME";
71
72     /** The field name in the periodic event for the time at which the event was sent. */
73     public static final String PERIODIC_CURRENT_TIME = "PERIODIC_CURRENT_TIME";
74
75     /**
76      * The field name in the periodic event for the number of occurrences of this event that have
77      * been sent to date, this is a sequence number for the periodic event.
78      */
79     public static final String PERIODIC_EVENT_COUNT = "PERIODIC_EVENT_COUNT";
80
81     // The Java timer used to send periodic events
82     private Timer timer = null;
83
84     // The engine service interface we'll send periodic events to
85     private final EngineServiceEventInterface engineServiceEventInterface;
86
87     // Timing information
88     private long eventGeneratorPeriod = 0;
89     private long firstEventTime = 0;
90     private long lastEventTime = 0;
91     private long eventCount = 0;
92
93     /**
94      * Constructor, save a reference to the event stream handler.
95      *
96      * @param engineServiceEventInterface the engine service event interface on which to send
97      *        periodic events
98      * @param period The period in milliseconds between events
99      */
100     public ApexPeriodicEventGenerator(final EngineServiceEventInterface engineServiceEventInterface,
101             final long period) {
102         // Save the engine service reference and delay
103         this.engineServiceEventInterface = engineServiceEventInterface;
104         this.eventGeneratorPeriod = period;
105
106         timer = new Timer(ApexPeriodicEventGenerator.class.getSimpleName(), true);
107         timer.schedule(this, period, period);
108     }
109
110     /**
111      * Output the metrics for stream loading.
112      */
113     @Override
114     public void run() {
115         final Map<String, Object> periodicEventMap = new HashMap<>();
116
117         // Record the current event time
118         final long currentEventTime = System.currentTimeMillis();
119
120         // Check if this is the first periodic event
121         if (firstEventTime == 0) {
122             firstEventTime = currentEventTime;
123             lastEventTime = currentEventTime;
124         }
125
126         // Increment the event counter
127         eventCount++;
128
129         // Set the fields in the periodic event
130         periodicEventMap.put(PERIODIC_DELAY, eventGeneratorPeriod);
131         periodicEventMap.put(PERIODIC_FIRST_TIME, firstEventTime);
132         periodicEventMap.put(PERIODIC_LAST_TIME, lastEventTime);
133         periodicEventMap.put(PERIODIC_CURRENT_TIME, currentEventTime);
134         periodicEventMap.put(PERIODIC_EVENT_COUNT, eventCount);
135
136         // Send the periodic event
137         try {
138             final ApexEvent periodicEvent = new ApexEvent(PERIODIC_EVENT_NAME, PERIODIC_EVENT_VERSION,
139                     PERIODIC_EVENT_NAMESPACE, PERIODIC_EVENT_SOURCE, PERIODIC_EVENT_TARGET);
140             periodicEvent.putAll(periodicEventMap);
141             engineServiceEventInterface.sendEvent(periodicEvent);
142         } catch (final ApexEventException e) {
143             LOGGER.warn("could not send Apex periodic event " + PERIODIC_EVENT_NAME + ":" + PERIODIC_EVENT_VERSION, e);
144             return;
145         }
146
147         // Save the current time as the last time
148         lastEventTime = currentEventTime;
149     }
150
151     /**
152      * Cancel the timer.
153      *
154      * @return true, if cancel
155      */
156     @Override
157     public boolean cancel() {
158         // Cancel the timer
159         if (timer != null) {
160             timer.cancel();
161         }
162         return true;
163     }
164
165     /**
166      * {@inheritDoc}.
167      */
168     @Override
169     public String toString() {
170         return "ApexPeriodicEventGenerator [period=" + eventGeneratorPeriod + ", firstEventTime=" + firstEventTime
171                 + ", lastEventTime=" + lastEventTime + ", eventCount=" + eventCount + "]";
172     }
173 }