Switched to using IMDMessageTranslator<OfHeader, List<DataObject>>> in MDController
[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.List;
5 import java.util.concurrent.CopyOnWriteArrayList;
6
7 import org.opendaylight.openflowplugin.openflow.md.core.IMDMessageTranslator;
8 import org.opendaylight.openflowplugin.openflow.md.core.SwitchConnectionDistinguisher;
9 import org.opendaylight.openflowplugin.openflow.md.core.session.SessionContext;
10 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
11 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRef;
12 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.PortNumberMatchEntry;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.oxm.fields.MatchEntries;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessage;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.Cookie;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketReceived;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketReceivedBuilder;
23 import org.opendaylight.yangtools.yang.binding.DataObject;
24 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
25 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.InstanceIdentifierBuilder;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 public class PacketInTranslator implements IMDMessageTranslator<OfHeader, List<DataObject>> {
30
31     protected static final Logger LOG = LoggerFactory
32             .getLogger(PacketInTranslator.class);
33     @Override
34     public List<DataObject> translate(SwitchConnectionDistinguisher cookie,
35             SessionContext sc, OfHeader msg) {
36         if(msg instanceof PacketInMessage) {
37             PacketInMessage message = (PacketInMessage)msg;
38             List<DataObject> list = new CopyOnWriteArrayList<DataObject>();
39             LOG.info("PacketIn: InPort: {} Cookie: {} Match.type: {}",
40                     message.getInPort(), message.getCookie(),
41                     message.getMatch() != null ? message.getMatch().getType()
42                                               : message.getMatch());
43
44            // create a packet received event builder
45            PacketReceivedBuilder pktInBuilder = new PacketReceivedBuilder();
46            pktInBuilder.setPayload(message.getData());
47
48            // get the DPID
49            GetFeaturesOutput features = sc.getFeatures();
50            BigInteger dpid = features.getDatapathId();
51
52            // get the Cookie if it exists
53            if(message.getCookie() != null) {
54                pktInBuilder.setCookie(new Cookie(message.getCookie().longValue()));
55            }
56
57            // extract the port number
58            Long port = null;
59
60            if (message.getInPort() != null) {
61                // this doesn't work--at least for OF1.3
62                port = message.getInPort().longValue();
63            }
64
65            // this should work for OF1.3
66            if (message.getMatch() != null && message.getMatch().getMatchEntries() != null) {
67                List<MatchEntries> entries = message.getMatch().getMatchEntries();
68                for (MatchEntries entry : entries) {
69                    PortNumberMatchEntry tmp = entry.getAugmentation(PortNumberMatchEntry.class);
70                    if (tmp != null) {
71                        if (port == null) {
72                            port = tmp.getPortNumber().getValue();
73                        } else {
74                            LOG.warn("Multiple input ports (at least {} and {})",
75                                     port, tmp.getPortNumber().getValue());
76                        }
77                    }
78                }
79            }
80
81            if (port == null) {
82                // no incoming port, so drop the event
83                LOG.warn("Received packet_in, but couldn't find an input port");
84                return null;
85            }else{
86                LOG.info("Receive packet_in from {} on port {}", dpid, port);
87            }
88
89            //TODO: need to get the NodeConnectorRef, but NodeConnectors aren't there yet
90            InstanceIdentifier<NodeConnector> nci = ncIndentifierFromDPIDandPort(dpid, port);
91            NodeConnectorRef ncr = new NodeConnectorRef(nci);
92            PacketReceived pktInEvent = pktInBuilder.build();
93            list.add(pktInEvent);
94             return list;
95         } else {
96             return null;
97         }
98     }
99
100     public static InstanceIdentifier<NodeConnector> ncIndentifierFromDPIDandPort(BigInteger dpid, Long port) {
101         InstanceIdentifierBuilder<?> builder = InstanceIdentifier.builder().node(Node.class);
102
103         // TODO: this doesn't work yet, needs to actaully get the ref for the real NodeConnector
104         //       but that doesn't exist yet
105         NodeConnectorKey ncKey = ncKeyFromDPIDandPort(dpid, port);
106         return builder.node(NodeConnector.class, ncKey).toInstance();
107     }
108
109
110     public static NodeConnectorKey ncKeyFromDPIDandPort(BigInteger dpid, Long port){
111         return new NodeConnectorKey(ncIDfromDPIDandPort(dpid, port));
112     }
113
114     public static NodeConnectorId ncIDfromDPIDandPort(BigInteger dpid, Long port){
115         return new NodeConnectorId("openflow:"+dpid.toString()+":"+port.toString());
116     }
117 }