3ec7cb8a7aa1aeae864c8cffe0639dc2944ce7de
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / configuration / ConfigurationServiceFactoryImpl.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies s.r.o. 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.openflowplugin.impl.configuration;
10
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;
18 import java.util.Map;
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;
34
35 public class ConfigurationServiceFactoryImpl implements ConfigurationServiceFactory {
36     private static final Logger LOG = LoggerFactory.getLogger(ConfigurationServiceFactory.class);
37
38     @Override
39     public ConfigurationService newInstance(final OpenflowProviderConfig providerConfig, final BundleContext bundleContext) {
40         return new ConfigurationServiceImpl(providerConfig, bundleContext);
41     }
42
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<>();
46
47         ConfigurationServiceImpl(final OpenflowProviderConfig providerConfig, final BundleContext bundleContext) {
48             LOG.info("Loading properties from '{}' YANG file", OpenflowProviderConfig.QNAME);
49             update(ImmutableMap
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())
85                     .build());
86
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);
90
91                 try {
92                     final Configuration configuration = configurationAdmin.getConfiguration(OFConstants.CONFIG_FILE_ID);
93
94                     Optional.ofNullable(configuration.getProperties()).ifPresent(properties -> {
95                         final Enumeration<String> keys = properties.keys();
96                         final Map<String, String> mapProperties = new HashMap<>(properties.size());
97
98                         while (keys.hasMoreElements()) {
99                             final String key = keys.nextElement();
100                             final String value = properties.get(key).toString();
101                             mapProperties.put(key, value);
102                         }
103
104                         update(mapProperties);
105                     });
106                 } catch (IOException e) {
107                     LOG.debug("Failed to load {} configuration file. Error {}", OFConstants.CONFIG_FILE_ID, e);
108                 }
109             });
110         }
111
112         @Override
113         public void update(@Nonnull final Map<String, String> properties) {
114             properties.forEach((propertyName, newValue) -> {
115                 final String originalValue = propertyMap.get(propertyName);
116
117                 if (Objects.nonNull(originalValue)) {
118                     if (originalValue.equals(newValue)) {
119                         return;
120                     }
121
122                     LOG.info("{} configuration property was changed from '{}' to '{}'", propertyName, originalValue, newValue);
123                 } else {
124                     if (Objects.isNull(newValue)) {
125                         return;
126                     }
127
128                     LOG.info("{} configuration property was changed to '{}'", propertyName, newValue);
129                 }
130
131                 propertyMap.put(propertyName, newValue);
132                 listeners.forEach(listener -> listener.onPropertyChanged(propertyName, newValue));
133             });
134         }
135
136         @Nonnull
137         @Override
138         public <T> T getProperty(@Nonnull final String key, @Nonnull final Function<String, T> transformer) {
139             return transformer.apply(propertyMap.get(key));
140         }
141
142         @Nonnull
143         @Override
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);
150         }
151
152         @Override
153         public void close() throws Exception {
154             propertyMap.clear();
155             listeners.clear();
156         }
157     }
158 }