Merge "Bug 5589 - Deprecate PortNumberCache"
[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
9 package org.opendaylight.openflowplugin.impl.translator;
10
11 import com.google.common.annotations.VisibleForTesting;
12 import java.math.BigInteger;
13 import java.util.List;
14 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
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.openflow.md.core.extension.MatchExtensionHelper;
21 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.match.MatchConvertorImpl;
22 import org.opendaylight.openflowplugin.openflow.md.util.PacketInUtil;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowCookie;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRef;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.Match;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntry;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.InPortCase;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessage;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketReceived;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketReceivedBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.packet.received.MatchBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.TableId;
33
34 /**
35  * Created by tkubas on 4/1/15.
36  */
37 public class PacketReceivedTranslator implements MessageTranslator<PacketInMessage, PacketReceived> {
38     @Override
39     public PacketReceived translate(final PacketInMessage input, final DeviceContext deviceContext, final Object connectionDistinguisher) {
40
41         PacketReceivedBuilder packetReceivedBuilder = new PacketReceivedBuilder();
42         BigInteger datapathId = deviceContext.getPrimaryConnectionContext().getFeatures().getDatapathId();
43
44         // TODO: connection cookie from connection distinguisher
45         // packetReceivedBuilder.setConnectionCookie(new ConnectionCookie(input.getCookie().longValue()));
46
47         packetReceivedBuilder.setPayload(input.getData());
48
49         // get the Cookie if it exists
50         if (input.getCookie() != null) {
51             packetReceivedBuilder.setFlowCookie(new FlowCookie(input.getCookie()));
52         }
53
54         // Try to create the NodeConnectorRef
55         BigInteger dataPathId = deviceContext.getDeviceState().getFeatures().getDatapathId();
56         NodeConnectorRef nodeConnectorRef = NodeConnectorRefToPortTranslator.toNodeConnectorRef(input, dataPathId);
57
58         // If we was able to create NodeConnectorRef, use it
59         if (nodeConnectorRef != null) {
60             packetReceivedBuilder.setIngress(nodeConnectorRef);
61         }
62
63         packetReceivedBuilder.setPacketInReason(PacketInUtil.getMdSalPacketInReason(input.getReason()));
64
65         if (input.getTableId() != null) {
66             packetReceivedBuilder.setTableId(new TableId(input.getTableId().getValue().shortValue()));
67         }
68
69         if (input.getMatch() != null) {
70             org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.packet.received.Match packetInMatch = getPacketInMatch(input, datapathId);
71             packetReceivedBuilder.setMatch(packetInMatch);
72         }
73
74         return packetReceivedBuilder.build();
75     }
76
77     @VisibleForTesting
78     static org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.packet.received.Match getPacketInMatch(final PacketInMessage input, final BigInteger datapathId) {
79         Match match = MatchConvertorImpl.fromOFMatchToSALMatch(input.getMatch(),
80                 datapathId,
81                 OpenflowVersion.get(input.getVersion().shortValue())).build();
82         MatchBuilder matchBuilder = new MatchBuilder(match);
83
84         AugmentTuple<org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.packet.received.Match> matchExtensionWrap =
85                 MatchExtensionHelper.processAllExtensions(
86                         input.getMatch().getMatchEntry(), OpenflowVersion.get(input.getVersion().shortValue()), MatchPath.PACKETRECEIVED_MATCH);
87         if (matchExtensionWrap != null) {
88             matchBuilder.addAugmentation(matchExtensionWrap.getAugmentationClass(), matchExtensionWrap.getAugmentationObject());
89         }
90         return matchBuilder.build();
91     }
92 }