blob: ce3584f732bf447b2eab769b37183b431fbff4eb [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;
Yi Tsengdbe05602017-11-17 18:02:43 -080028import org.onlab.util.ImmutableByteSequence;
zhiyong ke5395e642017-07-20 18:36:06 +080029import org.onosproject.net.PortNumber;
Carmelo Casconeb10194c2017-08-23 19:16:51 +020030import org.onosproject.net.flow.criteria.ArpHaCriterion;
31import org.onosproject.net.flow.criteria.ArpOpCriterion;
32import org.onosproject.net.flow.criteria.ArpPaCriterion;
Carmelo Casconef1db2842017-07-13 17:54:04 -040033import org.onosproject.net.flow.criteria.Criteria;
34import org.onosproject.net.flow.criteria.EthCriterion;
35import org.onosproject.net.flow.criteria.EthTypeCriterion;
36import org.onosproject.net.flow.criteria.IPCriterion;
zhiyong ke5395e642017-07-20 18:36:06 +080037import org.onosproject.net.flow.criteria.IPDscpCriterion;
Carmelo Casconeb10194c2017-08-23 19:16:51 +020038import org.onosproject.net.flow.criteria.IPEcnCriterion;
zhiyong ke5395e642017-07-20 18:36:06 +080039import org.onosproject.net.flow.criteria.IPProtocolCriterion;
40import org.onosproject.net.flow.criteria.IPv6ExthdrFlagsCriterion;
41import org.onosproject.net.flow.criteria.IPv6FlowLabelCriterion;
42import org.onosproject.net.flow.criteria.IPv6NDLinkLayerAddressCriterion;
43import org.onosproject.net.flow.criteria.IPv6NDTargetAddressCriterion;
44import org.onosproject.net.flow.criteria.IcmpCodeCriterion;
45import org.onosproject.net.flow.criteria.IcmpTypeCriterion;
46import org.onosproject.net.flow.criteria.Icmpv6CodeCriterion;
47import org.onosproject.net.flow.criteria.Icmpv6TypeCriterion;
48import org.onosproject.net.flow.criteria.MetadataCriterion;
49import org.onosproject.net.flow.criteria.MplsBosCriterion;
50import org.onosproject.net.flow.criteria.MplsCriterion;
51import org.onosproject.net.flow.criteria.MplsTcCriterion;
52import org.onosproject.net.flow.criteria.PbbIsidCriterion;
Carmelo Casconeb10194c2017-08-23 19:16:51 +020053import org.onosproject.net.flow.criteria.PortCriterion;
zhiyong ke5395e642017-07-20 18:36:06 +080054import org.onosproject.net.flow.criteria.SctpPortCriterion;
55import org.onosproject.net.flow.criteria.TcpFlagsCriterion;
56import org.onosproject.net.flow.criteria.TcpPortCriterion;
57import org.onosproject.net.flow.criteria.TunnelIdCriterion;
Carmelo Casconeb10194c2017-08-23 19:16:51 +020058import org.onosproject.net.flow.criteria.UdpPortCriterion;
zhiyong ke5395e642017-07-20 18:36:06 +080059import org.onosproject.net.flow.criteria.VlanIdCriterion;
Carmelo Casconeb10194c2017-08-23 19:16:51 +020060import org.onosproject.net.flow.criteria.VlanPcpCriterion;
Carmelo Cascone87892e22017-11-13 16:01:29 -080061import org.onosproject.net.pi.model.PiMatchFieldId;
Carmelo Casconef1db2842017-07-13 17:54:04 -040062import org.onosproject.net.pi.runtime.PiExactFieldMatch;
Carmelo Casconef1db2842017-07-13 17:54:04 -040063import org.onosproject.net.pi.runtime.PiLpmFieldMatch;
64import org.onosproject.net.pi.runtime.PiTernaryFieldMatch;
Carmelo Casconeb10194c2017-08-23 19:16:51 +020065
Carmelo Casconef1db2842017-07-13 17:54:04 -040066import java.util.Random;
Carmelo Casconeb10194c2017-08-23 19:16:51 +020067
Carmelo Casconef1db2842017-07-13 17:54:04 -040068import static org.hamcrest.CoreMatchers.is;
69import static org.hamcrest.MatcherAssert.assertThat;
70import static org.onosproject.net.pi.impl.CriterionTranslatorHelper.translateCriterion;
Carmelo Cascone87892e22017-11-13 16:01:29 -080071import static org.onosproject.net.pi.model.PiMatchType.EXACT;
72import static org.onosproject.net.pi.model.PiMatchType.LPM;
73import static org.onosproject.net.pi.model.PiMatchType.TERNARY;
Carmelo Casconef1db2842017-07-13 17:54:04 -040074
75/**
76 * Tests for CriterionTranslators.
77 */
78public class PiCriterionTranslatorsTest {
79
80 private Random random = new Random();
Carmelo Cascone87892e22017-11-13 16:01:29 -080081 private final PiMatchFieldId fieldId = PiMatchFieldId.of("foo.bar");
Carmelo Casconef1db2842017-07-13 17:54:04 -040082
83 @Test
84 public void testEthCriterion() throws Exception {
85 MacAddress value1 = MacAddress.valueOf(random.nextLong());
86 MacAddress value2 = MacAddress.valueOf(random.nextLong());
87 MacAddress mask = MacAddress.valueOf(random.nextLong());
88 int bitWidth = value1.toBytes().length * 8;
89
90 EthCriterion criterion = (EthCriterion) Criteria.matchEthDst(value1);
91 PiExactFieldMatch exactMatch = (PiExactFieldMatch) translateCriterion(criterion, fieldId, EXACT, bitWidth);
92
93 EthCriterion maskedCriterion = (EthCriterion) Criteria.matchEthDstMasked(value2, mask);
94 PiTernaryFieldMatch ternaryMatch = (PiTernaryFieldMatch) translateCriterion(maskedCriterion, fieldId, TERNARY,
zhiyong ke5395e642017-07-20 18:36:06 +080095 bitWidth);
Carmelo Casconef1db2842017-07-13 17:54:04 -040096
97 assertThat(exactMatch.value().asArray(), is(criterion.mac().toBytes()));
98 assertThat(ternaryMatch.value().asArray(), is(maskedCriterion.mac().toBytes()));
99 assertThat(ternaryMatch.mask().asArray(), is(maskedCriterion.mask().toBytes()));
100 }
101
102 @Test
103 public void testEthTypeCriterion() throws Exception {
104 EthType ethType = new EthType(random.nextInt());
105 int bitWidth = 16;
106
107 EthTypeCriterion criterion = (EthTypeCriterion) Criteria.matchEthType(ethType);
108
109 PiExactFieldMatch exactMatch = (PiExactFieldMatch) translateCriterion(criterion, fieldId, EXACT, bitWidth);
110
111 assertThat(exactMatch.value().asReadOnlyBuffer().getShort(), is(criterion.ethType().toShort()));
112 }
113
114 @Test
115 public void testIpCriterion() throws Exception {
116 IpPrefix prefix1 = IpPrefix.valueOf(random.nextInt(), random.nextInt(32));
117 int bitWidth = prefix1.address().toOctets().length * 8;
118
119 IPCriterion criterion = (IPCriterion) Criteria.matchIPDst(prefix1);
120
121 PiLpmFieldMatch lpmMatch = (PiLpmFieldMatch) translateCriterion(criterion, fieldId, LPM, bitWidth);
122
123 assertThat(lpmMatch.value().asArray(), is(criterion.ip().address().toOctets()));
124 assertThat(lpmMatch.prefixLength(), is(criterion.ip().prefixLength()));
125 }
126
zhiyong ke5395e642017-07-20 18:36:06 +0800127 @Test
128 public void testPortCriterion() throws Exception {
129 PortNumber portNumber = PortNumber.portNumber(random.nextLong());
130 int bitWidth = 64;
Carmelo Casconef1db2842017-07-13 17:54:04 -0400131
zhiyong ke5395e642017-07-20 18:36:06 +0800132 PortCriterion criterion = (PortCriterion) Criteria.matchInPort(portNumber);
133
134 PiExactFieldMatch exactMatch = (PiExactFieldMatch) translateCriterion(criterion, fieldId, EXACT, bitWidth);
135
136 assertThat(exactMatch.value().asReadOnlyBuffer().getLong(), is(criterion.port().toLong()));
137 }
138
139 @Test
140 public void testVlanIdCriterion() throws Exception {
141 VlanId vlanId = VlanId.vlanId((short) random.nextInt(255));
142 int bitWidth = 16;
143
144 VlanIdCriterion criterion = (VlanIdCriterion) Criteria.matchVlanId(vlanId);
145
146 PiExactFieldMatch exactMatch = (PiExactFieldMatch) translateCriterion(criterion, fieldId, EXACT, bitWidth);
147
148 assertThat(exactMatch.value().asReadOnlyBuffer().getShort(), is(criterion.vlanId().toShort()));
149 }
150
151 @Test
152 public void testUdpPortCriterion() throws Exception {
153 TpPort value1 = TpPort.tpPort(random.nextInt(65536));
154 TpPort value2 = TpPort.tpPort(random.nextInt(65536));
155 TpPort mask = TpPort.tpPort(random.nextInt(65536));
156 int bitWidth = 16;
157
158 UdpPortCriterion criterion = (UdpPortCriterion) Criteria.matchUdpDst(value1);
159 PiExactFieldMatch exactMatch = (PiExactFieldMatch) translateCriterion(criterion, fieldId, EXACT, bitWidth);
160
161 UdpPortCriterion maskedCriterion = (UdpPortCriterion) Criteria.matchUdpDstMasked(value2, mask);
162 PiTernaryFieldMatch ternaryMatch = (PiTernaryFieldMatch) translateCriterion(maskedCriterion, fieldId, TERNARY,
163 bitWidth);
164
165 assertThat(exactMatch.value().asReadOnlyBuffer().getShort(), is((short) criterion.udpPort().toInt()));
166 assertThat(ternaryMatch.value().asReadOnlyBuffer().getShort(), is((short) maskedCriterion.udpPort().toInt()));
167 assertThat(ternaryMatch.mask().asReadOnlyBuffer().getShort(), is((short) maskedCriterion.mask().toInt()));
168 }
169
170 @Test
171 public void testIPDscpCriterion() throws Exception {
172 byte[] ipDscp = new byte[1];
173 random.nextBytes(ipDscp);
174
175 IPDscpCriterion criterion = (IPDscpCriterion) Criteria.matchIPDscp(ipDscp[0]);
176 int bitWidth = 6;
177
178 PiExactFieldMatch exactMatch = (PiExactFieldMatch) translateCriterion(criterion, fieldId, EXACT, bitWidth);
179
180 assertThat(exactMatch.value().asReadOnlyBuffer().get(), is(criterion.ipDscp()));
181 }
182
183 @Test
184 public void testIPProtocolCriterion() throws Exception {
185 short proto = (short) random.nextInt(256);
186 int bitWidth = 16;
187
188 IPProtocolCriterion criterion = (IPProtocolCriterion) Criteria.matchIPProtocol(proto);
189
190 PiExactFieldMatch exactMatch = (PiExactFieldMatch) translateCriterion(criterion, fieldId, EXACT, bitWidth);
191
192 assertThat(exactMatch.value().asReadOnlyBuffer().getShort(), is(criterion.protocol()));
193 }
194
195 @Test
196 public void testIPv6ExthdrFlagsCriterion() throws Exception {
197 int exthdrFlags = random.nextInt();
198 int bitWidth = 32;
199
200 IPv6ExthdrFlagsCriterion criterion = (IPv6ExthdrFlagsCriterion) Criteria.matchIPv6ExthdrFlags(exthdrFlags);
201
202 PiExactFieldMatch exactMatch = (PiExactFieldMatch) translateCriterion(criterion, fieldId, EXACT, bitWidth);
203
204 assertThat(exactMatch.value().asReadOnlyBuffer().getInt(), is(criterion.exthdrFlags()));
205 }
206
207 @Test
208 public void testIPv6FlowLabelCriterion() throws Exception {
209 int flowLabel = random.nextInt();
210 int bitWidth = 32;
211
212 IPv6FlowLabelCriterion criterion = (IPv6FlowLabelCriterion) Criteria.matchIPv6FlowLabel(flowLabel);
213
214 PiExactFieldMatch exactMatch = (PiExactFieldMatch) translateCriterion(criterion, fieldId, EXACT, bitWidth);
215
216 assertThat(exactMatch.value().asReadOnlyBuffer().getInt(), is(criterion.flowLabel()));
217 }
218
219 @Test
220 public void testIPv6NDLinkLayerAddressCriterion() throws Exception {
221 MacAddress mac = MacAddress.valueOf(random.nextLong());
222 int bitWidth = mac.toBytes().length * 8;
223
224 IPv6NDLinkLayerAddressCriterion criterion = (IPv6NDLinkLayerAddressCriterion) Criteria
225 .matchIPv6NDSourceLinkLayerAddress(mac);
226
227 PiExactFieldMatch exactMatch = (PiExactFieldMatch) translateCriterion(criterion, fieldId, EXACT, bitWidth);
228
229 assertThat(exactMatch.value().asArray(), is(criterion.mac().toBytes()));
230 }
231
232 @Test
233 public void testIPv6NDTargetAddressCriterion() throws Exception {
234 Ip6Address targetAddress = Ip6Address.valueOf("2001:A304:6101:1::E0:F726:4E58");
235 int bitWidth = targetAddress.toOctets().length * 8;
236
237 IPv6NDTargetAddressCriterion criterion = (IPv6NDTargetAddressCriterion) Criteria
238 .matchIPv6NDTargetAddress(targetAddress);
239
240 PiExactFieldMatch exactMatch = (PiExactFieldMatch) translateCriterion(criterion, fieldId, EXACT, bitWidth);
241
242 assertThat(exactMatch.value().asArray(), is(criterion.targetAddress().toOctets()));
243 }
244
245 @Test
246 public void testIcmpCodeCriterion() throws Exception {
247 short icmpCode = (short) random.nextInt(256);
248 int bitWidth = 16;
249
250 IcmpCodeCriterion criterion = (IcmpCodeCriterion) Criteria.matchIcmpCode(icmpCode);
251
252 PiExactFieldMatch exactMatch = (PiExactFieldMatch) translateCriterion(criterion, fieldId, EXACT, bitWidth);
253
254 assertThat(exactMatch.value().asReadOnlyBuffer().getShort(), is(criterion.icmpCode()));
255 }
256
257 @Test
258 public void testIcmpTypeCriterion() throws Exception {
259 short icmpType = (short) random.nextInt(256);
260 int bitWidth = 16;
261
262 IcmpTypeCriterion criterion = (IcmpTypeCriterion) Criteria.matchIcmpType(icmpType);
263
264 PiExactFieldMatch exactMatch = (PiExactFieldMatch) translateCriterion(criterion, fieldId, EXACT, bitWidth);
265
266 assertThat(exactMatch.value().asReadOnlyBuffer().getShort(), is(criterion.icmpType()));
267 }
268
269 @Test
270 public void testIcmpv6CodeCriterion() throws Exception {
271 short icmpv6Code = (short) random.nextInt(256);
272 int bitWidth = 16;
273
274 Icmpv6CodeCriterion criterion = (Icmpv6CodeCriterion) Criteria.matchIcmpv6Code(icmpv6Code);
275
276 PiExactFieldMatch exactMatch = (PiExactFieldMatch) translateCriterion(criterion, fieldId, EXACT, bitWidth);
277
278 assertThat(exactMatch.value().asReadOnlyBuffer().getShort(), is(criterion.icmpv6Code()));
279 }
280
281 @Test
282 public void testIcmpv6TypeCriterion() throws Exception {
283 short icmpv6Type = (short) random.nextInt(256);
284 int bitWidth = 16;
285
286 Icmpv6TypeCriterion criterion = (Icmpv6TypeCriterion) Criteria.matchIcmpv6Type(icmpv6Type);
287
288 PiExactFieldMatch exactMatch = (PiExactFieldMatch) translateCriterion(criterion, fieldId, EXACT, bitWidth);
289
290 assertThat(exactMatch.value().asReadOnlyBuffer().getShort(), is(criterion.icmpv6Type()));
291 }
292
293 @Test
294 public void testMetadataCriterion() throws Exception {
295 long metadata = random.nextLong();
296 int bitWidth = 64;
297
298 MetadataCriterion criterion = (MetadataCriterion) Criteria.matchMetadata(metadata);
299
300 PiExactFieldMatch exactMatch = (PiExactFieldMatch) translateCriterion(criterion, fieldId, EXACT, bitWidth);
301
302 assertThat(exactMatch.value().asReadOnlyBuffer().getLong(), is(criterion.metadata()));
303 }
304
305 @Test
306 public void testMplsBosCriterion() throws Exception {
307 boolean mplsBos = random.nextBoolean();
308 int bitWidth = 32;
309 int bMplsBos = 0;
310
311 MplsBosCriterion criterion = (MplsBosCriterion) Criteria.matchMplsBos(mplsBos);
312
313 PiExactFieldMatch exactMatch = (PiExactFieldMatch) translateCriterion(criterion, fieldId, EXACT, bitWidth);
314
315 bMplsBos = (criterion.mplsBos()) ? 1 : 0;
316
317 assertThat(exactMatch.value().asReadOnlyBuffer().getInt(), is(bMplsBos));
318 }
319
320 @Test
321 public void testMplsCriterion() throws Exception {
Sean Condon0bd777c2021-01-01 14:23:29 +0000322 MplsLabel mplsLabel = MplsLabel.mplsLabel(random.nextInt(1 << 20));
zhiyong ke5395e642017-07-20 18:36:06 +0800323 int bitWidth = 32;
324
325 MplsCriterion criterion = (MplsCriterion) Criteria.matchMplsLabel(mplsLabel);
326
327 PiExactFieldMatch exactMatch = (PiExactFieldMatch) translateCriterion(criterion, fieldId, EXACT, bitWidth);
328
329 assertThat(exactMatch.value().asReadOnlyBuffer().getInt(), is(criterion.label().toInt()));
330 }
331
332 @Test
333 public void testMplsTcCriterion() throws Exception {
334 byte[] mplsTc = new byte[1];
335 random.nextBytes(mplsTc);
336
337 int bitWidth = 16;
338
339 MplsTcCriterion criterion = (MplsTcCriterion) Criteria.matchMplsTc(mplsTc[0]);
340
341 PiExactFieldMatch exactMatch = (PiExactFieldMatch) translateCriterion(criterion, fieldId, EXACT, bitWidth);
342
Carmelo Casconeb10194c2017-08-23 19:16:51 +0200343 assertThat(exactMatch.value().asReadOnlyBuffer().get(1), is(criterion.tc()));
zhiyong ke5395e642017-07-20 18:36:06 +0800344 }
345
346 @Test
347 public void testPbbIsidCriterion() throws Exception {
348 int pbbIsid = random.nextInt();
349 int bitWidth = 32;
350
351 PbbIsidCriterion criterion = (PbbIsidCriterion) Criteria.matchPbbIsid(pbbIsid);
352
353 PiExactFieldMatch exactMatch = (PiExactFieldMatch) translateCriterion(criterion, fieldId, EXACT, bitWidth);
354
355 assertThat(exactMatch.value().asReadOnlyBuffer().getInt(), is(criterion.pbbIsid()));
356 }
357
358 @Test
359 public void testSctpPortCriterion() throws Exception {
Sean Condon0bd777c2021-01-01 14:23:29 +0000360 TpPort value1 = TpPort.tpPort(random.nextInt(1 << 16));
361 TpPort value2 = TpPort.tpPort(random.nextInt(1 << 16));
362 TpPort mask = TpPort.tpPort(random.nextInt(1 << 16));
zhiyong ke5395e642017-07-20 18:36:06 +0800363
364 int bitWidth = 16;
365
366 SctpPortCriterion criterion = (SctpPortCriterion) Criteria.matchSctpDst(value1);
367 PiExactFieldMatch exactMatch = (PiExactFieldMatch) translateCriterion(criterion, fieldId, EXACT, bitWidth);
368
369 SctpPortCriterion maskedCriterion = (SctpPortCriterion) Criteria.matchSctpDstMasked(value2, mask);
370 PiTernaryFieldMatch ternaryMatch = (PiTernaryFieldMatch) translateCriterion(maskedCriterion, fieldId, TERNARY,
371 bitWidth);
372
373 assertThat(exactMatch.value().asReadOnlyBuffer().getShort(), is((short) criterion.sctpPort().toInt()));
374 assertThat(ternaryMatch.value().asReadOnlyBuffer().getShort(), is((short) maskedCriterion.sctpPort().toInt()));
375 assertThat(ternaryMatch.mask().asReadOnlyBuffer().getShort(), is((short) maskedCriterion.mask().toInt()));
376 }
377
378 @Test
379 public void testTcpFlagsCriterion() throws Exception {
Sean Condon0bd777c2021-01-01 14:23:29 +0000380 int pbbIsid = random.nextInt(1 << 12);
zhiyong ke5395e642017-07-20 18:36:06 +0800381 int bitWidth = 12;
382
383 TcpFlagsCriterion criterion = (TcpFlagsCriterion) Criteria.matchTcpFlags(pbbIsid);
384
385 PiExactFieldMatch exactMatch = (PiExactFieldMatch) translateCriterion(criterion, fieldId, EXACT, bitWidth);
386
387 assertThat(exactMatch.value().asReadOnlyBuffer().getShort(), is((short) criterion.flags()));
388 }
389
390 @Test
391 public void testTcpPortCriterion() throws Exception {
Sean Condon0bd777c2021-01-01 14:23:29 +0000392 TpPort value1 = TpPort.tpPort(random.nextInt(1 << 16));
393 TpPort value2 = TpPort.tpPort(random.nextInt(1 << 16));
394 TpPort mask = TpPort.tpPort(random.nextInt(1 << 16));
zhiyong ke5395e642017-07-20 18:36:06 +0800395
396 int bitWidth = 16;
397
398 TcpPortCriterion criterion = (TcpPortCriterion) Criteria.matchTcpDst(value1);
399 PiExactFieldMatch exactMatch = (PiExactFieldMatch) translateCriterion(criterion, fieldId, EXACT, bitWidth);
400
401 TcpPortCriterion maskedCriterion = (TcpPortCriterion) Criteria.matchTcpDstMasked(value2, mask);
402 PiTernaryFieldMatch ternaryMatch = (PiTernaryFieldMatch) translateCriterion(maskedCriterion, fieldId, TERNARY,
403 bitWidth);
404
405 assertThat(exactMatch.value().asReadOnlyBuffer().getShort(), is((short) criterion.tcpPort().toInt()));
406 assertThat(ternaryMatch.value().asReadOnlyBuffer().getShort(), is((short) maskedCriterion.tcpPort().toInt()));
407 assertThat(ternaryMatch.mask().asReadOnlyBuffer().getShort(), is((short) maskedCriterion.mask().toInt()));
408 }
409
410 @Test
411 public void testTunnelIdCriterion() throws Exception {
412 long tunnelId = random.nextLong();
413 int bitWidth = 64;
414
415 TunnelIdCriterion criterion = (TunnelIdCriterion) Criteria.matchTunnelId(tunnelId);
416
417 PiExactFieldMatch exactMatch = (PiExactFieldMatch) translateCriterion(criterion, fieldId, EXACT, bitWidth);
418
419 assertThat(exactMatch.value().asReadOnlyBuffer().getLong(), is(criterion.tunnelId()));
420 }
421
422
423 @Test
424 public void testVlanPcpCriterion() throws Exception {
425 byte[] vlanPcp = new byte[1];
426 random.nextBytes(vlanPcp);
427
428 int bitWidth = 3;
429
430 VlanPcpCriterion criterion = (VlanPcpCriterion) Criteria.matchVlanPcp(vlanPcp[0]);
431
432 PiExactFieldMatch exactMatch = (PiExactFieldMatch) translateCriterion(criterion, fieldId, EXACT, bitWidth);
433
434 assertThat(exactMatch.value().asReadOnlyBuffer().get(), is(criterion.priority()));
435 }
436
437 @Test
438 public void testArpHaCriterionn() throws Exception {
439 MacAddress mac = MacAddress.valueOf(random.nextLong());
440 int bitWidth = mac.toBytes().length * 8;
441
442 ArpHaCriterion criterion = (ArpHaCriterion) Criteria.matchArpTha(mac);
443
444 PiExactFieldMatch exactMatch = (PiExactFieldMatch) translateCriterion(criterion, fieldId, EXACT, bitWidth);
445
446 assertThat(exactMatch.value().asArray(), is(criterion.mac().toBytes()));
447 }
448
449 @Test
450 public void testArpOpCriterion() throws Exception {
451 int arpOp = random.nextInt();
452 int bitWidth = 32;
453
454 ArpOpCriterion criterion = (ArpOpCriterion) Criteria.matchArpOp(arpOp);
455
456 PiExactFieldMatch exactMatch = (PiExactFieldMatch) translateCriterion(criterion, fieldId, EXACT, bitWidth);
457
458 assertThat(exactMatch.value().asReadOnlyBuffer().getInt(), is(criterion.arpOp()));
459 }
460
461 @Test
462 public void testArpPaCriterion() throws Exception {
463 Ip4Address ip = Ip4Address.valueOf(random.nextInt());
464 int bitWidth = ip.toOctets().length * 8;
465
466 ArpPaCriterion criterion = (ArpPaCriterion) Criteria.matchArpTpa(ip);
467
468 PiExactFieldMatch exactMatch = (PiExactFieldMatch) translateCriterion(criterion, fieldId, EXACT, bitWidth);
469
470 assertThat(exactMatch.value().asReadOnlyBuffer().getInt(), is(criterion.ip().toInt()));
471 }
472
473 @Test
474 public void testIPEcnCriterion() throws Exception {
475 byte[] ipEcn = new byte[1];
476 random.nextBytes(ipEcn);
477
478 int bitWidth = 2;
479
480 IPEcnCriterion criterion = (IPEcnCriterion) Criteria.matchIPEcn(ipEcn[0]);
481
482 PiExactFieldMatch exactMatch = (PiExactFieldMatch) translateCriterion(criterion, fieldId, EXACT, bitWidth);
483
484 assertThat(exactMatch.value().asReadOnlyBuffer().get(), is(criterion.ipEcn()));
485 }
Yi Tsengdbe05602017-11-17 18:02:43 -0800486
487 @Test
488 public void testLpmToTernaryTranslation() throws Exception {
489 IpPrefix ipPrefix = IpPrefix.valueOf("10.0.0.1/23");
490 int bitWidth = ipPrefix.address().toOctets().length * Byte.SIZE;
491
492 IPCriterion criterion = (IPCriterion) Criteria.matchIPDst(ipPrefix);
493 PiTernaryFieldMatch ternaryMatch =
494 (PiTernaryFieldMatch) translateCriterion(criterion, fieldId, TERNARY, bitWidth);
495
496 ImmutableByteSequence expectedMask = ImmutableByteSequence.prefixOnes(Integer.BYTES, 23);
497 ImmutableByteSequence expectedValue = ImmutableByteSequence.copyFrom(ipPrefix.address().toOctets());
498
499 assertThat(ternaryMatch.mask(), is(expectedMask));
500 assertThat(ternaryMatch.value(), is(expectedValue));
501 }
502
503 @Test
504 public void testTernaryToLpmTranslation() throws Exception {
505 EthCriterion criterion =
506 (EthCriterion) Criteria.matchEthDstMasked(MacAddress.ONOS,
507 MacAddress.IPV4_MULTICAST_MASK);
508
509 PiLpmFieldMatch lpmMatch =
510 (PiLpmFieldMatch) translateCriterion(criterion, fieldId, LPM,
511 MacAddress.MAC_ADDRESS_LENGTH * Byte.SIZE);
512 ImmutableByteSequence expectedValue = ImmutableByteSequence.copyFrom(MacAddress.ONOS.toBytes());
513
514 assertThat(lpmMatch.prefixLength(), is(25));
515 assertThat(lpmMatch.value(), is(expectedValue));
516 }
Carmelo Cascone87892e22017-11-13 16:01:29 -0800517}