Fix double translation of messages.
[openflowplugin.git] / openflowplugin / src / main / java / org / opendaylight / openflowplugin / openflow / md / core / translator / PacketInTranslator.java
1 package org.opendaylight.openflowplugin.openflow.md.core.translator;
2
3 import java.math.BigInteger;
4 import java.util.Collections;
5 import java.util.List;
6 import java.util.concurrent.CopyOnWriteArrayList;
7
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;
23
24 public class PacketInTranslator implements IMDMessageTranslator<OfHeader, List<DataObject>> {
25
26     protected static final Logger LOG = LoggerFactory
27             .getLogger(PacketInTranslator.class);
28     @Override
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());
38
39            // create a packet received event builder
40            PacketReceivedBuilder pktInBuilder = new PacketReceivedBuilder();
41            pktInBuilder.setPayload(message.getData());
42
43            // get the DPID
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();
48     
49                // get the Cookie if it exists
50                if(message.getCookie() != null) {
51                    pktInBuilder.setCookie(new Cookie(message.getCookie().longValue()));
52                }
53     
54                // extract the port number
55                Long port = null;
56     
57                if (message.getInPort() != null) {
58                    // this doesn't work--at least for OF1.3
59                    port = message.getInPort().longValue();
60                }
61     
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);
67                        if (tmp != null) {
68                            if (port == null) {
69                                port = tmp.getPortNumber().getValue();
70                            } else {
71                                LOG.warn("Multiple input ports (at least {} and {})",
72                                         port, tmp.getPortNumber().getValue());
73                            }
74                        }
75                    }
76                }
77     
78                if (port == null) {
79                    // no incoming port, so drop the event
80                    LOG.warn("Received packet_in, but couldn't find an input port");
81                    return null;
82                }else{
83                    LOG.info("Receive packet_in from {} on port {}", dpid, port);
84                }
85                pktInBuilder.setIngress(InventoryDataServiceUtil.nodeConnectorRefFromDatapathIdPortno(dpid,port));
86                PacketReceived pktInEvent = pktInBuilder.build();
87                list.add(pktInEvent);
88                 return list;
89            } 
90         } 
91         return Collections.emptyList();
92     }
93 }