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.sal.binding.api.RpcProviderRegistry;
30 import org.opendaylight.openflowjava.protocol.spi.connection.SwitchConnectionProvider;
31 import org.opendaylight.openflowplugin.api.openflow.OpenFlowPluginProvider;
32 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionManager;
33 import org.opendaylight.openflowplugin.api.openflow.device.DeviceManager;
34 import org.opendaylight.openflowplugin.api.openflow.rpc.RpcManager;
35 import org.opendaylight.openflowplugin.api.openflow.statistics.StatisticsManager;
36 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageIntelligenceAgency;
37 import org.opendaylight.openflowplugin.extension.api.ExtensionConverterRegistrator;
38 import org.opendaylight.openflowplugin.extension.api.OpenFlowPluginExtensionRegistratorProvider;
39 import org.opendaylight.openflowplugin.impl.connection.ConnectionManagerImpl;
40 import org.opendaylight.openflowplugin.impl.device.DeviceManagerImpl;
41 import org.opendaylight.openflowplugin.impl.rpc.RpcManagerImpl;
42 import org.opendaylight.openflowplugin.impl.statistics.StatisticsManagerImpl;
43 import org.opendaylight.openflowplugin.impl.statistics.ofpspecific.MessageIntelligenceAgencyImpl;
44 import org.opendaylight.openflowplugin.impl.statistics.ofpspecific.MessageIntelligenceAgencyMXBean;
45 import org.opendaylight.openflowplugin.impl.util.TranslatorLibraryUtil;
46 import org.opendaylight.openflowplugin.openflow.md.core.extension.ExtensionConverterManager;
47 import org.opendaylight.openflowplugin.openflow.md.core.extension.ExtensionConverterManagerImpl;
48 import org.opendaylight.openflowplugin.openflow.md.core.session.OFSessionUtil;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.api.types.rev150327.OfpRole;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
54 * Created by Martin Bobak <mbobak@cisco.com> on 27.3.2015.
56 public class OpenFlowPluginProviderImpl implements OpenFlowPluginProvider, OpenFlowPluginExtensionRegistratorProvider {
58 private static final Logger LOG = LoggerFactory.getLogger(OpenFlowPluginProviderImpl.class);
60 private DeviceManager deviceManager;
61 private RpcManager rpcManager;
62 private RpcProviderRegistry rpcProviderRegistry;
63 private StatisticsManager statisticsManager;
64 private ConnectionManager connectionManager;
65 private NotificationService notificationProviderService;
66 private NotificationPublishService notificationPublishService;
68 private ExtensionConverterManager extensionConverterManager;
70 private DataBroker dataBroker;
72 private Collection<SwitchConnectionProvider> switchConnectionProviders;
73 private final Long rpcRequestsQuota;
75 public OpenFlowPluginProviderImpl(final Long rpcRequestsQuota) {
76 this.rpcRequestsQuota = rpcRequestsQuota;
80 private void startSwitchConnections() {
81 final List<ListenableFuture<Boolean>> starterChain = new ArrayList<>(switchConnectionProviders.size());
82 for (final SwitchConnectionProvider switchConnectionPrv : switchConnectionProviders) {
83 switchConnectionPrv.setSwitchConnectionHandler(connectionManager);
84 final ListenableFuture<Boolean> isOnlineFuture = switchConnectionPrv.startup();
85 starterChain.add(isOnlineFuture);
88 final ListenableFuture<List<Boolean>> srvStarted = Futures.allAsList(starterChain);
89 Futures.addCallback(srvStarted, new FutureCallback<List<Boolean>>() {
91 public void onSuccess(final List<Boolean> result) {
92 LOG.info("All switchConnectionProviders are up and running ({}).",
97 public void onFailure(final Throwable t) {
98 LOG.warn("Some switchConnectionProviders failed to start.", t);
104 public void setSwitchConnectionProviders(final Collection<SwitchConnectionProvider> switchConnectionProviders) {
105 this.switchConnectionProviders = switchConnectionProviders;
109 public void setDataBroker(final DataBroker dataBroker) {
110 this.dataBroker = dataBroker;
114 public void setRpcProviderRegistry(final RpcProviderRegistry rpcProviderRegistry) {
115 this.rpcProviderRegistry = rpcProviderRegistry;
119 public void setRole(final OfpRole role) {
125 public void initialize() {
127 Preconditions.checkNotNull(dataBroker, "missing data broker");
128 Preconditions.checkNotNull(rpcProviderRegistry, "missing RPC provider registry");
129 Preconditions.checkNotNull(notificationProviderService, "missing notification provider service");
131 extensionConverterManager = new ExtensionConverterManagerImpl();
132 // TODO: copied from OpenFlowPluginProvider (Helium) misusesing the old way of distributing extension converters
133 // TODO: rewrite later!
134 OFSessionUtil.getSessionManager().setExtensionConverterProvider(extensionConverterManager);
136 connectionManager = new ConnectionManagerImpl();
137 MessageIntelligenceAgency messageIntelligenceAgency = new MessageIntelligenceAgencyImpl();
139 registerMXBean(messageIntelligenceAgency);
141 deviceManager = new DeviceManagerImpl(dataBroker, messageIntelligenceAgency);
142 statisticsManager = new StatisticsManagerImpl();
143 rpcManager = new RpcManagerImpl(rpcProviderRegistry, rpcRequestsQuota);
145 connectionManager.setDeviceConnectedHandler(deviceManager);
146 deviceManager.setDeviceInitializationPhaseHandler(statisticsManager);
147 deviceManager.setNotificationService(this.notificationProviderService);
148 deviceManager.setNotificationPublishService(this.notificationPublishService);
149 statisticsManager.setDeviceInitializationPhaseHandler(rpcManager);
150 rpcManager.setDeviceInitializationPhaseHandler(deviceManager);
152 TranslatorLibraryUtil.setBasicTranslatorLibrary(deviceManager);
153 deviceManager.initialize();
155 startSwitchConnections();
158 private static void registerMXBean(final MessageIntelligenceAgency messageIntelligenceAgency) {
159 MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
161 String pathToMxBean = String.format("%s:type=%s",
162 MessageIntelligenceAgencyMXBean.class.getPackage().getName(),
163 MessageIntelligenceAgencyMXBean.class.getSimpleName());
164 ObjectName name = new ObjectName(pathToMxBean);
165 mbs.registerMBean(messageIntelligenceAgency, name);
166 } catch (MalformedObjectNameException
167 | NotCompliantMBeanException
168 | MBeanRegistrationException
169 | InstanceAlreadyExistsException e) {
170 LOG.warn("Error registering MBean {}", e);
175 public void setNotificationProviderService(final NotificationService notificationProviderService) {
176 this.notificationProviderService = notificationProviderService;
180 public void setNotificationPublishService(final NotificationPublishService notificationPublishProviderService) {
181 this.notificationPublishService = notificationPublishProviderService;
185 public ExtensionConverterRegistrator getExtensionConverterRegistrator() {
186 return extensionConverterManager;
190 public void close() throws Exception {
191 //TODO: close all contexts, switchConnections (, managers)