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;
37 public class MultipartReplyTableFeaturesDeserializerTest extends AbstractMultipartDeserializerTest {
39 private static final byte PADDING_IN_MULTIPART_REPLY_TABLE_FEATURES = 5;
40 private static final byte MAX_TABLE_NAME_LENGTH = 32;
42 private static final byte TABLE_ID = 1;
43 private static final long METADATA_MATCH = 2;
44 private static final long METADATA_WRITE = 3;
45 private static final int TABLE_CONFIG = 3;
46 private static final int MAX_ENTRIES = 3;
48 private static final int OFPTFPT_INSTRUCTIONS = 0;
49 private static final int OFPTFPT_INSTRUCTIONS_MISS = 1;
50 private static final int OFPTFPT_NEXT_TABLES = 2;
51 private static final int OFPTFPT_NEXT_TABLES_MISS = 3;
52 private static final int OFPTFPT_WRITE_ACTIONS = 4;
53 private static final int OFPTFPT_WRITE_ACTIONS_MISS = 5;
54 private static final int OFPTFPT_APPLY_ACTIONS = 6;
55 private static final int OFPTFPT_APPLY_ACTIONS_MISS = 7;
56 private static final int OFPTFPT_MATCH = 8;
57 private static final int OFPTFPT_WILDCARDS = 10;
58 private static final int OFPTFPT_WRITE_SETFIELD = 12;
59 private static final int OFPTFPT_WRITE_SETFIELD_MISS = 13;
60 private static final int OFPTFPT_APPLY_SETFIELD = 14;
61 private static final int OFPTFPT_APPLY_SETFIELD_MISS = 15;
64 public void deserialize() {
65 ByteBuf buffer = UnpooledByteBufAllocator.DEFAULT.buffer();
67 writeValues(buffer, OFPTFPT_INSTRUCTIONS);
68 MultipartReplyTableFeatures reply = (MultipartReplyTableFeatures) deserializeMultipart(buffer);
70 assertEquals(TABLE_ID, reply.getTableFeatures().get(0).getTableId().byteValue());
71 assertEquals(METADATA_MATCH, reply.getTableFeatures().get(0).getMetadataMatch().longValue());
72 assertEquals(METADATA_WRITE, reply.getTableFeatures().get(0).getMetadataWrite().longValue());
73 assertEquals(MAX_ENTRIES, reply.getTableFeatures().get(0).getMaxEntries().longValue());
74 assertEquals(MAX_ENTRIES, reply.getTableFeatures().get(0).getMaxEntries().longValue());
75 assertEquals(Instructions.class, reply.getTableFeatures().get(0).getTableProperties()
76 .getTableFeatureProperties().get(0).getTableFeaturePropType().implementedInterface());
78 writeValues(buffer, OFPTFPT_INSTRUCTIONS_MISS);
79 reply = (MultipartReplyTableFeatures) deserializeMultipart(buffer);
81 assertEquals(InstructionsMiss.class, reply.getTableFeatures().get(0).getTableProperties()
82 .getTableFeatureProperties().get(0).getTableFeaturePropType().implementedInterface());
84 writeValues(buffer, OFPTFPT_NEXT_TABLES);
85 reply = (MultipartReplyTableFeatures) deserializeMultipart(buffer);
87 assertEquals(NextTable.class, reply.getTableFeatures().get(0).getTableProperties()
88 .getTableFeatureProperties().get(0).getTableFeaturePropType().implementedInterface());
90 writeValues(buffer, OFPTFPT_NEXT_TABLES_MISS);
91 reply = (MultipartReplyTableFeatures) deserializeMultipart(buffer);
93 assertEquals(NextTableMiss.class, reply.getTableFeatures().get(0).getTableProperties()
94 .getTableFeatureProperties().get(0).getTableFeaturePropType().implementedInterface());
96 writeValues(buffer, OFPTFPT_WRITE_ACTIONS);
97 reply = (MultipartReplyTableFeatures) deserializeMultipart(buffer);
99 assertEquals(WriteActions.class, reply.getTableFeatures().get(0).getTableProperties()
100 .getTableFeatureProperties().get(0).getTableFeaturePropType().implementedInterface());
102 writeValues(buffer, OFPTFPT_WRITE_ACTIONS_MISS);
103 reply = (MultipartReplyTableFeatures) deserializeMultipart(buffer);
105 assertEquals(WriteActionsMiss.class, reply.getTableFeatures().get(0).getTableProperties()
106 .getTableFeatureProperties().get(0).getTableFeaturePropType().implementedInterface());
108 writeValues(buffer, OFPTFPT_APPLY_ACTIONS);
109 reply = (MultipartReplyTableFeatures) deserializeMultipart(buffer);
111 assertEquals(ApplyActions.class, reply.getTableFeatures().get(0).getTableProperties()
112 .getTableFeatureProperties().get(0).getTableFeaturePropType().implementedInterface());
114 writeValues(buffer, OFPTFPT_APPLY_ACTIONS_MISS);
115 reply = (MultipartReplyTableFeatures) deserializeMultipart(buffer);
117 assertEquals(ApplyActionsMiss.class, reply.getTableFeatures().get(0).getTableProperties()
118 .getTableFeatureProperties().get(0).getTableFeaturePropType().implementedInterface());
120 writeValues(buffer, OFPTFPT_MATCH);
121 reply = (MultipartReplyTableFeatures) deserializeMultipart(buffer);
123 assertEquals(Match.class, reply.getTableFeatures().get(0).getTableProperties()
124 .getTableFeatureProperties().get(0).getTableFeaturePropType().implementedInterface());
126 writeValues(buffer, OFPTFPT_WILDCARDS);
127 reply = (MultipartReplyTableFeatures) deserializeMultipart(buffer);
129 assertEquals(Wildcards.class, reply.getTableFeatures().get(0).getTableProperties()
130 .getTableFeatureProperties().get(0).getTableFeaturePropType().implementedInterface());
132 writeValues(buffer, OFPTFPT_WRITE_SETFIELD);
133 reply = (MultipartReplyTableFeatures) deserializeMultipart(buffer);
135 assertEquals(WriteSetfield.class, reply.getTableFeatures().get(0).getTableProperties()
136 .getTableFeatureProperties().get(0).getTableFeaturePropType().implementedInterface());
138 writeValues(buffer, OFPTFPT_WRITE_SETFIELD_MISS);
139 reply = (MultipartReplyTableFeatures) deserializeMultipart(buffer);
141 assertEquals(WriteSetfieldMiss.class, reply.getTableFeatures().get(0).getTableProperties()
142 .getTableFeatureProperties().get(0).getTableFeaturePropType().implementedInterface());
144 writeValues(buffer, OFPTFPT_APPLY_SETFIELD);
145 reply = (MultipartReplyTableFeatures) deserializeMultipart(buffer);
147 assertEquals(ApplySetfield.class, reply.getTableFeatures().get(0).getTableProperties()
148 .getTableFeatureProperties().get(0).getTableFeaturePropType().implementedInterface());
150 writeValues(buffer, OFPTFPT_APPLY_SETFIELD_MISS);
151 reply = (MultipartReplyTableFeatures) deserializeMultipart(buffer);
153 assertEquals(ApplySetfieldMiss.class, reply.getTableFeatures().get(0).getTableProperties()
154 .getTableFeatureProperties().get(0).getTableFeaturePropType().implementedInterface());
155 assertEquals(0, buffer.readableBytes());
158 private void writeValues(ByteBuf buffer, int propertyType) {
160 final int replyIndex = buffer.readerIndex();
161 buffer.writeShort(EncodeConstants.EMPTY_LENGTH);
162 buffer.writeByte(TABLE_ID);
163 buffer.writeZero(PADDING_IN_MULTIPART_REPLY_TABLE_FEATURES);
164 buffer.writeZero(MAX_TABLE_NAME_LENGTH);
165 buffer.writeLong(METADATA_MATCH);
166 buffer.writeLong(METADATA_WRITE);
167 buffer.writeInt(TABLE_CONFIG);
168 buffer.writeInt(MAX_ENTRIES);
170 final int propIndex = buffer.writerIndex();
171 buffer.writeShort(propertyType);
173 final int propLengthIndex = buffer.writerIndex();
174 buffer.writeShort(EncodeConstants.EMPTY_LENGTH);
176 TableFeaturesPropType propType = TableFeaturesPropType.forValue(propertyType);
179 case OFPTFPTINSTRUCTIONS:
180 case OFPTFPTINSTRUCTIONSMISS:
181 int instructionStartIndex = buffer.writerIndex();
182 buffer.writeShort(InstructionConstants.APPLY_ACTIONS_TYPE);
183 int instructionLengthIndex = buffer.writerIndex();
184 buffer.writeShort(EncodeConstants.EMPTY_LENGTH);
185 buffer.setShort(instructionLengthIndex, buffer.writerIndex() - instructionStartIndex);
187 case OFPTFPTNEXTTABLES:
188 case OFPTFPTNEXTTABLESMISS:
192 case OFPTFPTWRITEACTIONS:
193 case OFPTFPTWRITEACTIONSMISS:
194 buffer.writeShort(InstructionConstants.WRITE_ACTIONS_TYPE);
195 buffer.writeShort(EncodeConstants.EMPTY_LENGTH);
197 case OFPTFPTAPPLYACTIONS:
198 case OFPTFPTAPPLYACTIONSMISS:
199 buffer.writeShort(InstructionConstants.APPLY_ACTIONS_TYPE);
200 buffer.writeShort(EncodeConstants.EMPTY_LENGTH);
203 buffer.writeShort(OxmMatchConstants.OPENFLOW_BASIC_CLASS);
204 buffer.writeByte(OxmMatchConstants.ARP_OP << 1);
205 buffer.writeByte(EncodeConstants.EMPTY_LENGTH);
207 case OFPTFPTWILDCARDS:
208 buffer.writeShort(OxmMatchConstants.OPENFLOW_BASIC_CLASS);
209 buffer.writeByte(OxmMatchConstants.ARP_SHA << 1);
210 buffer.writeByte(EncodeConstants.EMPTY_LENGTH);
212 case OFPTFPTWRITESETFIELD:
213 buffer.writeShort(OxmMatchConstants.OPENFLOW_BASIC_CLASS);
214 buffer.writeByte(OxmMatchConstants.ARP_SPA << 1);
215 buffer.writeByte(EncodeConstants.EMPTY_LENGTH);
217 case OFPTFPTWRITESETFIELDMISS:
218 buffer.writeShort(OxmMatchConstants.OPENFLOW_BASIC_CLASS);
219 buffer.writeByte(OxmMatchConstants.ARP_THA << 1);
220 buffer.writeByte(EncodeConstants.EMPTY_LENGTH);
222 case OFPTFPTAPPLYSETFIELD:
223 buffer.writeShort(OxmMatchConstants.OPENFLOW_BASIC_CLASS);
224 buffer.writeByte(OxmMatchConstants.ARP_TPA << 1);
225 buffer.writeByte(EncodeConstants.EMPTY_LENGTH);
227 case OFPTFPTAPPLYSETFIELDMISS:
228 buffer.writeShort(OxmMatchConstants.OPENFLOW_BASIC_CLASS);
229 buffer.writeByte(OxmMatchConstants.ETH_TYPE << 1);
230 buffer.writeByte(EncodeConstants.EMPTY_LENGTH);
236 int propLength = buffer.writerIndex() - propIndex;
237 buffer.setShort(propLengthIndex, buffer.writerIndex() - propIndex);
239 int paddingRemainder = propLength % EncodeConstants.PADDING;
241 if (paddingRemainder != 0) {
242 result = EncodeConstants.PADDING - paddingRemainder;
244 buffer.writeZero(result);
246 buffer.setShort(replyIndex, buffer.writerIndex() - replyIndex);
250 protected int getType() {
251 return MultipartType.OFPMPTABLEFEATURES.getIntValue();