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;
20 import javax.management.InstanceAlreadyExistsException;
21 import javax.management.MBeanRegistrationException;
22 import javax.management.MBeanServer;
23 import javax.management.MalformedObjectNameException;
24 import javax.management.NotCompliantMBeanException;
25 import javax.management.ObjectName;
26 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
27 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
28 import org.opendaylight.controller.md.sal.binding.api.NotificationService;
29 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
30 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
31 import org.opendaylight.openflowjava.protocol.spi.connection.SwitchConnectionProvider;
32 import org.opendaylight.openflowplugin.api.openflow.OpenFlowPluginProvider;
33 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionManager;
34 import org.opendaylight.openflowplugin.api.openflow.device.DeviceManager;
35 import org.opendaylight.openflowplugin.api.openflow.role.RoleManager;
36 import org.opendaylight.openflowplugin.api.openflow.rpc.RpcManager;
37 import org.opendaylight.openflowplugin.api.openflow.statistics.StatisticsManager;
38 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageIntelligenceAgency;
39 import org.opendaylight.openflowplugin.extension.api.ExtensionConverterProviderKeeper;
40 import org.opendaylight.openflowplugin.extension.api.ExtensionConverterRegistrator;
41 import org.opendaylight.openflowplugin.extension.api.OpenFlowPluginExtensionRegistratorProvider;
42 import org.opendaylight.openflowplugin.extension.api.core.extension.ExtensionConverterManager;
43 import org.opendaylight.openflowplugin.impl.connection.ConnectionManagerImpl;
44 import org.opendaylight.openflowplugin.impl.device.DeviceManagerImpl;
45 import org.opendaylight.openflowplugin.impl.role.RoleManagerImpl;
46 import org.opendaylight.openflowplugin.impl.rpc.RpcManagerImpl;
47 import org.opendaylight.openflowplugin.impl.statistics.StatisticsManagerImpl;
48 import org.opendaylight.openflowplugin.impl.statistics.ofpspecific.MessageIntelligenceAgencyImpl;
49 import org.opendaylight.openflowplugin.impl.statistics.ofpspecific.MessageIntelligenceAgencyMXBean;
50 import org.opendaylight.openflowplugin.impl.util.TranslatorLibraryUtil;
51 import org.opendaylight.openflowplugin.openflow.md.core.extension.ExtensionConverterManagerImpl;
52 import org.opendaylight.openflowplugin.openflow.md.core.session.OFSessionUtil;
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
57 * Created by Martin Bobak <mbobak@cisco.com> on 27.3.2015.
59 public class OpenFlowPluginProviderImpl implements OpenFlowPluginProvider, OpenFlowPluginExtensionRegistratorProvider {
61 private static final Logger LOG = LoggerFactory.getLogger(OpenFlowPluginProviderImpl.class);
62 private static final MessageIntelligenceAgency messageIntelligenceAgency = new MessageIntelligenceAgencyImpl();
64 private final int rpcRequestsQuota;
65 private final long globalNotificationQuota;
66 private DeviceManager deviceManager;
67 private RoleManager roleManager;
68 private RpcManager rpcManager;
69 private RpcProviderRegistry rpcProviderRegistry;
70 private StatisticsManager statisticsManager;
71 private ConnectionManager connectionManager;
72 private NotificationService notificationProviderService;
73 private NotificationPublishService notificationPublishService;
74 private EntityOwnershipService entityOwnershipService;
76 private ExtensionConverterManager extensionConverterManager;
78 private DataBroker dataBroker;
79 private Collection<SwitchConnectionProvider> switchConnectionProviders;
80 private boolean switchFeaturesMandatory = false;
81 private boolean isStatisticsPollingOff = false;
82 private boolean isStatisticsRpcEnabled;
84 public OpenFlowPluginProviderImpl(final long rpcRequestsQuota, final Long globalNotificationQuota) {
85 Preconditions.checkArgument(rpcRequestsQuota > 0 && rpcRequestsQuota <= Integer.MAX_VALUE, "rpcRequestQuota has to be in range <1,%s>", Integer.MAX_VALUE);
86 this.rpcRequestsQuota = (int) rpcRequestsQuota;
87 this.globalNotificationQuota = Preconditions.checkNotNull(globalNotificationQuota);
91 public boolean isStatisticsPollingOff() {
92 return isStatisticsPollingOff;
96 public void setIsStatisticsPollingOff(final boolean isStatisticsPollingOff) {
97 this.isStatisticsPollingOff = isStatisticsPollingOff;
100 private void startSwitchConnections() {
101 final List<ListenableFuture<Boolean>> starterChain = new ArrayList<>(switchConnectionProviders.size());
102 for (final SwitchConnectionProvider switchConnectionPrv : switchConnectionProviders) {
103 switchConnectionPrv.setSwitchConnectionHandler(connectionManager);
104 final ListenableFuture<Boolean> isOnlineFuture = switchConnectionPrv.startup();
105 starterChain.add(isOnlineFuture);
108 final ListenableFuture<List<Boolean>> srvStarted = Futures.allAsList(starterChain);
109 Futures.addCallback(srvStarted, new FutureCallback<List<Boolean>>() {
111 public void onSuccess(final List<Boolean> result) {
112 LOG.info("All switchConnectionProviders are up and running ({}).",
117 public void onFailure(final Throwable t) {
118 LOG.warn("Some switchConnectionProviders failed to start.", t);
124 public boolean isSwitchFeaturesMandatory() {
125 return switchFeaturesMandatory;
129 public void setEntityOwnershipService(final EntityOwnershipService entityOwnershipService) {
130 this.entityOwnershipService = entityOwnershipService;
134 public void setSwitchFeaturesMandatory(final boolean switchFeaturesMandatory) {
135 this.switchFeaturesMandatory = switchFeaturesMandatory;
138 public static MessageIntelligenceAgency getMessageIntelligenceAgency() {
139 return OpenFlowPluginProviderImpl.messageIntelligenceAgency;
143 public void setSwitchConnectionProviders(final Collection<SwitchConnectionProvider> switchConnectionProviders) {
144 this.switchConnectionProviders = switchConnectionProviders;
148 public void setDataBroker(final DataBroker dataBroker) {
149 this.dataBroker = dataBroker;
153 public void setRpcProviderRegistry(final RpcProviderRegistry rpcProviderRegistry) {
154 this.rpcProviderRegistry = rpcProviderRegistry;
158 public void initialize() {
160 Preconditions.checkNotNull(dataBroker, "missing data broker");
161 Preconditions.checkNotNull(rpcProviderRegistry, "missing RPC provider registry");
162 Preconditions.checkNotNull(notificationProviderService, "missing notification provider service");
164 extensionConverterManager = new ExtensionConverterManagerImpl();
165 // TODO: copied from OpenFlowPluginProvider (Helium) misusesing the old way of distributing extension converters
166 // TODO: rewrite later!
167 OFSessionUtil.getSessionManager().setExtensionConverterProvider(extensionConverterManager);
169 connectionManager = new ConnectionManagerImpl();
171 registerMXBean(messageIntelligenceAgency);
173 deviceManager = new DeviceManagerImpl(dataBroker, messageIntelligenceAgency, globalNotificationQuota, switchFeaturesMandatory);
174 ((ExtensionConverterProviderKeeper) deviceManager).setExtensionConverterProvider(extensionConverterManager);
175 roleManager = new RoleManagerImpl(entityOwnershipService, dataBroker);
176 statisticsManager = new StatisticsManagerImpl(rpcProviderRegistry, isStatisticsPollingOff);
177 rpcManager = new RpcManagerImpl(rpcProviderRegistry, rpcRequestsQuota);
179 // CM -> DM -> SM -> RPC -> Role -> DM
180 connectionManager.setDeviceConnectedHandler(deviceManager);
181 deviceManager.setDeviceInitializationPhaseHandler(statisticsManager);
182 statisticsManager.setDeviceInitializationPhaseHandler(rpcManager);
183 rpcManager.setDeviceInitializationPhaseHandler(roleManager);
184 roleManager.setDeviceInitializationPhaseHandler(deviceManager);
186 rpcManager.setStatisticsRpcEnabled(isStatisticsRpcEnabled);
187 rpcManager.setNotificationPublishService(notificationPublishService);
189 deviceManager.setNotificationService(this.notificationProviderService);
190 deviceManager.setNotificationPublishService(this.notificationPublishService);
192 TranslatorLibraryUtil.setBasicTranslatorLibrary(deviceManager);
193 deviceManager.initialize();
195 startSwitchConnections();
198 private static void registerMXBean(final MessageIntelligenceAgency messageIntelligenceAgency) {
199 final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
201 final String pathToMxBean = String.format("%s:type=%s",
202 MessageIntelligenceAgencyMXBean.class.getPackage().getName(),
203 MessageIntelligenceAgencyMXBean.class.getSimpleName());
204 final ObjectName name = new ObjectName(pathToMxBean);
205 mbs.registerMBean(messageIntelligenceAgency, name);
206 } catch (MalformedObjectNameException
207 | NotCompliantMBeanException
208 | MBeanRegistrationException
209 | InstanceAlreadyExistsException e) {
210 LOG.warn("Error registering MBean {}", e);
215 public void setNotificationProviderService(final NotificationService notificationProviderService) {
216 this.notificationProviderService = notificationProviderService;
220 public void setNotificationPublishService(final NotificationPublishService notificationPublishProviderService) {
221 this.notificationPublishService = notificationPublishProviderService;
225 public ExtensionConverterRegistrator getExtensionConverterRegistrator() {
226 return extensionConverterManager;
230 public void setIsStatisticsRpcEnabled(final boolean isStatisticsRpcEnabled) {
231 this.isStatisticsRpcEnabled = isStatisticsRpcEnabled;
235 public void close() throws Exception {
236 //TODO: consider wrapping each manager into try-catch
237 deviceManager.close();
239 statisticsManager.close();
241 // TODO: needs to close org.opendaylight.openflowplugin.impl.role.OpenflowOwnershipListener after RoleContexts are down
242 // TODO: must not be executed prior to all living RoleContexts have been closed (via closing living DeviceContexts)