Post "Clustering optimization" updates
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / OpenFlowPluginProviderImpl.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.openflowplugin.impl;
10
11
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;
55
56 /**
57  * Created by Martin Bobak <mbobak@cisco.com> on 27.3.2015.
58  */
59 public class OpenFlowPluginProviderImpl implements OpenFlowPluginProvider, OpenFlowPluginExtensionRegistratorProvider {
60
61     private static final Logger LOG = LoggerFactory.getLogger(OpenFlowPluginProviderImpl.class);
62     private static final MessageIntelligenceAgency messageIntelligenceAgency = new MessageIntelligenceAgencyImpl();
63
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;
75
76     private ExtensionConverterManager extensionConverterManager;
77
78     private DataBroker dataBroker;
79     private Collection<SwitchConnectionProvider> switchConnectionProviders;
80     private boolean switchFeaturesMandatory = false;
81     private boolean isStatisticsPollingOff = false;
82     private boolean isStatisticsRpcEnabled;
83
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);
88     }
89
90     @Override
91     public boolean isStatisticsPollingOff() {
92         return isStatisticsPollingOff;
93     }
94
95     @Override
96     public void setIsStatisticsPollingOff(final boolean isStatisticsPollingOff) {
97         this.isStatisticsPollingOff = isStatisticsPollingOff;
98     }
99
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);
106         }
107
108         final ListenableFuture<List<Boolean>> srvStarted = Futures.allAsList(starterChain);
109         Futures.addCallback(srvStarted, new FutureCallback<List<Boolean>>() {
110             @Override
111             public void onSuccess(final List<Boolean> result) {
112                 LOG.info("All switchConnectionProviders are up and running ({}).",
113                         result.size());
114             }
115
116             @Override
117             public void onFailure(final Throwable t) {
118                 LOG.warn("Some switchConnectionProviders failed to start.", t);
119             }
120         });
121     }
122
123     @Override
124     public boolean isSwitchFeaturesMandatory() {
125         return switchFeaturesMandatory;
126     }
127
128     @Override
129     public void setEntityOwnershipService(final EntityOwnershipService entityOwnershipService) {
130         this.entityOwnershipService = entityOwnershipService;
131     }
132
133     @Override
134     public void setSwitchFeaturesMandatory(final boolean switchFeaturesMandatory) {
135         this.switchFeaturesMandatory = switchFeaturesMandatory;
136     }
137
138     public static MessageIntelligenceAgency getMessageIntelligenceAgency() {
139         return OpenFlowPluginProviderImpl.messageIntelligenceAgency;
140     }
141
142     @Override
143     public void setSwitchConnectionProviders(final Collection<SwitchConnectionProvider> switchConnectionProviders) {
144         this.switchConnectionProviders = switchConnectionProviders;
145     }
146
147     @Override
148     public void setDataBroker(final DataBroker dataBroker) {
149         this.dataBroker = dataBroker;
150     }
151
152     @Override
153     public void setRpcProviderRegistry(final RpcProviderRegistry rpcProviderRegistry) {
154         this.rpcProviderRegistry = rpcProviderRegistry;
155     }
156
157     @Override
158     public void initialize() {
159
160         Preconditions.checkNotNull(dataBroker, "missing data broker");
161         Preconditions.checkNotNull(rpcProviderRegistry, "missing RPC provider registry");
162         Preconditions.checkNotNull(notificationProviderService, "missing notification provider service");
163
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);
168
169         connectionManager = new ConnectionManagerImpl();
170
171         registerMXBean(messageIntelligenceAgency);
172
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);
178
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);
185
186         rpcManager.setStatisticsRpcEnabled(isStatisticsRpcEnabled);
187         rpcManager.setNotificationPublishService(notificationPublishService);
188
189         deviceManager.setNotificationService(this.notificationProviderService);
190         deviceManager.setNotificationPublishService(this.notificationPublishService);
191
192         TranslatorLibraryUtil.setBasicTranslatorLibrary(deviceManager);
193         deviceManager.initialize();
194
195         startSwitchConnections();
196     }
197
198     private static void registerMXBean(final MessageIntelligenceAgency messageIntelligenceAgency) {
199         final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
200         try {
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);
211         }
212     }
213
214     @Override
215     public void setNotificationProviderService(final NotificationService notificationProviderService) {
216         this.notificationProviderService = notificationProviderService;
217     }
218
219     @Override
220     public void setNotificationPublishService(final NotificationPublishService notificationPublishProviderService) {
221         this.notificationPublishService = notificationPublishProviderService;
222     }
223
224     @Override
225     public ExtensionConverterRegistrator getExtensionConverterRegistrator() {
226         return extensionConverterManager;
227     }
228
229     @Override
230     public void setIsStatisticsRpcEnabled(final boolean isStatisticsRpcEnabled) {
231         this.isStatisticsRpcEnabled = isStatisticsRpcEnabled;
232     }
233
234     @Override
235     public void close() throws Exception {
236         //TODO: consider wrapping each manager into try-catch
237         deviceManager.close();
238         rpcManager.close();
239         statisticsManager.close();
240
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)
243         roleManager.close();
244     }
245 }