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