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