Checkstyle: fix issues and enforce on implementation
[lispflowmapping.git] / mappingservice / implementation / src / main / java / org / opendaylight / lispflowmapping / implementation / config / ConfigIni.java
index 2620651d1eebe9a8a5dc5a404dc32e5b015b86f4..8c97a10c17beed1b6da7ed42d6d85358a540c272 100644 (file)
@@ -7,6 +7,7 @@
  */
 package org.opendaylight.lispflowmapping.implementation.config;
 
+import org.opendaylight.lispflowmapping.interfaces.mappingservice.IMappingService;
 import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.FrameworkUtil;
@@ -14,27 +15,133 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 public final class ConfigIni {
+
     protected static final Logger LOG = LoggerFactory.getLogger(ConfigIni.class);
+    private boolean mappingMerge;
     private boolean mappingOverwrite;
     private boolean smr;
     private String elpPolicy;
+    private IMappingService.LookupPolicy lookupPolicy;
+    private long registrationValiditySb;
+
+    // 'lisp.mappingMerge' and 'lisp.mappingOverWrite' are not independent, and they can't be both 'true'
+    // when there is a conflict, the setting in 'lisp.mappingMerge' takes precendence
+    // 'lisp.mappingOverwrite' defines the database behavior while 'lisp.mappingMerge' affects the result
+    // returned in Map-Replies
 
+    private static final String LISP_LOOKUP_POLICY = "lisp.lookupPolicy";
+    private static final String LISP_MAPPING_MERGE = "lisp.mappingMerge";
     private static final String LISP_MAPPING_OVERWRITE = "lisp.mappingOverwrite";
     private static final String LISP_SMR = "lisp.smr";
     private static final String LISP_ELP_POLICY = "lisp.elpPolicy";
+    private static final String LISP_REGISTER_VALIDITY_SB = "lisp.registerValiditySb";
+
+    // SB Map Register validity period in milliseconds. Default is 3.3 minutes.
+    public static final long MIN_REGISTRATION_VALIDITY_SB = 200000L;
 
     private static final ConfigIni INSTANCE = new ConfigIni();
 
     private ConfigIni() {
-        Bundle b = FrameworkUtil.getBundle(this.getClass());
+        Bundle bundle = FrameworkUtil.getBundle(this.getClass());
         BundleContext context = null;
-        if (b != null) {
-            context = b.getBundleContext();
+        if (bundle != null) {
+            context = bundle.getBundleContext();
         }
 
+        // Initialize mappingMerge first, since mappingOverwrite depends on it
+        initMappingMerge(context);
         initMappingOverwrite(context);
         initSmr(context);
         initElpPolicy(context);
+        initLookupPolicy(context);
+        initRegisterValiditySb(context);
+    }
+
+    private void initRegisterValiditySb(BundleContext context) {
+        // set the default value first
+        this.registrationValiditySb = MIN_REGISTRATION_VALIDITY_SB;
+
+        String str = null;
+
+        if (context != null) {
+            str = context.getProperty(LISP_REGISTER_VALIDITY_SB);
+        }
+
+        if (str == null) {
+            str = System.getProperty(LISP_REGISTER_VALIDITY_SB);
+            if (str == null) {
+                LOG.debug("Configuration variable '{}' is unset. Setting to default value: '3.33 minutes' ",
+                        LISP_REGISTER_VALIDITY_SB);
+                return;
+            }
+        }
+
+        try {
+            final long regValidity = Long.parseLong(str.trim());
+            if (regValidity >= MIN_REGISTRATION_VALIDITY_SB) {
+                this.registrationValiditySb = regValidity;
+            }
+        } catch (NumberFormatException e) {
+            this.registrationValiditySb = MIN_REGISTRATION_VALIDITY_SB;
+            LOG.debug("Configuration variable 'registerValiditySb' was not set correctly. Registration validity for"
+                    + "South Bound Map Registers is set to default value of 3.3 minutes");
+        }
+    }
+
+    private void initLookupPolicy(BundleContext context) {
+        // set the default value first
+        this.lookupPolicy = IMappingService.LookupPolicy.NB_FIRST;
+
+        String str = null;
+
+        if (context != null) {
+            str = context.getProperty(LISP_LOOKUP_POLICY);
+        }
+
+        if (str == null) {
+            str = System.getProperty(LISP_LOOKUP_POLICY);
+            if (str == null) {
+                LOG.debug("Configuration variable '{}' is unset. Setting to default value: 'northboundFirst' "
+                        + "(Southbound is only looked up if Northbound is empty) ", LISP_LOOKUP_POLICY);
+                return;
+            }
+        }
+
+        if (str.trim().equalsIgnoreCase("northboundAndSouthbound")) {
+            this.lookupPolicy = IMappingService.LookupPolicy.NB_AND_SB;
+            LOG.debug("Setting configuration variable '{}' to 'northboundAndSouthbound' (Southbound is always "
+                    + "looked up and can filter Northbound if intersection is not empty)", LISP_LOOKUP_POLICY);
+        } else {
+            LOG.debug("Setting configuration variable '{}' to 'northboundFirst' (Southbound is only looked up "
+                    + "if Northbound is empty)", LISP_LOOKUP_POLICY);
+        }
+    }
+
+    private void initMappingMerge(BundleContext context) {
+        // set the default value first
+        this.mappingMerge = false;
+
+        String str = null;
+
+        if (context != null) {
+            str = context.getProperty(LISP_MAPPING_MERGE);
+        }
+
+        if (str == null) {
+            str = System.getProperty(LISP_MAPPING_MERGE);
+            if (str == null) {
+                LOG.debug("Configuration variable '{}' is unset. Setting to default value: 'false'",
+                        LISP_MAPPING_MERGE);
+                return;
+            }
+        }
+
+        if (str.trim().equalsIgnoreCase("true")) {
+            this.mappingMerge = true;
+            LOG.debug("Setting configuration variable '{}' to 'true'", LISP_MAPPING_MERGE);
+        } else {
+            LOG.debug("Setting configuration variable '{}' to 'false'", LISP_MAPPING_MERGE);
+        }
     }
 
     private void initMappingOverwrite(BundleContext context) {
@@ -50,7 +157,15 @@ public final class ConfigIni {
         if (str == null) {
             str = System.getProperty(LISP_MAPPING_OVERWRITE);
             if (str == null) {
-                LOG.debug("Configuration variable '{}' is unset. Setting to default value: 'true'", LISP_MAPPING_OVERWRITE);
+                if (this.mappingMerge) {
+                    // If merge is enabled and overwriting configuration is not set, disable it
+                    LOG.debug("Configuration variable '{}' is unset. Since '{}'=true setting to 'false'",
+                            LISP_MAPPING_OVERWRITE, LISP_MAPPING_MERGE);
+                    this.mappingOverwrite = false;
+                } else {
+                    LOG.debug("Configuration variable '{}' is unset. Setting to default value: 'true'",
+                            LISP_MAPPING_OVERWRITE);
+                }
                 return;
             }
         }
@@ -59,7 +174,15 @@ public final class ConfigIni {
             this.mappingOverwrite = false;
             LOG.debug("Setting configuration variable '{}' to 'false'", LISP_MAPPING_OVERWRITE);
         } else {
-            LOG.debug("Setting configuration variable '{}' to 'true'", LISP_MAPPING_OVERWRITE);
+            if (this.mappingMerge) {
+                LOG.warn("Can't set configuration variable '{}' to 'true' since '{}' is enabled",
+                        LISP_MAPPING_OVERWRITE, LISP_MAPPING_MERGE);
+                LOG.warn("If you really need to enable overwriting, please disable merging.");
+                LOG.debug("Setting configuration variable '{}' to 'false'", LISP_MAPPING_OVERWRITE);
+                this.mappingOverwrite = false;
+            } else {
+                LOG.debug("Setting configuration variable '{}' to 'true'", LISP_MAPPING_OVERWRITE);
+            }
         }
     }
 
@@ -119,18 +242,46 @@ public final class ConfigIni {
         }
     }
 
+    public boolean mappingMergeIsSet() {
+        return mappingMerge;
+    }
+
     public boolean mappingOverwriteIsSet() {
         return mappingOverwrite;
     }
 
+    public void setMappingOverwrite(boolean mappingOverwrite) {
+        LOG.debug("Setting configuration variable '{}' to '{}'", LISP_MAPPING_OVERWRITE, mappingOverwrite);
+        this.mappingOverwrite = mappingOverwrite;
+        LOG.debug("Setting configuration variable '{}' to '{}'", LISP_MAPPING_MERGE, !(mappingOverwrite));
+        this.mappingMerge = !(mappingOverwrite);
+    }
+
     public boolean smrIsSet() {
         return smr;
     }
 
+    public void setSmr(boolean smr) {
+        LOG.debug("Setting configuration variable '{}' to '{}'", LISP_SMR, smr);
+        this.smr = smr;
+    }
+
     public String getElpPolicy() {
         return elpPolicy;
     }
 
+    public IMappingService.LookupPolicy getLookupPolicy() {
+        return lookupPolicy;
+    }
+
+    public long getRegistrationValiditySb() {
+        return registrationValiditySb;
+    }
+
+    public void setLookupPolicy(IMappingService.LookupPolicy lookupPolicy) {
+        this.lookupPolicy = lookupPolicy;
+    }
+
     public static ConfigIni getInstance() {
         return INSTANCE;
     }