Added default experimenter serializers
[openflowjava.git] / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / util / OF13ActionsSerializerTest.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.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.ExperimenterAction;\r
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.ExperimenterActionBuilder;\r
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.GroupIdAction;\r
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.GroupIdActionBuilder;\r
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MaxLengthAction;\r
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MaxLengthActionBuilder;\r
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MplsTtlAction;\r
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MplsTtlActionBuilder;\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.OxmFieldsAction;\r
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.OxmFieldsActionBuilder;\r
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.PortAction;\r
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.PortActionBuilder;\r
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.PortNumberMatchEntry;\r
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.PortNumberMatchEntryBuilder;\r
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.QueueIdAction;\r
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.QueueIdActionBuilder;\r
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.CopyTtlIn;\r
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.CopyTtlOut;\r
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.DecMplsTtl;\r
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.DecNwTtl;\r
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.Experimenter;\r
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.Group;\r
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.Output;\r
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.PopMpls;\r
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.PopPbb;\r
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.PopVlan;\r
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.PushMpls;\r
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.PushPbb;\r
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.PushVlan;\r
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.SetField;\r
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.SetMplsTtl;\r
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.SetNwTtl;\r
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.SetQueue;\r
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.actions.grouping.Action;\r
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.actions.grouping.ActionBuilder;\r
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.EtherType;\r
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortNumber;\r
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.InPort;\r
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.OpenflowBasicClass;\r
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.oxm.fields.grouping.MatchEntries;\r
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.oxm.fields.grouping.MatchEntriesBuilder;\r
68 \r
69 /**\r
70  * @author michal.polkorab\r
71  * \r
72  */\r
73 public class OF13ActionsSerializerTest {\r
74 \r
75     private SerializerRegistry registry;\r
76     private OFSerializer<Action> actionSerializer;\r
77 \r
78     /**\r
79      * Initializes serializer table and stores correct factory in field\r
80      */\r
81     @Before\r
82     public void startUp() {\r
83         registry = new SerializerRegistryImpl();\r
84         registry.init();\r
85         actionSerializer = registry.getSerializer(\r
86                 new MessageTypeKey<>(EncodeConstants.OF13_VERSION_ID, Action.class));\r
87     }\r
88 \r
89     /**\r
90      * Testing correct serialization of actions\r
91      */\r
92     @Test\r
93     public void test() {\r
94         List<Action> actions = new ArrayList<>();\r
95         ActionBuilder actionBuilder = new ActionBuilder();\r
96         actionBuilder.setType(Output.class);\r
97         PortActionBuilder port = new PortActionBuilder();\r
98         port.setPort(new PortNumber(42L));\r
99         actionBuilder.addAugmentation(PortAction.class, port.build());\r
100         MaxLengthActionBuilder maxLen = new MaxLengthActionBuilder();\r
101         maxLen.setMaxLength(52);\r
102         actionBuilder.addAugmentation(MaxLengthAction.class, maxLen.build());\r
103         actions.add(actionBuilder.build());\r
104         actionBuilder = new ActionBuilder();\r
105         actionBuilder.setType(CopyTtlOut.class);\r
106         actions.add(actionBuilder.build());\r
107         actionBuilder = new ActionBuilder();\r
108         actionBuilder.setType(CopyTtlIn.class);\r
109         actions.add(actionBuilder.build());\r
110         actionBuilder = new ActionBuilder();\r
111         actionBuilder.setType(SetMplsTtl.class);\r
112         MplsTtlActionBuilder mplsTtl = new MplsTtlActionBuilder();\r
113         mplsTtl.setMplsTtl((short) 4);\r
114         actionBuilder.addAugmentation(MplsTtlAction.class, mplsTtl.build());\r
115         actions.add(actionBuilder.build());\r
116         actionBuilder = new ActionBuilder();\r
117         actionBuilder.setType(DecMplsTtl.class);\r
118         actions.add(actionBuilder.build());\r
119         actionBuilder = new ActionBuilder();\r
120         actionBuilder.setType(PushVlan.class);\r
121         EthertypeActionBuilder etherType = new EthertypeActionBuilder();\r
122         etherType.setEthertype(new EtherType(16));\r
123         actionBuilder.addAugmentation(EthertypeAction.class, etherType.build());\r
124         actions.add(actionBuilder.build());\r
125         actionBuilder = new ActionBuilder();\r
126         actionBuilder.setType(PopVlan.class);\r
127         actions.add(actionBuilder.build());\r
128         actionBuilder = new ActionBuilder();\r
129         actionBuilder.setType(PushMpls.class);\r
130         etherType = new EthertypeActionBuilder();\r
131         etherType.setEthertype(new EtherType(17));\r
132         actionBuilder.addAugmentation(EthertypeAction.class, etherType.build());\r
133         actions.add(actionBuilder.build());\r
134         actionBuilder = new ActionBuilder();\r
135         actionBuilder.setType(PopMpls.class);\r
136         etherType = new EthertypeActionBuilder();\r
137         etherType.setEthertype(new EtherType(18));\r
138         actionBuilder.addAugmentation(EthertypeAction.class, etherType.build());\r
139         actions.add(actionBuilder.build());\r
140         actionBuilder = new ActionBuilder();\r
141         actionBuilder.setType(SetQueue.class);\r
142         QueueIdActionBuilder queueId = new QueueIdActionBuilder();\r
143         queueId.setQueueId(1234L);\r
144         actionBuilder.addAugmentation(QueueIdAction.class, queueId.build());\r
145         actions.add(actionBuilder.build());\r
146         actionBuilder = new ActionBuilder();\r
147         actionBuilder.setType(Group.class);\r
148         GroupIdActionBuilder group = new GroupIdActionBuilder();\r
149         group.setGroupId(555L);\r
150         actionBuilder.addAugmentation(GroupIdAction.class, group.build());\r
151         actions.add(actionBuilder.build());\r
152         actionBuilder = new ActionBuilder();\r
153         actionBuilder.setType(SetNwTtl.class);\r
154         NwTtlActionBuilder nwTtl = new NwTtlActionBuilder();\r
155         nwTtl.setNwTtl((short) 8);\r
156         actionBuilder.addAugmentation(NwTtlAction.class, nwTtl.build());\r
157         actions.add(actionBuilder.build());\r
158         actionBuilder = new ActionBuilder();\r
159         actionBuilder.setType(DecNwTtl.class);\r
160         actions.add(actionBuilder.build());\r
161         actionBuilder = new ActionBuilder();\r
162         actionBuilder.setType(SetField.class);\r
163         OxmFieldsActionBuilder matchEntries = new OxmFieldsActionBuilder();\r
164         List<MatchEntries> entries = new ArrayList<>();\r
165         MatchEntriesBuilder matchBuilder = new MatchEntriesBuilder();\r
166         matchBuilder.setOxmClass(OpenflowBasicClass.class);\r
167         matchBuilder.setOxmMatchField(InPort.class);\r
168         matchBuilder.setHasMask(false);\r
169         PortNumberMatchEntryBuilder portBuilder = new PortNumberMatchEntryBuilder();\r
170         portBuilder.setPortNumber(new PortNumber(1L));\r
171         matchBuilder.addAugmentation(PortNumberMatchEntry.class, portBuilder.build());\r
172         entries.add(matchBuilder.build());\r
173         matchEntries.setMatchEntries(entries);\r
174         actionBuilder.addAugmentation(OxmFieldsAction.class, matchEntries.build());\r
175         actions.add(actionBuilder.build());\r
176         actionBuilder = new ActionBuilder();\r
177         actionBuilder.setType(PushPbb.class);\r
178         etherType = new EthertypeActionBuilder();\r
179         etherType.setEthertype(new EtherType(19));\r
180         actionBuilder.addAugmentation(EthertypeAction.class, etherType.build());\r
181         actions.add(actionBuilder.build());\r
182         actionBuilder = new ActionBuilder();\r
183         actionBuilder.setType(PopPbb.class);\r
184         actions.add(actionBuilder.build());\r
185         actionBuilder = new ActionBuilder();\r
186         actionBuilder.setType(Experimenter.class);\r
187         ExperimenterActionBuilder expBuilder = new ExperimenterActionBuilder();\r
188         expBuilder.setExperimenter(82L);\r
189         byte[] expData = new byte[]{0, 0, 0, 0, 0, 0, 0, 1};\r
190         expBuilder.setData(expData);\r
191         actionBuilder.addAugmentation(ExperimenterAction.class, expBuilder.build());\r
192         actions.add(actionBuilder.build());\r
193         actionBuilder = new ActionBuilder();\r
194         actionBuilder.setType(Experimenter.class);\r
195         expBuilder = new ExperimenterActionBuilder();\r
196         expBuilder.setExperimenter(102L);\r
197         actionBuilder.addAugmentation(ExperimenterAction.class, expBuilder.build());\r
198         actions.add(actionBuilder.build());\r
199         \r
200         ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();\r
201         CodingUtils.serializeList(actions, actionSerializer, out);\r
202         \r
203         Assert.assertEquals("Wrong action type", 0, out.readUnsignedShort());\r
204         Assert.assertEquals("Wrong action length", 16, out.readUnsignedShort());\r
205         Assert.assertEquals("Wrong action port", 42, out.readUnsignedInt());\r
206         Assert.assertEquals("Wrong action max-length", 52, out.readUnsignedShort());\r
207         out.skipBytes(6);\r
208         Assert.assertEquals("Wrong action type", 11, out.readUnsignedShort());\r
209         Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());\r
210         out.skipBytes(4);\r
211         Assert.assertEquals("Wrong action type", 12, out.readUnsignedShort());\r
212         Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());\r
213         out.skipBytes(4);\r
214         Assert.assertEquals("Wrong action type", 15, out.readUnsignedShort());\r
215         Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());\r
216         Assert.assertEquals("Wrong action mpls-ttl", 4, out.readUnsignedByte());\r
217         out.skipBytes(3);\r
218         Assert.assertEquals("Wrong action type", 16, out.readUnsignedShort());\r
219         Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());\r
220         out.skipBytes(4);\r
221         Assert.assertEquals("Wrong action type", 17, out.readUnsignedShort());\r
222         Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());\r
223         Assert.assertEquals("Wrong action ethertype", 16, out.readUnsignedShort());\r
224         out.skipBytes(2);\r
225         Assert.assertEquals("Wrong action type", 18, out.readUnsignedShort());\r
226         Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());\r
227         out.skipBytes(4);\r
228         Assert.assertEquals("Wrong action type", 19, out.readUnsignedShort());\r
229         Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());\r
230         Assert.assertEquals("Wrong action ethertype", 17, out.readUnsignedShort());\r
231         out.skipBytes(2);\r
232         Assert.assertEquals("Wrong action type", 20, out.readUnsignedShort());\r
233         Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());\r
234         Assert.assertEquals("Wrong action ethertype", 18, out.readUnsignedShort());\r
235         out.skipBytes(2);\r
236         Assert.assertEquals("Wrong action type", 21, out.readUnsignedShort());\r
237         Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());\r
238         Assert.assertEquals("Wrong action queue-id", 1234, out.readUnsignedInt());\r
239         Assert.assertEquals("Wrong action type", 22, out.readUnsignedShort());\r
240         Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());\r
241         Assert.assertEquals("Wrong action group", 555, out.readUnsignedInt());\r
242         Assert.assertEquals("Wrong action type", 23, out.readUnsignedShort());\r
243         Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());\r
244         Assert.assertEquals("Wrong action nw-ttl", 8, out.readUnsignedByte());\r
245         out.skipBytes(3);\r
246         Assert.assertEquals("Wrong action type", 24, out.readUnsignedShort());\r
247         Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());\r
248         out.skipBytes(4);\r
249         Assert.assertEquals("Wrong action type", 25, out.readUnsignedShort());\r
250         Assert.assertEquals("Wrong action length", 16, out.readUnsignedShort());\r
251         Assert.assertEquals("Wrong match entry class", 0x8000, out.readUnsignedShort());\r
252         Assert.assertEquals("Wrong match entry field & mask", 0, out.readUnsignedByte());\r
253         Assert.assertEquals("Wrong match entry length", 4, out.readUnsignedByte());\r
254         Assert.assertEquals("Wrong match entry value", 1, out.readUnsignedInt());\r
255         out.skipBytes(4);\r
256         Assert.assertEquals("Wrong action type", 26, out.readUnsignedShort());\r
257         Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());\r
258         Assert.assertEquals("Wrong action ethertype", 19, out.readUnsignedShort());\r
259         out.skipBytes(2);\r
260         Assert.assertEquals("Wrong action type", 27, out.readUnsignedShort());\r
261         Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());\r
262         out.skipBytes(4);\r
263         Assert.assertEquals("Wrong action type", 65535, out.readUnsignedShort());\r
264         Assert.assertEquals("Wrong action length", 16, out.readUnsignedShort());\r
265         Assert.assertEquals("Wrong experimenter", 82, out.readUnsignedInt());\r
266         byte[] tmp = new byte[8];\r
267         out.readBytes(tmp);\r
268         Assert.assertArrayEquals("Wrong data", expData, tmp);\r
269         Assert.assertEquals("Wrong action type", 65535, out.readUnsignedShort());\r
270         Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());\r
271         Assert.assertEquals("Wrong experimenter", 102, out.readUnsignedInt());\r
272         Assert.assertTrue("Unread data", out.readableBytes() == 0);\r
273     }\r
274 \r
275 }\r