Add config service for loading cfg file
[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
25     private IpAddress odlTenantIp;
26     private boolean lispOverlayEnabled = DEFAULT_LISP_OVERLAY_ENABLED;
27     private boolean lispMapRegisterEnbled = DEFAULT_LISP_MAPREGISTER_ENABLED;
28
29     public static String ODL_TENANT_IP = "odl.ip.tenant";
30     public static String LISP_OVERLAY_ENABLED = "gbp.lisp.enabled";
31     public static String LISP_MAPREGISTER_ENABLED = "vpp.lisp.mapregister.enabled";
32
33     private static ConfigUtil INSTANCE = new ConfigUtil();
34
35     private ConfigUtil() {
36         configureDefaults();
37     }
38
39     public static ConfigUtil getInstance() {
40         return INSTANCE;
41     }
42
43     private void configureDefaults() {
44         configureOdlTenantIp(null);
45         configureLispOverlayEnabled(null);
46         configureMapRegister(null);
47     }
48
49     public void configureLispOverlayEnabled(String configStr) {
50         if (configStr == null) {
51             configStr = System.getProperty(LISP_OVERLAY_ENABLED);
52
53             if (configStr == null) {
54                 lispOverlayEnabled = DEFAULT_LISP_OVERLAY_ENABLED;
55                 LOG.debug("Configuration variable {} is being unset. Setting the variable to {}",
56                         LISP_OVERLAY_ENABLED, DEFAULT_LISP_OVERLAY_ENABLED);
57                 return;
58             }
59         }
60
61         configStr = configStr.trim();
62
63         if (configStr.equalsIgnoreCase("true")) {
64             lispOverlayEnabled = true;
65         } else {
66             lispOverlayEnabled = false;
67         }
68     }
69
70     public void configureOdlTenantIp(String configStr) {
71         if (configStr == null) {
72             odlTenantIp = null;
73             LOG.debug("Configuration variable {} is being unset. Setting the variable to null",
74                     ODL_TENANT_IP);
75             return;
76         }
77
78         configStr = configStr.trim();
79         odlTenantIp = new IpAddress(configStr.toCharArray());
80     }
81
82     public void configureMapRegister(String configStr) {
83         if (configStr == null) {
84             lispMapRegisterEnbled = DEFAULT_LISP_MAPREGISTER_ENABLED;
85             LOG.debug("Configuration variable {} is being unset. Setting the variable to {}",
86                     LISP_MAPREGISTER_ENABLED, DEFAULT_LISP_MAPREGISTER_ENABLED);
87             return;
88         }
89
90         configStr = configStr.trim();
91
92         if (configStr.equalsIgnoreCase("true")) {
93             lispMapRegisterEnbled = true;
94         } else {
95             lispOverlayEnabled = false;
96         }
97     }
98
99     public IpAddress getOdlTenantIp() {
100         return odlTenantIp;
101     }
102
103     public boolean isLispOverlayEnabled() {
104         if (lispOverlayEnabled) {
105             Preconditions.checkNotNull(odlTenantIp, "Configuration variable {} is not set. " +
106                     "So, {} can't be used for chances of invalid config!", LISP_OVERLAY_ENABLED);
107         }
108         return lispOverlayEnabled;
109     }
110
111     public boolean isLispMapRegisterEnbled() {
112         return lispMapRegisterEnbled;
113     }
114 }