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 com.google.common.util.concurrent.ForwardingBlockingQueue;
12 import com.google.common.util.concurrent.Futures;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import com.google.common.util.concurrent.ListeningExecutorService;
15 import com.google.common.util.concurrent.MoreExecutors;
16 import java.util.ArrayList;
17 import java.util.Collection;
18 import java.util.LinkedHashSet;
19 import java.util.List;
21 import java.util.concurrent.BlockingQueue;
22 import java.util.concurrent.ConcurrentHashMap;
23 import java.util.concurrent.ConcurrentMap;
24 import java.util.concurrent.ExecutionException;
25 import java.util.concurrent.Future;
26 import java.util.concurrent.LinkedBlockingQueue;
27 import java.util.concurrent.RejectedExecutionException;
28 import java.util.concurrent.RejectedExecutionHandler;
29 import java.util.concurrent.ThreadPoolExecutor;
30 import java.util.concurrent.TimeUnit;
31 import java.util.concurrent.TimeoutException;
32 import org.opendaylight.openflowjava.protocol.spi.connection.SwitchConnectionProvider;
33 import org.opendaylight.openflowplugin.api.OFConstants;
34 import org.opendaylight.openflowplugin.api.openflow.md.core.IMDMessageTranslator;
35 import org.opendaylight.openflowplugin.api.openflow.md.core.NotificationQueueWrapper;
36 import org.opendaylight.openflowplugin.api.openflow.md.core.TranslatorKey;
37 import org.opendaylight.openflowplugin.api.openflow.md.queue.PopListener;
38 import org.opendaylight.openflowplugin.api.openflow.statistics.MessageSpy;
39 import org.opendaylight.openflowplugin.extension.api.core.extension.ExtensionConverterProvider;
40 import org.opendaylight.openflowplugin.openflow.md.core.session.OFSessionUtil;
41 import org.opendaylight.openflowplugin.openflow.md.core.translator.ErrorTranslator;
42 import org.opendaylight.openflowplugin.openflow.md.core.translator.ErrorV10Translator;
43 import org.opendaylight.openflowplugin.openflow.md.core.translator.ExperimenterTranslator;
44 import org.opendaylight.openflowplugin.openflow.md.core.translator.FeaturesV10ToNodeConnectorUpdatedTranslator;
45 import org.opendaylight.openflowplugin.openflow.md.core.translator.FlowRemovedTranslator;
46 import org.opendaylight.openflowplugin.openflow.md.core.translator.MultiPartMessageDescToNodeUpdatedTranslator;
47 import org.opendaylight.openflowplugin.openflow.md.core.translator.MultiPartReplyPortToNodeConnectorUpdatedTranslator;
48 import org.opendaylight.openflowplugin.openflow.md.core.translator.MultipartReplyTableFeaturesToTableUpdatedTranslator;
49 import org.opendaylight.openflowplugin.openflow.md.core.translator.MultipartReplyTranslator;
50 import org.opendaylight.openflowplugin.openflow.md.core.translator.NotificationPlainTranslator;
51 import org.opendaylight.openflowplugin.openflow.md.core.translator.PacketInTranslator;
52 import org.opendaylight.openflowplugin.openflow.md.core.translator.PacketInV10Translator;
53 import org.opendaylight.openflowplugin.openflow.md.core.translator.PortStatusMessageToNodeConnectorUpdatedTranslator;
54 import org.opendaylight.openflowplugin.openflow.md.util.OpenflowPortsUtil;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.NodeErrorNotification;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SwitchFlowRemoved;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.AggregateFlowStatisticsUpdate;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowsStatisticsUpdate;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.FlowTableStatisticsUpdate;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GroupDescStatsUpdated;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GroupFeaturesUpdated;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GroupStatisticsUpdated;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRemoved;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorUpdated;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRemoved;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeUpdated;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.MeterConfigStatsUpdated;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.MeterFeaturesUpdated;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.MeterStatisticsUpdated;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.node.error.service.rev140410.BadActionErrorNotification;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.node.error.service.rev140410.BadInstructionErrorNotification;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.node.error.service.rev140410.BadMatchErrorNotification;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.node.error.service.rev140410.BadRequestErrorNotification;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.node.error.service.rev140410.ExperimenterErrorNotification;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.node.error.service.rev140410.FlowModErrorNotification;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.node.error.service.rev140410.GroupModErrorNotification;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.node.error.service.rev140410.HelloFailedErrorNotification;
78 import org.opendaylight.yang.gen.v1.urn.opendaylight.node.error.service.rev140410.MeterModErrorNotification;
79 import org.opendaylight.yang.gen.v1.urn.opendaylight.node.error.service.rev140410.PortModErrorNotification;
80 import org.opendaylight.yang.gen.v1.urn.opendaylight.node.error.service.rev140410.QueueOpErrorNotification;
81 import org.opendaylight.yang.gen.v1.urn.opendaylight.node.error.service.rev140410.RoleRequestErrorNotification;
82 import org.opendaylight.yang.gen.v1.urn.opendaylight.node.error.service.rev140410.SwitchConfigErrorNotification;
83 import org.opendaylight.yang.gen.v1.urn.opendaylight.node.error.service.rev140410.TableFeaturesErrorNotification;
84 import org.opendaylight.yang.gen.v1.urn.opendaylight.node.error.service.rev140410.TableModErrorNotification;
85 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ErrorMessage;
86 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ExperimenterMessage;
87 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowRemovedMessage;
88 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
89 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReplyMessage;
90 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
91 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessage;
92 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortStatusMessage;
93 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketReceived;
94 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.TransmitPacketInput;
95 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.NodeConnectorStatisticsUpdate;
96 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.QueueStatisticsUpdate;
97 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.TableUpdated;
98 import org.opendaylight.yangtools.yang.binding.DataContainer;
99 import org.opendaylight.yangtools.yang.binding.DataObject;
100 import org.slf4j.Logger;
101 import org.slf4j.LoggerFactory;
106 public class MDController implements IMDController, AutoCloseable {
108 private static final Logger LOG = LoggerFactory.getLogger(MDController.class);
110 private Collection<SwitchConnectionProvider> switchConnectionProviders;
112 private ConcurrentMap<TranslatorKey, Collection<IMDMessageTranslator<OfHeader, List<DataObject>>>> messageTranslators;
113 private Map<Class<? extends DataObject>, Collection<PopListener<DataObject>>> popListeners;
114 private MessageSpy<DataContainer> messageSpyCounter;
116 final private int OF10 = OFConstants.OFP_VERSION_1_0;
117 final private int OF13 = OFConstants.OFP_VERSION_1_3;
119 private ErrorHandlerSimpleImpl errorHandler;
121 private ExtensionConverterProvider extensionConverterProvider;
124 * @return translator mapping
126 public Map<TranslatorKey, Collection<IMDMessageTranslator<OfHeader, List<DataObject>>>> getMessageTranslators() {
127 return messageTranslators;
131 * provisioning of translator mapping
136 OpenflowPortsUtil.init();
138 messageTranslators = new ConcurrentHashMap<>();
139 popListeners = new ConcurrentHashMap<>();
140 //TODO: move registration to factory
141 addMessageTranslator(ErrorMessage.class, OF10, new ErrorV10Translator());
142 addMessageTranslator(ErrorMessage.class, OF13, new ErrorTranslator());
143 addMessageTranslator(FlowRemovedMessage.class, OF10, new FlowRemovedTranslator());
144 addMessageTranslator(FlowRemovedMessage.class, OF13, new FlowRemovedTranslator());
145 addMessageTranslator(PacketInMessage.class,OF10, new PacketInV10Translator());
146 addMessageTranslator(PacketInMessage.class,OF13, new PacketInTranslator());
147 addMessageTranslator(PortStatusMessage.class,OF10, new PortStatusMessageToNodeConnectorUpdatedTranslator());
148 addMessageTranslator(PortStatusMessage.class,OF13, new PortStatusMessageToNodeConnectorUpdatedTranslator());
149 addMessageTranslator(MultipartReplyMessage.class,OF13,new MultiPartReplyPortToNodeConnectorUpdatedTranslator());
150 addMessageTranslator(MultipartReplyMessage.class,OF10, new MultiPartMessageDescToNodeUpdatedTranslator());
151 addMessageTranslator(MultipartReplyMessage.class,OF13, new MultiPartMessageDescToNodeUpdatedTranslator());
152 addMessageTranslator(ExperimenterMessage.class, OF10, new ExperimenterTranslator());
153 addMessageTranslator(MultipartReplyMessage.class,OF10, new MultipartReplyTranslator());
154 addMessageTranslator(MultipartReplyMessage.class,OF13, new MultipartReplyTranslator());
155 addMessageTranslator(MultipartReplyMessage.class,OF13,new MultipartReplyTableFeaturesToTableUpdatedTranslator());
156 addMessageTranslator(GetFeaturesOutput.class,OF10, new FeaturesV10ToNodeConnectorUpdatedTranslator());
157 addMessageTranslator(NotificationQueueWrapper.class, OF10, new NotificationPlainTranslator());
158 addMessageTranslator(NotificationQueueWrapper.class, OF13, new NotificationPlainTranslator());
160 NotificationPopListener<DataObject> notificationPopListener = new NotificationPopListener<DataObject>();
161 notificationPopListener.setNotificationProviderService(
162 OFSessionUtil.getSessionManager().getNotificationProviderService());
163 notificationPopListener.setMessageSpy(messageSpyCounter);
165 //TODO: move registration to factory
166 addMessagePopListener(NodeErrorNotification.class, notificationPopListener);
167 addMessagePopListener(BadActionErrorNotification.class, notificationPopListener);
168 addMessagePopListener(BadInstructionErrorNotification.class, notificationPopListener);
169 addMessagePopListener(BadMatchErrorNotification.class, notificationPopListener);
170 addMessagePopListener(BadRequestErrorNotification.class, notificationPopListener);
171 addMessagePopListener(ExperimenterErrorNotification.class, notificationPopListener);
172 addMessagePopListener(FlowModErrorNotification.class, notificationPopListener);
173 addMessagePopListener(GroupModErrorNotification.class, notificationPopListener);
174 addMessagePopListener(HelloFailedErrorNotification.class, notificationPopListener);
175 addMessagePopListener(MeterModErrorNotification.class, notificationPopListener);
176 addMessagePopListener(PortModErrorNotification.class, notificationPopListener);
177 addMessagePopListener(QueueOpErrorNotification.class, notificationPopListener);
178 addMessagePopListener(RoleRequestErrorNotification.class, notificationPopListener);
179 addMessagePopListener(SwitchConfigErrorNotification.class, notificationPopListener);
180 addMessagePopListener(TableFeaturesErrorNotification.class, notificationPopListener);
181 addMessagePopListener(TableModErrorNotification.class, notificationPopListener);
182 addMessagePopListener(NodeConnectorUpdated.class,notificationPopListener);
183 addMessagePopListener(NodeConnectorRemoved.class,notificationPopListener);
184 addMessagePopListener(PacketReceived.class,notificationPopListener);
185 addMessagePopListener(TransmitPacketInput.class, notificationPopListener);
186 addMessagePopListener(NodeUpdated.class, notificationPopListener);
187 addMessagePopListener(NodeRemoved.class, notificationPopListener);
189 addMessagePopListener(SwitchFlowRemoved.class, notificationPopListener);
190 addMessagePopListener(TableUpdated.class, notificationPopListener);
192 //Notification registration for flow statistics
193 addMessagePopListener(FlowsStatisticsUpdate.class, notificationPopListener);
194 addMessagePopListener(AggregateFlowStatisticsUpdate.class, notificationPopListener);
196 //Notification registrations for group-statistics
197 addMessagePopListener(GroupStatisticsUpdated.class, notificationPopListener);
198 addMessagePopListener(GroupFeaturesUpdated.class, notificationPopListener);
199 addMessagePopListener(GroupDescStatsUpdated.class, notificationPopListener);
201 //Notification registrations for meter-statistics
202 addMessagePopListener(MeterStatisticsUpdated.class, notificationPopListener);
203 addMessagePopListener(MeterConfigStatsUpdated.class, notificationPopListener);
204 addMessagePopListener(MeterFeaturesUpdated.class, notificationPopListener);
206 //Notification registration for port-statistics
207 addMessagePopListener(NodeConnectorStatisticsUpdate.class, notificationPopListener);
209 //Notification registration for flow-table statistics
210 addMessagePopListener(FlowTableStatisticsUpdate.class, notificationPopListener);
212 //Notification registration for queue-statistics
213 addMessagePopListener(QueueStatisticsUpdate.class, notificationPopListener);
215 // Push the updated Listeners to Session Manager which will be then picked up by ConnectionConductor eventually
216 OFSessionUtil.getSessionManager().setTranslatorMapping(messageTranslators);
217 OFSessionUtil.getSessionManager().setPopListenerMapping(popListeners);
218 OFSessionUtil.getSessionManager().setMessageSpy(messageSpyCounter);
220 // prepare worker pool for rpc
221 // TODO: get size from configSubsystem
222 int rpcThreadLimit = 10;
223 ListeningExecutorService rpcPoolDelegator = createRpcPoolSpyDecorated(rpcThreadLimit, messageSpyCounter);
224 OFSessionUtil.getSessionManager().setRpcPool(rpcPoolDelegator);
225 OFSessionUtil.getSessionManager().setExtensionConverterProvider(extensionConverterProvider);
230 * @param rpcThreadLimit
234 private static ListeningExecutorService createRpcPoolSpyDecorated(final int rpcThreadLimit, final MessageSpy<DataContainer> messageSpy) {
235 final BlockingQueue<Runnable> delegate = new LinkedBlockingQueue<>(100000);
236 final BlockingQueue<Runnable> queue = new ForwardingBlockingQueue<Runnable>() {
238 protected BlockingQueue<Runnable> delegate() {
243 public boolean offer(final Runnable r) {
244 // ThreadPoolExecutor will spawn a new thread after core size is reached only
245 // if the queue.offer returns false.
250 ThreadPoolLoggingExecutor rpcPool = new ThreadPoolLoggingExecutor(rpcThreadLimit, rpcThreadLimit, 0L,
251 TimeUnit.MILLISECONDS, queue, "OFRpc");
252 rpcPool.setRejectedExecutionHandler(new RejectedExecutionHandler() {
254 public void rejectedExecution(final Runnable r, final ThreadPoolExecutor executor) {
256 executor.getQueue().put(r);
257 } catch (InterruptedException e) {
258 throw new RejectedExecutionException("Interrupted while waiting on queue", e);
263 ListeningExecutorService listeningRpcPool = MoreExecutors.listeningDecorator(rpcPool);
264 RpcListeningExecutorService rpcPoolDecorated = new RpcListeningExecutorService(listeningRpcPool);
265 rpcPoolDecorated.setMessageSpy(messageSpy);
266 return rpcPoolDecorated;
270 * @param switchConnectionProviders
271 * the switchConnectionProviders to set
273 public void setSwitchConnectionProviders(final Collection<SwitchConnectionProvider> switchConnectionProviders) {
274 this.switchConnectionProviders = switchConnectionProviders;
278 * Function called by dependency manager after "init ()" is called and after
279 * the services provided by the class are registered in the service registry
282 public void start() {
283 LOG.debug("starting ..");
284 LOG.debug("switchConnectionProvider: " + switchConnectionProviders);
286 SwitchConnectionHandlerImpl switchConnectionHandler = new SwitchConnectionHandlerImpl();
287 switchConnectionHandler.setMessageSpy(messageSpyCounter);
289 errorHandler = new ErrorHandlerSimpleImpl();
291 switchConnectionHandler.setErrorHandler(errorHandler);
292 switchConnectionHandler.init();
294 List<ListenableFuture<Boolean>> starterChain = new ArrayList<>(switchConnectionProviders.size());
295 for (SwitchConnectionProvider switchConnectionPrv : switchConnectionProviders) {
296 switchConnectionPrv.setSwitchConnectionHandler(switchConnectionHandler);
297 ListenableFuture<Boolean> isOnlineFuture = switchConnectionPrv.startup();
298 starterChain.add(isOnlineFuture);
301 Future<List<Boolean>> srvStarted = Futures.allAsList(starterChain);
305 * Function called by the dependency manager before the services exported by
306 * the component are unregistered, this will be followed by a "destroy ()"
311 LOG.debug("stopping");
312 List<ListenableFuture<Boolean>> stopChain = new ArrayList<>(switchConnectionProviders.size());
314 for (SwitchConnectionProvider switchConnectionPrv : switchConnectionProviders) {
315 ListenableFuture<Boolean> shutdown = switchConnectionPrv.shutdown();
316 stopChain.add(shutdown);
318 Futures.allAsList(stopChain).get(5000, TimeUnit.MILLISECONDS);
319 } catch (InterruptedException | ExecutionException | TimeoutException e) {
320 LOG.warn("failed to stop MDController: {}", e.getMessage());
321 LOG.debug("failed to stop MDController.. ", e);
327 * Function called by the dependency manager when at least one dependency
328 * become unsatisfied or when the component is shutting down because for
329 * example bundle is being stopped.
332 public void destroy() {
337 public void addMessageTranslator(final Class<? extends DataObject> messageType, final int version, final IMDMessageTranslator<OfHeader, List<DataObject>> translator) {
338 TranslatorKey tKey = new TranslatorKey(version, messageType.getName());
340 Collection<IMDMessageTranslator<OfHeader, List<DataObject>>> existingValues = messageTranslators.get(tKey);
341 if (existingValues == null) {
342 existingValues = new LinkedHashSet<>();
343 messageTranslators.put(tKey, existingValues);
345 existingValues.add(translator);
346 LOG.debug("{} is now translated by {}", messageType, translator);
350 public void removeMessageTranslator(final Class<? extends DataObject> messageType, final int version, final IMDMessageTranslator<OfHeader, List<DataObject>> translator) {
351 TranslatorKey tKey = new TranslatorKey(version, messageType.getName());
352 Collection<IMDMessageTranslator<OfHeader, List<DataObject>>> values = messageTranslators.get(tKey);
353 if (values != null) {
354 values.remove(translator);
355 if (values.isEmpty()) {
356 messageTranslators.remove(tKey);
358 LOG.debug("{} is now removed from translators", translator);
363 public void addMessagePopListener(final Class<? extends DataObject> messageType, final PopListener<DataObject> popListener) {
364 Collection<PopListener<DataObject>> existingValues = popListeners.get(messageType);
365 if (existingValues == null) {
366 existingValues = new LinkedHashSet<>();
367 popListeners.put(messageType, existingValues);
369 existingValues.add(popListener);
370 LOG.debug("{} is now popListened by {}", messageType, popListener);
374 public void removeMessagePopListener(final Class<? extends DataObject> messageType, final PopListener<DataObject> popListener) {
375 Collection<PopListener<DataObject>> values = popListeners.get(messageType);
376 if (values != null) {
377 values.remove(popListener);
378 if (values.isEmpty()) {
379 popListeners.remove(messageType);
381 LOG.debug("{} is now removed from popListeners", popListener);
386 * @param messageSpyCounter the messageSpyCounter to set
388 public void setMessageSpyCounter(
389 final MessageSpy<DataContainer> messageSpyCounter) {
390 this.messageSpyCounter = messageSpyCounter;
394 public void close() {
396 messageSpyCounter = null;
397 messageTranslators = null;
399 for (SwitchConnectionProvider switchConnectionPrv : switchConnectionProviders) {
400 switchConnectionPrv.setSwitchConnectionHandler(null);
402 switchConnectionProviders = null;
403 OpenflowPortsUtil.close();
404 OFSessionUtil.releaseSessionManager();
409 * @param extensionConverterProvider extension convertor provider
411 public void setExtensionConverterProvider(ExtensionConverterProvider extensionConverterProvider) {
412 this.extensionConverterProvider = extensionConverterProvider;