sal role service and role injection
[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
9 package org.opendaylight.openflowplugin.impl.translator;
10
11 import com.google.common.annotations.VisibleForTesting;
12 import java.math.BigInteger;
13 import java.util.List;
14 import org.opendaylight.openflowplugin.api.OFConstants;
15 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
16 import org.opendaylight.openflowplugin.api.openflow.device.MessageTranslator;
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.match.MatchConvertorImpl;
22 import org.opendaylight.openflowplugin.openflow.md.util.InventoryDataServiceUtil;
23 import org.opendaylight.openflowplugin.openflow.md.util.PacketInUtil;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowCookie;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRef;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.Match;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntry;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.InPortCase;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessage;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketReceived;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketReceivedBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.packet.received.MatchBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.TableId;
34
35 /**
36  * Created by tkubas on 4/1/15.
37  */
38 public class PacketReceivedTranslator implements MessageTranslator<PacketInMessage, PacketReceived> {
39     @Override
40     public PacketReceived translate(final PacketInMessage input, final DeviceContext deviceContext, final Object connectionDistinguisher) {
41
42         PacketReceivedBuilder packetReceivedBuilder = new PacketReceivedBuilder();
43         BigInteger datapathId = deviceContext.getPrimaryConnectionContext().getFeatures().getDatapathId();
44
45         // extract the port number
46         Long port = null;
47         if (input.getVersion() == OFConstants.OFP_VERSION_1_0 && input.getInPort() != null) {
48             port = input.getInPort().longValue();
49         } else if (input.getVersion() == OFConstants.OFP_VERSION_1_3) {
50             if (input.getMatch() != null && input.getMatch().getMatchEntry() != null) {
51                 port = getPortNumberFromMatch(input.getMatch().getMatchEntry());
52             }
53         }
54
55         //TODO connection cookie from connection distinguisher
56 //        packetReceivedBuilder.setConnectionCookie(new ConnectionCookie(input.getCookie().longValue()));
57         packetReceivedBuilder.setPayload(input.getData());
58         // get the Cookie if it exists
59         if (input.getCookie() != null) {
60             packetReceivedBuilder.setFlowCookie(new FlowCookie(input.getCookie()));
61         }
62         if (port != null) {
63             NodeConnectorRef nodeConnectorRef = deviceContext.lookupNodeConnectorRef(port);
64             if (nodeConnectorRef == null) {
65                 nodeConnectorRef = InventoryDataServiceUtil.nodeConnectorRefFromDatapathIdPortno(
66                         datapathId, port, OpenflowVersion.get(input.getVersion()), deviceContext.getDeviceState().getNodeInstanceIdentifier());
67                 deviceContext.storeNodeConnectorRef(port, nodeConnectorRef);
68             }
69             packetReceivedBuilder.setIngress(nodeConnectorRef);
70         }
71
72         packetReceivedBuilder.setPacketInReason(PacketInUtil.getMdSalPacketInReason(input.getReason()));
73
74         if (input.getTableId() != null) {
75             packetReceivedBuilder.setTableId(new TableId(input.getTableId().getValue().shortValue()));
76         }
77
78         if (input.getMatch() != null) {
79             org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.packet.received.Match packetInMatch = getPacketInMatch(input, datapathId);
80             packetReceivedBuilder.setMatch(packetInMatch);
81         }
82
83         return packetReceivedBuilder.build();
84     }
85
86     @VisibleForTesting
87     static org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.packet.received.Match getPacketInMatch(final PacketInMessage input, final BigInteger datapathId) {
88         Match match = MatchConvertorImpl.fromOFMatchToSALMatch(input.getMatch(),
89                 datapathId,
90                 OpenflowVersion.get(input.getVersion().shortValue())).build();
91         MatchBuilder matchBuilder = new MatchBuilder(match);
92
93         AugmentTuple<org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.packet.received.Match> matchExtensionWrap =
94                 MatchExtensionHelper.processAllExtensions(
95                         input.getMatch().getMatchEntry(), OpenflowVersion.get(input.getVersion().shortValue()), MatchPath.PACKETRECEIVED_MATCH);
96         if (matchExtensionWrap != null) {
97             matchBuilder.addAugmentation(matchExtensionWrap.getAugmentationClass(), matchExtensionWrap.getAugmentationObject());
98         }
99         return matchBuilder.build();
100     }
101
102     @VisibleForTesting
103     static Long getPortNumberFromMatch(final List<MatchEntry> entries) {
104         Long port = null;
105         for (MatchEntry entry : entries) {
106             if (InPortCase.class.equals(entry.getMatchEntryValue().getImplementedInterface())) {
107                 InPortCase inPortCase = ((InPortCase) entry.getMatchEntryValue());
108                 org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.in.port._case.InPort inPort = inPortCase.getInPort();
109                 if (inPort != null) {
110                     port = inPort.getPortNumber().getValue();
111                     break;
112                 }
113             }
114         }
115         return port;
116     }
117 }