Checkstyle: fix issues and enforce on implementation
[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.opendaylight.lispflowmapping.interfaces.mappingservice.IMappingService;
11 import org.osgi.framework.Bundle;
12 import org.osgi.framework.BundleContext;
13 import org.osgi.framework.FrameworkUtil;
14 import org.slf4j.Logger;
15 import org.slf4j.LoggerFactory;
16
17 public final class ConfigIni {
18
19     protected static final Logger LOG = LoggerFactory.getLogger(ConfigIni.class);
20     private boolean mappingMerge;
21     private boolean mappingOverwrite;
22     private boolean smr;
23     private String elpPolicy;
24     private IMappingService.LookupPolicy lookupPolicy;
25     private long registrationValiditySb;
26
27     // 'lisp.mappingMerge' and 'lisp.mappingOverWrite' are not independent, and they can't be both 'true'
28     // when there is a conflict, the setting in 'lisp.mappingMerge' takes precendence
29     // 'lisp.mappingOverwrite' defines the database behavior while 'lisp.mappingMerge' affects the result
30     // returned in Map-Replies
31
32     private static final String LISP_LOOKUP_POLICY = "lisp.lookupPolicy";
33     private static final String LISP_MAPPING_MERGE = "lisp.mappingMerge";
34     private static final String LISP_MAPPING_OVERWRITE = "lisp.mappingOverwrite";
35     private static final String LISP_SMR = "lisp.smr";
36     private static final String LISP_ELP_POLICY = "lisp.elpPolicy";
37     private static final String LISP_REGISTER_VALIDITY_SB = "lisp.registerValiditySb";
38
39     // SB Map Register validity period in milliseconds. Default is 3.3 minutes.
40     public static final long MIN_REGISTRATION_VALIDITY_SB = 200000L;
41
42     private static final ConfigIni INSTANCE = new ConfigIni();
43
44     private ConfigIni() {
45         Bundle bundle = FrameworkUtil.getBundle(this.getClass());
46         BundleContext context = null;
47         if (bundle != null) {
48             context = bundle.getBundleContext();
49         }
50
51         // Initialize mappingMerge first, since mappingOverwrite depends on it
52         initMappingMerge(context);
53         initMappingOverwrite(context);
54         initSmr(context);
55         initElpPolicy(context);
56         initLookupPolicy(context);
57         initRegisterValiditySb(context);
58     }
59
60     private void initRegisterValiditySb(BundleContext context) {
61         // set the default value first
62         this.registrationValiditySb = MIN_REGISTRATION_VALIDITY_SB;
63
64         String str = null;
65
66         if (context != null) {
67             str = context.getProperty(LISP_REGISTER_VALIDITY_SB);
68         }
69
70         if (str == null) {
71             str = System.getProperty(LISP_REGISTER_VALIDITY_SB);
72             if (str == null) {
73                 LOG.debug("Configuration variable '{}' is unset. Setting to default value: '3.33 minutes' ",
74                         LISP_REGISTER_VALIDITY_SB);
75                 return;
76             }
77         }
78
79         try {
80             final long regValidity = Long.parseLong(str.trim());
81             if (regValidity >= MIN_REGISTRATION_VALIDITY_SB) {
82                 this.registrationValiditySb = regValidity;
83             }
84         } catch (NumberFormatException e) {
85             this.registrationValiditySb = MIN_REGISTRATION_VALIDITY_SB;
86             LOG.debug("Configuration variable 'registerValiditySb' was not set correctly. Registration validity for"
87                     + "South Bound Map Registers is set to default value of 3.3 minutes");
88         }
89     }
90
91     private void initLookupPolicy(BundleContext context) {
92         // set the default value first
93         this.lookupPolicy = IMappingService.LookupPolicy.NB_FIRST;
94
95         String str = null;
96
97         if (context != null) {
98             str = context.getProperty(LISP_LOOKUP_POLICY);
99         }
100
101         if (str == null) {
102             str = System.getProperty(LISP_LOOKUP_POLICY);
103             if (str == null) {
104                 LOG.debug("Configuration variable '{}' is unset. Setting to default value: 'northboundFirst' "
105                         + "(Southbound is only looked up if Northbound is empty) ", LISP_LOOKUP_POLICY);
106                 return;
107             }
108         }
109
110         if (str.trim().equalsIgnoreCase("northboundAndSouthbound")) {
111             this.lookupPolicy = IMappingService.LookupPolicy.NB_AND_SB;
112             LOG.debug("Setting configuration variable '{}' to 'northboundAndSouthbound' (Southbound is always "
113                     + "looked up and can filter Northbound if intersection is not empty)", LISP_LOOKUP_POLICY);
114         } else {
115             LOG.debug("Setting configuration variable '{}' to 'northboundFirst' (Southbound is only looked up "
116                     + "if Northbound is empty)", LISP_LOOKUP_POLICY);
117         }
118     }
119
120     private void initMappingMerge(BundleContext context) {
121         // set the default value first
122         this.mappingMerge = false;
123
124         String str = null;
125
126         if (context != null) {
127             str = context.getProperty(LISP_MAPPING_MERGE);
128         }
129
130         if (str == null) {
131             str = System.getProperty(LISP_MAPPING_MERGE);
132             if (str == null) {
133                 LOG.debug("Configuration variable '{}' is unset. Setting to default value: 'false'",
134                         LISP_MAPPING_MERGE);
135                 return;
136             }
137         }
138
139         if (str.trim().equalsIgnoreCase("true")) {
140             this.mappingMerge = true;
141             LOG.debug("Setting configuration variable '{}' to 'true'", LISP_MAPPING_MERGE);
142         } else {
143             LOG.debug("Setting configuration variable '{}' to 'false'", LISP_MAPPING_MERGE);
144         }
145     }
146
147     private void initMappingOverwrite(BundleContext context) {
148         // set the default value first
149         this.mappingOverwrite = true;
150
151         String str = null;
152
153         if (context != null) {
154             str = context.getProperty(LISP_MAPPING_OVERWRITE);
155         }
156
157         if (str == null) {
158             str = System.getProperty(LISP_MAPPING_OVERWRITE);
159             if (str == null) {
160                 if (this.mappingMerge) {
161                     // If merge is enabled and overwriting configuration is not set, disable it
162                     LOG.debug("Configuration variable '{}' is unset. Since '{}'=true setting to 'false'",
163                             LISP_MAPPING_OVERWRITE, LISP_MAPPING_MERGE);
164                     this.mappingOverwrite = false;
165                 } else {
166                     LOG.debug("Configuration variable '{}' is unset. Setting to default value: 'true'",
167                             LISP_MAPPING_OVERWRITE);
168                 }
169                 return;
170             }
171         }
172
173         if (str.trim().equalsIgnoreCase("false")) {
174             this.mappingOverwrite = false;
175             LOG.debug("Setting configuration variable '{}' to 'false'", LISP_MAPPING_OVERWRITE);
176         } else {
177             if (this.mappingMerge) {
178                 LOG.warn("Can't set configuration variable '{}' to 'true' since '{}' is enabled",
179                         LISP_MAPPING_OVERWRITE, LISP_MAPPING_MERGE);
180                 LOG.warn("If you really need to enable overwriting, please disable merging.");
181                 LOG.debug("Setting configuration variable '{}' to 'false'", LISP_MAPPING_OVERWRITE);
182                 this.mappingOverwrite = false;
183             } else {
184                 LOG.debug("Setting configuration variable '{}' to 'true'", LISP_MAPPING_OVERWRITE);
185             }
186         }
187     }
188
189     private void initSmr(BundleContext context) {
190         // set the default value first
191         this.smr = true;
192
193         String str = null;
194
195         if (context != null) {
196             str = context.getProperty(LISP_SMR);
197         }
198
199         if (str == null) {
200             str = System.getProperty(LISP_SMR);
201             if (str == null) {
202                 LOG.debug("Configuration variable '{}' is unset. Setting to default value: 'true'", LISP_SMR);
203                 return;
204             }
205         }
206
207         if (str.trim().equalsIgnoreCase("false")) {
208             this.smr = false;
209             LOG.debug("Setting configuration variable '{}' to 'false'", LISP_SMR);
210         } else {
211             LOG.debug("Setting configuration variable '{}' to 'true'", LISP_SMR);
212         }
213     }
214
215     private void initElpPolicy(BundleContext context) {
216         // set the default value first
217         this.elpPolicy = "default";
218
219         String str = null;
220
221         if (context != null) {
222             str = context.getProperty(LISP_ELP_POLICY);
223         }
224
225         if (str == null) {
226             str = System.getProperty(LISP_ELP_POLICY);
227             if (str == null) {
228                 LOG.debug("Configuration variable '{}' is unset. Setting to default value: 'default' (ELP only)",
229                         LISP_ELP_POLICY);
230                 return;
231             }
232         }
233
234         if (str.trim().equalsIgnoreCase("both")) {
235             this.elpPolicy = "both";
236             LOG.debug("Setting configuration variable '{}' to 'both' (keep ELP, add next hop)", LISP_ELP_POLICY);
237         } else if (str.trim().equalsIgnoreCase("replace")) {
238             this.elpPolicy = "replace";
239             LOG.debug("Setting configuration variable '{}' to 'replace' (next hop only)", LISP_ELP_POLICY);
240         } else {
241             LOG.debug("Setting configuration variable '{}' to 'default' (ELP only)", LISP_ELP_POLICY);
242         }
243     }
244
245     public boolean mappingMergeIsSet() {
246         return mappingMerge;
247     }
248
249     public boolean mappingOverwriteIsSet() {
250         return mappingOverwrite;
251     }
252
253     public void setMappingOverwrite(boolean mappingOverwrite) {
254         LOG.debug("Setting configuration variable '{}' to '{}'", LISP_MAPPING_OVERWRITE, mappingOverwrite);
255         this.mappingOverwrite = mappingOverwrite;
256         LOG.debug("Setting configuration variable '{}' to '{}'", LISP_MAPPING_MERGE, !(mappingOverwrite));
257         this.mappingMerge = !(mappingOverwrite);
258     }
259
260     public boolean smrIsSet() {
261         return smr;
262     }
263
264     public void setSmr(boolean smr) {
265         LOG.debug("Setting configuration variable '{}' to '{}'", LISP_SMR, smr);
266         this.smr = smr;
267     }
268
269     public String getElpPolicy() {
270         return elpPolicy;
271     }
272
273     public IMappingService.LookupPolicy getLookupPolicy() {
274         return lookupPolicy;
275     }
276
277     public long getRegistrationValiditySb() {
278         return registrationValiditySb;
279     }
280
281     public void setLookupPolicy(IMappingService.LookupPolicy lookupPolicy) {
282         this.lookupPolicy = lookupPolicy;
283     }
284
285     public static ConfigIni getInstance() {
286         return INSTANCE;
287     }
288 }