View Javadoc

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      private int lookupPolicy;
23  
24      // 'lisp.mappingMerge' and 'lisp.mappingOverWrite' are not independent, and they can't be both 'true'
25      // when there is a conflict, the setting in 'lisp.mappingMerge' takes precendence
26      // 'lisp.mappingOverwrite' defines the database behavior while 'lisp.mappingMerge' affects the result
27      // returned in Map-Replies
28  
29      private static final String LISP_LOOKUP_POLICY = "lisp.lookupPolicy";
30      private static final String LISP_MAPPING_MERGE = "lisp.mappingMerge";
31      private static final String LISP_MAPPING_OVERWRITE = "lisp.mappingOverwrite";
32      private static final String LISP_SMR = "lisp.smr";
33      private static final String LISP_ELP_POLICY = "lisp.elpPolicy";
34  
35      // lookupPolicy options
36      public static final int NB_FIRST = 0;
37      public static final int NB_AND_SB = 1;
38  
39      private static final ConfigIni INSTANCE = new ConfigIni();
40  
41      private ConfigIni() {
42          Bundle b = FrameworkUtil.getBundle(this.getClass());
43          BundleContext context = null;
44          if (b != null) {
45              context = b.getBundleContext();
46          }
47  
48          // Initialize mappingMerge first, since mappingOverwrite depends on it
49          initMappingMerge(context);
50          initMappingOverwrite(context);
51          initSmr(context);
52          initElpPolicy(context);
53          initLookupPolicy(context);
54      }
55  
56      private void initLookupPolicy(BundleContext context) {
57          // set the default value first
58          this.lookupPolicy = NB_FIRST;
59  
60          String str = null;
61  
62          if (context != null) {
63              str = context.getProperty(LISP_LOOKUP_POLICY);
64          }
65  
66          if (str == null) {
67              str = System.getProperty(LISP_LOOKUP_POLICY);
68              if (str == null) {
69                  LOG.debug("Configuration variable '{}' is unset. Setting to default value: 'northboundFirst' "
70                          + "(Southbound is only looked up if Northbound is empty) ", LISP_LOOKUP_POLICY);
71                  return;
72              }
73          }
74  
75          if (str.trim().equalsIgnoreCase("northboundAndSouthbound")) {
76              this.lookupPolicy = NB_AND_SB;
77              LOG.debug("Setting configuration variable '{}' to 'northboundAndSouthbound' (Southbound is always "
78                      + "looked up and can filter Northbound if intersection is not empty)", LISP_LOOKUP_POLICY);
79          } else {
80              LOG.debug("Setting configuration variable '{}' to 'northboundFirst' (Southbound is only looked up "
81                      + "if Northbound is empty)", LISP_LOOKUP_POLICY);
82          }
83      }
84  
85      private void initMappingMerge(BundleContext context) {
86          // set the default value first
87          this.mappingMerge = false;
88  
89          String str = null;
90  
91          if (context != null) {
92              str = context.getProperty(LISP_MAPPING_MERGE);
93          }
94  
95          if (str == null) {
96              str = System.getProperty(LISP_MAPPING_MERGE);
97              if (str == null) {
98                  LOG.debug("Configuration variable '{}' is unset. Setting to default value: 'false'",
99                          LISP_MAPPING_MERGE);
100                 return;
101             }
102         }
103 
104         if (str.trim().equalsIgnoreCase("true")) {
105             this.mappingMerge = true;
106             LOG.debug("Setting configuration variable '{}' to 'true'", LISP_MAPPING_MERGE);
107         } else {
108             LOG.debug("Setting configuration variable '{}' to 'false'", LISP_MAPPING_MERGE);
109         }
110     }
111 
112     private void initMappingOverwrite(BundleContext context) {
113         // set the default value first
114         this.mappingOverwrite = true;
115 
116         String str = null;
117 
118         if (context != null) {
119             str = context.getProperty(LISP_MAPPING_OVERWRITE);
120         }
121 
122         if (str == null) {
123             str = System.getProperty(LISP_MAPPING_OVERWRITE);
124             if (str == null) {
125                 if (this.mappingMerge) {
126                     // If merge is enabled and overwriting configuration is not set, disable it
127                     LOG.debug("Configuration variable '{}' is unset. Since '{}'=true setting to 'false'",
128                             LISP_MAPPING_OVERWRITE, LISP_MAPPING_MERGE);
129                     this.mappingOverwrite = false;
130                 } else {
131                     LOG.debug("Configuration variable '{}' is unset. Setting to default value: 'true'",
132                             LISP_MAPPING_OVERWRITE);
133                 }
134                 return;
135             }
136         }
137 
138         if (str.trim().equalsIgnoreCase("false")) {
139             this.mappingOverwrite = false;
140             LOG.debug("Setting configuration variable '{}' to 'false'", LISP_MAPPING_OVERWRITE);
141         } else {
142             if (this.mappingMerge) {
143                 LOG.warn("Can't set configuration variable '{}' to 'true' since '{}' is enabled",
144                         LISP_MAPPING_OVERWRITE, LISP_MAPPING_MERGE);
145                 LOG.warn("If you really need to enable overwriting, please disable merging.");
146                 LOG.debug("Setting configuration variable '{}' to 'false'", LISP_MAPPING_OVERWRITE);
147                 this.mappingOverwrite = false;
148             } else {
149                 LOG.debug("Setting configuration variable '{}' to 'true'", LISP_MAPPING_OVERWRITE);
150             }
151         }
152     }
153 
154     private void initSmr(BundleContext context) {
155         // set the default value first
156         this.smr = true;
157 
158         String str = null;
159 
160         if (context != null) {
161             str = context.getProperty(LISP_SMR);
162         }
163 
164         if (str == null) {
165             str = System.getProperty(LISP_SMR);
166             if (str == null) {
167                 LOG.debug("Configuration variable '{}' is unset. Setting to default value: 'true'", LISP_SMR);
168                 return;
169             }
170         }
171 
172         if (str.trim().equalsIgnoreCase("false")) {
173             this.smr = false;
174             LOG.debug("Setting configuration variable '{}' to 'false'", LISP_SMR);
175         } else {
176             LOG.debug("Setting configuration variable '{}' to 'true'", LISP_SMR);
177         }
178     }
179 
180     private void initElpPolicy(BundleContext context) {
181         // set the default value first
182         this.elpPolicy = "default";
183 
184         String str = null;
185 
186         if (context != null) {
187             str = context.getProperty(LISP_ELP_POLICY);
188         }
189 
190         if (str == null) {
191             str = System.getProperty(LISP_ELP_POLICY);
192             if (str == null) {
193                 LOG.debug("Configuration variable '{}' is unset. Setting to default value: 'default' (ELP only)",
194                         LISP_ELP_POLICY);
195                 return;
196             }
197         }
198 
199         if (str.trim().equalsIgnoreCase("both")) {
200             this.elpPolicy = "both";
201             LOG.debug("Setting configuration variable '{}' to 'both' (keep ELP, add next hop)", LISP_ELP_POLICY);
202         } else if (str.trim().equalsIgnoreCase("replace")) {
203             this.elpPolicy = "replace";
204             LOG.debug("Setting configuration variable '{}' to 'replace' (next hop only)", LISP_ELP_POLICY);
205         } else {
206             LOG.debug("Setting configuration variable '{}' to 'default' (ELP only)", LISP_ELP_POLICY);
207         }
208     }
209 
210     public boolean mappingMergeIsSet() {
211         return mappingMerge;
212     }
213 
214     public boolean mappingOverwriteIsSet() {
215         return mappingOverwrite;
216     }
217 
218     public boolean smrIsSet() {
219         return smr;
220     }
221 
222     public String getElpPolicy() {
223         return elpPolicy;
224     }
225 
226     public int getLookupPolicy() {
227         return lookupPolicy;
228     }
229 
230     public static ConfigIni getInstance() {
231         return INSTANCE;
232     }
233 }