Default experimenters moved to separate bundle + unit tests
[openflowjava.git] / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / serialization / factories / multipart / TableFeaturesTest.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.serialization.factories.multipart;\r
9 \r
10 import io.netty.buffer.ByteBuf;\r
11 import io.netty.buffer.UnpooledByteBufAllocator;\r
12 \r
13 import java.math.BigInteger;\r
14 import java.util.ArrayList;\r
15 import java.util.List;\r
16 \r
17 import org.junit.Assert;\r
18 import org.junit.Before;\r
19 import org.junit.Test;\r
20 import org.opendaylight.openflowjava.protocol.api.extensibility.MessageTypeKey;\r
21 import org.opendaylight.openflowjava.protocol.api.extensibility.OFSerializer;\r
22 import org.opendaylight.openflowjava.protocol.api.extensibility.SerializerRegistry;\r
23 import org.opendaylight.openflowjava.protocol.impl.serialization.SerializerRegistryImpl;\r
24 import org.opendaylight.openflowjava.protocol.impl.serialization.factories.MultipartRequestInputFactory;\r
25 import org.opendaylight.openflowjava.protocol.impl.serialization.factories.MultipartRequestInputFactoryTest;\r
26 import org.opendaylight.openflowjava.protocol.impl.util.BufferHelper;\r
27 import org.opendaylight.openflowjava.util.ByteBufUtils;\r
28 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;\r
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.InstructionRelatedTableFeatureProperty;\r
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.InstructionRelatedTableFeaturePropertyBuilder;\r
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.NextTableRelatedTableFeatureProperty;\r
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.NextTableRelatedTableFeaturePropertyBuilder;\r
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.OxmRelatedTableFeatureProperty;\r
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.OxmRelatedTableFeaturePropertyBuilder;\r
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.table.features.properties.container.table.feature.properties.NextTableIds;\r
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.table.features.properties.container.table.feature.properties.NextTableIdsBuilder;\r
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.ApplyActions;\r
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.ClearActions;\r
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.GotoTable;\r
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.Meter;\r
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.WriteActions;\r
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.WriteMetadata;\r
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instructions.grouping.Instruction;\r
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instructions.grouping.InstructionBuilder;\r
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartRequestFlags;\r
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;\r
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.TableConfig;\r
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.TableFeaturesPropType;\r
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.InPhyPort;\r
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.InPort;\r
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.IpEcn;\r
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.IpProto;\r
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.OpenflowBasicClass;\r
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.oxm.fields.grouping.MatchEntries;\r
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.oxm.fields.grouping.MatchEntriesBuilder;\r
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartRequestInput;\r
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartRequestInputBuilder;\r
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestTableFeaturesCaseBuilder;\r
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.multipart.request.table.features._case.MultipartRequestTableFeaturesBuilder;\r
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.multipart.request.table.features._case.multipart.request.table.features.TableFeatures;\r
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.multipart.request.table.features._case.multipart.request.table.features.TableFeaturesBuilder;\r
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.table.features.properties.grouping.TableFeatureProperties;\r
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.table.features.properties.grouping.TableFeaturePropertiesBuilder;\r
64 \r
65 /**\r
66  * @author michal.polkorab\r
67  *\r
68  */\r
69 public class TableFeaturesTest {\r
70     private static final byte PADDING_IN_MULTIPART_REQUEST_MESSAGE =\r
71             MultipartRequestInputFactoryTest.PADDING_IN_MULTIPART_REQUEST_MESSAGE;\r
72     private SerializerRegistry registry;\r
73     private OFSerializer<MultipartRequestInput> multipartFactory;\r
74 \r
75     /**\r
76      * Initializes serializer registry and stores correct factory in field\r
77      */\r
78     @Before\r
79     public void startUp() {\r
80         registry = new SerializerRegistryImpl();\r
81         registry.init();\r
82         multipartFactory = registry.getSerializer(\r
83                 new MessageTypeKey<>(EncodeConstants.OF13_VERSION_ID, MultipartRequestInput.class));\r
84     }\r
85 \r
86     /**\r
87      * @throws Exception\r
88      * Testing of {@link MultipartRequestInputFactory} for correct translation from POJO\r
89      */\r
90     @Test\r
91     public void testMultipartRequestTableFeaturesMessageFactory() throws Exception {\r
92         MultipartRequestInputBuilder builder = new MultipartRequestInputBuilder();\r
93         BufferHelper.setupHeader(builder, EncodeConstants.OF13_VERSION_ID);\r
94         builder.setType(MultipartType.forValue(12));\r
95         builder.setFlags(new MultipartRequestFlags(true));\r
96         MultipartRequestTableFeaturesCaseBuilder caseBuilder = new MultipartRequestTableFeaturesCaseBuilder();\r
97         MultipartRequestTableFeaturesBuilder featuresBuilder = new MultipartRequestTableFeaturesBuilder();\r
98         List<TableFeatures> tableFeaturesList = new ArrayList<>();\r
99         TableFeaturesBuilder tableFeaturesBuilder = new TableFeaturesBuilder();\r
100         tableFeaturesBuilder.setTableId((short) 8);\r
101         tableFeaturesBuilder.setName("AAAABBBBCCCCDDDDEEEEFFFFGGGG");\r
102         tableFeaturesBuilder.setMetadataMatch(new BigInteger(new byte[] {0x00, 0x01, 0x02, 0x03, 0x01, 0x04, 0x08, 0x01}));\r
103         tableFeaturesBuilder.setMetadataWrite(new BigInteger(new byte[] {0x00, 0x07, 0x01, 0x05, 0x01, 0x00, 0x03, 0x01}));\r
104         tableFeaturesBuilder.setConfig(new TableConfig(true));\r
105         tableFeaturesBuilder.setMaxEntries(65L);\r
106         List<TableFeatureProperties> properties = new ArrayList<>();\r
107         TableFeaturePropertiesBuilder propBuilder = new TableFeaturePropertiesBuilder();\r
108         propBuilder.setType(TableFeaturesPropType.OFPTFPTNEXTTABLES);\r
109         NextTableRelatedTableFeaturePropertyBuilder nextPropBuilder =\r
110                 new NextTableRelatedTableFeaturePropertyBuilder();\r
111         List<NextTableIds> nextIds = new ArrayList<>();\r
112         nextIds.add(new NextTableIdsBuilder().setTableId((short) 1).build());\r
113         nextIds.add(new NextTableIdsBuilder().setTableId((short) 2).build());\r
114         nextPropBuilder.setNextTableIds(nextIds);\r
115         propBuilder.addAugmentation(NextTableRelatedTableFeatureProperty.class, nextPropBuilder.build());\r
116         properties.add(propBuilder.build());\r
117         propBuilder = new TableFeaturePropertiesBuilder();\r
118         propBuilder.setType(TableFeaturesPropType.OFPTFPTNEXTTABLESMISS);\r
119         nextPropBuilder = new NextTableRelatedTableFeaturePropertyBuilder();\r
120         nextIds = new ArrayList<>();\r
121         nextIds.add(new NextTableIdsBuilder().setTableId((short) 3).build());\r
122         nextPropBuilder.setNextTableIds(nextIds);\r
123         propBuilder.addAugmentation(NextTableRelatedTableFeatureProperty.class, nextPropBuilder.build());\r
124         properties.add(propBuilder.build());\r
125         propBuilder = new TableFeaturePropertiesBuilder();\r
126         propBuilder.setType(TableFeaturesPropType.OFPTFPTINSTRUCTIONS);\r
127         InstructionRelatedTableFeaturePropertyBuilder insPropBuilder =\r
128                 new InstructionRelatedTableFeaturePropertyBuilder();\r
129         List<Instruction> insIds = new ArrayList<>();\r
130         InstructionBuilder insBuilder = new InstructionBuilder();\r
131         insBuilder.setType(WriteActions.class);\r
132         insIds.add(insBuilder.build());\r
133         insBuilder = new InstructionBuilder();\r
134         insBuilder.setType(GotoTable.class);\r
135         insIds.add(insBuilder.build());\r
136         insPropBuilder.setInstruction(insIds);\r
137         propBuilder.addAugmentation(InstructionRelatedTableFeatureProperty.class, insPropBuilder.build());\r
138         properties.add(propBuilder.build());\r
139         propBuilder = new TableFeaturePropertiesBuilder();\r
140         propBuilder.setType(TableFeaturesPropType.OFPTFPTINSTRUCTIONSMISS);\r
141         insPropBuilder = new InstructionRelatedTableFeaturePropertyBuilder();\r
142         insIds = new ArrayList<>();\r
143         insBuilder = new InstructionBuilder();\r
144         insBuilder.setType(WriteMetadata.class);\r
145         insIds.add(insBuilder.build());\r
146         insBuilder = new InstructionBuilder();\r
147         insBuilder.setType(ApplyActions.class);\r
148         insIds.add(insBuilder.build());\r
149         insBuilder = new InstructionBuilder();\r
150         insBuilder.setType(Meter.class);\r
151         insIds.add(insBuilder.build());\r
152         insBuilder = new InstructionBuilder();\r
153         insBuilder.setType(ClearActions.class);\r
154         insIds.add(insBuilder.build());\r
155         insBuilder = new InstructionBuilder();\r
156         insBuilder.setType(GotoTable.class);\r
157         insIds.add(insBuilder.build());\r
158         insPropBuilder.setInstruction(insIds);\r
159         propBuilder.addAugmentation(InstructionRelatedTableFeatureProperty.class, insPropBuilder.build());\r
160         properties.add(propBuilder.build());\r
161         tableFeaturesBuilder.setTableFeatureProperties(properties);\r
162         tableFeaturesList.add(tableFeaturesBuilder.build());\r
163         tableFeaturesBuilder = new TableFeaturesBuilder();\r
164         tableFeaturesBuilder.setTableId((short) 8);\r
165         tableFeaturesBuilder.setName("AAAABBBBCCCCDDDDEEEEFFFFGGGG");\r
166         tableFeaturesBuilder.setMetadataMatch(new BigInteger(new byte[] {0x00, 0x01, 0x02, 0x03, 0x01, 0x04, 0x08, 0x01}));\r
167         tableFeaturesBuilder.setMetadataWrite(new BigInteger(new byte[] {0x00, 0x07, 0x01, 0x05, 0x01, 0x00, 0x03, 0x01}));\r
168         tableFeaturesBuilder.setConfig(new TableConfig(true));\r
169         tableFeaturesBuilder.setMaxEntries(67L);\r
170         properties = new ArrayList<>();\r
171         propBuilder = new TableFeaturePropertiesBuilder();\r
172         propBuilder.setType(TableFeaturesPropType.OFPTFPTMATCH);\r
173         OxmRelatedTableFeaturePropertyBuilder oxmBuilder = new OxmRelatedTableFeaturePropertyBuilder();\r
174         List<MatchEntries> entries = new ArrayList<>();\r
175         MatchEntriesBuilder entriesBuilder = new MatchEntriesBuilder();\r
176         entriesBuilder.setOxmClass(OpenflowBasicClass.class);\r
177         entriesBuilder.setOxmMatchField(InPhyPort.class);\r
178         entriesBuilder.setHasMask(false);\r
179         entries.add(entriesBuilder.build());\r
180         entriesBuilder = new MatchEntriesBuilder();\r
181         entriesBuilder.setOxmClass(OpenflowBasicClass.class);\r
182         entriesBuilder.setOxmMatchField(InPort.class);\r
183         entriesBuilder.setHasMask(false);\r
184         entries.add(entriesBuilder.build());\r
185         oxmBuilder.setMatchEntries(entries);\r
186         propBuilder.addAugmentation(OxmRelatedTableFeatureProperty.class, oxmBuilder.build());\r
187         properties.add(propBuilder.build());\r
188         propBuilder = new TableFeaturePropertiesBuilder();\r
189         propBuilder.setType(TableFeaturesPropType.OFPTFPTAPPLYSETFIELD);\r
190         oxmBuilder = new OxmRelatedTableFeaturePropertyBuilder();\r
191         entries = new ArrayList<>();\r
192         entriesBuilder = new MatchEntriesBuilder();\r
193         entriesBuilder.setOxmClass(OpenflowBasicClass.class);\r
194         entriesBuilder.setOxmMatchField(IpProto.class);\r
195         entriesBuilder.setHasMask(false);\r
196         entries.add(entriesBuilder.build());\r
197         entriesBuilder = new MatchEntriesBuilder();\r
198         entriesBuilder.setOxmClass(OpenflowBasicClass.class);\r
199         entriesBuilder.setOxmMatchField(IpEcn.class);\r
200         entriesBuilder.setHasMask(false);\r
201         entries.add(entriesBuilder.build());\r
202         oxmBuilder.setMatchEntries(entries);\r
203         propBuilder.addAugmentation(OxmRelatedTableFeatureProperty.class, oxmBuilder.build());\r
204         properties.add(propBuilder.build());\r
205         tableFeaturesBuilder.setTableFeatureProperties(properties);\r
206         tableFeaturesList.add(tableFeaturesBuilder.build());\r
207         featuresBuilder.setTableFeatures(tableFeaturesList);\r
208         caseBuilder.setMultipartRequestTableFeatures(featuresBuilder.build());\r
209         builder.setMultipartRequestBody(caseBuilder.build());\r
210         MultipartRequestInput message = builder.build();\r
211 \r
212         ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();\r
213         multipartFactory.serialize(message, out);\r
214 \r
215         BufferHelper.checkHeaderV13(out, (byte) 18, 232);\r
216         Assert.assertEquals("Wrong type", 12, out.readUnsignedShort());\r
217         Assert.assertEquals("Wrong flags", 1, out.readUnsignedShort());\r
218         out.skipBytes(PADDING_IN_MULTIPART_REQUEST_MESSAGE);\r
219         Assert.assertEquals("Wrong length", 120, out.readUnsignedShort());\r
220         Assert.assertEquals("Wrong registry-id", 8, out.readUnsignedByte());\r
221         out.skipBytes(5);\r
222         Assert.assertEquals("Wrong name", "AAAABBBBCCCCDDDDEEEEFFFFGGGG",\r
223                 ByteBufUtils.decodeNullTerminatedString(out, 32));\r
224         byte[] metadataMatch = new byte[EncodeConstants.SIZE_OF_LONG_IN_BYTES];\r
225         out.readBytes(metadataMatch);\r
226         Assert.assertArrayEquals("Wrong metadata-match",\r
227                 new byte[] {0x00, 0x01, 0x02, 0x03, 0x01, 0x04, 0x08, 0x01}, metadataMatch);\r
228         byte[] metadataWrite = new byte[EncodeConstants.SIZE_OF_LONG_IN_BYTES];\r
229         out.readBytes(metadataWrite);\r
230         Assert.assertArrayEquals("Wrong metadata-write",\r
231                 new byte[] {0x00, 0x07, 0x01, 0x05, 0x01, 0x00, 0x03, 0x01}, metadataWrite);\r
232         Assert.assertEquals("Wrong config", 8, out.readUnsignedInt());\r
233         Assert.assertEquals("Wrong max-entries", 65, out.readUnsignedInt());\r
234         Assert.assertEquals("Wrong property type", 2, out.readUnsignedShort());\r
235         Assert.assertEquals("Wrong property length", 6, out.readUnsignedShort());\r
236         Assert.assertEquals("Wrong next-registry-id", 1, out.readUnsignedByte());\r
237         Assert.assertEquals("Wrong next-registry-id", 2, out.readUnsignedByte());\r
238         out.skipBytes(2);\r
239         Assert.assertEquals("Wrong property type", 3, out.readUnsignedShort());\r
240         Assert.assertEquals("Wrong property length", 5, out.readUnsignedShort());\r
241         Assert.assertEquals("Wrong next-registry-id", 3, out.readUnsignedByte());\r
242         out.skipBytes(3);\r
243         Assert.assertEquals("Wrong property type", 0, out.readUnsignedShort());\r
244         Assert.assertEquals("Wrong property length", 12, out.readUnsignedShort());\r
245         Assert.assertEquals("Wrong instruction type", 3, out.readUnsignedShort());\r
246         Assert.assertEquals("Wrong instruction length", 4, out.readUnsignedShort());\r
247         Assert.assertEquals("Wrong instruction type", 1, out.readUnsignedShort());\r
248         Assert.assertEquals("Wrong instruction length", 4, out.readUnsignedShort());\r
249         out.skipBytes(4);\r
250         Assert.assertEquals("Wrong property type", 1, out.readUnsignedShort());\r
251         Assert.assertEquals("Wrong property length", 24, out.readUnsignedShort());\r
252         Assert.assertEquals("Wrong instruction type", 2, out.readUnsignedShort());\r
253         Assert.assertEquals("Wrong instruction length", 4, out.readUnsignedShort());\r
254         Assert.assertEquals("Wrong instruction type", 4, out.readUnsignedShort());\r
255         Assert.assertEquals("Wrong instruction length", 4, out.readUnsignedShort());\r
256         Assert.assertEquals("Wrong instruction type", 6, out.readUnsignedShort());\r
257         Assert.assertEquals("Wrong instruction length", 4, out.readUnsignedShort());\r
258         Assert.assertEquals("Wrong instruction type", 5, out.readUnsignedShort());\r
259         Assert.assertEquals("Wrong instruction length", 4, out.readUnsignedShort());\r
260         Assert.assertEquals("Wrong instruction type", 1, out.readUnsignedShort());\r
261         Assert.assertEquals("Wrong instruction length", 4, out.readUnsignedShort());\r
262         Assert.assertEquals("Wrong length", 96, out.readUnsignedShort());\r
263         Assert.assertEquals("Wrong registry-id", 8, out.readUnsignedByte());\r
264         out.skipBytes(5);\r
265         Assert.assertEquals("Wrong name", "AAAABBBBCCCCDDDDEEEEFFFFGGGG",\r
266                 ByteBufUtils.decodeNullTerminatedString(out, 32));\r
267         metadataMatch = new byte[EncodeConstants.SIZE_OF_LONG_IN_BYTES];\r
268         out.readBytes(metadataMatch);\r
269         Assert.assertArrayEquals("Wrong metadata-match",\r
270                 new byte[] {0x00, 0x01, 0x02, 0x03, 0x01, 0x04, 0x08, 0x01}, metadataMatch);\r
271         metadataWrite = new byte[EncodeConstants.SIZE_OF_LONG_IN_BYTES];\r
272         out.readBytes(metadataWrite);\r
273         Assert.assertArrayEquals("Wrong metadata-write",\r
274                 new byte[] {0x00, 0x07, 0x01, 0x05, 0x01, 0x00, 0x03, 0x01}, metadataWrite);\r
275         Assert.assertEquals("Wrong config", 8, out.readUnsignedInt());\r
276         Assert.assertEquals("Wrong max-entries", 67, out.readUnsignedInt());\r
277         Assert.assertEquals("Wrong property type", 8, out.readUnsignedShort());\r
278         Assert.assertEquals("Wrong property length", 12, out.readUnsignedShort());\r
279         Assert.assertEquals("Wrong match class", 0x8000, out.readUnsignedShort());\r
280         Assert.assertEquals("Wrong match field&mask", 2, out.readUnsignedByte());\r
281         Assert.assertEquals("Wrong match length", 4, out.readUnsignedByte());\r
282         Assert.assertEquals("Wrong match class", 0x8000, out.readUnsignedShort());\r
283         Assert.assertEquals("Wrong match field&mask", 0, out.readUnsignedByte());\r
284         Assert.assertEquals("Wrong match length", 4, out.readUnsignedByte());\r
285         out.skipBytes(4);\r
286         Assert.assertEquals("Wrong property type", 14, out.readUnsignedShort());\r
287         Assert.assertEquals("Wrong property length", 12, out.readUnsignedShort());\r
288         Assert.assertEquals("Wrong match class", 0x8000, out.readUnsignedShort());\r
289         Assert.assertEquals("Wrong match field&mask", 20, out.readUnsignedByte());\r
290         Assert.assertEquals("Wrong match length", 1, out.readUnsignedByte());\r
291         Assert.assertEquals("Wrong match class", 0x8000, out.readUnsignedShort());\r
292         Assert.assertEquals("Wrong match field&mask", 18, out.readUnsignedByte());\r
293         Assert.assertEquals("Wrong match length", 1, out.readUnsignedByte());\r
294         out.skipBytes(4);\r
295         Assert.assertTrue("Unread data", out.readableBytes() == 0);\r
296     }\r
297 \r
298 }\r