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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.service.engine.event;
23 import java.util.HashMap;
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;
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.
37 public class ApexPeriodicEventGenerator extends TimerTask {
38 private static final XLogger LOGGER = XLoggerFactory.getXLogger(ApexPeriodicEventGenerator.class);
40 /** The name of the periodic event. */
41 public static final String PERIODIC_EVENT_NAME = "PERIODIC_EVENT";
43 /** The version of the periodic event. */
44 public static final String PERIODIC_EVENT_VERSION = "0.0.1";
46 /** The name space of the periodic event. */
47 public static final String PERIODIC_EVENT_NAMESPACE = "org.onap.policy.apex.service.engine.event";
49 /** The source of the periodic event. */
50 public static final String PERIODIC_EVENT_SOURCE = "internal";
52 /** The target of the periodic event. */
53 public static final String PERIODIC_EVENT_TARGET = PERIODIC_EVENT_SOURCE;
56 * The field name in the periodic event for the delay between occurrences of the periodic event.
58 public static final String PERIODIC_DELAY = "PERIODIC_DELAY";
61 * The field name in the periodic event for the time at which the first periodic event will
64 public static final String PERIODIC_FIRST_TIME = "PERIODIC_FIRST_TIME";
67 * The field name in the periodic event for the time at which the last periodic event will
70 public static final String PERIODIC_LAST_TIME = "PERIODIC_LAST_TIME";
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";
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.
79 public static final String PERIODIC_EVENT_COUNT = "PERIODIC_EVENT_COUNT";
81 // The Java timer used to send periodic events
82 private Timer timer = null;
84 // The engine service interface we'll send periodic events to
85 private final EngineServiceEventInterface engineServiceEventInterface;
88 private long eventGeneratorPeriod = 0;
89 private long firstEventTime = 0;
90 private long lastEventTime = 0;
91 private long eventCount = 0;
94 * Constructor, save a reference to the event stream handler.
96 * @param engineServiceEventInterface the engine service event interface on which to send
98 * @param period The period in milliseconds between events
100 public ApexPeriodicEventGenerator(final EngineServiceEventInterface engineServiceEventInterface,
102 // Save the engine service reference and delay
103 this.engineServiceEventInterface = engineServiceEventInterface;
104 this.eventGeneratorPeriod = period;
106 timer = new Timer(ApexPeriodicEventGenerator.class.getSimpleName(), true);
107 timer.schedule(this, period, period);
111 * Output the metrics for stream loading.
115 final Map<String, Object> periodicEventMap = new HashMap<>();
117 // Record the current event time
118 final long currentEventTime = System.currentTimeMillis();
120 // Check if this is the first periodic event
121 if (firstEventTime == 0) {
122 firstEventTime = currentEventTime;
123 lastEventTime = currentEventTime;
126 // Increment the event counter
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);
136 // Send the periodic event
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);
147 // Save the current time as the last time
148 lastEventTime = currentEventTime;
154 * @return true, if cancel
157 public boolean cancel() {
169 public String toString() {
170 return "ApexPeriodicEventGenerator [period=" + eventGeneratorPeriod + ", firstEventTime=" + firstEventTime
171 + ", lastEventTime=" + lastEventTime + ", eventCount=" + eventCount + "]";