OPNFLWPLUG-983 Group and flow removal stats are not reported in order
[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(
40             final OpenflowProviderConfig providerConfig,
41             final BundleContext bundleContext) {
42         return new ConfigurationServiceImpl(providerConfig, bundleContext);
43     }
44
45     private static final class ConfigurationServiceImpl implements ConfigurationService {
46         private final Map<String, String> propertyMap = new HashMap<>();
47         private final List<ConfigurationListener> listeners = new ArrayList<>();
48
49         ConfigurationServiceImpl(final OpenflowProviderConfig providerConfig, final BundleContext bundleContext) {
50             LOG.info("Loading properties from '{}' YANG file", OpenflowProviderConfig.QNAME);
51             update(ImmutableMap
52                     .<String, String>builder()
53                     .put(ConfigurationProperty.RPC_REQUESTS_QUOTA.toString(),
54                             providerConfig.getRpcRequestsQuota().getValue().toString())
55                     .put(ConfigurationProperty.GLOBAL_NOTIFICATION_QUOTA.toString(),
56                             providerConfig.getGlobalNotificationQuota().toString())
57                     .put(ConfigurationProperty.SWITCH_FEATURES_MANDATORY.toString(),
58                             providerConfig.isSwitchFeaturesMandatory().toString())
59                     .put(ConfigurationProperty.ENABLE_FLOW_REMOVED_NOTIFICATION.toString(),
60                             providerConfig.isEnableFlowRemovedNotification().toString())
61                     .put(ConfigurationProperty.IS_STATISTICS_RPC_ENABLED.toString(),
62                             providerConfig.isIsStatisticsRpcEnabled().toString())
63                     .put(ConfigurationProperty.BARRIER_COUNT_LIMIT.toString(),
64                             providerConfig.getBarrierCountLimit().getValue().toString())
65                     .put(ConfigurationProperty.BARRIER_INTERVAL_TIMEOUT_LIMIT.toString(),
66                             providerConfig.getBarrierIntervalTimeoutLimit().getValue().toString())
67                     .put(ConfigurationProperty.ECHO_REPLY_TIMEOUT.toString(),
68                             providerConfig.getEchoReplyTimeout().getValue().toString())
69                     .put(ConfigurationProperty.IS_STATISTICS_POLLING_ON.toString(),
70                             providerConfig.isIsStatisticsPollingOn().toString())
71                     .put(ConfigurationProperty.SKIP_TABLE_FEATURES.toString(),
72                             providerConfig.isSkipTableFeatures().toString())
73                     .put(ConfigurationProperty.BASIC_TIMER_DELAY.toString(),
74                             providerConfig.getBasicTimerDelay().getValue().toString())
75                     .put(ConfigurationProperty.MAXIMUM_TIMER_DELAY.toString(),
76                             providerConfig.getMaximumTimerDelay().getValue().toString())
77                     .put(ConfigurationProperty.USE_SINGLE_LAYER_SERIALIZATION.toString(),
78                             providerConfig.isUseSingleLayerSerialization().toString())
79                     .put(ConfigurationProperty.THREAD_POOL_MIN_THREADS.toString(),
80                             providerConfig.getThreadPoolMinThreads().toString())
81                     .put(ConfigurationProperty.THREAD_POOL_MAX_THREADS.toString(),
82                             providerConfig.getThreadPoolMaxThreads().getValue().toString())
83                     .put(ConfigurationProperty.THREAD_POOL_TIMEOUT.toString(),
84                             providerConfig.getThreadPoolTimeout().toString())
85                     .build());
86
87             LOG.info("Loading configuration from '{}' configuration file", OFConstants.CONFIG_FILE_ID);
88             Optional.ofNullable(bundleContext.getServiceReference(ConfigurationAdmin.class))
89                     .ifPresent(serviceReference -> {
90                         final ConfigurationAdmin configurationAdmin = bundleContext.getService(serviceReference);
91
92                         try {
93                             final Configuration configuration
94                                     = configurationAdmin.getConfiguration(OFConstants.CONFIG_FILE_ID);
95
96                             Optional.ofNullable(configuration.getProperties()).ifPresent(properties -> {
97                                 final Enumeration<String> keys = properties.keys();
98                                 final Map<String, String> mapProperties = new HashMap<>(properties.size());
99
100                                 while (keys.hasMoreElements()) {
101                                     final String key = keys.nextElement();
102                                     final String value = properties.get(key).toString();
103                                     mapProperties.put(key, value);
104                                 }
105
106                                 update(mapProperties);
107                             });
108                         } catch (IOException e) {
109                             LOG.debug("Failed to load {} configuration file. Error {}", OFConstants.CONFIG_FILE_ID, e);
110                         }
111                     });
112         }
113
114         @Override
115         public void update(@Nonnull final Map<String, String> properties) {
116             properties.forEach((propertyName, newValue) -> {
117                 final String originalValue = propertyMap.get(propertyName);
118
119                 if (Objects.nonNull(originalValue)) {
120                     if (originalValue.equals(newValue)) {
121                         return;
122                     }
123
124                     LOG.info("{} configuration property was changed from '{}' to '{}'",
125                             propertyName,
126                             originalValue,
127                             newValue);
128                 } else {
129                     if (Objects.isNull(newValue)) {
130                         return;
131                     }
132
133                     LOG.info("{} configuration property was changed to '{}'", propertyName, newValue);
134                 }
135
136                 propertyMap.put(propertyName, newValue);
137                 listeners.forEach(listener -> listener.onPropertyChanged(propertyName, newValue));
138             });
139         }
140
141         @Nonnull
142         @Override
143         public <T> T getProperty(@Nonnull final String key, @Nonnull final Function<String, T> transformer) {
144             return transformer.apply(propertyMap.get(key));
145         }
146
147         @Nonnull
148         @Override
149         public AutoCloseable registerListener(@Nonnull final ConfigurationListener listener) {
150             Verify.verify(!listeners.contains(listener));
151             LOG.info("{} was registered as configuration listener to OpenFlowPlugin configuration service", listener);
152             listeners.add(listener);
153             propertyMap.forEach(listener::onPropertyChanged);
154             return () -> listeners.remove(listener);
155         }
156
157         @Override
158         public void close() throws Exception {
159             propertyMap.clear();
160             listeners.clear();
161         }
162     }
163 }