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.OpenflowVersion;
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;
30 * translates packetIn from OF-API model to MD-SAL model, supports OF-1.0
32 public class PacketInV10Translator implements IMDMessageTranslator<OfHeader, List<DataObject>> {
34 protected static final Logger LOG = LoggerFactory
35 .getLogger(PacketInV10Translator.class);
37 public List<DataObject> translate(final SwitchConnectionDistinguisher cookie,
38 final SessionContext sc, final OfHeader msg) {
40 List<DataObject> salPacketIn = Collections.emptyList();
42 if (sc != null && msg instanceof PacketInMessage) {
43 PacketInMessage message = (PacketInMessage)msg;
44 LOG.trace("PacketIn[v{}]: InPort: {}",
45 msg.getVersion(), message.getInPort());
47 // create a packet received event builder
48 PacketReceivedBuilder pktInBuilder = new PacketReceivedBuilder();
49 pktInBuilder.setPayload(message.getData());
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");
57 BigInteger dpid = features.getDatapathId();
59 // extract the port number
61 if (message.getInPort() != null) {
62 port = message.getInPort().longValue();
66 // no incoming port, so drop the event
67 LOG.warn("Received packet_in, but couldn't find an input port");
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 OpenflowVersion.get(sc.getPrimaryConductor().getVersion())));
73 PacketReceived pktInEvent = pktInBuilder.build();
74 salPacketIn = Collections.<DataObject>singletonList(pktInEvent);