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