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