Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [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 | |
| 17 | package org.onosproject.net.flow.criteria; |
| 18 | |
| 19 | import com.google.common.annotations.Beta; |
| 20 | import com.google.common.base.Objects; |
Carmelo Cascone | 32a01b2 | 2017-07-18 10:51:07 -0400 | [diff] [blame] | 21 | import com.google.common.collect.ImmutableMap; |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 22 | import org.onosproject.net.pi.model.PiMatchFieldId; |
Carmelo Cascone | 32a01b2 | 2017-07-18 10:51:07 -0400 | [diff] [blame] | 23 | import org.onosproject.net.pi.runtime.PiExactFieldMatch; |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 24 | import org.onosproject.net.pi.runtime.PiFieldMatch; |
Frank Wang | df38321 | 2017-06-23 09:17:41 +0800 | [diff] [blame] | 25 | import org.onosproject.net.pi.runtime.PiLpmFieldMatch; |
Daniele Moro | c6f2f7f | 2020-12-18 10:55:57 +0100 | [diff] [blame] | 26 | import org.onosproject.net.pi.runtime.PiOptionalFieldMatch; |
Frank Wang | df38321 | 2017-06-23 09:17:41 +0800 | [diff] [blame] | 27 | import org.onosproject.net.pi.runtime.PiRangeFieldMatch; |
Carmelo Cascone | 32a01b2 | 2017-07-18 10:51:07 -0400 | [diff] [blame] | 28 | import org.onosproject.net.pi.runtime.PiTernaryFieldMatch; |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 29 | |
| 30 | import java.util.Collection; |
Carmelo Cascone | 32a01b2 | 2017-07-18 10:51:07 -0400 | [diff] [blame] | 31 | import java.util.Optional; |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 32 | import java.util.StringJoiner; |
| 33 | |
Frank Wang | df38321 | 2017-06-23 09:17:41 +0800 | [diff] [blame] | 34 | import static com.google.common.base.Preconditions.checkArgument; |
| 35 | import static org.onlab.util.ImmutableByteSequence.copyFrom; |
| 36 | |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 37 | /** |
Carmelo Cascone | 2261917 | 2017-07-13 16:08:17 -0400 | [diff] [blame] | 38 | * Protocol-independent criterion. |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 39 | */ |
| 40 | @Beta |
| 41 | public final class PiCriterion implements Criterion { |
| 42 | |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 43 | private final ImmutableMap<PiMatchFieldId, PiFieldMatch> fieldMatchMap; |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 44 | |
| 45 | /** |
| 46 | * Creates a new protocol-independent criterion for the given match fields. |
| 47 | * |
Carmelo Cascone | 32a01b2 | 2017-07-18 10:51:07 -0400 | [diff] [blame] | 48 | * @param fieldMatchMap field match map |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 49 | */ |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 50 | private PiCriterion(ImmutableMap<PiMatchFieldId, PiFieldMatch> fieldMatchMap) { |
Carmelo Cascone | 32a01b2 | 2017-07-18 10:51:07 -0400 | [diff] [blame] | 51 | this.fieldMatchMap = fieldMatchMap; |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | /** |
Carmelo Cascone | 32a01b2 | 2017-07-18 10:51:07 -0400 | [diff] [blame] | 55 | * Returns all protocol-independent field matches defined by this criterion. |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 56 | * |
Carmelo Cascone | 32a01b2 | 2017-07-18 10:51:07 -0400 | [diff] [blame] | 57 | * @return collection of match parameters |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 58 | */ |
| 59 | public Collection<PiFieldMatch> fieldMatches() { |
Carmelo Cascone | 32a01b2 | 2017-07-18 10:51:07 -0400 | [diff] [blame] | 60 | return fieldMatchMap.values(); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * If present, returns the field match associated with the given header field identifier. |
| 65 | * |
| 66 | * @param fieldId field identifier |
| 67 | * @return optional field match |
| 68 | */ |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 69 | public Optional<PiFieldMatch> fieldMatch(PiMatchFieldId fieldId) { |
Carmelo Cascone | 32a01b2 | 2017-07-18 10:51:07 -0400 | [diff] [blame] | 70 | return Optional.ofNullable(fieldMatchMap.get(fieldId)); |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | @Override |
| 74 | public Type type() { |
| 75 | return Type.PROTOCOL_INDEPENDENT; |
| 76 | } |
| 77 | |
| 78 | @Override |
| 79 | public boolean equals(Object o) { |
| 80 | if (this == o) { |
| 81 | return true; |
| 82 | } |
| 83 | if (o == null || getClass() != o.getClass()) { |
| 84 | return false; |
| 85 | } |
| 86 | PiCriterion that = (PiCriterion) o; |
Carmelo Cascone | 32a01b2 | 2017-07-18 10:51:07 -0400 | [diff] [blame] | 87 | return Objects.equal(fieldMatchMap, that.fieldMatchMap); |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | @Override |
| 91 | public int hashCode() { |
Carmelo Cascone | 32a01b2 | 2017-07-18 10:51:07 -0400 | [diff] [blame] | 92 | return Objects.hashCode(fieldMatchMap); |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | @Override |
| 96 | public String toString() { |
Carmelo Cascone | 0316b96 | 2018-04-11 17:39:07 -0700 | [diff] [blame] | 97 | StringJoiner stringParams = new StringJoiner(", "); |
Carmelo Cascone | 32a01b2 | 2017-07-18 10:51:07 -0400 | [diff] [blame] | 98 | fieldMatchMap.forEach((key, value) -> stringParams.add(value.toString())); |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 99 | return stringParams.toString(); |
| 100 | } |
Frank Wang | df38321 | 2017-06-23 09:17:41 +0800 | [diff] [blame] | 101 | |
| 102 | /** |
| 103 | * Returns the PiCriterion builder. |
| 104 | * |
| 105 | * @return PiCriterion builder |
| 106 | */ |
| 107 | public static Builder builder() { |
| 108 | return new Builder(); |
| 109 | } |
| 110 | |
| 111 | /** |
pierventre | 5a936f5 | 2020-10-08 17:21:49 +0200 | [diff] [blame] | 112 | * Returns the PiCriterion builder initialized by the given PiCriterion. |
| 113 | * |
| 114 | * @param piCriterion the input PiCriterion |
| 115 | * @return PiCriterion builder |
| 116 | */ |
| 117 | public static Builder builder(PiCriterion piCriterion) { |
| 118 | return new Builder(piCriterion); |
| 119 | } |
| 120 | |
| 121 | /** |
Frank Wang | df38321 | 2017-06-23 09:17:41 +0800 | [diff] [blame] | 122 | * PiCriterion Builder. |
| 123 | */ |
| 124 | @Beta |
| 125 | public static final class Builder { |
| 126 | |
Carmelo Cascone | 2261917 | 2017-07-13 16:08:17 -0400 | [diff] [blame] | 127 | // Use map to guarantee that there's only one field match per field id. |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 128 | private final ImmutableMap.Builder<PiMatchFieldId, PiFieldMatch> fieldMatchMapBuilder = ImmutableMap.builder(); |
Frank Wang | df38321 | 2017-06-23 09:17:41 +0800 | [diff] [blame] | 129 | |
| 130 | private Builder() { |
| 131 | // ban constructor. |
| 132 | } |
| 133 | |
pierventre | 5a936f5 | 2020-10-08 17:21:49 +0200 | [diff] [blame] | 134 | private Builder(PiCriterion piCriterion) { |
| 135 | piCriterion.fieldMatchMap.forEach(((piMatchFieldId, piFieldMatch) -> add(piFieldMatch))); |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Adds a match field to the builder. |
| 140 | * |
| 141 | * @param field the field value |
| 142 | * @return this |
| 143 | */ |
| 144 | public Builder add(PiFieldMatch field) { |
| 145 | fieldMatchMapBuilder.put(field.fieldId(), field); |
| 146 | return this; |
| 147 | } |
| 148 | |
Frank Wang | df38321 | 2017-06-23 09:17:41 +0800 | [diff] [blame] | 149 | /** |
| 150 | * Adds an exact field match for the given fieldId and value. |
| 151 | * |
| 152 | * @param fieldId protocol-independent header field Id |
Carmelo Cascone | 32a01b2 | 2017-07-18 10:51:07 -0400 | [diff] [blame] | 153 | * @param value exact match value |
Frank Wang | df38321 | 2017-06-23 09:17:41 +0800 | [diff] [blame] | 154 | * @return this |
| 155 | */ |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 156 | public Builder matchExact(PiMatchFieldId fieldId, short value) { |
Carmelo Cascone | 32a01b2 | 2017-07-18 10:51:07 -0400 | [diff] [blame] | 157 | fieldMatchMapBuilder.put(fieldId, new PiExactFieldMatch(fieldId, copyFrom(value))); |
Frank Wang | df38321 | 2017-06-23 09:17:41 +0800 | [diff] [blame] | 158 | return this; |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Adds an exact field match for the given fieldId and value. |
| 163 | * |
| 164 | * @param fieldId protocol-independent header field Id |
Carmelo Cascone | 32a01b2 | 2017-07-18 10:51:07 -0400 | [diff] [blame] | 165 | * @param value exact match value |
Frank Wang | df38321 | 2017-06-23 09:17:41 +0800 | [diff] [blame] | 166 | * @return this |
| 167 | */ |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 168 | public Builder matchExact(PiMatchFieldId fieldId, int value) { |
Carmelo Cascone | 32a01b2 | 2017-07-18 10:51:07 -0400 | [diff] [blame] | 169 | fieldMatchMapBuilder.put(fieldId, new PiExactFieldMatch(fieldId, copyFrom(value))); |
Frank Wang | df38321 | 2017-06-23 09:17:41 +0800 | [diff] [blame] | 170 | return this; |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Adds an exact field match for the given fieldId and value. |
| 175 | * |
| 176 | * @param fieldId protocol-independent header field Id |
Carmelo Cascone | 32a01b2 | 2017-07-18 10:51:07 -0400 | [diff] [blame] | 177 | * @param value exact match value |
Frank Wang | df38321 | 2017-06-23 09:17:41 +0800 | [diff] [blame] | 178 | * @return this |
| 179 | */ |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 180 | public Builder matchExact(PiMatchFieldId fieldId, long value) { |
Carmelo Cascone | 32a01b2 | 2017-07-18 10:51:07 -0400 | [diff] [blame] | 181 | fieldMatchMapBuilder.put(fieldId, new PiExactFieldMatch(fieldId, copyFrom(value))); |
Frank Wang | df38321 | 2017-06-23 09:17:41 +0800 | [diff] [blame] | 182 | return this; |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Adds an exact field match for the given fieldId and value. |
| 187 | * |
| 188 | * @param fieldId protocol-independent header field Id |
Carmelo Cascone | 32a01b2 | 2017-07-18 10:51:07 -0400 | [diff] [blame] | 189 | * @param value exact match value |
Frank Wang | df38321 | 2017-06-23 09:17:41 +0800 | [diff] [blame] | 190 | * @return this |
| 191 | */ |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 192 | public Builder matchExact(PiMatchFieldId fieldId, byte[] value) { |
Carmelo Cascone | 32a01b2 | 2017-07-18 10:51:07 -0400 | [diff] [blame] | 193 | fieldMatchMapBuilder.put(fieldId, new PiExactFieldMatch(fieldId, copyFrom(value))); |
Frank Wang | df38321 | 2017-06-23 09:17:41 +0800 | [diff] [blame] | 194 | return this; |
| 195 | } |
| 196 | |
| 197 | /** |
Daniele Moro | 7aa13e6 | 2021-02-23 15:28:07 +0100 | [diff] [blame] | 198 | * Adds an exact field match for the given fieldId and value. |
| 199 | * |
| 200 | * @param fieldId protocol-independent header field Id |
| 201 | * @param value exact match value |
| 202 | * @return this |
| 203 | */ |
| 204 | public Builder matchExact(PiMatchFieldId fieldId, String value) { |
| 205 | fieldMatchMapBuilder.put(fieldId, new PiExactFieldMatch(fieldId, copyFrom(value))); |
| 206 | return this; |
| 207 | } |
| 208 | |
| 209 | /** |
Frank Wang | df38321 | 2017-06-23 09:17:41 +0800 | [diff] [blame] | 210 | * Adds a ternary field match for the given fieldId, value and mask. |
| 211 | * |
| 212 | * @param fieldId protocol-independent header field Id |
Carmelo Cascone | 32a01b2 | 2017-07-18 10:51:07 -0400 | [diff] [blame] | 213 | * @param value ternary match value |
| 214 | * @param mask ternary match mask |
Frank Wang | df38321 | 2017-06-23 09:17:41 +0800 | [diff] [blame] | 215 | * @return this |
| 216 | */ |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 217 | public Builder matchTernary(PiMatchFieldId fieldId, short value, short mask) { |
Carmelo Cascone | 32a01b2 | 2017-07-18 10:51:07 -0400 | [diff] [blame] | 218 | fieldMatchMapBuilder.put(fieldId, new PiTernaryFieldMatch(fieldId, copyFrom(value), copyFrom(mask))); |
Frank Wang | df38321 | 2017-06-23 09:17:41 +0800 | [diff] [blame] | 219 | return this; |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * Adds a ternary field match for the given fieldId, value and mask. |
| 224 | * |
| 225 | * @param fieldId protocol-independent header field Id |
Carmelo Cascone | 32a01b2 | 2017-07-18 10:51:07 -0400 | [diff] [blame] | 226 | * @param value ternary match value |
| 227 | * @param mask ternary match mask |
Frank Wang | df38321 | 2017-06-23 09:17:41 +0800 | [diff] [blame] | 228 | * @return this |
| 229 | */ |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 230 | public Builder matchTernary(PiMatchFieldId fieldId, int value, int mask) { |
Carmelo Cascone | 32a01b2 | 2017-07-18 10:51:07 -0400 | [diff] [blame] | 231 | fieldMatchMapBuilder.put(fieldId, new PiTernaryFieldMatch(fieldId, copyFrom(value), copyFrom(mask))); |
Frank Wang | df38321 | 2017-06-23 09:17:41 +0800 | [diff] [blame] | 232 | return this; |
| 233 | } |
| 234 | |
| 235 | /** |
| 236 | * Adds a ternary field match for the given fieldId, value and mask. |
| 237 | * |
| 238 | * @param fieldId protocol-independent header field Id |
Carmelo Cascone | 32a01b2 | 2017-07-18 10:51:07 -0400 | [diff] [blame] | 239 | * @param value ternary match value |
| 240 | * @param mask ternary match mask |
Frank Wang | df38321 | 2017-06-23 09:17:41 +0800 | [diff] [blame] | 241 | * @return this |
| 242 | */ |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 243 | public Builder matchTernary(PiMatchFieldId fieldId, long value, long mask) { |
Carmelo Cascone | 32a01b2 | 2017-07-18 10:51:07 -0400 | [diff] [blame] | 244 | fieldMatchMapBuilder.put(fieldId, new PiTernaryFieldMatch(fieldId, copyFrom(value), copyFrom(mask))); |
Frank Wang | df38321 | 2017-06-23 09:17:41 +0800 | [diff] [blame] | 245 | return this; |
| 246 | } |
| 247 | |
| 248 | /** |
| 249 | * Adds a ternary field match for the given fieldId, value and mask. |
| 250 | * |
| 251 | * @param fieldId protocol-independent header field Id |
Carmelo Cascone | 32a01b2 | 2017-07-18 10:51:07 -0400 | [diff] [blame] | 252 | * @param value ternary match value |
| 253 | * @param mask ternary match mask |
Frank Wang | df38321 | 2017-06-23 09:17:41 +0800 | [diff] [blame] | 254 | * @return this |
| 255 | */ |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 256 | public Builder matchTernary(PiMatchFieldId fieldId, byte[] value, byte[] mask) { |
Carmelo Cascone | 32a01b2 | 2017-07-18 10:51:07 -0400 | [diff] [blame] | 257 | fieldMatchMapBuilder.put(fieldId, new PiTernaryFieldMatch(fieldId, copyFrom(value), copyFrom(mask))); |
Frank Wang | df38321 | 2017-06-23 09:17:41 +0800 | [diff] [blame] | 258 | return this; |
| 259 | } |
| 260 | |
| 261 | /** |
| 262 | * Adds a longest-prefix field match for the given fieldId, value and prefix length. |
| 263 | * |
Carmelo Cascone | 32a01b2 | 2017-07-18 10:51:07 -0400 | [diff] [blame] | 264 | * @param fieldId protocol-independent header field Id |
| 265 | * @param value lpm match value |
Frank Wang | df38321 | 2017-06-23 09:17:41 +0800 | [diff] [blame] | 266 | * @param prefixLength lpm match prefix length |
| 267 | * @return this |
| 268 | */ |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 269 | public Builder matchLpm(PiMatchFieldId fieldId, short value, int prefixLength) { |
Carmelo Cascone | 32a01b2 | 2017-07-18 10:51:07 -0400 | [diff] [blame] | 270 | fieldMatchMapBuilder.put(fieldId, new PiLpmFieldMatch(fieldId, copyFrom(value), prefixLength)); |
Frank Wang | df38321 | 2017-06-23 09:17:41 +0800 | [diff] [blame] | 271 | return this; |
| 272 | } |
| 273 | |
| 274 | /** |
| 275 | * Adds a longest-prefix field match for the given fieldId, value and prefix length. |
| 276 | * |
Carmelo Cascone | 32a01b2 | 2017-07-18 10:51:07 -0400 | [diff] [blame] | 277 | * @param fieldId protocol-independent header field Id |
| 278 | * @param value lpm match value |
Frank Wang | df38321 | 2017-06-23 09:17:41 +0800 | [diff] [blame] | 279 | * @param prefixLength lpm match prefix length |
| 280 | * @return this |
| 281 | */ |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 282 | public Builder matchLpm(PiMatchFieldId fieldId, int value, int prefixLength) { |
Carmelo Cascone | 32a01b2 | 2017-07-18 10:51:07 -0400 | [diff] [blame] | 283 | fieldMatchMapBuilder.put(fieldId, new PiLpmFieldMatch(fieldId, copyFrom(value), prefixLength)); |
Frank Wang | df38321 | 2017-06-23 09:17:41 +0800 | [diff] [blame] | 284 | return this; |
| 285 | } |
| 286 | |
| 287 | /** |
| 288 | * Adds a longest-prefix field match for the given fieldId, value and prefix length. |
| 289 | * |
Carmelo Cascone | 32a01b2 | 2017-07-18 10:51:07 -0400 | [diff] [blame] | 290 | * @param fieldId protocol-independent header field Id |
| 291 | * @param value lpm match value |
Frank Wang | df38321 | 2017-06-23 09:17:41 +0800 | [diff] [blame] | 292 | * @param prefixLength lpm match prefix length |
| 293 | * @return this |
| 294 | */ |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 295 | public Builder matchLpm(PiMatchFieldId fieldId, long value, int prefixLength) { |
Carmelo Cascone | 32a01b2 | 2017-07-18 10:51:07 -0400 | [diff] [blame] | 296 | fieldMatchMapBuilder.put(fieldId, new PiLpmFieldMatch(fieldId, copyFrom(value), prefixLength)); |
Frank Wang | df38321 | 2017-06-23 09:17:41 +0800 | [diff] [blame] | 297 | return this; |
| 298 | } |
| 299 | |
| 300 | /** |
| 301 | * Adds a longest-prefix field match for the given fieldId, value and prefix length. |
| 302 | * |
Carmelo Cascone | 32a01b2 | 2017-07-18 10:51:07 -0400 | [diff] [blame] | 303 | * @param fieldId protocol-independent header field Id |
| 304 | * @param value lpm match value |
Frank Wang | df38321 | 2017-06-23 09:17:41 +0800 | [diff] [blame] | 305 | * @param prefixLength lpm match prefix length |
| 306 | * @return this |
| 307 | */ |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 308 | public Builder matchLpm(PiMatchFieldId fieldId, byte[] value, int prefixLength) { |
Carmelo Cascone | 32a01b2 | 2017-07-18 10:51:07 -0400 | [diff] [blame] | 309 | fieldMatchMapBuilder.put(fieldId, new PiLpmFieldMatch(fieldId, copyFrom(value), prefixLength)); |
Frank Wang | df38321 | 2017-06-23 09:17:41 +0800 | [diff] [blame] | 310 | return this; |
| 311 | } |
| 312 | |
| 313 | /** |
Frank Wang | df38321 | 2017-06-23 09:17:41 +0800 | [diff] [blame] | 314 | * Adds a range field match for the given fieldId, low and high. |
| 315 | * |
| 316 | * @param fieldId protocol-independent header field Id |
Carmelo Cascone | 32a01b2 | 2017-07-18 10:51:07 -0400 | [diff] [blame] | 317 | * @param low range match low value |
| 318 | * @param high range match high value |
Frank Wang | df38321 | 2017-06-23 09:17:41 +0800 | [diff] [blame] | 319 | * @return this |
| 320 | */ |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 321 | public Builder matchRange(PiMatchFieldId fieldId, short low, short high) { |
Carmelo Cascone | 32a01b2 | 2017-07-18 10:51:07 -0400 | [diff] [blame] | 322 | fieldMatchMapBuilder.put(fieldId, new PiRangeFieldMatch(fieldId, copyFrom(low), copyFrom(high))); |
Frank Wang | df38321 | 2017-06-23 09:17:41 +0800 | [diff] [blame] | 323 | return this; |
| 324 | } |
| 325 | |
| 326 | /** |
| 327 | * Adds a range field match for the given fieldId, low and high. |
| 328 | * |
| 329 | * @param fieldId protocol-independent header field Id |
Carmelo Cascone | 32a01b2 | 2017-07-18 10:51:07 -0400 | [diff] [blame] | 330 | * @param low range match low value |
| 331 | * @param high range match high value |
Frank Wang | df38321 | 2017-06-23 09:17:41 +0800 | [diff] [blame] | 332 | * @return this |
| 333 | */ |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 334 | public Builder matchRange(PiMatchFieldId fieldId, int low, int high) { |
Carmelo Cascone | 32a01b2 | 2017-07-18 10:51:07 -0400 | [diff] [blame] | 335 | fieldMatchMapBuilder.put(fieldId, new PiRangeFieldMatch(fieldId, copyFrom(low), copyFrom(high))); |
Frank Wang | df38321 | 2017-06-23 09:17:41 +0800 | [diff] [blame] | 336 | return this; |
| 337 | } |
Carmelo Cascone | 32a01b2 | 2017-07-18 10:51:07 -0400 | [diff] [blame] | 338 | |
Frank Wang | df38321 | 2017-06-23 09:17:41 +0800 | [diff] [blame] | 339 | /** |
| 340 | * Adds a range field match for the given fieldId, low and high. |
| 341 | * |
| 342 | * @param fieldId protocol-independent header field Id |
Carmelo Cascone | 32a01b2 | 2017-07-18 10:51:07 -0400 | [diff] [blame] | 343 | * @param low range match low value |
| 344 | * @param high range match high value |
Frank Wang | df38321 | 2017-06-23 09:17:41 +0800 | [diff] [blame] | 345 | * @return this |
| 346 | */ |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 347 | public Builder matchRange(PiMatchFieldId fieldId, long low, long high) { |
Carmelo Cascone | 32a01b2 | 2017-07-18 10:51:07 -0400 | [diff] [blame] | 348 | fieldMatchMapBuilder.put(fieldId, new PiRangeFieldMatch(fieldId, copyFrom(low), copyFrom(high))); |
Frank Wang | df38321 | 2017-06-23 09:17:41 +0800 | [diff] [blame] | 349 | return this; |
| 350 | } |
Carmelo Cascone | 32a01b2 | 2017-07-18 10:51:07 -0400 | [diff] [blame] | 351 | |
Frank Wang | df38321 | 2017-06-23 09:17:41 +0800 | [diff] [blame] | 352 | /** |
| 353 | * Adds a range field match for the given fieldId, low and high. |
| 354 | * |
| 355 | * @param fieldId protocol-independent header field Id |
Carmelo Cascone | 32a01b2 | 2017-07-18 10:51:07 -0400 | [diff] [blame] | 356 | * @param low range match low value |
| 357 | * @param high range match high value |
Frank Wang | df38321 | 2017-06-23 09:17:41 +0800 | [diff] [blame] | 358 | * @return this |
| 359 | */ |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 360 | public Builder matchRange(PiMatchFieldId fieldId, byte[] low, byte[] high) { |
Carmelo Cascone | 32a01b2 | 2017-07-18 10:51:07 -0400 | [diff] [blame] | 361 | fieldMatchMapBuilder.put(fieldId, new PiRangeFieldMatch(fieldId, copyFrom(low), copyFrom(high))); |
Frank Wang | df38321 | 2017-06-23 09:17:41 +0800 | [diff] [blame] | 362 | return this; |
| 363 | } |
| 364 | |
| 365 | /** |
Daniele Moro | c6f2f7f | 2020-12-18 10:55:57 +0100 | [diff] [blame] | 366 | * Adds an optional field match for the given fieldId and value. |
| 367 | * |
| 368 | * @param fieldId protocol-independent header field Id |
| 369 | * @param value optional match value |
| 370 | * @return this |
| 371 | */ |
| 372 | public Builder matchOptional(PiMatchFieldId fieldId, short value) { |
| 373 | fieldMatchMapBuilder.put(fieldId, new PiOptionalFieldMatch(fieldId, copyFrom(value))); |
| 374 | return this; |
| 375 | } |
| 376 | |
| 377 | /** |
| 378 | * Adds an optional field match for the given fieldId and value. |
| 379 | * |
| 380 | * @param fieldId protocol-independent header field Id |
| 381 | * @param value optional match value |
| 382 | * @return this |
| 383 | */ |
| 384 | public Builder matchOptional(PiMatchFieldId fieldId, int value) { |
| 385 | fieldMatchMapBuilder.put(fieldId, new PiOptionalFieldMatch(fieldId, copyFrom(value))); |
| 386 | return this; |
| 387 | } |
| 388 | |
| 389 | /** |
| 390 | * Adds an optional field match for the given fieldId and value. |
| 391 | * |
| 392 | * @param fieldId protocol-independent header field Id |
| 393 | * @param value optional match value |
| 394 | * @return this |
| 395 | */ |
| 396 | public Builder matchOptional(PiMatchFieldId fieldId, long value) { |
| 397 | fieldMatchMapBuilder.put(fieldId, new PiOptionalFieldMatch(fieldId, copyFrom(value))); |
| 398 | return this; |
| 399 | } |
| 400 | |
| 401 | /** |
| 402 | * Adds an optional field match for the given fieldId and value. |
| 403 | * |
| 404 | * @param fieldId protocol-independent header field Id |
| 405 | * @param value optional match value |
| 406 | * @return this |
| 407 | */ |
| 408 | public Builder matchOptional(PiMatchFieldId fieldId, byte[] value) { |
| 409 | fieldMatchMapBuilder.put(fieldId, new PiOptionalFieldMatch(fieldId, copyFrom(value))); |
| 410 | return this; |
| 411 | } |
| 412 | |
| 413 | /** |
Daniele Moro | 7aa13e6 | 2021-02-23 15:28:07 +0100 | [diff] [blame] | 414 | * Adds an optional field match for the given fieldId and value. |
| 415 | * |
| 416 | * @param fieldId protocol-independent header field Id |
| 417 | * @param value optional match value |
| 418 | * @return this |
| 419 | */ |
| 420 | public Builder matchOptional(PiMatchFieldId fieldId, String value) { |
| 421 | fieldMatchMapBuilder.put(fieldId, new PiOptionalFieldMatch(fieldId, copyFrom(value))); |
| 422 | return this; |
| 423 | } |
| 424 | |
| 425 | /** |
Frank Wang | df38321 | 2017-06-23 09:17:41 +0800 | [diff] [blame] | 426 | * Builds a PiCriterion. |
| 427 | * |
| 428 | * @return PiCriterion |
| 429 | */ |
Carmelo Cascone | 770507f | 2017-09-14 20:58:04 +0200 | [diff] [blame] | 430 | public PiCriterion build() { |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 431 | ImmutableMap<PiMatchFieldId, PiFieldMatch> fieldMatchMap = fieldMatchMapBuilder.build(); |
Carmelo Cascone | 32a01b2 | 2017-07-18 10:51:07 -0400 | [diff] [blame] | 432 | checkArgument(fieldMatchMap.size() > 0, "Cannot build PI criterion with 0 field matches"); |
| 433 | return new PiCriterion(fieldMatchMap); |
Frank Wang | df38321 | 2017-06-23 09:17:41 +0800 | [diff] [blame] | 434 | } |
| 435 | } |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 436 | } |