Fix modernization issues
[controller.git] / opendaylight / md-sal / samples / clustering-test-app / provider / src / main / java / org / opendaylight / controller / clustering / it / provider / impl / PublishNotificationsTask.java
1 /*
2  * Copyright (c) 2017 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.clustering.it.provider.impl;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
12
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;
22
23 public class PublishNotificationsTask implements Runnable {
24
25     private static final Logger LOG = LoggerFactory.getLogger(PublishNotificationsTask.class);
26     private static final int SECOND_AS_NANO = 1000000000;
27
28     private final NotificationPublishService notificationPublishService;
29     private final String notificationId;
30     private final long timeToTake;
31     private final long delay;
32
33     private final ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
34
35     private long sequenceNumber = 1;
36     private long startTime;
37     private ScheduledFuture<?> scheduledFuture;
38
39     private Exception lastError = null;
40
41     public PublishNotificationsTask(final NotificationPublishService notificationPublishService,
42                                     final String notificationId, final long secondsToTake, final long maxPerSecond) {
43
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;
50
51         LOG.debug("Delay : {}", delay);
52     }
53
54     @Override
55     public void run() {
56         final long current = System.nanoTime();
57
58         final IdSequence notification =
59                 new IdSequenceBuilder().setId(notificationId).setSequenceNumber(sequenceNumber).build();
60         sequenceNumber++;
61
62         try {
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);
67             lastError = e;
68
69             //stop on error
70             scheduledFuture.cancel(false);
71             executor.shutdown();
72             return;
73         }
74
75         LOG.debug("current {}, starttime: {}, timetotake: {}, current-start = {}",
76                 current, startTime, timeToTake, current - startTime);
77
78         if (current - startTime > timeToTake) {
79             LOG.debug("Sequence number: {}", sequenceNumber);
80             scheduledFuture.cancel(false);
81             executor.shutdown();
82         }
83     }
84
85     public void start() {
86         startTime = System.nanoTime();
87         scheduledFuture = executor.scheduleAtFixedRate(this, 0, delay, TimeUnit.NANOSECONDS);
88     }
89
90     public boolean isFinished() {
91         return scheduledFuture.isCancelled();
92     }
93
94     public long getCurrentNotif() {
95         return sequenceNumber;
96     }
97
98     public Exception getLastError() {
99         return lastError;
100     }
101 }