Metrics and Configuration
[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
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;
15
16 import java.util.HashMap;
17 import java.util.Map;
18 import java.util.concurrent.TimeUnit;
19
20 public class CommonConfig extends AbstractConfig {
21
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";
27
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;
32
33     //locally cached values
34     private FiniteDuration cachedMailBoxPushTimeout;
35     private Integer cachedMailBoxCapacity;
36     private Boolean cachedMetricCaptureEnableFlag;
37
38     public CommonConfig(Config config) {
39         super(config);
40     }
41
42     public String getActorSystemName() {
43         return get().getString(TAG_ACTOR_SYSTEM_NAME);
44     }
45
46     public boolean isMetricCaptureEnabled(){
47         if (cachedMetricCaptureEnableFlag != null){
48             return cachedMetricCaptureEnableFlag;
49         }
50
51         cachedMetricCaptureEnableFlag = get().hasPath(TAG_METRIC_CAPTURE_ENABLED)
52                 ? get().getBoolean(TAG_METRIC_CAPTURE_ENABLED)
53                 : false;
54
55         return cachedMetricCaptureEnableFlag;
56     }
57
58     public String getMailBoxName() {
59         return TAG_MAILBOX;
60     }
61
62     public Integer getMailBoxCapacity() {
63
64         if (cachedMailBoxCapacity != null) {
65             return cachedMailBoxCapacity;
66         }
67
68         final String PATH = new StringBuilder(TAG_MAILBOX).append(".").append(TAG_MAILBOX_CAPACITY).toString();
69         cachedMailBoxCapacity = get().hasPath(PATH)
70                 ? get().getInt(PATH)
71                 : DEFAULT_MAILBOX_CAPACITY;
72
73         return cachedMailBoxCapacity;
74     }
75
76     public FiniteDuration getMailBoxPushTimeout() {
77
78         if (cachedMailBoxPushTimeout != null) {
79             return cachedMailBoxPushTimeout;
80         }
81
82         final String PATH = new StringBuilder(TAG_MAILBOX).append(".").append(TAG_MAILBOX_PUSH_TIMEOUT).toString();
83
84         long timeout = get().hasPath(PATH)
85                 ? get().getDuration(PATH, TimeUnit.NANOSECONDS)
86                 : DEFAULT_MAILBOX_PUSH_TIMEOUT;
87
88         cachedMailBoxPushTimeout = new FiniteDuration(timeout, TimeUnit.NANOSECONDS);
89         return cachedMailBoxPushTimeout;
90     }
91
92     public static class Builder<T extends Builder> extends AbstractConfig.Builder<T>{
93
94         public Builder(String actorSystemName) {
95             super(actorSystemName);
96
97             //actor system config
98             configHolder.put(TAG_ACTOR_SYSTEM_NAME, actorSystemName);
99
100             //config for bounded mailbox
101             configHolder.put(TAG_MAILBOX, new HashMap<String, Object>());
102         }
103
104         public T metricCaptureEnabled(boolean enabled) {
105             configHolder.put(TAG_METRIC_CAPTURE_ENABLED, String.valueOf(enabled));
106             return (T)this;
107         }
108
109         public T mailboxCapacity(int capacity) {
110             Preconditions.checkArgument(capacity > 0, "mailbox capacity must be >0");
111
112             Map<String, Object> boundedMailbox = (Map) configHolder.get(TAG_MAILBOX);
113             boundedMailbox.put(TAG_MAILBOX_CAPACITY, capacity);
114             return (T)this;
115         }
116
117         public T mailboxPushTimeout(String timeout){
118             Duration pushTimeout = Duration.create(timeout);
119             Preconditions.checkArgument(pushTimeout.isFinite(), "invalid value for mailbox push timeout");
120
121             Map<String, Object> boundedMailbox = (Map) configHolder.get(TAG_MAILBOX);
122             boundedMailbox.put(TAG_MAILBOX_PUSH_TIMEOUT, timeout);
123             return (T)this;
124         }
125
126         public CommonConfig build() {
127             return new CommonConfig(merge());
128         }
129     }
130 }