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