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 com.google.common.annotations.VisibleForTesting;
11 import java.util.Optional;
12 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
13 import org.opendaylight.openflowplugin.api.openflow.device.MessageTranslator;
14 import org.opendaylight.openflowplugin.api.openflow.md.util.OpenflowVersion;
15 import org.opendaylight.openflowplugin.extension.api.AugmentTuple;
16 import org.opendaylight.openflowplugin.extension.api.path.MatchPath;
17 import org.opendaylight.openflowplugin.impl.util.NodeConnectorRefToPortTranslator;
18 import org.opendaylight.openflowplugin.impl.util.PacketInUtil;
19 import org.opendaylight.openflowplugin.openflow.md.core.extension.MatchExtensionHelper;
20 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorExecutor;
21 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.data.VersionDatapathIdConvertorData;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowCookie;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRef;
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.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;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.TableId;
29 import org.opendaylight.yangtools.yang.common.Uint64;
31 public class PacketReceivedTranslator implements MessageTranslator<PacketInMessage, PacketReceived> {
32 private final ConvertorExecutor convertorExecutor;
34 public PacketReceivedTranslator(final ConvertorExecutor convertorExecutor) {
35 this.convertorExecutor = convertorExecutor;
39 public PacketReceived translate(final PacketInMessage input, final DeviceInfo deviceInfo,
40 final Object connectionDistinguisher) {
42 PacketReceivedBuilder packetReceivedBuilder = new PacketReceivedBuilder();
43 Uint64 datapathId = deviceInfo.getDatapathId();
45 // TODO: connection cookie from connection distinguisher
46 packetReceivedBuilder.setPayload(input.getData());
48 // get the Cookie if it exists
49 if (input.getCookie() != null) {
50 packetReceivedBuilder.setFlowCookie(new FlowCookie(input.getCookie()));
53 // Try to create the NodeConnectorRef
54 Uint64 dataPathId = deviceInfo.getDatapathId();
55 NodeConnectorRef nodeConnectorRef = NodeConnectorRefToPortTranslator.toNodeConnectorRef(input, dataPathId);
57 // If we was able to create NodeConnectorRef, use it
58 if (nodeConnectorRef != null) {
59 packetReceivedBuilder.setIngress(nodeConnectorRef);
62 packetReceivedBuilder.setPacketInReason(PacketInUtil.getMdSalPacketInReason(input.getReason()));
64 if (input.getTableId() != null) {
65 packetReceivedBuilder.setTableId(new 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
70 = getPacketInMatch(input, datapathId);
71 packetReceivedBuilder.setMatch(packetInMatch);
74 return packetReceivedBuilder.build();
78 org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.packet.received.Match getPacketInMatch(
79 final PacketInMessage input, final Uint64 datapathId) {
81 final VersionDatapathIdConvertorData datapathIdConvertorData = new VersionDatapathIdConvertorData(
82 input.getVersion().toJava());
83 datapathIdConvertorData.setDatapathId(datapathId);
85 final Optional<org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder>
86 matchOptional = convertorExecutor.convert(input.getMatch(), datapathIdConvertorData);
87 final MatchBuilder matchBuilder = matchOptional.map(matchBuilder1 -> new MatchBuilder(matchBuilder1.build()))
88 .orElseGet(MatchBuilder::new);
90 final AugmentTuple<org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.packet.received.Match>
91 matchExtensionWrap = MatchExtensionHelper
92 .processAllExtensions(input.getMatch().getMatchEntry(), OpenflowVersion.get(input.getVersion()),
93 MatchPath.PACKET_RECEIVED_MATCH);
95 if (matchExtensionWrap != null) {
96 matchBuilder.addAugmentation(matchExtensionWrap.getAugmentationClass(),
97 matchExtensionWrap.getAugmentationObject());
100 return matchBuilder.build();