e13577ad814439e6d3b48787bd8f02643b005273
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / protocol / deserialization / multipart / MultipartReplyTableFeaturesDeserializerTest.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies s.r.o. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.openflowplugin.impl.protocol.deserialization.multipart;
10
11 import static org.junit.Assert.assertEquals;
12
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
37 public class MultipartReplyTableFeaturesDeserializerTest extends AbstractMultipartDeserializerTest {
38
39     private static final byte PADDING_IN_MULTIPART_REPLY_TABLE_FEATURES = 5;
40     private static final byte MAX_TABLE_NAME_LENGTH = 32;
41
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;
47
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;
62
63     @Test
64     public void deserialize() {
65         ByteBuf buffer = UnpooledByteBufAllocator.DEFAULT.buffer();
66
67         writeValues(buffer, OFPTFPT_INSTRUCTIONS);
68         MultipartReplyTableFeatures reply = (MultipartReplyTableFeatures) deserializeMultipart(buffer);
69
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());
77
78         writeValues(buffer, OFPTFPT_INSTRUCTIONS_MISS);
79         reply = (MultipartReplyTableFeatures) deserializeMultipart(buffer);
80
81         assertEquals(InstructionsMiss.class, reply.getTableFeatures().get(0).getTableProperties()
82                 .getTableFeatureProperties().get(0).getTableFeaturePropType().implementedInterface());
83
84         writeValues(buffer, OFPTFPT_NEXT_TABLES);
85         reply = (MultipartReplyTableFeatures) deserializeMultipart(buffer);
86
87         assertEquals(NextTable.class, reply.getTableFeatures().get(0).getTableProperties()
88                 .getTableFeatureProperties().get(0).getTableFeaturePropType().implementedInterface());
89
90         writeValues(buffer, OFPTFPT_NEXT_TABLES_MISS);
91         reply = (MultipartReplyTableFeatures) deserializeMultipart(buffer);
92
93         assertEquals(NextTableMiss.class, reply.getTableFeatures().get(0).getTableProperties()
94                 .getTableFeatureProperties().get(0).getTableFeaturePropType().implementedInterface());
95
96         writeValues(buffer, OFPTFPT_WRITE_ACTIONS);
97         reply = (MultipartReplyTableFeatures) deserializeMultipart(buffer);
98
99         assertEquals(WriteActions.class, reply.getTableFeatures().get(0).getTableProperties()
100                 .getTableFeatureProperties().get(0).getTableFeaturePropType().implementedInterface());
101
102         writeValues(buffer, OFPTFPT_WRITE_ACTIONS_MISS);
103         reply = (MultipartReplyTableFeatures) deserializeMultipart(buffer);
104
105         assertEquals(WriteActionsMiss.class, reply.getTableFeatures().get(0).getTableProperties()
106                 .getTableFeatureProperties().get(0).getTableFeaturePropType().implementedInterface());
107
108         writeValues(buffer, OFPTFPT_APPLY_ACTIONS);
109         reply = (MultipartReplyTableFeatures) deserializeMultipart(buffer);
110
111         assertEquals(ApplyActions.class, reply.getTableFeatures().get(0).getTableProperties()
112                 .getTableFeatureProperties().get(0).getTableFeaturePropType().implementedInterface());
113
114         writeValues(buffer, OFPTFPT_APPLY_ACTIONS_MISS);
115         reply = (MultipartReplyTableFeatures) deserializeMultipart(buffer);
116
117         assertEquals(ApplyActionsMiss.class, reply.getTableFeatures().get(0).getTableProperties()
118                 .getTableFeatureProperties().get(0).getTableFeaturePropType().implementedInterface());
119
120         writeValues(buffer, OFPTFPT_MATCH);
121         reply = (MultipartReplyTableFeatures) deserializeMultipart(buffer);
122
123         assertEquals(Match.class, reply.getTableFeatures().get(0).getTableProperties()
124                 .getTableFeatureProperties().get(0).getTableFeaturePropType().implementedInterface());
125
126         writeValues(buffer, OFPTFPT_WILDCARDS);
127         reply = (MultipartReplyTableFeatures) deserializeMultipart(buffer);
128
129         assertEquals(Wildcards.class, reply.getTableFeatures().get(0).getTableProperties()
130                 .getTableFeatureProperties().get(0).getTableFeaturePropType().implementedInterface());
131
132         writeValues(buffer, OFPTFPT_WRITE_SETFIELD);
133         reply = (MultipartReplyTableFeatures) deserializeMultipart(buffer);
134
135         assertEquals(WriteSetfield.class, reply.getTableFeatures().get(0).getTableProperties()
136                 .getTableFeatureProperties().get(0).getTableFeaturePropType().implementedInterface());
137
138         writeValues(buffer, OFPTFPT_WRITE_SETFIELD_MISS);
139         reply = (MultipartReplyTableFeatures) deserializeMultipart(buffer);
140
141         assertEquals(WriteSetfieldMiss.class, reply.getTableFeatures().get(0).getTableProperties()
142                 .getTableFeatureProperties().get(0).getTableFeaturePropType().implementedInterface());
143
144         writeValues(buffer, OFPTFPT_APPLY_SETFIELD);
145         reply = (MultipartReplyTableFeatures) deserializeMultipart(buffer);
146
147         assertEquals(ApplySetfield.class, reply.getTableFeatures().get(0).getTableProperties()
148                 .getTableFeatureProperties().get(0).getTableFeaturePropType().implementedInterface());
149
150         writeValues(buffer, OFPTFPT_APPLY_SETFIELD_MISS);
151         reply = (MultipartReplyTableFeatures) deserializeMultipart(buffer);
152
153         assertEquals(ApplySetfieldMiss.class, reply.getTableFeatures().get(0).getTableProperties()
154                 .getTableFeatureProperties().get(0).getTableFeaturePropType().implementedInterface());
155         assertEquals(0, buffer.readableBytes());
156     }
157
158     private void writeValues(ByteBuf buffer, int propertyType) {
159         buffer.clear();
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);
169
170         final int propIndex = buffer.writerIndex();
171         buffer.writeShort(propertyType);
172
173         final int propLengthIndex = buffer.writerIndex();
174         buffer.writeShort(EncodeConstants.EMPTY_LENGTH);
175
176         TableFeaturesPropType propType = TableFeaturesPropType.forValue(propertyType);
177
178         switch (propType) {
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);
186                 break;
187             case OFPTFPTNEXTTABLES:
188             case OFPTFPTNEXTTABLESMISS:
189                 buffer.writeByte(1);
190                 buffer.writeByte(2);
191                 break;
192             case OFPTFPTWRITEACTIONS:
193             case OFPTFPTWRITEACTIONSMISS:
194                 buffer.writeShort(InstructionConstants.WRITE_ACTIONS_TYPE);
195                 buffer.writeShort(EncodeConstants.EMPTY_LENGTH);
196                 break;
197             case OFPTFPTAPPLYACTIONS:
198             case OFPTFPTAPPLYACTIONSMISS:
199                 buffer.writeShort(InstructionConstants.APPLY_ACTIONS_TYPE);
200                 buffer.writeShort(EncodeConstants.EMPTY_LENGTH);
201                 break;
202             case OFPTFPTMATCH:
203                 buffer.writeShort(OxmMatchConstants.OPENFLOW_BASIC_CLASS);
204                 buffer.writeByte(OxmMatchConstants.ARP_OP << 1);
205                 buffer.writeByte(EncodeConstants.EMPTY_LENGTH);
206                 break;
207             case OFPTFPTWILDCARDS:
208                 buffer.writeShort(OxmMatchConstants.OPENFLOW_BASIC_CLASS);
209                 buffer.writeByte(OxmMatchConstants.ARP_SHA << 1);
210                 buffer.writeByte(EncodeConstants.EMPTY_LENGTH);
211                 break;
212             case OFPTFPTWRITESETFIELD:
213                 buffer.writeShort(OxmMatchConstants.OPENFLOW_BASIC_CLASS);
214                 buffer.writeByte(OxmMatchConstants.ARP_SPA << 1);
215                 buffer.writeByte(EncodeConstants.EMPTY_LENGTH);
216                 break;
217             case OFPTFPTWRITESETFIELDMISS:
218                 buffer.writeShort(OxmMatchConstants.OPENFLOW_BASIC_CLASS);
219                 buffer.writeByte(OxmMatchConstants.ARP_THA << 1);
220                 buffer.writeByte(EncodeConstants.EMPTY_LENGTH);
221                 break;
222             case OFPTFPTAPPLYSETFIELD:
223                 buffer.writeShort(OxmMatchConstants.OPENFLOW_BASIC_CLASS);
224                 buffer.writeByte(OxmMatchConstants.ARP_TPA << 1);
225                 buffer.writeByte(EncodeConstants.EMPTY_LENGTH);
226                 break;
227             case OFPTFPTAPPLYSETFIELDMISS:
228                 buffer.writeShort(OxmMatchConstants.OPENFLOW_BASIC_CLASS);
229                 buffer.writeByte(OxmMatchConstants.ETH_TYPE << 1);
230                 buffer.writeByte(EncodeConstants.EMPTY_LENGTH);
231                 break;
232             default:
233                 break;
234         }
235
236         int propLength = buffer.writerIndex() - propIndex;
237         buffer.setShort(propLengthIndex, buffer.writerIndex() - propIndex);
238
239         int paddingRemainder = propLength % EncodeConstants.PADDING;
240         int result = 0;
241         if (paddingRemainder != 0) {
242             result = EncodeConstants.PADDING - paddingRemainder;
243         }
244         buffer.writeZero(result);
245
246         buffer.setShort(replyIndex, buffer.writerIndex() - replyIndex);
247     }
248
249     @Override
250     protected int getType() {
251         return MultipartType.OFPMPTABLEFEATURES.getIntValue();
252     }
253 }