defensively commented code revived
[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  * <p/>
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.extension.api.AugmentTuple;
17 import org.opendaylight.openflowplugin.extension.api.path.MatchPath;
18 import org.opendaylight.openflowplugin.openflow.md.core.extension.MatchExtensionHelper;
19 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.match.MatchConvertorImpl;
20 import org.opendaylight.openflowplugin.openflow.md.util.InventoryDataServiceUtil;
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.model.match.types.rev131026.Match;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntry;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.InPortCase;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessage;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketReceived;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketReceivedBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.packet.received.MatchBuilder;
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(PacketInMessage input, DeviceContext deviceContext, Object connectionDistinguisher) {
37
38         PacketReceivedBuilder packetReceivedBuilder = new PacketReceivedBuilder();
39         BigInteger datapathId = deviceContext.getPrimaryConnectionContext().getFeatures().getDatapathId();
40
41         // extract the port number
42         Long port = null;
43         if (input.getVersion() == OFConstants.OFP_VERSION_1_0 && input.getInPort() != null) {
44             port = input.getInPort().longValue();
45         } else if (input.getVersion() == OFConstants.OFP_VERSION_1_3) {
46             if (input.getMatch() != null && input.getMatch().getMatchEntry() != null) {
47                 port = getPortNumberFromMatch(input.getMatch().getMatchEntry());
48             }
49         }
50
51         //TODO connection cookie from connection distinguisher
52 //        packetReceivedBuilder.setConnectionCookie(new ConnectionCookie(input.getCookie().longValue()));
53         packetReceivedBuilder.setPayload(input.getData());
54         // get the Cookie if it exists
55         if (input.getCookie() != null) {
56             packetReceivedBuilder.setFlowCookie(new FlowCookie(input.getCookie()));
57         }
58         if (port != null) {
59             packetReceivedBuilder.setIngress(InventoryDataServiceUtil.nodeConnectorRefFromDatapathIdPortno(datapathId, port, OpenflowVersion.get(input.getVersion())));
60         }
61         packetReceivedBuilder.setPacketInReason(PacketInUtil.getMdSalPacketInReason(input.getReason()));
62
63         if (input.getTableId() != null) {
64             packetReceivedBuilder.setTableId(new org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.TableId(input.getTableId().getValue().shortValue()));
65         }
66
67         if (input.getMatch() != null) {
68             org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.packet.received.Match packetInMatch = getPacketInMatch(input, datapathId);
69             packetReceivedBuilder.setMatch(packetInMatch);
70         }
71
72         return packetReceivedBuilder.build();
73     }
74
75     private org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.packet.received.Match getPacketInMatch(PacketInMessage input, 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
90     private Long getPortNumberFromMatch(List<MatchEntry> entries) {
91         Long port = null;
92         for (MatchEntry entry : entries) {
93             if (InPortCase.class.equals(entry.getMatchEntryValue().getImplementedInterface())) {
94                 InPortCase inPortCase = ((InPortCase) entry.getMatchEntryValue());
95                 org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.in.port._case.InPort inPort = inPortCase.getInPort();
96                 if (inPort != null) {
97                     port = inPort.getPortNumber().getValue();
98                     break;
99                 }
100             }
101         }
102         return port;
103     }
104 }