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;
 
  28 import org.onap.policy.apex.service.engine.runtime.EngineServiceEventInterface;
 
  29 import org.slf4j.ext.XLogger;
 
  30 import org.slf4j.ext.XLoggerFactory;
 
  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.
 
  38 public class ApexPeriodicEventGenerator extends TimerTask {
 
  39     private static final XLogger LOGGER = XLoggerFactory.getXLogger(ApexPeriodicEventGenerator.class);
 
  41     /** The name of the periodic event. */
 
  42     public static final String PERIODIC_EVENT_NAME = "PERIODIC_EVENT";
 
  44     /** The version of the periodic event. */
 
  45     public static final String PERIODIC_EVENT_VERSION = "0.0.1";
 
  47     /** The name space of the periodic event. */
 
  48     public static final String PERIODIC_EVENT_NAMESPACE = "org.onap.policy.apex.service.engine.event";
 
  50     /** The source of the periodic event. */
 
  51     public static final String PERIODIC_EVENT_SOURCE = "internal";
 
  53     /** The target of the periodic event. */
 
  54     public static final String PERIODIC_EVENT_TARGET = PERIODIC_EVENT_SOURCE;
 
  57      * The field name in the periodic event for the delay between occurrences of the periodic event.
 
  59     public static final String PERIODIC_DELAY = "PERIODIC_DELAY";
 
  62      * The field name in the periodic event for the time at which the first periodic event will
 
  65     public static final String PERIODIC_FIRST_TIME = "PERIODIC_FIRST_TIME";
 
  68      * The field name in the periodic event for the time at which the last periodic event will
 
  71     public static final String PERIODIC_LAST_TIME = "PERIODIC_LAST_TIME";
 
  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";
 
  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.
 
  80     public static final String PERIODIC_EVENT_COUNT = "PERIODIC_EVENT_COUNT";
 
  82     // The Java timer used to send periodic events
 
  83     private Timer timer = null;
 
  85     // The engine service interface we'll send periodic events to
 
  86     private final EngineServiceEventInterface engineServiceEventInterface;
 
  89     private long eventGeneratorPeriod = 0;
 
  90     private long firstEventTime = 0;
 
  91     private long lastEventTime = 0;
 
  92     private long eventCount = 0;
 
  95      * Constructor, save a reference to the event stream handler.
 
  97      * @param engineServiceEventInterface the engine service event interface on which to send
 
  99      * @param period The period in milliseconds between events
 
 101     public ApexPeriodicEventGenerator(final EngineServiceEventInterface engineServiceEventInterface,
 
 103         // Save the engine service reference and delay
 
 104         this.engineServiceEventInterface = engineServiceEventInterface;
 
 105         this.eventGeneratorPeriod = period;
 
 107         timer = new Timer(ApexPeriodicEventGenerator.class.getSimpleName(), true);
 
 108         timer.schedule(this, period, period);
 
 112      * Output the metrics for stream loading.
 
 116         final Map<String, Object> periodicEventMap = new HashMap<>();
 
 118         // Record the current event time
 
 119         final long currentEventTime = System.currentTimeMillis();
 
 121         // Check if this is the first periodic event
 
 122         if (firstEventTime == 0) {
 
 123             firstEventTime = currentEventTime;
 
 124             lastEventTime = currentEventTime;
 
 127         // Increment the event counter
 
 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);
 
 137         // Send the periodic event
 
 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);
 
 148         // Save the current time as the last time
 
 149         lastEventTime = currentEventTime;
 
 155      * @return true, if cancel
 
 158     public boolean cancel() {
 
 170     public String toString() {
 
 171         return "ApexPeriodicEventGenerator [period=" + eventGeneratorPeriod + ", firstEventTime=" + firstEventTime
 
 172                 + ", lastEventTime=" + lastEventTime + ", eventCount=" + eventCount + "]";