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.controller.protocol_plugin.openflow.internal;
11 import java.util.Collections;
12 import java.util.List;
15 import java.util.concurrent.ConcurrentHashMap;
16 import java.util.concurrent.ConcurrentMap;
17 import java.util.concurrent.CopyOnWriteArrayList;
19 import org.opendaylight.controller.protocol_plugin.openflow.IDataPacketListen;
20 import org.opendaylight.controller.protocol_plugin.openflow.IDataPacketMux;
21 import org.opendaylight.controller.protocol_plugin.openflow.IInventoryShimExternalListener;
22 import org.opendaylight.controller.protocol_plugin.openflow.core.IController;
23 import org.opendaylight.controller.protocol_plugin.openflow.core.IMessageListener;
24 import org.opendaylight.controller.protocol_plugin.openflow.core.ISwitch;
25 import org.openflow.protocol.OFMessage;
26 import org.openflow.protocol.OFPacketIn;
27 import org.openflow.protocol.OFPacketOut;
28 import org.openflow.protocol.OFPort;
29 import org.openflow.protocol.OFType;
30 import org.openflow.protocol.action.OFAction;
31 import org.openflow.protocol.action.OFActionOutput;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
35 import org.opendaylight.controller.sal.core.ConstructionException;
36 import org.opendaylight.controller.sal.core.ContainerFlow;
37 import org.opendaylight.controller.sal.core.IContainerListener;
38 import org.opendaylight.controller.sal.core.Node;
39 import org.opendaylight.controller.sal.core.NodeConnector;
40 import org.opendaylight.controller.sal.core.Property;
41 import org.opendaylight.controller.sal.core.UpdateType;
42 import org.opendaylight.controller.sal.packet.IPluginOutDataPacketService;
43 import org.opendaylight.controller.sal.packet.PacketResult;
44 import org.opendaylight.controller.sal.packet.RawPacket;
45 import org.opendaylight.controller.sal.utils.GlobalConstants;
46 import org.opendaylight.controller.sal.utils.HexEncode;
48 public class DataPacketMuxDemux implements IContainerListener,
49 IMessageListener, IDataPacketMux, IInventoryShimExternalListener {
50 protected static final Logger logger = LoggerFactory
51 .getLogger(DataPacketMuxDemux.class);
52 private IController controller = null;
53 private ConcurrentMap<Long, ISwitch> swID2ISwitch = new ConcurrentHashMap<Long, ISwitch>();
54 // Gives a map between a Container and all the DataPacket listeners on SAL
55 private ConcurrentMap<String, IPluginOutDataPacketService> pluginOutDataPacketServices = new ConcurrentHashMap<String, IPluginOutDataPacketService>();
56 // Gives a map between a NodeConnector and the containers to which it
58 private ConcurrentMap<NodeConnector, List<String>> nc2Container = new ConcurrentHashMap<NodeConnector, List<String>>();
59 // Gives a map between a Container and the FlowSpecs on it
60 private ConcurrentMap<String, List<ContainerFlow>> container2FlowSpecs = new ConcurrentHashMap<String, List<ContainerFlow>>();
61 // Track local data packet listener
62 private List<IDataPacketListen> iDataPacketListen = new CopyOnWriteArrayList<IDataPacketListen>();
64 void setIDataPacketListen(IDataPacketListen s) {
65 if (this.iDataPacketListen != null) {
66 if (!this.iDataPacketListen.contains(s)) {
67 logger.debug("Added new IDataPacketListen");
68 this.iDataPacketListen.add(s);
73 void unsetIDataPacketListen(IDataPacketListen s) {
74 if (this.iDataPacketListen != null) {
75 if (this.iDataPacketListen.contains(s)) {
76 logger.debug("Removed IDataPacketListen");
77 this.iDataPacketListen.remove(s);
82 void setPluginOutDataPacketService(Map<String, Object> props,
83 IPluginOutDataPacketService s) {
85 logger.error("Didn't receive the service properties");
88 String containerName = (String) props.get("containerName");
89 if (containerName == null) {
90 logger.error("containerName not supplied");
93 if (this.pluginOutDataPacketServices != null) {
94 // It's expected only one SAL per container as long as the
95 // replication is done in the SAL implementation toward
97 this.pluginOutDataPacketServices.put(containerName, s);
98 logger.debug("New outService for container: {}", containerName);
102 void unsetPluginOutDataPacketService(Map<String, Object> props,
103 IPluginOutDataPacketService s) {
105 logger.error("Didn't receive the service properties");
108 String containerName = (String) props.get("containerName");
109 if (containerName == null) {
110 logger.error("containerName not supplied");
113 if (this.pluginOutDataPacketServices != null) {
114 this.pluginOutDataPacketServices.remove(containerName);
115 logger.debug("Removed outService for container: {}", containerName);
119 void setController(IController s) {
120 logger.debug("Controller provider set in DATAPACKET SERVICES");
124 void unsetController(IController s) {
125 if (this.controller == s) {
126 logger.debug("Controller provider UNset in DATAPACKET SERVICES");
127 this.controller = null;
132 * Function called by the dependency manager when all the required
133 * dependencies are satisfied
137 this.controller.addMessageListener(OFType.PACKET_IN, this);
141 * Function called by the dependency manager when at least one dependency
142 * become unsatisfied or when the component is shutting down because for
143 * example bundle is being stopped.
147 this.controller.removeMessageListener(OFType.PACKET_IN, this);
149 // Clear state that may need to be reused on component restart
150 this.pluginOutDataPacketServices.clear();
151 this.nc2Container.clear();
152 this.container2FlowSpecs.clear();
153 this.controller = null;
154 this.swID2ISwitch.clear();
158 public void receive(ISwitch sw, OFMessage msg) {
159 if (sw == null || msg == null
160 || this.pluginOutDataPacketServices == null) {
161 // Something fishy, we cannot do anything
163 "sw: {} and/or msg: {} and/or pluginOutDataPacketServices: {} is null!",
164 new Object[] { sw, msg, this.pluginOutDataPacketServices });
167 if (msg instanceof OFPacketIn) {
168 OFPacketIn ofPacket = (OFPacketIn) msg;
169 Long ofSwitchID = Long.valueOf(sw.getId());
170 Short ofPortID = Short.valueOf(ofPacket.getInPort());
173 Node n = new Node(Node.NodeIDType.OPENFLOW, ofSwitchID);
174 NodeConnector p = PortConverter.toNodeConnector(ofPortID, n);
175 RawPacket dataPacket = new RawPacket(ofPacket.getPacketData());
176 dataPacket.setIncomingNodeConnector(p);
178 // Try to dispatch the packet locally, in here we will
179 // pass the parsed packet simply because once the
180 // packet infra is settled all the packets will passed
181 // around as parsed and read-only
182 for (int i = 0; i < this.iDataPacketListen.size(); i++) {
183 IDataPacketListen s = this.iDataPacketListen.get(i);
184 if (s.receiveDataPacket(dataPacket).equals(
185 PacketResult.CONSUME)) {
186 logger.trace("Consumed locally data packet");
191 // Now dispatch the packet toward SAL at least for
192 // default container, we need to revisit this in a general
193 // slicing architecture refresh
194 IPluginOutDataPacketService defaultOutService = this.pluginOutDataPacketServices
195 .get(GlobalConstants.DEFAULT.toString());
196 if (defaultOutService != null) {
197 defaultOutService.receiveDataPacket(dataPacket);
199 "Dispatched to apps a frame of size: {} on container: {}: {}",
201 ofPacket.getPacketData().length,
202 GlobalConstants.DEFAULT.toString(),
203 HexEncode.bytesToHexString(dataPacket
204 .getPacketData()) });
206 // Now check the mapping between nodeConnector and
207 // Container and later on optimally filter based on
209 List<String> containersRX = this.nc2Container.get(p);
210 if (containersRX != null) {
211 for (int i = 0; i < containersRX.size(); i++) {
212 String container = containersRX.get(i);
213 IPluginOutDataPacketService s = this.pluginOutDataPacketServices
216 // TODO add filtering on a per-flowSpec base
217 s.receiveDataPacket(dataPacket);
219 "Dispatched to apps a frame of size: {} on container: {}: {}",
221 ofPacket.getPacketData().length,
222 GlobalConstants.DEFAULT.toString(),
224 .bytesToHexString(dataPacket
225 .getPacketData()) });
231 // This is supposed to be the catch all for all the
232 // DataPacket hence we will assume it has been handled
234 } catch (ConstructionException cex) {
237 // If we reach this point something went wrong.
240 // We don't care about non-data packets
246 public void transmitDataPacket(RawPacket outPkt) {
248 if (outPkt == null) {
249 logger.debug("outPkt is null!");
253 NodeConnector outPort = outPkt.getOutgoingNodeConnector();
254 if (outPort == null) {
255 logger.debug("outPort is null! outPkt: {}", outPkt);
259 if (!outPort.getType().equals(
260 NodeConnector.NodeConnectorIDType.OPENFLOW)) {
261 // The output Port is not of type OpenFlow
262 logger.debug("outPort is not OF Type! outPort: {}", outPort);
266 Short port = (Short) outPort.getID();
267 Long swID = (Long) outPort.getNode().getID();
268 ISwitch sw = this.swID2ISwitch.get(swID);
271 // If we cannot get the controller descriptor we cannot even
272 // send out the frame
273 logger.debug("swID: {} - sw is null!", swID);
277 byte[] data = outPkt.getPacketData();
279 OFActionOutput action = new OFActionOutput().setPort(port);
281 OFPacketOut po = new OFPacketOut()
282 .setBufferId(OFPacketOut.BUFFER_ID_NONE)
283 .setInPort(OFPort.OFPP_NONE)
284 .setActions(Collections.singletonList((OFAction) action))
285 .setActionsLength((short) OFActionOutput.MINIMUM_LENGTH);
287 po.setLengthU(OFPacketOut.MINIMUM_LENGTH + po.getActionsLength()
289 po.setPacketData(data);
292 logger.trace("Transmitted a frame of size: {}", data.length);
295 public void addNode(Node node, Set<Property> props) {
297 logger.debug("node is null!");
301 long sid = (Long) node.getID();
302 ISwitch sw = controller.getSwitches().get(sid);
304 logger.debug("sid: {} - sw is null!", sid);
307 this.swID2ISwitch.put(sw.getId(), sw);
310 public void removeNode(Node node) {
312 logger.debug("node is null!");
316 long sid = (Long) node.getID();
317 ISwitch sw = controller.getSwitches().get(sid);
319 logger.debug("sid: {} - sw is null!", sid);
322 this.swID2ISwitch.remove(sw.getId());
326 public void tagUpdated(String containerName, Node n, short oldTag,
327 short newTag, UpdateType t) {
332 public void containerFlowUpdated(String containerName,
333 ContainerFlow previousFlow, ContainerFlow currentFlow, UpdateType t) {
334 if (this.container2FlowSpecs == null) {
335 logger.error("container2FlowSpecs is NULL");
338 List<ContainerFlow> fSpecs = this.container2FlowSpecs
340 if (fSpecs == null) {
341 fSpecs = new CopyOnWriteArrayList<ContainerFlow>();
345 if (!fSpecs.contains(previousFlow)) {
346 fSpecs.add(previousFlow);
350 if (fSpecs.contains(previousFlow)) {
351 fSpecs.remove(previousFlow);
360 public void nodeConnectorUpdated(String containerName, NodeConnector p,
362 if (this.nc2Container == null) {
363 logger.error("nc2Container is NULL");
366 List<String> containers = this.nc2Container.get(p);
367 if (containers == null) {
368 containers = new CopyOnWriteArrayList<String>();
370 boolean updateMap = false;
373 if (!containers.contains(containerName)) {
374 containers.add(containerName);
379 if (containers.contains(containerName)) {
380 containers.remove(containerName);
388 if (containers.isEmpty()) {
389 // Do cleanup to reduce memory footprint if no
390 // elements to be tracked
391 this.nc2Container.remove(p);
393 this.nc2Container.put(p, containers);
399 public void containerModeUpdated(UpdateType t) {
404 public void updateNode(Node node, UpdateType type, Set<Property> props) {
407 addNode(node, props);
418 public void updateNodeConnector(NodeConnector nodeConnector,
419 UpdateType type, Set<Property> props) {