Merge "Bug 1088 - flow id in operational not matched; after removal not removed compl...
[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.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;
24
25 /**
26  *
27  */
28 public class StatisticsUpdateCommiterTest {
29
30     private static final Logger LOG = LoggerFactory
31             .getLogger(StatisticsUpdateCommiterTest.class);
32
33     /**
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)}.
35      */
36     @Test
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"}},
43
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}},
48
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"}},
53
54                 {{null, null}, {null, "10.1.1.0/24"}},
55                 {{null, null}, {null, null}},
56         };
57
58         boolean[] matches = new boolean[] {
59                 true,
60                 false,
61                 false,
62                 false,
63
64                 false,
65                 false,
66                 false,
67                 true,
68
69                 false,
70                 false,
71                 false,
72                 true,
73
74                 false,
75                 true
76         };
77
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],
82                     matches[i]);
83         }
84     }
85
86     /**
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
92      *
93      */
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;
99         try {
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());
106         }
107     }
108
109     private static Ipv4Match prepareIPv4Match(String source, String destination) {
110         Ipv4MatchBuilder ipv4MatchBuilder = new Ipv4MatchBuilder();
111         if (source != null) {
112             ipv4MatchBuilder.setIpv4Source(new Ipv4Prefix(source));
113         }
114         if (destination != null) {
115             ipv4MatchBuilder.setIpv4Destination(new Ipv4Prefix(destination));
116         }
117
118         return ipv4MatchBuilder.build();
119     }
120     /**
121      * Test method for {@link org.opendaylight.controller.md.statistics.manager.FlowComparator#ethernetMatchEquals(EthernetMatch, EthernetMatch)
122      */
123     @Test
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"}},
132
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"}},
137
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}},
141
142                 {{null, null,null}, {null, null,"0800"}},
143                 {{null, null,null}, {null, null,null}},
144         };
145
146         boolean[] matches = new boolean[] {
147                 true,
148                 false,
149                 true,
150                 true,
151                 true,
152                 false,
153
154                 true,
155                 false,
156                 false,
157                 false,
158
159                 false,
160                 true,
161                 false,
162
163                 false,
164                 true
165         };
166
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],
171                     matches[i]);
172         }
173     }
174
175     /*
176      * @param ethernetMatch1
177      * @param ethernetMatch2
178      */
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;
184         try {
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());
191         }
192     }
193
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));
199         }
200         if (macAddressMask != null) {
201             ethernetSourceBuilder.setMask(new MacAddress(macAddressMask));
202         }
203         if(etherType != null){
204             EthernetTypeBuilder ethernetType = new EthernetTypeBuilder();
205             ethernetType.setType(new EtherType(Long.parseLong(etherType,16)));
206             ethernetMatchBuilder.setEthernetType(ethernetType.build());
207         }
208         ethernetMatchBuilder.setEthernetSource(ethernetSourceBuilder.build());
209
210         return ethernetMatchBuilder.build();
211     }
212 }