Bug 1277 - Move ByteBuffUtils to separate bundle
[openflowjava.git] / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / util / OF13InstructionsSerializerTest.java
1 /*\r
2  * Copyright (c) 2013 Pantheon Technologies s.r.o. and others.  All rights reserved.\r
3  *\r
4  * This program and the accompanying materials are made available under the\r
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,\r
6  * and is available at http://www.eclipse.org/legal/epl-v10.html\r
7  */\r
8 package org.opendaylight.openflowjava.protocol.impl.util;\r
9 \r
10 import io.netty.buffer.ByteBuf;\r
11 import io.netty.buffer.UnpooledByteBufAllocator;\r
12 \r
13 import java.util.ArrayList;\r
14 import java.util.List;\r
15 \r
16 import org.junit.Assert;\r
17 import org.junit.Before;\r
18 import org.junit.Test;\r
19 import org.opendaylight.openflowjava.protocol.api.extensibility.SerializerRegistry;\r
20 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;\r
21 import org.opendaylight.openflowjava.protocol.impl.serialization.SerializerRegistryImpl;\r
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.ActionsInstruction;\r
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.ActionsInstructionBuilder;\r
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.EthertypeAction;\r
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.EthertypeActionBuilder;\r
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MaxLengthAction;\r
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MaxLengthActionBuilder;\r
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MetadataInstruction;\r
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MetadataInstructionBuilder;\r
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MeterIdInstruction;\r
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MeterIdInstructionBuilder;\r
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.NwTtlAction;\r
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.NwTtlActionBuilder;\r
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.PortAction;\r
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.PortActionBuilder;\r
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.TableIdInstruction;\r
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.TableIdInstructionBuilder;\r
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.Output;\r
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.PopPbb;\r
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.PushVlan;\r
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.SetNwTtl;\r
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.actions.grouping.Action;\r
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.actions.grouping.ActionBuilder;\r
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.ApplyActions;\r
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.ClearActions;\r
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.GotoTable;\r
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.Meter;\r
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.WriteActions;\r
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.WriteMetadata;\r
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instructions.grouping.Instruction;\r
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instructions.grouping.InstructionBuilder;\r
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.EtherType;\r
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortNumber;\r
54 \r
55 /**\r
56  * @author michal.polkorab\r
57  *\r
58  */\r
59 public class OF13InstructionsSerializerTest {\r
60 \r
61     private SerializerRegistry registry;\r
62 \r
63     /**\r
64      * Initializes serializer table and stores correct factory in field\r
65      */\r
66     @Before\r
67     public void startUp() {\r
68         registry = new SerializerRegistryImpl();\r
69         registry.init();\r
70     }\r
71 \r
72     /**\r
73      * Testing instructions translation\r
74      */\r
75     @Test\r
76     public void test() {\r
77         List<Instruction> instructions = new ArrayList<>();\r
78         // Goto_table instruction\r
79         InstructionBuilder builder = new InstructionBuilder();\r
80         builder.setType(GotoTable.class);\r
81         TableIdInstructionBuilder tableIdBuilder = new TableIdInstructionBuilder();\r
82         tableIdBuilder.setTableId((short) 5);\r
83         builder.addAugmentation(TableIdInstruction.class, tableIdBuilder.build());\r
84         instructions.add(builder.build());\r
85         builder = new InstructionBuilder();\r
86         // Write_metadata instruction\r
87         builder.setType(WriteMetadata.class);\r
88         MetadataInstructionBuilder metaBuilder = new MetadataInstructionBuilder();\r
89         metaBuilder.setMetadata(ByteBufUtils.hexStringToBytes("00 01 02 03 04 05 06 07"));\r
90         metaBuilder.setMetadataMask(ByteBufUtils.hexStringToBytes("07 06 05 04 03 02 01 00"));\r
91         builder.addAugmentation(MetadataInstruction.class, metaBuilder.build());\r
92         instructions.add(builder.build());\r
93         // Clear_actions instruction\r
94         builder = new InstructionBuilder();\r
95         builder.setType(ClearActions.class);\r
96         instructions.add(builder.build());\r
97         // Meter instruction\r
98         builder = new InstructionBuilder();\r
99         builder.setType(Meter.class);\r
100         MeterIdInstructionBuilder meterBuilder = new MeterIdInstructionBuilder();\r
101         meterBuilder.setMeterId(42L);\r
102         builder.addAugmentation(MeterIdInstruction.class, meterBuilder.build());\r
103         instructions.add(builder.build());\r
104         // Write_actions instruction\r
105         builder = new InstructionBuilder();\r
106         builder.setType(WriteActions.class);\r
107         ActionsInstructionBuilder actionsBuilder = new ActionsInstructionBuilder();\r
108         List<Action> actions = new ArrayList<>();\r
109         ActionBuilder actionBuilder = new ActionBuilder();\r
110         actionBuilder.setType(Output.class);\r
111         PortActionBuilder portBuilder = new PortActionBuilder();\r
112         portBuilder.setPort(new PortNumber(45L));\r
113         actionBuilder.addAugmentation(PortAction.class, portBuilder.build());\r
114         MaxLengthActionBuilder maxBuilder = new MaxLengthActionBuilder();\r
115         maxBuilder.setMaxLength(55);\r
116         actionBuilder.addAugmentation(MaxLengthAction.class, maxBuilder.build());\r
117         actions.add(actionBuilder.build());\r
118         actionBuilder = new ActionBuilder();\r
119         actionBuilder.setType(SetNwTtl.class);\r
120         NwTtlActionBuilder nwTtl = new NwTtlActionBuilder();\r
121         nwTtl.setNwTtl((short) 64);\r
122         actionBuilder.addAugmentation(NwTtlAction.class, nwTtl.build());\r
123         actions.add(actionBuilder.build());\r
124         actionsBuilder.setAction(actions);\r
125         builder.addAugmentation(ActionsInstruction.class, actionsBuilder.build());\r
126         instructions.add(builder.build());\r
127         // Apply_actions instruction\r
128         builder = new InstructionBuilder();\r
129         builder.setType(ApplyActions.class);\r
130         actionsBuilder = new ActionsInstructionBuilder();\r
131         actions = new ArrayList<>();\r
132         actionBuilder = new ActionBuilder();\r
133         actionBuilder.setType(PushVlan.class);\r
134         EthertypeActionBuilder ethertypeBuilder = new EthertypeActionBuilder();\r
135         ethertypeBuilder.setEthertype(new EtherType(14));\r
136         actionBuilder.addAugmentation(EthertypeAction.class, ethertypeBuilder.build());\r
137         actions.add(actionBuilder.build());\r
138         actionBuilder = new ActionBuilder();\r
139         actionBuilder.setType(PopPbb.class);\r
140         actions.add(actionBuilder.build());\r
141         actionsBuilder.setAction(actions);\r
142         builder.addAugmentation(ActionsInstruction.class, actionsBuilder.build());\r
143         instructions.add(builder.build());\r
144         \r
145         ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();\r
146         ListSerializer.serializeList(instructions, EnhancedTypeKeyMakerFactory\r
147                 .createInstructionKeyMaker(EncodeConstants.OF13_VERSION_ID), registry, out);\r
148         \r
149         Assert.assertEquals("Wrong instruction type", 1, out.readUnsignedShort());\r
150         Assert.assertEquals("Wrong instruction length", 8, out.readUnsignedShort());\r
151         Assert.assertEquals("Wrong instruction table-id", 5, out.readUnsignedByte());\r
152         out.skipBytes(3);\r
153         Assert.assertEquals("Wrong instruction type", 2, out.readUnsignedShort());\r
154         Assert.assertEquals("Wrong instruction length", 24, out.readUnsignedShort());\r
155         out.skipBytes(4);\r
156         byte[] actual = new byte[8];\r
157         out.readBytes(actual);\r
158         Assert.assertEquals("Wrong instruction metadata", "00 01 02 03 04 05 06 07",\r
159                 ByteBufUtils.bytesToHexString(actual));\r
160         actual = new byte[8];\r
161         out.readBytes(actual);\r
162         Assert.assertEquals("Wrong instruction metadata-mask", "07 06 05 04 03 02 01 00",\r
163                 ByteBufUtils.bytesToHexString(actual));\r
164         Assert.assertEquals("Wrong instruction type", 5, out.readUnsignedShort());\r
165         Assert.assertEquals("Wrong instruction length", 8, out.readUnsignedShort());\r
166         out.skipBytes(4);\r
167         Assert.assertEquals("Wrong instruction type", 6, out.readUnsignedShort());\r
168         Assert.assertEquals("Wrong instruction length", 8, out.readUnsignedShort());\r
169         Assert.assertEquals("Wrong instruction meter-id", 42, out.readUnsignedInt());\r
170         Assert.assertEquals("Wrong instruction type", 3, out.readUnsignedShort());\r
171         Assert.assertEquals("Wrong instruction length", 32, out.readUnsignedShort());\r
172         out.skipBytes(4);\r
173         Assert.assertEquals("Wrong action type", 0, out.readUnsignedShort());\r
174         Assert.assertEquals("Wrong action length", 16, out.readUnsignedShort());\r
175         Assert.assertEquals("Wrong action type", 45, out.readUnsignedInt());\r
176         Assert.assertEquals("Wrong action type", 55, out.readUnsignedShort());\r
177         out.skipBytes(6);\r
178         Assert.assertEquals("Wrong action type", 23, out.readUnsignedShort());\r
179         Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());\r
180         Assert.assertEquals("Wrong action type", 64, out.readUnsignedByte());\r
181         out.skipBytes(3);\r
182         Assert.assertEquals("Wrong instruction type", 4, out.readUnsignedShort());\r
183         Assert.assertEquals("Wrong instruction length", 24, out.readUnsignedShort());\r
184         out.skipBytes(4);\r
185         Assert.assertEquals("Wrong action type", 17, out.readUnsignedShort());\r
186         Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());\r
187         Assert.assertEquals("Wrong action ethertype", 14, out.readUnsignedShort());\r
188         out.skipBytes(2);\r
189         Assert.assertEquals("Wrong action type", 27, out.readUnsignedShort());\r
190         Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());\r
191         out.skipBytes(4);\r
192         Assert.assertTrue("Not all data were read", out.readableBytes() == 0);\r
193     }\r
194 \r
195 }\r