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