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