1 package org.opendaylight.openflowplugin.openflow.md.core.translator;
3 import java.math.BigInteger;
4 import java.util.Collections;
6 import java.util.concurrent.CopyOnWriteArrayList;
8 import org.opendaylight.openflowplugin.openflow.md.core.IMDMessageTranslator;
9 import org.opendaylight.openflowplugin.openflow.md.core.SwitchConnectionDistinguisher;
10 import org.opendaylight.openflowplugin.openflow.md.core.session.SessionContext;
11 import org.opendaylight.openflowplugin.openflow.md.util.InventoryDataServiceUtil;
12 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.PortNumberMatchEntry;
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.oxm.fields.MatchEntries;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessage;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.Cookie;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketReceived;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketReceivedBuilder;
20 import org.opendaylight.yangtools.yang.binding.DataObject;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
24 public class PacketInTranslator implements IMDMessageTranslator<OfHeader, List<DataObject>> {
26 protected static final Logger LOG = LoggerFactory
27 .getLogger(PacketInTranslator.class);
29 public List<DataObject> translate(SwitchConnectionDistinguisher cookie,
30 SessionContext sc, OfHeader msg) {
31 if(sc !=null && msg instanceof PacketInMessage) {
32 PacketInMessage message = (PacketInMessage)msg;
33 List<DataObject> list = new CopyOnWriteArrayList<DataObject>();
34 LOG.info("PacketIn: InPort: {} Cookie: {} Match.type: {}",
35 message.getInPort(), message.getCookie(),
36 message.getMatch() != null ? message.getMatch().getType()
37 : message.getMatch());
39 // create a packet received event builder
40 PacketReceivedBuilder pktInBuilder = new PacketReceivedBuilder();
41 pktInBuilder.setPayload(message.getData());
44 GetFeaturesOutput features = sc.getFeatures();
45 // Make sure we actually have features, some naughty switches start sending packetIn before they send us the FeatureReply
46 if ( features != null) {
47 BigInteger dpid = features.getDatapathId();
49 // get the Cookie if it exists
50 if(message.getCookie() != null) {
51 pktInBuilder.setCookie(new Cookie(message.getCookie().longValue()));
54 // extract the port number
57 if (message.getInPort() != null) {
58 // this doesn't work--at least for OF1.3
59 port = message.getInPort().longValue();
62 // this should work for OF1.3
63 if (message.getMatch() != null && message.getMatch().getMatchEntries() != null) {
64 List<MatchEntries> entries = message.getMatch().getMatchEntries();
65 for (MatchEntries entry : entries) {
66 PortNumberMatchEntry tmp = entry.getAugmentation(PortNumberMatchEntry.class);
69 port = tmp.getPortNumber().getValue();
71 LOG.warn("Multiple input ports (at least {} and {})",
72 port, tmp.getPortNumber().getValue());
79 // no incoming port, so drop the event
80 LOG.warn("Received packet_in, but couldn't find an input port");
83 LOG.info("Receive packet_in from {} on port {}", dpid, port);
85 pktInBuilder.setIngress(InventoryDataServiceUtil.nodeConnectorRefFromDatapathIdPortno(dpid,port));
86 PacketReceived pktInEvent = pktInBuilder.build();
91 return Collections.emptyList();