Bug 7449: Slice ReadTransactionSuccess response
[controller.git] / opendaylight / md-sal / cds-access-client / src / main / java / org / opendaylight / controller / cluster / access / client / ClientActorContext.java
1 /*
2  * Copyright (c) 2016 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.cluster.access.client;
9
10 import akka.actor.ActorRef;
11 import akka.actor.ActorSystem;
12 import akka.actor.Cancellable;
13 import akka.actor.Scheduler;
14 import com.google.common.annotations.Beta;
15 import com.google.common.base.Preconditions;
16 import com.google.common.base.Ticker;
17 import javax.annotation.Nonnull;
18 import javax.annotation.concurrent.ThreadSafe;
19 import org.opendaylight.controller.cluster.access.concepts.ClientIdentifier;
20 import org.opendaylight.controller.cluster.common.actor.Dispatchers;
21 import org.opendaylight.yangtools.concepts.Identifiable;
22 import scala.concurrent.ExecutionContext;
23 import scala.concurrent.duration.FiniteDuration;
24
25 /**
26  * An actor context associated with this {@link AbstractClientActor}.
27  *
28  * <p>
29  * Time-keeping in a client actor is based on monotonic time. The precision of this time can be expected to be the
30  * same as {@link System#nanoTime()}, but it is not tied to that particular clock. Actor clock is exposed as
31  * a {@link Ticker}, which can be obtained via {@link #ticker()}.
32  *
33  * @author Robert Varga
34  */
35 @Beta
36 @ThreadSafe
37 public class ClientActorContext extends AbstractClientActorContext implements Identifiable<ClientIdentifier> {
38     private final ExecutionContext executionContext;
39     private final ClientIdentifier identifier;
40     private final Scheduler scheduler;
41     private final Dispatchers dispatchers;
42     private final ClientActorConfig config;
43
44     // Hidden to avoid subclassing
45     ClientActorContext(final ActorRef self, final String persistenceId, final ActorSystem system,
46             final ClientIdentifier identifier, final ClientActorConfig config) {
47         super(self, persistenceId);
48         this.identifier = Preconditions.checkNotNull(identifier);
49         this.scheduler = Preconditions.checkNotNull(system).scheduler();
50         this.executionContext = system.dispatcher();
51         this.dispatchers = new Dispatchers(system.dispatchers());
52         this.config = Preconditions.checkNotNull(config);
53     }
54
55     @Override
56     @Nonnull
57     public ClientIdentifier getIdentifier() {
58         return identifier;
59     }
60
61     @Nonnull
62     public ClientActorConfig config() {
63         return config;
64     }
65
66     @Nonnull
67     public Dispatchers dispatchers() {
68         return dispatchers;
69     }
70
71     /**
72      * Return the time ticker for this {@link ClientActorContext}. This should be used for in all time-tracking
73      * done within a client actor. Subclasses of {@link ClientActorBehavior} are encouraged to use
74      * {@link com.google.common.base.Stopwatch}.
75      *
76      * @return Client actor time source
77      */
78     @Nonnull
79     public Ticker ticker() {
80         return Ticker.systemTicker();
81     }
82
83     /**
84      * Execute a command in the context of the client actor.
85      *
86      * @param command Block of code which needs to be execute
87      * @param <T> BackendInfo type
88      */
89     public <T extends BackendInfo> void executeInActor(@Nonnull final InternalCommand<T> command) {
90         self().tell(Preconditions.checkNotNull(command), ActorRef.noSender());
91     }
92
93     public <T extends BackendInfo> Cancellable executeInActor(@Nonnull final InternalCommand<T> command,
94             final FiniteDuration delay) {
95         return scheduler.scheduleOnce(Preconditions.checkNotNull(delay), self(), Preconditions.checkNotNull(command),
96             executionContext, ActorRef.noSender());
97     }
98 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.