2 * Copyright (c) 2018 Red Hat, Inc. and others. All rights reserved.
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
8 package org.opendaylight.openflowplugin.impl.configuration;
10 import java.io.IOException;
11 import java.util.Enumeration;
12 import java.util.HashMap;
14 import java.util.Optional;
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.service.cm.Configuration;
20 import org.osgi.service.cm.ConfigurationAdmin;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
24 // NOT @Singleton @Service - we do not want this OSGi specific implementation to be auto-discovered in a standalone env
25 public class ConfigurationServiceFactoryOsgiImpl extends ConfigurationServiceFactoryImpl {
27 private static final Logger LOG = LoggerFactory.getLogger(ConfigurationServiceFactoryOsgiImpl.class);
29 private final BundleContext bundleContext;
31 public ConfigurationServiceFactoryOsgiImpl(BundleContext bundleContext) {
32 this.bundleContext = bundleContext;
36 public ConfigurationService newInstance(OpenflowProviderConfig providerConfig) {
37 ConfigurationService cs = super.newInstance(providerConfig);
42 private void update(ConfigurationService configurationService) {
43 LOG.info("Loading configuration from '{}' configuration file", OFConstants.CONFIG_FILE_ID);
44 Optional.ofNullable(bundleContext.getServiceReference(ConfigurationAdmin.class)).ifPresent(serviceReference -> {
45 final ConfigurationAdmin configurationAdmin = bundleContext.getService(serviceReference);
48 final Configuration configuration = configurationAdmin.getConfiguration(OFConstants.CONFIG_FILE_ID);
50 Optional.ofNullable(configuration.getProperties()).ifPresent(properties -> {
51 final Enumeration<String> keys = properties.keys();
52 final Map<String, String> mapProperties = new HashMap<>(properties.size());
54 while (keys.hasMoreElements()) {
55 final String key = keys.nextElement();
56 final String value = properties.get(key).toString();
57 mapProperties.put(key, value);
60 configurationService.update(mapProperties);
62 } catch (IOException e) {
63 LOG.debug("Failed to load {} configuration file", OFConstants.CONFIG_FILE_ID);