Update MRI projects for Aluminium
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / translator / PacketReceivedTranslator.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.openflowplugin.impl.translator;
9
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;
30
31 public class PacketReceivedTranslator implements MessageTranslator<PacketInMessage, PacketReceived> {
32     private final ConvertorExecutor convertorExecutor;
33
34     public PacketReceivedTranslator(final ConvertorExecutor convertorExecutor) {
35         this.convertorExecutor = convertorExecutor;
36     }
37
38     @Override
39     public PacketReceived translate(final PacketInMessage input, final DeviceInfo deviceInfo,
40                                     final Object connectionDistinguisher) {
41
42         PacketReceivedBuilder packetReceivedBuilder = new PacketReceivedBuilder();
43         Uint64 datapathId = deviceInfo.getDatapathId();
44
45         // TODO: connection cookie from connection distinguisher
46         packetReceivedBuilder.setPayload(input.getData());
47
48         // get the Cookie if it exists
49         if (input.getCookie() != null) {
50             packetReceivedBuilder.setFlowCookie(new FlowCookie(input.getCookie()));
51         }
52
53         // Try to create the NodeConnectorRef
54         Uint64 dataPathId = deviceInfo.getDatapathId();
55         NodeConnectorRef nodeConnectorRef = NodeConnectorRefToPortTranslator.toNodeConnectorRef(input, dataPathId);
56
57         // If we was able to create NodeConnectorRef, use it
58         if (nodeConnectorRef != null) {
59             packetReceivedBuilder.setIngress(nodeConnectorRef);
60         }
61
62         packetReceivedBuilder.setPacketInReason(PacketInUtil.getMdSalPacketInReason(input.getReason()));
63
64         if (input.getTableId() != null) {
65             packetReceivedBuilder.setTableId(new TableId(input.getTableId().getValue().shortValue()));
66         }
67
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);
72         }
73
74         return packetReceivedBuilder.build();
75     }
76
77     @VisibleForTesting
78     org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.packet.received.Match getPacketInMatch(
79             final PacketInMessage input, final Uint64 datapathId) {
80
81         final VersionDatapathIdConvertorData datapathIdConvertorData = new VersionDatapathIdConvertorData(
82                 input.getVersion().toJava());
83         datapathIdConvertorData.setDatapathId(datapathId);
84
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);
89
90         final AugmentTuple<org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.packet.received.Match>
91                 matchExtensionWrap = MatchExtensionHelper.processAllExtensions(input.getMatch().nonnullMatchEntry(),
92                     OpenflowVersion.get(input.getVersion()), MatchPath.PACKET_RECEIVED_MATCH);
93
94         if (matchExtensionWrap != null) {
95             matchBuilder.addAugmentation(matchExtensionWrap.getAugmentationClass(),
96                                          matchExtensionWrap.getAugmentationObject());
97         }
98
99         return matchBuilder.build();
100     }
101 }