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.lispflowmapping.implementation.config;
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;
17 public final class ConfigIni {
19 protected static final Logger LOG = LoggerFactory.getLogger(ConfigIni.class);
20 private boolean mappingMerge;
21 private boolean mappingOverwrite;
23 private String elpPolicy;
24 private IMappingService.LookupPolicy lookupPolicy;
25 private long registrationValiditySb;
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
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";
39 // SB Map Register validity period in milliseconds. Default is 3.3 minutes.
40 public static final long MIN_REGISTRATION_VALIDITY_SB = 200000L;
42 private static final ConfigIni INSTANCE = new ConfigIni();
45 Bundle b = FrameworkUtil.getBundle(this.getClass());
46 BundleContext context = null;
48 context = b.getBundleContext();
51 // Initialize mappingMerge first, since mappingOverwrite depends on it
52 initMappingMerge(context);
53 initMappingOverwrite(context);
55 initElpPolicy(context);
56 initLookupPolicy(context);
57 initRegisterValiditySb(context);
60 private void initRegisterValiditySb(BundleContext context) {
61 // set the default value first
62 this.registrationValiditySb = MIN_REGISTRATION_VALIDITY_SB;
66 if (context != null) {
67 str = context.getProperty(LISP_REGISTER_VALIDITY_SB);
71 str = System.getProperty(LISP_REGISTER_VALIDITY_SB);
73 LOG.debug("Configuration variable '{}' is unset. Setting to default value: '3.33 minutes' "
74 , LISP_REGISTER_VALIDITY_SB);
80 final long regValidity = Long.parseLong(str.trim());
81 if (regValidity >= MIN_REGISTRATION_VALIDITY_SB) {
82 this.registrationValiditySb = regValidity;
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");
92 private void initLookupPolicy(BundleContext context) {
93 // set the default value first
94 this.lookupPolicy = IMappingService.LookupPolicy.NB_FIRST;
98 if (context != null) {
99 str = context.getProperty(LISP_LOOKUP_POLICY);
103 str = System.getProperty(LISP_LOOKUP_POLICY);
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);
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);
116 LOG.debug("Setting configuration variable '{}' to 'northboundFirst' (Southbound is only looked up "
117 + "if Northbound is empty)", LISP_LOOKUP_POLICY);
121 private void initMappingMerge(BundleContext context) {
122 // set the default value first
123 this.mappingMerge = false;
127 if (context != null) {
128 str = context.getProperty(LISP_MAPPING_MERGE);
132 str = System.getProperty(LISP_MAPPING_MERGE);
134 LOG.debug("Configuration variable '{}' is unset. Setting to default value: 'false'",
140 if (str.trim().equalsIgnoreCase("true")) {
141 this.mappingMerge = true;
142 LOG.debug("Setting configuration variable '{}' to 'true'", LISP_MAPPING_MERGE);
144 LOG.debug("Setting configuration variable '{}' to 'false'", LISP_MAPPING_MERGE);
148 private void initMappingOverwrite(BundleContext context) {
149 // set the default value first
150 this.mappingOverwrite = true;
154 if (context != null) {
155 str = context.getProperty(LISP_MAPPING_OVERWRITE);
159 str = System.getProperty(LISP_MAPPING_OVERWRITE);
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;
167 LOG.debug("Configuration variable '{}' is unset. Setting to default value: 'true'",
168 LISP_MAPPING_OVERWRITE);
174 if (str.trim().equalsIgnoreCase("false")) {
175 this.mappingOverwrite = false;
176 LOG.debug("Setting configuration variable '{}' to 'false'", LISP_MAPPING_OVERWRITE);
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;
185 LOG.debug("Setting configuration variable '{}' to 'true'", LISP_MAPPING_OVERWRITE);
190 private void initSmr(BundleContext context) {
191 // set the default value first
196 if (context != null) {
197 str = context.getProperty(LISP_SMR);
201 str = System.getProperty(LISP_SMR);
203 LOG.debug("Configuration variable '{}' is unset. Setting to default value: 'true'", LISP_SMR);
208 if (str.trim().equalsIgnoreCase("false")) {
210 LOG.debug("Setting configuration variable '{}' to 'false'", LISP_SMR);
212 LOG.debug("Setting configuration variable '{}' to 'true'", LISP_SMR);
216 private void initElpPolicy(BundleContext context) {
217 // set the default value first
218 this.elpPolicy = "default";
222 if (context != null) {
223 str = context.getProperty(LISP_ELP_POLICY);
227 str = System.getProperty(LISP_ELP_POLICY);
229 LOG.debug("Configuration variable '{}' is unset. Setting to default value: 'default' (ELP only)",
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);
242 LOG.debug("Setting configuration variable '{}' to 'default' (ELP only)", LISP_ELP_POLICY);
246 public boolean mappingMergeIsSet() {
250 public boolean mappingOverwriteIsSet() {
251 return mappingOverwrite;
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);
261 public boolean smrIsSet() {
265 public void setSmr(boolean smr) {
266 LOG.debug("Setting configuration variable '{}' to '{}'", LISP_SMR, smr);
270 public String getElpPolicy() {
274 public IMappingService.LookupPolicy getLookupPolicy() {
278 public long getRegistrationValiditySb() {
279 return registrationValiditySb;
282 public void setLookupPolicy(IMappingService.LookupPolicy lookupPolicy) {
283 this.lookupPolicy = lookupPolicy;
286 public static ConfigIni getInstance() {