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 java.lang.management.ManagementFactory;
17 import java.util.ArrayList;
18 import java.util.Collection;
19 import java.util.List;
21 import java.util.concurrent.SynchronousQueue;
22 import java.util.concurrent.ThreadPoolExecutor;
23 import java.util.concurrent.TimeUnit;
24 import javax.annotation.Nonnull;
25 import javax.management.InstanceAlreadyExistsException;
26 import javax.management.MBeanRegistrationException;
27 import javax.management.MBeanServer;
28 import javax.management.MalformedObjectNameException;
29 import javax.management.NotCompliantMBeanException;
30 import javax.management.ObjectName;
31 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
32 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
33 import org.opendaylight.controller.md.sal.binding.api.NotificationService;
34 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
35 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
36 import org.opendaylight.openflowjava.protocol.spi.connection.SwitchConnectionProvider;
37 import org.opendaylight.openflowplugin.api.openflow.OpenFlowPluginProvider;
38 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionManager;
39 import org.opendaylight.openflowplugin.api.openflow.device.DeviceManager;
40 import org.opendaylight.openflowplugin.api.openflow.lifecycle.LifecycleConductor;
41 import org.opendaylight.openflowplugin.api.openflow.lifecycle.RoleChangeListener;
42 import org.opendaylight.openflowplugin.api.openflow.role.RoleManager;
43 import org.opendaylight.openflowplugin.api.openflow.rpc.RpcManager;
44 import org.opendaylight.openflowplugin.api.openflow.statistics.StatisticsManager;
45 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageIntelligenceAgency;
46 import org.opendaylight.openflowplugin.extension.api.ExtensionConverterProviderKeeper;
47 import org.opendaylight.openflowplugin.extension.api.ExtensionConverterRegistrator;
48 import org.opendaylight.openflowplugin.extension.api.OpenFlowPluginExtensionRegistratorProvider;
49 import org.opendaylight.openflowplugin.extension.api.core.extension.ExtensionConverterManager;
50 import org.opendaylight.openflowplugin.impl.connection.ConnectionManagerImpl;
51 import org.opendaylight.openflowplugin.impl.device.DeviceManagerImpl;
52 import org.opendaylight.openflowplugin.impl.role.RoleManagerImpl;
53 import org.opendaylight.openflowplugin.impl.rpc.RpcManagerImpl;
54 import org.opendaylight.openflowplugin.impl.statistics.StatisticsManagerImpl;
55 import org.opendaylight.openflowplugin.impl.statistics.ofpspecific.MessageIntelligenceAgencyImpl;
56 import org.opendaylight.openflowplugin.impl.statistics.ofpspecific.MessageIntelligenceAgencyMXBean;
57 import org.opendaylight.openflowplugin.impl.util.TranslatorLibraryUtil;
58 import org.opendaylight.openflowplugin.openflow.md.core.ThreadPoolLoggingExecutor;
59 import org.opendaylight.openflowplugin.openflow.md.core.extension.ExtensionConverterManagerImpl;
60 import org.opendaylight.openflowplugin.openflow.md.core.session.OFSessionUtil;
61 import org.slf4j.Logger;
62 import org.slf4j.LoggerFactory;
64 public class OpenFlowPluginProviderImpl implements OpenFlowPluginProvider, OpenFlowPluginExtensionRegistratorProvider {
66 private static final Logger LOG = LoggerFactory.getLogger(OpenFlowPluginProviderImpl.class);
67 private static final MessageIntelligenceAgency messageIntelligenceAgency = new MessageIntelligenceAgencyImpl();
69 private final int rpcRequestsQuota;
70 private final long globalNotificationQuota;
71 private long barrierInterval;
72 private int barrierCountLimit;
73 private long echoReplyTimeout;
74 private DeviceManager deviceManager;
75 private RoleManager roleManager;
76 private RpcManager rpcManager;
77 private RpcProviderRegistry rpcProviderRegistry;
78 private StatisticsManager statisticsManager;
79 private ConnectionManager connectionManager;
80 private NotificationService notificationProviderService;
81 private NotificationPublishService notificationPublishService;
82 private EntityOwnershipService entityOwnershipService;
84 private ExtensionConverterManager extensionConverterManager;
86 private DataBroker dataBroker;
87 private Collection<SwitchConnectionProvider> switchConnectionProviders;
88 private boolean switchFeaturesMandatory = false;
89 private boolean isStatisticsPollingOff = false;
90 private boolean isStatisticsRpcEnabled;
91 private boolean isNotificationFlowRemovedOff = false;
92 private Map<String,Object> managedProperties;
94 private final LifecycleConductor conductor;
95 private final ThreadPoolExecutor threadPool;
97 public OpenFlowPluginProviderImpl(final long rpcRequestsQuota,
98 final long globalNotificationQuota,
99 final int threadPoolMinThreads,
100 final int threadPoolMaxThreads,
101 final long threadPoolTimeout) {
102 Preconditions.checkArgument(rpcRequestsQuota > 0 && rpcRequestsQuota <= Integer.MAX_VALUE, "rpcRequestQuota has to be in range <1,%s>", Integer.MAX_VALUE);
103 this.rpcRequestsQuota = (int) rpcRequestsQuota;
104 this.globalNotificationQuota = Preconditions.checkNotNull(globalNotificationQuota);
106 // Creates a thread pool that creates new threads as needed, but will reuse previously
107 // constructed threads when they are available.
108 // Threads that have not been used for x seconds are terminated and removed from the cache.
109 threadPool = new ThreadPoolLoggingExecutor(
110 Preconditions.checkNotNull(threadPoolMinThreads),
111 Preconditions.checkNotNull(threadPoolMaxThreads),
112 Preconditions.checkNotNull(threadPoolTimeout), TimeUnit.SECONDS,
113 new SynchronousQueue<>(), "ofppool");
115 conductor = new LifecycleConductorImpl(messageIntelligenceAgency);
119 public boolean isStatisticsPollingOff() {
120 return isStatisticsPollingOff;
124 public void setIsStatisticsPollingOff(final boolean isStatisticsPollingOff) {
125 this.isStatisticsPollingOff = isStatisticsPollingOff;
128 private void startSwitchConnections() {
129 final List<ListenableFuture<Boolean>> starterChain = new ArrayList<>(switchConnectionProviders.size());
130 for (final SwitchConnectionProvider switchConnectionPrv : switchConnectionProviders) {
131 switchConnectionPrv.setSwitchConnectionHandler(connectionManager);
132 final ListenableFuture<Boolean> isOnlineFuture = switchConnectionPrv.startup();
133 starterChain.add(isOnlineFuture);
136 final ListenableFuture<List<Boolean>> srvStarted = Futures.allAsList(starterChain);
137 Futures.addCallback(srvStarted, new FutureCallback<List<Boolean>>() {
139 public void onSuccess(final List<Boolean> result) {
140 LOG.info("All switchConnectionProviders are up and running ({}).",
145 public void onFailure(@Nonnull final Throwable t) {
146 LOG.warn("Some switchConnectionProviders failed to start.", t);
152 public boolean isSwitchFeaturesMandatory() {
153 return switchFeaturesMandatory;
157 public void setEntityOwnershipService(final EntityOwnershipService entityOwnershipService) {
158 this.entityOwnershipService = entityOwnershipService;
162 public void setBarrierCountLimit(final int barrierCountLimit) {
163 this.barrierCountLimit = barrierCountLimit;
167 public void setBarrierInterval(final long barrierTimeoutLimit) {
168 this.barrierInterval = barrierTimeoutLimit;
172 public void setEchoReplyTimeout(final long echoReplyTimeout) {
173 this.echoReplyTimeout = echoReplyTimeout;
177 public void setNotificationFlowRemovedOff(boolean isNotificationFlowRemovedOff) {
178 this.isNotificationFlowRemovedOff = isNotificationFlowRemovedOff;
183 public void setSwitchFeaturesMandatory(final boolean switchFeaturesMandatory) {
184 this.switchFeaturesMandatory = switchFeaturesMandatory;
187 public static MessageIntelligenceAgency getMessageIntelligenceAgency() {
188 return OpenFlowPluginProviderImpl.messageIntelligenceAgency;
192 public void setSwitchConnectionProviders(final Collection<SwitchConnectionProvider> switchConnectionProviders) {
193 this.switchConnectionProviders = switchConnectionProviders;
197 public void setDataBroker(final DataBroker dataBroker) {
198 this.dataBroker = dataBroker;
202 public void setRpcProviderRegistry(final RpcProviderRegistry rpcProviderRegistry) {
203 this.rpcProviderRegistry = rpcProviderRegistry;
207 public void initialize() {
208 Preconditions.checkNotNull(dataBroker, "missing data broker");
209 Preconditions.checkNotNull(rpcProviderRegistry, "missing RPC provider registry");
210 Preconditions.checkNotNull(notificationProviderService, "missing notification provider service");
212 extensionConverterManager = new ExtensionConverterManagerImpl();
213 // TODO: copied from OpenFlowPluginProvider (Helium) misusesing the old way of distributing extension converters
214 // TODO: rewrite later!
215 OFSessionUtil.getSessionManager().setExtensionConverterProvider(extensionConverterManager);
217 connectionManager = new ConnectionManagerImpl(echoReplyTimeout, threadPool);
219 registerMXBean(messageIntelligenceAgency);
221 deviceManager = new DeviceManagerImpl(dataBroker,
222 globalNotificationQuota,
223 switchFeaturesMandatory,
227 isNotificationFlowRemovedOff);
228 ((ExtensionConverterProviderKeeper) conductor).setExtensionConverterProvider(extensionConverterManager);
229 ((ExtensionConverterProviderKeeper) deviceManager).setExtensionConverterProvider(extensionConverterManager);
231 conductor.setSafelyManager(deviceManager);
232 conductor.setNotificationPublishService(notificationPublishService);
234 roleManager = new RoleManagerImpl(entityOwnershipService, dataBroker, conductor);
235 statisticsManager = new StatisticsManagerImpl(rpcProviderRegistry, isStatisticsPollingOff, conductor);
236 conductor.setSafelyManager(statisticsManager);
238 rpcManager = new RpcManagerImpl(rpcProviderRegistry, rpcRequestsQuota, conductor);
239 conductor.setSafelyManager(rpcManager);
241 roleManager.addRoleChangeListener((RoleChangeListener) conductor);
243 /* Initialization Phase ordering - OFP Device Context suite */
244 // CM -> DM -> SM -> RPC -> Role -> DM
245 connectionManager.setDeviceConnectedHandler(deviceManager);
246 deviceManager.setDeviceInitializationPhaseHandler(statisticsManager);
247 statisticsManager.setDeviceInitializationPhaseHandler(rpcManager);
248 rpcManager.setDeviceInitializationPhaseHandler(roleManager);
249 roleManager.setDeviceInitializationPhaseHandler(deviceManager);
251 /* Termination Phase ordering - OFP Device Context suite */
252 deviceManager.setDeviceTerminationPhaseHandler(rpcManager);
253 rpcManager.setDeviceTerminationPhaseHandler(statisticsManager);
254 statisticsManager.setDeviceTerminationPhaseHandler(roleManager);
255 roleManager.setDeviceTerminationPhaseHandler(deviceManager);
257 rpcManager.setStatisticsRpcEnabled(isStatisticsRpcEnabled);
259 TranslatorLibraryUtil.setBasicTranslatorLibrary(deviceManager);
260 deviceManager.initialize();
262 startSwitchConnections();
266 public void update(Map<String,Object> props) {
267 LOG.debug("Update managed properties = {}", props.toString());
268 this.managedProperties = props;
270 if(deviceManager != null && props.containsKey("notification-flow-removed-off")) {
271 deviceManager.setIsNotificationFlowRemovedOff(Boolean.valueOf(props.get("notification-flow-removed-off").toString()));
275 private static void registerMXBean(final MessageIntelligenceAgency messageIntelligenceAgency) {
276 final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
278 final String pathToMxBean = String.format("%s:type=%s",
279 MessageIntelligenceAgencyMXBean.class.getPackage().getName(),
280 MessageIntelligenceAgencyMXBean.class.getSimpleName());
281 final ObjectName name = new ObjectName(pathToMxBean);
282 mbs.registerMBean(messageIntelligenceAgency, name);
283 } catch (MalformedObjectNameException
284 | NotCompliantMBeanException
285 | MBeanRegistrationException
286 | InstanceAlreadyExistsException e) {
287 LOG.warn("Error registering MBean {}", e);
292 public void setNotificationProviderService(final NotificationService notificationProviderService) {
293 this.notificationProviderService = notificationProviderService;
297 public void setNotificationPublishService(final NotificationPublishService notificationPublishProviderService) {
298 this.notificationPublishService = notificationPublishProviderService;
302 public ExtensionConverterRegistrator getExtensionConverterRegistrator() {
303 return extensionConverterManager;
307 public void setIsStatisticsRpcEnabled(final boolean isStatisticsRpcEnabled) {
308 this.isStatisticsRpcEnabled = isStatisticsRpcEnabled;
312 public void close() throws Exception {
313 //TODO: consider wrapping each manager into try-catch
314 deviceManager.close();
316 statisticsManager.close();
318 // TODO: needs to close org.opendaylight.openflowplugin.impl.role.OpenflowOwnershipListener after RoleContexts are down
319 // TODO: must not be executed prior to all living RoleContexts have been closed (via closing living DeviceContexts)
322 // Manually shutdown all remaining running threads in pool
323 threadPool.shutdown();