221b3508b10a2334b6e742692df332581431c2df
[groupbasedpolicy.git] / renderers / vpp / src / main / java / org / opendaylight / groupbasedpolicy / renderer / vpp / config / ConfigUtil.java
1 /*
2  * Copyright (c) 2017 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
9 package org.opendaylight.groupbasedpolicy.renderer.vpp.config;
10
11 import com.google.common.base.Preconditions;
12 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
13 import org.slf4j.Logger;
14 import org.slf4j.LoggerFactory;
15
16 /**
17  * Created by Shakib Ahmed on 4/13/17.
18  */
19 public class ConfigUtil {
20
21     private static final Logger LOG = LoggerFactory.getLogger(ConfigUtil.class);
22
23     private static final boolean DEFAULT_LISP_OVERLAY_ENABLED = false;
24     private static final boolean DEFAULT_LISP_MAPREGISTER_ENABLED = true;
25     private static final boolean DEFAULT_L3_FLAT_ENABLED = false;
26     private static final String DEFAULT_TRUE_STRING_VALUE = "true";
27     private static final String CONFIGURATION_VARIABLE_MESSAGE =
28         "Configuration variable {} is being unset. Setting the variable to {}";
29
30     private IpAddress odlTenantIp;
31     private boolean lispOverlayEnabled = DEFAULT_LISP_OVERLAY_ENABLED;
32     private boolean lispMapRegisterEnabled = DEFAULT_LISP_MAPREGISTER_ENABLED;
33     private boolean l3FlatEnabled = DEFAULT_L3_FLAT_ENABLED;
34
35     static final String ODL_TENANT_IP = "odl.ip.tenant";
36     static final String LISP_OVERLAY_ENABLED = "gbp.lisp.enabled";
37     static final String LISP_MAPREGISTER_ENABLED = "vpp.lisp.mapregister.enabled";
38     static final String L3_FLAT_ENABLED = "vpp.l3.flat.enabled";
39
40     private static final ConfigUtil INSTANCE = new ConfigUtil();
41
42     private ConfigUtil() {
43         configureOdlTenantIp(null);
44         configureLispOverlayEnabled(null);
45         configureMapRegister(null);
46         configL3FlatEnabled(null);
47     }
48
49     public static ConfigUtil getInstance() {
50         return INSTANCE;
51     }
52
53     void configureLispOverlayEnabled(String configStr) {
54         if (configStr == null) {
55             configStr = System.getProperty(LISP_OVERLAY_ENABLED);
56
57             if (configStr == null) {
58                 lispOverlayEnabled = DEFAULT_LISP_OVERLAY_ENABLED;
59                 LOG.debug(CONFIGURATION_VARIABLE_MESSAGE, LISP_OVERLAY_ENABLED, DEFAULT_LISP_OVERLAY_ENABLED);
60                 return;
61             }
62         }
63         lispOverlayEnabled = configStr.trim().equalsIgnoreCase(DEFAULT_TRUE_STRING_VALUE);
64     }
65
66     void configureOdlTenantIp(String configStr) {
67         if (configStr == null) {
68             odlTenantIp = null;
69             LOG.debug("Configuration variable {} is being unset. Setting the variable to null",
70                     ODL_TENANT_IP);
71             return;
72         }
73         odlTenantIp = new IpAddress(configStr.trim().toCharArray());
74     }
75
76     void configureMapRegister(String configStr) {
77         if (configStr == null) {
78             lispMapRegisterEnabled = DEFAULT_LISP_MAPREGISTER_ENABLED;
79             LOG.debug(CONFIGURATION_VARIABLE_MESSAGE, LISP_MAPREGISTER_ENABLED, DEFAULT_LISP_MAPREGISTER_ENABLED);
80             return;
81         }
82         lispMapRegisterEnabled = configStr.trim().equalsIgnoreCase(DEFAULT_TRUE_STRING_VALUE);
83     }
84
85     void configL3FlatEnabled(String configStr) {
86         if (configStr == null) {
87             l3FlatEnabled = DEFAULT_L3_FLAT_ENABLED;
88             LOG.debug(CONFIGURATION_VARIABLE_MESSAGE, L3_FLAT_ENABLED, DEFAULT_L3_FLAT_ENABLED);
89             return;
90         }
91         l3FlatEnabled = configStr.trim().equalsIgnoreCase(DEFAULT_TRUE_STRING_VALUE);
92     }
93
94     public IpAddress getOdlTenantIp() {
95         return odlTenantIp;
96     }
97
98     public boolean isLispOverlayEnabled() {
99         if (lispOverlayEnabled) {
100             Preconditions.checkNotNull(odlTenantIp, "Configuration variable {} is not set. " +
101                     "So, {} can't be used for chances of invalid config!", LISP_OVERLAY_ENABLED);
102         }
103         return lispOverlayEnabled;
104     }
105
106     public boolean isLispMapRegisterEnabled() {
107         return lispMapRegisterEnabled;
108     }
109
110     public boolean isL3FlatEnabled() {
111         return l3FlatEnabled;
112     }
113 }