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.sal.convertor.ConvertorExecutor;
41 import org.opendaylight.openflowplugin.openflow.md.core.session.OFSessionUtil;
42 import org.opendaylight.openflowplugin.openflow.md.core.translator.ErrorTranslator;
43 import org.opendaylight.openflowplugin.openflow.md.core.translator.ErrorV10Translator;
44 import org.opendaylight.openflowplugin.openflow.md.core.translator.ExperimenterTranslator;
45 import org.opendaylight.openflowplugin.openflow.md.core.translator.FeaturesV10ToNodeConnectorUpdatedTranslator;
46 import org.opendaylight.openflowplugin.openflow.md.core.translator.FlowRemovedTranslator;
47 import org.opendaylight.openflowplugin.openflow.md.core.translator.MultiPartMessageDescToNodeUpdatedTranslator;
48 import org.opendaylight.openflowplugin.openflow.md.core.translator.MultiPartReplyPortToNodeConnectorUpdatedTranslator;
49 import org.opendaylight.openflowplugin.openflow.md.core.translator.MultipartReplyTableFeaturesToTableUpdatedTranslator;
50 import org.opendaylight.openflowplugin.openflow.md.core.translator.MultipartReplyTranslator;
51 import org.opendaylight.openflowplugin.openflow.md.core.translator.NotificationPlainTranslator;
52 import org.opendaylight.openflowplugin.openflow.md.core.translator.PacketInTranslator;
53 import org.opendaylight.openflowplugin.openflow.md.core.translator.PacketInV10Translator;
54 import org.opendaylight.openflowplugin.openflow.md.core.translator.PortStatusMessageToNodeConnectorUpdatedTranslator;
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);
109 private final ConvertorExecutor convertorExecutor;
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;
124 public MDController(ConvertorExecutor convertorExecutor) {
125 this.convertorExecutor = convertorExecutor;
129 * @return translator mapping
131 public Map<TranslatorKey, Collection<IMDMessageTranslator<OfHeader, List<DataObject>>>> getMessageTranslators() {
132 return messageTranslators;
136 * provisioning of translator mapping
141 messageTranslators = new ConcurrentHashMap<>();
142 popListeners = new ConcurrentHashMap<>();
143 //TODO: move registration to factory
144 addMessageTranslator(ErrorMessage.class, OF10, new ErrorV10Translator());
145 addMessageTranslator(ErrorMessage.class, OF13, new ErrorTranslator());
146 addMessageTranslator(FlowRemovedMessage.class, OF10, new FlowRemovedTranslator(convertorExecutor));
147 addMessageTranslator(FlowRemovedMessage.class, OF13, new FlowRemovedTranslator(convertorExecutor));
148 addMessageTranslator(PacketInMessage.class,OF10, new PacketInV10Translator());
149 addMessageTranslator(PacketInMessage.class,OF13, new PacketInTranslator(convertorExecutor));
150 addMessageTranslator(PortStatusMessage.class,OF10, new PortStatusMessageToNodeConnectorUpdatedTranslator());
151 addMessageTranslator(PortStatusMessage.class,OF13, new PortStatusMessageToNodeConnectorUpdatedTranslator());
152 addMessageTranslator(MultipartReplyMessage.class,OF13,new MultiPartReplyPortToNodeConnectorUpdatedTranslator());
153 addMessageTranslator(MultipartReplyMessage.class,OF10, new MultiPartMessageDescToNodeUpdatedTranslator());
154 addMessageTranslator(MultipartReplyMessage.class,OF13, new MultiPartMessageDescToNodeUpdatedTranslator());
155 addMessageTranslator(ExperimenterMessage.class, OF10, new ExperimenterTranslator());
156 addMessageTranslator(MultipartReplyMessage.class,OF10, new MultipartReplyTranslator(convertorExecutor));
157 addMessageTranslator(MultipartReplyMessage.class,OF13, new MultipartReplyTranslator(convertorExecutor));
158 addMessageTranslator(MultipartReplyMessage.class,OF13,new MultipartReplyTableFeaturesToTableUpdatedTranslator(convertorExecutor));
159 addMessageTranslator(GetFeaturesOutput.class,OF10, new FeaturesV10ToNodeConnectorUpdatedTranslator());
160 addMessageTranslator(NotificationQueueWrapper.class, OF10, new NotificationPlainTranslator());
161 addMessageTranslator(NotificationQueueWrapper.class, OF13, new NotificationPlainTranslator());
163 NotificationPopListener<DataObject> notificationPopListener = new NotificationPopListener<DataObject>();
164 notificationPopListener.setNotificationProviderService(
165 OFSessionUtil.getSessionManager().getNotificationProviderService());
166 notificationPopListener.setMessageSpy(messageSpyCounter);
168 //TODO: move registration to factory
169 addMessagePopListener(NodeErrorNotification.class, notificationPopListener);
170 addMessagePopListener(BadActionErrorNotification.class, notificationPopListener);
171 addMessagePopListener(BadInstructionErrorNotification.class, notificationPopListener);
172 addMessagePopListener(BadMatchErrorNotification.class, notificationPopListener);
173 addMessagePopListener(BadRequestErrorNotification.class, notificationPopListener);
174 addMessagePopListener(ExperimenterErrorNotification.class, notificationPopListener);
175 addMessagePopListener(FlowModErrorNotification.class, notificationPopListener);
176 addMessagePopListener(GroupModErrorNotification.class, notificationPopListener);
177 addMessagePopListener(HelloFailedErrorNotification.class, notificationPopListener);
178 addMessagePopListener(MeterModErrorNotification.class, notificationPopListener);
179 addMessagePopListener(PortModErrorNotification.class, notificationPopListener);
180 addMessagePopListener(QueueOpErrorNotification.class, notificationPopListener);
181 addMessagePopListener(RoleRequestErrorNotification.class, notificationPopListener);
182 addMessagePopListener(SwitchConfigErrorNotification.class, notificationPopListener);
183 addMessagePopListener(TableFeaturesErrorNotification.class, notificationPopListener);
184 addMessagePopListener(TableModErrorNotification.class, notificationPopListener);
185 addMessagePopListener(NodeConnectorUpdated.class,notificationPopListener);
186 addMessagePopListener(NodeConnectorRemoved.class,notificationPopListener);
187 addMessagePopListener(PacketReceived.class,notificationPopListener);
188 addMessagePopListener(TransmitPacketInput.class, notificationPopListener);
189 addMessagePopListener(NodeUpdated.class, notificationPopListener);
190 addMessagePopListener(NodeRemoved.class, notificationPopListener);
192 addMessagePopListener(SwitchFlowRemoved.class, notificationPopListener);
193 addMessagePopListener(TableUpdated.class, notificationPopListener);
195 //Notification registration for flow statistics
196 addMessagePopListener(FlowsStatisticsUpdate.class, notificationPopListener);
197 addMessagePopListener(AggregateFlowStatisticsUpdate.class, notificationPopListener);
199 //Notification registrations for group-statistics
200 addMessagePopListener(GroupStatisticsUpdated.class, notificationPopListener);
201 addMessagePopListener(GroupFeaturesUpdated.class, notificationPopListener);
202 addMessagePopListener(GroupDescStatsUpdated.class, notificationPopListener);
204 //Notification registrations for meter-statistics
205 addMessagePopListener(MeterStatisticsUpdated.class, notificationPopListener);
206 addMessagePopListener(MeterConfigStatsUpdated.class, notificationPopListener);
207 addMessagePopListener(MeterFeaturesUpdated.class, notificationPopListener);
209 //Notification registration for port-statistics
210 addMessagePopListener(NodeConnectorStatisticsUpdate.class, notificationPopListener);
212 //Notification registration for flow-table statistics
213 addMessagePopListener(FlowTableStatisticsUpdate.class, notificationPopListener);
215 //Notification registration for queue-statistics
216 addMessagePopListener(QueueStatisticsUpdate.class, notificationPopListener);
218 // Push the updated Listeners to Session Manager which will be then picked up by ConnectionConductor eventually
219 OFSessionUtil.getSessionManager().setTranslatorMapping(messageTranslators);
220 OFSessionUtil.getSessionManager().setPopListenerMapping(popListeners);
221 OFSessionUtil.getSessionManager().setMessageSpy(messageSpyCounter);
223 // prepare worker pool for rpc
224 // TODO: get size from configSubsystem
225 int rpcThreadLimit = 10;
226 ListeningExecutorService rpcPoolDelegator = createRpcPoolSpyDecorated(rpcThreadLimit, messageSpyCounter);
227 OFSessionUtil.getSessionManager().setRpcPool(rpcPoolDelegator);
228 OFSessionUtil.getSessionManager().setExtensionConverterProvider(extensionConverterProvider);
233 * @param rpcThreadLimit
237 private static ListeningExecutorService createRpcPoolSpyDecorated(final int rpcThreadLimit, final MessageSpy<DataContainer> messageSpy) {
238 final BlockingQueue<Runnable> delegate = new LinkedBlockingQueue<>(100000);
239 final BlockingQueue<Runnable> queue = new ForwardingBlockingQueue<Runnable>() {
241 protected BlockingQueue<Runnable> delegate() {
246 public boolean offer(final Runnable r) {
247 // ThreadPoolExecutor will spawn a new thread after core size is reached only
248 // if the queue.offer returns false.
253 ThreadPoolLoggingExecutor rpcPool = new ThreadPoolLoggingExecutor(rpcThreadLimit, rpcThreadLimit, 0L,
254 TimeUnit.MILLISECONDS, queue, "OFRpc");
255 rpcPool.setRejectedExecutionHandler(new RejectedExecutionHandler() {
257 public void rejectedExecution(final Runnable r, final ThreadPoolExecutor executor) {
259 executor.getQueue().put(r);
260 } catch (InterruptedException e) {
261 throw new RejectedExecutionException("Interrupted while waiting on queue", e);
266 ListeningExecutorService listeningRpcPool = MoreExecutors.listeningDecorator(rpcPool);
267 RpcListeningExecutorService rpcPoolDecorated = new RpcListeningExecutorService(listeningRpcPool);
268 rpcPoolDecorated.setMessageSpy(messageSpy);
269 return rpcPoolDecorated;
273 * @param switchConnectionProviders
274 * the switchConnectionProviders to set
276 public void setSwitchConnectionProviders(final Collection<SwitchConnectionProvider> switchConnectionProviders) {
277 this.switchConnectionProviders = switchConnectionProviders;
281 * Function called by dependency manager after "init ()" is called and after
282 * the services provided by the class are registered in the service registry
285 public void start() {
286 LOG.debug("starting ..");
287 LOG.debug("switchConnectionProvider: " + switchConnectionProviders);
289 SwitchConnectionHandlerImpl switchConnectionHandler = new SwitchConnectionHandlerImpl();
290 switchConnectionHandler.setMessageSpy(messageSpyCounter);
292 errorHandler = new ErrorHandlerSimpleImpl();
294 switchConnectionHandler.setErrorHandler(errorHandler);
295 switchConnectionHandler.init();
297 List<ListenableFuture<Boolean>> starterChain = new ArrayList<>(switchConnectionProviders.size());
298 for (SwitchConnectionProvider switchConnectionPrv : switchConnectionProviders) {
299 switchConnectionPrv.setSwitchConnectionHandler(switchConnectionHandler);
300 ListenableFuture<Boolean> isOnlineFuture = switchConnectionPrv.startup();
301 starterChain.add(isOnlineFuture);
304 Future<List<Boolean>> srvStarted = Futures.allAsList(starterChain);
308 * Function called by the dependency manager before the services exported by
309 * the component are unregistered, this will be followed by a "destroy ()"
314 LOG.debug("stopping");
315 List<ListenableFuture<Boolean>> stopChain = new ArrayList<>(switchConnectionProviders.size());
317 for (SwitchConnectionProvider switchConnectionPrv : switchConnectionProviders) {
318 ListenableFuture<Boolean> shutdown = switchConnectionPrv.shutdown();
319 stopChain.add(shutdown);
321 Futures.allAsList(stopChain).get(5000, TimeUnit.MILLISECONDS);
322 } catch (InterruptedException | ExecutionException | TimeoutException e) {
323 LOG.warn("failed to stop MDController: {}", e.getMessage());
324 LOG.debug("failed to stop MDController.. ", 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 OFSessionUtil.releaseSessionManager();
411 * @param extensionConverterProvider extension convertor provider
413 public void setExtensionConverterProvider(ExtensionConverterProvider extensionConverterProvider) {
414 this.extensionConverterProvider = extensionConverterProvider;