2ed4e20df96636d49b0d1bae29be6e8f2101ed13
[controller.git] / opendaylight / md-sal / statistics-manager / src / test / java / org / opendaylight / controller / md / statistics / manager / StatisticsUpdateCommiterTest.java
1 /**
2  * Copyright (c) 2014 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
9 package org.opendaylight.controller.md.statistics.manager;
10
11 import org.junit.Assert;
12 import org.junit.Test;
13 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv4Match;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv4MatchBuilder;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18
19 /**
20  *
21  */
22 public class StatisticsUpdateCommiterTest {
23
24     private static final Logger LOG = LoggerFactory
25             .getLogger(StatisticsUpdateCommiterTest.class);
26
27     /**
28      * Test method for {@link org.opendaylight.controller.md.statistics.manager.StatisticsUpdateCommiter#layer3MatchEquals(org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Layer3Match, org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Layer3Match)}.
29      */
30     @Test
31     public void testLayer3MatchEquals() {
32         String[][][] matchSeeds = new String[][][] {
33                 {{"10.1.2.0/24", "10.1.2.0/24"}, {"10.1.2.0/24", "10.1.2.0/24"}},
34                 {{"10.1.2.0/24", "10.1.2.0/24"}, {"10.1.2.0/24", "10.1.1.0/24"}},
35                 {{"10.1.1.0/24", "10.1.2.0/24"}, {"10.1.2.0/24", "10.1.2.0/24"}},
36                 {{"10.1.1.0/24", "10.1.1.0/24"}, {"10.1.2.0/24", "10.1.2.0/24"}},
37
38                 {{"10.1.1.0/24", null}, {"10.1.1.0/24", "10.1.2.0/24"}},
39                 {{"10.1.1.0/24", null}, {"10.1.2.0/24", "10.1.2.0/24"}},
40                 {{"10.1.1.0/24", null}, {"10.1.2.0/24", null}},
41                 {{"10.1.1.0/24", null}, {"10.1.1.0/24", null}},
42
43                 {{null, "10.1.1.0/24"}, {"10.1.2.0/24", "10.1.1.0/24"}},
44                 {{null, "10.1.1.0/24"}, {"10.1.2.0/24", "10.1.2.0/24"}},
45                 {{null, "10.1.1.0/24"}, {null, "10.1.2.0/24"}},
46                 {{null, "10.1.1.0/24"}, {null, "10.1.1.0/24"}},
47
48                 {{null, null}, {null, "10.1.1.0/24"}},
49                 {{null, null}, {null, null}},
50         };
51
52         boolean[] matches = new boolean[] {
53                 true,
54                 false,
55                 false,
56                 false,
57
58                 false,
59                 false,
60                 false,
61                 true,
62
63                 false,
64                 false,
65                 false,
66                 true,
67
68                 false,
69                 true
70         };
71
72         for (int i = 0; i < matches.length; i++) {
73             checkComparisonOfL3Match(
74                     matchSeeds[i][0][0], matchSeeds[i][0][1],
75                     matchSeeds[i][1][0], matchSeeds[i][1][1],
76                     matches[i]);
77         }
78     }
79
80     /**
81      * @param m1Source match1 - src
82      * @param m1Destination match1 - dest
83      * @param m2Source match2 - src
84      * @param msDestination match2 - dest
85      * @param matches expected match output
86      *
87      */
88     private static void checkComparisonOfL3Match(String m1Source, String m1Destination,
89             String m2Source, String msDestination, boolean matches) {
90         Ipv4Match m1Layer3 = prepareIPv4Match(m1Source, m1Destination);
91         Ipv4Match m2Layer3 = prepareIPv4Match(m2Source, msDestination);
92         boolean comparisonResult;
93         try {
94             comparisonResult = FlowComparator.layer3MatchEquals(m1Layer3, m2Layer3);
95             Assert.assertEquals("failed to compare: "+m1Layer3+" vs. "+m2Layer3,
96                     matches, comparisonResult);
97         } catch (Exception e) {
98             LOG.error("failed to compare: {} vs. {}", m1Layer3, m2Layer3, e);
99             Assert.fail(e.getMessage());
100         }
101     }
102
103     private static Ipv4Match prepareIPv4Match(String source, String destination) {
104         Ipv4MatchBuilder ipv4MatchBuilder = new Ipv4MatchBuilder();
105         if (source != null) {
106             ipv4MatchBuilder.setIpv4Source(new Ipv4Prefix(source));
107         }
108         if (destination != null) {
109             ipv4MatchBuilder.setIpv4Destination(new Ipv4Prefix(destination));
110         }
111
112         return ipv4MatchBuilder.build();
113     }
114
115 }