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.translator;
10 import java.math.BigInteger;
11 import java.util.List;
12 import org.opendaylight.openflowplugin.api.OFConstants;
13 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
14 import org.opendaylight.openflowplugin.api.openflow.device.MessageTranslator;
15 import org.opendaylight.openflowplugin.api.openflow.md.util.OpenflowVersion;
16 import org.opendaylight.openflowplugin.extension.api.AugmentTuple;
17 import org.opendaylight.openflowplugin.extension.api.path.MatchPath;
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.openflow.md.util.InventoryDataServiceUtil;
21 import org.opendaylight.openflowplugin.openflow.md.util.PacketInUtil;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowCookie;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.Match;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntry;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.InPortCase;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessage;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketReceived;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketReceivedBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.packet.received.MatchBuilder;
32 * Created by tkubas on 4/1/15.
34 public class PacketReceivedTranslator implements MessageTranslator<PacketInMessage, PacketReceived> {
36 public PacketReceived translate(final PacketInMessage input, final DeviceContext deviceContext, final Object connectionDistinguisher) {
38 PacketReceivedBuilder packetReceivedBuilder = new PacketReceivedBuilder();
39 BigInteger datapathId = deviceContext.getPrimaryConnectionContext().getFeatures().getDatapathId();
41 // extract the port number
43 if (input.getVersion() == OFConstants.OFP_VERSION_1_0 && input.getInPort() != null) {
44 port = input.getInPort().longValue();
45 } else if (input.getVersion() == OFConstants.OFP_VERSION_1_3) {
46 if (input.getMatch() != null && input.getMatch().getMatchEntry() != null) {
47 port = getPortNumberFromMatch(input.getMatch().getMatchEntry());
51 //TODO connection cookie from connection distinguisher
52 // packetReceivedBuilder.setConnectionCookie(new ConnectionCookie(input.getCookie().longValue()));
53 packetReceivedBuilder.setPayload(input.getData());
54 // get the Cookie if it exists
55 if (input.getCookie() != null) {
56 packetReceivedBuilder.setFlowCookie(new FlowCookie(input.getCookie()));
59 packetReceivedBuilder.setIngress(InventoryDataServiceUtil.nodeConnectorRefFromDatapathIdPortno(
60 datapathId, port, OpenflowVersion.get(input.getVersion()), deviceContext.getDeviceState().getNodeInstanceIdentifier()));
62 packetReceivedBuilder.setPacketInReason(PacketInUtil.getMdSalPacketInReason(input.getReason()));
64 if (input.getTableId() != null) {
65 packetReceivedBuilder.setTableId(new org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.TableId(input.getTableId().getValue().shortValue()));
68 if (input.getMatch() != null) {
69 org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.packet.received.Match packetInMatch = getPacketInMatch(input, datapathId);
70 packetReceivedBuilder.setMatch(packetInMatch);
73 return packetReceivedBuilder.build();
76 private static org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.packet.received.Match getPacketInMatch(final PacketInMessage input, final BigInteger datapathId) {
77 Match match = MatchConvertorImpl.fromOFMatchToSALMatch(input.getMatch(),
79 OpenflowVersion.get(input.getVersion().shortValue())).build();
80 MatchBuilder matchBuilder = new MatchBuilder(match);
82 AugmentTuple<org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.packet.received.Match> matchExtensionWrap =
83 MatchExtensionHelper.processAllExtensions(
84 input.getMatch().getMatchEntry(), OpenflowVersion.get(input.getVersion().shortValue()), MatchPath.PACKETRECEIVED_MATCH);
85 if (matchExtensionWrap != null) {
86 matchBuilder.addAugmentation(matchExtensionWrap.getAugmentationClass(), matchExtensionWrap.getAugmentationObject());
88 return matchBuilder.build();
91 private static Long getPortNumberFromMatch(final List<MatchEntry> entries) {
93 for (MatchEntry entry : entries) {
94 if (InPortCase.class.equals(entry.getMatchEntryValue().getImplementedInterface())) {
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();
98 port = inPort.getPortNumber().getValue();