BUG-2794: incorporate openflowjava api changes to openflowplugin
[openflowplugin.git] / openflowplugin / src / main / java / org / opendaylight / openflowplugin / openflow / md / core / translator / PacketInTranslator.java
1 /**
2  * Copyright (c) 2013 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.openflow.md.core.translator;
9
10 import org.opendaylight.openflowplugin.api.openflow.md.core.IMDMessageTranslator;
11 import org.opendaylight.openflowplugin.api.openflow.md.core.SwitchConnectionDistinguisher;
12 import org.opendaylight.openflowplugin.api.openflow.md.core.session.SessionContext;
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.GetFeaturesOutput;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
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.ConnectionCookie;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketReceived;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketReceivedBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.packet.received.MatchBuilder;
31 import org.opendaylight.yangtools.yang.binding.DataObject;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 import java.math.BigInteger;
35 import java.util.Collections;
36 import java.util.List;
37
38 /**
39  * translates packetIn from OF-API model to MD-SAL model, supports OF-1.3
40  */
41 public class PacketInTranslator implements IMDMessageTranslator<OfHeader, List<DataObject>> {
42
43     protected static final Logger LOG = LoggerFactory
44             .getLogger(PacketInTranslator.class);
45
46     @Override
47     public List<DataObject> translate(final SwitchConnectionDistinguisher cookie,
48                                       final SessionContext sc, final OfHeader msg) {
49
50         List<DataObject> salPacketIn = Collections.emptyList();
51
52         if (sc != null && msg instanceof PacketInMessage) {
53             PacketInMessage message = (PacketInMessage) msg;
54             LOG.trace("PacketIn[v{}]: Cookie: {} Match.type: {}",
55                     message.getVersion(), message.getCookie(),
56                     message.getMatch() != null ? message.getMatch().getType() : message.getMatch());
57
58             // create a packet received event builder
59             PacketReceivedBuilder pktInBuilder = new PacketReceivedBuilder();
60             pktInBuilder.setPayload(message.getData());
61             if (cookie != null) {
62                 pktInBuilder.setConnectionCookie(new ConnectionCookie(cookie.getCookie()));
63             }
64
65             // get the DPID
66             GetFeaturesOutput features = sc.getFeatures();
67             // Make sure we actually have features, some naughty switches start sending packetIn before they send us the FeatureReply
68             if (features == null) {
69                 LOG.warn("Received packet_in, but there is no device datapathId received yet");
70             } else {
71                 BigInteger dpid = features.getDatapathId();
72
73                 // get the Cookie if it exists
74                 if (message.getCookie() != null) {
75                     pktInBuilder.setFlowCookie(new FlowCookie(message.getCookie()));
76                 }
77
78                 // extract the port number
79                 Long port = null;
80                 if (message.getMatch() != null && message.getMatch().getMatchEntry() != null) {
81                     List<MatchEntry> entries = message.getMatch().getMatchEntry();
82                     for (MatchEntry entry : entries) {
83                         InPortCase inPortCase = ((InPortCase) entry.getMatchEntryValue());
84                         org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.in.port._case.InPort inPort = inPortCase.getInPort();
85                         if (inPort != null) {
86                             if (port == null) {
87                                 port = inPort.getPortNumber().getValue();
88                             } else {
89                                 LOG.warn("Multiple input ports discovered when walking through match entries (at least {} and {})",
90                                         port, inPort.getPortNumber().getValue());
91                             }
92                         }
93                     }
94                 }
95
96                 if (port == null) {
97                     // no incoming port, so drop the event
98                     LOG.warn("Received packet_in, but couldn't find an input port");
99                 } else {
100                     LOG.trace("Received packet_in from {} on port {}", dpid, port);
101
102                     OpenflowVersion ofVersion = OpenflowVersion.get(sc.getPrimaryConductor().getVersion());
103                     Match match = MatchConvertorImpl.fromOFMatchToSALMatch(message.getMatch(), dpid, ofVersion).build();
104                     MatchBuilder matchBuilder = new MatchBuilder(match);
105
106                     AugmentTuple<org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.packet.received.Match> matchExtensionWrap =
107                             MatchExtensionHelper.processAllExtensions(
108                                     message.getMatch().getMatchEntry(), ofVersion, MatchPath.PACKETRECEIVED_MATCH);
109                     if (matchExtensionWrap != null) {
110                         matchBuilder.addAugmentation(matchExtensionWrap.getAugmentationClass(), matchExtensionWrap.getAugmentationObject());
111                     }
112
113                     org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.packet.received.Match packetInMatch = matchBuilder.build();
114                     pktInBuilder.setMatch(packetInMatch);
115                     pktInBuilder.setPacketInReason(PacketInUtil.getMdSalPacketInReason(message.getReason()));
116                     pktInBuilder.setTableId(new org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.TableId(message.getTableId().getValue().shortValue()));
117                     pktInBuilder.setIngress(InventoryDataServiceUtil.nodeConnectorRefFromDatapathIdPortno(dpid, port, ofVersion));
118                     PacketReceived pktInEvent = pktInBuilder.build();
119                     salPacketIn = Collections.<DataObject>singletonList(pktInEvent);
120                 }
121             }
122         }
123         return salPacketIn;
124     }
125 }