877d9aa4c16bf23535416e205ec3edbdde1b364d
[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 import org.opendaylight.lispflowmapping.interfaces.mappingservice.IMappingService;
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 b = FrameworkUtil.getBundle(this.getClass());
46         BundleContext context = null;
47         if (b != null) {
48             context = b.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         }
85         catch (NumberFormatException e) {
86             this.registrationValiditySb = MIN_REGISTRATION_VALIDITY_SB;
87             LOG.debug("Configuration variable 'registerValiditySb' was not set correctly. Registration validity for"
88                     + "South Bound Map Registers is set to default value of 3.3 minutes");
89         }
90     }
91
92     private void initLookupPolicy(BundleContext context) {
93         // set the default value first
94         this.lookupPolicy = IMappingService.LookupPolicy.NB_FIRST;
95
96         String str = null;
97
98         if (context != null) {
99             str = context.getProperty(LISP_LOOKUP_POLICY);
100         }
101
102         if (str == null) {
103             str = System.getProperty(LISP_LOOKUP_POLICY);
104             if (str == null) {
105                 LOG.debug("Configuration variable '{}' is unset. Setting to default value: 'northboundFirst' "
106                         + "(Southbound is only looked up if Northbound is empty) ", LISP_LOOKUP_POLICY);
107                 return;
108             }
109         }
110
111         if (str.trim().equalsIgnoreCase("northboundAndSouthbound")) {
112             this.lookupPolicy = IMappingService.LookupPolicy.NB_AND_SB;
113             LOG.debug("Setting configuration variable '{}' to 'northboundAndSouthbound' (Southbound is always "
114                     + "looked up and can filter Northbound if intersection is not empty)", LISP_LOOKUP_POLICY);
115         } else {
116             LOG.debug("Setting configuration variable '{}' to 'northboundFirst' (Southbound is only looked up "
117                     + "if Northbound is empty)", LISP_LOOKUP_POLICY);
118         }
119     }
120
121     private void initMappingMerge(BundleContext context) {
122         // set the default value first
123         this.mappingMerge = false;
124
125         String str = null;
126
127         if (context != null) {
128             str = context.getProperty(LISP_MAPPING_MERGE);
129         }
130
131         if (str == null) {
132             str = System.getProperty(LISP_MAPPING_MERGE);
133             if (str == null) {
134                 LOG.debug("Configuration variable '{}' is unset. Setting to default value: 'false'",
135                         LISP_MAPPING_MERGE);
136                 return;
137             }
138         }
139
140         if (str.trim().equalsIgnoreCase("true")) {
141             this.mappingMerge = true;
142             LOG.debug("Setting configuration variable '{}' to 'true'", LISP_MAPPING_MERGE);
143         } else {
144             LOG.debug("Setting configuration variable '{}' to 'false'", LISP_MAPPING_MERGE);
145         }
146     }
147
148     private void initMappingOverwrite(BundleContext context) {
149         // set the default value first
150         this.mappingOverwrite = true;
151
152         String str = null;
153
154         if (context != null) {
155             str = context.getProperty(LISP_MAPPING_OVERWRITE);
156         }
157
158         if (str == null) {
159             str = System.getProperty(LISP_MAPPING_OVERWRITE);
160             if (str == null) {
161                 if (this.mappingMerge) {
162                     // If merge is enabled and overwriting configuration is not set, disable it
163                     LOG.debug("Configuration variable '{}' is unset. Since '{}'=true setting to 'false'",
164                             LISP_MAPPING_OVERWRITE, LISP_MAPPING_MERGE);
165                     this.mappingOverwrite = false;
166                 } else {
167                     LOG.debug("Configuration variable '{}' is unset. Setting to default value: 'true'",
168                             LISP_MAPPING_OVERWRITE);
169                 }
170                 return;
171             }
172         }
173
174         if (str.trim().equalsIgnoreCase("false")) {
175             this.mappingOverwrite = false;
176             LOG.debug("Setting configuration variable '{}' to 'false'", LISP_MAPPING_OVERWRITE);
177         } else {
178             if (this.mappingMerge) {
179                 LOG.warn("Can't set configuration variable '{}' to 'true' since '{}' is enabled",
180                         LISP_MAPPING_OVERWRITE, LISP_MAPPING_MERGE);
181                 LOG.warn("If you really need to enable overwriting, please disable merging.");
182                 LOG.debug("Setting configuration variable '{}' to 'false'", LISP_MAPPING_OVERWRITE);
183                 this.mappingOverwrite = false;
184             } else {
185                 LOG.debug("Setting configuration variable '{}' to 'true'", LISP_MAPPING_OVERWRITE);
186             }
187         }
188     }
189
190     private void initSmr(BundleContext context) {
191         // set the default value first
192         this.smr = true;
193
194         String str = null;
195
196         if (context != null) {
197             str = context.getProperty(LISP_SMR);
198         }
199
200         if (str == null) {
201             str = System.getProperty(LISP_SMR);
202             if (str == null) {
203                 LOG.debug("Configuration variable '{}' is unset. Setting to default value: 'true'", LISP_SMR);
204                 return;
205             }
206         }
207
208         if (str.trim().equalsIgnoreCase("false")) {
209             this.smr = false;
210             LOG.debug("Setting configuration variable '{}' to 'false'", LISP_SMR);
211         } else {
212             LOG.debug("Setting configuration variable '{}' to 'true'", LISP_SMR);
213         }
214     }
215
216     private void initElpPolicy(BundleContext context) {
217         // set the default value first
218         this.elpPolicy = "default";
219
220         String str = null;
221
222         if (context != null) {
223             str = context.getProperty(LISP_ELP_POLICY);
224         }
225
226         if (str == null) {
227             str = System.getProperty(LISP_ELP_POLICY);
228             if (str == null) {
229                 LOG.debug("Configuration variable '{}' is unset. Setting to default value: 'default' (ELP only)",
230                         LISP_ELP_POLICY);
231                 return;
232             }
233         }
234
235         if (str.trim().equalsIgnoreCase("both")) {
236             this.elpPolicy = "both";
237             LOG.debug("Setting configuration variable '{}' to 'both' (keep ELP, add next hop)", LISP_ELP_POLICY);
238         } else if (str.trim().equalsIgnoreCase("replace")) {
239             this.elpPolicy = "replace";
240             LOG.debug("Setting configuration variable '{}' to 'replace' (next hop only)", LISP_ELP_POLICY);
241         } else {
242             LOG.debug("Setting configuration variable '{}' to 'default' (ELP only)", LISP_ELP_POLICY);
243         }
244     }
245
246     public boolean mappingMergeIsSet() {
247         return mappingMerge;
248     }
249
250     public boolean mappingOverwriteIsSet() {
251         return mappingOverwrite;
252     }
253
254     public void setMappingOverwrite(boolean mappingOverwrite) {
255         LOG.debug("Setting configuration variable '{}' to '{}'", LISP_MAPPING_OVERWRITE, mappingOverwrite);
256         this.mappingOverwrite = mappingOverwrite;
257         LOG.debug("Setting configuration variable '{}' to '{}'", LISP_MAPPING_MERGE, !(mappingOverwrite));
258         this.mappingMerge = !(mappingOverwrite);
259     }
260
261     public boolean smrIsSet() {
262         return smr;
263     }
264
265     public void setSmr(boolean smr) {
266         LOG.debug("Setting configuration variable '{}' to '{}'", LISP_SMR, smr);
267         this.smr = smr;
268     }
269
270     public String getElpPolicy() {
271         return elpPolicy;
272     }
273
274     public IMappingService.LookupPolicy getLookupPolicy() {
275         return lookupPolicy;
276     }
277
278     public long getRegistrationValiditySb() {
279         return registrationValiditySb;
280     }
281
282     public void setLookupPolicy(IMappingService.LookupPolicy lookupPolicy) {
283             this.lookupPolicy = lookupPolicy;
284     }
285
286     public static ConfigIni getInstance() {
287         return INSTANCE;
288     }
289 }