Decompose RPC implementation classes
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / configuration / ConfigurationServiceFactoryOsgiImpl.java
1 /*
2  * Copyright (c) 2018 Red Hat, 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 package org.opendaylight.openflowplugin.impl.configuration;
9
10 import java.io.IOException;
11 import java.util.Dictionary;
12 import java.util.Enumeration;
13 import java.util.HashMap;
14 import java.util.Map;
15 import org.opendaylight.openflowplugin.api.OFConstants;
16 import org.opendaylight.openflowplugin.api.openflow.configuration.ConfigurationService;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflow.provider.config.rev160510.OpenflowProviderConfig;
18 import org.osgi.framework.BundleContext;
19 import org.osgi.framework.ServiceReference;
20 import org.osgi.service.cm.Configuration;
21 import org.osgi.service.cm.ConfigurationAdmin;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 // NOT @Singleton @Service - we do not want this OSGi specific implementation to be auto-discovered in a standalone env
26 public class ConfigurationServiceFactoryOsgiImpl extends ConfigurationServiceFactoryImpl {
27     private static final Logger LOG = LoggerFactory.getLogger(ConfigurationServiceFactoryOsgiImpl.class);
28
29     private final BundleContext bundleContext;
30
31     public ConfigurationServiceFactoryOsgiImpl(final BundleContext bundleContext) {
32         this.bundleContext = bundleContext;
33     }
34
35     @Override
36     public ConfigurationService newInstance(final OpenflowProviderConfig providerConfig) {
37         ConfigurationService cs = super.newInstance(providerConfig);
38         update(cs);
39         return cs;
40     }
41
42     private void update(final ConfigurationService configurationService) {
43         LOG.info("Loading configuration from '{}' configuration file", OFConstants.CONFIG_FILE_ID);
44         final ServiceReference<ConfigurationAdmin> serviceReference =
45             bundleContext.getServiceReference(ConfigurationAdmin.class);
46         if (serviceReference == null) {
47             return;
48         }
49         final ConfigurationAdmin configurationAdmin = bundleContext.getService(serviceReference);
50         if (configurationAdmin == null) {
51             return;
52         }
53
54         try {
55             final Configuration configuration;
56             try {
57                 configuration = configurationAdmin.getConfiguration(OFConstants.CONFIG_FILE_ID);
58             } catch (IOException e) {
59                 LOG.warn("Failed to load {} configuration file", OFConstants.CONFIG_FILE_ID, e);
60                 return;
61             }
62
63             final Dictionary<String, Object> properties = configuration.getProperties();
64             if (properties != null) {
65                 final Enumeration<String> keys = properties.keys();
66                 final Map<String, String> mapProperties = new HashMap<>(properties.size());
67
68                 while (keys.hasMoreElements()) {
69                     final String key = keys.nextElement();
70                     final String value = properties.get(key).toString();
71                     mapProperties.put(key, value);
72                 }
73
74                 configurationService.update(mapProperties);
75             }
76         } finally {
77             bundleContext.ungetService(serviceReference);
78         }
79     }
80 }