Update MRI projects for Aluminium
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / translator / PacketReceivedTranslator.java
index 991a855c9dc7c62e8e0943f7d5d503e5fb4909cb..9fee75d23e8aef990634f4da049c2d41310b0d91 100644 (file)
@@ -1,4 +1,4 @@
-/**
+/*
  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
  *
  * This program and the accompanying materials are made available under the
  */
 package org.opendaylight.openflowplugin.impl.translator;
 
-import org.opendaylight.openflowplugin.api.OFConstants;
-import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
+import com.google.common.annotations.VisibleForTesting;
+import java.util.Optional;
+import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
 import org.opendaylight.openflowplugin.api.openflow.device.MessageTranslator;
 import org.opendaylight.openflowplugin.api.openflow.md.util.OpenflowVersion;
 import org.opendaylight.openflowplugin.extension.api.AugmentTuple;
 import org.opendaylight.openflowplugin.extension.api.path.MatchPath;
+import org.opendaylight.openflowplugin.impl.util.NodeConnectorRefToPortTranslator;
+import org.opendaylight.openflowplugin.impl.util.PacketInUtil;
 import org.opendaylight.openflowplugin.openflow.md.core.extension.MatchExtensionHelper;
-import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.match.MatchConvertorImpl;
-import org.opendaylight.openflowplugin.openflow.md.util.InventoryDataServiceUtil;
-import org.opendaylight.openflowplugin.openflow.md.util.PacketInUtil;
+import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorExecutor;
+import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.data.VersionDatapathIdConvertorData;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowCookie;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.Match;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntry;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.InPortCase;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRef;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessage;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketReceived;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketReceivedBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.packet.received.MatchBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.TableId;
+import org.opendaylight.yangtools.yang.common.Uint64;
 
-import java.math.BigInteger;
-import java.util.List;
-
-/**
- * Created by tkubas on 4/1/15.
- */
 public class PacketReceivedTranslator implements MessageTranslator<PacketInMessage, PacketReceived> {
+    private final ConvertorExecutor convertorExecutor;
+
+    public PacketReceivedTranslator(final ConvertorExecutor convertorExecutor) {
+        this.convertorExecutor = convertorExecutor;
+    }
+
     @Override
-    public PacketReceived translate(PacketInMessage input, DeviceContext deviceContext, Object connectionDistinguisher) {
+    public PacketReceived translate(final PacketInMessage input, final DeviceInfo deviceInfo,
+                                    final Object connectionDistinguisher) {
 
         PacketReceivedBuilder packetReceivedBuilder = new PacketReceivedBuilder();
-        BigInteger datapathId = deviceContext.getPrimaryConnectionContext().getFeatures().getDatapathId();
-
-        // extract the port number
-        Long port = null;
-        if(input.getVersion() == OFConstants.OFP_VERSION_1_0 && input.getInPort() != null) {
-            port = input.getInPort().longValue();
-        } else if (input.getVersion() == OFConstants.OFP_VERSION_1_3) {
-            if (input.getMatch() != null && input.getMatch().getMatchEntry() != null) {
-                port = getPortNumberFromMatch(input.getMatch().getMatchEntry());
-            }
-        }
+        Uint64 datapathId = deviceInfo.getDatapathId();
 
-        //TODO connection cookie from connection distinguisher
-//        packetReceivedBuilder.setConnectionCookie(new ConnectionCookie(input.getCookie().longValue()));
+        // TODO: connection cookie from connection distinguisher
         packetReceivedBuilder.setPayload(input.getData());
+
         // get the Cookie if it exists
         if (input.getCookie() != null) {
             packetReceivedBuilder.setFlowCookie(new FlowCookie(input.getCookie()));
         }
-        if(port != null) {
-            packetReceivedBuilder.setIngress(InventoryDataServiceUtil.nodeConnectorRefFromDatapathIdPortno(datapathId, port, OpenflowVersion.get(input.getVersion())));
+
+        // Try to create the NodeConnectorRef
+        Uint64 dataPathId = deviceInfo.getDatapathId();
+        NodeConnectorRef nodeConnectorRef = NodeConnectorRefToPortTranslator.toNodeConnectorRef(input, dataPathId);
+
+        // If we was able to create NodeConnectorRef, use it
+        if (nodeConnectorRef != null) {
+            packetReceivedBuilder.setIngress(nodeConnectorRef);
         }
+
         packetReceivedBuilder.setPacketInReason(PacketInUtil.getMdSalPacketInReason(input.getReason()));
 
-        if(input.getTableId() != null) {
-            packetReceivedBuilder.setTableId(new org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.TableId(input.getTableId().getValue().shortValue()));
+        if (input.getTableId() != null) {
+            packetReceivedBuilder.setTableId(new TableId(input.getTableId().getValue().shortValue()));
         }
 
-        if(input.getMatch() != null) {
-            org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.packet.received.Match packetInMatch = getPacketInMatch(input, datapathId);
+        if (input.getMatch() != null) {
+            org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.packet.received.Match packetInMatch
+                    = getPacketInMatch(input, datapathId);
             packetReceivedBuilder.setMatch(packetInMatch);
         }
 
         return packetReceivedBuilder.build();
     }
 
-    private org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.packet.received.Match getPacketInMatch(PacketInMessage input, BigInteger datapathId) {
-        Match match = MatchConvertorImpl.fromOFMatchToSALMatch(input.getMatch(),
-                datapathId,
-                OpenflowVersion.get(input.getVersion().shortValue())).build();
-        MatchBuilder matchBuilder = new MatchBuilder(match);
+    @VisibleForTesting
+    org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.packet.received.Match getPacketInMatch(
+            final PacketInMessage input, final Uint64 datapathId) {
+
+        final VersionDatapathIdConvertorData datapathIdConvertorData = new VersionDatapathIdConvertorData(
+                input.getVersion().toJava());
+        datapathIdConvertorData.setDatapathId(datapathId);
+
+        final Optional<org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder>
+                matchOptional = convertorExecutor.convert(input.getMatch(), datapathIdConvertorData);
+        final MatchBuilder matchBuilder = matchOptional.map(matchBuilder1 -> new MatchBuilder(matchBuilder1.build()))
+                .orElseGet(MatchBuilder::new);
+
+        final AugmentTuple<org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.packet.received.Match>
+                matchExtensionWrap = MatchExtensionHelper.processAllExtensions(input.getMatch().nonnullMatchEntry(),
+                    OpenflowVersion.get(input.getVersion()), MatchPath.PACKET_RECEIVED_MATCH);
 
-        AugmentTuple<org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.packet.received.Match> matchExtensionWrap =
-                MatchExtensionHelper.processAllExtensions(
-                        input.getMatch().getMatchEntry(), OpenflowVersion.get(input.getVersion().shortValue()), MatchPath.PACKETRECEIVED_MATCH);
         if (matchExtensionWrap != null) {
-            matchBuilder.addAugmentation(matchExtensionWrap.getAugmentationClass(), matchExtensionWrap.getAugmentationObject());
+            matchBuilder.addAugmentation(matchExtensionWrap.getAugmentationClass(),
+                                         matchExtensionWrap.getAugmentationObject());
         }
-        return matchBuilder.build();
-    }
 
-    private Long getPortNumberFromMatch(List<MatchEntry> entries) {
-        Long port = null;
-        for (MatchEntry entry : entries) {
-            InPortCase inPortCase = ((InPortCase) entry.getMatchEntryValue());
-            org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.in.port._case.InPort inPort = inPortCase.getInPort();
-            if (inPort != null) {
-
-                if (port == null) {
-                    port = inPort.getPortNumber().getValue();
-                } else {
-//                        LOG.warn("Multiple input ports discovered when walking through match entries (at least {} and {})",
-//                                port, inPort.getPortNumber().getValue());
-                }
-            }
-        }
-        return port;
+        return matchBuilder.build();
     }
 }