Fixup Augmentable and Identifiable methods changing
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / protocol / serialization / multipart / MultipartRequestTableFeaturesSerializerTest.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.serialization.multipart;
10
11 import static org.junit.Assert.assertEquals;
12
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.UnpooledByteBufAllocator;
15 import java.math.BigInteger;
16 import java.util.Collections;
17 import org.junit.Test;
18 import org.opendaylight.openflowjava.protocol.api.keys.MessageTypeKey;
19 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
20 import org.opendaylight.openflowjava.util.ByteBufUtils;
21 import org.opendaylight.openflowplugin.impl.protocol.serialization.AbstractSerializerTest;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.TableFeaturesPropType;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.TableConfig;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.multipart.request.multipart.request.body.MultipartRequestTableFeatures;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.multipart.request.multipart.request.body.MultipartRequestTableFeaturesBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.feature.prop.type.table.feature.prop.type.NextTable;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.feature.prop.type.table.feature.prop.type.NextTableBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.feature.prop.type.table.feature.prop.type.next.table.TablesBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.TableFeaturesBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.table.features.TablePropertiesBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.table.features.table.properties.TableFeaturePropertiesBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.table.features.table.properties.TableFeaturePropertiesKey;
33
34 public class MultipartRequestTableFeaturesSerializerTest extends AbstractSerializerTest {
35     private static final byte PADDING_IN_MULTIPART_REQUEST_TABLE_FEATURES_BODY = 5;
36     private static final byte MAX_TABLE_NAME_LENGTH = 32;
37     private static final short TABLE_ID = 42;
38     private static final String NAME = "table_prop";
39     private static final BigInteger METADATA_MATCH = BigInteger.ONE;
40     private static final BigInteger METADATA_WRITE = BigInteger.TEN;
41     private static final long MAX_ENTRIES = 12;
42     private static final boolean IS_DEPRECATED_MASK = true;
43     private static final short NEXT_TABLE_ID = 43;
44
45     private static final TableFeaturesPropType NEXT_TABLE_TYPE = TableFeaturesPropType.OFPTFPTNEXTTABLES;
46     private static final NextTable NEXT_TABLE = new NextTableBuilder()
47             .setTables(new TablesBuilder()
48                     .setTableIds(Collections.singletonList(NEXT_TABLE_ID))
49                     .build())
50             .build();
51     private static final MultipartRequestTableFeatures BODY = new MultipartRequestTableFeaturesBuilder()
52             .setTableFeatures(Collections.singletonList(new TableFeaturesBuilder()
53                     .setTableId(TABLE_ID)
54                     .setName(NAME)
55                     .setMetadataMatch(METADATA_MATCH)
56                     .setMetadataWrite(METADATA_WRITE)
57                     .setConfig(new TableConfig(IS_DEPRECATED_MASK))
58                     .setMaxEntries(MAX_ENTRIES)
59                     .setTableProperties(new TablePropertiesBuilder()
60                             .setTableFeatureProperties(Collections.singletonList(new TableFeaturePropertiesBuilder()
61                                     .setOrder(0)
62                                     .withKey(new TableFeaturePropertiesKey(0))
63                                     .setTableFeaturePropType(NEXT_TABLE)
64                                     .build()))
65                             .build())
66                     .build()))
67             .build();
68
69     private MultipartRequestTableFeaturesSerializer serializer;
70
71     @Override
72     protected void init() {
73         serializer = getRegistry().getSerializer(new MessageTypeKey<>(EncodeConstants.OF13_VERSION_ID,
74                 MultipartRequestTableFeatures.class));
75     }
76
77     @Test
78     public void testSerialize() throws Exception {
79         final ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
80         serializer.serialize(BODY, out);
81
82         out.skipBytes(EncodeConstants.SIZE_OF_SHORT_IN_BYTES); // skip length
83         assertEquals(TABLE_ID, out.readUnsignedByte());
84         out.skipBytes(PADDING_IN_MULTIPART_REQUEST_TABLE_FEATURES_BODY);
85         assertEquals(NAME, ByteBufUtils.decodeNullTerminatedString(out, MAX_TABLE_NAME_LENGTH));
86         final byte[] match = new byte[EncodeConstants.SIZE_OF_LONG_IN_BYTES];
87         out.readBytes(match);
88         assertEquals(METADATA_MATCH, new BigInteger(1, match));
89         final byte[] write = new byte[EncodeConstants.SIZE_OF_LONG_IN_BYTES];
90         out.readBytes(write);
91         assertEquals(METADATA_WRITE, new BigInteger(1, write));
92         assertEquals(IS_DEPRECATED_MASK, (out.readUnsignedInt() & 3) != 0);
93         assertEquals(MAX_ENTRIES, out.readUnsignedInt());
94         assertEquals(NEXT_TABLE_TYPE.getIntValue(), out.readUnsignedShort());
95         final int propLength = out.readUnsignedShort();
96         final int paddingRemainder = propLength % EncodeConstants.PADDING;
97         assertEquals(NEXT_TABLE_ID, out.readUnsignedByte());
98
99         if (paddingRemainder != 0) {
100             out.skipBytes(EncodeConstants.PADDING - paddingRemainder);
101         }
102
103         assertEquals(out.readableBytes(), 0);
104     }
105
106 }