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.role.RoleManager;
42 import org.opendaylight.openflowplugin.api.openflow.rpc.RpcManager;
43 import org.opendaylight.openflowplugin.api.openflow.statistics.StatisticsManager;
44 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageIntelligenceAgency;
45 import org.opendaylight.openflowplugin.extension.api.ExtensionConverterProviderKeeper;
46 import org.opendaylight.openflowplugin.extension.api.ExtensionConverterRegistrator;
47 import org.opendaylight.openflowplugin.extension.api.OpenFlowPluginExtensionRegistratorProvider;
48 import org.opendaylight.openflowplugin.extension.api.core.extension.ExtensionConverterManager;
49 import org.opendaylight.openflowplugin.impl.connection.ConnectionManagerImpl;
50 import org.opendaylight.openflowplugin.impl.device.DeviceManagerImpl;
51 import org.opendaylight.openflowplugin.impl.role.RoleManagerImpl;
52 import org.opendaylight.openflowplugin.impl.rpc.RpcManagerImpl;
53 import org.opendaylight.openflowplugin.impl.statistics.StatisticsManagerImpl;
54 import org.opendaylight.openflowplugin.impl.statistics.ofpspecific.MessageIntelligenceAgencyImpl;
55 import org.opendaylight.openflowplugin.impl.statistics.ofpspecific.MessageIntelligenceAgencyMXBean;
56 import org.opendaylight.openflowplugin.impl.util.TranslatorLibraryUtil;
57 import org.opendaylight.openflowplugin.openflow.md.core.ThreadPoolLoggingExecutor;
58 import org.opendaylight.openflowplugin.openflow.md.core.extension.ExtensionConverterManagerImpl;
59 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorManager;
60 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorManagerFactory;
61 import org.opendaylight.openflowplugin.openflow.md.core.session.OFSessionUtil;
62 import org.slf4j.Logger;
63 import org.slf4j.LoggerFactory;
65 public class OpenFlowPluginProviderImpl implements OpenFlowPluginProvider, OpenFlowPluginExtensionRegistratorProvider {
67 private static final Logger LOG = LoggerFactory.getLogger(OpenFlowPluginProviderImpl.class);
68 private static final MessageIntelligenceAgency messageIntelligenceAgency = new MessageIntelligenceAgencyImpl();
69 private static final int TICKS_PER_WHEEL = 500;
71 private static final long TICK_DURATION = 10;
73 private final HashedWheelTimer hashedWheelTimer = new HashedWheelTimer(TICK_DURATION, TimeUnit.MILLISECONDS, TICKS_PER_WHEEL);
75 private final int rpcRequestsQuota;
76 private final long globalNotificationQuota;
77 private final ConvertorManager convertorManager;
78 private long barrierInterval;
79 private int barrierCountLimit;
80 private long echoReplyTimeout;
81 private DeviceManager deviceManager;
82 private RoleManager roleManager;
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 isStatisticsPollingOff = false;
94 private boolean isStatisticsRpcEnabled;
95 private boolean isNotificationFlowRemovedOff = false;
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 isStatisticsPollingOff() {
123 return isStatisticsPollingOff;
127 public void setIsStatisticsPollingOff(final boolean isStatisticsPollingOff) {
128 this.isStatisticsPollingOff = isStatisticsPollingOff;
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 setNotificationFlowRemovedOff(boolean isNotificationFlowRemovedOff) {
176 this.isNotificationFlowRemovedOff = isNotificationFlowRemovedOff;
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 isNotificationFlowRemovedOff,
236 singletonServicesProvider,
237 notificationPublishService,
242 ((ExtensionConverterProviderKeeper) deviceManager).setExtensionConverterProvider(extensionConverterManager);
244 rpcManager = new RpcManagerImpl(rpcProviderRegistry, rpcRequestsQuota, extensionConverterManager, convertorManager, notificationPublishService);
245 roleManager = new RoleManagerImpl(dataBroker, hashedWheelTimer);
246 statisticsManager = new StatisticsManagerImpl(rpcProviderRegistry, isStatisticsPollingOff, hashedWheelTimer, convertorManager);
248 /* Initialization Phase ordering - OFP Device Context suite */
249 // CM -> DM -> SM -> RPC -> Role -> DM
250 connectionManager.setDeviceConnectedHandler(deviceManager);
251 deviceManager.setDeviceInitializationPhaseHandler(statisticsManager);
252 statisticsManager.setDeviceInitializationPhaseHandler(rpcManager);
253 rpcManager.setDeviceInitializationPhaseHandler(roleManager);
254 roleManager.setDeviceInitializationPhaseHandler(deviceManager);
256 /* Termination Phase ordering - OFP Device Context suite */
257 deviceManager.setDeviceTerminationPhaseHandler(rpcManager);
258 rpcManager.setDeviceTerminationPhaseHandler(statisticsManager);
259 statisticsManager.setDeviceTerminationPhaseHandler(roleManager);
260 roleManager.setDeviceTerminationPhaseHandler(deviceManager);
262 rpcManager.setStatisticsRpcEnabled(isStatisticsRpcEnabled);
264 TranslatorLibraryUtil.injectBasicTranslatorLibrary(deviceManager, convertorManager);
265 deviceManager.initialize();
267 startSwitchConnections();
271 public void update(Map<String,Object> props) {
272 LOG.debug("Update managed properties = {}", props.toString());
273 if(deviceManager != null && props.containsKey("notification-flow-removed-off")) {
274 deviceManager.setIsNotificationFlowRemovedOff(Boolean.valueOf(props.get("notification-flow-removed-off").toString()));
276 if(deviceManager != null && props.containsKey("skip-table-features")) {
277 deviceManager.setSkipTableFeatures(Boolean.valueOf(props.get("skip-table-features").toString()));
281 private static void registerMXBean(final MessageIntelligenceAgency messageIntelligenceAgency) {
282 final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
284 final String pathToMxBean = String.format("%s:type=%s",
285 MessageIntelligenceAgencyMXBean.class.getPackage().getName(),
286 MessageIntelligenceAgencyMXBean.class.getSimpleName());
287 final ObjectName name = new ObjectName(pathToMxBean);
288 mbs.registerMBean(messageIntelligenceAgency, name);
289 } catch (MalformedObjectNameException
290 | NotCompliantMBeanException
291 | MBeanRegistrationException
292 | InstanceAlreadyExistsException e) {
293 LOG.warn("Error registering MBean {}", e);
298 public void setNotificationProviderService(final NotificationService notificationProviderService) {
299 this.notificationProviderService = notificationProviderService;
303 public void setNotificationPublishService(final NotificationPublishService notificationPublishProviderService) {
304 this.notificationPublishService = notificationPublishProviderService;
308 public ExtensionConverterRegistrator getExtensionConverterRegistrator() {
309 return extensionConverterManager;
313 public void setIsStatisticsRpcEnabled(final boolean isStatisticsRpcEnabled) {
314 this.isStatisticsRpcEnabled = isStatisticsRpcEnabled;
318 public void close() throws Exception {
319 //TODO: consider wrapping each manager into try-catch
320 deviceManager.close();
322 statisticsManager.close();
324 // TODO: needs to close org.opendaylight.openflowplugin.impl.role.OpenflowOwnershipListener after RoleContexts are down
325 // TODO: must not be executed prior to all living RoleContexts have been closed (via closing living DeviceContexts)
328 // Manually shutdown all remaining running threads in pool
329 threadPool.shutdown();