Handling async replies from device in DeviceCtx 95/17595/3
authorTimotej Kubas <tkubas@cisco.com>
Wed, 1 Apr 2015 17:05:59 +0000 (19:05 +0200)
committermichal rehak <mirehak@cisco.com>
Thu, 2 Apr 2015 10:43:46 +0000 (10:43 +0000)
- introduced simple test for PacketReceivedTranslator
- fixed asterix in inports

Change-Id: I172d7b2db82a552140e5fd3822d080df9cbb97f9
Signed-off-by: Timotej Kubas <tkubas@cisco.com>
openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/device/DeviceContextImpl.java
openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/device/translator/PacketReceivedTranslator.java
openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/device/translator/PortUpdateTranslator.java
openflowplugin-impl/src/test/java/org/opendaylight/openflowplugin/impl/device/translator/PacketReceivedTranslatorTest.java

index 2203b65b313b7c47f8705d224683cc5c6555eff5..78d772c36da17694899edfa101d4a31382819ad8 100644 (file)
@@ -23,7 +23,14 @@ import org.opendaylight.controller.md.sal.common.api.data.AsyncTransaction;
 import org.opendaylight.controller.md.sal.common.api.data.TransactionChain;
 import org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener;
 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
-import org.opendaylight.openflowplugin.api.openflow.device.*;
+import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
+import org.opendaylight.openflowplugin.api.openflow.device.DeviceReplyProcessor;
+import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
+import org.opendaylight.openflowplugin.api.openflow.device.MessageTranslator;
+import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
+import org.opendaylight.openflowplugin.api.openflow.device.TranslatorLibrary;
+import org.opendaylight.openflowplugin.api.openflow.device.Xid;
+import org.opendaylight.openflowplugin.api.openflow.device.XidGenerator;
 import org.opendaylight.openflowplugin.api.openflow.device.exception.DeviceDataException;
 import org.opendaylight.openflowplugin.api.openflow.md.core.SwitchConnectionDistinguisher;
 import org.opendaylight.openflowplugin.api.openflow.md.core.TranslatorKey;
@@ -32,8 +39,12 @@ import org.opendaylight.openflowplugin.impl.device.translator.PacketReceivedTran
 import org.opendaylight.openflowplugin.impl.device.translator.PortUpdateTranslator;
 import org.opendaylight.openflowplugin.openflow.md.core.session.SwitchConnectionCookieOFImpl;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnector;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.*;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.Error;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.Error;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowRemoved;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessage;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortStatusMessage;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketReceived;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.TableFeatures;
 import org.opendaylight.yangtools.yang.binding.ChildOf;
index 4e26486ab955042d97b0b78c1b8c007141e07ceb..ac506d54f771c388e06ad3d63c618336baebd123 100644 (file)
@@ -7,6 +7,7 @@
  */
 package org.opendaylight.openflowplugin.impl.device.translator;
 
+import org.opendaylight.openflowplugin.api.OFConstants;
 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
 import org.opendaylight.openflowplugin.api.openflow.device.MessageTranslator;
 import org.opendaylight.openflowplugin.api.openflow.md.util.OpenflowVersion;
@@ -41,9 +42,9 @@ public class PacketReceivedTranslator implements MessageTranslator<PacketInMessa
 
         // extract the port number
         Long port = null;
-        if(input.getVersion() == OpenflowVersion.OF10.getVersion()) {
+        if(input.getVersion() == OFConstants.OFP_VERSION_1_0 && input.getInPort() != null) {
             port = input.getInPort().longValue();
-        } else if (input.getVersion() == OpenflowVersion.OF13.getVersion()) {
+        } else if (input.getVersion() == OFConstants.OFP_VERSION_1_3) {
             if (input.getMatch() != null && input.getMatch().getMatchEntry() != null) {
                 port = getPortNumberFromMatch(input.getMatch().getMatchEntry());
             }
@@ -52,15 +53,23 @@ public class PacketReceivedTranslator implements MessageTranslator<PacketInMessa
         //TODO connection cookie from connection distinguisher
 //        packetReceivedBuilder.setConnectionCookie(new ConnectionCookie(input.getCookie().longValue()));
         packetReceivedBuilder.setPayload(input.getData());
-        packetReceivedBuilder.setFlowCookie(new FlowCookie(input.getCookie()));
-        packetReceivedBuilder.setIngress(InventoryDataServiceUtil.nodeConnectorRefFromDatapathIdPortno(datapathId, port, OpenflowVersion.get(input.getVersion())));
-
+        // 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())));
+        }
         packetReceivedBuilder.setPacketInReason(PacketInUtil.getMdSalPacketInReason(input.getReason()));
-        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 org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.TableId(input.getTableId().getValue().shortValue()));
+        }
 
-        org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.packet.received.Match packetInMatch = getPacketInMatch(input, datapathId);
-        packetReceivedBuilder.setMatch(packetInMatch);
+        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();
     }
index b694359cc61ee4c91b27967458d180316b7d6dd4..ccb2b56968c5176cb259640ad589b97395649d23 100644 (file)
@@ -7,6 +7,7 @@
  */
 package org.opendaylight.openflowplugin.impl.device.translator;
 
+import org.opendaylight.openflowplugin.api.OFConstants;
 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
 import org.opendaylight.openflowplugin.api.openflow.device.MessageTranslator;
 import org.opendaylight.openflowplugin.api.openflow.md.util.OpenflowVersion;
@@ -28,14 +29,14 @@ public class PortUpdateTranslator implements MessageTranslator<PortStatusMessage
         // TODO Auto-generated method stub
         FlowCapableNodeConnectorBuilder builder = new FlowCapableNodeConnectorBuilder();
         //OF1.0
-        if(input.getVersion() == OpenflowVersion.OF10.getVersion()) {
+        if(input.getVersion() == OFConstants.OFP_VERSION_1_0) {
             builder.setAdvertisedFeatures(PortTranslatorUtil.translatePortFeatures(input.getAdvertisedFeaturesV10()));
             builder.setConfiguration(PortTranslatorUtil.translatePortConfig(input.getConfigV10()));
             builder.setCurrentFeature(PortTranslatorUtil.translatePortFeatures(input.getCurrentFeaturesV10()));
             builder.setPeerFeatures(PortTranslatorUtil.translatePortFeatures(input.getPeerFeaturesV10()));
             builder.setState(PortTranslatorUtil.translatePortState(input.getStateV10()));
             builder.setSupported(PortTranslatorUtil.translatePortFeatures(input.getSupportedFeaturesV10()));
-        } else if (input.getVersion() == OpenflowVersion.OF13.getVersion()) {
+        } else if (input.getVersion() == OFConstants.OFP_VERSION_1_3) {
             builder.setAdvertisedFeatures(PortTranslatorUtil.translatePortFeatures(input.getAdvertisedFeatures()));
             builder.setConfiguration(PortTranslatorUtil.translatePortConfig(input.getConfig()));
             builder.setCurrentFeature(PortTranslatorUtil.translatePortFeatures(input.getCurrentFeatures()));
index 9fe9891d6b5ca0f9f89c566eeffc11308b8694aa..4239696b0a928da11af0e9dfaa7cc9dc01556312 100644 (file)
@@ -1,16 +1,25 @@
 package org.opendaylight.openflowplugin.impl.device.translator;
 
+import org.junit.Assert;
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.mockito.Mock;
+import org.mockito.Mockito;
 import org.mockito.runners.MockitoJUnitRunner;
+import org.opendaylight.controller.md.sal.binding.api.DataBroker;
 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
+import org.opendaylight.openflowplugin.api.OFConstants;
+import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
+import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
 import org.opendaylight.openflowplugin.impl.device.DeviceContextImpl;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PacketInReason;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessage;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessageBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketReceived;
+import java.math.BigInteger;
 
 /**
  * Created by tkubas on 4/1/15.
@@ -19,26 +28,41 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731
 @RunWith(MockitoJUnitRunner.class)
 public class PacketReceivedTranslatorTest {
 
+    @Mock
+    ConnectionContext connectionContext;
+    @Mock
+    FeaturesReply featuresReply;
+    @Mock
+    DeviceState deviceState;
+    @Mock
+    DataBroker dataBroker;
     @Mock
     DeviceContext deviceContext;
+    String data = "Test_Data";
 
     @Before
     public void setUp() throws Exception {
-
+        Mockito.when(deviceContext.getPrimaryConnectionContext()).thenReturn(connectionContext);
+        Mockito.when(connectionContext.getFeatures()).thenReturn(featuresReply);
+        Mockito.when(featuresReply.getDatapathId()).thenReturn(BigInteger.TEN);
     }
 
     @Test
     public void testTranslate() throws Exception {
         PacketReceivedTranslator packetReceivedTranslator = new PacketReceivedTranslator();
-//        packetReceivedTranslator.translate()
+        PacketInMessage packetInMessage = createPacketInMessage(data.getBytes(), 5);
+        PacketReceived packetReceived = packetReceivedTranslator.translate(packetInMessage, deviceContext, null);
+        Assert.assertArrayEquals(packetInMessage.getData(), packetReceived.getPayload());
+        Assert.assertEquals("org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.SendToController",
+                             packetReceived.getPacketInReason().getName());
     }
 
     private static PacketInMessage createPacketInMessage(final byte[] data,
                                                          final java.lang.Integer port) {
         PacketInReason reason = PacketInReason.OFPRACTION;
         return new PacketInMessageBuilder()
-                .setVersion((short) EncodeConstants.OF10_VERSION_ID)
-                .setInPort(port).setData(data).setReason(reason).build();
+                .setVersion((short) OFConstants.OFP_VERSION_1_0)
+                .setData(data).setReason(reason).build();
 
     }
 }
\ No newline at end of file