Add enhanced ELP RLOC processing in Map-Replies
[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 class ConfigIni {
17     protected static final Logger logger = 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     public ConfigIni() {
27         Bundle b = FrameworkUtil.getBundle(this.getClass());
28         BundleContext context = null;
29         if (b != null) {
30             context = b.getBundleContext();
31         }
32
33         initMappingOverwrite(context);
34         initSmr(context);
35         initElpPolicy(context);
36     }
37
38     private void initMappingOverwrite(BundleContext context) {
39         // set the default value first
40         this.mappingOverwrite = true;
41
42         String str = null;
43
44         if (context != null)
45             str = context.getProperty(LISP_MAPPING_OVERWRITE);
46
47         if (str == null) {
48             str = System.getProperty(LISP_MAPPING_OVERWRITE);
49             if (str == null) {
50                 logger.debug("Configuration variable '{}' is unset. Setting to default value: 'true'", LISP_MAPPING_OVERWRITE);
51                 return;
52             }
53         }
54
55         if (str.trim().equalsIgnoreCase("false")) {
56             this.mappingOverwrite = false;
57             logger.debug("Setting configuration variable '{}' to 'false'", LISP_MAPPING_OVERWRITE);
58         } else {
59             logger.debug("Setting configuration variable '{}' to 'true'", LISP_MAPPING_OVERWRITE);
60         }
61     }
62
63     private void initSmr(BundleContext context) {
64         // set the default value first
65         this.smr = false;
66
67         String str = null;
68
69         if (context != null)
70             str = context.getProperty(LISP_SMR);
71
72         if (str == null) {
73             str = System.getProperty(LISP_SMR);
74             if (str == null) {
75                 logger.debug("Configuration variable '{}' is unset. Setting to default value: 'false'", LISP_SMR);
76                 return;
77             }
78         }
79
80         if (str.trim().equalsIgnoreCase("true")) {
81             this.smr = true;
82             logger.debug("Setting configuration variable '{}' to 'true'", LISP_SMR);
83         } else {
84             logger.debug("Setting configuration variable '{}' to 'false'", LISP_SMR);
85         }
86     }
87
88     private void initElpPolicy(BundleContext context) {
89         // set the default value first
90         this.elpPolicy = "default";
91
92         String str = null;
93
94         if (context != null)
95             str = context.getProperty(LISP_ELP_POLICY);
96
97         if (str == null) {
98             str = System.getProperty(LISP_ELP_POLICY);
99             if (str == null) {
100                 logger.debug("Configuration variable '{}' is unset. Setting to default value: 'default' (ELP only)",
101                         LISP_ELP_POLICY);
102                 return;
103             }
104         }
105
106         if (str.trim().equalsIgnoreCase("both")) {
107             this.elpPolicy = "both";
108             logger.debug("Setting configuration variable '{}' to 'both' (keep ELP, add next hop)", LISP_ELP_POLICY);
109         } else if (str.trim().equalsIgnoreCase("replace")) {
110             this.elpPolicy = "replace";
111             logger.debug("Setting configuration variable '{}' to 'replace' (next hop only)", LISP_ELP_POLICY);
112         } else {
113             logger.debug("Setting configuration variable '{}' to 'default' (ELP only)", LISP_ELP_POLICY);
114         }
115     }
116
117     public boolean mappingOverwriteIsSet() {
118         return mappingOverwrite;
119     }
120
121     public boolean smrIsSet() {
122         return smr;
123     }
124
125     public String getElpPolicy() {
126         return elpPolicy;
127     }
128 }