2 * Copyright (c) 2015 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.impl.device.translator;
10 import org.opendaylight.openflowplugin.api.OFConstants;
11 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
12 import org.opendaylight.openflowplugin.api.openflow.device.MessageTranslator;
13 import org.opendaylight.openflowplugin.api.openflow.md.util.OpenflowVersion;
14 import org.opendaylight.openflowplugin.extension.api.AugmentTuple;
15 import org.opendaylight.openflowplugin.extension.api.path.MatchPath;
16 import org.opendaylight.openflowplugin.openflow.md.core.extension.MatchExtensionHelper;
17 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.match.MatchConvertorImpl;
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.flow.types.rev131026.FlowCookie;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.Match;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntry;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.InPortCase;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessage;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.ConnectionCookie;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketReceived;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketReceivedBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.packet.received.MatchBuilder;
30 import java.math.BigInteger;
31 import java.util.List;
34 * Created by tkubas on 4/1/15.
36 public class PacketReceivedTranslator implements MessageTranslator<PacketInMessage, PacketReceived> {
38 public PacketReceived translate(PacketInMessage input, DeviceContext deviceContext, Object connectionDistinguisher) {
40 PacketReceivedBuilder packetReceivedBuilder = new PacketReceivedBuilder();
41 BigInteger datapathId = deviceContext.getPrimaryConnectionContext().getFeatures().getDatapathId();
43 // extract the port number
45 if(input.getVersion() == OFConstants.OFP_VERSION_1_0 && input.getInPort() != null) {
46 port = input.getInPort().longValue();
47 } else if (input.getVersion() == OFConstants.OFP_VERSION_1_3) {
48 if (input.getMatch() != null && input.getMatch().getMatchEntry() != null) {
49 port = getPortNumberFromMatch(input.getMatch().getMatchEntry());
53 //TODO connection cookie from connection distinguisher
54 // packetReceivedBuilder.setConnectionCookie(new ConnectionCookie(input.getCookie().longValue()));
55 packetReceivedBuilder.setPayload(input.getData());
56 // get the Cookie if it exists
57 if (input.getCookie() != null) {
58 packetReceivedBuilder.setFlowCookie(new FlowCookie(input.getCookie()));
61 packetReceivedBuilder.setIngress(InventoryDataServiceUtil.nodeConnectorRefFromDatapathIdPortno(datapathId, port, OpenflowVersion.get(input.getVersion())));
63 packetReceivedBuilder.setPacketInReason(PacketInUtil.getMdSalPacketInReason(input.getReason()));
65 if(input.getTableId() != null) {
66 packetReceivedBuilder.setTableId(new org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.TableId(input.getTableId().getValue().shortValue()));
69 if(input.getMatch() != null) {
70 org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.packet.received.Match packetInMatch = getPacketInMatch(input, datapathId);
71 packetReceivedBuilder.setMatch(packetInMatch);
74 return packetReceivedBuilder.build();
77 private org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.packet.received.Match getPacketInMatch(PacketInMessage input, BigInteger datapathId) {
78 Match match = MatchConvertorImpl.fromOFMatchToSALMatch(input.getMatch(),
80 OpenflowVersion.get(input.getVersion().shortValue())).build();
81 MatchBuilder matchBuilder = new MatchBuilder(match);
83 AugmentTuple<org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.packet.received.Match> matchExtensionWrap =
84 MatchExtensionHelper.processAllExtensions(
85 input.getMatch().getMatchEntry(), OpenflowVersion.get(input.getVersion().shortValue()), MatchPath.PACKETRECEIVED_MATCH);
86 if (matchExtensionWrap != null) {
87 matchBuilder.addAugmentation(matchExtensionWrap.getAugmentationClass(), matchExtensionWrap.getAugmentationObject());
89 return matchBuilder.build();
92 private Long getPortNumberFromMatch(List<MatchEntry> entries) {
94 for (MatchEntry entry : entries) {
95 InPortCase inPortCase = ((InPortCase) entry.getMatchEntryValue());
96 org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.in.port._case.InPort inPort = inPortCase.getInPort();
100 port = inPort.getPortNumber().getValue();
102 // LOG.warn("Multiple input ports discovered when walking through match entries (at least {} and {})",
103 // port, inPort.getPortNumber().getValue());