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