Simplify code using Java 8 features
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / ShardDataTreeNotificationPublisherActor.java
1 /*
2  * Copyright (c) 2016 Brocade Communications 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.cluster.datastore;
9
10 import com.google.common.base.Stopwatch;
11 import java.util.concurrent.TimeUnit;
12 import org.opendaylight.controller.cluster.common.actor.AbstractUntypedActor;
13 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
14
15 /**
16  * Actor used to generate and publish data tree notifications. This is used to offload the potentially
17  * expensive notification generation from the Shard actor.
18  *
19  * @author Thomas Pantelis
20  */
21 public class ShardDataTreeNotificationPublisherActor<T extends ShardDataTreeNotificationPublisher>
22         extends AbstractUntypedActor {
23     private final T publisher;
24     private final Stopwatch timer = Stopwatch.createUnstarted();
25     private final String name;
26     private final String logContext;
27
28     protected ShardDataTreeNotificationPublisherActor(final T publisher, final String name, final String logContext) {
29         this.publisher = publisher;
30         this.name = name;
31         this.logContext = logContext;
32     }
33
34     protected T publisher() {
35         return publisher;
36     }
37
38     protected String logContext() {
39         return logContext;
40     }
41
42     @Override
43     protected void handleReceive(Object message) {
44         if (message instanceof PublishNotifications) {
45             PublishNotifications toPublish = (PublishNotifications)message;
46             timer.start();
47
48             try {
49                 publisher.publishChanges(toPublish.candidate);
50             } finally {
51                 long elapsedTime = timer.elapsed(TimeUnit.MILLISECONDS);
52
53                 if (elapsedTime >= ShardDataTreeNotificationPublisher.PUBLISH_DELAY_THRESHOLD_IN_MS) {
54                     LOG.warn("{}: Generation of change events for {} took longer than expected. Elapsed time: {}",
55                             logContext, name, timer);
56                 } else {
57                     LOG.debug("{}: Elapsed time for generation of change events for {}: {}", logContext, name, timer);
58                 }
59
60                 timer.reset();
61             }
62         }
63     }
64
65     static class PublishNotifications {
66         private final DataTreeCandidate candidate;
67
68         PublishNotifications(DataTreeCandidate candidate) {
69             this.candidate = candidate;
70         }
71     }
72 }