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