2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.openflowplugin.openflow.md.core.translator;
10 import java.math.BigInteger;
11 import java.util.Collections;
12 import java.util.List;
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;
29 * translates packetIn from OF-API model to MD-SAL model, supports OF-1.0
31 public class PacketInV10Translator implements IMDMessageTranslator<OfHeader, List<DataObject>> {
33 protected static final Logger LOG = LoggerFactory
34 .getLogger(PacketInV10Translator.class);
36 public List<DataObject> translate(final SwitchConnectionDistinguisher cookie,
37 final SessionContext sc, final OfHeader msg) {
39 List<DataObject> salPacketIn = Collections.emptyList();
41 if (sc != null && msg instanceof PacketInMessage) {
42 PacketInMessage message = (PacketInMessage)msg;
43 LOG.trace("PacketIn[v{}]: InPort: {}",
44 msg.getVersion(), message.getInPort());
46 // create a packet received event builder
47 PacketReceivedBuilder pktInBuilder = new PacketReceivedBuilder();
48 pktInBuilder.setPayload(message.getData());
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");
56 BigInteger dpid = features.getDatapathId();
58 // extract the port number
60 if (message.getInPort() != null) {
61 port = message.getInPort().longValue();
65 // no incoming port, so drop the event
66 LOG.warn("Received packet_in, but couldn't find an input port");
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);