Handling async replies from device in DeviceCtx
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / device / 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.device.translator;
9
10 import org.opendaylight.openflowplugin.api.OFConstants;
11 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
12 import org.opendaylight.openflowplugin.api.openflow.device.MessageTranslator;
13 import org.opendaylight.openflowplugin.api.openflow.md.util.OpenflowVersion;
14 import org.opendaylight.openflowplugin.extension.api.AugmentTuple;
15 import org.opendaylight.openflowplugin.extension.api.path.MatchPath;
16 import org.opendaylight.openflowplugin.openflow.md.core.extension.MatchExtensionHelper;
17 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.match.MatchConvertorImpl;
18 import org.opendaylight.openflowplugin.openflow.md.util.InventoryDataServiceUtil;
19 import org.opendaylight.openflowplugin.openflow.md.util.PacketInUtil;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowCookie;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.Match;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntry;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.InPortCase;
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.ConnectionCookie;
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
30 import java.math.BigInteger;
31 import java.util.List;
32
33 /**
34  * Created by tkubas on 4/1/15.
35  */
36 public class PacketReceivedTranslator implements MessageTranslator<PacketInMessage, PacketReceived> {
37     @Override
38     public PacketReceived translate(PacketInMessage input, DeviceContext deviceContext, Object connectionDistinguisher) {
39
40         PacketReceivedBuilder packetReceivedBuilder = new PacketReceivedBuilder();
41         BigInteger datapathId = deviceContext.getPrimaryConnectionContext().getFeatures().getDatapathId();
42
43         // extract the port number
44         Long port = null;
45         if(input.getVersion() == OFConstants.OFP_VERSION_1_0 && input.getInPort() != null) {
46             port = input.getInPort().longValue();
47         } else if (input.getVersion() == OFConstants.OFP_VERSION_1_3) {
48             if (input.getMatch() != null && input.getMatch().getMatchEntry() != null) {
49                 port = getPortNumberFromMatch(input.getMatch().getMatchEntry());
50             }
51         }
52
53         //TODO connection cookie from connection distinguisher
54 //        packetReceivedBuilder.setConnectionCookie(new ConnectionCookie(input.getCookie().longValue()));
55         packetReceivedBuilder.setPayload(input.getData());
56         // get the Cookie if it exists
57         if (input.getCookie() != null) {
58             packetReceivedBuilder.setFlowCookie(new FlowCookie(input.getCookie()));
59         }
60         if(port != null) {
61             packetReceivedBuilder.setIngress(InventoryDataServiceUtil.nodeConnectorRefFromDatapathIdPortno(datapathId, port, OpenflowVersion.get(input.getVersion())));
62         }
63         packetReceivedBuilder.setPacketInReason(PacketInUtil.getMdSalPacketInReason(input.getReason()));
64
65         if(input.getTableId() != null) {
66             packetReceivedBuilder.setTableId(new org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.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     private org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.packet.received.Match getPacketInMatch(PacketInMessage input, BigInteger datapathId) {
78         Match match = MatchConvertorImpl.fromOFMatchToSALMatch(input.getMatch(),
79                 datapathId,
80                 OpenflowVersion.get(input.getVersion().shortValue())).build();
81         MatchBuilder matchBuilder = new MatchBuilder(match);
82
83         AugmentTuple<org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.packet.received.Match> matchExtensionWrap =
84                 MatchExtensionHelper.processAllExtensions(
85                         input.getMatch().getMatchEntry(), OpenflowVersion.get(input.getVersion().shortValue()), MatchPath.PACKETRECEIVED_MATCH);
86         if (matchExtensionWrap != null) {
87             matchBuilder.addAugmentation(matchExtensionWrap.getAugmentationClass(), matchExtensionWrap.getAugmentationObject());
88         }
89         return matchBuilder.build();
90     }
91
92     private Long getPortNumberFromMatch(List<MatchEntry> entries) {
93         Long port = null;
94         for (MatchEntry entry : entries) {
95             InPortCase inPortCase = ((InPortCase) entry.getMatchEntryValue());
96             org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.in.port._case.InPort inPort = inPortCase.getInPort();
97             if (inPort != null) {
98
99                 if (port == null) {
100                     port = inPort.getPortNumber().getValue();
101                 } else {
102 //                        LOG.warn("Multiple input ports discovered when walking through match entries (at least {} and {})",
103 //                                port, inPort.getPortNumber().getValue());
104                 }
105             }
106         }
107         return port;
108     }
109 }