Mass replace CRLF->LF
[openflowjava.git] / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / serialization / match / OxmMetadataSerializerTest.java
1 /*
2  * Copyright (c) 2014 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.openflowjava.protocol.impl.serialization.match;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertTrue;
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.PooledByteBufAllocator;
15
16 import org.junit.Assert;
17 import org.junit.Test;
18 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
19 import org.opendaylight.openflowjava.protocol.api.util.OxmMatchConstants;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MaskMatchEntry;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MaskMatchEntryBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MetadataMatchEntry;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MetadataMatchEntryBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.Metadata;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.OpenflowBasicClass;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.oxm.fields.grouping.MatchEntriesBuilder;
27
28 /**
29  * @author michal.polkorab
30  *
31  */
32 public class OxmMetadataSerializerTest {
33
34     OxmMetadataSerializer serializer = new OxmMetadataSerializer();
35
36     /**
37      * Test correct serialization
38      */
39     @Test
40     public void testSerializeWithoutMask() {
41         MatchEntriesBuilder builder = prepareMatchEntry(false, new byte[]{0, 1, 2, 3, 4, 5, 6, 7});
42         
43         ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
44         serializer.serialize(builder.build(), buffer);
45
46         checkHeader(buffer, false);
47         byte[] address = new byte[8];
48         buffer.readBytes(address);
49         Assert.assertArrayEquals("Wrong address", new byte[]{0, 1, 2, 3, 4, 5, 6, 7}, address);
50         assertTrue("Unexpected data", buffer.readableBytes() == 0);
51     }
52
53     /**
54      * Test correct serialization
55      */
56     @Test
57     public void testSerializeWithMask() {
58         MatchEntriesBuilder builder = prepareMatchEntry(true, new byte[]{8, 9, 10, 11, 12, 13, 14, 15});
59         
60         ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
61         serializer.serialize(builder.build(), buffer);
62
63         checkHeader(buffer, true);
64         
65         byte[] address = new byte[8];
66         buffer.readBytes(address);
67         Assert.assertArrayEquals("Wrong address", new byte[]{8, 9, 10, 11, 12, 13, 14, 15}, address);
68         byte[] tmp = new byte[8];
69         buffer.readBytes(tmp);
70         Assert.assertArrayEquals("Wrong mask", new byte[]{30, 30, 25, 25, 15, 15, 0, 0}, tmp);
71         assertTrue("Unexpected data", buffer.readableBytes() == 0);
72     }
73
74     /**
75      * Test correct header serialization
76      */
77     @Test
78     public void testSerializeHeaderWithoutMask() {
79         MatchEntriesBuilder builder = prepareHeader(false);
80         
81         ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
82         serializer.serializeHeader(builder.build(), buffer);
83
84         checkHeader(buffer, false);
85         assertTrue("Unexpected data", buffer.readableBytes() == 0);
86     }
87
88     /**
89      * Test correct header serialization
90      */
91     @Test
92     public void testSerializeHeaderWithMask() {
93         MatchEntriesBuilder builder = prepareHeader(true);
94         
95         ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
96         serializer.serializeHeader(builder.build(), buffer);
97
98         checkHeader(buffer, true);
99         assertTrue("Unexpected data", buffer.readableBytes() == 0);
100     }
101
102     /**
103      * Test correct oxm-class return value
104      */
105     @Test
106     public void testGetOxmClassCode() {
107         assertEquals("Wrong oxm-class", OxmMatchConstants.OPENFLOW_BASIC_CLASS, serializer.getOxmClassCode());
108     }
109
110     /**
111      * Test correct oxm-field return value
112      */
113     @Test
114     public void getOxmFieldCode() {
115         assertEquals("Wrong oxm-class", OxmMatchConstants.METADATA, serializer.getOxmFieldCode());
116     }
117
118     /**
119      * Test correct value length return value
120      */
121     @Test
122     public void testGetValueLength() {
123         assertEquals("Wrong value length", EncodeConstants.SIZE_OF_LONG_IN_BYTES, serializer.getValueLength());
124     }
125
126     private static MatchEntriesBuilder prepareMatchEntry(boolean hasMask, byte[] value) {
127         MatchEntriesBuilder builder = prepareHeader(hasMask);
128         if (hasMask) {
129             MaskMatchEntryBuilder maskBuilder = new MaskMatchEntryBuilder();
130             maskBuilder.setMask(new byte[]{30, 30, 25, 25, 15, 15, 0, 0});
131             builder.addAugmentation(MaskMatchEntry.class, maskBuilder.build());
132         }
133         MetadataMatchEntryBuilder metadataBuilder = new MetadataMatchEntryBuilder();
134         metadataBuilder.setMetadata(value);
135         builder.addAugmentation(MetadataMatchEntry.class, metadataBuilder.build());
136         return builder;
137     }
138
139     private static MatchEntriesBuilder prepareHeader(boolean hasMask) {
140         MatchEntriesBuilder builder = new MatchEntriesBuilder();
141         builder.setOxmClass(OpenflowBasicClass.class);
142         builder.setOxmMatchField(Metadata.class);
143         builder.setHasMask(hasMask);
144         return builder;
145     }
146
147     private static void checkHeader(ByteBuf buffer, boolean hasMask) {
148         assertEquals("Wrong oxm-class", OxmMatchConstants.OPENFLOW_BASIC_CLASS, buffer.readUnsignedShort());
149         short fieldAndMask = buffer.readUnsignedByte();
150         assertEquals("Wrong oxm-field", OxmMatchConstants.METADATA, fieldAndMask >>> 1);
151         assertEquals("Wrong hasMask", hasMask, (fieldAndMask & 1) != 0);
152         if (hasMask) {
153             assertEquals("Wrong length", 2 * EncodeConstants.SIZE_OF_LONG_IN_BYTES, buffer.readUnsignedByte());
154         } else {
155             assertEquals("Wrong length", EncodeConstants.SIZE_OF_LONG_IN_BYTES, buffer.readUnsignedByte());
156         }
157     }
158 }