5b328526b7682582bc9f552872ddea2b5a56643f
[openflowplugin.git] / openflowplugin / src / main / java / org / opendaylight / openflowplugin / openflow / md / core / translator / PacketInV10Translator.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.session.SessionContext;
17 import org.opendaylight.openflowplugin.openflow.md.util.InventoryDataServiceUtil;
18 import org.opendaylight.openflowplugin.openflow.md.util.PacketInUtil;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessage;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketReceived;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketReceivedBuilder;
24 import org.opendaylight.yangtools.yang.binding.DataObject;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 /**
29  * translates packetIn from OF-API model to MD-SAL model, supports OF-1.0
30  */
31 public class PacketInV10Translator implements IMDMessageTranslator<OfHeader, List<DataObject>> {
32
33     protected static final Logger LOG = LoggerFactory
34             .getLogger(PacketInV10Translator.class);
35     @Override
36     public List<DataObject> translate(final SwitchConnectionDistinguisher cookie,
37             final SessionContext sc, final OfHeader msg) {
38
39         List<DataObject> salPacketIn = Collections.emptyList();
40
41         if (sc != null && msg instanceof PacketInMessage) {
42             PacketInMessage message = (PacketInMessage)msg;
43             LOG.trace("PacketIn[v{}]: InPort: {}",
44                     msg.getVersion(), message.getInPort());
45
46             // create a packet received event builder
47             PacketReceivedBuilder pktInBuilder = new PacketReceivedBuilder();
48             pktInBuilder.setPayload(message.getData());
49
50             // get the DPID
51             GetFeaturesOutput features = sc.getFeatures();
52             // Make sure we actually have features, some naughty switches start sending packetIn before they send us the FeatureReply
53             if ( features == null) {
54                 LOG.warn("Received packet_in, but there is no device datapathId received yet");
55             } else {
56                 BigInteger dpid = features.getDatapathId();
57
58                 // extract the port number
59                 Long port = null;
60                 if (message.getInPort() != null) {
61                     port = message.getInPort().longValue();
62                 }
63
64                 if (port == null) {
65                     // no incoming port, so drop the event
66                     LOG.warn("Received packet_in, but couldn't find an input port");
67                 } else {
68                     LOG.trace("Received packet_in from {} on port {}", dpid, port);
69                     pktInBuilder.setPacketInReason(PacketInUtil.getMdSalPacketInReason(message.getReason()));
70                     pktInBuilder.setIngress(InventoryDataServiceUtil.nodeConnectorRefFromDatapathIdPortno(dpid, port));
71                     PacketReceived pktInEvent = pktInBuilder.build();
72                     salPacketIn = Collections.<DataObject>singletonList(pktInEvent);
73                 }
74             }
75         }
76         return salPacketIn;
77     }
78 }