Add L3 flat overlay config in startup.cfg
[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     private static final Logger LOG = LoggerFactory.getLogger(ConfigUtil.class);
21
22     private static boolean DEFAULT_LISP_OVERLAY_ENABLED = false;
23     private static boolean DEFAULT_LISP_MAPREGISTER_ENABLED = true;
24     private static boolean DEFAULT_L3_FLAT_ENABLED = false;
25
26     private IpAddress odlTenantIp;
27     private boolean lispOverlayEnabled = DEFAULT_LISP_OVERLAY_ENABLED;
28     private boolean lispMapRegisterEnbled = DEFAULT_LISP_MAPREGISTER_ENABLED;
29     private boolean l3FlatEnabled = DEFAULT_L3_FLAT_ENABLED;
30
31
32     public static String ODL_TENANT_IP = "odl.ip.tenant";
33     public static String LISP_OVERLAY_ENABLED = "gbp.lisp.enabled";
34     public static String LISP_MAPREGISTER_ENABLED = "vpp.lisp.mapregister.enabled";
35     public static String L3_FLAT_ENABLED = "vpp.l3.flat.enabled";
36
37     private static ConfigUtil INSTANCE = new ConfigUtil();
38
39     private ConfigUtil() {
40         configureDefaults();
41     }
42
43     public static ConfigUtil getInstance() {
44         return INSTANCE;
45     }
46
47     private void configureDefaults() {
48         configureOdlTenantIp(null);
49         configureLispOverlayEnabled(null);
50         configureMapRegister(null);
51     }
52
53     public 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 {} is being unset. Setting the variable to {}",
60                         LISP_OVERLAY_ENABLED, DEFAULT_LISP_OVERLAY_ENABLED);
61                 return;
62             }
63         }
64
65         configStr = configStr.trim();
66
67         if (configStr.equalsIgnoreCase("true")) {
68             lispOverlayEnabled = true;
69         } else {
70             lispOverlayEnabled = false;
71         }
72     }
73
74     public void configureOdlTenantIp(String configStr) {
75         if (configStr == null) {
76             odlTenantIp = null;
77             LOG.debug("Configuration variable {} is being unset. Setting the variable to null",
78                     ODL_TENANT_IP);
79             return;
80         }
81
82         configStr = configStr.trim();
83         odlTenantIp = new IpAddress(configStr.toCharArray());
84     }
85
86     public void configureMapRegister(String configStr) {
87         if (configStr == null) {
88             lispMapRegisterEnbled = DEFAULT_LISP_MAPREGISTER_ENABLED;
89             LOG.debug("Configuration variable {} is being unset. Setting the variable to {}",
90                     LISP_MAPREGISTER_ENABLED, DEFAULT_LISP_MAPREGISTER_ENABLED);
91             return;
92         }
93
94         configStr = configStr.trim();
95
96         if (configStr.equalsIgnoreCase("true")) {
97             lispMapRegisterEnbled = true;
98         } else {
99             lispOverlayEnabled = false;
100         }
101     }
102
103     public void configL3FlatEnabled(String configStr) {
104         if (configStr == null) {
105             l3FlatEnabled = DEFAULT_L3_FLAT_ENABLED;
106             LOG.debug("Configuration variable {} is being unset. Setting the variable to {}",
107                     L3_FLAT_ENABLED, DEFAULT_L3_FLAT_ENABLED);
108         }
109
110         configStr = configStr.trim();
111
112         if (configStr.equalsIgnoreCase("true")) {
113             l3FlatEnabled = true;
114         } else {
115             l3FlatEnabled = false;
116         }
117     }
118
119     public IpAddress getOdlTenantIp() {
120         return odlTenantIp;
121     }
122
123     public boolean isLispOverlayEnabled() {
124         if (lispOverlayEnabled) {
125             Preconditions.checkNotNull(odlTenantIp, "Configuration variable {} is not set. " +
126                     "So, {} can't be used for chances of invalid config!", LISP_OVERLAY_ENABLED);
127         }
128         return lispOverlayEnabled;
129     }
130
131     public boolean isLispMapRegisterEnbled() {
132         return lispMapRegisterEnbled;
133     }
134
135     public boolean isL3FlatEnabled() {
136         return l3FlatEnabled;
137     }
138 }