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