2 * Copyright (c) 2017 Pantheon Technologies s.r.o. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.openflowplugin.impl.protocol.deserialization.multipart;
11 import static org.junit.Assert.assertEquals;
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.UnpooledByteBufAllocator;
15 import org.junit.Test;
16 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
17 import org.opendaylight.openflowjava.protocol.api.util.OxmMatchConstants;
18 import org.opendaylight.openflowjava.protocol.impl.util.InstructionConstants;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.TableFeaturesPropType;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.multipart.reply.multipart.reply.body.MultipartReplyTableFeatures;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.feature.prop.type.table.feature.prop.type.ApplyActions;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.feature.prop.type.table.feature.prop.type.ApplyActionsMiss;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.feature.prop.type.table.feature.prop.type.ApplySetfield;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.feature.prop.type.table.feature.prop.type.ApplySetfieldMiss;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.feature.prop.type.table.feature.prop.type.Instructions;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.feature.prop.type.table.feature.prop.type.InstructionsMiss;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.feature.prop.type.table.feature.prop.type.Match;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.feature.prop.type.table.feature.prop.type.NextTable;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.feature.prop.type.table.feature.prop.type.NextTableMiss;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.feature.prop.type.table.feature.prop.type.Wildcards;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.feature.prop.type.table.feature.prop.type.WriteActions;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.feature.prop.type.table.feature.prop.type.WriteActionsMiss;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.feature.prop.type.table.feature.prop.type.WriteSetfield;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.feature.prop.type.table.feature.prop.type.WriteSetfieldMiss;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.TableFeatures;
38 public class MultipartReplyTableFeaturesDeserializerTest extends AbstractMultipartDeserializerTest {
40 private static final byte PADDING_IN_MULTIPART_REPLY_TABLE_FEATURES = 5;
41 private static final byte MAX_TABLE_NAME_LENGTH = 32;
43 private static final byte TABLE_ID = 1;
44 private static final long METADATA_MATCH = 2;
45 private static final long METADATA_WRITE = 3;
46 private static final int TABLE_CONFIG = 3;
47 private static final int MAX_ENTRIES = 3;
49 private static final int OFPTFPT_INSTRUCTIONS = 0;
50 private static final int OFPTFPT_INSTRUCTIONS_MISS = 1;
51 private static final int OFPTFPT_NEXT_TABLES = 2;
52 private static final int OFPTFPT_NEXT_TABLES_MISS = 3;
53 private static final int OFPTFPT_WRITE_ACTIONS = 4;
54 private static final int OFPTFPT_WRITE_ACTIONS_MISS = 5;
55 private static final int OFPTFPT_APPLY_ACTIONS = 6;
56 private static final int OFPTFPT_APPLY_ACTIONS_MISS = 7;
57 private static final int OFPTFPT_MATCH = 8;
58 private static final int OFPTFPT_WILDCARDS = 10;
59 private static final int OFPTFPT_WRITE_SETFIELD = 12;
60 private static final int OFPTFPT_WRITE_SETFIELD_MISS = 13;
61 private static final int OFPTFPT_APPLY_SETFIELD = 14;
62 private static final int OFPTFPT_APPLY_SETFIELD_MISS = 15;
65 public void deserialize() {
66 ByteBuf buffer = UnpooledByteBufAllocator.DEFAULT.buffer();
68 writeValues(buffer, OFPTFPT_INSTRUCTIONS);
69 MultipartReplyTableFeatures reply = (MultipartReplyTableFeatures) deserializeMultipart(buffer);
71 TableFeatures firstTableFeature = reply.nonnullTableFeatures().values().iterator().next();
72 assertEquals(TABLE_ID, firstTableFeature.getTableId().byteValue());
73 assertEquals(METADATA_MATCH, firstTableFeature.getMetadataMatch().longValue());
74 assertEquals(METADATA_WRITE, firstTableFeature.getMetadataWrite().longValue());
75 assertEquals(MAX_ENTRIES, firstTableFeature.getMaxEntries().longValue());
76 assertEquals(MAX_ENTRIES, firstTableFeature.getMaxEntries().longValue());
77 assertEquals(Instructions.class, firstTableFeature.getTableProperties()
78 .nonnullTableFeatureProperties().values().iterator().next().getTableFeaturePropType()
79 .implementedInterface());
81 writeValues(buffer, OFPTFPT_INSTRUCTIONS_MISS);
82 reply = (MultipartReplyTableFeatures) deserializeMultipart(buffer);
84 assertEquals(InstructionsMiss.class, reply.nonnullTableFeatures().values().iterator().next()
85 .getTableProperties().nonnullTableFeatureProperties().values().iterator().next()
86 .getTableFeaturePropType().implementedInterface());
88 writeValues(buffer, OFPTFPT_NEXT_TABLES);
89 reply = (MultipartReplyTableFeatures) deserializeMultipart(buffer);
91 assertEquals(NextTable.class, reply.nonnullTableFeatures().values().iterator().next().getTableProperties()
92 .nonnullTableFeatureProperties().values().iterator().next().getTableFeaturePropType()
93 .implementedInterface());
95 writeValues(buffer, OFPTFPT_NEXT_TABLES_MISS);
96 reply = (MultipartReplyTableFeatures) deserializeMultipart(buffer);
98 assertEquals(NextTableMiss.class, reply.nonnullTableFeatures().values().iterator().next().getTableProperties()
99 .nonnullTableFeatureProperties().values().iterator().next().getTableFeaturePropType()
100 .implementedInterface());
102 writeValues(buffer, OFPTFPT_WRITE_ACTIONS);
103 reply = (MultipartReplyTableFeatures) deserializeMultipart(buffer);
105 assertEquals(WriteActions.class, reply.nonnullTableFeatures().values().iterator().next().getTableProperties()
106 .nonnullTableFeatureProperties().values().iterator().next().getTableFeaturePropType()
107 .implementedInterface());
109 writeValues(buffer, OFPTFPT_WRITE_ACTIONS_MISS);
110 reply = (MultipartReplyTableFeatures) deserializeMultipart(buffer);
112 assertEquals(WriteActionsMiss.class, reply.nonnullTableFeatures().values().iterator().next()
113 .getTableProperties().nonnullTableFeatureProperties().values().iterator().next()
114 .getTableFeaturePropType().implementedInterface());
116 writeValues(buffer, OFPTFPT_APPLY_ACTIONS);
117 reply = (MultipartReplyTableFeatures) deserializeMultipart(buffer);
119 assertEquals(ApplyActions.class, reply.nonnullTableFeatures().values().iterator().next().getTableProperties()
120 .nonnullTableFeatureProperties().values().iterator().next().getTableFeaturePropType()
121 .implementedInterface());
123 writeValues(buffer, OFPTFPT_APPLY_ACTIONS_MISS);
124 reply = (MultipartReplyTableFeatures) deserializeMultipart(buffer);
126 assertEquals(ApplyActionsMiss.class, reply.nonnullTableFeatures().values().iterator().next()
127 .getTableProperties().nonnullTableFeatureProperties().values().iterator().next()
128 .getTableFeaturePropType().implementedInterface());
130 writeValues(buffer, OFPTFPT_MATCH);
131 reply = (MultipartReplyTableFeatures) deserializeMultipart(buffer);
133 assertEquals(Match.class, reply.nonnullTableFeatures().values().iterator().next().getTableProperties()
134 .nonnullTableFeatureProperties().values().iterator().next().getTableFeaturePropType()
135 .implementedInterface());
137 writeValues(buffer, OFPTFPT_WILDCARDS);
138 reply = (MultipartReplyTableFeatures) deserializeMultipart(buffer);
140 assertEquals(Wildcards.class, reply.nonnullTableFeatures().values().iterator().next().getTableProperties()
141 .nonnullTableFeatureProperties().values().iterator().next().getTableFeaturePropType()
142 .implementedInterface());
144 writeValues(buffer, OFPTFPT_WRITE_SETFIELD);
145 reply = (MultipartReplyTableFeatures) deserializeMultipart(buffer);
147 assertEquals(WriteSetfield.class, reply.nonnullTableFeatures().values().iterator().next().getTableProperties()
148 .nonnullTableFeatureProperties().values().iterator().next().getTableFeaturePropType()
149 .implementedInterface());
151 writeValues(buffer, OFPTFPT_WRITE_SETFIELD_MISS);
152 reply = (MultipartReplyTableFeatures) deserializeMultipart(buffer);
154 assertEquals(WriteSetfieldMiss.class, reply.nonnullTableFeatures().values().iterator().next()
155 .getTableProperties().nonnullTableFeatureProperties().values().iterator().next()
156 .getTableFeaturePropType().implementedInterface());
158 writeValues(buffer, OFPTFPT_APPLY_SETFIELD);
159 reply = (MultipartReplyTableFeatures) deserializeMultipart(buffer);
161 assertEquals(ApplySetfield.class, reply.nonnullTableFeatures().values().iterator().next().getTableProperties()
162 .nonnullTableFeatureProperties().values().iterator().next().getTableFeaturePropType()
163 .implementedInterface());
165 writeValues(buffer, OFPTFPT_APPLY_SETFIELD_MISS);
166 reply = (MultipartReplyTableFeatures) deserializeMultipart(buffer);
168 assertEquals(ApplySetfieldMiss.class, reply.nonnullTableFeatures().values().iterator().next()
169 .getTableProperties().nonnullTableFeatureProperties().values().iterator().next()
170 .getTableFeaturePropType().implementedInterface());
171 assertEquals(0, buffer.readableBytes());
175 private static void writeValues(final ByteBuf buffer, final int propertyType) {
177 final int replyIndex = buffer.readerIndex();
178 buffer.writeShort(EncodeConstants.EMPTY_LENGTH);
179 buffer.writeByte(TABLE_ID);
180 buffer.writeZero(PADDING_IN_MULTIPART_REPLY_TABLE_FEATURES);
181 buffer.writeZero(MAX_TABLE_NAME_LENGTH);
182 buffer.writeLong(METADATA_MATCH);
183 buffer.writeLong(METADATA_WRITE);
184 buffer.writeInt(TABLE_CONFIG);
185 buffer.writeInt(MAX_ENTRIES);
187 final int propIndex = buffer.writerIndex();
188 buffer.writeShort(propertyType);
190 final int propLengthIndex = buffer.writerIndex();
191 buffer.writeShort(EncodeConstants.EMPTY_LENGTH);
193 TableFeaturesPropType propType = TableFeaturesPropType.forValue(propertyType);
196 case OFPTFPTINSTRUCTIONS:
197 case OFPTFPTINSTRUCTIONSMISS:
198 int instructionStartIndex = buffer.writerIndex();
199 buffer.writeShort(InstructionConstants.APPLY_ACTIONS_TYPE);
200 int instructionLengthIndex = buffer.writerIndex();
201 buffer.writeShort(EncodeConstants.EMPTY_LENGTH);
202 buffer.setShort(instructionLengthIndex, buffer.writerIndex() - instructionStartIndex);
204 case OFPTFPTNEXTTABLES:
205 case OFPTFPTNEXTTABLESMISS:
209 case OFPTFPTWRITEACTIONS:
210 case OFPTFPTWRITEACTIONSMISS:
211 buffer.writeShort(InstructionConstants.WRITE_ACTIONS_TYPE);
212 buffer.writeShort(EncodeConstants.EMPTY_LENGTH);
214 case OFPTFPTAPPLYACTIONS:
215 case OFPTFPTAPPLYACTIONSMISS:
216 buffer.writeShort(InstructionConstants.APPLY_ACTIONS_TYPE);
217 buffer.writeShort(EncodeConstants.EMPTY_LENGTH);
220 buffer.writeShort(OxmMatchConstants.OPENFLOW_BASIC_CLASS);
221 buffer.writeByte(OxmMatchConstants.ARP_OP << 1);
222 buffer.writeByte(EncodeConstants.EMPTY_LENGTH);
224 case OFPTFPTWILDCARDS:
225 buffer.writeShort(OxmMatchConstants.OPENFLOW_BASIC_CLASS);
226 buffer.writeByte(OxmMatchConstants.ARP_SHA << 1);
227 buffer.writeByte(EncodeConstants.EMPTY_LENGTH);
229 case OFPTFPTWRITESETFIELD:
230 buffer.writeShort(OxmMatchConstants.OPENFLOW_BASIC_CLASS);
231 buffer.writeByte(OxmMatchConstants.ARP_SPA << 1);
232 buffer.writeByte(EncodeConstants.EMPTY_LENGTH);
234 case OFPTFPTWRITESETFIELDMISS:
235 buffer.writeShort(OxmMatchConstants.OPENFLOW_BASIC_CLASS);
236 buffer.writeByte(OxmMatchConstants.ARP_THA << 1);
237 buffer.writeByte(EncodeConstants.EMPTY_LENGTH);
239 case OFPTFPTAPPLYSETFIELD:
240 buffer.writeShort(OxmMatchConstants.OPENFLOW_BASIC_CLASS);
241 buffer.writeByte(OxmMatchConstants.ARP_TPA << 1);
242 buffer.writeByte(EncodeConstants.EMPTY_LENGTH);
244 case OFPTFPTAPPLYSETFIELDMISS:
245 buffer.writeShort(OxmMatchConstants.OPENFLOW_BASIC_CLASS);
246 buffer.writeByte(OxmMatchConstants.ETH_TYPE << 1);
247 buffer.writeByte(EncodeConstants.EMPTY_LENGTH);
253 int propLength = buffer.writerIndex() - propIndex;
254 buffer.setShort(propLengthIndex, buffer.writerIndex() - propIndex);
256 int paddingRemainder = propLength % EncodeConstants.PADDING;
258 if (paddingRemainder != 0) {
259 result = EncodeConstants.PADDING - paddingRemainder;
261 buffer.writeZero(result);
263 buffer.setShort(replyIndex, buffer.writerIndex() - replyIndex);
267 protected int getType() {
268 return MultipartType.OFPMPTABLEFEATURES.getIntValue();