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.openflow.device.DeviceContext;
11 import org.opendaylight.openflowplugin.api.openflow.device.MessageTranslator;
12 import org.opendaylight.openflowplugin.api.openflow.md.util.OpenflowVersion;
13 import org.opendaylight.openflowplugin.extension.api.AugmentTuple;
14 import org.opendaylight.openflowplugin.extension.api.path.MatchPath;
15 import org.opendaylight.openflowplugin.openflow.md.core.extension.MatchExtensionHelper;
16 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.match.MatchConvertorImpl;
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.flow.types.rev131026.FlowCookie;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.Match;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntry;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.InPortCase;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessage;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.ConnectionCookie;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketReceived;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketReceivedBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.packet.received.MatchBuilder;
29 import java.math.BigInteger;
30 import java.util.List;
33 * Created by tkubas on 4/1/15.
35 public class PacketReceivedTranslator implements MessageTranslator<PacketInMessage, PacketReceived> {
37 public PacketReceived translate(PacketInMessage input, DeviceContext deviceContext, Object connectionDistinguisher) {
39 PacketReceivedBuilder packetReceivedBuilder = new PacketReceivedBuilder();
40 BigInteger datapathId = deviceContext.getPrimaryConnectionContext().getFeatures().getDatapathId();
42 // extract the port number
44 if(input.getVersion() == OpenflowVersion.OF10.getVersion()) {
45 port = input.getInPort().longValue();
46 } else if (input.getVersion() == OpenflowVersion.OF13.getVersion()) {
47 if (input.getMatch() != null && input.getMatch().getMatchEntry() != null) {
48 port = getPortNumberFromMatch(input.getMatch().getMatchEntry());
52 //TODO connection cookie from connection distinguisher
53 // packetReceivedBuilder.setConnectionCookie(new ConnectionCookie(input.getCookie().longValue()));
54 packetReceivedBuilder.setPayload(input.getData());
55 packetReceivedBuilder.setFlowCookie(new FlowCookie(input.getCookie()));
56 packetReceivedBuilder.setIngress(InventoryDataServiceUtil.nodeConnectorRefFromDatapathIdPortno(datapathId, port, OpenflowVersion.get(input.getVersion())));
58 packetReceivedBuilder.setPacketInReason(PacketInUtil.getMdSalPacketInReason(input.getReason()));
59 packetReceivedBuilder.setTableId(new org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.TableId(input.getTableId().getValue().shortValue()));
62 org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.packet.received.Match packetInMatch = getPacketInMatch(input, datapathId);
63 packetReceivedBuilder.setMatch(packetInMatch);
65 return packetReceivedBuilder.build();
68 private org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.packet.received.Match getPacketInMatch(PacketInMessage input, BigInteger datapathId) {
69 Match match = MatchConvertorImpl.fromOFMatchToSALMatch(input.getMatch(),
71 OpenflowVersion.get(input.getVersion().shortValue())).build();
72 MatchBuilder matchBuilder = new MatchBuilder(match);
74 AugmentTuple<org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.packet.received.Match> matchExtensionWrap =
75 MatchExtensionHelper.processAllExtensions(
76 input.getMatch().getMatchEntry(), OpenflowVersion.get(input.getVersion().shortValue()), MatchPath.PACKETRECEIVED_MATCH);
77 if (matchExtensionWrap != null) {
78 matchBuilder.addAugmentation(matchExtensionWrap.getAugmentationClass(), matchExtensionWrap.getAugmentationObject());
80 return matchBuilder.build();
83 private Long getPortNumberFromMatch(List<MatchEntry> entries) {
85 for (MatchEntry entry : entries) {
86 InPortCase inPortCase = ((InPortCase) entry.getMatchEntryValue());
87 org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.in.port._case.InPort inPort = inPortCase.getInPort();
91 port = inPort.getPortNumber().getValue();
93 // LOG.warn("Multiple input ports discovered when walking through match entries (at least {} and {})",
94 // port, inPort.getPortNumber().getValue());