2 * Copyright (c) 2017 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.clustering.it.provider.impl;
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
13 import java.util.concurrent.Executors;
14 import java.util.concurrent.ScheduledExecutorService;
15 import java.util.concurrent.ScheduledFuture;
16 import java.util.concurrent.TimeUnit;
17 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
18 import org.opendaylight.yang.gen.v1.tag.opendaylight.org._2017.controller.yang.lowlevel.target.rev170215.IdSequence;
19 import org.opendaylight.yang.gen.v1.tag.opendaylight.org._2017.controller.yang.lowlevel.target.rev170215.IdSequenceBuilder;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
23 public class PublishNotificationsTask implements Runnable {
25 private static final Logger LOG = LoggerFactory.getLogger(PublishNotificationsTask.class);
26 private static final int SECOND_AS_NANO = 1000000000;
28 private final NotificationPublishService notificationPublishService;
29 private final String notificationId;
30 private final long timeToTake;
31 private final long delay;
33 private final ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
35 private long sequenceNumber = 1;
36 private long startTime;
37 private ScheduledFuture<?> scheduledFuture;
39 private Exception lastError = null;
41 public PublishNotificationsTask(final NotificationPublishService notificationPublishService,
42 final String notificationId, final long secondsToTake, final long maxPerSecond) {
44 this.notificationPublishService = requireNonNull(notificationPublishService);
45 this.notificationId = requireNonNull(notificationId);
46 checkArgument(secondsToTake > 0);
47 this.timeToTake = secondsToTake * SECOND_AS_NANO;
48 checkArgument(maxPerSecond > 0);
49 this.delay = SECOND_AS_NANO / maxPerSecond;
51 LOG.debug("Delay : {}", delay);
56 final long current = System.nanoTime();
58 final IdSequence notification =
59 new IdSequenceBuilder().setId(notificationId).setSequenceNumber(sequenceNumber).build();
63 LOG.debug("Publishing notification: {}", notification);
64 notificationPublishService.putNotification(notification);
65 } catch (final InterruptedException e) {
66 LOG.warn("Unexpected exception while publishing notification, : {}", notification, e);
70 scheduledFuture.cancel(false);
75 LOG.debug("current {}, starttime: {}, timetotake: {}, current-start = {}",
76 current, startTime, timeToTake, current - startTime);
78 if (current - startTime > timeToTake) {
79 LOG.debug("Sequence number: {}", sequenceNumber);
80 scheduledFuture.cancel(false);
86 startTime = System.nanoTime();
87 scheduledFuture = executor.scheduleAtFixedRate(this, 0, delay, TimeUnit.NANOSECONDS);
90 public boolean isFinished() {
91 return scheduledFuture.isCancelled();
94 public long getCurrentNotif() {
95 return sequenceNumber;
98 public Exception getLastError() {