Bug 4822: Fix mapping record merging
[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 mappingMerge;
19     private boolean mappingOverwrite;
20     private boolean smr;
21     private String elpPolicy;
22
23     // 'lisp.mappingMerge' and 'lisp.mappingOverWrite' are not independent, and they can't be both 'true'
24     // when there is a conflict, the setting in 'lisp.mappingMerge' takes precendence
25     private static final String LISP_MAPPING_MERGE = "lisp.mappingMerge";
26     private static final String LISP_MAPPING_OVERWRITE = "lisp.mappingOverwrite";
27     private static final String LISP_SMR = "lisp.smr";
28     private static final String LISP_ELP_POLICY = "lisp.elpPolicy";
29
30     private static final ConfigIni INSTANCE = new ConfigIni();
31
32     private ConfigIni() {
33         Bundle b = FrameworkUtil.getBundle(this.getClass());
34         BundleContext context = null;
35         if (b != null) {
36             context = b.getBundleContext();
37         }
38
39         // Initialize mappingMerge first, since mappingOverwrite depends on it
40         initMappingMerge(context);
41         initMappingOverwrite(context);
42         initSmr(context);
43         initElpPolicy(context);
44     }
45
46     private void initMappingMerge(BundleContext context) {
47         // set the default value first
48         this.mappingMerge = false;
49
50         String str = null;
51
52         if (context != null) {
53             str = context.getProperty(LISP_MAPPING_MERGE);
54         }
55
56         if (str == null) {
57             str = System.getProperty(LISP_MAPPING_MERGE);
58             if (str == null) {
59                 LOG.debug("Configuration variable '{}' is unset. Setting to default value: 'false'", LISP_MAPPING_MERGE);
60                 return;
61             }
62         }
63
64         if (str.trim().equalsIgnoreCase("true")) {
65             this.mappingMerge = true;
66             LOG.debug("Setting configuration variable '{}' to 'true'", LISP_MAPPING_MERGE);
67         } else {
68             LOG.debug("Setting configuration variable '{}' to 'false'", LISP_MAPPING_MERGE);
69         }
70     }
71
72     private void initMappingOverwrite(BundleContext context) {
73         // set the default value first
74         this.mappingOverwrite = true;
75
76         String str = null;
77
78         if (context != null) {
79             str = context.getProperty(LISP_MAPPING_OVERWRITE);
80         }
81
82         if (str == null) {
83             str = System.getProperty(LISP_MAPPING_OVERWRITE);
84             if (str == null) {
85                 if (this.mappingMerge) {
86                     // If merge is enabled and overwriting configuration is not set, disable it
87                     LOG.debug("Configuration variable '{}' is unset. Since '{}'=true setting to 'false'",
88                             LISP_MAPPING_OVERWRITE, LISP_MAPPING_MERGE);
89                     this.mappingOverwrite = false;
90                 } else {
91                     LOG.debug("Configuration variable '{}' is unset. Setting to default value: 'true'",
92                             LISP_MAPPING_OVERWRITE);
93                 }
94                 return;
95             }
96         }
97
98         if (str.trim().equalsIgnoreCase("false")) {
99             this.mappingOverwrite = false;
100             LOG.debug("Setting configuration variable '{}' to 'false'", LISP_MAPPING_OVERWRITE);
101         } else {
102             if (this.mappingMerge) {
103                 LOG.warn("Can't set configuration variable '{}' to 'true' since '{}' is enabled",
104                         LISP_MAPPING_OVERWRITE, LISP_MAPPING_MERGE);
105                 LOG.warn("If you really need to enable overwriting, please disable merging.");
106                 LOG.debug("Setting configuration variable '{}' to 'false'", LISP_MAPPING_OVERWRITE);
107                 this.mappingOverwrite = false;
108             } else {
109                 LOG.debug("Setting configuration variable '{}' to 'true'", LISP_MAPPING_OVERWRITE);
110             }
111         }
112     }
113
114     private void initSmr(BundleContext context) {
115         // set the default value first
116         this.smr = true;
117
118         String str = null;
119
120         if (context != null) {
121             str = context.getProperty(LISP_SMR);
122         }
123
124         if (str == null) {
125             str = System.getProperty(LISP_SMR);
126             if (str == null) {
127                 LOG.debug("Configuration variable '{}' is unset. Setting to default value: 'true'", LISP_SMR);
128                 return;
129             }
130         }
131
132         if (str.trim().equalsIgnoreCase("false")) {
133             this.smr = false;
134             LOG.debug("Setting configuration variable '{}' to 'false'", LISP_SMR);
135         } else {
136             LOG.debug("Setting configuration variable '{}' to 'true'", LISP_SMR);
137         }
138     }
139
140     private void initElpPolicy(BundleContext context) {
141         // set the default value first
142         this.elpPolicy = "default";
143
144         String str = null;
145
146         if (context != null) {
147             str = context.getProperty(LISP_ELP_POLICY);
148         }
149
150         if (str == null) {
151             str = System.getProperty(LISP_ELP_POLICY);
152             if (str == null) {
153                 LOG.debug("Configuration variable '{}' is unset. Setting to default value: 'default' (ELP only)",
154                         LISP_ELP_POLICY);
155                 return;
156             }
157         }
158
159         if (str.trim().equalsIgnoreCase("both")) {
160             this.elpPolicy = "both";
161             LOG.debug("Setting configuration variable '{}' to 'both' (keep ELP, add next hop)", LISP_ELP_POLICY);
162         } else if (str.trim().equalsIgnoreCase("replace")) {
163             this.elpPolicy = "replace";
164             LOG.debug("Setting configuration variable '{}' to 'replace' (next hop only)", LISP_ELP_POLICY);
165         } else {
166             LOG.debug("Setting configuration variable '{}' to 'default' (ELP only)", LISP_ELP_POLICY);
167         }
168     }
169
170     public boolean mappingMergeIsSet() {
171         return mappingMerge;
172     }
173
174     public boolean mappingOverwriteIsSet() {
175         return mappingOverwrite;
176     }
177
178     public boolean smrIsSet() {
179         return smr;
180     }
181
182     public String getElpPolicy() {
183         return elpPolicy;
184     }
185
186     public static ConfigIni getInstance() {
187         return INSTANCE;
188     }
189 }