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.openflow.md.lldp.LLDPSpeakerPopListener;
49 import org.opendaylight.openflowplugin.api.openflow.md.queue.PopListener;
50 import org.opendaylight.openflowplugin.openflow.md.util.OpenflowPortsUtil;
51 import org.opendaylight.openflowplugin.api.statistics.MessageSpy;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.NodeErrorNotification;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SwitchFlowRemoved;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.AggregateFlowStatisticsUpdate;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowsStatisticsUpdate;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.FlowTableStatisticsUpdate;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GroupDescStatsUpdated;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GroupFeaturesUpdated;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GroupStatisticsUpdated;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorUpdated;
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(PacketReceived.class,notificationPopListener);
186 addMessagePopListener(TransmitPacketInput.class, notificationPopListener);
187 addMessagePopListener(NodeUpdated.class, notificationPopListener);
188 addMessagePopListener(NodeRemoved.class, notificationPopListener);
190 addMessagePopListener(SwitchFlowRemoved.class, notificationPopListener);
191 addMessagePopListener(TableUpdated.class, notificationPopListener);
193 //Notification registration for flow statistics
194 addMessagePopListener(FlowsStatisticsUpdate.class, notificationPopListener);
195 addMessagePopListener(AggregateFlowStatisticsUpdate.class, notificationPopListener);
197 //Notification registrations for group-statistics
198 addMessagePopListener(GroupStatisticsUpdated.class, notificationPopListener);
199 addMessagePopListener(GroupFeaturesUpdated.class, notificationPopListener);
200 addMessagePopListener(GroupDescStatsUpdated.class, notificationPopListener);
202 //Notification registrations for meter-statistics
203 addMessagePopListener(MeterStatisticsUpdated.class, notificationPopListener);
204 addMessagePopListener(MeterConfigStatsUpdated.class, notificationPopListener);
205 addMessagePopListener(MeterFeaturesUpdated.class, notificationPopListener);
207 //Notification registration for port-statistics
208 addMessagePopListener(NodeConnectorStatisticsUpdate.class, notificationPopListener);
210 //Notification registration for flow-table statistics
211 addMessagePopListener(FlowTableStatisticsUpdate.class, notificationPopListener);
213 //Notification registration for queue-statistics
214 addMessagePopListener(QueueStatisticsUpdate.class, notificationPopListener);
216 //Notification for LLDPSpeaker
217 LLDPSpeakerPopListener<DataObject> lldpPopListener = new LLDPSpeakerPopListener<DataObject>();
218 addMessagePopListener(NodeConnectorUpdated.class,lldpPopListener);
220 // Push the updated Listeners to Session Manager which will be then picked up by ConnectionConductor eventually
221 OFSessionUtil.getSessionManager().setTranslatorMapping(messageTranslators);
222 OFSessionUtil.getSessionManager().setPopListenerMapping(popListeners);
223 OFSessionUtil.getSessionManager().setMessageSpy(messageSpyCounter);
225 // prepare worker pool for rpc
226 // TODO: get size from configSubsystem
227 int rpcThreadLimit = 10;
228 ListeningExecutorService rpcPoolDelegator = createRpcPoolSpyDecorated(rpcThreadLimit, messageSpyCounter);
229 OFSessionUtil.getSessionManager().setRpcPool(rpcPoolDelegator);
230 OFSessionUtil.getSessionManager().setExtensionConverterProvider(extensionConverterProvider);
235 * @param rpcThreadLimit
239 private static ListeningExecutorService createRpcPoolSpyDecorated(final int rpcThreadLimit, final MessageSpy<DataContainer> messageSpy) {
240 final BlockingQueue<Runnable> delegate = new LinkedBlockingQueue<>(100000);
241 final BlockingQueue<Runnable> queue = new ForwardingBlockingQueue<Runnable>() {
243 protected BlockingQueue<Runnable> delegate() {
248 public boolean offer(final Runnable r) {
249 // ThreadPoolExecutor will spawn a new thread after core size is reached only
250 // if the queue.offer returns false.
255 ThreadPoolLoggingExecutor rpcPool = new ThreadPoolLoggingExecutor(rpcThreadLimit, rpcThreadLimit, 0L,
256 TimeUnit.MILLISECONDS, queue, "OFRpc");
257 rpcPool.setRejectedExecutionHandler(new RejectedExecutionHandler() {
259 public void rejectedExecution(final Runnable r, final ThreadPoolExecutor executor) {
261 executor.getQueue().put(r);
262 } catch (InterruptedException e) {
263 throw new RejectedExecutionException("Interrupted while waiting on queue", e);
268 ListeningExecutorService listeningRpcPool = MoreExecutors.listeningDecorator(rpcPool);
269 RpcListeningExecutorService rpcPoolDecorated = new RpcListeningExecutorService(listeningRpcPool);
270 rpcPoolDecorated.setMessageSpy(messageSpy);
271 return rpcPoolDecorated;
275 * @param switchConnectionProviders
276 * the switchConnectionProviders to set
278 public void setSwitchConnectionProviders(final Collection<SwitchConnectionProvider> switchConnectionProviders) {
279 this.switchConnectionProviders = switchConnectionProviders;
283 * Function called by dependency manager after "init ()" is called and after
284 * the services provided by the class are registered in the service registry
287 public void start() {
288 LOG.debug("starting ..");
289 LOG.debug("switchConnectionProvider: " + switchConnectionProviders);
291 SwitchConnectionHandlerImpl switchConnectionHandler = new SwitchConnectionHandlerImpl();
292 switchConnectionHandler.setMessageSpy(messageSpyCounter);
294 errorHandler = new ErrorHandlerSimpleImpl();
296 switchConnectionHandler.setErrorHandler(errorHandler);
297 switchConnectionHandler.init();
299 List<ListenableFuture<Boolean>> starterChain = new ArrayList<>(switchConnectionProviders.size());
300 for (SwitchConnectionProvider switchConnectionPrv : switchConnectionProviders) {
301 switchConnectionPrv.setSwitchConnectionHandler(switchConnectionHandler);
302 ListenableFuture<Boolean> isOnlineFuture = switchConnectionPrv.startup();
303 starterChain.add(isOnlineFuture);
306 Future<List<Boolean>> srvStarted = Futures.allAsList(starterChain);
310 * Function called by the dependency manager before the services exported by
311 * the component are unregistered, this will be followed by a "destroy ()"
316 LOG.debug("stopping");
317 List<ListenableFuture<Boolean>> stopChain = new ArrayList<>(switchConnectionProviders.size());
319 for (SwitchConnectionProvider switchConnectionPrv : switchConnectionProviders) {
320 ListenableFuture<Boolean> shutdown = switchConnectionPrv.shutdown();
321 stopChain.add(shutdown);
323 Futures.allAsList(stopChain).get(5000, TimeUnit.MILLISECONDS);
324 } catch (InterruptedException | ExecutionException | TimeoutException e) {
325 LOG.error(e.getMessage(), e);
331 * Function called by the dependency manager when at least one dependency
332 * become unsatisfied or when the component is shutting down because for
333 * example bundle is being stopped.
336 public void destroy() {
341 public void addMessageTranslator(final Class<? extends DataObject> messageType, final int version, final IMDMessageTranslator<OfHeader, List<DataObject>> translator) {
342 TranslatorKey tKey = new TranslatorKey(version, messageType.getName());
344 Collection<IMDMessageTranslator<OfHeader, List<DataObject>>> existingValues = messageTranslators.get(tKey);
345 if (existingValues == null) {
346 existingValues = new LinkedHashSet<>();
347 messageTranslators.put(tKey, existingValues);
349 existingValues.add(translator);
350 LOG.debug("{} is now translated by {}", messageType, translator);
354 public void removeMessageTranslator(final Class<? extends DataObject> messageType, final int version, final IMDMessageTranslator<OfHeader, List<DataObject>> translator) {
355 TranslatorKey tKey = new TranslatorKey(version, messageType.getName());
356 Collection<IMDMessageTranslator<OfHeader, List<DataObject>>> values = messageTranslators.get(tKey);
357 if (values != null) {
358 values.remove(translator);
359 if (values.isEmpty()) {
360 messageTranslators.remove(tKey);
362 LOG.debug("{} is now removed from translators", translator);
367 public void addMessagePopListener(final Class<? extends DataObject> messageType, final PopListener<DataObject> popListener) {
368 Collection<PopListener<DataObject>> existingValues = popListeners.get(messageType);
369 if (existingValues == null) {
370 existingValues = new LinkedHashSet<>();
371 popListeners.put(messageType, existingValues);
373 existingValues.add(popListener);
374 LOG.debug("{} is now popListened by {}", messageType, popListener);
378 public void removeMessagePopListener(final Class<? extends DataObject> messageType, final PopListener<DataObject> popListener) {
379 Collection<PopListener<DataObject>> values = popListeners.get(messageType);
380 if (values != null) {
381 values.remove(popListener);
382 if (values.isEmpty()) {
383 popListeners.remove(messageType);
385 LOG.debug("{} is now removed from popListeners", popListener);
390 * @param messageSpyCounter the messageSpyCounter to set
392 public void setMessageSpyCounter(
393 final MessageSpy<DataContainer> messageSpyCounter) {
394 this.messageSpyCounter = messageSpyCounter;
398 public void close() {
400 messageSpyCounter = null;
401 messageTranslators = null;
403 for (SwitchConnectionProvider switchConnectionPrv : switchConnectionProviders) {
404 switchConnectionPrv.setSwitchConnectionHandler(null);
406 switchConnectionProviders = null;
407 OpenflowPortsUtil.close();
408 OFSessionUtil.releaseSessionManager();
413 * @param extensionConverterProvider
415 public void setExtensionConverterProvider(ExtensionConverterProvider extensionConverterProvider) {
416 this.extensionConverterProvider = extensionConverterProvider;