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