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
9 package org.opendaylight.openflowplugin.impl.translator;
11 import com.google.common.annotations.VisibleForTesting;
12 import java.math.BigInteger;
13 import java.util.Optional;
14 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
15 import org.opendaylight.openflowplugin.api.openflow.device.MessageTranslator;
16 import org.opendaylight.openflowplugin.api.openflow.md.util.OpenflowVersion;
17 import org.opendaylight.openflowplugin.extension.api.AugmentTuple;
18 import org.opendaylight.openflowplugin.extension.api.path.MatchPath;
19 import org.opendaylight.openflowplugin.impl.util.NodeConnectorRefToPortTranslator;
20 import org.opendaylight.openflowplugin.impl.util.PacketInUtil;
21 import org.opendaylight.openflowplugin.openflow.md.core.extension.MatchExtensionHelper;
22 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorExecutor;
23 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.data.VersionDatapathIdConvertorData;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowCookie;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRef;
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;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.TableId;
32 public class PacketReceivedTranslator implements MessageTranslator<PacketInMessage, PacketReceived> {
33 private final ConvertorExecutor convertorExecutor;
35 public PacketReceivedTranslator(ConvertorExecutor convertorExecutor) {
36 this.convertorExecutor = convertorExecutor;
40 public PacketReceived translate(final PacketInMessage input, final DeviceInfo deviceInfo,
41 final Object connectionDistinguisher) {
43 PacketReceivedBuilder packetReceivedBuilder = new PacketReceivedBuilder();
44 BigInteger datapathId = deviceInfo.getDatapathId();
46 // TODO: connection cookie from connection distinguisher
47 packetReceivedBuilder.setPayload(input.getData());
49 // get the Cookie if it exists
50 if (input.getCookie() != null) {
51 packetReceivedBuilder.setFlowCookie(new FlowCookie(input.getCookie()));
54 // Try to create the NodeConnectorRef
55 BigInteger dataPathId = deviceInfo.getDatapathId();
56 NodeConnectorRef nodeConnectorRef = NodeConnectorRefToPortTranslator.toNodeConnectorRef(input, dataPathId);
58 // If we was able to create NodeConnectorRef, use it
59 if (nodeConnectorRef != null) {
60 packetReceivedBuilder.setIngress(nodeConnectorRef);
63 packetReceivedBuilder.setPacketInReason(PacketInUtil.getMdSalPacketInReason(input.getReason()));
65 if (input.getTableId() != null) {
66 packetReceivedBuilder.setTableId(new 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
71 = getPacketInMatch(input, datapathId);
72 packetReceivedBuilder.setMatch(packetInMatch);
75 return packetReceivedBuilder.build();
79 org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.packet.received.Match getPacketInMatch(
80 final PacketInMessage input, final BigInteger datapathId) {
82 final VersionDatapathIdConvertorData datapathIdConvertorData = new VersionDatapathIdConvertorData(
84 datapathIdConvertorData.setDatapathId(datapathId);
86 final Optional<org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder>
87 matchOptional = convertorExecutor.convert(input.getMatch(), datapathIdConvertorData);
88 final MatchBuilder matchBuilder = matchOptional.map(matchBuilder1 -> new MatchBuilder(matchBuilder1.build()))
89 .orElseGet(MatchBuilder::new);
91 final AugmentTuple<org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.packet.received.Match>
92 matchExtensionWrap = MatchExtensionHelper
93 .processAllExtensions(input.getMatch().getMatchEntry(), OpenflowVersion.get(input.getVersion()),
94 MatchPath.PACKETRECEIVED_MATCH);
96 if (matchExtensionWrap != null) {
97 matchBuilder.addAugmentation(matchExtensionWrap.getAugmentationClass(),
98 matchExtensionWrap.getAugmentationObject());
101 return matchBuilder.build();