BUG-5280: introduce cookie in LocalHistoryIdentifier
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / actors / client / ClientActorContext.java
index ca393608b75b303cd4073e1886acb4a808e3c821..3aa1a5200b077f91fda048ba2704648de98c8f88 100644 (file)
@@ -10,25 +10,52 @@ package org.opendaylight.controller.cluster.datastore.actors.client;
 import akka.actor.ActorRef;
 import com.google.common.annotations.Beta;
 import com.google.common.base.Preconditions;
+import com.google.common.base.Ticker;
+import javax.annotation.Nonnull;
 import org.opendaylight.controller.cluster.access.concepts.ClientIdentifier;
 import org.opendaylight.yangtools.concepts.Identifiable;
 
 /**
- * An actor context associated with this {@link AbstractClientActor}
+ * An actor context associated with this {@link AbstractClientActor}.
+ *
+ * Time-keeping in a client actor is based on monotonic time. The precision of this time can be expected to be the
+ * same as {@link System#nanoTime()}, but it is not tied to that particular clock. Actor clock is exposed as
+ * a {@link Ticker}, which can be obtained via {@link #ticker()}.
  *
  * @author Robert Varga
  */
 @Beta
-public final class ClientActorContext extends AbstractClientActorContext implements Identifiable<ClientIdentifier> {
+public class ClientActorContext extends AbstractClientActorContext implements Identifiable<ClientIdentifier> {
     private final ClientIdentifier identifier;
 
+    // Hidden to avoid subclassing
     ClientActorContext(final ActorRef self, final String persistenceId, final ClientIdentifier identifier) {
         super(self, persistenceId);
         this.identifier = Preconditions.checkNotNull(identifier);
     }
 
     @Override
-    public ClientIdentifier getIdentifier() {
+    public @Nonnull ClientIdentifier getIdentifier() {
         return identifier;
     }
+
+    /**
+     * Return the time ticker for this {@link ClientActorContext}. This should be used for in all time-tracking
+     * done within a client actor. Subclasses of {@link ClientActorBehavior} are encouraged to use
+     * {@link com.google.common.base.Stopwatch}.
+     *
+     * @return Client actor time source
+     */
+    public @Nonnull Ticker ticker() {
+        return Ticker.systemTicker();
+    }
+
+    /**
+     * Execute a command in the context of the client actor.
+     *
+     * @param command Block of code which needs to be execute
+     */
+    public void executeInActor(final @Nonnull InternalCommand command) {
+        self().tell(Preconditions.checkNotNull(command), ActorRef.noSender());
+    }
 }