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.ConcurrentHashMap;
17 import java.util.concurrent.ConcurrentMap;
18 import java.util.concurrent.ExecutionException;
19 import java.util.concurrent.Executors;
20 import java.util.concurrent.Future;
21 import java.util.concurrent.LinkedBlockingQueue;
22 import java.util.concurrent.TimeUnit;
23 import java.util.concurrent.TimeoutException;
25 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionConfiguration;
26 import org.opendaylight.openflowjava.protocol.spi.connection.SwitchConnectionProvider;
27 import org.opendaylight.openflowplugin.openflow.md.OFConstants;
28 import org.opendaylight.openflowplugin.openflow.md.core.session.OFSessionUtil;
29 import org.opendaylight.openflowplugin.openflow.md.core.translator.ErrorTranslator;
30 import org.opendaylight.openflowplugin.openflow.md.core.translator.ErrorV10Translator;
31 import org.opendaylight.openflowplugin.openflow.md.core.translator.ExperimenterTranslator;
32 import org.opendaylight.openflowplugin.openflow.md.core.translator.FeaturesV10ToNodeConnectorUpdatedTranslator;
33 import org.opendaylight.openflowplugin.openflow.md.core.translator.FlowRemovedTranslator;
34 import org.opendaylight.openflowplugin.openflow.md.core.translator.MultiPartMessageDescToNodeUpdatedTranslator;
35 import org.opendaylight.openflowplugin.openflow.md.core.translator.MultiPartReplyPortToNodeConnectorUpdatedTranslator;
36 import org.opendaylight.openflowplugin.openflow.md.core.translator.MultipartReplyTableFeaturesToTableUpdatedTranslator;
37 import org.opendaylight.openflowplugin.openflow.md.core.translator.MultipartReplyTranslator;
38 import org.opendaylight.openflowplugin.openflow.md.core.translator.PacketInTranslator;
39 import org.opendaylight.openflowplugin.openflow.md.core.translator.PacketInV10Translator;
40 import org.opendaylight.openflowplugin.openflow.md.core.translator.PortStatusMessageToNodeConnectorUpdatedTranslator;
41 import org.opendaylight.openflowplugin.openflow.md.lldp.LLDPSpeakerPopListener;
42 import org.opendaylight.openflowplugin.openflow.md.queue.MessageSpy;
43 import org.opendaylight.openflowplugin.openflow.md.queue.PopListener;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.NodeErrorNotification;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SwitchFlowRemoved;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.AggregateFlowStatisticsUpdate;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowsStatisticsUpdate;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.FlowTableStatisticsUpdate;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GroupDescStatsUpdated;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GroupFeaturesUpdated;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GroupStatisticsUpdated;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorUpdated;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeUpdated;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.MeterConfigStatsUpdated;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.MeterFeaturesUpdated;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.MeterStatisticsUpdated;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.node.error.service.rev140410.BadActionErrorNotification;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.node.error.service.rev140410.BadInstructionErrorNotification;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.node.error.service.rev140410.BadMatchErrorNotification;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.node.error.service.rev140410.BadRequestErrorNotification;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.node.error.service.rev140410.ExperimenterErrorNotification;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.node.error.service.rev140410.FlowModErrorNotification;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.node.error.service.rev140410.GroupModErrorNotification;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.node.error.service.rev140410.HelloFailedErrorNotification;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.node.error.service.rev140410.MeterModErrorNotification;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.node.error.service.rev140410.PortModErrorNotification;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.node.error.service.rev140410.QueueOpErrorNotification;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.node.error.service.rev140410.RoleRequestErrorNotification;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.node.error.service.rev140410.SwitchConfigErrorNotification;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.node.error.service.rev140410.TableFeaturesErrorNotification;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.node.error.service.rev140410.TableModErrorNotification;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ErrorMessage;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ExperimenterMessage;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowRemovedMessage;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReplyMessage;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
78 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessage;
79 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortStatusMessage;
80 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketReceived;
81 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.TransmitPacketInput;
82 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.NodeConnectorStatisticsUpdate;
83 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.QueueStatisticsUpdate;
84 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.TableUpdated;
85 import org.opendaylight.yangtools.yang.binding.DataContainer;
86 import org.opendaylight.yangtools.yang.binding.DataObject;
87 import org.slf4j.Logger;
88 import org.slf4j.LoggerFactory;
90 import com.google.common.collect.Lists;
91 import com.google.common.util.concurrent.Futures;
92 import com.google.common.util.concurrent.ListenableFuture;
93 import com.google.common.util.concurrent.ListeningExecutorService;
94 import com.google.common.util.concurrent.MoreExecutors;
99 public class MDController implements IMDController, AutoCloseable {
101 private static final Logger LOG = LoggerFactory.getLogger(MDController.class);
103 private Collection<SwitchConnectionProvider> switchConnectionProviders;
105 private ConcurrentMap<TranslatorKey, Collection<IMDMessageTranslator<OfHeader, List<DataObject>>>> messageTranslators;
106 private Map<Class<? extends DataObject>, Collection<PopListener<DataObject>>> popListeners;
107 private MessageSpy<DataContainer> messageSpyCounter;
109 final private int OF10 = OFConstants.OFP_VERSION_1_0;
110 final private int OF13 = OFConstants.OFP_VERSION_1_3;
112 private ErrorHandlerSimpleImpl errorHandler;
115 * @return translator mapping
117 public Map<TranslatorKey, Collection<IMDMessageTranslator<OfHeader, List<DataObject>>>> getMessageTranslators() {
118 return messageTranslators;
122 * provisioning of translator mapping
126 messageTranslators = new ConcurrentHashMap<>();
127 popListeners = new ConcurrentHashMap<>();
128 //TODO: move registration to factory
129 addMessageTranslator(ErrorMessage.class, OF10, new ErrorV10Translator());
130 addMessageTranslator(ErrorMessage.class, OF13, new ErrorTranslator());
131 addMessageTranslator(FlowRemovedMessage.class, OF10, new FlowRemovedTranslator());
132 addMessageTranslator(FlowRemovedMessage.class, OF13, new FlowRemovedTranslator());
133 addMessageTranslator(PacketInMessage.class,OF10, new PacketInV10Translator());
134 addMessageTranslator(PacketInMessage.class,OF13, new PacketInTranslator());
135 addMessageTranslator(PortStatusMessage.class,OF10, new PortStatusMessageToNodeConnectorUpdatedTranslator());
136 addMessageTranslator(PortStatusMessage.class,OF13, new PortStatusMessageToNodeConnectorUpdatedTranslator());
137 addMessageTranslator(MultipartReplyMessage.class,OF13,new MultiPartReplyPortToNodeConnectorUpdatedTranslator());
138 addMessageTranslator(MultipartReplyMessage.class,OF10, new MultiPartMessageDescToNodeUpdatedTranslator());
139 addMessageTranslator(MultipartReplyMessage.class,OF13, new MultiPartMessageDescToNodeUpdatedTranslator());
140 addMessageTranslator(ExperimenterMessage.class, OF10, new ExperimenterTranslator());
141 addMessageTranslator(MultipartReplyMessage.class,OF10, new MultipartReplyTranslator());
142 addMessageTranslator(MultipartReplyMessage.class,OF13, new MultipartReplyTranslator());
143 addMessageTranslator(MultipartReplyMessage.class,OF13,new MultipartReplyTableFeaturesToTableUpdatedTranslator());
144 addMessageTranslator(GetFeaturesOutput.class,OF10, new FeaturesV10ToNodeConnectorUpdatedTranslator());
146 NotificationPopListener<DataObject> notificationPopListener = new NotificationPopListener<DataObject>();
147 notificationPopListener.setNotificationProviderService(
148 OFSessionUtil.getSessionManager().getNotificationProviderService());
149 notificationPopListener.setMessageSpy(messageSpyCounter);
151 //TODO: move registration to factory
152 addMessagePopListener(NodeErrorNotification.class, notificationPopListener);
153 addMessagePopListener(BadActionErrorNotification.class, notificationPopListener);
154 addMessagePopListener(BadInstructionErrorNotification.class, notificationPopListener);
155 addMessagePopListener(BadMatchErrorNotification.class, notificationPopListener);
156 addMessagePopListener(BadRequestErrorNotification.class, notificationPopListener);
157 addMessagePopListener(ExperimenterErrorNotification.class, notificationPopListener);
158 addMessagePopListener(FlowModErrorNotification.class, notificationPopListener);
159 addMessagePopListener(GroupModErrorNotification.class, notificationPopListener);
160 addMessagePopListener(HelloFailedErrorNotification.class, notificationPopListener);
161 addMessagePopListener(MeterModErrorNotification.class, notificationPopListener);
162 addMessagePopListener(PortModErrorNotification.class, notificationPopListener);
163 addMessagePopListener(QueueOpErrorNotification.class, notificationPopListener);
164 addMessagePopListener(RoleRequestErrorNotification.class, notificationPopListener);
165 addMessagePopListener(SwitchConfigErrorNotification.class, notificationPopListener);
166 addMessagePopListener(TableFeaturesErrorNotification.class, notificationPopListener);
167 addMessagePopListener(TableModErrorNotification.class, notificationPopListener);
168 addMessagePopListener(NodeConnectorUpdated.class,notificationPopListener);
169 addMessagePopListener(PacketReceived.class,notificationPopListener);
170 addMessagePopListener(TransmitPacketInput.class, notificationPopListener);
171 addMessagePopListener(NodeUpdated.class, notificationPopListener);
173 addMessagePopListener(SwitchFlowRemoved.class, notificationPopListener);
174 addMessagePopListener(TableUpdated.class, notificationPopListener);
176 //Notification registration for flow statistics
177 addMessagePopListener(FlowsStatisticsUpdate.class, notificationPopListener);
178 addMessagePopListener(AggregateFlowStatisticsUpdate.class, notificationPopListener);
180 //Notification registrations for group-statistics
181 addMessagePopListener(GroupStatisticsUpdated.class, notificationPopListener);
182 addMessagePopListener(GroupFeaturesUpdated.class, notificationPopListener);
183 addMessagePopListener(GroupDescStatsUpdated.class, notificationPopListener);
185 //Notification registrations for meter-statistics
186 addMessagePopListener(MeterStatisticsUpdated.class, notificationPopListener);
187 addMessagePopListener(MeterConfigStatsUpdated.class, notificationPopListener);
188 addMessagePopListener(MeterFeaturesUpdated.class, notificationPopListener);
190 //Notification registration for port-statistics
191 addMessagePopListener(NodeConnectorStatisticsUpdate.class, notificationPopListener);
193 //Notification registration for flow-table statistics
194 addMessagePopListener(FlowTableStatisticsUpdate.class, notificationPopListener);
196 //Notification registration for queue-statistics
197 addMessagePopListener(QueueStatisticsUpdate.class, notificationPopListener);
199 //Notification for LLDPSpeaker
200 LLDPSpeakerPopListener<DataObject> lldpPopListener = new LLDPSpeakerPopListener<DataObject>();
201 addMessagePopListener(NodeConnectorUpdated.class,lldpPopListener);
203 // Push the updated Listeners to Session Manager which will be then picked up by ConnectionConductor eventually
204 OFSessionUtil.getSessionManager().setTranslatorMapping(messageTranslators);
205 OFSessionUtil.getSessionManager().setPopListenerMapping(popListeners);
206 OFSessionUtil.getSessionManager().setMessageSpy(messageSpyCounter);
208 // prepare worker pool for rpc
209 // TODO: get size from configSubsystem
210 int rpcThreadLimit = 10;
211 ListeningExecutorService rpcPoolDelegator = createRpcPoolSpyDecorated(rpcThreadLimit, messageSpyCounter);
212 OFSessionUtil.getSessionManager().setRpcPool(rpcPoolDelegator);
217 * @param rpcThreadLimit
221 private static ListeningExecutorService createRpcPoolSpyDecorated(int rpcThreadLimit, MessageSpy<DataContainer> messageSpy) {
222 ThreadPoolLoggingExecutor rpcPool = new ThreadPoolLoggingExecutor(rpcThreadLimit, rpcThreadLimit, 0L,
223 TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>());
224 ListeningExecutorService listeningRpcPool = MoreExecutors.listeningDecorator(rpcPool);
225 RpcListeningExecutorService rpcPoolDecorated = new RpcListeningExecutorService(listeningRpcPool);
226 rpcPoolDecorated.setMessageSpy(messageSpy);
227 return rpcPoolDecorated;
231 * @param switchConnectionProviders
232 * the switchConnectionProviders to set
234 public void setSwitchConnectionProviders(Collection<SwitchConnectionProvider> switchConnectionProviders) {
235 this.switchConnectionProviders = switchConnectionProviders;
239 * Function called by dependency manager after "init ()" is called and after
240 * the services provided by the class are registered in the service registry
243 public void start() {
244 LOG.debug("starting ..");
245 LOG.debug("switchConnectionProvider: " + switchConnectionProviders);
247 SwitchConnectionHandlerImpl switchConnectionHandler = new SwitchConnectionHandlerImpl();
248 switchConnectionHandler.setMessageSpy(messageSpyCounter);
250 errorHandler = new ErrorHandlerSimpleImpl();
252 switchConnectionHandler.setErrorHandler(errorHandler);
253 switchConnectionHandler.init();
255 List<ListenableFuture<Boolean>> starterChain = new ArrayList<>();
256 for (SwitchConnectionProvider switchConnectionPrv : switchConnectionProviders) {
257 switchConnectionPrv.setSwitchConnectionHandler(switchConnectionHandler);
258 ListenableFuture<Boolean> isOnlineFuture = switchConnectionPrv.startup();
259 starterChain.add(isOnlineFuture);
262 Future<List<Boolean>> srvStarted = Futures.allAsList(starterChain);
266 * @return wished connections configurations
267 * @deprecated use configSubsystem
270 private static Collection<ConnectionConfiguration> getConnectionConfiguration() {
271 // TODO:: get config from state manager
272 ConnectionConfiguration configuration = ConnectionConfigurationFactory.getDefault();
273 ConnectionConfiguration configurationLegacy = ConnectionConfigurationFactory.getLegacy();
274 return Lists.newArrayList(configuration, configurationLegacy);
278 * Function called by the dependency manager before the services exported by
279 * the component are unregistered, this will be followed by a "destroy ()"
284 LOG.debug("stopping");
285 List<ListenableFuture<Boolean>> stopChain = new ArrayList<>();
287 for (SwitchConnectionProvider switchConnectionPrv : switchConnectionProviders) {
288 ListenableFuture<Boolean> shutdown = switchConnectionPrv.shutdown();
289 stopChain.add(shutdown);
291 Futures.allAsList(stopChain).get(5000, TimeUnit.MILLISECONDS);
292 } catch (InterruptedException | ExecutionException | TimeoutException e) {
293 LOG.error(e.getMessage(), e);
299 * Function called by the dependency manager when at least one dependency
300 * become unsatisfied or when the component is shutting down because for
301 * example bundle is being stopped.
304 public void destroy() {
309 public void addMessageTranslator(Class<? extends DataObject> messageType, int version, IMDMessageTranslator<OfHeader, List<DataObject>> translator) {
310 TranslatorKey tKey = new TranslatorKey(version, messageType.getName());
312 Collection<IMDMessageTranslator<OfHeader, List<DataObject>>> existingValues = messageTranslators.get(tKey);
313 if (existingValues == null) {
314 existingValues = new LinkedHashSet<>();
315 messageTranslators.put(tKey, existingValues);
317 existingValues.add(translator);
318 LOG.debug("{} is now translated by {}", messageType, translator);
322 public void removeMessageTranslator(Class<? extends DataObject> messageType, int version, IMDMessageTranslator<OfHeader, List<DataObject>> translator) {
323 TranslatorKey tKey = new TranslatorKey(version, messageType.getName());
324 Collection<IMDMessageTranslator<OfHeader, List<DataObject>>> values = messageTranslators.get(tKey);
325 if (values != null) {
326 values.remove(translator);
327 if (values.isEmpty()) {
328 messageTranslators.remove(tKey);
330 LOG.debug("{} is now removed from translators", translator);
335 public void addMessagePopListener(Class<? extends DataObject> messageType, PopListener<DataObject> popListener) {
336 Collection<PopListener<DataObject>> existingValues = popListeners.get(messageType);
337 if (existingValues == null) {
338 existingValues = new LinkedHashSet<>();
339 popListeners.put(messageType, existingValues);
341 existingValues.add(popListener);
342 LOG.debug("{} is now popListened by {}", messageType, popListener);
346 public void removeMessagePopListener(Class<? extends DataObject> messageType, PopListener<DataObject> popListener) {
347 Collection<PopListener<DataObject>> values = popListeners.get(messageType);
348 if (values != null) {
349 values.remove(popListener);
350 if (values.isEmpty()) {
351 popListeners.remove(messageType);
353 LOG.debug("{} is now removed from popListeners", popListener);
358 * @param messageSpyCounter the messageSpyCounter to set
360 public void setMessageSpyCounter(
361 MessageSpy<DataContainer> messageSpyCounter) {
362 this.messageSpyCounter = messageSpyCounter;
366 public void close() {
368 messageSpyCounter = null;
369 messageTranslators = null;
371 for (SwitchConnectionProvider switchConnectionPrv : switchConnectionProviders) {
372 switchConnectionPrv.setSwitchConnectionHandler(null);
374 switchConnectionProviders = null;
375 OFSessionUtil.releaseSessionManager();