Merge "use annotations instead XML for BP in impl"
[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.Enumeration;
12 import java.util.HashMap;
13 import java.util.Map;
14 import java.util.Optional;
15 import javax.inject.Inject;
16 import org.opendaylight.openflowplugin.api.OFConstants;
17 import org.opendaylight.openflowplugin.api.openflow.configuration.ConfigurationService;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflow.provider.config.rev160510.OpenflowProviderConfig;
19 import org.osgi.framework.BundleContext;
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
28     private static final Logger LOG = LoggerFactory.getLogger(ConfigurationServiceFactoryOsgiImpl.class);
29
30     private final BundleContext bundleContext;
31
32     @Inject
33     public ConfigurationServiceFactoryOsgiImpl(BundleContext bundleContext) {
34         this.bundleContext = bundleContext;
35     }
36
37     @Override
38     public ConfigurationService newInstance(OpenflowProviderConfig providerConfig) {
39         ConfigurationService cs = super.newInstance(providerConfig);
40         update(cs);
41         return cs;
42     }
43
44     private void update(ConfigurationService configurationService) {
45         LOG.info("Loading configuration from '{}' configuration file", OFConstants.CONFIG_FILE_ID);
46         Optional.ofNullable(bundleContext.getServiceReference(ConfigurationAdmin.class)).ifPresent(serviceReference -> {
47             final ConfigurationAdmin configurationAdmin = bundleContext.getService(serviceReference);
48
49             try {
50                 final Configuration configuration = configurationAdmin.getConfiguration(OFConstants.CONFIG_FILE_ID);
51
52                 Optional.ofNullable(configuration.getProperties()).ifPresent(properties -> {
53                     final Enumeration<String> keys = properties.keys();
54                     final Map<String, String> mapProperties = new HashMap<>(properties.size());
55
56                     while (keys.hasMoreElements()) {
57                         final String key = keys.nextElement();
58                         final String value = properties.get(key).toString();
59                         mapProperties.put(key, value);
60                     }
61
62                     configurationService.update(mapProperties);
63                 });
64             } catch (IOException e) {
65                 LOG.debug("Failed to load {} configuration file. Error {}", OFConstants.CONFIG_FILE_ID, e);
66             }
67         });
68     }
69 }