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