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