2 * Copyright (c) 2017 Pantheon Technologies s.r.o. 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
9 package org.opendaylight.openflowplugin.impl.configuration;
11 import com.google.common.base.Verify;
12 import com.google.common.collect.ImmutableMap;
13 import java.io.IOException;
14 import java.util.ArrayList;
15 import java.util.Enumeration;
16 import java.util.HashMap;
17 import java.util.List;
19 import java.util.Objects;
20 import java.util.Optional;
21 import java.util.function.Function;
22 import javax.annotation.Nonnull;
23 import org.opendaylight.openflowplugin.api.OFConstants;
24 import org.opendaylight.openflowplugin.api.openflow.configuration.ConfigurationListener;
25 import org.opendaylight.openflowplugin.api.openflow.configuration.ConfigurationProperty;
26 import org.opendaylight.openflowplugin.api.openflow.configuration.ConfigurationService;
27 import org.opendaylight.openflowplugin.api.openflow.configuration.ConfigurationServiceFactory;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflow.provider.config.rev160510.OpenflowProviderConfig;
29 import org.osgi.framework.BundleContext;
30 import org.osgi.service.cm.Configuration;
31 import org.osgi.service.cm.ConfigurationAdmin;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
35 public class ConfigurationServiceFactoryImpl implements ConfigurationServiceFactory {
36 private static final Logger LOG = LoggerFactory.getLogger(ConfigurationServiceFactory.class);
39 public ConfigurationService newInstance(final OpenflowProviderConfig providerConfig, final BundleContext bundleContext) {
40 return new ConfigurationServiceImpl(providerConfig, bundleContext);
43 private static final class ConfigurationServiceImpl implements ConfigurationService {
44 private final Map<String, String> propertyMap = new HashMap<>();
45 private final List<ConfigurationListener> listeners = new ArrayList<>();
47 ConfigurationServiceImpl(final OpenflowProviderConfig providerConfig, final BundleContext bundleContext) {
48 LOG.info("Loading properties from '{}' YANG file", OpenflowProviderConfig.QNAME);
50 .<String, String>builder()
51 .put(ConfigurationProperty.RPC_REQUESTS_QUOTA.toString(),
52 providerConfig.getRpcRequestsQuota().getValue().toString())
53 .put(ConfigurationProperty.GLOBAL_NOTIFICATION_QUOTA.toString(),
54 providerConfig.getGlobalNotificationQuota().toString())
55 .put(ConfigurationProperty.SWITCH_FEATURES_MANDATORY.toString(),
56 providerConfig.isSwitchFeaturesMandatory().toString())
57 .put(ConfigurationProperty.ENABLE_FLOW_REMOVED_NOTIFICATION.toString(),
58 providerConfig.isEnableFlowRemovedNotification().toString())
59 .put(ConfigurationProperty.IS_STATISTICS_RPC_ENABLED.toString(),
60 providerConfig.isIsStatisticsRpcEnabled().toString())
61 .put(ConfigurationProperty.BARRIER_COUNT_LIMIT.toString(),
62 providerConfig.getBarrierCountLimit().getValue().toString())
63 .put(ConfigurationProperty.BARRIER_INTERVAL_TIMEOUT_LIMIT.toString(),
64 providerConfig.getBarrierIntervalTimeoutLimit().getValue().toString())
65 .put(ConfigurationProperty.ECHO_REPLY_TIMEOUT.toString(),
66 providerConfig.getEchoReplyTimeout().getValue().toString())
67 .put(ConfigurationProperty.IS_STATISTICS_POLLING_ON.toString(),
68 providerConfig.isIsStatisticsPollingOn().toString())
69 .put(ConfigurationProperty.SKIP_TABLE_FEATURES.toString(),
70 providerConfig.isSkipTableFeatures().toString())
71 .put(ConfigurationProperty.BASIC_TIMER_DELAY.toString(),
72 providerConfig.getBasicTimerDelay().getValue().toString())
73 .put(ConfigurationProperty.MAXIMUM_TIMER_DELAY.toString(),
74 providerConfig.getMaximumTimerDelay().getValue().toString())
75 .put(ConfigurationProperty.USE_SINGLE_LAYER_SERIALIZATION.toString(),
76 providerConfig.isUseSingleLayerSerialization().toString())
77 .put(ConfigurationProperty.THREAD_POOL_MIN_THREADS.toString(),
78 providerConfig.getThreadPoolMinThreads().toString())
79 .put(ConfigurationProperty.THREAD_POOL_MAX_THREADS.toString(),
80 providerConfig.getThreadPoolMaxThreads().getValue().toString())
81 .put(ConfigurationProperty.THREAD_POOL_TIMEOUT.toString(),
82 providerConfig.getThreadPoolTimeout().toString())
83 .put(ConfigurationProperty.USING_RECONCILIATION_FRAMEWORK.toString(),
84 providerConfig.isUseSingleLayerSerialization().toString())
87 LOG.info("Loading configuration from '{}' configuration file", OFConstants.CONFIG_FILE_ID);
88 Optional.ofNullable(bundleContext.getServiceReference(ConfigurationAdmin.class.getName())).ifPresent(serviceReference -> {
89 final ConfigurationAdmin configurationAdmin = (ConfigurationAdmin) bundleContext.getService(serviceReference);
92 final Configuration configuration = configurationAdmin.getConfiguration(OFConstants.CONFIG_FILE_ID);
94 Optional.ofNullable(configuration.getProperties()).ifPresent(properties -> {
95 final Enumeration<String> keys = properties.keys();
96 final Map<String, String> mapProperties = new HashMap<>(properties.size());
98 while (keys.hasMoreElements()) {
99 final String key = keys.nextElement();
100 final String value = properties.get(key).toString();
101 mapProperties.put(key, value);
104 update(mapProperties);
106 } catch (IOException e) {
107 LOG.debug("Failed to load {} configuration file. Error {}", OFConstants.CONFIG_FILE_ID, e);
113 public void update(@Nonnull final Map<String, String> properties) {
114 properties.forEach((propertyName, newValue) -> {
115 final String originalValue = propertyMap.get(propertyName);
117 if (Objects.nonNull(originalValue)) {
118 if (originalValue.equals(newValue)) {
122 LOG.info("{} configuration property was changed from '{}' to '{}'", propertyName, originalValue, newValue);
124 if (Objects.isNull(newValue)) {
128 LOG.info("{} configuration property was changed to '{}'", propertyName, newValue);
131 propertyMap.put(propertyName, newValue);
132 listeners.forEach(listener -> listener.onPropertyChanged(propertyName, newValue));
138 public <T> T getProperty(@Nonnull final String key, @Nonnull final Function<String, T> transformer) {
139 return transformer.apply(propertyMap.get(key));
144 public AutoCloseable registerListener(@Nonnull final ConfigurationListener listener) {
145 Verify.verify(!listeners.contains(listener));
146 LOG.info("{} was registered as configuration listener to OpenFlowPlugin configuration service", listener);
147 listeners.add(listener);
148 propertyMap.forEach(listener::onPropertyChanged);
149 return () -> listeners.remove(listener);
153 public void close() throws Exception {