336814874a2458b68da393e5359c45776b77b3c2
[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 package org.opendaylight.openflowplugin.impl.configuration;
9
10 import com.google.common.base.Verify;
11 import com.google.common.collect.ImmutableMap;
12 import java.util.ArrayList;
13 import java.util.HashMap;
14 import java.util.List;
15 import java.util.Map;
16 import java.util.function.Function;
17 import org.eclipse.jdt.annotation.NonNull;
18 import org.opendaylight.openflowplugin.api.openflow.configuration.ConfigurationListener;
19 import org.opendaylight.openflowplugin.api.openflow.configuration.ConfigurationProperty;
20 import org.opendaylight.openflowplugin.api.openflow.configuration.ConfigurationService;
21 import org.opendaylight.openflowplugin.api.openflow.configuration.ConfigurationServiceFactory;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflow.provider.config.rev160510.OpenflowProviderConfig;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 public class ConfigurationServiceFactoryImpl implements ConfigurationServiceFactory {
27     private static final Logger LOG = LoggerFactory.getLogger(ConfigurationServiceFactoryImpl.class);
28
29     @Override
30     public ConfigurationService newInstance(
31             final OpenflowProviderConfig providerConfig) {
32         return new ConfigurationServiceImpl(providerConfig);
33     }
34
35     private static final class ConfigurationServiceImpl implements ConfigurationService {
36         private final Map<String, String> propertyMap = new HashMap<>();
37         private final List<ConfigurationListener> listeners = new ArrayList<>();
38
39         ConfigurationServiceImpl(final OpenflowProviderConfig providerConfig) {
40             LOG.info("Loading properties from '{}' YANG file", OpenflowProviderConfig.QNAME);
41             update(ImmutableMap
42                     .<String, String>builder()
43                     .put(ConfigurationProperty.RPC_REQUESTS_QUOTA.toString(),
44                             providerConfig.getRpcRequestsQuota().getValue().toString())
45                     .put(ConfigurationProperty.GLOBAL_NOTIFICATION_QUOTA.toString(),
46                             providerConfig.getGlobalNotificationQuota().toString())
47                     .put(ConfigurationProperty.SWITCH_FEATURES_MANDATORY.toString(),
48                             providerConfig.getSwitchFeaturesMandatory().toString())
49                     .put(ConfigurationProperty.ENABLE_FLOW_REMOVED_NOTIFICATION.toString(),
50                             providerConfig.getEnableFlowRemovedNotification().toString())
51                     .put(ConfigurationProperty.IS_STATISTICS_RPC_ENABLED.toString(),
52                             providerConfig.getIsStatisticsRpcEnabled().toString())
53                     .put(ConfigurationProperty.BARRIER_COUNT_LIMIT.toString(),
54                             providerConfig.getBarrierCountLimit().getValue().toString())
55                     .put(ConfigurationProperty.BARRIER_INTERVAL_TIMEOUT_LIMIT.toString(),
56                             providerConfig.getBarrierIntervalTimeoutLimit().getValue().toString())
57                     .put(ConfigurationProperty.ECHO_REPLY_TIMEOUT.toString(),
58                             providerConfig.getEchoReplyTimeout().getValue().toString())
59                     .put(ConfigurationProperty.IS_STATISTICS_POLLING_ON.toString(),
60                             providerConfig.getIsStatisticsPollingOn().toString())
61                     .put(ConfigurationProperty.IS_TABLE_STATISTICS_POLLING_ON.toString(),
62                             providerConfig.getIsTableStatisticsPollingOn().toString())
63                     .put(ConfigurationProperty.IS_FLOW_STATISTICS_POLLING_ON.toString(),
64                             providerConfig.getIsFlowStatisticsPollingOn().toString())
65                     .put(ConfigurationProperty.IS_GROUP_STATISTICS_POLLING_ON.toString(),
66                             providerConfig.getIsGroupStatisticsPollingOn().toString())
67                     .put(ConfigurationProperty.IS_METER_STATISTICS_POLLING_ON.toString(),
68                             providerConfig.getIsMeterStatisticsPollingOn().toString())
69                     .put(ConfigurationProperty.IS_PORT_STATISTICS_POLLING_ON.toString(),
70                             providerConfig.getIsPortStatisticsPollingOn().toString())
71                     .put(ConfigurationProperty.IS_QUEUE_STATISTICS_POLLING_ON.toString(),
72                             providerConfig.getIsQueueStatisticsPollingOn().toString())
73                     .put(ConfigurationProperty.SKIP_TABLE_FEATURES.toString(),
74                             providerConfig.getSkipTableFeatures().toString())
75                     .put(ConfigurationProperty.BASIC_TIMER_DELAY.toString(),
76                             providerConfig.getBasicTimerDelay().getValue().toString())
77                     .put(ConfigurationProperty.MAXIMUM_TIMER_DELAY.toString(),
78                             providerConfig.getMaximumTimerDelay().getValue().toString())
79                     .put(ConfigurationProperty.USE_SINGLE_LAYER_SERIALIZATION.toString(),
80                             providerConfig.getUseSingleLayerSerialization().toString())
81                     .put(ConfigurationProperty.THREAD_POOL_MIN_THREADS.toString(),
82                             providerConfig.getThreadPoolMinThreads().toString())
83                     .put(ConfigurationProperty.THREAD_POOL_MAX_THREADS.toString(),
84                             providerConfig.getThreadPoolMaxThreads().getValue().toString())
85                     .put(ConfigurationProperty.THREAD_POOL_TIMEOUT.toString(),
86                             providerConfig.getThreadPoolTimeout().toString())
87                     .put(ConfigurationProperty.DEVICE_CONNECTION_RATE_LIMIT_PER_MIN.toString(),
88                             providerConfig.getDeviceConnectionRateLimitPerMin().toString())
89                     .put(ConfigurationProperty.DEVICE_CONNECTION_HOLD_TIME_IN_SECONDS.toString(),
90                             providerConfig.getDeviceConnectionHoldTimeInSeconds().toString())
91                     .put(ConfigurationProperty.DEVICE_DATASTORE_REMOVAL_DELAY.toString(),
92                             providerConfig.getDeviceDatastoreRemovalDelay().getValue().toString())
93                     .build());
94         }
95
96         @Override
97         public void update(@NonNull final Map<String, String> properties) {
98             properties.forEach((propertyName, newValue) -> {
99                 final String originalValue = propertyMap.get(propertyName);
100
101                 if (originalValue != null) {
102                     if (originalValue.equals(newValue)) {
103                         return;
104                     }
105
106                     LOG.info("{} configuration property was changed from '{}' to '{}'",
107                             propertyName,
108                             originalValue,
109                             newValue);
110                 } else {
111                     if (newValue == null) {
112                         return;
113                     }
114
115                     LOG.info("{} configuration property was changed to '{}'", propertyName, newValue);
116                 }
117
118                 propertyMap.put(propertyName, newValue);
119                 listeners.forEach(listener -> listener.onPropertyChanged(propertyName, newValue));
120             });
121         }
122
123         @NonNull
124         @Override
125         public <T> T getProperty(@NonNull final String key, @NonNull final Function<String, T> transformer) {
126             return transformer.apply(propertyMap.get(key));
127         }
128
129         @NonNull
130         @Override
131         public AutoCloseable registerListener(@NonNull final ConfigurationListener listener) {
132             Verify.verify(!listeners.contains(listener));
133             LOG.info("{} was registered as configuration listener to OpenFlowPlugin configuration service", listener);
134             listeners.add(listener);
135             propertyMap.forEach(listener::onPropertyChanged);
136             return () -> listeners.remove(listener);
137         }
138
139         @Override
140         public void close() {
141             propertyMap.clear();
142             listeners.clear();
143         }
144     }
145 }