Update ODL Tenant IP to ODL IP
[groupbasedpolicy.git] / renderers / vpp / src / main / java / org / opendaylight / groupbasedpolicy / renderer / vpp / config / ConfigurationService.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 org.osgi.framework.Bundle;
12 import org.osgi.framework.BundleContext;
13 import org.osgi.framework.Constants;
14 import org.osgi.framework.FrameworkUtil;
15 import org.osgi.service.cm.ConfigurationException;
16 import org.osgi.service.cm.ManagedService;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19
20 import java.util.Dictionary;
21 import java.util.Enumeration;
22 import java.util.HashMap;
23 import java.util.Hashtable;
24 import java.util.function.Consumer;
25
26 /**
27  * Created by Shakib Ahmed on 4/17/17.
28  */
29 public class ConfigurationService implements ManagedService{
30     private static final Logger LOG = LoggerFactory.getLogger(ConfigurationService.class);
31
32     HashMap<String, Consumer> configMethods;
33
34     ConfigUtil configUtil = ConfigUtil.getInstance();
35
36     public ConfigurationService() {
37         Hashtable<String, Object> properties = new Hashtable<>();
38         properties.put(Constants.SERVICE_PID, "org.opendaylight.groupbasedpolicy.renderer.vpp.startup");
39         Bundle bundle = FrameworkUtil.getBundle(this.getClass());
40         BundleContext context = null;
41         if (bundle != null) {
42             context = bundle.getBundleContext();
43         }
44
45         //this function needs to be called before context.registerService() method
46         mapConfigMethods();
47
48         context.registerService(ManagedService.class.getName(), this, properties);
49     }
50
51     @Override
52     public void updated(Dictionary dictionary) throws ConfigurationException {
53         if (dictionary == null) {
54             return;
55         }
56
57         Enumeration keys = dictionary.keys();
58         while (keys.hasMoreElements()) {
59             String key = (String) keys.nextElement();
60
61             if (configMethods.containsKey(key)) {
62                 configMethods.get(key).accept(dictionary.get(key));
63                 LOG.info("Property {} being updated", key);
64             } else {
65                 LOG.debug("Configuration {} = {} being ignored because no consumer for this " +
66                         "configuration key has been mapped", keys, dictionary.get(key));
67             }
68         }
69     }
70
71     private void mapConfigMethods() {
72         configMethods = new HashMap<>();
73
74         configMethods.put(ConfigUtil.ODL_IP,
75                 ip -> configUtil.configureOdlTenantIp((String) ip));
76         configMethods.put(ConfigUtil.LISP_MAPREGISTER_ENABLED,
77                 mrConfig -> configUtil.configureMapRegister((String) mrConfig));
78         configMethods.put(ConfigUtil.LISP_OVERLAY_ENABLED,
79                 overlayConfig -> configUtil.configureLispOverlayEnabled((String) overlayConfig));
80         configMethods.put(ConfigUtil.L3_FLAT_ENABLED,
81                 l3FlatConfig -> configUtil.configL3FlatEnabled((String) l3FlatConfig));
82     }
83 }