db2a5ba28010a446d2b53cf9afbc0acfa83f5c1c
[openflowplugin.git] / openflowplugin / src / test / java / org / opendaylight / openflowplugin / openflow / md / core / sal / convertor / match / MatchConvertorUtilTest.java
1 /**
2  * Copyright (c) 2013 Cisco Systems, Inc. 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 package org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.match;
9
10 import java.lang.reflect.Constructor;
11 import java.math.BigInteger;
12
13 import org.junit.Assert;
14 import org.junit.Test;
15 import org.opendaylight.openflowjava.util.ByteBufUtils;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.Ipv6ExthdrFlags;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19
20 /**
21  * 
22  */
23 public class MatchConvertorUtilTest {
24     
25     private static Logger LOG = LoggerFactory
26             .getLogger(MatchConvertorUtilTest.class);
27
28     /**
29      * Test method for {@link org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.match.MatchConvertorUtil#ipv6ExthdrFlagsToInt(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.Ipv6ExthdrFlags)}.
30      * @throws Exception 
31      */
32     @Test
33     public void testIpv6ExthdrFlagsToInt() throws Exception {
34         Ipv6ExthdrFlags pField;
35         Constructor<Ipv6ExthdrFlags> ctor = Ipv6ExthdrFlags.class.getConstructor(
36                 Boolean.class, Boolean.class, Boolean.class, Boolean.class, 
37                 Boolean.class, Boolean.class, Boolean.class, Boolean.class, Boolean.class);
38         
39         int[] expectedFlagCumulants = new int[] {
40                 4, 8, 2, 16, 64, 1, 32, 128, 256
41         };
42         
43         for (int i = 0; i < 9; i++) {
44             pField = ctor.newInstance(createIpv6ExthdrFlagsCtorParams(i));
45             int intResult = MatchConvertorUtil.ipv6ExthdrFlagsToInt(pField);
46             LOG.debug("{}:Ipv6ExthdrFlags[{}] as int = {}", i, pField, intResult);
47             Assert.assertEquals(expectedFlagCumulants[i], intResult);
48         }
49         
50         pField = new Ipv6ExthdrFlags(
51                 false, false, false, false, false, false, false, false, false);
52         Assert.assertEquals(0, MatchConvertorUtil.ipv6ExthdrFlagsToInt(pField).intValue());
53         
54         pField = new Ipv6ExthdrFlags(
55                 true, true, true, true, true, true, true, true, true);
56         Assert.assertEquals(511, MatchConvertorUtil.ipv6ExthdrFlagsToInt(pField).intValue());
57     }
58
59     /**
60      * @return
61      */
62     private static Object[] createIpv6ExthdrFlagsCtorParams(int trueIndex) {
63         Boolean[] flags = new Boolean[]{false, false, false, false, false, false, false, false, false};
64         flags[trueIndex] = true;
65         return flags;
66     }
67
68     /**
69      * Test method for {@link org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.match.MatchConvertorUtil#ipv6NetmaskArrayToCIDRValue(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MaskMatchEntry)}.
70      * @throws Exception
71      */
72     @Test
73     public void testIpv6NetmaskArrayToCIDRValue() throws Exception {
74         BigInteger maskSeed = new BigInteger("1ffffffffffffffffffffffffffffffff", 16);
75         byte[] maskArray = new byte[16];
76         LOG.debug("maskSeed= {}", ByteBufUtils.bytesToHexString(maskSeed.toByteArray()));
77
78         for (int i = 0; i <= 128; i++) {
79             System.arraycopy(maskSeed.toByteArray(), 1, maskArray, 0, 16);
80             LOG.debug("maskHex[{}] = {}", i, ByteBufUtils.bytesToHexString(maskArray));
81             int cidr = MatchConvertorUtil.ipv6NetmaskArrayToCIDRValue(maskArray);
82             LOG.debug("cidr = {}", cidr);
83             Assert.assertEquals(128-i, cidr);
84
85             maskSeed = maskSeed.clearBit(i);
86         }
87     }
88
89 }