--- /dev/null
+/**\r
+ * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.\r
+ *\r
+ * This program and the accompanying materials are made available under the\r
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,\r
+ * and is available at http://www.eclipse.org/legal/epl-v10.html\r
+ */\r
+\r
+package org.opendaylight.openflowplugin.openflow.md.core.translator;\r
+\r
+import io.netty.channel.ChannelFuture;\r
+import io.netty.channel.EventLoopGroup;\r
+import io.netty.channel.nio.NioEventLoopGroup;\r
+import io.netty.channel.socket.nio.NioSocketChannel;\r
+\r
+import java.io.IOException;\r
+import java.math.BigInteger;\r
+import java.net.InetSocketAddress;\r
+import java.util.ArrayList;\r
+import java.util.Collection;\r
+import java.util.HashMap;\r
+import java.util.List;\r
+import java.util.Map;\r
+\r
+import org.junit.Assert;\r
+import org.junit.Test;\r
+import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;\r
+import org.opendaylight.openflowjava.protocol.impl.connection.ConnectionAdapterImpl;\r
+import org.opendaylight.openflowplugin.api.openflow.md.core.SwitchConnectionDistinguisher;\r
+import org.opendaylight.openflowplugin.api.openflow.md.core.TranslatorKey;\r
+import org.opendaylight.openflowplugin.openflow.md.core.ConnectionConductorImpl;\r
+import org.opendaylight.openflowplugin.openflow.md.core.IMDMessageTranslator;\r
+import org.opendaylight.openflowplugin.openflow.md.core.session.SessionContext;\r
+import org.opendaylight.openflowplugin.openflow.md.core.session.SessionContextOFImpl;\r
+import org.opendaylight.openflowplugin.openflow.md.core.session.SwitchConnectionCookieOFImpl;\r
+import org.opendaylight.openflowplugin.openflow.md.queue.QueueProcessorLightImpl;\r
+import org.opendaylight.openflowplugin.openflow.md.util.OpenflowPortsUtil;\r
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PacketInReason;\r
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;\r
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutputBuilder;\r
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;\r
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessage;\r
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessageBuilder;\r
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.SetConfigInput;\r
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.SetConfigInputBuilder;\r
+import org.opendaylight.yangtools.yang.binding.DataObject;\r
+import org.slf4j.Logger;\r
+import org.slf4j.LoggerFactory;\r
+\r
+/**\r
+ * Created by Jakub Toth jatoth@cisco.com on 3/10/14.\r
+ */\r
+\r
+public class PacketInV10TranslatorTest{\r
+ private static final Logger log = LoggerFactory\r
+ .getLogger(PacketInV10TranslatorTest.class);\r
+\r
+ /**\r
+ * test\r
+ * {@link PacketInV10Translator#translate(SwitchConnectionDistinguisher, SessionContext, OfHeader)}\r
+ * - all parameteres are null - translates packetIn from OF-API model to\r
+ * MD-SAL model, supports OF-1.0\r
+ */\r
+ @Test\r
+ public void testTranslateWithAllNullParam(){\r
+ SwitchConnectionDistinguisher cookie = null;\r
+ SessionContext sessionContext = null;\r
+ OfHeader msg = null;\r
+\r
+ PacketInV10Translator packetInV10Translator = new PacketInV10Translator();\r
+\r
+ List<DataObject> salPacketIn = packetInV10Translator.translate(cookie,\r
+ sessionContext, msg);\r
+\r
+ Assert.assertEquals(true, salPacketIn.isEmpty());\r
+\r
+ log.info("Test with all null parameters done.");\r
+ }\r
+\r
+ /**\r
+ * test\r
+ * {@link PacketInV10Translator#translate(SwitchConnectionDistinguisher, SessionContext, OfHeader)}\r
+ * - DPID is null- translates packetIn from OF-API model to MD-SAL model,\r
+ * supports OF-1.0\r
+ */\r
+ @Test\r
+ public void testTranslateDPIDNull(){\r
+ SessionContextOFImpl sessionContextOFImpl = new SessionContextOFImpl();\r
+\r
+ SwitchConnectionDistinguisher cookie = this.settingCookie();\r
+\r
+ byte[] data = this.messageData();\r
+ PacketInMessage message = this.createPacketInMessage(data, null);\r
+\r
+ PacketInV10Translator packetInV10Translator = new PacketInV10Translator();\r
+\r
+ List<DataObject> salPacketIn = packetInV10Translator.translate(cookie,\r
+ sessionContextOFImpl, message);\r
+\r
+ Assert.assertEquals(true, salPacketIn.isEmpty());\r
+ log.info("Test with null DPID parameter done.");\r
+ }\r
+\r
+ /**\r
+ * test\r
+ * {@link PacketInV10Translator#translate(SwitchConnectionDistinguisher, SessionContext, OfHeader)}\r
+ * - inPort is null- translates packetIn from OF-API model to MD-SAL model,\r
+ * supports OF-1.0\r
+ */\r
+ @Test\r
+ public void testTranslateInPortNull(){\r
+ BigInteger datapathId = this.dataPathId();\r
+\r
+ GetFeaturesOutputBuilder featuresBuilder = new GetFeaturesOutputBuilder();\r
+ featuresBuilder.setDatapathId(datapathId);\r
+\r
+ SessionContextOFImpl sessionContextOFImpl = new SessionContextOFImpl();\r
+ sessionContextOFImpl.setFeatures(featuresBuilder.build());\r
+\r
+ SwitchConnectionDistinguisher cookie = this.settingCookie();\r
+\r
+ byte[] data = this.messageData();\r
+\r
+ PacketInMessage message = this.createPacketInMessage(data, null);\r
+\r
+ PacketInV10Translator packetInV10Translator = new PacketInV10Translator();\r
+\r
+ List<DataObject> salPacketIn = packetInV10Translator.translate(cookie,\r
+ sessionContextOFImpl, message);\r
+\r
+ Assert.assertEquals(true, salPacketIn.isEmpty());\r
+\r
+ log.info("Test with null inPort parameter done.");\r
+ }\r
+\r
+ /**\r
+ * test\r
+ * {@link PacketInV10Translator#translate(SwitchConnectionDistinguisher, SessionContext, OfHeader)}\r
+ * - translates packetIn from OF-API model to MD-SAL model, supports OF-1.0\r
+ * \r
+ * @throws IOException\r
+ */\r
+ @Test\r
+ public void testTranslate(){\r
+ BigInteger datapathId = this.dataPathId();\r
+\r
+ byte[] data = this.messageData();\r
+\r
+ PacketInMessage message = this.createPacketInMessage(data, 5);\r
+\r
+ GetFeaturesOutput featuresOutput = this\r
+ .createGetFeatureOutput(datapathId);\r
+\r
+ NioSocketChannel nioSocketChannel = new NioSocketChannel();\r
+ this.registerChannel(nioSocketChannel);\r
+\r
+ SetConfigInput setConfigInput = this.createSetConfigInput();\r
+\r
+ ConnectionAdapterImpl connectionAdapter = new ConnectionAdapterImpl(\r
+ nioSocketChannel, new InetSocketAddress(51));\r
+ connectionAdapter.setConfig(setConfigInput);\r
+\r
+ ConnectionConductorImpl connectionConductor = new ConnectionConductorImpl(\r
+ connectionAdapter);\r
+\r
+ this.initConnectionConductor(connectionConductor, featuresOutput);\r
+\r
+ SessionContextOFImpl sessionContextOFImpl = new SessionContextOFImpl();\r
+ sessionContextOFImpl.setFeatures(featuresOutput);\r
+ sessionContextOFImpl.setPrimaryConductor(connectionConductor);\r
+\r
+ SwitchConnectionDistinguisher cookie = this.settingCookie();\r
+\r
+ PacketInV10Translator packetInV10Translator = new PacketInV10Translator();\r
+\r
+ OpenflowPortsUtil.init();\r
+\r
+ List<DataObject> salPacketIn = packetInV10Translator.translate(cookie,\r
+ sessionContextOFImpl, message);\r
+\r
+ String expectedString = "[PacketReceived [_ingress=NodeConnectorRef [_value=KeyedInstanceIdentifier"\r
+ + "{targetType=interface org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector,"\r
+ + " path=[org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes,"\r
+ + " org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node["\r
+ + "key=NodeKey [_id=Uri [_value=openflow:"\r
+ + datapathId.toString()\r
+ + "]]],"\r
+ + " org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector["\r
+ + "key=NodeConnectorKey [_id=Uri [_value=openflow:"\r
+ + datapathId.toString()\r
+ + ":"\r
+ + message.getInPort().toString()\r
+ + "]]]]}], _packetInReason=class org.opendaylight.yang.gen.v1.urn.opendaylight."\r
+ + "packet.service.rev130709.SendToController, _payload=[115, 101, 110, 100, 79, 117,"\r
+ + " 116, 112, 117, 116, 77, 115, 103, 95, 84, 69, 83, 84], augmentation=[]]]";\r
+ Assert.assertEquals(expectedString, salPacketIn.toString());\r
+\r
+ log.info("Test translate done.");\r
+ }\r
+\r
+ /**\r
+ * create datapathID\r
+ * \r
+ * @return BigInteger\r
+ */\r
+ private BigInteger dataPathId(){\r
+ byte[] datapathIdByte = new byte[EncodeConstants.SIZE_OF_LONG_IN_BYTES];\r
+ for(int i = 0; i < datapathIdByte.length; i++){\r
+ datapathIdByte[i] = 1;\r
+ }\r
+ return new BigInteger(1, datapathIdByte);\r
+ }\r
+\r
+ /**\r
+ * generate message from string to byte[]\r
+ * \r
+ * @return byte[]\r
+ */\r
+ private byte[] messageData(){\r
+ String string = new String("sendOutputMsg_TEST");\r
+ return string.getBytes();\r
+ }\r
+\r
+ /**\r
+ * create PacketInMessage with setting Version, InPort, Data, Reason\r
+ * \r
+ * @param data\r
+ * @param port\r
+ * @return PacketInMessage\r
+ */\r
+ private PacketInMessage createPacketInMessage(final byte[] data,\r
+ final java.lang.Integer port){\r
+ PacketInReason reason = PacketInReason.OFPRACTION;\r
+ return new PacketInMessageBuilder()\r
+ .setVersion((short) EncodeConstants.OF10_VERSION_ID)\r
+ .setInPort(port).setData(data).setReason(reason).build();\r
+\r
+ }\r
+\r
+ /**\r
+ * create cookie\r
+ * \r
+ * @return SwitchConnectionDistinguisher\r
+ */\r
+ private SwitchConnectionDistinguisher settingCookie(){\r
+ SwitchConnectionCookieOFImpl key = new SwitchConnectionCookieOFImpl();\r
+ key.setAuxiliaryId((short) 1);\r
+ key.init(42);\r
+ return key;\r
+ }\r
+\r
+ /**\r
+ * create GetFeatureOutput\r
+ * \r
+ * @param datapathId\r
+ * @return GetFeaturesOutput\r
+ */\r
+ private GetFeaturesOutput createGetFeatureOutput(final BigInteger datapathId){\r
+ return new GetFeaturesOutputBuilder().setDatapathId(datapathId)\r
+ .setVersion((short) 0x01).build();\r
+ }\r
+\r
+ /**\r
+ * register eventLop\r
+ * \r
+ * @param channel\r
+ */\r
+ private void registerChannel(final NioSocketChannel channel){\r
+ EventLoopGroup eventLop = new NioEventLoopGroup();\r
+ ChannelFuture regFuture = eventLop.register(channel);\r
+ }\r
+\r
+ /**\r
+ * build SetConfigInput with setting Version and Xid\r
+ * \r
+ * @return SetConfigInput\r
+ */\r
+ private SetConfigInput createSetConfigInput(){\r
+ return new SetConfigInputBuilder().setVersion((short) 0x01)\r
+ .setXid(0xfffffL).build();\r
+ }\r
+\r
+ /**\r
+ * init connectionConductor\r
+ * \r
+ * @param connectionConductor\r
+ * @param featuresOutput\r
+ */\r
+ private void initConnectionConductor(\r
+ final ConnectionConductorImpl connectionConductor,\r
+ final GetFeaturesOutput featuresOutput){\r
+ TranslatorKey paramK = new TranslatorKey(1, "PacketInMessage.class");\r
+ Collection<IMDMessageTranslator<OfHeader, List<DataObject>>> coll = new ArrayList<>();\r
+ coll.add(new PacketInV10Translator());\r
+ Map<TranslatorKey, Collection<IMDMessageTranslator<OfHeader, List<DataObject>>>> translatorMapping = new HashMap<>();\r
+ translatorMapping.put(paramK, coll);\r
+\r
+ QueueProcessorLightImpl queueProcessor = new QueueProcessorLightImpl();\r
+ queueProcessor.setTranslatorMapping(translatorMapping);\r
+ queueProcessor.init();\r
+ connectionConductor.setQueueProcessor(queueProcessor);\r
+ connectionConductor.init();\r
+ connectionConductor\r
+ .onHandshakeSuccessfull(featuresOutput, (short) 0x01);\r
+ }\r
+}\r