blob: 1938c05fbfffa628a53e0a26ccae431a18d9879b [file] [log] [blame]
Carmelo Casconef1db2842017-07-13 17:54:04 -04001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Carmelo Casconef1db2842017-07-13 17:54:04 -04003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.onosproject.net.pi.impl;
18
19import org.junit.Test;
zhiyong ke5395e642017-07-20 18:36:06 +080020import org.onlab.packet.EthType;
zhiyong ke5395e642017-07-20 18:36:06 +080021import org.onlab.packet.Ip4Address;
Carmelo Casconeb10194c2017-08-23 19:16:51 +020022import org.onlab.packet.Ip6Address;
23import org.onlab.packet.IpPrefix;
24import org.onlab.packet.MacAddress;
25import org.onlab.packet.MplsLabel;
26import org.onlab.packet.TpPort;
27import org.onlab.packet.VlanId;
zhiyong ke5395e642017-07-20 18:36:06 +080028import org.onosproject.net.PortNumber;
Carmelo Casconeb10194c2017-08-23 19:16:51 +020029import org.onosproject.net.flow.criteria.ArpHaCriterion;
30import org.onosproject.net.flow.criteria.ArpOpCriterion;
31import org.onosproject.net.flow.criteria.ArpPaCriterion;
Carmelo Casconef1db2842017-07-13 17:54:04 -040032import org.onosproject.net.flow.criteria.Criteria;
33import org.onosproject.net.flow.criteria.EthCriterion;
34import org.onosproject.net.flow.criteria.EthTypeCriterion;
35import org.onosproject.net.flow.criteria.IPCriterion;
zhiyong ke5395e642017-07-20 18:36:06 +080036import org.onosproject.net.flow.criteria.IPDscpCriterion;
Carmelo Casconeb10194c2017-08-23 19:16:51 +020037import org.onosproject.net.flow.criteria.IPEcnCriterion;
zhiyong ke5395e642017-07-20 18:36:06 +080038import org.onosproject.net.flow.criteria.IPProtocolCriterion;
39import org.onosproject.net.flow.criteria.IPv6ExthdrFlagsCriterion;
40import org.onosproject.net.flow.criteria.IPv6FlowLabelCriterion;
41import org.onosproject.net.flow.criteria.IPv6NDLinkLayerAddressCriterion;
42import org.onosproject.net.flow.criteria.IPv6NDTargetAddressCriterion;
43import org.onosproject.net.flow.criteria.IcmpCodeCriterion;
44import org.onosproject.net.flow.criteria.IcmpTypeCriterion;
45import org.onosproject.net.flow.criteria.Icmpv6CodeCriterion;
46import org.onosproject.net.flow.criteria.Icmpv6TypeCriterion;
47import org.onosproject.net.flow.criteria.MetadataCriterion;
48import org.onosproject.net.flow.criteria.MplsBosCriterion;
49import org.onosproject.net.flow.criteria.MplsCriterion;
50import org.onosproject.net.flow.criteria.MplsTcCriterion;
51import org.onosproject.net.flow.criteria.PbbIsidCriterion;
Carmelo Casconeb10194c2017-08-23 19:16:51 +020052import org.onosproject.net.flow.criteria.PortCriterion;
zhiyong ke5395e642017-07-20 18:36:06 +080053import org.onosproject.net.flow.criteria.SctpPortCriterion;
54import org.onosproject.net.flow.criteria.TcpFlagsCriterion;
55import org.onosproject.net.flow.criteria.TcpPortCriterion;
56import org.onosproject.net.flow.criteria.TunnelIdCriterion;
Carmelo Casconeb10194c2017-08-23 19:16:51 +020057import org.onosproject.net.flow.criteria.UdpPortCriterion;
zhiyong ke5395e642017-07-20 18:36:06 +080058import org.onosproject.net.flow.criteria.VlanIdCriterion;
Carmelo Casconeb10194c2017-08-23 19:16:51 +020059import org.onosproject.net.flow.criteria.VlanPcpCriterion;
Carmelo Cascone87892e22017-11-13 16:01:29 -080060import org.onosproject.net.pi.model.PiMatchFieldId;
Carmelo Casconef1db2842017-07-13 17:54:04 -040061import org.onosproject.net.pi.runtime.PiExactFieldMatch;
Carmelo Casconef1db2842017-07-13 17:54:04 -040062import org.onosproject.net.pi.runtime.PiLpmFieldMatch;
63import org.onosproject.net.pi.runtime.PiTernaryFieldMatch;
Carmelo Casconeb10194c2017-08-23 19:16:51 +020064
Carmelo Casconef1db2842017-07-13 17:54:04 -040065import java.util.Random;
Carmelo Casconeb10194c2017-08-23 19:16:51 +020066
Carmelo Casconef1db2842017-07-13 17:54:04 -040067import static org.hamcrest.CoreMatchers.is;
68import static org.hamcrest.MatcherAssert.assertThat;
69import static org.onosproject.net.pi.impl.CriterionTranslatorHelper.translateCriterion;
Carmelo Cascone87892e22017-11-13 16:01:29 -080070import static org.onosproject.net.pi.model.PiMatchType.EXACT;
71import static org.onosproject.net.pi.model.PiMatchType.LPM;
72import static org.onosproject.net.pi.model.PiMatchType.TERNARY;
Carmelo Casconef1db2842017-07-13 17:54:04 -040073
74/**
75 * Tests for CriterionTranslators.
76 */
77public class PiCriterionTranslatorsTest {
78
79 private Random random = new Random();
Carmelo Cascone87892e22017-11-13 16:01:29 -080080 private final PiMatchFieldId fieldId = PiMatchFieldId.of("foo.bar");
Carmelo Casconef1db2842017-07-13 17:54:04 -040081
82 @Test
83 public void testEthCriterion() throws Exception {
84 MacAddress value1 = MacAddress.valueOf(random.nextLong());
85 MacAddress value2 = MacAddress.valueOf(random.nextLong());
86 MacAddress mask = MacAddress.valueOf(random.nextLong());
87 int bitWidth = value1.toBytes().length * 8;
88
89 EthCriterion criterion = (EthCriterion) Criteria.matchEthDst(value1);
90 PiExactFieldMatch exactMatch = (PiExactFieldMatch) translateCriterion(criterion, fieldId, EXACT, bitWidth);
91
92 EthCriterion maskedCriterion = (EthCriterion) Criteria.matchEthDstMasked(value2, mask);
93 PiTernaryFieldMatch ternaryMatch = (PiTernaryFieldMatch) translateCriterion(maskedCriterion, fieldId, TERNARY,
zhiyong ke5395e642017-07-20 18:36:06 +080094 bitWidth);
Carmelo Casconef1db2842017-07-13 17:54:04 -040095
96 assertThat(exactMatch.value().asArray(), is(criterion.mac().toBytes()));
97 assertThat(ternaryMatch.value().asArray(), is(maskedCriterion.mac().toBytes()));
98 assertThat(ternaryMatch.mask().asArray(), is(maskedCriterion.mask().toBytes()));
99 }
100
101 @Test
102 public void testEthTypeCriterion() throws Exception {
103 EthType ethType = new EthType(random.nextInt());
104 int bitWidth = 16;
105
106 EthTypeCriterion criterion = (EthTypeCriterion) Criteria.matchEthType(ethType);
107
108 PiExactFieldMatch exactMatch = (PiExactFieldMatch) translateCriterion(criterion, fieldId, EXACT, bitWidth);
109
110 assertThat(exactMatch.value().asReadOnlyBuffer().getShort(), is(criterion.ethType().toShort()));
111 }
112
113 @Test
114 public void testIpCriterion() throws Exception {
115 IpPrefix prefix1 = IpPrefix.valueOf(random.nextInt(), random.nextInt(32));
116 int bitWidth = prefix1.address().toOctets().length * 8;
117
118 IPCriterion criterion = (IPCriterion) Criteria.matchIPDst(prefix1);
119
120 PiLpmFieldMatch lpmMatch = (PiLpmFieldMatch) translateCriterion(criterion, fieldId, LPM, bitWidth);
121
122 assertThat(lpmMatch.value().asArray(), is(criterion.ip().address().toOctets()));
123 assertThat(lpmMatch.prefixLength(), is(criterion.ip().prefixLength()));
124 }
125
zhiyong ke5395e642017-07-20 18:36:06 +0800126 @Test
127 public void testPortCriterion() throws Exception {
128 PortNumber portNumber = PortNumber.portNumber(random.nextLong());
129 int bitWidth = 64;
Carmelo Casconef1db2842017-07-13 17:54:04 -0400130
zhiyong ke5395e642017-07-20 18:36:06 +0800131 PortCriterion criterion = (PortCriterion) Criteria.matchInPort(portNumber);
132
133 PiExactFieldMatch exactMatch = (PiExactFieldMatch) translateCriterion(criterion, fieldId, EXACT, bitWidth);
134
135 assertThat(exactMatch.value().asReadOnlyBuffer().getLong(), is(criterion.port().toLong()));
136 }
137
138 @Test
139 public void testVlanIdCriterion() throws Exception {
140 VlanId vlanId = VlanId.vlanId((short) random.nextInt(255));
141 int bitWidth = 16;
142
143 VlanIdCriterion criterion = (VlanIdCriterion) Criteria.matchVlanId(vlanId);
144
145 PiExactFieldMatch exactMatch = (PiExactFieldMatch) translateCriterion(criterion, fieldId, EXACT, bitWidth);
146
147 assertThat(exactMatch.value().asReadOnlyBuffer().getShort(), is(criterion.vlanId().toShort()));
148 }
149
150 @Test
151 public void testUdpPortCriterion() throws Exception {
152 TpPort value1 = TpPort.tpPort(random.nextInt(65536));
153 TpPort value2 = TpPort.tpPort(random.nextInt(65536));
154 TpPort mask = TpPort.tpPort(random.nextInt(65536));
155 int bitWidth = 16;
156
157 UdpPortCriterion criterion = (UdpPortCriterion) Criteria.matchUdpDst(value1);
158 PiExactFieldMatch exactMatch = (PiExactFieldMatch) translateCriterion(criterion, fieldId, EXACT, bitWidth);
159
160 UdpPortCriterion maskedCriterion = (UdpPortCriterion) Criteria.matchUdpDstMasked(value2, mask);
161 PiTernaryFieldMatch ternaryMatch = (PiTernaryFieldMatch) translateCriterion(maskedCriterion, fieldId, TERNARY,
162 bitWidth);
163
164 assertThat(exactMatch.value().asReadOnlyBuffer().getShort(), is((short) criterion.udpPort().toInt()));
165 assertThat(ternaryMatch.value().asReadOnlyBuffer().getShort(), is((short) maskedCriterion.udpPort().toInt()));
166 assertThat(ternaryMatch.mask().asReadOnlyBuffer().getShort(), is((short) maskedCriterion.mask().toInt()));
167 }
168
169 @Test
170 public void testIPDscpCriterion() throws Exception {
171 byte[] ipDscp = new byte[1];
172 random.nextBytes(ipDscp);
173
174 IPDscpCriterion criterion = (IPDscpCriterion) Criteria.matchIPDscp(ipDscp[0]);
175 int bitWidth = 6;
176
177 PiExactFieldMatch exactMatch = (PiExactFieldMatch) translateCriterion(criterion, fieldId, EXACT, bitWidth);
178
179 assertThat(exactMatch.value().asReadOnlyBuffer().get(), is(criterion.ipDscp()));
180 }
181
182 @Test
183 public void testIPProtocolCriterion() throws Exception {
184 short proto = (short) random.nextInt(256);
185 int bitWidth = 16;
186
187 IPProtocolCriterion criterion = (IPProtocolCriterion) Criteria.matchIPProtocol(proto);
188
189 PiExactFieldMatch exactMatch = (PiExactFieldMatch) translateCriterion(criterion, fieldId, EXACT, bitWidth);
190
191 assertThat(exactMatch.value().asReadOnlyBuffer().getShort(), is(criterion.protocol()));
192 }
193
194 @Test
195 public void testIPv6ExthdrFlagsCriterion() throws Exception {
196 int exthdrFlags = random.nextInt();
197 int bitWidth = 32;
198
199 IPv6ExthdrFlagsCriterion criterion = (IPv6ExthdrFlagsCriterion) Criteria.matchIPv6ExthdrFlags(exthdrFlags);
200
201 PiExactFieldMatch exactMatch = (PiExactFieldMatch) translateCriterion(criterion, fieldId, EXACT, bitWidth);
202
203 assertThat(exactMatch.value().asReadOnlyBuffer().getInt(), is(criterion.exthdrFlags()));
204 }
205
206 @Test
207 public void testIPv6FlowLabelCriterion() throws Exception {
208 int flowLabel = random.nextInt();
209 int bitWidth = 32;
210
211 IPv6FlowLabelCriterion criterion = (IPv6FlowLabelCriterion) Criteria.matchIPv6FlowLabel(flowLabel);
212
213 PiExactFieldMatch exactMatch = (PiExactFieldMatch) translateCriterion(criterion, fieldId, EXACT, bitWidth);
214
215 assertThat(exactMatch.value().asReadOnlyBuffer().getInt(), is(criterion.flowLabel()));
216 }
217
218 @Test
219 public void testIPv6NDLinkLayerAddressCriterion() throws Exception {
220 MacAddress mac = MacAddress.valueOf(random.nextLong());
221 int bitWidth = mac.toBytes().length * 8;
222
223 IPv6NDLinkLayerAddressCriterion criterion = (IPv6NDLinkLayerAddressCriterion) Criteria
224 .matchIPv6NDSourceLinkLayerAddress(mac);
225
226 PiExactFieldMatch exactMatch = (PiExactFieldMatch) translateCriterion(criterion, fieldId, EXACT, bitWidth);
227
228 assertThat(exactMatch.value().asArray(), is(criterion.mac().toBytes()));
229 }
230
231 @Test
232 public void testIPv6NDTargetAddressCriterion() throws Exception {
233 Ip6Address targetAddress = Ip6Address.valueOf("2001:A304:6101:1::E0:F726:4E58");
234 int bitWidth = targetAddress.toOctets().length * 8;
235
236 IPv6NDTargetAddressCriterion criterion = (IPv6NDTargetAddressCriterion) Criteria
237 .matchIPv6NDTargetAddress(targetAddress);
238
239 PiExactFieldMatch exactMatch = (PiExactFieldMatch) translateCriterion(criterion, fieldId, EXACT, bitWidth);
240
241 assertThat(exactMatch.value().asArray(), is(criterion.targetAddress().toOctets()));
242 }
243
244 @Test
245 public void testIcmpCodeCriterion() throws Exception {
246 short icmpCode = (short) random.nextInt(256);
247 int bitWidth = 16;
248
249 IcmpCodeCriterion criterion = (IcmpCodeCriterion) Criteria.matchIcmpCode(icmpCode);
250
251 PiExactFieldMatch exactMatch = (PiExactFieldMatch) translateCriterion(criterion, fieldId, EXACT, bitWidth);
252
253 assertThat(exactMatch.value().asReadOnlyBuffer().getShort(), is(criterion.icmpCode()));
254 }
255
256 @Test
257 public void testIcmpTypeCriterion() throws Exception {
258 short icmpType = (short) random.nextInt(256);
259 int bitWidth = 16;
260
261 IcmpTypeCriterion criterion = (IcmpTypeCriterion) Criteria.matchIcmpType(icmpType);
262
263 PiExactFieldMatch exactMatch = (PiExactFieldMatch) translateCriterion(criterion, fieldId, EXACT, bitWidth);
264
265 assertThat(exactMatch.value().asReadOnlyBuffer().getShort(), is(criterion.icmpType()));
266 }
267
268 @Test
269 public void testIcmpv6CodeCriterion() throws Exception {
270 short icmpv6Code = (short) random.nextInt(256);
271 int bitWidth = 16;
272
273 Icmpv6CodeCriterion criterion = (Icmpv6CodeCriterion) Criteria.matchIcmpv6Code(icmpv6Code);
274
275 PiExactFieldMatch exactMatch = (PiExactFieldMatch) translateCriterion(criterion, fieldId, EXACT, bitWidth);
276
277 assertThat(exactMatch.value().asReadOnlyBuffer().getShort(), is(criterion.icmpv6Code()));
278 }
279
280 @Test
281 public void testIcmpv6TypeCriterion() throws Exception {
282 short icmpv6Type = (short) random.nextInt(256);
283 int bitWidth = 16;
284
285 Icmpv6TypeCriterion criterion = (Icmpv6TypeCriterion) Criteria.matchIcmpv6Type(icmpv6Type);
286
287 PiExactFieldMatch exactMatch = (PiExactFieldMatch) translateCriterion(criterion, fieldId, EXACT, bitWidth);
288
289 assertThat(exactMatch.value().asReadOnlyBuffer().getShort(), is(criterion.icmpv6Type()));
290 }
291
292 @Test
293 public void testMetadataCriterion() throws Exception {
294 long metadata = random.nextLong();
295 int bitWidth = 64;
296
297 MetadataCriterion criterion = (MetadataCriterion) Criteria.matchMetadata(metadata);
298
299 PiExactFieldMatch exactMatch = (PiExactFieldMatch) translateCriterion(criterion, fieldId, EXACT, bitWidth);
300
301 assertThat(exactMatch.value().asReadOnlyBuffer().getLong(), is(criterion.metadata()));
302 }
303
304 @Test
305 public void testMplsBosCriterion() throws Exception {
306 boolean mplsBos = random.nextBoolean();
307 int bitWidth = 32;
308 int bMplsBos = 0;
309
310 MplsBosCriterion criterion = (MplsBosCriterion) Criteria.matchMplsBos(mplsBos);
311
312 PiExactFieldMatch exactMatch = (PiExactFieldMatch) translateCriterion(criterion, fieldId, EXACT, bitWidth);
313
314 bMplsBos = (criterion.mplsBos()) ? 1 : 0;
315
316 assertThat(exactMatch.value().asReadOnlyBuffer().getInt(), is(bMplsBos));
317 }
318
319 @Test
320 public void testMplsCriterion() throws Exception {
321 MplsLabel mplsLabel = MplsLabel.mplsLabel(random.nextInt(2 ^ 20));
322 int bitWidth = 32;
323
324 MplsCriterion criterion = (MplsCriterion) Criteria.matchMplsLabel(mplsLabel);
325
326 PiExactFieldMatch exactMatch = (PiExactFieldMatch) translateCriterion(criterion, fieldId, EXACT, bitWidth);
327
328 assertThat(exactMatch.value().asReadOnlyBuffer().getInt(), is(criterion.label().toInt()));
329 }
330
331 @Test
332 public void testMplsTcCriterion() throws Exception {
333 byte[] mplsTc = new byte[1];
334 random.nextBytes(mplsTc);
335
336 int bitWidth = 16;
337
338 MplsTcCriterion criterion = (MplsTcCriterion) Criteria.matchMplsTc(mplsTc[0]);
339
340 PiExactFieldMatch exactMatch = (PiExactFieldMatch) translateCriterion(criterion, fieldId, EXACT, bitWidth);
341
Carmelo Casconeb10194c2017-08-23 19:16:51 +0200342 assertThat(exactMatch.value().asReadOnlyBuffer().get(1), is(criterion.tc()));
zhiyong ke5395e642017-07-20 18:36:06 +0800343 }
344
345 @Test
346 public void testPbbIsidCriterion() throws Exception {
347 int pbbIsid = random.nextInt();
348 int bitWidth = 32;
349
350 PbbIsidCriterion criterion = (PbbIsidCriterion) Criteria.matchPbbIsid(pbbIsid);
351
352 PiExactFieldMatch exactMatch = (PiExactFieldMatch) translateCriterion(criterion, fieldId, EXACT, bitWidth);
353
354 assertThat(exactMatch.value().asReadOnlyBuffer().getInt(), is(criterion.pbbIsid()));
355 }
356
357 @Test
358 public void testSctpPortCriterion() throws Exception {
359 TpPort value1 = TpPort.tpPort(random.nextInt(2 ^ 16));
360 TpPort value2 = TpPort.tpPort(random.nextInt(2 ^ 16));
361 TpPort mask = TpPort.tpPort(random.nextInt(2 ^ 16));
362
363 int bitWidth = 16;
364
365 SctpPortCriterion criterion = (SctpPortCriterion) Criteria.matchSctpDst(value1);
366 PiExactFieldMatch exactMatch = (PiExactFieldMatch) translateCriterion(criterion, fieldId, EXACT, bitWidth);
367
368 SctpPortCriterion maskedCriterion = (SctpPortCriterion) Criteria.matchSctpDstMasked(value2, mask);
369 PiTernaryFieldMatch ternaryMatch = (PiTernaryFieldMatch) translateCriterion(maskedCriterion, fieldId, TERNARY,
370 bitWidth);
371
372 assertThat(exactMatch.value().asReadOnlyBuffer().getShort(), is((short) criterion.sctpPort().toInt()));
373 assertThat(ternaryMatch.value().asReadOnlyBuffer().getShort(), is((short) maskedCriterion.sctpPort().toInt()));
374 assertThat(ternaryMatch.mask().asReadOnlyBuffer().getShort(), is((short) maskedCriterion.mask().toInt()));
375 }
376
377 @Test
378 public void testTcpFlagsCriterion() throws Exception {
379 int pbbIsid = random.nextInt(2 ^ 12);
380 int bitWidth = 12;
381
382 TcpFlagsCriterion criterion = (TcpFlagsCriterion) Criteria.matchTcpFlags(pbbIsid);
383
384 PiExactFieldMatch exactMatch = (PiExactFieldMatch) translateCriterion(criterion, fieldId, EXACT, bitWidth);
385
386 assertThat(exactMatch.value().asReadOnlyBuffer().getShort(), is((short) criterion.flags()));
387 }
388
389 @Test
390 public void testTcpPortCriterion() throws Exception {
391 TpPort value1 = TpPort.tpPort(random.nextInt(2 ^ 16));
392 TpPort value2 = TpPort.tpPort(random.nextInt(2 ^ 16));
393 TpPort mask = TpPort.tpPort(random.nextInt(2 ^ 16));
394
395 int bitWidth = 16;
396
397 TcpPortCriterion criterion = (TcpPortCriterion) Criteria.matchTcpDst(value1);
398 PiExactFieldMatch exactMatch = (PiExactFieldMatch) translateCriterion(criterion, fieldId, EXACT, bitWidth);
399
400 TcpPortCriterion maskedCriterion = (TcpPortCriterion) Criteria.matchTcpDstMasked(value2, mask);
401 PiTernaryFieldMatch ternaryMatch = (PiTernaryFieldMatch) translateCriterion(maskedCriterion, fieldId, TERNARY,
402 bitWidth);
403
404 assertThat(exactMatch.value().asReadOnlyBuffer().getShort(), is((short) criterion.tcpPort().toInt()));
405 assertThat(ternaryMatch.value().asReadOnlyBuffer().getShort(), is((short) maskedCriterion.tcpPort().toInt()));
406 assertThat(ternaryMatch.mask().asReadOnlyBuffer().getShort(), is((short) maskedCriterion.mask().toInt()));
407 }
408
409 @Test
410 public void testTunnelIdCriterion() throws Exception {
411 long tunnelId = random.nextLong();
412 int bitWidth = 64;
413
414 TunnelIdCriterion criterion = (TunnelIdCriterion) Criteria.matchTunnelId(tunnelId);
415
416 PiExactFieldMatch exactMatch = (PiExactFieldMatch) translateCriterion(criterion, fieldId, EXACT, bitWidth);
417
418 assertThat(exactMatch.value().asReadOnlyBuffer().getLong(), is(criterion.tunnelId()));
419 }
420
421
422 @Test
423 public void testVlanPcpCriterion() throws Exception {
424 byte[] vlanPcp = new byte[1];
425 random.nextBytes(vlanPcp);
426
427 int bitWidth = 3;
428
429 VlanPcpCriterion criterion = (VlanPcpCriterion) Criteria.matchVlanPcp(vlanPcp[0]);
430
431 PiExactFieldMatch exactMatch = (PiExactFieldMatch) translateCriterion(criterion, fieldId, EXACT, bitWidth);
432
433 assertThat(exactMatch.value().asReadOnlyBuffer().get(), is(criterion.priority()));
434 }
435
436 @Test
437 public void testArpHaCriterionn() throws Exception {
438 MacAddress mac = MacAddress.valueOf(random.nextLong());
439 int bitWidth = mac.toBytes().length * 8;
440
441 ArpHaCriterion criterion = (ArpHaCriterion) Criteria.matchArpTha(mac);
442
443 PiExactFieldMatch exactMatch = (PiExactFieldMatch) translateCriterion(criterion, fieldId, EXACT, bitWidth);
444
445 assertThat(exactMatch.value().asArray(), is(criterion.mac().toBytes()));
446 }
447
448 @Test
449 public void testArpOpCriterion() throws Exception {
450 int arpOp = random.nextInt();
451 int bitWidth = 32;
452
453 ArpOpCriterion criterion = (ArpOpCriterion) Criteria.matchArpOp(arpOp);
454
455 PiExactFieldMatch exactMatch = (PiExactFieldMatch) translateCriterion(criterion, fieldId, EXACT, bitWidth);
456
457 assertThat(exactMatch.value().asReadOnlyBuffer().getInt(), is(criterion.arpOp()));
458 }
459
460 @Test
461 public void testArpPaCriterion() throws Exception {
462 Ip4Address ip = Ip4Address.valueOf(random.nextInt());
463 int bitWidth = ip.toOctets().length * 8;
464
465 ArpPaCriterion criterion = (ArpPaCriterion) Criteria.matchArpTpa(ip);
466
467 PiExactFieldMatch exactMatch = (PiExactFieldMatch) translateCriterion(criterion, fieldId, EXACT, bitWidth);
468
469 assertThat(exactMatch.value().asReadOnlyBuffer().getInt(), is(criterion.ip().toInt()));
470 }
471
472 @Test
473 public void testIPEcnCriterion() throws Exception {
474 byte[] ipEcn = new byte[1];
475 random.nextBytes(ipEcn);
476
477 int bitWidth = 2;
478
479 IPEcnCriterion criterion = (IPEcnCriterion) Criteria.matchIPEcn(ipEcn[0]);
480
481 PiExactFieldMatch exactMatch = (PiExactFieldMatch) translateCriterion(criterion, fieldId, EXACT, bitWidth);
482
483 assertThat(exactMatch.value().asReadOnlyBuffer().get(), is(criterion.ipEcn()));
484 }
Carmelo Cascone87892e22017-11-13 16:01:29 -0800485}