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.extension.api.AugmentTuple;
15 import org.opendaylight.openflowplugin.extension.api.path.MatchPath;
16 import org.opendaylight.openflowplugin.api.openflow.md.core.IMDMessageTranslator;
17 import org.opendaylight.openflowplugin.api.openflow.md.core.SwitchConnectionDistinguisher;
18 import org.opendaylight.openflowplugin.openflow.md.core.extension.MatchExtensionHelper;
19 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.match.MatchConvertorImpl;
20 import org.opendaylight.openflowplugin.api.openflow.md.core.session.SessionContext;
21 import org.opendaylight.openflowplugin.openflow.md.util.InventoryDataServiceUtil;
22 import org.opendaylight.openflowplugin.api.openflow.md.util.OpenflowVersion;
23 import org.opendaylight.openflowplugin.openflow.md.util.PacketInUtil;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowCookie;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.Match;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.PortNumberMatchEntry;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.oxm.fields.grouping.MatchEntries;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessage;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.ConnectionCookie;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketReceived;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketReceivedBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.packet.received.MatchBuilder;
35 import org.opendaylight.yangtools.yang.binding.DataObject;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
40 * translates packetIn from OF-API model to MD-SAL model, supports OF-1.3
42 public class PacketInTranslator implements IMDMessageTranslator<OfHeader, List<DataObject>> {
44 protected static final Logger LOG = LoggerFactory
45 .getLogger(PacketInTranslator.class);
47 public List<DataObject> translate(final SwitchConnectionDistinguisher cookie,
48 final SessionContext sc, final OfHeader msg) {
50 List<DataObject> salPacketIn = Collections.emptyList();
52 if (sc != null && msg instanceof PacketInMessage) {
53 PacketInMessage message = (PacketInMessage)msg;
54 LOG.trace("PacketIn[v{}]: Cookie: {} Match.type: {}",
55 message.getVersion(), message.getCookie(),
56 message.getMatch() != null ? message.getMatch().getType() : message.getMatch());
58 // create a packet received event builder
59 PacketReceivedBuilder pktInBuilder = new PacketReceivedBuilder();
60 pktInBuilder.setPayload(message.getData());
62 pktInBuilder.setConnectionCookie(new ConnectionCookie(cookie.getCookie()));
66 GetFeaturesOutput features = sc.getFeatures();
67 // Make sure we actually have features, some naughty switches start sending packetIn before they send us the FeatureReply
68 if ( features == null) {
69 LOG.warn("Received packet_in, but there is no device datapathId received yet");
71 BigInteger dpid = features.getDatapathId();
73 // get the Cookie if it exists
74 if(message.getCookie() != null) {
75 pktInBuilder.setFlowCookie(new FlowCookie(message.getCookie()));
78 // extract the port number
80 if (message.getMatch() != null && message.getMatch().getMatchEntries() != null) {
81 List<MatchEntries> entries = message.getMatch().getMatchEntries();
82 for (MatchEntries entry : entries) {
83 PortNumberMatchEntry tmp = entry.getAugmentation(PortNumberMatchEntry.class);
86 port = tmp.getPortNumber().getValue();
88 LOG.warn("Multiple input ports discovered when walking through match entries (at least {} and {})",
89 port, tmp.getPortNumber().getValue());
96 // no incoming port, so drop the event
97 LOG.warn("Received packet_in, but couldn't find an input port");
99 LOG.trace("Received packet_in from {} on port {}", dpid, port);
101 OpenflowVersion ofVersion = OpenflowVersion.get(sc.getPrimaryConductor().getVersion());
102 Match match = MatchConvertorImpl.fromOFMatchToSALMatch(message.getMatch(),dpid, ofVersion).build();
103 MatchBuilder matchBuilder = new MatchBuilder(match);
105 AugmentTuple<org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.packet.received.Match> matchExtensionWrap =
106 MatchExtensionHelper.processAllExtensions(
107 message.getMatch().getMatchEntries(), ofVersion, MatchPath.PACKETRECEIVED_MATCH);
108 if (matchExtensionWrap != null) {
109 matchBuilder.addAugmentation(matchExtensionWrap.getAugmentationClass(), matchExtensionWrap.getAugmentationObject());
112 org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.packet.received.Match packetInMatch = matchBuilder.build();
113 pktInBuilder.setMatch(packetInMatch);
114 pktInBuilder.setPacketInReason(PacketInUtil.getMdSalPacketInReason(message.getReason()));
115 pktInBuilder.setTableId(new org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.TableId(message.getTableId().getValue().shortValue()));
116 pktInBuilder.setIngress(InventoryDataServiceUtil.nodeConnectorRefFromDatapathIdPortno(dpid, port, ofVersion));
117 PacketReceived pktInEvent = pktInBuilder.build();
118 salPacketIn = Collections.<DataObject>singletonList(pktInEvent);