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;
11 import com.google.common.util.concurrent.FutureCallback;
12 import com.google.common.util.concurrent.Futures;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import io.netty.util.HashedWheelTimer;
15 import io.netty.util.Timer;
16 import java.lang.management.ManagementFactory;
17 import java.util.Collection;
18 import java.util.List;
19 import java.util.Objects;
20 import java.util.concurrent.ExecutionException;
21 import java.util.concurrent.SynchronousQueue;
22 import java.util.concurrent.ThreadPoolExecutor;
23 import java.util.concurrent.TimeUnit;
24 import java.util.concurrent.TimeoutException;
25 import java.util.stream.Collectors;
26 import javax.annotation.Nonnull;
27 import javax.management.InstanceAlreadyExistsException;
28 import javax.management.InstanceNotFoundException;
29 import javax.management.MBeanRegistrationException;
30 import javax.management.MBeanServer;
31 import javax.management.MalformedObjectNameException;
32 import javax.management.NotCompliantMBeanException;
33 import javax.management.ObjectName;
34 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
35 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
36 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
37 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
38 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
39 import org.opendaylight.openflowjava.protocol.spi.connection.SwitchConnectionProvider;
40 import org.opendaylight.openflowplugin.api.openflow.OpenFlowPluginProvider;
41 import org.opendaylight.openflowplugin.api.openflow.configuration.ConfigurationService;
42 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionManager;
43 import org.opendaylight.openflowplugin.api.openflow.device.DeviceManager;
44 import org.opendaylight.openflowplugin.api.openflow.mastership.MastershipChangeServiceManager;
45 import org.opendaylight.openflowplugin.api.openflow.role.RoleManager;
46 import org.opendaylight.openflowplugin.api.openflow.rpc.RpcManager;
47 import org.opendaylight.openflowplugin.api.openflow.statistics.StatisticsManager;
48 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageIntelligenceAgency;
49 import org.opendaylight.openflowplugin.extension.api.ExtensionConverterProviderKeeper;
50 import org.opendaylight.openflowplugin.extension.api.ExtensionConverterRegistrator;
51 import org.opendaylight.openflowplugin.extension.api.OpenFlowPluginExtensionRegistratorProvider;
52 import org.opendaylight.openflowplugin.extension.api.core.extension.ExtensionConverterManager;
53 import org.opendaylight.openflowplugin.impl.configuration.OpenFlowProviderConfigImpl;
54 import org.opendaylight.openflowplugin.impl.connection.ConnectionManagerImpl;
55 import org.opendaylight.openflowplugin.impl.device.DeviceManagerImpl;
56 import org.opendaylight.openflowplugin.impl.device.initialization.DeviceInitializerProvider;
57 import org.opendaylight.openflowplugin.impl.device.initialization.DeviceInitializerProviderFactory;
58 import org.opendaylight.openflowplugin.impl.lifecycle.ContextChainHolderImpl;
59 import org.opendaylight.openflowplugin.impl.protocol.deserialization.DeserializerInjector;
60 import org.opendaylight.openflowplugin.impl.protocol.serialization.SerializerInjector;
61 import org.opendaylight.openflowplugin.impl.role.RoleManagerImpl;
62 import org.opendaylight.openflowplugin.impl.rpc.RpcManagerImpl;
63 import org.opendaylight.openflowplugin.impl.statistics.StatisticsManagerImpl;
64 import org.opendaylight.openflowplugin.impl.statistics.ofpspecific.MessageIntelligenceAgencyImpl;
65 import org.opendaylight.openflowplugin.impl.statistics.ofpspecific.MessageIntelligenceAgencyMXBean;
66 import org.opendaylight.openflowplugin.impl.util.TranslatorLibraryUtil;
67 import org.opendaylight.openflowplugin.impl.util.ThreadPoolLoggingExecutor;
68 import org.opendaylight.openflowplugin.openflow.md.core.extension.ExtensionConverterManagerImpl;
69 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorManager;
70 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorManagerFactory;
71 import org.opendaylight.openflowplugin.openflow.md.core.session.OFSessionUtil;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflow.provider.config.rev160510.OpenflowProviderConfig;
73 import org.slf4j.Logger;
74 import org.slf4j.LoggerFactory;
76 public class OpenFlowPluginProviderImpl implements
77 OpenFlowPluginProvider,
78 OpenFlowPluginExtensionRegistratorProvider {
80 private static final Logger LOG = LoggerFactory.getLogger(OpenFlowPluginProviderImpl.class);
82 private static final int TICKS_PER_WHEEL = 500; // 0.5 sec.
83 private static final long TICK_DURATION = 10;
84 private static final String POOL_NAME = "ofppool";
86 private static final MessageIntelligenceAgency MESSAGE_INTELLIGENCE_AGENCY = new MessageIntelligenceAgencyImpl();
87 private static final String MESSAGE_INTELLIGENCE_AGENCY_MX_BEAN_NAME = String
89 MessageIntelligenceAgencyMXBean.class.getPackage().getName(),
90 MessageIntelligenceAgencyMXBean.class.getSimpleName());
92 private final HashedWheelTimer hashedWheelTimer =
93 new HashedWheelTimer(TICK_DURATION, TimeUnit.MILLISECONDS, TICKS_PER_WHEEL);
94 private final NotificationPublishService notificationPublishService;
95 private final ExtensionConverterManager extensionConverterManager;
96 private final DataBroker dataBroker;
97 private final Collection<SwitchConnectionProvider> switchConnectionProviders;
98 private final DeviceInitializerProvider deviceInitializerProvider;
99 private final ConvertorManager convertorManager;
100 private final RpcProviderRegistry rpcProviderRegistry;
101 private final ClusterSingletonServiceProvider singletonServicesProvider;
102 private final OpenflowProviderConfig config;
103 private final EntityOwnershipService entityOwnershipService;
104 private final MastershipChangeServiceManager mastershipChangeServiceManager;
105 private DeviceManager deviceManager;
106 private RpcManager rpcManager;
107 private StatisticsManager statisticsManager;
108 private RoleManager roleManager;
109 private ConnectionManager connectionManager;
110 private ThreadPoolExecutor threadPool;
111 private ContextChainHolderImpl contextChainHolder;
113 public static MessageIntelligenceAgency getMessageIntelligenceAgency() {
114 return MESSAGE_INTELLIGENCE_AGENCY;
117 OpenFlowPluginProviderImpl(final ConfigurationService configurationService,
118 final List<SwitchConnectionProvider> switchConnectionProviders,
119 final DataBroker dataBroker,
120 final RpcProviderRegistry rpcProviderRegistry,
121 final NotificationPublishService notificationPublishService,
122 final ClusterSingletonServiceProvider singletonServiceProvider,
123 final EntityOwnershipService entityOwnershipService,
124 final MastershipChangeServiceManager mastershipChangeServiceManager) {
125 this.switchConnectionProviders = switchConnectionProviders;
126 this.dataBroker = dataBroker;
127 this.rpcProviderRegistry = rpcProviderRegistry;
128 this.notificationPublishService = notificationPublishService;
129 this.singletonServicesProvider = singletonServiceProvider;
130 this.entityOwnershipService = entityOwnershipService;
131 convertorManager = ConvertorManagerFactory.createDefaultManager();
132 extensionConverterManager = new ExtensionConverterManagerImpl();
133 deviceInitializerProvider = DeviceInitializerProviderFactory.createDefaultProvider();
134 config = new OpenFlowProviderConfigImpl(configurationService);
135 this.mastershipChangeServiceManager = mastershipChangeServiceManager;
139 private void startSwitchConnections() {
140 Futures.addCallback(Futures.allAsList(switchConnectionProviders.stream().map(switchConnectionProvider -> {
141 // Inject OpenFlowPlugin custom serializers and deserializers into OpenFlowJava
142 if (config.isUseSingleLayerSerialization()) {
143 SerializerInjector.injectSerializers(switchConnectionProvider);
144 DeserializerInjector.injectDeserializers(switchConnectionProvider);
146 DeserializerInjector.revertDeserializers(switchConnectionProvider);
149 // Set handler of incoming connections and start switch connection provider
150 switchConnectionProvider.setSwitchConnectionHandler(connectionManager);
151 return switchConnectionProvider.startup();
152 }).collect(Collectors.toSet())), new FutureCallback<List<Boolean>>() {
154 public void onSuccess(final List<Boolean> result) {
155 LOG.info("All switchConnectionProviders are up and running ({}).", result.size());
159 public void onFailure(@Nonnull final Throwable throwable) {
160 LOG.warn("Some switchConnectionProviders failed to start.", throwable);
165 private ListenableFuture<List<Boolean>> shutdownSwitchConnections() {
166 final ListenableFuture<List<Boolean>> listListenableFuture =
167 Futures.allAsList(switchConnectionProviders.stream().map(switchConnectionProvider -> {
168 // Revert deserializers to their original state
169 if (config.isUseSingleLayerSerialization()) {
170 DeserializerInjector.revertDeserializers(switchConnectionProvider);
173 // Shutdown switch connection provider
174 return switchConnectionProvider.shutdown();
175 }).collect(Collectors.toSet()));
177 Futures.addCallback(listListenableFuture, new FutureCallback<List<Boolean>>() {
179 public void onSuccess(final List<Boolean> result) {
180 LOG.info("All switchConnectionProviders were successfully shut down ({}).", result.size());
184 public void onFailure(@Nonnull final Throwable throwable) {
185 LOG.warn("Some switchConnectionProviders failed to shutdown.", throwable);
189 return listListenableFuture;
193 public void initialize() {
194 registerMXBean(MESSAGE_INTELLIGENCE_AGENCY, MESSAGE_INTELLIGENCE_AGENCY_MX_BEAN_NAME);
196 // TODO: copied from OpenFlowPluginProvider (Helium) misusesing the old way of distributing extension converters
197 // TODO: rewrite later!
198 OFSessionUtil.getSessionManager().setExtensionConverterProvider(extensionConverterManager);
200 // Creates a thread pool that creates new threads as needed, but will reuse previously
201 // constructed threads when they are available.
202 // Threads that have not been used for x seconds are terminated and removed from the cache.
203 threadPool = new ThreadPoolLoggingExecutor(
204 config.getThreadPoolMinThreads(),
205 config.getThreadPoolMaxThreads().getValue(),
206 config.getThreadPoolTimeout(),
207 TimeUnit.SECONDS, new SynchronousQueue<>(), POOL_NAME);
209 deviceManager = new DeviceManagerImpl(
212 getMessageIntelligenceAgency(),
213 notificationPublishService,
216 deviceInitializerProvider);
218 TranslatorLibraryUtil.injectBasicTranslatorLibrary(deviceManager, convertorManager);
219 ((ExtensionConverterProviderKeeper) deviceManager).setExtensionConverterProvider(extensionConverterManager);
221 rpcManager = new RpcManagerImpl(
224 extensionConverterManager,
226 notificationPublishService);
228 statisticsManager = new StatisticsManagerImpl(
233 roleManager = new RoleManagerImpl(hashedWheelTimer);
235 contextChainHolder = new ContextChainHolderImpl(
237 singletonServicesProvider,
238 entityOwnershipService,
239 mastershipChangeServiceManager);
241 contextChainHolder.addManager(deviceManager);
242 contextChainHolder.addManager(statisticsManager);
243 contextChainHolder.addManager(rpcManager);
244 contextChainHolder.addManager(roleManager);
246 connectionManager = new ConnectionManagerImpl(config, threadPool);
247 connectionManager.setDeviceConnectedHandler(contextChainHolder);
248 connectionManager.setDeviceDisconnectedHandler(contextChainHolder);
250 deviceManager.initialize();
251 startSwitchConnections();
255 public ExtensionConverterRegistrator getExtensionConverterRegistrator() {
256 return extensionConverterManager;
260 public void close() {
262 shutdownSwitchConnections().get(10, TimeUnit.SECONDS);
263 } catch (InterruptedException | ExecutionException | TimeoutException e) {
264 LOG.warn("Failed to shut down switch connections in time {}s, error: {}", 10, e);
267 gracefulShutdown(contextChainHolder);
268 gracefulShutdown(deviceManager);
269 gracefulShutdown(rpcManager);
270 gracefulShutdown(statisticsManager);
271 gracefulShutdown(roleManager);
272 gracefulShutdown(threadPool);
273 gracefulShutdown(hashedWheelTimer);
274 unregisterMXBean(MESSAGE_INTELLIGENCE_AGENCY_MX_BEAN_NAME);
277 @SuppressWarnings("checkstyle:IllegalCatch")
278 private static void gracefulShutdown(final AutoCloseable closeable) {
279 if (Objects.isNull(closeable)) {
285 } catch (Exception e) {
286 LOG.warn("Failed to shutdown {} gracefully.", closeable);
290 private static void gracefulShutdown(final Timer timer) {
291 if (Objects.isNull(timer)) {
297 } catch (IllegalStateException e) {
298 LOG.warn("Failed to shutdown {} gracefully.", timer);
302 private static void gracefulShutdown(final ThreadPoolExecutor threadPoolExecutor) {
303 if (Objects.isNull(threadPoolExecutor)) {
308 threadPoolExecutor.shutdownNow();
309 } catch (SecurityException e) {
310 LOG.warn("Failed to shutdown {} gracefully.", threadPoolExecutor);
314 private static void registerMXBean(final Object bean, final String beanName) {
315 final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
318 mbs.registerMBean(bean, new ObjectName(beanName));
319 } catch (MalformedObjectNameException
320 | NotCompliantMBeanException
321 | MBeanRegistrationException
322 | InstanceAlreadyExistsException e) {
323 LOG.warn("Error registering MBean {}", e);
327 private static void unregisterMXBean(final String beanName) {
328 final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
331 mbs.unregisterMBean(new ObjectName(beanName));
332 } catch (InstanceNotFoundException
333 | MBeanRegistrationException
334 | MalformedObjectNameException e) {
335 LOG.warn("Error unregistering MBean {}", e);