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