Sonar: if/else/for/while/do statements need braces
[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 LOG = 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
48         if (str == null) {
49             str = System.getProperty(LISP_MAPPING_OVERWRITE);
50             if (str == null) {
51                 LOG.debug("Configuration variable '{}' is unset. Setting to default value: 'true'", LISP_MAPPING_OVERWRITE);
52                 return;
53             }
54         }
55
56         if (str.trim().equalsIgnoreCase("false")) {
57             this.mappingOverwrite = false;
58             LOG.debug("Setting configuration variable '{}' to 'false'", LISP_MAPPING_OVERWRITE);
59         } else {
60             LOG.debug("Setting configuration variable '{}' to 'true'", LISP_MAPPING_OVERWRITE);
61         }
62     }
63
64     private void initSmr(BundleContext context) {
65         // set the default value first
66         this.smr = true;
67
68         String str = null;
69
70         if (context != null) {
71             str = context.getProperty(LISP_SMR);
72         }
73
74         if (str == null) {
75             str = System.getProperty(LISP_SMR);
76             if (str == null) {
77                 LOG.debug("Configuration variable '{}' is unset. Setting to default value: 'true'", LISP_SMR);
78                 return;
79             }
80         }
81
82         if (str.trim().equalsIgnoreCase("false")) {
83             this.smr = false;
84             LOG.debug("Setting configuration variable '{}' to 'false'", LISP_SMR);
85         } else {
86             LOG.debug("Setting configuration variable '{}' to 'true'", LISP_SMR);
87         }
88     }
89
90     private void initElpPolicy(BundleContext context) {
91         // set the default value first
92         this.elpPolicy = "default";
93
94         String str = null;
95
96         if (context != null) {
97             str = context.getProperty(LISP_ELP_POLICY);
98         }
99
100         if (str == null) {
101             str = System.getProperty(LISP_ELP_POLICY);
102             if (str == null) {
103                 LOG.debug("Configuration variable '{}' is unset. Setting to default value: 'default' (ELP only)",
104                         LISP_ELP_POLICY);
105                 return;
106             }
107         }
108
109         if (str.trim().equalsIgnoreCase("both")) {
110             this.elpPolicy = "both";
111             LOG.debug("Setting configuration variable '{}' to 'both' (keep ELP, add next hop)", LISP_ELP_POLICY);
112         } else if (str.trim().equalsIgnoreCase("replace")) {
113             this.elpPolicy = "replace";
114             LOG.debug("Setting configuration variable '{}' to 'replace' (next hop only)", LISP_ELP_POLICY);
115         } else {
116             LOG.debug("Setting configuration variable '{}' to 'default' (ELP only)", LISP_ELP_POLICY);
117         }
118     }
119
120     public boolean mappingOverwriteIsSet() {
121         return mappingOverwrite;
122     }
123
124     public boolean smrIsSet() {
125         return smr;
126     }
127
128     public String getElpPolicy() {
129         return elpPolicy;
130     }
131 }