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;
11 import com.google.common.base.Preconditions;
12 import com.typesafe.config.Config;
13 import scala.concurrent.duration.Duration;
14 import scala.concurrent.duration.FiniteDuration;
16 import java.util.HashMap;
18 import java.util.concurrent.TimeUnit;
20 public class CommonConfig extends AbstractConfig {
22 protected static final String TAG_ACTOR_SYSTEM_NAME = "actor-system-name";
23 protected static final String TAG_METRIC_CAPTURE_ENABLED = "metric-capture-enabled";
24 protected static final String TAG_MAILBOX_CAPACITY = "mailbox-capacity";
25 protected static final String TAG_MAILBOX = "bounded-mailbox";
26 protected static final String TAG_MAILBOX_PUSH_TIMEOUT = "mailbox-push-timeout-time";
28 //TODO: Ideally these defaults should go to reference.conf
29 // https://bugs.opendaylight.org/show_bug.cgi?id=1709
30 private static final int DEFAULT_MAILBOX_CAPACITY = 1000;
31 private static final int DEFAULT_MAILBOX_PUSH_TIMEOUT = 100;
33 //locally cached values
34 private FiniteDuration cachedMailBoxPushTimeout;
35 private Integer cachedMailBoxCapacity;
36 private Boolean cachedMetricCaptureEnableFlag;
38 public CommonConfig(Config config) {
42 public String getActorSystemName() {
43 return get().getString(TAG_ACTOR_SYSTEM_NAME);
46 public boolean isMetricCaptureEnabled(){
47 if (cachedMetricCaptureEnableFlag != null){
48 return cachedMetricCaptureEnableFlag;
51 cachedMetricCaptureEnableFlag = get().hasPath(TAG_METRIC_CAPTURE_ENABLED)
52 ? get().getBoolean(TAG_METRIC_CAPTURE_ENABLED)
55 return cachedMetricCaptureEnableFlag;
58 public String getMailBoxName() {
62 public Integer getMailBoxCapacity() {
64 if (cachedMailBoxCapacity != null) {
65 return cachedMailBoxCapacity;
68 final String PATH = new StringBuilder(TAG_MAILBOX).append(".").append(TAG_MAILBOX_CAPACITY).toString();
69 cachedMailBoxCapacity = get().hasPath(PATH)
71 : DEFAULT_MAILBOX_CAPACITY;
73 return cachedMailBoxCapacity;
76 public FiniteDuration getMailBoxPushTimeout() {
78 if (cachedMailBoxPushTimeout != null) {
79 return cachedMailBoxPushTimeout;
82 final String PATH = new StringBuilder(TAG_MAILBOX).append(".").append(TAG_MAILBOX_PUSH_TIMEOUT).toString();
84 long timeout = get().hasPath(PATH)
85 ? get().getDuration(PATH, TimeUnit.NANOSECONDS)
86 : DEFAULT_MAILBOX_PUSH_TIMEOUT;
88 cachedMailBoxPushTimeout = new FiniteDuration(timeout, TimeUnit.NANOSECONDS);
89 return cachedMailBoxPushTimeout;
92 public static class Builder<T extends Builder> extends AbstractConfig.Builder<T>{
94 public Builder(String actorSystemName) {
95 super(actorSystemName);
98 configHolder.put(TAG_ACTOR_SYSTEM_NAME, actorSystemName);
100 //config for bounded mailbox
101 configHolder.put(TAG_MAILBOX, new HashMap<String, Object>());
104 public T metricCaptureEnabled(boolean enabled) {
105 configHolder.put(TAG_METRIC_CAPTURE_ENABLED, String.valueOf(enabled));
109 public T mailboxCapacity(int capacity) {
110 Preconditions.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 public T mailboxPushTimeout(String timeout){
118 Duration pushTimeout = Duration.create(timeout);
119 Preconditions.checkArgument(pushTimeout.isFinite(), "invalid value for mailbox push timeout");
121 Map<String, Object> boundedMailbox = (Map<String, Object>) configHolder.get(TAG_MAILBOX);
122 boundedMailbox.put(TAG_MAILBOX_PUSH_TIMEOUT, timeout);
126 public CommonConfig build() {
127 return new CommonConfig(merge());