BUG-3128: rework sal-remoterpc-connector
[controller.git] / opendaylight / md-sal / sal-remoterpc-connector / src / main / java / org / opendaylight / controller / remote / rpc / RemoteRpcProviderConfig.java
1 /*
2  * Copyright (c) 2014 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.remote.rpc;
9
10 import akka.util.Timeout;
11 import com.typesafe.config.Config;
12 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
13 import java.util.concurrent.TimeUnit;
14 import org.opendaylight.controller.cluster.common.actor.CommonConfig;
15 import scala.concurrent.duration.FiniteDuration;
16
17 public class RemoteRpcProviderConfig extends CommonConfig {
18
19     protected static final String TAG_RPC_BROKER_NAME = "rpc-broker-name";
20     protected static final String TAG_RPC_REGISTRAR_NAME = "rpc-registrar-name";
21     protected static final String TAG_RPC_REGISTRY_NAME = "registry-name";
22     protected static final String TAG_RPC_MGR_NAME = "rpc-manager-name";
23     protected static final String TAG_RPC_BROKER_PATH = "rpc-broker-path";
24     protected static final String TAG_RPC_REGISTRY_PATH = "rpc-registry-path";
25     protected static final String TAG_RPC_MGR_PATH = "rpc-manager-path";
26     protected static final String TAG_ASK_DURATION = "ask-duration";
27     private static final String TAG_GOSSIP_TICK_INTERVAL = "gossip-tick-interval";
28
29     //locally cached values
30     private Timeout cachedAskDuration;
31     private FiniteDuration cachedGossipTickInterval;
32
33     public RemoteRpcProviderConfig(final Config config) {
34         super(config);
35     }
36
37     public String getRpcBrokerName() {
38         return get().getString(TAG_RPC_BROKER_NAME);
39     }
40
41     public String getRpcRegistrarName() {
42         return get().getString(TAG_RPC_REGISTRAR_NAME);
43     }
44
45     public String getRpcRegistryName() {
46         return get().getString(TAG_RPC_REGISTRY_NAME);
47     }
48
49     public String getRpcManagerName() {
50         return get().getString(TAG_RPC_MGR_NAME);
51     }
52
53     public String getRpcBrokerPath() {
54         return get().getString(TAG_RPC_BROKER_PATH);
55     }
56
57     public String getRpcRegistryPath() {
58         return get().getString(TAG_RPC_REGISTRY_PATH);
59
60     }
61
62     public String getRpcManagerPath() {
63         return get().getString(TAG_RPC_MGR_PATH);
64     }
65
66     public Timeout getAskDuration() {
67         if (cachedAskDuration != null) {
68             return cachedAskDuration;
69         }
70
71         cachedAskDuration = new Timeout(new FiniteDuration(
72                 get().getDuration(TAG_ASK_DURATION, TimeUnit.NANOSECONDS), TimeUnit.NANOSECONDS));
73
74         return cachedAskDuration;
75     }
76
77     public FiniteDuration getGossipTickInterval() {
78         if (cachedGossipTickInterval != null) {
79             return cachedGossipTickInterval;
80         }
81
82         cachedGossipTickInterval = new FiniteDuration(
83                 get().getDuration(TAG_GOSSIP_TICK_INTERVAL, TimeUnit.NANOSECONDS), TimeUnit.NANOSECONDS);
84
85         return cachedGossipTickInterval;
86     }
87
88     /**
89      * This is called via blueprint xml as the builder pattern can't be used.
90      */
91     @SuppressFBWarnings(value = "BC_UNCONFIRMED_CAST_OF_RETURN_VALUE",
92             justification = "Findbugs flags this as an unconfirmed cast of return value but the build method clearly "
93                 + "returns RemoteRpcProviderConfig. Perhaps it's confused b/c the build method is overloaded and "
94                 + "and differs in return type from the base class.")
95     public static RemoteRpcProviderConfig newInstance(final String actorSystemName, final boolean metricCaptureEnabled,
96             final int mailboxCapacity) {
97         return new Builder(actorSystemName).metricCaptureEnabled(metricCaptureEnabled)
98                 .mailboxCapacity(mailboxCapacity).build();
99     }
100
101     public static class Builder extends CommonConfig.Builder<Builder> {
102
103         public Builder(final String actorSystemName) {
104             super(actorSystemName);
105
106             //Actor names
107             configHolder.put(TAG_RPC_BROKER_NAME, "broker");
108             configHolder.put(TAG_RPC_REGISTRAR_NAME, "registrar");
109             configHolder.put(TAG_RPC_REGISTRY_NAME, "registry");
110             configHolder.put(TAG_RPC_MGR_NAME, "rpc");
111
112             //Actor paths
113             configHolder.put(TAG_RPC_BROKER_PATH, "/user/rpc/broker");
114             configHolder.put(TAG_RPC_REGISTRY_PATH, "/user/rpc/registry");
115             configHolder.put(TAG_RPC_MGR_PATH, "/user/rpc");
116
117             //durations
118             configHolder.put(TAG_ASK_DURATION, "15s");
119             configHolder.put(TAG_GOSSIP_TICK_INTERVAL, "500ms");
120
121         }
122
123         public Builder gossipTickInterval(final String interval) {
124             configHolder.put(TAG_GOSSIP_TICK_INTERVAL, interval);
125             return this;
126         }
127
128         @Override
129         public RemoteRpcProviderConfig build() {
130             return new RemoteRpcProviderConfig(merge());
131         }
132     }
133 }