Bug-5523 Lifecycle conductor changes
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / util / NodeConnectorRefToPortTranslator.java
index e85e930338adc79a5001a33b11775a92da5cce18..4c71ea59e7ed4510d3c9f4f1996d80539ad4170c 100644 (file)
@@ -10,12 +10,17 @@ package org.opendaylight.openflowplugin.impl.util;
 
 import com.google.common.annotations.VisibleForTesting;
 import com.google.common.base.Preconditions;
+import org.opendaylight.openflowplugin.api.OFConstants;
 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
 import org.opendaylight.openflowplugin.api.openflow.md.util.OpenflowVersion;
 import org.opendaylight.openflowplugin.openflow.md.util.InventoryDataServiceUtil;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRef;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey;
+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.in.port._case.InPort;
+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.openflow.protocol.rev130731.PacketIn;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.features.reply.PhyPort;
 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
 
@@ -30,55 +35,81 @@ import java.util.Objects;
  */
 public class NodeConnectorRefToPortTranslator {
     /**
-     * Converts {@link DeviceState} to {@link NodeConnectorRef}
-     * @param deviceState Device state to be converted
-     * @return Device state converted to node connector reference
+     * Converts {@link PacketIn} to {@link NodeConnectorRef}
+     * @param packetIn Packet input
+     * @param dataPathId Data path id
+     * @return packet input converted to node connector reference
      */
     @Nullable
-    public static NodeConnectorRef toNodeConnectorRef(@Nonnull DeviceState deviceState) {
-        Preconditions.checkNotNull(deviceState);
+    public static NodeConnectorRef toNodeConnectorRef(@Nonnull PacketIn packetIn, BigInteger dataPathId) {
+        Preconditions.checkNotNull(packetIn);
 
-        Long port = getPortNoFromDeviceState(deviceState);
-        OpenflowVersion version = OpenflowVersion.get(deviceState.getVersion());
-        BigInteger dataPathId = deviceState.getFeatures().getDatapathId();
+        NodeConnectorRef ref = null;
+        Long port = getPortNoFromPacketIn(packetIn);
 
-        return InventoryDataServiceUtil.nodeConnectorRefFromDatapathIdPortno(dataPathId, port, version);
+        if (port != null) {
+            OpenflowVersion version = OpenflowVersion.get(packetIn.getVersion());
+
+            ref = InventoryDataServiceUtil.nodeConnectorRefFromDatapathIdPortno(dataPathId, port, version);
+        }
+
+        return ref;
     }
 
     /**
-     * Gets port number from {@link NodeConnectorRef}. If it is null, it will try to get the port from
-     * {@link DeviceState}
-     * @param deviceState Device state fallback if there is any problem with node connector reference
+     * Gets port number from {@link NodeConnectorRef}.
      * @param nodeConnectorRef Node connector reference
+     * @param version Openflow version
      * @return port number
      */
     @SuppressWarnings("unchecked")
     @Nullable
-    public static Long fromNodeConnectorRef(@Nonnull DeviceState deviceState, NodeConnectorRef nodeConnectorRef) {
-        Preconditions.checkNotNull(deviceState);
+    public static Long fromNodeConnectorRef(@Nonnull NodeConnectorRef nodeConnectorRef, short version) {
+        Preconditions.checkNotNull(nodeConnectorRef);
 
-        if (nodeConnectorRef != null && nodeConnectorRef.getValue() instanceof KeyedInstanceIdentifier) {
+        Long port = null;
+
+        if (nodeConnectorRef.getValue() instanceof KeyedInstanceIdentifier) {
             KeyedInstanceIdentifier<NodeConnector, NodeConnectorKey> identifier =
                     (KeyedInstanceIdentifier<NodeConnector, NodeConnectorKey>) nodeConnectorRef.getValue();
 
-            OpenflowVersion version = OpenflowVersion.get(deviceState.getVersion());
+            OpenflowVersion ofVersion = OpenflowVersion.get(version);
             String nodeConnectorId = identifier.getKey().getId().getValue();
 
-            return InventoryDataServiceUtil.portNumberfromNodeConnectorId(version, nodeConnectorId);
-        } else {
-            return getPortNoFromDeviceState(deviceState);
+            port = InventoryDataServiceUtil.portNumberfromNodeConnectorId(ofVersion, nodeConnectorId);
         }
+
+        return port;
     }
 
     @VisibleForTesting
     @Nullable
-    static Long getPortNoFromDeviceState(@Nonnull DeviceState deviceState) {
-        Preconditions.checkNotNull(deviceState);
+    static Long getPortNoFromPacketIn(@Nonnull PacketIn packetIn) {
+        Preconditions.checkNotNull(packetIn);
 
-        List<PhyPort> ports = deviceState.getFeatures().getPhyPort();
+        Long port = null;
+
+        if (packetIn.getVersion() == OFConstants.OFP_VERSION_1_0 && packetIn.getInPort() != null) {
+            port = packetIn.getInPort().longValue();
+        } else if (packetIn.getVersion() == OFConstants.OFP_VERSION_1_3) {
+            if (packetIn.getMatch() != null && packetIn.getMatch().getMatchEntry() != null) {
+                List<MatchEntry> entries = packetIn.getMatch().getMatchEntry();
+
+                for (MatchEntry entry : entries) {
+                    if (entry.getMatchEntryValue() instanceof InPortCase) {
+                        InPortCase inPortCase = (InPortCase) entry.getMatchEntryValue();
+
+                        InPort inPort = inPortCase.getInPort();
+
+                        if (inPort != null) {
+                            port = inPort.getPortNumber().getValue();
+                            break;
+                        }
+                    }
+                }
+            }
+        }
 
-        return ports != null ?
-                ports.stream().filter(Objects::nonNull).map(PhyPort::getPortNo).filter(Objects::nonNull).findFirst().orElse(null) :
-                null;
+        return port;
     }
 }