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.spi.connection.SwitchConnectionProvider;
29 import org.opendaylight.openflowplugin.api.OFConstants;
30 import org.opendaylight.openflowplugin.api.openflow.md.core.IMDMessageTranslator;
31 import org.opendaylight.openflowplugin.api.openflow.md.core.NotificationQueueWrapper;
32 import org.opendaylight.openflowplugin.api.openflow.md.core.TranslatorKey;
33 import org.opendaylight.openflowplugin.extension.api.core.extension.ExtensionConverterProvider;
34 import org.opendaylight.openflowplugin.openflow.md.core.session.OFSessionUtil;
35 import org.opendaylight.openflowplugin.openflow.md.core.translator.ErrorTranslator;
36 import org.opendaylight.openflowplugin.openflow.md.core.translator.ErrorV10Translator;
37 import org.opendaylight.openflowplugin.openflow.md.core.translator.ExperimenterTranslator;
38 import org.opendaylight.openflowplugin.openflow.md.core.translator.FeaturesV10ToNodeConnectorUpdatedTranslator;
39 import org.opendaylight.openflowplugin.openflow.md.core.translator.FlowRemovedTranslator;
40 import org.opendaylight.openflowplugin.openflow.md.core.translator.MultiPartMessageDescToNodeUpdatedTranslator;
41 import org.opendaylight.openflowplugin.openflow.md.core.translator.MultiPartReplyPortToNodeConnectorUpdatedTranslator;
42 import org.opendaylight.openflowplugin.openflow.md.core.translator.MultipartReplyTableFeaturesToTableUpdatedTranslator;
43 import org.opendaylight.openflowplugin.openflow.md.core.translator.MultipartReplyTranslator;
44 import org.opendaylight.openflowplugin.openflow.md.core.translator.NotificationPlainTranslator;
45 import org.opendaylight.openflowplugin.openflow.md.core.translator.PacketInTranslator;
46 import org.opendaylight.openflowplugin.openflow.md.core.translator.PacketInV10Translator;
47 import org.opendaylight.openflowplugin.openflow.md.core.translator.PortStatusMessageToNodeConnectorUpdatedTranslator;
48 import org.opendaylight.openflowplugin.api.openflow.md.queue.PopListener;
49 import org.opendaylight.openflowplugin.openflow.md.util.OpenflowPortsUtil;
50 import org.opendaylight.openflowplugin.api.statistics.MessageSpy;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.NodeErrorNotification;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SwitchFlowRemoved;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.AggregateFlowStatisticsUpdate;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowsStatisticsUpdate;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.FlowTableStatisticsUpdate;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GroupDescStatsUpdated;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GroupFeaturesUpdated;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GroupStatisticsUpdated;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorUpdated;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRemoved;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRemoved;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeUpdated;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.MeterConfigStatsUpdated;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.MeterFeaturesUpdated;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.MeterStatisticsUpdated;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.node.error.service.rev140410.BadActionErrorNotification;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.node.error.service.rev140410.BadInstructionErrorNotification;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.node.error.service.rev140410.BadMatchErrorNotification;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.node.error.service.rev140410.BadRequestErrorNotification;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.node.error.service.rev140410.ExperimenterErrorNotification;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.node.error.service.rev140410.FlowModErrorNotification;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.node.error.service.rev140410.GroupModErrorNotification;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.node.error.service.rev140410.HelloFailedErrorNotification;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.node.error.service.rev140410.MeterModErrorNotification;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.node.error.service.rev140410.PortModErrorNotification;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.node.error.service.rev140410.QueueOpErrorNotification;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.node.error.service.rev140410.RoleRequestErrorNotification;
78 import org.opendaylight.yang.gen.v1.urn.opendaylight.node.error.service.rev140410.SwitchConfigErrorNotification;
79 import org.opendaylight.yang.gen.v1.urn.opendaylight.node.error.service.rev140410.TableFeaturesErrorNotification;
80 import org.opendaylight.yang.gen.v1.urn.opendaylight.node.error.service.rev140410.TableModErrorNotification;
81 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ErrorMessage;
82 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ExperimenterMessage;
83 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowRemovedMessage;
84 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
85 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReplyMessage;
86 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
87 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessage;
88 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortStatusMessage;
89 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketReceived;
90 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.TransmitPacketInput;
91 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.NodeConnectorStatisticsUpdate;
92 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.QueueStatisticsUpdate;
93 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.TableUpdated;
94 import org.opendaylight.yangtools.yang.binding.DataContainer;
95 import org.opendaylight.yangtools.yang.binding.DataObject;
96 import org.slf4j.Logger;
97 import org.slf4j.LoggerFactory;
99 import com.google.common.util.concurrent.ForwardingBlockingQueue;
100 import com.google.common.util.concurrent.Futures;
101 import com.google.common.util.concurrent.ListenableFuture;
102 import com.google.common.util.concurrent.ListeningExecutorService;
103 import com.google.common.util.concurrent.MoreExecutors;
108 public class MDController implements IMDController, AutoCloseable {
110 private static final Logger LOG = LoggerFactory.getLogger(MDController.class);
112 private Collection<SwitchConnectionProvider> switchConnectionProviders;
114 private ConcurrentMap<TranslatorKey, Collection<IMDMessageTranslator<OfHeader, List<DataObject>>>> messageTranslators;
115 private Map<Class<? extends DataObject>, Collection<PopListener<DataObject>>> popListeners;
116 private MessageSpy<DataContainer> messageSpyCounter;
118 final private int OF10 = OFConstants.OFP_VERSION_1_0;
119 final private int OF13 = OFConstants.OFP_VERSION_1_3;
121 private ErrorHandlerSimpleImpl errorHandler;
123 private ExtensionConverterProvider extensionConverterProvider;
126 * @return translator mapping
128 public Map<TranslatorKey, Collection<IMDMessageTranslator<OfHeader, List<DataObject>>>> getMessageTranslators() {
129 return messageTranslators;
133 * provisioning of translator mapping
138 OpenflowPortsUtil.init();
140 messageTranslators = new ConcurrentHashMap<>();
141 popListeners = new ConcurrentHashMap<>();
142 //TODO: move registration to factory
143 addMessageTranslator(ErrorMessage.class, OF10, new ErrorV10Translator());
144 addMessageTranslator(ErrorMessage.class, OF13, new ErrorTranslator());
145 addMessageTranslator(FlowRemovedMessage.class, OF10, new FlowRemovedTranslator());
146 addMessageTranslator(FlowRemovedMessage.class, OF13, new FlowRemovedTranslator());
147 addMessageTranslator(PacketInMessage.class,OF10, new PacketInV10Translator());
148 addMessageTranslator(PacketInMessage.class,OF13, new PacketInTranslator());
149 addMessageTranslator(PortStatusMessage.class,OF10, new PortStatusMessageToNodeConnectorUpdatedTranslator());
150 addMessageTranslator(PortStatusMessage.class,OF13, new PortStatusMessageToNodeConnectorUpdatedTranslator());
151 addMessageTranslator(MultipartReplyMessage.class,OF13,new MultiPartReplyPortToNodeConnectorUpdatedTranslator());
152 addMessageTranslator(MultipartReplyMessage.class,OF10, new MultiPartMessageDescToNodeUpdatedTranslator());
153 addMessageTranslator(MultipartReplyMessage.class,OF13, new MultiPartMessageDescToNodeUpdatedTranslator());
154 addMessageTranslator(ExperimenterMessage.class, OF10, new ExperimenterTranslator());
155 addMessageTranslator(MultipartReplyMessage.class,OF10, new MultipartReplyTranslator());
156 addMessageTranslator(MultipartReplyMessage.class,OF13, new MultipartReplyTranslator());
157 addMessageTranslator(MultipartReplyMessage.class,OF13,new MultipartReplyTableFeaturesToTableUpdatedTranslator());
158 addMessageTranslator(GetFeaturesOutput.class,OF10, new FeaturesV10ToNodeConnectorUpdatedTranslator());
159 addMessageTranslator(NotificationQueueWrapper.class, OF10, new NotificationPlainTranslator());
160 addMessageTranslator(NotificationQueueWrapper.class, OF13, new NotificationPlainTranslator());
162 NotificationPopListener<DataObject> notificationPopListener = new NotificationPopListener<DataObject>();
163 notificationPopListener.setNotificationProviderService(
164 OFSessionUtil.getSessionManager().getNotificationProviderService());
165 notificationPopListener.setMessageSpy(messageSpyCounter);
167 //TODO: move registration to factory
168 addMessagePopListener(NodeErrorNotification.class, notificationPopListener);
169 addMessagePopListener(BadActionErrorNotification.class, notificationPopListener);
170 addMessagePopListener(BadInstructionErrorNotification.class, notificationPopListener);
171 addMessagePopListener(BadMatchErrorNotification.class, notificationPopListener);
172 addMessagePopListener(BadRequestErrorNotification.class, notificationPopListener);
173 addMessagePopListener(ExperimenterErrorNotification.class, notificationPopListener);
174 addMessagePopListener(FlowModErrorNotification.class, notificationPopListener);
175 addMessagePopListener(GroupModErrorNotification.class, notificationPopListener);
176 addMessagePopListener(HelloFailedErrorNotification.class, notificationPopListener);
177 addMessagePopListener(MeterModErrorNotification.class, notificationPopListener);
178 addMessagePopListener(PortModErrorNotification.class, notificationPopListener);
179 addMessagePopListener(QueueOpErrorNotification.class, notificationPopListener);
180 addMessagePopListener(RoleRequestErrorNotification.class, notificationPopListener);
181 addMessagePopListener(SwitchConfigErrorNotification.class, notificationPopListener);
182 addMessagePopListener(TableFeaturesErrorNotification.class, notificationPopListener);
183 addMessagePopListener(TableModErrorNotification.class, notificationPopListener);
184 addMessagePopListener(NodeConnectorUpdated.class,notificationPopListener);
185 addMessagePopListener(NodeConnectorRemoved.class,notificationPopListener);
186 addMessagePopListener(PacketReceived.class,notificationPopListener);
187 addMessagePopListener(TransmitPacketInput.class, notificationPopListener);
188 addMessagePopListener(NodeUpdated.class, notificationPopListener);
189 addMessagePopListener(NodeRemoved.class, notificationPopListener);
191 addMessagePopListener(SwitchFlowRemoved.class, notificationPopListener);
192 addMessagePopListener(TableUpdated.class, notificationPopListener);
194 //Notification registration for flow statistics
195 addMessagePopListener(FlowsStatisticsUpdate.class, notificationPopListener);
196 addMessagePopListener(AggregateFlowStatisticsUpdate.class, notificationPopListener);
198 //Notification registrations for group-statistics
199 addMessagePopListener(GroupStatisticsUpdated.class, notificationPopListener);
200 addMessagePopListener(GroupFeaturesUpdated.class, notificationPopListener);
201 addMessagePopListener(GroupDescStatsUpdated.class, notificationPopListener);
203 //Notification registrations for meter-statistics
204 addMessagePopListener(MeterStatisticsUpdated.class, notificationPopListener);
205 addMessagePopListener(MeterConfigStatsUpdated.class, notificationPopListener);
206 addMessagePopListener(MeterFeaturesUpdated.class, notificationPopListener);
208 //Notification registration for port-statistics
209 addMessagePopListener(NodeConnectorStatisticsUpdate.class, notificationPopListener);
211 //Notification registration for flow-table statistics
212 addMessagePopListener(FlowTableStatisticsUpdate.class, notificationPopListener);
214 //Notification registration for queue-statistics
215 addMessagePopListener(QueueStatisticsUpdate.class, notificationPopListener);
217 // Push the updated Listeners to Session Manager which will be then picked up by ConnectionConductor eventually
218 OFSessionUtil.getSessionManager().setTranslatorMapping(messageTranslators);
219 OFSessionUtil.getSessionManager().setPopListenerMapping(popListeners);
220 OFSessionUtil.getSessionManager().setMessageSpy(messageSpyCounter);
222 // prepare worker pool for rpc
223 // TODO: get size from configSubsystem
224 int rpcThreadLimit = 10;
225 ListeningExecutorService rpcPoolDelegator = createRpcPoolSpyDecorated(rpcThreadLimit, messageSpyCounter);
226 OFSessionUtil.getSessionManager().setRpcPool(rpcPoolDelegator);
227 OFSessionUtil.getSessionManager().setExtensionConverterProvider(extensionConverterProvider);
232 * @param rpcThreadLimit
236 private static ListeningExecutorService createRpcPoolSpyDecorated(final int rpcThreadLimit, final MessageSpy<DataContainer> messageSpy) {
237 final BlockingQueue<Runnable> delegate = new LinkedBlockingQueue<>(100000);
238 final BlockingQueue<Runnable> queue = new ForwardingBlockingQueue<Runnable>() {
240 protected BlockingQueue<Runnable> delegate() {
245 public boolean offer(final Runnable r) {
246 // ThreadPoolExecutor will spawn a new thread after core size is reached only
247 // if the queue.offer returns false.
252 ThreadPoolLoggingExecutor rpcPool = new ThreadPoolLoggingExecutor(rpcThreadLimit, rpcThreadLimit, 0L,
253 TimeUnit.MILLISECONDS, queue, "OFRpc");
254 rpcPool.setRejectedExecutionHandler(new RejectedExecutionHandler() {
256 public void rejectedExecution(final Runnable r, final ThreadPoolExecutor executor) {
258 executor.getQueue().put(r);
259 } catch (InterruptedException e) {
260 throw new RejectedExecutionException("Interrupted while waiting on queue", e);
265 ListeningExecutorService listeningRpcPool = MoreExecutors.listeningDecorator(rpcPool);
266 RpcListeningExecutorService rpcPoolDecorated = new RpcListeningExecutorService(listeningRpcPool);
267 rpcPoolDecorated.setMessageSpy(messageSpy);
268 return rpcPoolDecorated;
272 * @param switchConnectionProviders
273 * the switchConnectionProviders to set
275 public void setSwitchConnectionProviders(final Collection<SwitchConnectionProvider> switchConnectionProviders) {
276 this.switchConnectionProviders = switchConnectionProviders;
280 * Function called by dependency manager after "init ()" is called and after
281 * the services provided by the class are registered in the service registry
284 public void start() {
285 LOG.debug("starting ..");
286 LOG.debug("switchConnectionProvider: " + switchConnectionProviders);
288 SwitchConnectionHandlerImpl switchConnectionHandler = new SwitchConnectionHandlerImpl();
289 switchConnectionHandler.setMessageSpy(messageSpyCounter);
291 errorHandler = new ErrorHandlerSimpleImpl();
293 switchConnectionHandler.setErrorHandler(errorHandler);
294 switchConnectionHandler.init();
296 List<ListenableFuture<Boolean>> starterChain = new ArrayList<>(switchConnectionProviders.size());
297 for (SwitchConnectionProvider switchConnectionPrv : switchConnectionProviders) {
298 switchConnectionPrv.setSwitchConnectionHandler(switchConnectionHandler);
299 ListenableFuture<Boolean> isOnlineFuture = switchConnectionPrv.startup();
300 starterChain.add(isOnlineFuture);
303 Future<List<Boolean>> srvStarted = Futures.allAsList(starterChain);
307 * Function called by the dependency manager before the services exported by
308 * the component are unregistered, this will be followed by a "destroy ()"
313 LOG.debug("stopping");
314 List<ListenableFuture<Boolean>> stopChain = new ArrayList<>(switchConnectionProviders.size());
316 for (SwitchConnectionProvider switchConnectionPrv : switchConnectionProviders) {
317 ListenableFuture<Boolean> shutdown = switchConnectionPrv.shutdown();
318 stopChain.add(shutdown);
320 Futures.allAsList(stopChain).get(5000, TimeUnit.MILLISECONDS);
321 } catch (InterruptedException | ExecutionException | TimeoutException e) {
322 LOG.error(e.getMessage(), e);
328 * Function called by the dependency manager when at least one dependency
329 * become unsatisfied or when the component is shutting down because for
330 * example bundle is being stopped.
333 public void destroy() {
338 public void addMessageTranslator(final Class<? extends DataObject> messageType, final int version, final IMDMessageTranslator<OfHeader, List<DataObject>> translator) {
339 TranslatorKey tKey = new TranslatorKey(version, messageType.getName());
341 Collection<IMDMessageTranslator<OfHeader, List<DataObject>>> existingValues = messageTranslators.get(tKey);
342 if (existingValues == null) {
343 existingValues = new LinkedHashSet<>();
344 messageTranslators.put(tKey, existingValues);
346 existingValues.add(translator);
347 LOG.debug("{} is now translated by {}", messageType, translator);
351 public void removeMessageTranslator(final Class<? extends DataObject> messageType, final int version, final IMDMessageTranslator<OfHeader, List<DataObject>> translator) {
352 TranslatorKey tKey = new TranslatorKey(version, messageType.getName());
353 Collection<IMDMessageTranslator<OfHeader, List<DataObject>>> values = messageTranslators.get(tKey);
354 if (values != null) {
355 values.remove(translator);
356 if (values.isEmpty()) {
357 messageTranslators.remove(tKey);
359 LOG.debug("{} is now removed from translators", translator);
364 public void addMessagePopListener(final Class<? extends DataObject> messageType, final PopListener<DataObject> popListener) {
365 Collection<PopListener<DataObject>> existingValues = popListeners.get(messageType);
366 if (existingValues == null) {
367 existingValues = new LinkedHashSet<>();
368 popListeners.put(messageType, existingValues);
370 existingValues.add(popListener);
371 LOG.debug("{} is now popListened by {}", messageType, popListener);
375 public void removeMessagePopListener(final Class<? extends DataObject> messageType, final PopListener<DataObject> popListener) {
376 Collection<PopListener<DataObject>> values = popListeners.get(messageType);
377 if (values != null) {
378 values.remove(popListener);
379 if (values.isEmpty()) {
380 popListeners.remove(messageType);
382 LOG.debug("{} is now removed from popListeners", popListener);
387 * @param messageSpyCounter the messageSpyCounter to set
389 public void setMessageSpyCounter(
390 final MessageSpy<DataContainer> messageSpyCounter) {
391 this.messageSpyCounter = messageSpyCounter;
395 public void close() {
397 messageSpyCounter = null;
398 messageTranslators = null;
400 for (SwitchConnectionProvider switchConnectionPrv : switchConnectionProviders) {
401 switchConnectionPrv.setSwitchConnectionHandler(null);
403 switchConnectionProviders = null;
404 OpenflowPortsUtil.close();
405 OFSessionUtil.releaseSessionManager();
410 * @param extensionConverterProvider
412 public void setExtensionConverterProvider(ExtensionConverterProvider extensionConverterProvider) {
413 this.extensionConverterProvider = extensionConverterProvider;