2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.cluster.common.actor;
10 import static com.google.common.base.Preconditions.checkArgument;
12 import com.typesafe.config.Config;
13 import java.util.HashMap;
15 import java.util.concurrent.TimeUnit;
16 import scala.concurrent.duration.FiniteDuration;
18 public class CommonConfig extends AbstractConfig {
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";
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;
31 //locally cached values
32 private FiniteDuration cachedMailBoxPushTimeout;
33 private Integer cachedMailBoxCapacity;
34 private Boolean cachedMetricCaptureEnableFlag;
36 public CommonConfig(Config config) {
40 public String getActorSystemName() {
41 return get().getString(TAG_ACTOR_SYSTEM_NAME);
44 public boolean isMetricCaptureEnabled() {
45 if (cachedMetricCaptureEnableFlag != null) {
46 return cachedMetricCaptureEnableFlag;
49 cachedMetricCaptureEnableFlag = get().hasPath(TAG_METRIC_CAPTURE_ENABLED)
50 ? get().getBoolean(TAG_METRIC_CAPTURE_ENABLED)
53 return cachedMetricCaptureEnableFlag;
56 public String getMailBoxName() {
60 public Integer getMailBoxCapacity() {
62 if (cachedMailBoxCapacity != null) {
63 return cachedMailBoxCapacity;
66 final String PATH = TAG_MAILBOX + "." + TAG_MAILBOX_CAPACITY;
67 cachedMailBoxCapacity = get().hasPath(PATH)
69 : DEFAULT_MAILBOX_CAPACITY;
71 return cachedMailBoxCapacity;
74 public FiniteDuration getMailBoxPushTimeout() {
76 if (cachedMailBoxPushTimeout != null) {
77 return cachedMailBoxPushTimeout;
80 final String PATH = TAG_MAILBOX + "." + TAG_MAILBOX_PUSH_TIMEOUT;
82 long timeout = get().hasPath(PATH)
83 ? get().getDuration(PATH, TimeUnit.NANOSECONDS)
84 : DEFAULT_MAILBOX_PUSH_TIMEOUT;
86 cachedMailBoxPushTimeout = FiniteDuration.create(timeout, TimeUnit.NANOSECONDS);
87 return cachedMailBoxPushTimeout;
90 public static class Builder<T extends Builder<T>> extends AbstractConfig.Builder<T> {
92 public Builder(String actorSystemName) {
93 super(actorSystemName);
96 configHolder.put(TAG_ACTOR_SYSTEM_NAME, actorSystemName);
98 //config for bounded mailbox
99 configHolder.put(TAG_MAILBOX, new HashMap<String, Object>());
102 @SuppressWarnings("unchecked")
103 public T metricCaptureEnabled(boolean enabled) {
104 configHolder.put(TAG_METRIC_CAPTURE_ENABLED, String.valueOf(enabled));
108 @SuppressWarnings("unchecked")
109 public T mailboxCapacity(int capacity) {
110 checkArgument(capacity > 0, "mailbox capacity must be >0");
112 Map<String, Object> boundedMailbox = (Map<String, Object>) configHolder.get(TAG_MAILBOX);
113 boundedMailbox.put(TAG_MAILBOX_CAPACITY, capacity);
117 @SuppressWarnings("unchecked")
118 public T mailboxPushTimeout(String timeout) {
119 checkArgument(FiniteDuration.create(timeout).isFinite(), "invalid value \"%s\" for mailbox push timeout",
122 Map<String, Object> boundedMailbox = (Map<String, Object>) configHolder.get(TAG_MAILBOX);
123 boundedMailbox.put(TAG_MAILBOX_PUSH_TIMEOUT, timeout);
127 public CommonConfig build() {
128 return new CommonConfig(merge());