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.TranslatorKey;
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.NotificationPlainTranslator;
43 import org.opendaylight.openflowplugin.openflow.md.core.translator.PacketInTranslator;
44 import org.opendaylight.openflowplugin.openflow.md.core.translator.PacketInV10Translator;
45 import org.opendaylight.openflowplugin.openflow.md.core.translator.PortStatusMessageToNodeConnectorUpdatedTranslator;
46 import org.opendaylight.openflowplugin.openflow.md.lldp.LLDPSpeakerPopListener;
47 import org.opendaylight.openflowplugin.openflow.md.queue.PopListener;
48 import org.opendaylight.openflowplugin.openflow.md.util.OpenflowPortsUtil;
49 import org.opendaylight.openflowplugin.api.statistics.MessageSpy;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.NodeErrorNotification;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SwitchFlowRemoved;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.AggregateFlowStatisticsUpdate;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowsStatisticsUpdate;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.FlowTableStatisticsUpdate;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GroupDescStatsUpdated;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GroupFeaturesUpdated;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GroupStatisticsUpdated;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorUpdated;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRemoved;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeUpdated;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.MeterConfigStatsUpdated;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.MeterFeaturesUpdated;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.MeterStatisticsUpdated;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.node.error.service.rev140410.BadActionErrorNotification;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.node.error.service.rev140410.BadInstructionErrorNotification;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.node.error.service.rev140410.BadMatchErrorNotification;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.node.error.service.rev140410.BadRequestErrorNotification;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.node.error.service.rev140410.ExperimenterErrorNotification;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.node.error.service.rev140410.FlowModErrorNotification;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.node.error.service.rev140410.GroupModErrorNotification;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.node.error.service.rev140410.HelloFailedErrorNotification;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.node.error.service.rev140410.MeterModErrorNotification;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.node.error.service.rev140410.PortModErrorNotification;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.node.error.service.rev140410.QueueOpErrorNotification;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.node.error.service.rev140410.RoleRequestErrorNotification;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.node.error.service.rev140410.SwitchConfigErrorNotification;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.node.error.service.rev140410.TableFeaturesErrorNotification;
78 import org.opendaylight.yang.gen.v1.urn.opendaylight.node.error.service.rev140410.TableModErrorNotification;
79 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ErrorMessage;
80 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ExperimenterMessage;
81 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowRemovedMessage;
82 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
83 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReplyMessage;
84 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
85 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessage;
86 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortStatusMessage;
87 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketReceived;
88 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.TransmitPacketInput;
89 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.NodeConnectorStatisticsUpdate;
90 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.QueueStatisticsUpdate;
91 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.TableUpdated;
92 import org.opendaylight.yangtools.yang.binding.DataContainer;
93 import org.opendaylight.yangtools.yang.binding.DataObject;
94 import org.slf4j.Logger;
95 import org.slf4j.LoggerFactory;
97 import com.google.common.collect.Lists;
98 import com.google.common.util.concurrent.ForwardingBlockingQueue;
99 import com.google.common.util.concurrent.Futures;
100 import com.google.common.util.concurrent.ListenableFuture;
101 import com.google.common.util.concurrent.ListeningExecutorService;
102 import com.google.common.util.concurrent.MoreExecutors;
107 public class MDController implements IMDController, AutoCloseable {
109 private static final Logger LOG = LoggerFactory.getLogger(MDController.class);
111 private Collection<SwitchConnectionProvider> switchConnectionProviders;
113 private ConcurrentMap<TranslatorKey, Collection<IMDMessageTranslator<OfHeader, List<DataObject>>>> messageTranslators;
114 private Map<Class<? extends DataObject>, Collection<PopListener<DataObject>>> popListeners;
115 private MessageSpy<DataContainer> messageSpyCounter;
117 final private int OF10 = OFConstants.OFP_VERSION_1_0;
118 final private int OF13 = OFConstants.OFP_VERSION_1_3;
120 private ErrorHandlerSimpleImpl errorHandler;
122 private ExtensionConverterProvider extensionConverterProvider;
125 * @return translator mapping
127 public Map<TranslatorKey, Collection<IMDMessageTranslator<OfHeader, List<DataObject>>>> getMessageTranslators() {
128 return messageTranslators;
132 * provisioning of translator mapping
137 OpenflowPortsUtil.init();
139 messageTranslators = new ConcurrentHashMap<>();
140 popListeners = new ConcurrentHashMap<>();
141 //TODO: move registration to factory
142 addMessageTranslator(ErrorMessage.class, OF10, new ErrorV10Translator());
143 addMessageTranslator(ErrorMessage.class, OF13, new ErrorTranslator());
144 addMessageTranslator(FlowRemovedMessage.class, OF10, new FlowRemovedTranslator());
145 addMessageTranslator(FlowRemovedMessage.class, OF13, new FlowRemovedTranslator());
146 addMessageTranslator(PacketInMessage.class,OF10, new PacketInV10Translator());
147 addMessageTranslator(PacketInMessage.class,OF13, new PacketInTranslator());
148 addMessageTranslator(PortStatusMessage.class,OF10, new PortStatusMessageToNodeConnectorUpdatedTranslator());
149 addMessageTranslator(PortStatusMessage.class,OF13, new PortStatusMessageToNodeConnectorUpdatedTranslator());
150 addMessageTranslator(MultipartReplyMessage.class,OF13,new MultiPartReplyPortToNodeConnectorUpdatedTranslator());
151 addMessageTranslator(MultipartReplyMessage.class,OF10, new MultiPartMessageDescToNodeUpdatedTranslator());
152 addMessageTranslator(MultipartReplyMessage.class,OF13, new MultiPartMessageDescToNodeUpdatedTranslator());
153 addMessageTranslator(ExperimenterMessage.class, OF10, new ExperimenterTranslator());
154 addMessageTranslator(MultipartReplyMessage.class,OF10, new MultipartReplyTranslator());
155 addMessageTranslator(MultipartReplyMessage.class,OF13, new MultipartReplyTranslator());
156 addMessageTranslator(MultipartReplyMessage.class,OF13,new MultipartReplyTableFeaturesToTableUpdatedTranslator());
157 addMessageTranslator(GetFeaturesOutput.class,OF10, new FeaturesV10ToNodeConnectorUpdatedTranslator());
158 addMessageTranslator(NotificationQueueWrapper.class, OF10, new NotificationPlainTranslator());
159 addMessageTranslator(NotificationQueueWrapper.class, OF13, new NotificationPlainTranslator());
161 NotificationPopListener<DataObject> notificationPopListener = new NotificationPopListener<DataObject>();
162 notificationPopListener.setNotificationProviderService(
163 OFSessionUtil.getSessionManager().getNotificationProviderService());
164 notificationPopListener.setMessageSpy(messageSpyCounter);
166 //TODO: move registration to factory
167 addMessagePopListener(NodeErrorNotification.class, notificationPopListener);
168 addMessagePopListener(BadActionErrorNotification.class, notificationPopListener);
169 addMessagePopListener(BadInstructionErrorNotification.class, notificationPopListener);
170 addMessagePopListener(BadMatchErrorNotification.class, notificationPopListener);
171 addMessagePopListener(BadRequestErrorNotification.class, notificationPopListener);
172 addMessagePopListener(ExperimenterErrorNotification.class, notificationPopListener);
173 addMessagePopListener(FlowModErrorNotification.class, notificationPopListener);
174 addMessagePopListener(GroupModErrorNotification.class, notificationPopListener);
175 addMessagePopListener(HelloFailedErrorNotification.class, notificationPopListener);
176 addMessagePopListener(MeterModErrorNotification.class, notificationPopListener);
177 addMessagePopListener(PortModErrorNotification.class, notificationPopListener);
178 addMessagePopListener(QueueOpErrorNotification.class, notificationPopListener);
179 addMessagePopListener(RoleRequestErrorNotification.class, notificationPopListener);
180 addMessagePopListener(SwitchConfigErrorNotification.class, notificationPopListener);
181 addMessagePopListener(TableFeaturesErrorNotification.class, notificationPopListener);
182 addMessagePopListener(TableModErrorNotification.class, notificationPopListener);
183 addMessagePopListener(NodeConnectorUpdated.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 //Notification for LLDPSpeaker
216 LLDPSpeakerPopListener<DataObject> lldpPopListener = new LLDPSpeakerPopListener<DataObject>();
217 addMessagePopListener(NodeConnectorUpdated.class,lldpPopListener);
219 // Push the updated Listeners to Session Manager which will be then picked up by ConnectionConductor eventually
220 OFSessionUtil.getSessionManager().setTranslatorMapping(messageTranslators);
221 OFSessionUtil.getSessionManager().setPopListenerMapping(popListeners);
222 OFSessionUtil.getSessionManager().setMessageSpy(messageSpyCounter);
224 // prepare worker pool for rpc
225 // TODO: get size from configSubsystem
226 int rpcThreadLimit = 10;
227 ListeningExecutorService rpcPoolDelegator = createRpcPoolSpyDecorated(rpcThreadLimit, messageSpyCounter);
228 OFSessionUtil.getSessionManager().setRpcPool(rpcPoolDelegator);
229 OFSessionUtil.getSessionManager().setExtensionConverterProvider(extensionConverterProvider);
234 * @param rpcThreadLimit
238 private static ListeningExecutorService createRpcPoolSpyDecorated(final int rpcThreadLimit, final MessageSpy<DataContainer> messageSpy) {
239 final BlockingQueue<Runnable> delegate = new LinkedBlockingQueue<>(100000);
240 final BlockingQueue<Runnable> queue = new ForwardingBlockingQueue<Runnable>() {
242 protected BlockingQueue<Runnable> delegate() {
247 public boolean offer(final Runnable r) {
248 // ThreadPoolExecutor will spawn a new thread after core size is reached only
249 // if the queue.offer returns false.
254 ThreadPoolLoggingExecutor rpcPool = new ThreadPoolLoggingExecutor(rpcThreadLimit, rpcThreadLimit, 0L,
255 TimeUnit.MILLISECONDS, queue, "OFRpc");
256 rpcPool.setRejectedExecutionHandler(new RejectedExecutionHandler() {
258 public void rejectedExecution(final Runnable r, final ThreadPoolExecutor executor) {
260 executor.getQueue().put(r);
261 } catch (InterruptedException e) {
262 throw new RejectedExecutionException("Interrupted while waiting on queue", e);
267 ListeningExecutorService listeningRpcPool = MoreExecutors.listeningDecorator(rpcPool);
268 RpcListeningExecutorService rpcPoolDecorated = new RpcListeningExecutorService(listeningRpcPool);
269 rpcPoolDecorated.setMessageSpy(messageSpy);
270 return rpcPoolDecorated;
274 * @param switchConnectionProviders
275 * the switchConnectionProviders to set
277 public void setSwitchConnectionProviders(final Collection<SwitchConnectionProvider> switchConnectionProviders) {
278 this.switchConnectionProviders = switchConnectionProviders;
282 * Function called by dependency manager after "init ()" is called and after
283 * the services provided by the class are registered in the service registry
286 public void start() {
287 LOG.debug("starting ..");
288 LOG.debug("switchConnectionProvider: " + switchConnectionProviders);
290 SwitchConnectionHandlerImpl switchConnectionHandler = new SwitchConnectionHandlerImpl();
291 switchConnectionHandler.setMessageSpy(messageSpyCounter);
293 errorHandler = new ErrorHandlerSimpleImpl();
295 switchConnectionHandler.setErrorHandler(errorHandler);
296 switchConnectionHandler.init();
298 List<ListenableFuture<Boolean>> starterChain = new ArrayList<>(switchConnectionProviders.size());
299 for (SwitchConnectionProvider switchConnectionPrv : switchConnectionProviders) {
300 switchConnectionPrv.setSwitchConnectionHandler(switchConnectionHandler);
301 ListenableFuture<Boolean> isOnlineFuture = switchConnectionPrv.startup();
302 starterChain.add(isOnlineFuture);
305 Future<List<Boolean>> srvStarted = Futures.allAsList(starterChain);
309 * Function called by the dependency manager before the services exported by
310 * the component are unregistered, this will be followed by a "destroy ()"
315 LOG.debug("stopping");
316 List<ListenableFuture<Boolean>> stopChain = new ArrayList<>(switchConnectionProviders.size());
318 for (SwitchConnectionProvider switchConnectionPrv : switchConnectionProviders) {
319 ListenableFuture<Boolean> shutdown = switchConnectionPrv.shutdown();
320 stopChain.add(shutdown);
322 Futures.allAsList(stopChain).get(5000, TimeUnit.MILLISECONDS);
323 } catch (InterruptedException | ExecutionException | TimeoutException e) {
324 LOG.error(e.getMessage(), e);
330 * Function called by the dependency manager when at least one dependency
331 * become unsatisfied or when the component is shutting down because for
332 * example bundle is being stopped.
335 public void destroy() {
340 public void addMessageTranslator(final Class<? extends DataObject> messageType, final int version, final IMDMessageTranslator<OfHeader, List<DataObject>> translator) {
341 TranslatorKey tKey = new TranslatorKey(version, messageType.getName());
343 Collection<IMDMessageTranslator<OfHeader, List<DataObject>>> existingValues = messageTranslators.get(tKey);
344 if (existingValues == null) {
345 existingValues = new LinkedHashSet<>();
346 messageTranslators.put(tKey, existingValues);
348 existingValues.add(translator);
349 LOG.debug("{} is now translated by {}", messageType, translator);
353 public void removeMessageTranslator(final Class<? extends DataObject> messageType, final int version, final IMDMessageTranslator<OfHeader, List<DataObject>> translator) {
354 TranslatorKey tKey = new TranslatorKey(version, messageType.getName());
355 Collection<IMDMessageTranslator<OfHeader, List<DataObject>>> values = messageTranslators.get(tKey);
356 if (values != null) {
357 values.remove(translator);
358 if (values.isEmpty()) {
359 messageTranslators.remove(tKey);
361 LOG.debug("{} is now removed from translators", translator);
366 public void addMessagePopListener(final Class<? extends DataObject> messageType, final PopListener<DataObject> popListener) {
367 Collection<PopListener<DataObject>> existingValues = popListeners.get(messageType);
368 if (existingValues == null) {
369 existingValues = new LinkedHashSet<>();
370 popListeners.put(messageType, existingValues);
372 existingValues.add(popListener);
373 LOG.debug("{} is now popListened by {}", messageType, popListener);
377 public void removeMessagePopListener(final Class<? extends DataObject> messageType, final PopListener<DataObject> popListener) {
378 Collection<PopListener<DataObject>> values = popListeners.get(messageType);
379 if (values != null) {
380 values.remove(popListener);
381 if (values.isEmpty()) {
382 popListeners.remove(messageType);
384 LOG.debug("{} is now removed from popListeners", popListener);
389 * @param messageSpyCounter the messageSpyCounter to set
391 public void setMessageSpyCounter(
392 final MessageSpy<DataContainer> messageSpyCounter) {
393 this.messageSpyCounter = messageSpyCounter;
397 public void close() {
399 messageSpyCounter = null;
400 messageTranslators = null;
402 for (SwitchConnectionProvider switchConnectionPrv : switchConnectionProviders) {
403 switchConnectionPrv.setSwitchConnectionHandler(null);
405 switchConnectionProviders = null;
406 OpenflowPortsUtil.close();
407 OFSessionUtil.releaseSessionManager();
412 * @param extensionConverterProvider
414 public void setExtensionConverterProvider(ExtensionConverterProvider extensionConverterProvider) {
415 this.extensionConverterProvider = extensionConverterProvider;