2 * Copyright (c) 2015 Cisco Systems, 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
9 package org.opendaylight.openflowplugin.impl;
12 import com.google.common.base.Preconditions;
13 import com.google.common.util.concurrent.FutureCallback;
14 import com.google.common.util.concurrent.Futures;
15 import com.google.common.util.concurrent.ListenableFuture;
16 import io.netty.util.HashedWheelTimer;
17 import java.lang.management.ManagementFactory;
18 import java.util.ArrayList;
19 import java.util.Collection;
20 import java.util.List;
22 import java.util.concurrent.SynchronousQueue;
23 import java.util.concurrent.ThreadPoolExecutor;
24 import java.util.concurrent.TimeUnit;
25 import javax.annotation.Nonnull;
26 import javax.management.InstanceAlreadyExistsException;
27 import javax.management.MBeanRegistrationException;
28 import javax.management.MBeanServer;
29 import javax.management.MalformedObjectNameException;
30 import javax.management.NotCompliantMBeanException;
31 import javax.management.ObjectName;
32 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
33 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
34 import org.opendaylight.controller.md.sal.binding.api.NotificationService;
35 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
36 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
37 import org.opendaylight.openflowjava.protocol.spi.connection.SwitchConnectionProvider;
38 import org.opendaylight.openflowplugin.api.openflow.OpenFlowPluginProvider;
39 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionManager;
40 import org.opendaylight.openflowplugin.api.openflow.device.DeviceManager;
41 import org.opendaylight.openflowplugin.api.openflow.rpc.RpcManager;
42 import org.opendaylight.openflowplugin.api.openflow.statistics.StatisticsManager;
43 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageIntelligenceAgency;
44 import org.opendaylight.openflowplugin.extension.api.ExtensionConverterProviderKeeper;
45 import org.opendaylight.openflowplugin.extension.api.ExtensionConverterRegistrator;
46 import org.opendaylight.openflowplugin.extension.api.OpenFlowPluginExtensionRegistratorProvider;
47 import org.opendaylight.openflowplugin.extension.api.core.extension.ExtensionConverterManager;
48 import org.opendaylight.openflowplugin.impl.connection.ConnectionManagerImpl;
49 import org.opendaylight.openflowplugin.impl.device.DeviceManagerImpl;
50 import org.opendaylight.openflowplugin.impl.rpc.RpcManagerImpl;
51 import org.opendaylight.openflowplugin.impl.statistics.StatisticsManagerImpl;
52 import org.opendaylight.openflowplugin.impl.statistics.ofpspecific.MessageIntelligenceAgencyImpl;
53 import org.opendaylight.openflowplugin.impl.statistics.ofpspecific.MessageIntelligenceAgencyMXBean;
54 import org.opendaylight.openflowplugin.impl.util.TranslatorLibraryUtil;
55 import org.opendaylight.openflowplugin.openflow.md.core.ThreadPoolLoggingExecutor;
56 import org.opendaylight.openflowplugin.openflow.md.core.extension.ExtensionConverterManagerImpl;
57 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorManager;
58 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorManagerFactory;
59 import org.opendaylight.openflowplugin.openflow.md.core.session.OFSessionUtil;
60 import org.slf4j.Logger;
61 import org.slf4j.LoggerFactory;
63 public class OpenFlowPluginProviderImpl implements OpenFlowPluginProvider, OpenFlowPluginExtensionRegistratorProvider {
65 private static final Logger LOG = LoggerFactory.getLogger(OpenFlowPluginProviderImpl.class);
66 private static final MessageIntelligenceAgency messageIntelligenceAgency = new MessageIntelligenceAgencyImpl();
67 private static final int TICKS_PER_WHEEL = 500;
69 private static final long TICK_DURATION = 10;
70 private static final Integer DEFAULT_BARRIER_COUNT = 25600;
71 private static final Long DEFAULT_ECHO_TIMEOUT = 2000L;
72 private static final Long DEFAULT_BARRIER_TIMEOUT = 500L;
74 private final HashedWheelTimer hashedWheelTimer = new HashedWheelTimer(TICK_DURATION, TimeUnit.MILLISECONDS, TICKS_PER_WHEEL);
76 private final int rpcRequestsQuota;
77 private final long globalNotificationQuota;
78 private final ConvertorManager convertorManager;
79 private long barrierInterval;
80 private int barrierCountLimit;
81 private long echoReplyTimeout;
82 private DeviceManager deviceManager;
83 private RpcManager rpcManager;
84 private RpcProviderRegistry rpcProviderRegistry;
85 private StatisticsManager statisticsManager;
86 private ConnectionManager connectionManager;
87 private NotificationService notificationProviderService;
88 private NotificationPublishService notificationPublishService;
89 private ExtensionConverterManager extensionConverterManager;
90 private DataBroker dataBroker;
91 private Collection<SwitchConnectionProvider> switchConnectionProviders;
92 private boolean switchFeaturesMandatory = false;
93 private boolean isStatisticsPollingOn = true;
94 private boolean isStatisticsRpcEnabled;
95 private boolean isFlowRemovedNotificationOn = true;
96 private boolean skipTableFeatures = true;
98 private final ThreadPoolExecutor threadPool;
99 private ClusterSingletonServiceProvider singletonServicesProvider;
101 public OpenFlowPluginProviderImpl(final long rpcRequestsQuota,
102 final long globalNotificationQuota,
103 final int threadPoolMinThreads,
104 final int threadPoolMaxThreads,
105 final long threadPoolTimeout) {
106 Preconditions.checkArgument(rpcRequestsQuota > 0 && rpcRequestsQuota <= Integer.MAX_VALUE, "rpcRequestQuota has to be in range <1,%s>", Integer.MAX_VALUE);
107 this.rpcRequestsQuota = (int) rpcRequestsQuota;
108 this.globalNotificationQuota = Preconditions.checkNotNull(globalNotificationQuota);
110 // Creates a thread pool that creates new threads as needed, but will reuse previously
111 // constructed threads when they are available.
112 // Threads that have not been used for x seconds are terminated and removed from the cache.
113 threadPool = new ThreadPoolLoggingExecutor(
114 Preconditions.checkNotNull(threadPoolMinThreads),
115 Preconditions.checkNotNull(threadPoolMaxThreads),
116 Preconditions.checkNotNull(threadPoolTimeout), TimeUnit.SECONDS,
117 new SynchronousQueue<>(), "ofppool");
118 convertorManager = ConvertorManagerFactory.createDefaultManager();
122 public boolean isStatisticsPollingOn() {
123 return isStatisticsPollingOn;
127 public void setStatisticsPollingOn(final boolean isStatisticsPollingOn) {
128 this.isStatisticsPollingOn = isStatisticsPollingOn;
131 private void startSwitchConnections() {
132 final List<ListenableFuture<Boolean>> starterChain = new ArrayList<>(switchConnectionProviders.size());
133 for (final SwitchConnectionProvider switchConnectionPrv : switchConnectionProviders) {
134 switchConnectionPrv.setSwitchConnectionHandler(connectionManager);
135 final ListenableFuture<Boolean> isOnlineFuture = switchConnectionPrv.startup();
136 starterChain.add(isOnlineFuture);
139 final ListenableFuture<List<Boolean>> srvStarted = Futures.allAsList(starterChain);
140 Futures.addCallback(srvStarted, new FutureCallback<List<Boolean>>() {
142 public void onSuccess(final List<Boolean> result) {
143 LOG.info("All switchConnectionProviders are up and running ({}).",
148 public void onFailure(@Nonnull final Throwable t) {
149 LOG.warn("Some switchConnectionProviders failed to start.", t);
155 public boolean isSwitchFeaturesMandatory() {
156 return switchFeaturesMandatory;
160 public void setBarrierCountLimit(final int barrierCountLimit) {
161 this.barrierCountLimit = barrierCountLimit;
165 public void setBarrierInterval(final long barrierTimeoutLimit) {
166 this.barrierInterval = barrierTimeoutLimit;
170 public void setEchoReplyTimeout(final long echoReplyTimeout) {
171 this.echoReplyTimeout = echoReplyTimeout;
175 public void setFlowRemovedNotification(boolean isFlowRemovedNotificationOn) {
176 this.isFlowRemovedNotificationOn = this.isFlowRemovedNotificationOn;
180 public void setClusteringSingletonServicesProvider(ClusterSingletonServiceProvider singletonServicesProvider) {
181 this.singletonServicesProvider = singletonServicesProvider;
185 public void setSkipTableFeatures(final boolean skipTableFeatures){
186 this.skipTableFeatures = skipTableFeatures;
190 public void setSwitchFeaturesMandatory(final boolean switchFeaturesMandatory) {
191 this.switchFeaturesMandatory = switchFeaturesMandatory;
194 public static MessageIntelligenceAgency getMessageIntelligenceAgency() {
195 return OpenFlowPluginProviderImpl.messageIntelligenceAgency;
199 public void setSwitchConnectionProviders(final Collection<SwitchConnectionProvider> switchConnectionProviders) {
200 this.switchConnectionProviders = switchConnectionProviders;
204 public void setDataBroker(final DataBroker dataBroker) {
205 this.dataBroker = dataBroker;
209 public void setRpcProviderRegistry(final RpcProviderRegistry rpcProviderRegistry) {
210 this.rpcProviderRegistry = rpcProviderRegistry;
214 public void initialize() {
215 Preconditions.checkNotNull(dataBroker, "missing data broker");
216 Preconditions.checkNotNull(rpcProviderRegistry, "missing RPC provider registry");
217 Preconditions.checkNotNull(notificationProviderService, "missing notification provider service");
218 Preconditions.checkNotNull(singletonServicesProvider, "missing singleton services provider");
220 extensionConverterManager = new ExtensionConverterManagerImpl();
221 // TODO: copied from OpenFlowPluginProvider (Helium) misusesing the old way of distributing extension converters
222 // TODO: rewrite later!
223 OFSessionUtil.getSessionManager().setExtensionConverterProvider(extensionConverterManager);
225 connectionManager = new ConnectionManagerImpl(echoReplyTimeout, threadPool);
227 registerMXBean(messageIntelligenceAgency);
229 deviceManager = new DeviceManagerImpl(dataBroker,
230 globalNotificationQuota,
231 switchFeaturesMandatory,
234 getMessageIntelligenceAgency(),
235 isFlowRemovedNotificationOn,
236 singletonServicesProvider,
237 notificationPublishService,
242 ((ExtensionConverterProviderKeeper) deviceManager).setExtensionConverterProvider(extensionConverterManager);
244 rpcManager = new RpcManagerImpl(rpcProviderRegistry, rpcRequestsQuota, extensionConverterManager, convertorManager, notificationPublishService);
245 statisticsManager = new StatisticsManagerImpl(rpcProviderRegistry, isStatisticsPollingOn, hashedWheelTimer, convertorManager);
247 /* Initialization Phase ordering - OFP Device Context suite */
248 // CM -> DM -> SM -> RPC -> Role -> DM
249 connectionManager.setDeviceConnectedHandler(deviceManager);
250 deviceManager.setDeviceInitializationPhaseHandler(statisticsManager);
251 statisticsManager.setDeviceInitializationPhaseHandler(rpcManager);
252 rpcManager.setDeviceInitializationPhaseHandler(deviceManager);
254 /* Termination Phase ordering - OFP Device Context suite */
255 deviceManager.setDeviceTerminationPhaseHandler(rpcManager);
256 rpcManager.setDeviceTerminationPhaseHandler(statisticsManager);
257 statisticsManager.setDeviceTerminationPhaseHandler(deviceManager);
259 rpcManager.setStatisticsRpcEnabled(isStatisticsRpcEnabled);
261 TranslatorLibraryUtil.injectBasicTranslatorLibrary(deviceManager, convertorManager);
262 deviceManager.initialize();
264 startSwitchConnections();
268 public void update(Map<String,Object> props) {
269 LOG.debug("Update managed properties = {}", props.toString());
271 if(deviceManager != null) {
272 if (props.containsKey("notification-flow-removed-off")) {
273 deviceManager.setFlowRemovedNotificationOn(Boolean.valueOf(props.get("enable-flow-removed-notification").toString()));
275 if (props.containsKey("skip-table-features")) {
276 deviceManager.setSkipTableFeatures(Boolean.valueOf(props.get("skip-table-features").toString()));
278 if (props.containsKey("barrier-count-limit")) {
280 deviceManager.setBarrierCountLimit(Integer.valueOf(props.get("barrier-count-limit").toString()));
281 } catch (NumberFormatException ex) {
282 deviceManager.setBarrierCountLimit(DEFAULT_BARRIER_COUNT);
285 if (props.containsKey("barrier-interval-timeout-limit")){
287 deviceManager.setBarrierInterval(Long.valueOf(props.get("barrier-interval-timeout-limit").toString()));
288 } catch (NumberFormatException ex) {
289 deviceManager.setBarrierInterval(DEFAULT_BARRIER_TIMEOUT);
294 if(rpcManager != null && props.containsKey("is-statistics-rpc-enabled")){
295 rpcManager.setStatisticsRpcEnabled(Boolean.valueOf((props.get("is-statistics-rpc-enabled").toString())));
298 if (connectionManager != null && props.containsKey("echo-reply-timeout") ){
300 connectionManager.setEchoReplyTimeout(Long.valueOf(props.get("echo-reply-timeout").toString()));
301 }catch (NumberFormatException ex){
302 connectionManager.setEchoReplyTimeout(DEFAULT_ECHO_TIMEOUT);
306 if(statisticsManager != null && props.containsKey("is-statistics-polling-on")){
307 statisticsManager.setIsStatisticsPollingOn(Boolean.valueOf(props.get("is-statistics-polling-on").toString()));
312 private static void registerMXBean(final MessageIntelligenceAgency messageIntelligenceAgency) {
313 final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
315 final String pathToMxBean = String.format("%s:type=%s",
316 MessageIntelligenceAgencyMXBean.class.getPackage().getName(),
317 MessageIntelligenceAgencyMXBean.class.getSimpleName());
318 final ObjectName name = new ObjectName(pathToMxBean);
319 mbs.registerMBean(messageIntelligenceAgency, name);
320 } catch (MalformedObjectNameException
321 | NotCompliantMBeanException
322 | MBeanRegistrationException
323 | InstanceAlreadyExistsException e) {
324 LOG.warn("Error registering MBean {}", e);
329 public void setNotificationProviderService(final NotificationService notificationProviderService) {
330 this.notificationProviderService = notificationProviderService;
334 public void setNotificationPublishService(final NotificationPublishService notificationPublishProviderService) {
335 this.notificationPublishService = notificationPublishProviderService;
339 public ExtensionConverterRegistrator getExtensionConverterRegistrator() {
340 return extensionConverterManager;
344 public void setIsStatisticsRpcEnabled(final boolean isStatisticsRpcEnabled) {
345 this.isStatisticsRpcEnabled = isStatisticsRpcEnabled;
349 public void close() throws Exception {
350 //TODO: consider wrapping each manager into try-catch
351 deviceManager.close();
353 statisticsManager.close();
355 // Manually shutdown all remaining running threads in pool
356 threadPool.shutdown();