85eea8ca29d1245171414f1625973da569a2c61d
[openflowplugin.git] / openflowplugin / src / main / java / org / opendaylight / openflowplugin / openflow / md / core / translator / PacketInTranslator.java
1 /**
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.openflowplugin.openflow.md.core.translator;
9
10 import java.math.BigInteger;
11 import java.util.Collections;
12 import java.util.List;
13
14 import org.opendaylight.openflowplugin.openflow.md.core.IMDMessageTranslator;
15 import org.opendaylight.openflowplugin.openflow.md.core.SwitchConnectionDistinguisher;
16 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.match.MatchConvertorImpl;
17 import org.opendaylight.openflowplugin.openflow.md.core.session.SessionContext;
18 import org.opendaylight.openflowplugin.openflow.md.util.InventoryDataServiceUtil;
19 import org.opendaylight.openflowplugin.openflow.md.util.PacketInUtil;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowCookie;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.Match;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.PortNumberMatchEntry;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.oxm.fields.grouping.MatchEntries;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessage;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.ConnectionCookie;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketReceived;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketReceivedBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.packet.received.MatchBuilder;
31 import org.opendaylight.yangtools.yang.binding.DataObject;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 /**
36  * translates packetIn from OF-API model to MD-SAL model, supports OF-1.3
37  */
38 public class PacketInTranslator implements IMDMessageTranslator<OfHeader, List<DataObject>> {
39
40     protected static final Logger LOG = LoggerFactory
41             .getLogger(PacketInTranslator.class);
42     @Override
43     public List<DataObject> translate(final SwitchConnectionDistinguisher cookie,
44             final SessionContext sc, final OfHeader msg) {
45
46         List<DataObject> salPacketIn = Collections.emptyList();
47
48         if (sc != null && msg instanceof PacketInMessage) {
49             PacketInMessage message = (PacketInMessage)msg;
50             LOG.trace("PacketIn[v{}]: Cookie: {} Match.type: {}",
51                     message.getVersion(), message.getCookie(),
52                     message.getMatch() != null ? message.getMatch().getType() : message.getMatch());
53
54            // create a packet received event builder
55            PacketReceivedBuilder pktInBuilder = new PacketReceivedBuilder();
56            pktInBuilder.setPayload(message.getData());
57            if (cookie != null) {
58                pktInBuilder.setConnectionCookie(new ConnectionCookie(cookie.getCookie()));
59            }
60
61            // get the DPID
62            GetFeaturesOutput features = sc.getFeatures();
63            // Make sure we actually have features, some naughty switches start sending packetIn before they send us the FeatureReply
64            if ( features == null) {
65                LOG.warn("Received packet_in, but there is no device datapathId received yet");
66            } else {
67                BigInteger dpid = features.getDatapathId();
68
69                // get the Cookie if it exists
70                if(message.getCookie() != null) {
71                    pktInBuilder.setFlowCookie(new FlowCookie(message.getCookie()));
72                }
73
74                // extract the port number
75                Long port = null;
76                if (message.getMatch() != null && message.getMatch().getMatchEntries() != null) {
77                    List<MatchEntries> entries = message.getMatch().getMatchEntries();
78                    for (MatchEntries entry : entries) {
79                        PortNumberMatchEntry tmp = entry.getAugmentation(PortNumberMatchEntry.class);
80                        if (tmp != null) {
81                            if (port == null) {
82                                port = tmp.getPortNumber().getValue();
83                            } else {
84                                LOG.warn("Multiple input ports discovered when walking through match entries (at least {} and {})",
85                                         port, tmp.getPortNumber().getValue());
86                            }
87                        }
88                    }
89                }
90
91                if (port == null) {
92                    // no incoming port, so drop the event
93                    LOG.warn("Received packet_in, but couldn't find an input port");
94                } else {
95                    LOG.trace("Received packet_in from {} on port {}", dpid, port);
96
97                    Match match = MatchConvertorImpl.fromOFMatchToSALMatch(message.getMatch(),dpid);
98                    MatchBuilder matchBuilder = new MatchBuilder(match);
99                    pktInBuilder.setMatch(matchBuilder.build());
100
101                    pktInBuilder.setPacketInReason(PacketInUtil.getMdSalPacketInReason(message.getReason()));
102                    pktInBuilder.setTableId(new org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.TableId(message.getTableId().getValue().shortValue()));
103                    pktInBuilder.setIngress(InventoryDataServiceUtil.nodeConnectorRefFromDatapathIdPortno(dpid,port));
104                    PacketReceived pktInEvent = pktInBuilder.build();
105                    salPacketIn = Collections.<DataObject>singletonList(pktInEvent);
106                }
107            }
108         }
109         return salPacketIn;
110     }
111 }