Ray Milkey | 73ba84a | 2015-02-04 17:08:41 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Ray Milkey | 73ba84a | 2015-02-04 17:08:41 -0800 | [diff] [blame] | 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 | package org.onosproject.codec.impl; |
| 17 | |
Frank Wang | 74ce2c1 | 2018-04-11 20:26:45 +0800 | [diff] [blame] | 18 | import java.io.IOException; |
| 19 | import java.io.InputStream; |
| 20 | import java.util.Collection; |
Ray Milkey | 73ba84a | 2015-02-04 17:08:41 -0800 | [diff] [blame] | 21 | import java.util.EnumMap; |
| 22 | |
Frank Wang | 74ce2c1 | 2018-04-11 20:26:45 +0800 | [diff] [blame] | 23 | import com.fasterxml.jackson.databind.JsonNode; |
| 24 | import org.hamcrest.MatcherAssert; |
| 25 | import org.junit.Assert; |
Ray Milkey | 9ee62f5 | 2015-02-06 13:47:35 -0800 | [diff] [blame] | 26 | import org.junit.Before; |
Ray Milkey | 73ba84a | 2015-02-04 17:08:41 -0800 | [diff] [blame] | 27 | import org.junit.Test; |
Ray Milkey | 9ee62f5 | 2015-02-06 13:47:35 -0800 | [diff] [blame] | 28 | import org.onlab.packet.Ip6Address; |
| 29 | import org.onlab.packet.IpPrefix; |
| 30 | import org.onlab.packet.MacAddress; |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 31 | import org.onlab.packet.MplsLabel; |
Hyunsun Moon | cf732fb | 2015-08-22 21:04:23 -0700 | [diff] [blame] | 32 | import org.onlab.packet.TpPort; |
Ray Milkey | 9ee62f5 | 2015-02-06 13:47:35 -0800 | [diff] [blame] | 33 | import org.onlab.packet.VlanId; |
| 34 | import org.onosproject.codec.CodecContext; |
| 35 | import org.onosproject.codec.JsonCodec; |
Sho SHIMIZU | c44c0c3 | 2015-06-01 11:40:48 -0700 | [diff] [blame] | 36 | import org.onosproject.net.ChannelSpacing; |
| 37 | import org.onosproject.net.GridType; |
| 38 | import org.onosproject.net.Lambda; |
Sho SHIMIZU | 6c70f64 | 2015-05-29 17:27:22 -0700 | [diff] [blame] | 39 | import org.onosproject.net.OchSignalType; |
Yafit Hadar | 5796d97 | 2015-10-15 13:16:11 +0300 | [diff] [blame] | 40 | import org.onosproject.net.OduSignalId; |
| 41 | import org.onosproject.net.OduSignalType; |
Ray Milkey | 9ee62f5 | 2015-02-06 13:47:35 -0800 | [diff] [blame] | 42 | import org.onosproject.net.PortNumber; |
| 43 | import org.onosproject.net.flow.criteria.Criteria; |
Ray Milkey | 73ba84a | 2015-02-04 17:08:41 -0800 | [diff] [blame] | 44 | import org.onosproject.net.flow.criteria.Criterion; |
| 45 | |
Ray Milkey | 9ee62f5 | 2015-02-06 13:47:35 -0800 | [diff] [blame] | 46 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Frank Wang | 74ce2c1 | 2018-04-11 20:26:45 +0800 | [diff] [blame] | 47 | import org.onosproject.net.flow.criteria.PiCriterion; |
| 48 | import org.onosproject.net.pi.model.PiMatchFieldId; |
| 49 | import org.onosproject.net.pi.runtime.PiExactFieldMatch; |
| 50 | import org.onosproject.net.pi.runtime.PiFieldMatch; |
| 51 | import org.onosproject.net.pi.runtime.PiLpmFieldMatch; |
Daniele Moro | c6f2f7f | 2020-12-18 10:55:57 +0100 | [diff] [blame] | 52 | import org.onosproject.net.pi.runtime.PiOptionalFieldMatch; |
Frank Wang | 74ce2c1 | 2018-04-11 20:26:45 +0800 | [diff] [blame] | 53 | import org.onosproject.net.pi.runtime.PiRangeFieldMatch; |
| 54 | import org.onosproject.net.pi.runtime.PiTernaryFieldMatch; |
Ray Milkey | 9ee62f5 | 2015-02-06 13:47:35 -0800 | [diff] [blame] | 55 | |
Ray Milkey | 73ba84a | 2015-02-04 17:08:41 -0800 | [diff] [blame] | 56 | import static org.hamcrest.MatcherAssert.assertThat; |
Frank Wang | 74ce2c1 | 2018-04-11 20:26:45 +0800 | [diff] [blame] | 57 | import static org.hamcrest.Matchers.is; |
Ray Milkey | 0cc189e | 2015-02-09 11:08:01 -0800 | [diff] [blame] | 58 | import static org.hamcrest.Matchers.notNullValue; |
| 59 | import static org.onlab.junit.TestUtils.getField; |
Frank Wang | 74ce2c1 | 2018-04-11 20:26:45 +0800 | [diff] [blame] | 60 | import static org.onlab.util.ImmutableByteSequence.copyFrom; |
Ray Milkey | 46670a8 | 2015-02-08 17:57:17 -0800 | [diff] [blame] | 61 | import static org.onosproject.codec.impl.CriterionJsonMatcher.matchesCriterion; |
Ray Milkey | 73ba84a | 2015-02-04 17:08:41 -0800 | [diff] [blame] | 62 | |
| 63 | /** |
| 64 | * Unit tests for criterion codec. |
| 65 | */ |
| 66 | public class CriterionCodecTest { |
| 67 | |
Ray Milkey | 9ee62f5 | 2015-02-06 13:47:35 -0800 | [diff] [blame] | 68 | CodecContext context; |
| 69 | JsonCodec<Criterion> criterionCodec; |
| 70 | final PortNumber port = PortNumber.portNumber(1); |
| 71 | final IpPrefix ipPrefix4 = IpPrefix.valueOf("10.1.1.0/24"); |
| 72 | final IpPrefix ipPrefix6 = IpPrefix.valueOf("fe80::/64"); |
| 73 | final MacAddress mac1 = MacAddress.valueOf("00:00:11:00:00:01"); |
Seyeon Jeong | 8f01414 | 2020-02-26 12:51:03 -0800 | [diff] [blame] | 74 | final MacAddress mcastMac = MacAddress.valueOf("01:00:5E:00:00:01"); |
| 75 | final MacAddress mcastMacMask = MacAddress.valueOf("FF:FF:FF:80:00:00"); |
Hyunsun Moon | cf732fb | 2015-08-22 21:04:23 -0700 | [diff] [blame] | 76 | final TpPort tpPort = TpPort.tpPort(40000); |
Yafit Hadar | 5796d97 | 2015-10-15 13:16:11 +0300 | [diff] [blame] | 77 | final int tributaryPortNumber = 11; |
| 78 | final int tributarySlotLen = 80; |
| 79 | final byte[] tributarySlotBitmap = new byte[] {1, 2, 3, 4, 2, 3, 4, 2, 3, 4}; |
| 80 | |
Ray Milkey | 9ee62f5 | 2015-02-06 13:47:35 -0800 | [diff] [blame] | 81 | |
| 82 | /** |
| 83 | * Sets up for each test. Creates a context and fetches the criterion |
| 84 | * codec. |
| 85 | */ |
| 86 | @Before |
| 87 | public void setUp() { |
| 88 | context = new MockCodecContext(); |
| 89 | criterionCodec = context.codec(Criterion.class); |
| 90 | assertThat(criterionCodec, notNullValue()); |
| 91 | } |
| 92 | |
| 93 | |
Ray Milkey | 73ba84a | 2015-02-04 17:08:41 -0800 | [diff] [blame] | 94 | /** |
| 95 | * Checks that all criterion types are covered by the codec. |
| 96 | */ |
| 97 | @Test |
| 98 | public void checkCriterionTypes() throws Exception { |
Ray Milkey | 6d7968e | 2015-07-06 14:30:02 -0700 | [diff] [blame] | 99 | EncodeCriterionCodecHelper encoder = new EncodeCriterionCodecHelper( |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 100 | Criteria.dummy(), context); |
Ray Milkey | 9ee62f5 | 2015-02-06 13:47:35 -0800 | [diff] [blame] | 101 | EnumMap<Criterion.Type, Object> formatMap = |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 102 | getField(encoder, "formatMap"); |
Ray Milkey | 73ba84a | 2015-02-04 17:08:41 -0800 | [diff] [blame] | 103 | assertThat(formatMap, notNullValue()); |
| 104 | |
| 105 | for (Criterion.Type type : Criterion.Type.values()) { |
| 106 | assertThat("Entry not found for " + type.toString(), |
| 107 | formatMap.get(type), notNullValue()); |
| 108 | } |
| 109 | } |
Ray Milkey | 9ee62f5 | 2015-02-06 13:47:35 -0800 | [diff] [blame] | 110 | |
| 111 | /** |
| 112 | * Tests in port criterion. |
| 113 | */ |
| 114 | @Test |
| 115 | public void matchInPortTest() { |
| 116 | Criterion criterion = Criteria.matchInPort(port); |
| 117 | ObjectNode result = criterionCodec.encode(criterion, context); |
Ray Milkey | 46670a8 | 2015-02-08 17:57:17 -0800 | [diff] [blame] | 118 | assertThat(result, matchesCriterion(criterion)); |
Ray Milkey | 9ee62f5 | 2015-02-06 13:47:35 -0800 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Tests in physical port criterion. |
| 123 | */ |
| 124 | @Test |
| 125 | public void matchInPhyPortTest() { |
| 126 | Criterion criterion = Criteria.matchInPhyPort(port); |
| 127 | ObjectNode result = criterionCodec.encode(criterion, context); |
Ray Milkey | 46670a8 | 2015-02-08 17:57:17 -0800 | [diff] [blame] | 128 | assertThat(result, matchesCriterion(criterion)); |
Ray Milkey | 9ee62f5 | 2015-02-06 13:47:35 -0800 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Tests metadata criterion. |
| 133 | */ |
| 134 | @Test |
| 135 | public void matchMetadataTest() { |
| 136 | Criterion criterion = Criteria.matchMetadata(0xabcdL); |
| 137 | ObjectNode result = criterionCodec.encode(criterion, context); |
Ray Milkey | 46670a8 | 2015-02-08 17:57:17 -0800 | [diff] [blame] | 138 | assertThat(result, matchesCriterion(criterion)); |
Ray Milkey | 9ee62f5 | 2015-02-06 13:47:35 -0800 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Tests ethernet destination criterion. |
| 143 | */ |
| 144 | @Test |
| 145 | public void matchEthDstTest() { |
| 146 | Criterion criterion = Criteria.matchEthDst(mac1); |
| 147 | ObjectNode result = criterionCodec.encode(criterion, context); |
Ray Milkey | 46670a8 | 2015-02-08 17:57:17 -0800 | [diff] [blame] | 148 | assertThat(result, matchesCriterion(criterion)); |
Ray Milkey | 9ee62f5 | 2015-02-06 13:47:35 -0800 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | /** |
Seyeon Jeong | 8f01414 | 2020-02-26 12:51:03 -0800 | [diff] [blame] | 152 | * Tests masked ethernet destination criterion (Criterion.Type.ETH_DST_MASKED). |
| 153 | */ |
| 154 | @Test |
| 155 | public void matchEthDstMaskTest() { |
| 156 | Criterion criterion = Criteria.matchEthDstMasked(mcastMac, mcastMacMask); |
| 157 | ObjectNode result = criterionCodec.encode(criterion, context); |
| 158 | assertThat(result, matchesCriterion(criterion)); |
| 159 | } |
| 160 | |
| 161 | /** |
Ray Milkey | 9ee62f5 | 2015-02-06 13:47:35 -0800 | [diff] [blame] | 162 | * Tests ethernet source criterion. |
| 163 | */ |
| 164 | @Test |
| 165 | public void matchEthSrcTest() { |
| 166 | Criterion criterion = Criteria.matchEthSrc(mac1); |
| 167 | ObjectNode result = criterionCodec.encode(criterion, context); |
Ray Milkey | 46670a8 | 2015-02-08 17:57:17 -0800 | [diff] [blame] | 168 | assertThat(result, matchesCriterion(criterion)); |
Ray Milkey | 9ee62f5 | 2015-02-06 13:47:35 -0800 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Tests ethernet type criterion. |
| 173 | */ |
| 174 | @Test |
| 175 | public void matchEthTypeTest() { |
Pavlin Radoslavov | f3b6933 | 2015-02-06 15:47:05 -0800 | [diff] [blame] | 176 | Criterion criterion = Criteria.matchEthType((short) 0x8844); |
Ray Milkey | 9ee62f5 | 2015-02-06 13:47:35 -0800 | [diff] [blame] | 177 | ObjectNode result = criterionCodec.encode(criterion, context); |
Ray Milkey | 46670a8 | 2015-02-08 17:57:17 -0800 | [diff] [blame] | 178 | assertThat(result, matchesCriterion(criterion)); |
Ray Milkey | 9ee62f5 | 2015-02-06 13:47:35 -0800 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Tests VLAN Id criterion. |
| 183 | */ |
| 184 | @Test |
| 185 | public void matchVlanIdTest() { |
| 186 | Criterion criterion = Criteria.matchVlanId(VlanId.ANY); |
| 187 | ObjectNode result = criterionCodec.encode(criterion, context); |
Ray Milkey | 46670a8 | 2015-02-08 17:57:17 -0800 | [diff] [blame] | 188 | assertThat(result, matchesCriterion(criterion)); |
Ray Milkey | 9ee62f5 | 2015-02-06 13:47:35 -0800 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | /** |
| 192 | * Tests VLAN PCP criterion. |
| 193 | */ |
| 194 | @Test |
| 195 | public void matchVlanPcpTest() { |
Pavlin Radoslavov | f3b6933 | 2015-02-06 15:47:05 -0800 | [diff] [blame] | 196 | Criterion criterion = Criteria.matchVlanPcp((byte) 7); |
Ray Milkey | 9ee62f5 | 2015-02-06 13:47:35 -0800 | [diff] [blame] | 197 | ObjectNode result = criterionCodec.encode(criterion, context); |
Ray Milkey | 46670a8 | 2015-02-08 17:57:17 -0800 | [diff] [blame] | 198 | assertThat(result, matchesCriterion(criterion)); |
Ray Milkey | 9ee62f5 | 2015-02-06 13:47:35 -0800 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Tests IP DSCP criterion. |
| 203 | */ |
| 204 | @Test |
| 205 | public void matchIPDscpTest() { |
Pavlin Radoslavov | f3b6933 | 2015-02-06 15:47:05 -0800 | [diff] [blame] | 206 | Criterion criterion = Criteria.matchIPDscp((byte) 63); |
Ray Milkey | 9ee62f5 | 2015-02-06 13:47:35 -0800 | [diff] [blame] | 207 | ObjectNode result = criterionCodec.encode(criterion, context); |
Ray Milkey | 46670a8 | 2015-02-08 17:57:17 -0800 | [diff] [blame] | 208 | assertThat(result, matchesCriterion(criterion)); |
Ray Milkey | 9ee62f5 | 2015-02-06 13:47:35 -0800 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | /** |
| 212 | * Tests IP ECN criterion. |
| 213 | */ |
| 214 | @Test |
| 215 | public void matchIPEcnTest() { |
Pavlin Radoslavov | ab8553a | 2015-02-20 14:13:50 -0800 | [diff] [blame] | 216 | Criterion criterion = Criteria.matchIPEcn((byte) 3); |
Ray Milkey | 9ee62f5 | 2015-02-06 13:47:35 -0800 | [diff] [blame] | 217 | ObjectNode result = criterionCodec.encode(criterion, context); |
Ray Milkey | 46670a8 | 2015-02-08 17:57:17 -0800 | [diff] [blame] | 218 | assertThat(result, matchesCriterion(criterion)); |
Ray Milkey | 9ee62f5 | 2015-02-06 13:47:35 -0800 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | /** |
| 222 | * Tests IP protocol criterion. |
| 223 | */ |
| 224 | @Test |
| 225 | public void matchIPProtocolTest() { |
Pavlin Radoslavov | f3b6933 | 2015-02-06 15:47:05 -0800 | [diff] [blame] | 226 | Criterion criterion = Criteria.matchIPProtocol((byte) 250); |
Ray Milkey | 9ee62f5 | 2015-02-06 13:47:35 -0800 | [diff] [blame] | 227 | ObjectNode result = criterionCodec.encode(criterion, context); |
Ray Milkey | 46670a8 | 2015-02-08 17:57:17 -0800 | [diff] [blame] | 228 | assertThat(result, matchesCriterion(criterion)); |
Ray Milkey | 9ee62f5 | 2015-02-06 13:47:35 -0800 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | /** |
| 232 | * Tests IP source criterion. |
| 233 | */ |
| 234 | @Test |
| 235 | public void matchIPSrcTest() { |
| 236 | Criterion criterion = Criteria.matchIPSrc(ipPrefix4); |
| 237 | ObjectNode result = criterionCodec.encode(criterion, context); |
Ray Milkey | 46670a8 | 2015-02-08 17:57:17 -0800 | [diff] [blame] | 238 | assertThat(result, matchesCriterion(criterion)); |
Ray Milkey | 9ee62f5 | 2015-02-06 13:47:35 -0800 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | /** |
| 242 | * Tests IP destination criterion. |
| 243 | */ |
| 244 | @Test |
| 245 | public void matchIPDstTest() { |
| 246 | Criterion criterion = Criteria.matchIPDst(ipPrefix4); |
| 247 | ObjectNode result = criterionCodec.encode(criterion, context); |
Ray Milkey | 46670a8 | 2015-02-08 17:57:17 -0800 | [diff] [blame] | 248 | assertThat(result, matchesCriterion(criterion)); |
Ray Milkey | 9ee62f5 | 2015-02-06 13:47:35 -0800 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | /** |
| 252 | * Tests source TCP port criterion. |
| 253 | */ |
| 254 | @Test |
| 255 | public void matchTcpSrcTest() { |
Hyunsun Moon | cf732fb | 2015-08-22 21:04:23 -0700 | [diff] [blame] | 256 | Criterion criterion = Criteria.matchTcpSrc(tpPort); |
Ray Milkey | 9ee62f5 | 2015-02-06 13:47:35 -0800 | [diff] [blame] | 257 | ObjectNode result = criterionCodec.encode(criterion, context); |
Ray Milkey | 46670a8 | 2015-02-08 17:57:17 -0800 | [diff] [blame] | 258 | assertThat(result, matchesCriterion(criterion)); |
Ray Milkey | 9ee62f5 | 2015-02-06 13:47:35 -0800 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | /** |
| 262 | * Tests destination TCP port criterion. |
| 263 | */ |
| 264 | @Test |
| 265 | public void matchTcpDstTest() { |
Hyunsun Moon | cf732fb | 2015-08-22 21:04:23 -0700 | [diff] [blame] | 266 | Criterion criterion = Criteria.matchTcpDst(tpPort); |
Ray Milkey | 9ee62f5 | 2015-02-06 13:47:35 -0800 | [diff] [blame] | 267 | ObjectNode result = criterionCodec.encode(criterion, context); |
Ray Milkey | 46670a8 | 2015-02-08 17:57:17 -0800 | [diff] [blame] | 268 | assertThat(result, matchesCriterion(criterion)); |
Ray Milkey | 9ee62f5 | 2015-02-06 13:47:35 -0800 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | /** |
| 272 | * Tests source UDP port criterion. |
| 273 | */ |
| 274 | @Test |
| 275 | public void matchUdpSrcTest() { |
Hyunsun Moon | cf732fb | 2015-08-22 21:04:23 -0700 | [diff] [blame] | 276 | Criterion criterion = Criteria.matchUdpSrc(tpPort); |
Ray Milkey | 9ee62f5 | 2015-02-06 13:47:35 -0800 | [diff] [blame] | 277 | ObjectNode result = criterionCodec.encode(criterion, context); |
Ray Milkey | 46670a8 | 2015-02-08 17:57:17 -0800 | [diff] [blame] | 278 | assertThat(result, matchesCriterion(criterion)); |
Ray Milkey | 9ee62f5 | 2015-02-06 13:47:35 -0800 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | /** |
| 282 | * Tests destination UDP criterion. |
| 283 | */ |
| 284 | @Test |
| 285 | public void matchUdpDstTest() { |
Hyunsun Moon | cf732fb | 2015-08-22 21:04:23 -0700 | [diff] [blame] | 286 | Criterion criterion = Criteria.matchUdpDst(tpPort); |
Ray Milkey | 9ee62f5 | 2015-02-06 13:47:35 -0800 | [diff] [blame] | 287 | ObjectNode result = criterionCodec.encode(criterion, context); |
Ray Milkey | 46670a8 | 2015-02-08 17:57:17 -0800 | [diff] [blame] | 288 | assertThat(result, matchesCriterion(criterion)); |
Ray Milkey | 9ee62f5 | 2015-02-06 13:47:35 -0800 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | /** |
| 292 | * Tests source SCTP criterion. |
| 293 | */ |
| 294 | @Test |
| 295 | public void matchSctpSrcTest() { |
Hyunsun Moon | cf732fb | 2015-08-22 21:04:23 -0700 | [diff] [blame] | 296 | Criterion criterion = Criteria.matchSctpSrc(tpPort); |
Ray Milkey | 9ee62f5 | 2015-02-06 13:47:35 -0800 | [diff] [blame] | 297 | ObjectNode result = criterionCodec.encode(criterion, context); |
Ray Milkey | 46670a8 | 2015-02-08 17:57:17 -0800 | [diff] [blame] | 298 | assertThat(result, matchesCriterion(criterion)); |
Ray Milkey | 9ee62f5 | 2015-02-06 13:47:35 -0800 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | /** |
| 302 | * Tests destination SCTP criterion. |
| 303 | */ |
| 304 | @Test |
| 305 | public void matchSctpDstTest() { |
Hyunsun Moon | cf732fb | 2015-08-22 21:04:23 -0700 | [diff] [blame] | 306 | Criterion criterion = Criteria.matchSctpDst(tpPort); |
Ray Milkey | 9ee62f5 | 2015-02-06 13:47:35 -0800 | [diff] [blame] | 307 | ObjectNode result = criterionCodec.encode(criterion, context); |
Ray Milkey | 46670a8 | 2015-02-08 17:57:17 -0800 | [diff] [blame] | 308 | assertThat(result, matchesCriterion(criterion)); |
Ray Milkey | 9ee62f5 | 2015-02-06 13:47:35 -0800 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | /** |
| 312 | * Tests ICMP type criterion. |
| 313 | */ |
| 314 | @Test |
| 315 | public void matchIcmpTypeTest() { |
Pavlin Radoslavov | f3b6933 | 2015-02-06 15:47:05 -0800 | [diff] [blame] | 316 | Criterion criterion = Criteria.matchIcmpType((byte) 250); |
Ray Milkey | 9ee62f5 | 2015-02-06 13:47:35 -0800 | [diff] [blame] | 317 | ObjectNode result = criterionCodec.encode(criterion, context); |
Ray Milkey | 46670a8 | 2015-02-08 17:57:17 -0800 | [diff] [blame] | 318 | assertThat(result, matchesCriterion(criterion)); |
Ray Milkey | 9ee62f5 | 2015-02-06 13:47:35 -0800 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | /** |
| 322 | * Tests ICMP code criterion. |
| 323 | */ |
| 324 | @Test |
| 325 | public void matchIcmpCodeTest() { |
Pavlin Radoslavov | f3b6933 | 2015-02-06 15:47:05 -0800 | [diff] [blame] | 326 | Criterion criterion = Criteria.matchIcmpCode((byte) 250); |
Ray Milkey | 9ee62f5 | 2015-02-06 13:47:35 -0800 | [diff] [blame] | 327 | ObjectNode result = criterionCodec.encode(criterion, context); |
Ray Milkey | 46670a8 | 2015-02-08 17:57:17 -0800 | [diff] [blame] | 328 | assertThat(result, matchesCriterion(criterion)); |
Ray Milkey | 9ee62f5 | 2015-02-06 13:47:35 -0800 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | /** |
| 332 | * Tests IPv6 source criterion. |
| 333 | */ |
| 334 | @Test |
| 335 | public void matchIPv6SrcTest() { |
| 336 | Criterion criterion = Criteria.matchIPv6Src(ipPrefix6); |
| 337 | ObjectNode result = criterionCodec.encode(criterion, context); |
Ray Milkey | 46670a8 | 2015-02-08 17:57:17 -0800 | [diff] [blame] | 338 | assertThat(result, matchesCriterion(criterion)); |
Ray Milkey | 9ee62f5 | 2015-02-06 13:47:35 -0800 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | /** |
| 342 | * Tests IPv6 destination criterion. |
| 343 | */ |
| 344 | @Test |
| 345 | public void matchIPv6DstTest() { |
| 346 | Criterion criterion = Criteria.matchIPv6Dst(ipPrefix6); |
| 347 | ObjectNode result = criterionCodec.encode(criterion, context); |
Ray Milkey | 46670a8 | 2015-02-08 17:57:17 -0800 | [diff] [blame] | 348 | assertThat(result, matchesCriterion(criterion)); |
Ray Milkey | 9ee62f5 | 2015-02-06 13:47:35 -0800 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | /** |
| 352 | * Tests IPv6 flow label criterion. |
| 353 | */ |
| 354 | @Test |
| 355 | public void matchIPv6FlowLabelTest() { |
Pavlin Radoslavov | f3b6933 | 2015-02-06 15:47:05 -0800 | [diff] [blame] | 356 | Criterion criterion = Criteria.matchIPv6FlowLabel(0xffffe); |
Ray Milkey | 9ee62f5 | 2015-02-06 13:47:35 -0800 | [diff] [blame] | 357 | ObjectNode result = criterionCodec.encode(criterion, context); |
Ray Milkey | 46670a8 | 2015-02-08 17:57:17 -0800 | [diff] [blame] | 358 | assertThat(result, matchesCriterion(criterion)); |
Ray Milkey | 9ee62f5 | 2015-02-06 13:47:35 -0800 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | /** |
| 362 | * Tests ICMP v6 type criterion. |
| 363 | */ |
| 364 | @Test |
| 365 | public void matchIcmpv6TypeTest() { |
Pavlin Radoslavov | f3b6933 | 2015-02-06 15:47:05 -0800 | [diff] [blame] | 366 | Criterion criterion = Criteria.matchIcmpv6Type((byte) 250); |
Ray Milkey | 9ee62f5 | 2015-02-06 13:47:35 -0800 | [diff] [blame] | 367 | ObjectNode result = criterionCodec.encode(criterion, context); |
Ray Milkey | 46670a8 | 2015-02-08 17:57:17 -0800 | [diff] [blame] | 368 | assertThat(result, matchesCriterion(criterion)); |
Ray Milkey | 9ee62f5 | 2015-02-06 13:47:35 -0800 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | /** |
| 372 | * Tests ICMP v6 code criterion. |
| 373 | */ |
| 374 | @Test |
| 375 | public void matchIcmpv6CodeTest() { |
Pavlin Radoslavov | f3b6933 | 2015-02-06 15:47:05 -0800 | [diff] [blame] | 376 | Criterion criterion = Criteria.matchIcmpv6Code((byte) 250); |
Ray Milkey | 9ee62f5 | 2015-02-06 13:47:35 -0800 | [diff] [blame] | 377 | ObjectNode result = criterionCodec.encode(criterion, context); |
Ray Milkey | 46670a8 | 2015-02-08 17:57:17 -0800 | [diff] [blame] | 378 | assertThat(result, matchesCriterion(criterion)); |
Ray Milkey | 9ee62f5 | 2015-02-06 13:47:35 -0800 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | /** |
| 382 | * Tests IPV6 target address criterion. |
| 383 | */ |
| 384 | @Test |
| 385 | public void matchIPv6NDTargetAddressTest() { |
| 386 | Criterion criterion = |
| 387 | Criteria.matchIPv6NDTargetAddress( |
| 388 | Ip6Address.valueOf("1111:2222::")); |
| 389 | ObjectNode result = criterionCodec.encode(criterion, context); |
Ray Milkey | 46670a8 | 2015-02-08 17:57:17 -0800 | [diff] [blame] | 390 | assertThat(result, matchesCriterion(criterion)); |
Ray Milkey | 9ee62f5 | 2015-02-06 13:47:35 -0800 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | /** |
| 394 | * Tests IPV6 SLL criterion. |
| 395 | */ |
| 396 | @Test |
| 397 | public void matchIPv6NDSourceLinkLayerAddressTest() { |
| 398 | Criterion criterion = Criteria.matchIPv6NDSourceLinkLayerAddress(mac1); |
| 399 | ObjectNode result = criterionCodec.encode(criterion, context); |
Ray Milkey | 46670a8 | 2015-02-08 17:57:17 -0800 | [diff] [blame] | 400 | assertThat(result, matchesCriterion(criterion)); |
Ray Milkey | 9ee62f5 | 2015-02-06 13:47:35 -0800 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | /** |
| 404 | * Tests IPV6 TLL criterion. |
| 405 | */ |
| 406 | @Test |
| 407 | public void matchIPv6NDTargetLinkLayerAddressTest() { |
| 408 | Criterion criterion = Criteria.matchIPv6NDTargetLinkLayerAddress(mac1); |
| 409 | ObjectNode result = criterionCodec.encode(criterion, context); |
Ray Milkey | 46670a8 | 2015-02-08 17:57:17 -0800 | [diff] [blame] | 410 | assertThat(result, matchesCriterion(criterion)); |
Ray Milkey | 9ee62f5 | 2015-02-06 13:47:35 -0800 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | /** |
| 414 | * Tests MPLS label criterion. |
| 415 | */ |
| 416 | @Test |
| 417 | public void matchMplsLabelTest() { |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 418 | Criterion criterion = Criteria.matchMplsLabel(MplsLabel.mplsLabel(0xffffe)); |
Ray Milkey | 9ee62f5 | 2015-02-06 13:47:35 -0800 | [diff] [blame] | 419 | ObjectNode result = criterionCodec.encode(criterion, context); |
Ray Milkey | 46670a8 | 2015-02-08 17:57:17 -0800 | [diff] [blame] | 420 | assertThat(result, matchesCriterion(criterion)); |
Ray Milkey | 9ee62f5 | 2015-02-06 13:47:35 -0800 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | /** |
Pavlin Radoslavov | 5e4f754 | 2015-02-06 18:18:21 -0800 | [diff] [blame] | 424 | * Tests IPv6 Extension Header pseudo-field flags criterion. |
| 425 | */ |
| 426 | @Test |
| 427 | public void matchIPv6ExthdrFlagsTest() { |
| 428 | int exthdrFlags = |
| 429 | Criterion.IPv6ExthdrFlags.NONEXT.getValue() | |
| 430 | Criterion.IPv6ExthdrFlags.ESP.getValue() | |
| 431 | Criterion.IPv6ExthdrFlags.AUTH.getValue() | |
| 432 | Criterion.IPv6ExthdrFlags.DEST.getValue() | |
| 433 | Criterion.IPv6ExthdrFlags.FRAG.getValue() | |
| 434 | Criterion.IPv6ExthdrFlags.ROUTER.getValue() | |
| 435 | Criterion.IPv6ExthdrFlags.HOP.getValue() | |
| 436 | Criterion.IPv6ExthdrFlags.UNREP.getValue() | |
| 437 | Criterion.IPv6ExthdrFlags.UNSEQ.getValue(); |
| 438 | Criterion criterion = Criteria.matchIPv6ExthdrFlags(exthdrFlags); |
| 439 | ObjectNode result = criterionCodec.encode(criterion, context); |
Ray Milkey | 46670a8 | 2015-02-08 17:57:17 -0800 | [diff] [blame] | 440 | |
| 441 | assertThat(result, matchesCriterion(criterion)); |
Pavlin Radoslavov | 5e4f754 | 2015-02-06 18:18:21 -0800 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | /** |
Ray Milkey | 9ee62f5 | 2015-02-06 13:47:35 -0800 | [diff] [blame] | 445 | * Tests lambda criterion. |
| 446 | */ |
| 447 | @Test |
Sho SHIMIZU | c44c0c3 | 2015-06-01 11:40:48 -0700 | [diff] [blame] | 448 | public void matchOchSignal() { |
| 449 | Lambda ochSignal = Lambda.ochSignal(GridType.DWDM, ChannelSpacing.CHL_100GHZ, 4, 8); |
| 450 | Criterion criterion = Criteria.matchLambda(ochSignal); |
Ray Milkey | 9ee62f5 | 2015-02-06 13:47:35 -0800 | [diff] [blame] | 451 | ObjectNode result = criterionCodec.encode(criterion, context); |
Ray Milkey | 46670a8 | 2015-02-08 17:57:17 -0800 | [diff] [blame] | 452 | assertThat(result, matchesCriterion(criterion)); |
Ray Milkey | 9ee62f5 | 2015-02-06 13:47:35 -0800 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | /** |
Sho SHIMIZU | 68b8c1f | 2015-05-29 18:42:26 -0700 | [diff] [blame] | 456 | * Tests Och signal type criterion. |
Ray Milkey | 9ee62f5 | 2015-02-06 13:47:35 -0800 | [diff] [blame] | 457 | */ |
| 458 | @Test |
Sho SHIMIZU | 68b8c1f | 2015-05-29 18:42:26 -0700 | [diff] [blame] | 459 | public void matchOchSignalTypeTest() { |
Sho SHIMIZU | 6c70f64 | 2015-05-29 17:27:22 -0700 | [diff] [blame] | 460 | Criterion criterion = Criteria.matchOchSignalType(OchSignalType.FIXED_GRID); |
Ray Milkey | 9ee62f5 | 2015-02-06 13:47:35 -0800 | [diff] [blame] | 461 | ObjectNode result = criterionCodec.encode(criterion, context); |
Ray Milkey | 46670a8 | 2015-02-08 17:57:17 -0800 | [diff] [blame] | 462 | assertThat(result, matchesCriterion(criterion)); |
Ray Milkey | 9ee62f5 | 2015-02-06 13:47:35 -0800 | [diff] [blame] | 463 | } |
Yafit Hadar | 5796d97 | 2015-10-15 13:16:11 +0300 | [diff] [blame] | 464 | |
| 465 | /** |
| 466 | * Tests Odu Signal ID criterion. |
| 467 | */ |
| 468 | @Test |
| 469 | public void matchOduSignalIdTest() { |
| 470 | |
| 471 | OduSignalId oduSignalId = OduSignalId.oduSignalId(tributaryPortNumber, tributarySlotLen, tributarySlotBitmap); |
| 472 | |
| 473 | Criterion criterion = Criteria.matchOduSignalId(oduSignalId); |
| 474 | ObjectNode result = criterionCodec.encode(criterion, context); |
| 475 | assertThat(result, matchesCriterion(criterion)); |
| 476 | } |
| 477 | |
| 478 | /** |
| 479 | * Tests Odu Signal Type criterion. |
| 480 | */ |
| 481 | @Test |
| 482 | public void matchOduSignalTypeTest() { |
| 483 | |
| 484 | OduSignalType signalType = OduSignalType.ODU2; |
| 485 | |
| 486 | Criterion criterion = Criteria.matchOduSignalType(signalType); |
| 487 | ObjectNode result = criterionCodec.encode(criterion, context); |
| 488 | assertThat(result, matchesCriterion(criterion)); |
| 489 | } |
| 490 | |
Frank Wang | 74ce2c1 | 2018-04-11 20:26:45 +0800 | [diff] [blame] | 491 | /** |
| 492 | * Tests protocol-independent type criterion encoding. |
| 493 | */ |
| 494 | @Test |
| 495 | public void matchPiTypeEncodingTest() { |
| 496 | |
| 497 | PiMatchFieldId ethMatchFieldId = PiMatchFieldId.of("ethernet_t.etherType"); |
| 498 | byte[] matchExactBytes1 = {0x08, 0x00}; |
| 499 | Criterion exactBytesCriterion = PiCriterion.builder().matchExact(ethMatchFieldId, matchExactBytes1).build(); |
| 500 | ObjectNode exactResult = criterionCodec.encode(exactBytesCriterion, context); |
| 501 | assertThat(exactResult, matchesCriterion(exactBytesCriterion)); |
| 502 | |
| 503 | PiMatchFieldId ipv4MatchFieldId = PiMatchFieldId.of("ipv4_t.dstAddr"); |
| 504 | int mask = 0x00ffffff; |
| 505 | byte[] matchLpmBytes1 = {0x0a, 0x01, 0x01, 0x01}; |
| 506 | Criterion lpmBytesCriterion = PiCriterion.builder().matchLpm(ipv4MatchFieldId, matchLpmBytes1, mask).build(); |
| 507 | ObjectNode lpmResult = criterionCodec.encode(lpmBytesCriterion, context); |
| 508 | assertThat(lpmResult, matchesCriterion(lpmBytesCriterion)); |
| 509 | |
| 510 | byte[] matchTernaryBytes1 = {0x0a, 0x01, 0x01, 0x01}; |
| 511 | byte[] matchTernaryMaskBytes = {0x7f, 0x7f, 0x7f, 0x00}; |
| 512 | Criterion ternaryBytesCriterion = PiCriterion.builder().matchTernary(ipv4MatchFieldId, matchTernaryBytes1, |
| 513 | matchTernaryMaskBytes).build(); |
| 514 | ObjectNode ternaryResult = criterionCodec.encode(ternaryBytesCriterion, context); |
| 515 | assertThat(ternaryResult, matchesCriterion(ternaryBytesCriterion)); |
| 516 | |
| 517 | byte[] matchRangeBytes1 = {0x10}; |
| 518 | byte[] matchRangeHighBytes = {0x30}; |
| 519 | Criterion rangeBytesCriterion = PiCriterion.builder() |
| 520 | .matchRange(ipv4MatchFieldId, matchRangeBytes1, matchRangeHighBytes).build(); |
| 521 | ObjectNode rangeResult = criterionCodec.encode(rangeBytesCriterion, context); |
| 522 | assertThat(rangeResult, matchesCriterion(rangeBytesCriterion)); |
Daniele Moro | c6f2f7f | 2020-12-18 10:55:57 +0100 | [diff] [blame] | 523 | |
| 524 | byte[] matchOptionalBytes = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55}; |
| 525 | Criterion optionalBytesCriterion = PiCriterion.builder().matchOptional(ethMatchFieldId, |
| 526 | matchOptionalBytes).build(); |
| 527 | ObjectNode optionalResult = criterionCodec.encode(optionalBytesCriterion, context); |
| 528 | assertThat(optionalResult, matchesCriterion(optionalBytesCriterion)); |
Frank Wang | 74ce2c1 | 2018-04-11 20:26:45 +0800 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | /** |
| 532 | * Tests protocol-independent type criterion decoding. |
| 533 | */ |
| 534 | @Test |
| 535 | public void matchPiTypeDecodingTest() throws IOException { |
| 536 | Criterion criterion = getCriterion("PiCriterion.json"); |
| 537 | Assert.assertThat(criterion.type(), is(Criterion.Type.PROTOCOL_INDEPENDENT)); |
| 538 | Collection<PiFieldMatch> piFieldMatches = ((PiCriterion) criterion).fieldMatches(); |
| 539 | for (PiFieldMatch piFieldMatch : piFieldMatches) { |
| 540 | switch (piFieldMatch.type()) { |
| 541 | case EXACT: |
| 542 | Assert.assertThat(piFieldMatch.fieldId().id(), is("ingress_port")); |
| 543 | Assert.assertThat(((PiExactFieldMatch) piFieldMatch).value(), is( |
| 544 | copyFrom((byte) 0x10))); |
| 545 | break; |
| 546 | case LPM: |
| 547 | Assert.assertThat(piFieldMatch.fieldId().id(), is("src_addr")); |
| 548 | Assert.assertThat(((PiLpmFieldMatch) piFieldMatch).value(), |
| 549 | is(copyFrom(0xa010101))); |
| 550 | Assert.assertThat(((PiLpmFieldMatch) piFieldMatch).prefixLength(), is(24)); |
| 551 | break; |
| 552 | case TERNARY: |
| 553 | Assert.assertThat(piFieldMatch.fieldId().id(), is("dst_addr")); |
| 554 | Assert.assertThat(((PiTernaryFieldMatch) piFieldMatch).value(), |
| 555 | is(copyFrom(0xa010101))); |
| 556 | Assert.assertThat(((PiTernaryFieldMatch) piFieldMatch).mask(), |
| 557 | is(copyFrom(0xfffffff))); |
| 558 | break; |
| 559 | case RANGE: |
| 560 | Assert.assertThat(piFieldMatch.fieldId().id(), is("egress_port")); |
| 561 | Assert.assertThat(((PiRangeFieldMatch) piFieldMatch).highValue(), |
| 562 | is(copyFrom((byte) 0x20))); |
| 563 | Assert.assertThat(((PiRangeFieldMatch) piFieldMatch).lowValue(), |
| 564 | is(copyFrom((byte) 0x10))); |
| 565 | break; |
Daniele Moro | c6f2f7f | 2020-12-18 10:55:57 +0100 | [diff] [blame] | 566 | case OPTIONAL: |
| 567 | Assert.assertThat(piFieldMatch.fieldId().id(), is("eth_dst")); |
| 568 | Assert.assertThat(((PiOptionalFieldMatch) piFieldMatch).value(), |
| 569 | is(copyFrom(new byte[]{0x00, 0x11, 0x22, 0x33, 0x44, 0x55}))); |
| 570 | break; |
Frank Wang | 74ce2c1 | 2018-04-11 20:26:45 +0800 | [diff] [blame] | 571 | default: |
Carmelo Cascone | 6af4e17 | 2018-06-15 16:01:30 +0200 | [diff] [blame] | 572 | Assert.fail(); |
Frank Wang | 74ce2c1 | 2018-04-11 20:26:45 +0800 | [diff] [blame] | 573 | } |
| 574 | } |
| 575 | } |
| 576 | |
| 577 | /** |
| 578 | * Reads in a criterion from the given resource and decodes it. |
| 579 | * |
| 580 | * @param resourceName resource to use to read the JSON for the rule |
| 581 | * @return decoded criterion |
| 582 | * @throws IOException if processing the resource fails |
| 583 | */ |
| 584 | private Criterion getCriterion(String resourceName) throws IOException { |
| 585 | InputStream jsonStream = CriterionCodecTest.class.getResourceAsStream(resourceName); |
| 586 | JsonNode json = context.mapper().readTree(jsonStream); |
| 587 | MatcherAssert.assertThat(json, notNullValue()); |
| 588 | Criterion criterion = criterionCodec.decode((ObjectNode) json, context); |
| 589 | Assert.assertThat(criterion, notNullValue()); |
| 590 | return criterion; |
| 591 | } |
Ray Milkey | 73ba84a | 2015-02-04 17:08:41 -0800 | [diff] [blame] | 592 | } |