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