BUG-4083: Li: Enabling polling of statistics via config subsystem.
[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.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;
52
53 /**
54  * Created by Martin Bobak <mbobak@cisco.com> on 27.3.2015.
55  */
56 public class OpenFlowPluginProviderImpl implements OpenFlowPluginProvider, OpenFlowPluginExtensionRegistratorProvider {
57
58     private static final Logger LOG = LoggerFactory.getLogger(OpenFlowPluginProviderImpl.class);
59     private static final MessageIntelligenceAgency messageIntelligenceAgency = new MessageIntelligenceAgencyImpl();
60
61     private final int rpcRequestsQuota;
62     private final long globalNotificationQuota;
63     private DeviceManager deviceManager;
64     private RpcManager rpcManager;
65     private RpcProviderRegistry rpcProviderRegistry;
66     private StatisticsManager statisticsManager;
67     private ConnectionManager connectionManager;
68     private NotificationService notificationProviderService;
69     private NotificationPublishService notificationPublishService;
70
71     private ExtensionConverterManager extensionConverterManager;
72
73     private DataBroker dataBroker;
74     private OfpRole role;
75     private Collection<SwitchConnectionProvider> switchConnectionProviders;
76     private boolean switchFeaturesMandatory = false;
77     private boolean isStatisticsPollingOff = false;
78
79     public OpenFlowPluginProviderImpl(final long rpcRequestsQuota, final Long globalNotificationQuota) {
80         Preconditions.checkArgument(rpcRequestsQuota > 0 && rpcRequestsQuota <= Integer.MAX_VALUE, "rpcRequestQuota has to be in range <1,%s>", Integer.MAX_VALUE);
81         this.rpcRequestsQuota = (int) rpcRequestsQuota;
82         this.globalNotificationQuota = Preconditions.checkNotNull(globalNotificationQuota);
83     }
84
85     @Override
86     public boolean isStatisticsPollingOff() {
87         return isStatisticsPollingOff;
88     }
89
90     @Override
91     public void setIsStatisticsPollingOff(final boolean isStatisticsPollingOff) {
92         this.isStatisticsPollingOff = isStatisticsPollingOff;
93     }
94
95     private void startSwitchConnections() {
96         final List<ListenableFuture<Boolean>> starterChain = new ArrayList<>(switchConnectionProviders.size());
97         for (final SwitchConnectionProvider switchConnectionPrv : switchConnectionProviders) {
98             switchConnectionPrv.setSwitchConnectionHandler(connectionManager);
99             final ListenableFuture<Boolean> isOnlineFuture = switchConnectionPrv.startup();
100             starterChain.add(isOnlineFuture);
101         }
102
103         final ListenableFuture<List<Boolean>> srvStarted = Futures.allAsList(starterChain);
104         Futures.addCallback(srvStarted, new FutureCallback<List<Boolean>>() {
105             @Override
106             public void onSuccess(final List<Boolean> result) {
107                 LOG.info("All switchConnectionProviders are up and running ({}).",
108                         result.size());
109             }
110
111             @Override
112             public void onFailure(final Throwable t) {
113                 LOG.warn("Some switchConnectionProviders failed to start.", t);
114             }
115         });
116     }
117
118     public boolean isSwitchFeaturesMandatory() {
119         return switchFeaturesMandatory;
120     }
121
122     public void setSwitchFeaturesMandatory(final boolean switchFeaturesMandatory) {
123         this.switchFeaturesMandatory = switchFeaturesMandatory;
124     }
125
126     public static MessageIntelligenceAgency getMessageIntelligenceAgency() {
127         return OpenFlowPluginProviderImpl.messageIntelligenceAgency;
128     }
129
130     @Override
131     public void setSwitchConnectionProviders(final Collection<SwitchConnectionProvider> switchConnectionProviders) {
132         this.switchConnectionProviders = switchConnectionProviders;
133     }
134
135     @Override
136     public void setDataBroker(final DataBroker dataBroker) {
137         this.dataBroker = dataBroker;
138     }
139
140     @Override
141     public void setRpcProviderRegistry(final RpcProviderRegistry rpcProviderRegistry) {
142         this.rpcProviderRegistry = rpcProviderRegistry;
143     }
144
145     @Override
146     public void setRole(final OfpRole role) {
147         this.role = role;
148     }
149
150
151     @Override
152     public void initialize() {
153
154         Preconditions.checkNotNull(dataBroker, "missing data broker");
155         Preconditions.checkNotNull(rpcProviderRegistry, "missing RPC provider registry");
156         Preconditions.checkNotNull(notificationProviderService, "missing notification provider service");
157
158         extensionConverterManager = new ExtensionConverterManagerImpl();
159         // TODO: copied from OpenFlowPluginProvider (Helium) misusesing the old way of distributing extension converters
160         // TODO: rewrite later!
161         OFSessionUtil.getSessionManager().setExtensionConverterProvider(extensionConverterManager);
162
163         connectionManager = new ConnectionManagerImpl();
164
165         registerMXBean(messageIntelligenceAgency);
166
167         deviceManager = new DeviceManagerImpl(dataBroker, messageIntelligenceAgency, switchFeaturesMandatory, globalNotificationQuota);
168         statisticsManager = new StatisticsManagerImpl();
169         rpcManager = new RpcManagerImpl(rpcProviderRegistry, rpcRequestsQuota);
170
171         connectionManager.setDeviceConnectedHandler(deviceManager);
172         deviceManager.setDeviceInitializationPhaseHandler(statisticsManager);
173         deviceManager.setNotificationService(this.notificationProviderService);
174         deviceManager.setNotificationPublishService(this.notificationPublishService);
175         statisticsManager.setDeviceInitializationPhaseHandler(rpcManager);
176         rpcManager.setDeviceInitializationPhaseHandler(deviceManager);
177
178         TranslatorLibraryUtil.setBasicTranslatorLibrary(deviceManager);
179         deviceManager.initialize();
180
181         startSwitchConnections();
182     }
183
184     private static void registerMXBean(final MessageIntelligenceAgency messageIntelligenceAgency) {
185         MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
186         try {
187             String pathToMxBean = String.format("%s:type=%s",
188                     MessageIntelligenceAgencyMXBean.class.getPackage().getName(),
189                     MessageIntelligenceAgencyMXBean.class.getSimpleName());
190             ObjectName name = new ObjectName(pathToMxBean);
191             mbs.registerMBean(messageIntelligenceAgency, name);
192         } catch (MalformedObjectNameException
193                 | NotCompliantMBeanException
194                 | MBeanRegistrationException
195                 | InstanceAlreadyExistsException e) {
196             LOG.warn("Error registering MBean {}", e);
197         }
198     }
199
200     @Override
201     public void setNotificationProviderService(final NotificationService notificationProviderService) {
202         this.notificationProviderService = notificationProviderService;
203     }
204
205     @Override
206     public void setNotificationPublishService(final NotificationPublishService notificationPublishProviderService) {
207         this.notificationPublishService = notificationPublishProviderService;
208     }
209
210     @Override
211     public ExtensionConverterRegistrator getExtensionConverterRegistrator() {
212         return extensionConverterManager;
213     }
214
215     @Override
216     public void close() throws Exception {
217         //TODO: close all contexts, switchConnections (, managers)
218     }
219 }