BUG-2794: incorporate openflowjava api changes to openflowplugin
[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 org.junit.Assert;
11 import org.junit.Test;
12 import org.opendaylight.openflowjava.util.ByteBufUtils;
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.Ipv6ExthdrFlags;
14 import org.slf4j.Logger;
15 import org.slf4j.LoggerFactory;
16 import java.lang.reflect.Constructor;
17 import java.math.BigInteger;
18 import java.util.Arrays;
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 MatchConvertorUtil#ipv6ExthdrFlagsToInt(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.Ipv6ExthdrFlags)}.
30      *
31      * @throws Exception
32      */
33
34     @Test
35     public void testIpv6ExthdrFlagsToInt() throws Exception {
36         Ipv6ExthdrFlags pField;
37         Constructor<Ipv6ExthdrFlags> ctor = Ipv6ExthdrFlags.class.getConstructor(
38                 Boolean.class, Boolean.class, Boolean.class, Boolean.class,
39                 Boolean.class, Boolean.class, Boolean.class, Boolean.class, Boolean.class);
40
41         int[] expectedFlagCumulants = new int[]{
42                 4, 8, 2, 16, 64, 1, 32, 128, 256
43         };
44
45         for (int i = 0; i < 9; i++) {
46             pField = ctor.newInstance(createIpv6ExthdrFlagsCtorParams(i));
47             int intResult = MatchConvertorUtil.ipv6ExthdrFlagsToInt(pField);
48             LOG.debug("{}:Ipv6ExthdrFlags[{}] as int = {}", i, pField, intResult);
49             Assert.assertEquals(expectedFlagCumulants[i], intResult);
50         }
51
52         pField = new Ipv6ExthdrFlags(
53                 false, false, false, false, false, false, false, false, false);
54         Assert.assertEquals(0, MatchConvertorUtil.ipv6ExthdrFlagsToInt(pField).intValue());
55
56         pField = new Ipv6ExthdrFlags(
57                 true, true, true, true, true, true, true, true, true);
58         Assert.assertEquals(511, MatchConvertorUtil.ipv6ExthdrFlagsToInt(pField).intValue());
59     }
60
61     /**
62      * @return
63      */
64
65     private static Object[] createIpv6ExthdrFlagsCtorParams(int trueIndex) {
66         Boolean[] flags = new Boolean[]{false, false, false, false, false, false, false, false, false};
67         flags[trueIndex] = true;
68         return flags;
69     }
70
71     /**
72      * Test method for {@link MatchConvertorUtil#ipv6NetmaskArrayToCIDRValue(byte[])} (org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev150225.MatchEntry)}.
73      *
74      * @throws Exception
75      */
76
77     @Test
78     public void testIpv6NetmaskArrayToCIDRValue() throws Exception {
79         BigInteger maskSeed = new BigInteger("1ffffffffffffffffffffffffffffffff", 16);
80         byte[] maskArray = new byte[16];
81         LOG.debug("maskSeed= {}", ByteBufUtils.bytesToHexString(maskSeed.toByteArray()));
82
83         for (int i = 0; i <= 128; i++) {
84             System.arraycopy(maskSeed.toByteArray(), 1, maskArray, 0, 16);
85             LOG.debug("maskHex[{}] = {}", i, ByteBufUtils.bytesToHexString(maskArray));
86             int cidr = MatchConvertorUtil.ipv6NetmaskArrayToCIDRValue(maskArray);
87             LOG.debug("cidr = {}", cidr);
88             Assert.assertEquals(128 - i, cidr);
89
90             maskSeed = maskSeed.clearBit(i);
91         }
92     }
93
94     /**
95      * Test method for {@link MatchConvertorUtil#getIpv4Mask(byte[])}.
96      *
97      * @throws Exception
98      */
99
100     @Test
101     public void testGetIpv4Mask() {
102         byte[][] maskInputs = new byte[][]{
103                 {(byte) 255, (byte) 255, (byte) 255, (byte) 255},
104                 {(byte) 255, (byte) 255, (byte) 254, 0},
105                 {(byte) 128, 0, 0, 0},
106                 {0, 0, 0, 0},
107         };
108
109         String[] maskOutputs = new String[]{
110                 "/32", "/23", "/1", "/0"
111         };
112
113         for (int i = 0; i < maskInputs.length; i++) {
114             String mask = MatchConvertorUtil.getIpv4Mask(maskInputs[i]);
115             Assert.assertEquals(maskOutputs[i], mask);
116         }
117     }
118
119     /**
120      * Test method for {@link MatchConvertorUtil#getIpv4Mask(byte[])}.
121      *
122      * @throws Exception
123      */
124
125     @Test
126     public void testGetIpv4MaskNegative() {
127         byte[][] maskInputs = new byte[][]{
128                 {(byte) 127, 0, 0, 0},
129                 {(byte) 127, 0, 0},
130         };
131
132         for (int i = 0; i < maskInputs.length; i++) {
133             try {
134                 String mask = MatchConvertorUtil.getIpv4Mask(maskInputs[i]);
135                 Assert.fail("invalid mask should not have passed: " + Arrays.toString(maskInputs[i]));
136             } catch (Exception e) {
137                 // expected
138                 LOG.info("ipv4 mask excepted exception: {}", e.getMessage(), e);
139             }
140         }
141     }
142 }