Update MRI projects for Aluminium
[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 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.TableFeatures;
37
38 public class MultipartReplyTableFeaturesDeserializerTest extends AbstractMultipartDeserializerTest {
39
40     private static final byte PADDING_IN_MULTIPART_REPLY_TABLE_FEATURES = 5;
41     private static final byte MAX_TABLE_NAME_LENGTH = 32;
42
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;
48
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;
63
64     @Test
65     public void deserialize() {
66         ByteBuf buffer = UnpooledByteBufAllocator.DEFAULT.buffer();
67
68         writeValues(buffer, OFPTFPT_INSTRUCTIONS);
69         MultipartReplyTableFeatures reply = (MultipartReplyTableFeatures) deserializeMultipart(buffer);
70
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());
80
81         writeValues(buffer, OFPTFPT_INSTRUCTIONS_MISS);
82         reply = (MultipartReplyTableFeatures) deserializeMultipart(buffer);
83
84         assertEquals(InstructionsMiss.class, reply.nonnullTableFeatures().values().iterator().next()
85                 .getTableProperties().nonnullTableFeatureProperties().values().iterator().next()
86                 .getTableFeaturePropType().implementedInterface());
87
88         writeValues(buffer, OFPTFPT_NEXT_TABLES);
89         reply = (MultipartReplyTableFeatures) deserializeMultipart(buffer);
90
91         assertEquals(NextTable.class, reply.nonnullTableFeatures().values().iterator().next().getTableProperties()
92                 .nonnullTableFeatureProperties().values().iterator().next().getTableFeaturePropType()
93                 .implementedInterface());
94
95         writeValues(buffer, OFPTFPT_NEXT_TABLES_MISS);
96         reply = (MultipartReplyTableFeatures) deserializeMultipart(buffer);
97
98         assertEquals(NextTableMiss.class, reply.nonnullTableFeatures().values().iterator().next().getTableProperties()
99                 .nonnullTableFeatureProperties().values().iterator().next().getTableFeaturePropType()
100                 .implementedInterface());
101
102         writeValues(buffer, OFPTFPT_WRITE_ACTIONS);
103         reply = (MultipartReplyTableFeatures) deserializeMultipart(buffer);
104
105         assertEquals(WriteActions.class, reply.nonnullTableFeatures().values().iterator().next().getTableProperties()
106                 .nonnullTableFeatureProperties().values().iterator().next().getTableFeaturePropType()
107                 .implementedInterface());
108
109         writeValues(buffer, OFPTFPT_WRITE_ACTIONS_MISS);
110         reply = (MultipartReplyTableFeatures) deserializeMultipart(buffer);
111
112         assertEquals(WriteActionsMiss.class, reply.nonnullTableFeatures().values().iterator().next()
113                 .getTableProperties().nonnullTableFeatureProperties().values().iterator().next()
114                 .getTableFeaturePropType().implementedInterface());
115
116         writeValues(buffer, OFPTFPT_APPLY_ACTIONS);
117         reply = (MultipartReplyTableFeatures) deserializeMultipart(buffer);
118
119         assertEquals(ApplyActions.class, reply.nonnullTableFeatures().values().iterator().next().getTableProperties()
120                 .nonnullTableFeatureProperties().values().iterator().next().getTableFeaturePropType()
121                 .implementedInterface());
122
123         writeValues(buffer, OFPTFPT_APPLY_ACTIONS_MISS);
124         reply = (MultipartReplyTableFeatures) deserializeMultipart(buffer);
125
126         assertEquals(ApplyActionsMiss.class, reply.nonnullTableFeatures().values().iterator().next()
127                 .getTableProperties().nonnullTableFeatureProperties().values().iterator().next()
128                 .getTableFeaturePropType().implementedInterface());
129
130         writeValues(buffer, OFPTFPT_MATCH);
131         reply = (MultipartReplyTableFeatures) deserializeMultipart(buffer);
132
133         assertEquals(Match.class, reply.nonnullTableFeatures().values().iterator().next().getTableProperties()
134                 .nonnullTableFeatureProperties().values().iterator().next().getTableFeaturePropType()
135                 .implementedInterface());
136
137         writeValues(buffer, OFPTFPT_WILDCARDS);
138         reply = (MultipartReplyTableFeatures) deserializeMultipart(buffer);
139
140         assertEquals(Wildcards.class, reply.nonnullTableFeatures().values().iterator().next().getTableProperties()
141                 .nonnullTableFeatureProperties().values().iterator().next().getTableFeaturePropType()
142                 .implementedInterface());
143
144         writeValues(buffer, OFPTFPT_WRITE_SETFIELD);
145         reply = (MultipartReplyTableFeatures) deserializeMultipart(buffer);
146
147         assertEquals(WriteSetfield.class, reply.nonnullTableFeatures().values().iterator().next().getTableProperties()
148                 .nonnullTableFeatureProperties().values().iterator().next().getTableFeaturePropType()
149                 .implementedInterface());
150
151         writeValues(buffer, OFPTFPT_WRITE_SETFIELD_MISS);
152         reply = (MultipartReplyTableFeatures) deserializeMultipart(buffer);
153
154         assertEquals(WriteSetfieldMiss.class, reply.nonnullTableFeatures().values().iterator().next()
155                 .getTableProperties().nonnullTableFeatureProperties().values().iterator().next()
156                 .getTableFeaturePropType().implementedInterface());
157
158         writeValues(buffer, OFPTFPT_APPLY_SETFIELD);
159         reply = (MultipartReplyTableFeatures) deserializeMultipart(buffer);
160
161         assertEquals(ApplySetfield.class, reply.nonnullTableFeatures().values().iterator().next().getTableProperties()
162                 .nonnullTableFeatureProperties().values().iterator().next().getTableFeaturePropType()
163                 .implementedInterface());
164
165         writeValues(buffer, OFPTFPT_APPLY_SETFIELD_MISS);
166         reply = (MultipartReplyTableFeatures) deserializeMultipart(buffer);
167
168         assertEquals(ApplySetfieldMiss.class, reply.nonnullTableFeatures().values().iterator().next()
169                 .getTableProperties().nonnullTableFeatureProperties().values().iterator().next()
170                 .getTableFeaturePropType().implementedInterface());
171         assertEquals(0, buffer.readableBytes());
172     }
173
174
175     private static void writeValues(final ByteBuf buffer, final int propertyType) {
176         buffer.clear();
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);
186
187         final int propIndex = buffer.writerIndex();
188         buffer.writeShort(propertyType);
189
190         final int propLengthIndex = buffer.writerIndex();
191         buffer.writeShort(EncodeConstants.EMPTY_LENGTH);
192
193         TableFeaturesPropType propType = TableFeaturesPropType.forValue(propertyType);
194
195         switch (propType) {
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);
203                 break;
204             case OFPTFPTNEXTTABLES:
205             case OFPTFPTNEXTTABLESMISS:
206                 buffer.writeByte(1);
207                 buffer.writeByte(2);
208                 break;
209             case OFPTFPTWRITEACTIONS:
210             case OFPTFPTWRITEACTIONSMISS:
211                 buffer.writeShort(InstructionConstants.WRITE_ACTIONS_TYPE);
212                 buffer.writeShort(EncodeConstants.EMPTY_LENGTH);
213                 break;
214             case OFPTFPTAPPLYACTIONS:
215             case OFPTFPTAPPLYACTIONSMISS:
216                 buffer.writeShort(InstructionConstants.APPLY_ACTIONS_TYPE);
217                 buffer.writeShort(EncodeConstants.EMPTY_LENGTH);
218                 break;
219             case OFPTFPTMATCH:
220                 buffer.writeShort(OxmMatchConstants.OPENFLOW_BASIC_CLASS);
221                 buffer.writeByte(OxmMatchConstants.ARP_OP << 1);
222                 buffer.writeByte(EncodeConstants.EMPTY_LENGTH);
223                 break;
224             case OFPTFPTWILDCARDS:
225                 buffer.writeShort(OxmMatchConstants.OPENFLOW_BASIC_CLASS);
226                 buffer.writeByte(OxmMatchConstants.ARP_SHA << 1);
227                 buffer.writeByte(EncodeConstants.EMPTY_LENGTH);
228                 break;
229             case OFPTFPTWRITESETFIELD:
230                 buffer.writeShort(OxmMatchConstants.OPENFLOW_BASIC_CLASS);
231                 buffer.writeByte(OxmMatchConstants.ARP_SPA << 1);
232                 buffer.writeByte(EncodeConstants.EMPTY_LENGTH);
233                 break;
234             case OFPTFPTWRITESETFIELDMISS:
235                 buffer.writeShort(OxmMatchConstants.OPENFLOW_BASIC_CLASS);
236                 buffer.writeByte(OxmMatchConstants.ARP_THA << 1);
237                 buffer.writeByte(EncodeConstants.EMPTY_LENGTH);
238                 break;
239             case OFPTFPTAPPLYSETFIELD:
240                 buffer.writeShort(OxmMatchConstants.OPENFLOW_BASIC_CLASS);
241                 buffer.writeByte(OxmMatchConstants.ARP_TPA << 1);
242                 buffer.writeByte(EncodeConstants.EMPTY_LENGTH);
243                 break;
244             case OFPTFPTAPPLYSETFIELDMISS:
245                 buffer.writeShort(OxmMatchConstants.OPENFLOW_BASIC_CLASS);
246                 buffer.writeByte(OxmMatchConstants.ETH_TYPE << 1);
247                 buffer.writeByte(EncodeConstants.EMPTY_LENGTH);
248                 break;
249             default:
250                 break;
251         }
252
253         int propLength = buffer.writerIndex() - propIndex;
254         buffer.setShort(propLengthIndex, buffer.writerIndex() - propIndex);
255
256         int paddingRemainder = propLength % EncodeConstants.PADDING;
257         int result = 0;
258         if (paddingRemainder != 0) {
259             result = EncodeConstants.PADDING - paddingRemainder;
260         }
261         buffer.writeZero(result);
262
263         buffer.setShort(replyIndex, buffer.writerIndex() - replyIndex);
264     }
265
266     @Override
267     protected int getType() {
268         return MultipartType.OFPMPTABLEFEATURES.getIntValue();
269     }
270 }