Fix followerDistributedDataStore tear down
[controller.git] / opendaylight / md-sal / sal-clustering-commons / src / main / java / org / opendaylight / controller / cluster / common / actor / CommonConfig.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.cluster.common.actor;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11
12 import com.typesafe.config.Config;
13 import java.util.HashMap;
14 import java.util.Map;
15 import java.util.concurrent.TimeUnit;
16 import scala.concurrent.duration.FiniteDuration;
17
18 public class CommonConfig extends AbstractConfig {
19
20     protected static final String TAG_ACTOR_SYSTEM_NAME = "actor-system-name";
21     protected static final String TAG_METRIC_CAPTURE_ENABLED = "metric-capture-enabled";
22     protected static final String TAG_MAILBOX_CAPACITY = "mailbox-capacity";
23     protected static final String TAG_MAILBOX = "bounded-mailbox";
24     protected static final String TAG_MAILBOX_PUSH_TIMEOUT = "mailbox-push-timeout-time";
25
26     //TODO: Ideally these defaults should go to reference.conf
27     // https://bugs.opendaylight.org/show_bug.cgi?id=1709
28     private static final int DEFAULT_MAILBOX_CAPACITY = 1000;
29     private static final int DEFAULT_MAILBOX_PUSH_TIMEOUT = 100;
30
31     //locally cached values
32     private FiniteDuration cachedMailBoxPushTimeout;
33     private Integer cachedMailBoxCapacity;
34     private Boolean cachedMetricCaptureEnableFlag;
35
36     public CommonConfig(Config config) {
37         super(config);
38     }
39
40     public String getActorSystemName() {
41         return get().getString(TAG_ACTOR_SYSTEM_NAME);
42     }
43
44     public boolean isMetricCaptureEnabled() {
45         if (cachedMetricCaptureEnableFlag != null) {
46             return cachedMetricCaptureEnableFlag;
47         }
48
49         cachedMetricCaptureEnableFlag = get().hasPath(TAG_METRIC_CAPTURE_ENABLED)
50                 ? get().getBoolean(TAG_METRIC_CAPTURE_ENABLED)
51                 : false;
52
53         return cachedMetricCaptureEnableFlag;
54     }
55
56     public String getMailBoxName() {
57         return TAG_MAILBOX;
58     }
59
60     public Integer getMailBoxCapacity() {
61
62         if (cachedMailBoxCapacity != null) {
63             return cachedMailBoxCapacity;
64         }
65
66         final String PATH = TAG_MAILBOX + "." + TAG_MAILBOX_CAPACITY;
67         cachedMailBoxCapacity = get().hasPath(PATH)
68                 ? get().getInt(PATH)
69                 : DEFAULT_MAILBOX_CAPACITY;
70
71         return cachedMailBoxCapacity;
72     }
73
74     public FiniteDuration getMailBoxPushTimeout() {
75
76         if (cachedMailBoxPushTimeout != null) {
77             return cachedMailBoxPushTimeout;
78         }
79
80         final String PATH = TAG_MAILBOX + "." + TAG_MAILBOX_PUSH_TIMEOUT;
81
82         long timeout = get().hasPath(PATH)
83                 ? get().getDuration(PATH, TimeUnit.NANOSECONDS)
84                 : DEFAULT_MAILBOX_PUSH_TIMEOUT;
85
86         cachedMailBoxPushTimeout = FiniteDuration.create(timeout, TimeUnit.NANOSECONDS);
87         return cachedMailBoxPushTimeout;
88     }
89
90     public static class Builder<T extends Builder<T>> extends AbstractConfig.Builder<T> {
91
92         public Builder(String actorSystemName) {
93             super(actorSystemName);
94
95             //actor system config
96             configHolder.put(TAG_ACTOR_SYSTEM_NAME, actorSystemName);
97
98             //config for bounded mailbox
99             configHolder.put(TAG_MAILBOX, new HashMap<String, Object>());
100         }
101
102         @SuppressWarnings("unchecked")
103         public T metricCaptureEnabled(boolean enabled) {
104             configHolder.put(TAG_METRIC_CAPTURE_ENABLED, String.valueOf(enabled));
105             return (T)this;
106         }
107
108         @SuppressWarnings("unchecked")
109         public T mailboxCapacity(int capacity) {
110             checkArgument(capacity > 0, "mailbox capacity must be >0");
111
112             Map<String, Object> boundedMailbox = (Map<String, Object>) configHolder.get(TAG_MAILBOX);
113             boundedMailbox.put(TAG_MAILBOX_CAPACITY, capacity);
114             return (T)this;
115         }
116
117         @SuppressWarnings("unchecked")
118         public T mailboxPushTimeout(String timeout) {
119             checkArgument(FiniteDuration.create(timeout).isFinite(), "invalid value \"%s\" for mailbox push timeout",
120                 timeout);
121
122             Map<String, Object> boundedMailbox = (Map<String, Object>) configHolder.get(TAG_MAILBOX);
123             boundedMailbox.put(TAG_MAILBOX_PUSH_TIMEOUT, timeout);
124             return (T)this;
125         }
126
127         public CommonConfig build() {
128             return new CommonConfig(merge());
129         }
130     }
131 }