2 * Copyright (c) 2013 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.openflow.md.core;
11 import java.util.ArrayList;
12 import java.util.Collection;
13 import java.util.LinkedHashSet;
14 import java.util.List;
16 import java.util.concurrent.BlockingQueue;
17 import java.util.concurrent.ConcurrentHashMap;
18 import java.util.concurrent.ConcurrentMap;
19 import java.util.concurrent.ExecutionException;
20 import java.util.concurrent.Future;
21 import java.util.concurrent.LinkedBlockingQueue;
22 import java.util.concurrent.RejectedExecutionException;
23 import java.util.concurrent.RejectedExecutionHandler;
24 import java.util.concurrent.ThreadPoolExecutor;
25 import java.util.concurrent.TimeUnit;
26 import java.util.concurrent.TimeoutException;
28 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionConfiguration;
29 import org.opendaylight.openflowjava.protocol.spi.connection.SwitchConnectionProvider;
30 import org.opendaylight.openflowplugin.openflow.md.OFConstants;
31 import org.opendaylight.openflowplugin.openflow.md.core.extension.ExtensionConverterProvider;
32 import org.opendaylight.openflowplugin.openflow.md.core.session.OFSessionUtil;
33 import org.opendaylight.openflowplugin.openflow.md.core.translator.ErrorTranslator;
34 import org.opendaylight.openflowplugin.openflow.md.core.translator.ErrorV10Translator;
35 import org.opendaylight.openflowplugin.openflow.md.core.translator.ExperimenterTranslator;
36 import org.opendaylight.openflowplugin.openflow.md.core.translator.FeaturesV10ToNodeConnectorUpdatedTranslator;
37 import org.opendaylight.openflowplugin.openflow.md.core.translator.FlowRemovedTranslator;
38 import org.opendaylight.openflowplugin.openflow.md.core.translator.MultiPartMessageDescToNodeUpdatedTranslator;
39 import org.opendaylight.openflowplugin.openflow.md.core.translator.MultiPartReplyPortToNodeConnectorUpdatedTranslator;
40 import org.opendaylight.openflowplugin.openflow.md.core.translator.MultipartReplyTableFeaturesToTableUpdatedTranslator;
41 import org.opendaylight.openflowplugin.openflow.md.core.translator.MultipartReplyTranslator;
42 import org.opendaylight.openflowplugin.openflow.md.core.translator.PacketInTranslator;
43 import org.opendaylight.openflowplugin.openflow.md.core.translator.PacketInV10Translator;
44 import org.opendaylight.openflowplugin.openflow.md.core.translator.PortStatusMessageToNodeConnectorUpdatedTranslator;
45 import org.opendaylight.openflowplugin.openflow.md.lldp.LLDPSpeakerPopListener;
46 import org.opendaylight.openflowplugin.openflow.md.queue.PopListener;
47 import org.opendaylight.openflowplugin.openflow.md.util.OpenflowPortsUtil;
48 import org.opendaylight.openflowplugin.statistics.MessageSpy;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.NodeErrorNotification;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SwitchFlowRemoved;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.AggregateFlowStatisticsUpdate;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowsStatisticsUpdate;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.FlowTableStatisticsUpdate;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GroupDescStatsUpdated;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GroupFeaturesUpdated;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GroupStatisticsUpdated;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorUpdated;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeUpdated;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.MeterConfigStatsUpdated;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.MeterFeaturesUpdated;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.MeterStatisticsUpdated;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.node.error.service.rev140410.BadActionErrorNotification;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.node.error.service.rev140410.BadInstructionErrorNotification;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.node.error.service.rev140410.BadMatchErrorNotification;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.node.error.service.rev140410.BadRequestErrorNotification;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.node.error.service.rev140410.ExperimenterErrorNotification;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.node.error.service.rev140410.FlowModErrorNotification;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.node.error.service.rev140410.GroupModErrorNotification;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.node.error.service.rev140410.HelloFailedErrorNotification;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.node.error.service.rev140410.MeterModErrorNotification;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.node.error.service.rev140410.PortModErrorNotification;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.node.error.service.rev140410.QueueOpErrorNotification;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.node.error.service.rev140410.RoleRequestErrorNotification;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.node.error.service.rev140410.SwitchConfigErrorNotification;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.node.error.service.rev140410.TableFeaturesErrorNotification;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.node.error.service.rev140410.TableModErrorNotification;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ErrorMessage;
78 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ExperimenterMessage;
79 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowRemovedMessage;
80 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
81 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReplyMessage;
82 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
83 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessage;
84 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortStatusMessage;
85 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketReceived;
86 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.TransmitPacketInput;
87 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.NodeConnectorStatisticsUpdate;
88 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.QueueStatisticsUpdate;
89 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.TableUpdated;
90 import org.opendaylight.yangtools.yang.binding.DataContainer;
91 import org.opendaylight.yangtools.yang.binding.DataObject;
92 import org.slf4j.Logger;
93 import org.slf4j.LoggerFactory;
95 import com.google.common.collect.Lists;
96 import com.google.common.util.concurrent.ForwardingBlockingQueue;
97 import com.google.common.util.concurrent.Futures;
98 import com.google.common.util.concurrent.ListenableFuture;
99 import com.google.common.util.concurrent.ListeningExecutorService;
100 import com.google.common.util.concurrent.MoreExecutors;
105 public class MDController implements IMDController, AutoCloseable {
107 private static final Logger LOG = LoggerFactory.getLogger(MDController.class);
109 private Collection<SwitchConnectionProvider> switchConnectionProviders;
111 private ConcurrentMap<TranslatorKey, Collection<IMDMessageTranslator<OfHeader, List<DataObject>>>> messageTranslators;
112 private Map<Class<? extends DataObject>, Collection<PopListener<DataObject>>> popListeners;
113 private MessageSpy<DataContainer> messageSpyCounter;
115 final private int OF10 = OFConstants.OFP_VERSION_1_0;
116 final private int OF13 = OFConstants.OFP_VERSION_1_3;
118 private ErrorHandlerSimpleImpl errorHandler;
120 private ExtensionConverterProvider extensionConverterProvider;
123 * @return translator mapping
125 public Map<TranslatorKey, Collection<IMDMessageTranslator<OfHeader, List<DataObject>>>> getMessageTranslators() {
126 return messageTranslators;
130 * provisioning of translator mapping
135 OpenflowPortsUtil.init();
137 messageTranslators = new ConcurrentHashMap<>();
138 popListeners = new ConcurrentHashMap<>();
139 //TODO: move registration to factory
140 addMessageTranslator(ErrorMessage.class, OF10, new ErrorV10Translator());
141 addMessageTranslator(ErrorMessage.class, OF13, new ErrorTranslator());
142 addMessageTranslator(FlowRemovedMessage.class, OF10, new FlowRemovedTranslator());
143 addMessageTranslator(FlowRemovedMessage.class, OF13, new FlowRemovedTranslator());
144 addMessageTranslator(PacketInMessage.class,OF10, new PacketInV10Translator());
145 addMessageTranslator(PacketInMessage.class,OF13, new PacketInTranslator());
146 addMessageTranslator(PortStatusMessage.class,OF10, new PortStatusMessageToNodeConnectorUpdatedTranslator());
147 addMessageTranslator(PortStatusMessage.class,OF13, new PortStatusMessageToNodeConnectorUpdatedTranslator());
148 addMessageTranslator(MultipartReplyMessage.class,OF13,new MultiPartReplyPortToNodeConnectorUpdatedTranslator());
149 addMessageTranslator(MultipartReplyMessage.class,OF10, new MultiPartMessageDescToNodeUpdatedTranslator());
150 addMessageTranslator(MultipartReplyMessage.class,OF13, new MultiPartMessageDescToNodeUpdatedTranslator());
151 addMessageTranslator(ExperimenterMessage.class, OF10, new ExperimenterTranslator());
152 addMessageTranslator(MultipartReplyMessage.class,OF10, new MultipartReplyTranslator());
153 addMessageTranslator(MultipartReplyMessage.class,OF13, new MultipartReplyTranslator());
154 addMessageTranslator(MultipartReplyMessage.class,OF13,new MultipartReplyTableFeaturesToTableUpdatedTranslator());
155 addMessageTranslator(GetFeaturesOutput.class,OF10, new FeaturesV10ToNodeConnectorUpdatedTranslator());
157 NotificationPopListener<DataObject> notificationPopListener = new NotificationPopListener<DataObject>();
158 notificationPopListener.setNotificationProviderService(
159 OFSessionUtil.getSessionManager().getNotificationProviderService());
160 notificationPopListener.setMessageSpy(messageSpyCounter);
162 //TODO: move registration to factory
163 addMessagePopListener(NodeErrorNotification.class, notificationPopListener);
164 addMessagePopListener(BadActionErrorNotification.class, notificationPopListener);
165 addMessagePopListener(BadInstructionErrorNotification.class, notificationPopListener);
166 addMessagePopListener(BadMatchErrorNotification.class, notificationPopListener);
167 addMessagePopListener(BadRequestErrorNotification.class, notificationPopListener);
168 addMessagePopListener(ExperimenterErrorNotification.class, notificationPopListener);
169 addMessagePopListener(FlowModErrorNotification.class, notificationPopListener);
170 addMessagePopListener(GroupModErrorNotification.class, notificationPopListener);
171 addMessagePopListener(HelloFailedErrorNotification.class, notificationPopListener);
172 addMessagePopListener(MeterModErrorNotification.class, notificationPopListener);
173 addMessagePopListener(PortModErrorNotification.class, notificationPopListener);
174 addMessagePopListener(QueueOpErrorNotification.class, notificationPopListener);
175 addMessagePopListener(RoleRequestErrorNotification.class, notificationPopListener);
176 addMessagePopListener(SwitchConfigErrorNotification.class, notificationPopListener);
177 addMessagePopListener(TableFeaturesErrorNotification.class, notificationPopListener);
178 addMessagePopListener(TableModErrorNotification.class, notificationPopListener);
179 addMessagePopListener(NodeConnectorUpdated.class,notificationPopListener);
180 addMessagePopListener(PacketReceived.class,notificationPopListener);
181 addMessagePopListener(TransmitPacketInput.class, notificationPopListener);
182 addMessagePopListener(NodeUpdated.class, notificationPopListener);
184 addMessagePopListener(SwitchFlowRemoved.class, notificationPopListener);
185 addMessagePopListener(TableUpdated.class, notificationPopListener);
187 //Notification registration for flow statistics
188 addMessagePopListener(FlowsStatisticsUpdate.class, notificationPopListener);
189 addMessagePopListener(AggregateFlowStatisticsUpdate.class, notificationPopListener);
191 //Notification registrations for group-statistics
192 addMessagePopListener(GroupStatisticsUpdated.class, notificationPopListener);
193 addMessagePopListener(GroupFeaturesUpdated.class, notificationPopListener);
194 addMessagePopListener(GroupDescStatsUpdated.class, notificationPopListener);
196 //Notification registrations for meter-statistics
197 addMessagePopListener(MeterStatisticsUpdated.class, notificationPopListener);
198 addMessagePopListener(MeterConfigStatsUpdated.class, notificationPopListener);
199 addMessagePopListener(MeterFeaturesUpdated.class, notificationPopListener);
201 //Notification registration for port-statistics
202 addMessagePopListener(NodeConnectorStatisticsUpdate.class, notificationPopListener);
204 //Notification registration for flow-table statistics
205 addMessagePopListener(FlowTableStatisticsUpdate.class, notificationPopListener);
207 //Notification registration for queue-statistics
208 addMessagePopListener(QueueStatisticsUpdate.class, notificationPopListener);
210 //Notification for LLDPSpeaker
211 LLDPSpeakerPopListener<DataObject> lldpPopListener = new LLDPSpeakerPopListener<DataObject>();
212 addMessagePopListener(NodeConnectorUpdated.class,lldpPopListener);
214 // Push the updated Listeners to Session Manager which will be then picked up by ConnectionConductor eventually
215 OFSessionUtil.getSessionManager().setTranslatorMapping(messageTranslators);
216 OFSessionUtil.getSessionManager().setPopListenerMapping(popListeners);
217 OFSessionUtil.getSessionManager().setMessageSpy(messageSpyCounter);
219 // prepare worker pool for rpc
220 // TODO: get size from configSubsystem
221 int rpcThreadLimit = 10;
222 ListeningExecutorService rpcPoolDelegator = createRpcPoolSpyDecorated(rpcThreadLimit, messageSpyCounter);
223 OFSessionUtil.getSessionManager().setRpcPool(rpcPoolDelegator);
224 OFSessionUtil.getSessionManager().setExtensionConverterProvider(extensionConverterProvider);
229 * @param rpcThreadLimit
233 private static ListeningExecutorService createRpcPoolSpyDecorated(final int rpcThreadLimit, final MessageSpy<DataContainer> messageSpy) {
234 final BlockingQueue<Runnable> delegate = new LinkedBlockingQueue<>(100000);
235 final BlockingQueue<Runnable> queue = new ForwardingBlockingQueue<Runnable>() {
237 protected BlockingQueue<Runnable> delegate() {
242 public boolean offer(final Runnable r) {
243 // ThreadPoolExecutor will spawn a new thread after core size is reached only
244 // if the queue.offer returns false.
249 ThreadPoolLoggingExecutor rpcPool = new ThreadPoolLoggingExecutor(rpcThreadLimit, rpcThreadLimit, 0L,
250 TimeUnit.MILLISECONDS, queue, "OFRpc");
251 rpcPool.setRejectedExecutionHandler(new RejectedExecutionHandler() {
253 public void rejectedExecution(final Runnable r, final ThreadPoolExecutor executor) {
255 executor.getQueue().put(r);
256 } catch (InterruptedException e) {
257 throw new RejectedExecutionException("Interrupted while waiting on queue", e);
262 ListeningExecutorService listeningRpcPool = MoreExecutors.listeningDecorator(rpcPool);
263 RpcListeningExecutorService rpcPoolDecorated = new RpcListeningExecutorService(listeningRpcPool);
264 rpcPoolDecorated.setMessageSpy(messageSpy);
265 return rpcPoolDecorated;
269 * @param switchConnectionProviders
270 * the switchConnectionProviders to set
272 public void setSwitchConnectionProviders(final Collection<SwitchConnectionProvider> switchConnectionProviders) {
273 this.switchConnectionProviders = switchConnectionProviders;
277 * Function called by dependency manager after "init ()" is called and after
278 * the services provided by the class are registered in the service registry
281 public void start() {
282 LOG.debug("starting ..");
283 LOG.debug("switchConnectionProvider: " + switchConnectionProviders);
285 SwitchConnectionHandlerImpl switchConnectionHandler = new SwitchConnectionHandlerImpl();
286 switchConnectionHandler.setMessageSpy(messageSpyCounter);
288 errorHandler = new ErrorHandlerSimpleImpl();
290 switchConnectionHandler.setErrorHandler(errorHandler);
291 switchConnectionHandler.init();
293 List<ListenableFuture<Boolean>> starterChain = new ArrayList<>(switchConnectionProviders.size());
294 for (SwitchConnectionProvider switchConnectionPrv : switchConnectionProviders) {
295 switchConnectionPrv.setSwitchConnectionHandler(switchConnectionHandler);
296 ListenableFuture<Boolean> isOnlineFuture = switchConnectionPrv.startup();
297 starterChain.add(isOnlineFuture);
300 Future<List<Boolean>> srvStarted = Futures.allAsList(starterChain);
304 * @return wished connections configurations
305 * @deprecated use configSubsystem
308 private static Collection<ConnectionConfiguration> getConnectionConfiguration() {
309 // TODO:: get config from state manager
310 ConnectionConfiguration configuration = ConnectionConfigurationFactory.getDefault();
311 ConnectionConfiguration configurationLegacy = ConnectionConfigurationFactory.getLegacy();
312 return Lists.newArrayList(configuration, configurationLegacy);
316 * Function called by the dependency manager before the services exported by
317 * the component are unregistered, this will be followed by a "destroy ()"
322 LOG.debug("stopping");
323 List<ListenableFuture<Boolean>> stopChain = new ArrayList<>(switchConnectionProviders.size());
325 for (SwitchConnectionProvider switchConnectionPrv : switchConnectionProviders) {
326 ListenableFuture<Boolean> shutdown = switchConnectionPrv.shutdown();
327 stopChain.add(shutdown);
329 Futures.allAsList(stopChain).get(5000, TimeUnit.MILLISECONDS);
330 } catch (InterruptedException | ExecutionException | TimeoutException e) {
331 LOG.error(e.getMessage(), e);
337 * Function called by the dependency manager when at least one dependency
338 * become unsatisfied or when the component is shutting down because for
339 * example bundle is being stopped.
342 public void destroy() {
347 public void addMessageTranslator(final Class<? extends DataObject> messageType, final int version, final IMDMessageTranslator<OfHeader, List<DataObject>> translator) {
348 TranslatorKey tKey = new TranslatorKey(version, messageType.getName());
350 Collection<IMDMessageTranslator<OfHeader, List<DataObject>>> existingValues = messageTranslators.get(tKey);
351 if (existingValues == null) {
352 existingValues = new LinkedHashSet<>();
353 messageTranslators.put(tKey, existingValues);
355 existingValues.add(translator);
356 LOG.debug("{} is now translated by {}", messageType, translator);
360 public void removeMessageTranslator(final Class<? extends DataObject> messageType, final int version, final IMDMessageTranslator<OfHeader, List<DataObject>> translator) {
361 TranslatorKey tKey = new TranslatorKey(version, messageType.getName());
362 Collection<IMDMessageTranslator<OfHeader, List<DataObject>>> values = messageTranslators.get(tKey);
363 if (values != null) {
364 values.remove(translator);
365 if (values.isEmpty()) {
366 messageTranslators.remove(tKey);
368 LOG.debug("{} is now removed from translators", translator);
373 public void addMessagePopListener(final Class<? extends DataObject> messageType, final PopListener<DataObject> popListener) {
374 Collection<PopListener<DataObject>> existingValues = popListeners.get(messageType);
375 if (existingValues == null) {
376 existingValues = new LinkedHashSet<>();
377 popListeners.put(messageType, existingValues);
379 existingValues.add(popListener);
380 LOG.debug("{} is now popListened by {}", messageType, popListener);
384 public void removeMessagePopListener(final Class<? extends DataObject> messageType, final PopListener<DataObject> popListener) {
385 Collection<PopListener<DataObject>> values = popListeners.get(messageType);
386 if (values != null) {
387 values.remove(popListener);
388 if (values.isEmpty()) {
389 popListeners.remove(messageType);
391 LOG.debug("{} is now removed from popListeners", popListener);
396 * @param messageSpyCounter the messageSpyCounter to set
398 public void setMessageSpyCounter(
399 final MessageSpy<DataContainer> messageSpyCounter) {
400 this.messageSpyCounter = messageSpyCounter;
404 public void close() {
406 messageSpyCounter = null;
407 messageTranslators = null;
409 for (SwitchConnectionProvider switchConnectionPrv : switchConnectionProviders) {
410 switchConnectionPrv.setSwitchConnectionHandler(null);
412 switchConnectionProviders = null;
413 OpenflowPortsUtil.close();
414 OFSessionUtil.releaseSessionManager();
419 * @param extensionConverterProvider
421 public void setExtensionConverterProvider(ExtensionConverterProvider extensionConverterProvider) {
422 this.extensionConverterProvider = extensionConverterProvider;