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