5520359802b3170dd278bd8b66a8b349f712edf5
[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     private final ClientActorConfig config;
40
41     // Hidden to avoid subclassing
42     ClientActorContext(final ActorRef self, final Scheduler scheduler, final ExecutionContext executionContext,
43             final String persistenceId, final ClientIdentifier identifier, final ClientActorConfig config) {
44         super(self, persistenceId);
45         this.identifier = Preconditions.checkNotNull(identifier);
46         this.scheduler = Preconditions.checkNotNull(scheduler);
47         this.executionContext = Preconditions.checkNotNull(executionContext);
48         this.config = Preconditions.checkNotNull(config);
49     }
50
51     @Override
52     @Nonnull
53     public ClientIdentifier getIdentifier() {
54         return identifier;
55     }
56
57     @Nonnull
58     public ClientActorConfig config() {
59         return config;
60     }
61
62     /**
63      * Return the time ticker for this {@link ClientActorContext}. This should be used for in all time-tracking
64      * done within a client actor. Subclasses of {@link ClientActorBehavior} are encouraged to use
65      * {@link com.google.common.base.Stopwatch}.
66      *
67      * @return Client actor time source
68      */
69     @Nonnull
70     public Ticker ticker() {
71         return Ticker.systemTicker();
72     }
73
74     /**
75      * Execute a command in the context of the client actor.
76      *
77      * @param command Block of code which needs to be execute
78      * @param <T> BackendInfo type
79      */
80     public <T extends BackendInfo> void executeInActor(@Nonnull final InternalCommand<T> command) {
81         self().tell(Preconditions.checkNotNull(command), ActorRef.noSender());
82     }
83
84     public <T extends BackendInfo> Cancellable executeInActor(@Nonnull final InternalCommand<T> command,
85             final FiniteDuration delay) {
86         return scheduler.scheduleOnce(Preconditions.checkNotNull(delay), self(), Preconditions.checkNotNull(command),
87             executionContext, ActorRef.noSender());
88     }
89 }

©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.