Bug 4822: Simplify access to configuration variables
[lispflowmapping.git] / mappingservice / implementation / src / main / java / org / opendaylight / lispflowmapping / implementation / config / ConfigIni.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.lispflowmapping.implementation.config;
9
10 import org.osgi.framework.Bundle;
11 import org.osgi.framework.BundleContext;
12 import org.osgi.framework.FrameworkUtil;
13 import org.slf4j.Logger;
14 import org.slf4j.LoggerFactory;
15
16 public final class ConfigIni {
17     protected static final Logger LOG = LoggerFactory.getLogger(ConfigIni.class);
18     private boolean mappingOverwrite;
19     private boolean smr;
20     private String elpPolicy;
21
22     private static final String LISP_MAPPING_OVERWRITE = "lisp.mappingOverwrite";
23     private static final String LISP_SMR = "lisp.smr";
24     private static final String LISP_ELP_POLICY = "lisp.elpPolicy";
25
26     private static final ConfigIni INSTANCE = new ConfigIni();
27
28     private ConfigIni() {
29         Bundle b = FrameworkUtil.getBundle(this.getClass());
30         BundleContext context = null;
31         if (b != null) {
32             context = b.getBundleContext();
33         }
34
35         initMappingOverwrite(context);
36         initSmr(context);
37         initElpPolicy(context);
38     }
39
40     private void initMappingOverwrite(BundleContext context) {
41         // set the default value first
42         this.mappingOverwrite = true;
43
44         String str = null;
45
46         if (context != null) {
47             str = context.getProperty(LISP_MAPPING_OVERWRITE);
48         }
49
50         if (str == null) {
51             str = System.getProperty(LISP_MAPPING_OVERWRITE);
52             if (str == null) {
53                 LOG.debug("Configuration variable '{}' is unset. Setting to default value: 'true'", LISP_MAPPING_OVERWRITE);
54                 return;
55             }
56         }
57
58         if (str.trim().equalsIgnoreCase("false")) {
59             this.mappingOverwrite = false;
60             LOG.debug("Setting configuration variable '{}' to 'false'", LISP_MAPPING_OVERWRITE);
61         } else {
62             LOG.debug("Setting configuration variable '{}' to 'true'", LISP_MAPPING_OVERWRITE);
63         }
64     }
65
66     private void initSmr(BundleContext context) {
67         // set the default value first
68         this.smr = true;
69
70         String str = null;
71
72         if (context != null) {
73             str = context.getProperty(LISP_SMR);
74         }
75
76         if (str == null) {
77             str = System.getProperty(LISP_SMR);
78             if (str == null) {
79                 LOG.debug("Configuration variable '{}' is unset. Setting to default value: 'true'", LISP_SMR);
80                 return;
81             }
82         }
83
84         if (str.trim().equalsIgnoreCase("false")) {
85             this.smr = false;
86             LOG.debug("Setting configuration variable '{}' to 'false'", LISP_SMR);
87         } else {
88             LOG.debug("Setting configuration variable '{}' to 'true'", LISP_SMR);
89         }
90     }
91
92     private void initElpPolicy(BundleContext context) {
93         // set the default value first
94         this.elpPolicy = "default";
95
96         String str = null;
97
98         if (context != null) {
99             str = context.getProperty(LISP_ELP_POLICY);
100         }
101
102         if (str == null) {
103             str = System.getProperty(LISP_ELP_POLICY);
104             if (str == null) {
105                 LOG.debug("Configuration variable '{}' is unset. Setting to default value: 'default' (ELP only)",
106                         LISP_ELP_POLICY);
107                 return;
108             }
109         }
110
111         if (str.trim().equalsIgnoreCase("both")) {
112             this.elpPolicy = "both";
113             LOG.debug("Setting configuration variable '{}' to 'both' (keep ELP, add next hop)", LISP_ELP_POLICY);
114         } else if (str.trim().equalsIgnoreCase("replace")) {
115             this.elpPolicy = "replace";
116             LOG.debug("Setting configuration variable '{}' to 'replace' (next hop only)", LISP_ELP_POLICY);
117         } else {
118             LOG.debug("Setting configuration variable '{}' to 'default' (ELP only)", LISP_ELP_POLICY);
119         }
120     }
121
122     public boolean mappingOverwriteIsSet() {
123         return mappingOverwrite;
124     }
125
126     public boolean smrIsSet() {
127         return smr;
128     }
129
130     public String getElpPolicy() {
131         return elpPolicy;
132     }
133
134     public static ConfigIni getInstance() {
135         return INSTANCE;
136     }
137 }