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