2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.controller.md.statistics.manager;
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.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.EtherType;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetSourceBuilder;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetTypeBuilder;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.EthernetMatch;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.EthernetMatchBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv4Match;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv4MatchBuilder;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
28 public class StatisticsUpdateCommiterTest {
30 private static final Logger LOG = LoggerFactory
31 .getLogger(StatisticsUpdateCommiterTest.class);
34 * Test method for {@link org.opendaylight.controller.md.statistics.manager.StatisticsListener#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)}.
37 public void testLayer3MatchEquals() {
38 String[][][] matchSeeds = new String[][][] {
39 {{"10.1.2.0/24", "10.1.2.0/24"}, {"10.1.2.0/24", "10.1.2.0/24"}},
40 {{"10.1.2.0/24", "10.1.2.0/24"}, {"10.1.2.0/24", "10.1.1.0/24"}},
41 {{"10.1.1.0/24", "10.1.2.0/24"}, {"10.1.2.0/24", "10.1.2.0/24"}},
42 {{"10.1.1.0/24", "10.1.1.0/24"}, {"10.1.2.0/24", "10.1.2.0/24"}},
44 {{"10.1.1.0/24", null}, {"10.1.1.0/24", "10.1.2.0/24"}},
45 {{"10.1.1.0/24", null}, {"10.1.2.0/24", "10.1.2.0/24"}},
46 {{"10.1.1.0/24", null}, {"10.1.2.0/24", null}},
47 {{"10.1.1.0/24", null}, {"10.1.1.0/24", null}},
49 {{null, "10.1.1.0/24"}, {"10.1.2.0/24", "10.1.1.0/24"}},
50 {{null, "10.1.1.0/24"}, {"10.1.2.0/24", "10.1.2.0/24"}},
51 {{null, "10.1.1.0/24"}, {null, "10.1.2.0/24"}},
52 {{null, "10.1.1.0/24"}, {null, "10.1.1.0/24"}},
54 {{null, null}, {null, "10.1.1.0/24"}},
55 {{null, null}, {null, null}},
58 boolean[] matches = new boolean[] {
78 for (int i = 0; i < matches.length; i++) {
79 checkComparisonOfL3Match(
80 matchSeeds[i][0][0], matchSeeds[i][0][1],
81 matchSeeds[i][1][0], matchSeeds[i][1][1],
87 * @param m1Source match1 - src
88 * @param m1Destination match1 - dest
89 * @param m2Source match2 - src
90 * @param msDestination match2 - dest
91 * @param matches expected match output
94 private static void checkComparisonOfL3Match(String m1Source, String m1Destination,
95 String m2Source, String msDestination, boolean matches) {
96 Ipv4Match m1Layer3 = prepareIPv4Match(m1Source, m1Destination);
97 Ipv4Match m2Layer3 = prepareIPv4Match(m2Source, msDestination);
98 boolean comparisonResult;
100 comparisonResult = FlowComparator.layer3MatchEquals(m1Layer3, m2Layer3);
101 Assert.assertEquals("failed to compare: "+m1Layer3+" vs. "+m2Layer3,
102 matches, comparisonResult);
103 } catch (Exception e) {
104 LOG.error("failed to compare: {} vs. {}", m1Layer3, m2Layer3, e);
105 Assert.fail(e.getMessage());
109 private static Ipv4Match prepareIPv4Match(String source, String destination) {
110 Ipv4MatchBuilder ipv4MatchBuilder = new Ipv4MatchBuilder();
111 if (source != null) {
112 ipv4MatchBuilder.setIpv4Source(new Ipv4Prefix(source));
114 if (destination != null) {
115 ipv4MatchBuilder.setIpv4Destination(new Ipv4Prefix(destination));
118 return ipv4MatchBuilder.build();
121 * Test method for {@link org.opendaylight.controller.md.statistics.manager.FlowComparator#ethernetMatchEquals(EthernetMatch, EthernetMatch)
124 public void testEthernetMatchEquals() {
125 String[][][] ethernetMatchSeeds = new String[][][] {
126 {{"aa:bb:cc:dd:ee:ff", "ff:ff:ff:ff:ff:ff","0800"}, {"aa:bb:cc:dd:ee:ff", "ff:ff:ff:ff:ff:ff","0800"}},
127 {{"aa:bb:cc:dd:ee:ff", "ff:ff:ff:ff:ff:ff","0800"}, {"aa:bb:bc:cd:ee:ff", "ff:ff:ff:ff:ff:ff","0800"}},
128 {{"aa:bb:cc:dd:ee:ff", "ff:ff:ff:ff:ff:ff","0800"}, {"AA:BB:CC:DD:EE:FF", "ff:ff:ff:ff:ff:ff","0800"}},
129 {{"AA:BB:CC:dd:ee:ff", "ff:ff:ff:ff:ff:ff","0800"}, {"aa:bb:cc:dd:ee:ff", "ff:ff:ff:ff:ff:ff","0800"}},
130 {{"AA:BB:CC:dd:ee:ff", "ff:ff:ff:ff:ff:ff","0800"}, {"aa:bb:cc:dd:ee:ff", "FF:FF:FF:FF:FF:FF","0800"}},
131 {{"AA:BB:CC:dd:ee:ff", "ff:ff:ff:ee:ee:ee","0800"}, {"aa:bb:cc:dd:ee:ff", "FF:FF:FF:FF:FF:FF","0800"}},
133 {{"AA:BB:CC:dd:ee:ff", null,"0800"}, {"aa:bb:cc:dd:ee:ff", null,"0800"}},
134 {{"AA:BB:CC:dd:ee:ff", null,"0800"}, {"aa:bb:cc:dd:ee:ff", null,"0806"}},
135 {{"AA:BB:CC:dd:ee:ff", null,"0800"}, {"aa:bb:cc:dd:ee:ff", "FF:FF:FF:FF:FF:FF","0800"}},
136 {{"AA:BB:CC:dd:ee:ff", null,"0800"}, {null, "FF:FF:FF:FF:FF:FF","0800"}},
138 {{"AA:BB:CC:dd:ee:ff", "ff:ff:ff:ff:ff:ff",null}, {null, "FF:FF:FF:FF:FF:FF","0800"}},
139 {{"AA:BB:CC:dd:ee:ff", "ff:ff:ff:ff:ff:ff",null}, {"aa:bb:cc:dd:ee:ff", "FF:FF:FF:FF:FF:FF",null}},
140 {{"AA:BB:CC:dd:ee:ff", "ff:ff:ff:ff:ff:ff",null}, {null, "FF:FF:FF:FF:FF:FF",null}},
142 {{null, null,null}, {null, null,"0800"}},
143 {{null, null,null}, {null, null,null}},
146 boolean[] matches = new boolean[] {
167 for (int i = 0; i < matches.length; i++) {
168 checkComparisonOfEthernetMatch(
169 ethernetMatchSeeds[i][0][0], ethernetMatchSeeds[i][0][1],ethernetMatchSeeds[i][0][2],
170 ethernetMatchSeeds[i][1][0], ethernetMatchSeeds[i][1][1],ethernetMatchSeeds[i][1][2],
176 * @param ethernetMatch1
177 * @param ethernetMatch2
179 private static void checkComparisonOfEthernetMatch(String macAddress1, String macAddressMask1,String etherType1,
180 String macAddress2, String macAddressMask2,String etherType2, boolean expectedResult) {
181 EthernetMatch ethernetMatch1 = prepareEthernetMatch(macAddress1, macAddressMask1,etherType1);
182 EthernetMatch ethernetMatch2 = prepareEthernetMatch(macAddress2, macAddressMask2,etherType2);
183 boolean comparisonResult;
185 comparisonResult = FlowComparator.ethernetMatchEquals(ethernetMatch1, ethernetMatch2);
186 Assert.assertEquals("failed to compare: "+ethernetMatch1+" vs. "+ethernetMatch2,
187 expectedResult, comparisonResult);
188 } catch (Exception e) {
189 LOG.error("failed to compare: {} vs. {}", ethernetMatch1, ethernetMatch2, e);
190 Assert.fail(e.getMessage());
194 private static EthernetMatch prepareEthernetMatch(String macAddress, String macAddressMask, String etherType) {
195 EthernetMatchBuilder ethernetMatchBuilder = new EthernetMatchBuilder();
196 EthernetSourceBuilder ethernetSourceBuilder = new EthernetSourceBuilder();
197 if (macAddress != null) {
198 ethernetSourceBuilder.setAddress(new MacAddress(macAddress));
200 if (macAddressMask != null) {
201 ethernetSourceBuilder.setMask(new MacAddress(macAddressMask));
203 if(etherType != null){
204 EthernetTypeBuilder ethernetType = new EthernetTypeBuilder();
205 ethernetType.setType(new EtherType(Long.parseLong(etherType,16)));
206 ethernetMatchBuilder.setEthernetType(ethernetType.build());
208 ethernetMatchBuilder.setEthernetSource(ethernetSourceBuilder.build());
210 return ethernetMatchBuilder.build();