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.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.PacketReceived;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketReceivedBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.packet.received.MatchBuilder;
29 * Created by tkubas on 4/1/15.
31 public class PacketReceivedTranslator implements MessageTranslator<PacketInMessage, PacketReceived> {
33 public PacketReceived translate(PacketInMessage input, DeviceContext deviceContext, Object connectionDistinguisher) {
35 PacketReceivedBuilder packetReceivedBuilder = new PacketReceivedBuilder();
36 BigInteger datapathId = deviceContext.getPrimaryConnectionContext().getFeatures().getDatapathId();
38 // extract the port number
40 if (input.getVersion() == OFConstants.OFP_VERSION_1_0 && input.getInPort() != null) {
41 port = input.getInPort().longValue();
42 } else if (input.getVersion() == OFConstants.OFP_VERSION_1_3) {
43 if (input.getMatch() != null && input.getMatch().getMatchEntry() != null) {
44 port = getPortNumberFromMatch(input.getMatch().getMatchEntry());
48 //TODO connection cookie from connection distinguisher
49 // packetReceivedBuilder.setConnectionCookie(new ConnectionCookie(input.getCookie().longValue()));
50 packetReceivedBuilder.setPayload(input.getData());
51 // get the Cookie if it exists
52 if (input.getCookie() != null) {
53 packetReceivedBuilder.setFlowCookie(new FlowCookie(input.getCookie()));
56 packetReceivedBuilder.setIngress(InventoryDataServiceUtil.nodeConnectorRefFromDatapathIdPortno(datapathId, port, OpenflowVersion.get(input.getVersion())));
58 packetReceivedBuilder.setPacketInReason(PacketInUtil.getMdSalPacketInReason(input.getReason()));
60 if (input.getTableId() != null) {
61 packetReceivedBuilder.setTableId(new org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.TableId(input.getTableId().getValue().shortValue()));
64 if (input.getMatch() != null) {
65 org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.packet.received.Match packetInMatch = getPacketInMatch(input, datapathId);
66 packetReceivedBuilder.setMatch(packetInMatch);
69 return packetReceivedBuilder.build();
72 private org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.packet.received.Match getPacketInMatch(PacketInMessage input, BigInteger datapathId) {
73 Match match = MatchConvertorImpl.fromOFMatchToSALMatch(input.getMatch(),
75 OpenflowVersion.get(input.getVersion().shortValue())).build();
76 MatchBuilder matchBuilder = new MatchBuilder(match);
78 // FIXME: need to solve extension translator wiring and initializing
79 // AugmentTuple<org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.packet.received.Match> matchExtensionWrap =
80 // MatchExtensionHelper.processAllExtensions(
81 // input.getMatch().getMatchEntry(), OpenflowVersion.get(input.getVersion().shortValue()), MatchPath.PACKETRECEIVED_MATCH);
82 // if (matchExtensionWrap != null) {
83 // matchBuilder.addAugmentation(matchExtensionWrap.getAugmentationClass(), matchExtensionWrap.getAugmentationObject());
85 return matchBuilder.build();
88 private Long getPortNumberFromMatch(List<MatchEntry> entries) {
90 for (MatchEntry entry : entries) {
91 if (InPortCase.class.equals(entry.getMatchEntryValue().getImplementedInterface())) {
92 InPortCase inPortCase = ((InPortCase) entry.getMatchEntryValue());
93 org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.in.port._case.InPort inPort = inPortCase.getInPort();
95 port = inPort.getPortNumber().getValue();