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