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