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