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; |
| 26 | import org.onosproject.net.pi.runtime.PiRangeFieldMatch; |
Carmelo Cascone | 32a01b2 | 2017-07-18 10:51:07 -0400 | [diff] [blame] | 27 | import org.onosproject.net.pi.runtime.PiTernaryFieldMatch; |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 28 | |
| 29 | import java.util.Collection; |
Carmelo Cascone | 32a01b2 | 2017-07-18 10:51:07 -0400 | [diff] [blame] | 30 | import java.util.Optional; |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 31 | import java.util.StringJoiner; |
| 32 | |
Frank Wang | df38321 | 2017-06-23 09:17:41 +0800 | [diff] [blame] | 33 | import static com.google.common.base.Preconditions.checkArgument; |
| 34 | import static org.onlab.util.ImmutableByteSequence.copyFrom; |
| 35 | |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 36 | /** |
Carmelo Cascone | 2261917 | 2017-07-13 16:08:17 -0400 | [diff] [blame] | 37 | * Protocol-independent criterion. |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 38 | */ |
| 39 | @Beta |
| 40 | public final class PiCriterion implements Criterion { |
| 41 | |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 42 | private final ImmutableMap<PiMatchFieldId, PiFieldMatch> fieldMatchMap; |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 43 | |
| 44 | /** |
| 45 | * Creates a new protocol-independent criterion for the given match fields. |
| 46 | * |
Carmelo Cascone | 32a01b2 | 2017-07-18 10:51:07 -0400 | [diff] [blame] | 47 | * @param fieldMatchMap field match map |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 48 | */ |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 49 | private PiCriterion(ImmutableMap<PiMatchFieldId, PiFieldMatch> fieldMatchMap) { |
Carmelo Cascone | 32a01b2 | 2017-07-18 10:51:07 -0400 | [diff] [blame] | 50 | this.fieldMatchMap = fieldMatchMap; |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | /** |
Carmelo Cascone | 32a01b2 | 2017-07-18 10:51:07 -0400 | [diff] [blame] | 54 | * Returns all protocol-independent field matches defined by this criterion. |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 55 | * |
Carmelo Cascone | 32a01b2 | 2017-07-18 10:51:07 -0400 | [diff] [blame] | 56 | * @return collection of match parameters |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 57 | */ |
| 58 | public Collection<PiFieldMatch> fieldMatches() { |
Carmelo Cascone | 32a01b2 | 2017-07-18 10:51:07 -0400 | [diff] [blame] | 59 | return fieldMatchMap.values(); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * If present, returns the field match associated with the given header field identifier. |
| 64 | * |
| 65 | * @param fieldId field identifier |
| 66 | * @return optional field match |
| 67 | */ |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 68 | public Optional<PiFieldMatch> fieldMatch(PiMatchFieldId fieldId) { |
Carmelo Cascone | 32a01b2 | 2017-07-18 10:51:07 -0400 | [diff] [blame] | 69 | return Optional.ofNullable(fieldMatchMap.get(fieldId)); |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | @Override |
| 73 | public Type type() { |
| 74 | return Type.PROTOCOL_INDEPENDENT; |
| 75 | } |
| 76 | |
| 77 | @Override |
| 78 | public boolean equals(Object o) { |
| 79 | if (this == o) { |
| 80 | return true; |
| 81 | } |
| 82 | if (o == null || getClass() != o.getClass()) { |
| 83 | return false; |
| 84 | } |
| 85 | PiCriterion that = (PiCriterion) o; |
Carmelo Cascone | 32a01b2 | 2017-07-18 10:51:07 -0400 | [diff] [blame] | 86 | return Objects.equal(fieldMatchMap, that.fieldMatchMap); |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | @Override |
| 90 | public int hashCode() { |
Carmelo Cascone | 32a01b2 | 2017-07-18 10:51:07 -0400 | [diff] [blame] | 91 | return Objects.hashCode(fieldMatchMap); |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | @Override |
| 95 | public String toString() { |
Carmelo Cascone | 0316b96 | 2018-04-11 17:39:07 -0700 | [diff] [blame] | 96 | StringJoiner stringParams = new StringJoiner(", "); |
Carmelo Cascone | 32a01b2 | 2017-07-18 10:51:07 -0400 | [diff] [blame] | 97 | fieldMatchMap.forEach((key, value) -> stringParams.add(value.toString())); |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 98 | return stringParams.toString(); |
| 99 | } |
Frank Wang | df38321 | 2017-06-23 09:17:41 +0800 | [diff] [blame] | 100 | |
| 101 | /** |
| 102 | * Returns the PiCriterion builder. |
| 103 | * |
| 104 | * @return PiCriterion builder |
| 105 | */ |
| 106 | public static Builder builder() { |
| 107 | return new Builder(); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * PiCriterion Builder. |
| 112 | */ |
| 113 | @Beta |
| 114 | public static final class Builder { |
| 115 | |
Carmelo Cascone | 2261917 | 2017-07-13 16:08:17 -0400 | [diff] [blame] | 116 | // 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] | 117 | private final ImmutableMap.Builder<PiMatchFieldId, PiFieldMatch> fieldMatchMapBuilder = ImmutableMap.builder(); |
Frank Wang | df38321 | 2017-06-23 09:17:41 +0800 | [diff] [blame] | 118 | |
| 119 | private Builder() { |
| 120 | // ban constructor. |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Adds an exact field match for the given fieldId and value. |
| 125 | * |
| 126 | * @param fieldId protocol-independent header field Id |
Carmelo Cascone | 32a01b2 | 2017-07-18 10:51:07 -0400 | [diff] [blame] | 127 | * @param value exact match value |
Frank Wang | df38321 | 2017-06-23 09:17:41 +0800 | [diff] [blame] | 128 | * @return this |
| 129 | */ |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 130 | public Builder matchExact(PiMatchFieldId fieldId, short value) { |
Carmelo Cascone | 32a01b2 | 2017-07-18 10:51:07 -0400 | [diff] [blame] | 131 | fieldMatchMapBuilder.put(fieldId, new PiExactFieldMatch(fieldId, copyFrom(value))); |
Frank Wang | df38321 | 2017-06-23 09:17:41 +0800 | [diff] [blame] | 132 | return this; |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Adds an exact field match for the given fieldId and value. |
| 137 | * |
| 138 | * @param fieldId protocol-independent header field Id |
Carmelo Cascone | 32a01b2 | 2017-07-18 10:51:07 -0400 | [diff] [blame] | 139 | * @param value exact match value |
Frank Wang | df38321 | 2017-06-23 09:17:41 +0800 | [diff] [blame] | 140 | * @return this |
| 141 | */ |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 142 | public Builder matchExact(PiMatchFieldId fieldId, int value) { |
Carmelo Cascone | 32a01b2 | 2017-07-18 10:51:07 -0400 | [diff] [blame] | 143 | fieldMatchMapBuilder.put(fieldId, new PiExactFieldMatch(fieldId, copyFrom(value))); |
Frank Wang | df38321 | 2017-06-23 09:17:41 +0800 | [diff] [blame] | 144 | return this; |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Adds an exact field match for the given fieldId and value. |
| 149 | * |
| 150 | * @param fieldId protocol-independent header field Id |
Carmelo Cascone | 32a01b2 | 2017-07-18 10:51:07 -0400 | [diff] [blame] | 151 | * @param value exact match value |
Frank Wang | df38321 | 2017-06-23 09:17:41 +0800 | [diff] [blame] | 152 | * @return this |
| 153 | */ |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 154 | public Builder matchExact(PiMatchFieldId fieldId, long value) { |
Carmelo Cascone | 32a01b2 | 2017-07-18 10:51:07 -0400 | [diff] [blame] | 155 | fieldMatchMapBuilder.put(fieldId, new PiExactFieldMatch(fieldId, copyFrom(value))); |
Frank Wang | df38321 | 2017-06-23 09:17:41 +0800 | [diff] [blame] | 156 | return this; |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Adds an exact field match for the given fieldId and value. |
| 161 | * |
| 162 | * @param fieldId protocol-independent header field Id |
Carmelo Cascone | 32a01b2 | 2017-07-18 10:51:07 -0400 | [diff] [blame] | 163 | * @param value exact match value |
Frank Wang | df38321 | 2017-06-23 09:17:41 +0800 | [diff] [blame] | 164 | * @return this |
| 165 | */ |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 166 | public Builder matchExact(PiMatchFieldId fieldId, byte[] value) { |
Carmelo Cascone | 32a01b2 | 2017-07-18 10:51:07 -0400 | [diff] [blame] | 167 | fieldMatchMapBuilder.put(fieldId, new PiExactFieldMatch(fieldId, copyFrom(value))); |
Frank Wang | df38321 | 2017-06-23 09:17:41 +0800 | [diff] [blame] | 168 | return this; |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Adds a ternary field match for the given fieldId, value and mask. |
| 173 | * |
| 174 | * @param fieldId protocol-independent header field Id |
Carmelo Cascone | 32a01b2 | 2017-07-18 10:51:07 -0400 | [diff] [blame] | 175 | * @param value ternary match value |
| 176 | * @param mask ternary match mask |
Frank Wang | df38321 | 2017-06-23 09:17:41 +0800 | [diff] [blame] | 177 | * @return this |
| 178 | */ |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 179 | public Builder matchTernary(PiMatchFieldId fieldId, short value, short mask) { |
Carmelo Cascone | 32a01b2 | 2017-07-18 10:51:07 -0400 | [diff] [blame] | 180 | fieldMatchMapBuilder.put(fieldId, new PiTernaryFieldMatch(fieldId, copyFrom(value), copyFrom(mask))); |
Frank Wang | df38321 | 2017-06-23 09:17:41 +0800 | [diff] [blame] | 181 | return this; |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Adds a ternary field match for the given fieldId, value and mask. |
| 186 | * |
| 187 | * @param fieldId protocol-independent header field Id |
Carmelo Cascone | 32a01b2 | 2017-07-18 10:51:07 -0400 | [diff] [blame] | 188 | * @param value ternary match value |
| 189 | * @param mask ternary match mask |
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 matchTernary(PiMatchFieldId fieldId, int value, int mask) { |
Carmelo Cascone | 32a01b2 | 2017-07-18 10:51:07 -0400 | [diff] [blame] | 193 | fieldMatchMapBuilder.put(fieldId, new PiTernaryFieldMatch(fieldId, copyFrom(value), copyFrom(mask))); |
Frank Wang | df38321 | 2017-06-23 09:17:41 +0800 | [diff] [blame] | 194 | return this; |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * Adds a ternary field match for the given fieldId, value and mask. |
| 199 | * |
| 200 | * @param fieldId protocol-independent header field Id |
Carmelo Cascone | 32a01b2 | 2017-07-18 10:51:07 -0400 | [diff] [blame] | 201 | * @param value ternary match value |
| 202 | * @param mask ternary match mask |
Frank Wang | df38321 | 2017-06-23 09:17:41 +0800 | [diff] [blame] | 203 | * @return this |
| 204 | */ |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 205 | public Builder matchTernary(PiMatchFieldId fieldId, long value, long mask) { |
Carmelo Cascone | 32a01b2 | 2017-07-18 10:51:07 -0400 | [diff] [blame] | 206 | fieldMatchMapBuilder.put(fieldId, new PiTernaryFieldMatch(fieldId, copyFrom(value), copyFrom(mask))); |
Frank Wang | df38321 | 2017-06-23 09:17:41 +0800 | [diff] [blame] | 207 | return this; |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * Adds a ternary field match for the given fieldId, value and mask. |
| 212 | * |
| 213 | * @param fieldId protocol-independent header field Id |
Carmelo Cascone | 32a01b2 | 2017-07-18 10:51:07 -0400 | [diff] [blame] | 214 | * @param value ternary match value |
| 215 | * @param mask ternary match mask |
Frank Wang | df38321 | 2017-06-23 09:17:41 +0800 | [diff] [blame] | 216 | * @return this |
| 217 | */ |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 218 | public Builder matchTernary(PiMatchFieldId fieldId, byte[] value, byte[] mask) { |
Carmelo Cascone | 32a01b2 | 2017-07-18 10:51:07 -0400 | [diff] [blame] | 219 | fieldMatchMapBuilder.put(fieldId, new PiTernaryFieldMatch(fieldId, copyFrom(value), copyFrom(mask))); |
Frank Wang | df38321 | 2017-06-23 09:17:41 +0800 | [diff] [blame] | 220 | return this; |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Adds a longest-prefix field match for the given fieldId, value and prefix length. |
| 225 | * |
Carmelo Cascone | 32a01b2 | 2017-07-18 10:51:07 -0400 | [diff] [blame] | 226 | * @param fieldId protocol-independent header field Id |
| 227 | * @param value lpm match value |
Frank Wang | df38321 | 2017-06-23 09:17:41 +0800 | [diff] [blame] | 228 | * @param prefixLength lpm match prefix length |
| 229 | * @return this |
| 230 | */ |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 231 | public Builder matchLpm(PiMatchFieldId fieldId, short value, int prefixLength) { |
Carmelo Cascone | 32a01b2 | 2017-07-18 10:51:07 -0400 | [diff] [blame] | 232 | fieldMatchMapBuilder.put(fieldId, new PiLpmFieldMatch(fieldId, copyFrom(value), prefixLength)); |
Frank Wang | df38321 | 2017-06-23 09:17:41 +0800 | [diff] [blame] | 233 | return this; |
| 234 | } |
| 235 | |
| 236 | /** |
| 237 | * Adds a longest-prefix field match for the given fieldId, value and prefix length. |
| 238 | * |
Carmelo Cascone | 32a01b2 | 2017-07-18 10:51:07 -0400 | [diff] [blame] | 239 | * @param fieldId protocol-independent header field Id |
| 240 | * @param value lpm match value |
Frank Wang | df38321 | 2017-06-23 09:17:41 +0800 | [diff] [blame] | 241 | * @param prefixLength lpm match prefix length |
| 242 | * @return this |
| 243 | */ |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 244 | public Builder matchLpm(PiMatchFieldId fieldId, int value, int prefixLength) { |
Carmelo Cascone | 32a01b2 | 2017-07-18 10:51:07 -0400 | [diff] [blame] | 245 | fieldMatchMapBuilder.put(fieldId, new PiLpmFieldMatch(fieldId, copyFrom(value), prefixLength)); |
Frank Wang | df38321 | 2017-06-23 09:17:41 +0800 | [diff] [blame] | 246 | return this; |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | * Adds a longest-prefix field match for the given fieldId, value and prefix length. |
| 251 | * |
Carmelo Cascone | 32a01b2 | 2017-07-18 10:51:07 -0400 | [diff] [blame] | 252 | * @param fieldId protocol-independent header field Id |
| 253 | * @param value lpm match value |
Frank Wang | df38321 | 2017-06-23 09:17:41 +0800 | [diff] [blame] | 254 | * @param prefixLength lpm match prefix length |
| 255 | * @return this |
| 256 | */ |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 257 | public Builder matchLpm(PiMatchFieldId fieldId, long value, int prefixLength) { |
Carmelo Cascone | 32a01b2 | 2017-07-18 10:51:07 -0400 | [diff] [blame] | 258 | fieldMatchMapBuilder.put(fieldId, new PiLpmFieldMatch(fieldId, copyFrom(value), prefixLength)); |
Frank Wang | df38321 | 2017-06-23 09:17:41 +0800 | [diff] [blame] | 259 | return this; |
| 260 | } |
| 261 | |
| 262 | /** |
| 263 | * Adds a longest-prefix field match for the given fieldId, value and prefix length. |
| 264 | * |
Carmelo Cascone | 32a01b2 | 2017-07-18 10:51:07 -0400 | [diff] [blame] | 265 | * @param fieldId protocol-independent header field Id |
| 266 | * @param value lpm match value |
Frank Wang | df38321 | 2017-06-23 09:17:41 +0800 | [diff] [blame] | 267 | * @param prefixLength lpm match prefix length |
| 268 | * @return this |
| 269 | */ |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 270 | public Builder matchLpm(PiMatchFieldId fieldId, byte[] value, int prefixLength) { |
Carmelo Cascone | 32a01b2 | 2017-07-18 10:51:07 -0400 | [diff] [blame] | 271 | fieldMatchMapBuilder.put(fieldId, new PiLpmFieldMatch(fieldId, copyFrom(value), prefixLength)); |
Frank Wang | df38321 | 2017-06-23 09:17:41 +0800 | [diff] [blame] | 272 | return this; |
| 273 | } |
| 274 | |
| 275 | /** |
Frank Wang | df38321 | 2017-06-23 09:17:41 +0800 | [diff] [blame] | 276 | * Adds a range field match for the given fieldId, low and high. |
| 277 | * |
| 278 | * @param fieldId protocol-independent header field Id |
Carmelo Cascone | 32a01b2 | 2017-07-18 10:51:07 -0400 | [diff] [blame] | 279 | * @param low range match low value |
| 280 | * @param high range match high value |
Frank Wang | df38321 | 2017-06-23 09:17:41 +0800 | [diff] [blame] | 281 | * @return this |
| 282 | */ |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 283 | public Builder matchRange(PiMatchFieldId fieldId, short low, short high) { |
Carmelo Cascone | 32a01b2 | 2017-07-18 10:51:07 -0400 | [diff] [blame] | 284 | fieldMatchMapBuilder.put(fieldId, new PiRangeFieldMatch(fieldId, copyFrom(low), copyFrom(high))); |
Frank Wang | df38321 | 2017-06-23 09:17:41 +0800 | [diff] [blame] | 285 | return this; |
| 286 | } |
| 287 | |
| 288 | /** |
| 289 | * Adds a range field match for the given fieldId, low and high. |
| 290 | * |
| 291 | * @param fieldId protocol-independent header field Id |
Carmelo Cascone | 32a01b2 | 2017-07-18 10:51:07 -0400 | [diff] [blame] | 292 | * @param low range match low value |
| 293 | * @param high range match high value |
Frank Wang | df38321 | 2017-06-23 09:17:41 +0800 | [diff] [blame] | 294 | * @return this |
| 295 | */ |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 296 | public Builder matchRange(PiMatchFieldId fieldId, int low, int high) { |
Carmelo Cascone | 32a01b2 | 2017-07-18 10:51:07 -0400 | [diff] [blame] | 297 | fieldMatchMapBuilder.put(fieldId, new PiRangeFieldMatch(fieldId, copyFrom(low), copyFrom(high))); |
Frank Wang | df38321 | 2017-06-23 09:17:41 +0800 | [diff] [blame] | 298 | return this; |
| 299 | } |
Carmelo Cascone | 32a01b2 | 2017-07-18 10:51:07 -0400 | [diff] [blame] | 300 | |
Frank Wang | df38321 | 2017-06-23 09:17:41 +0800 | [diff] [blame] | 301 | /** |
| 302 | * Adds a range field match for the given fieldId, low and high. |
| 303 | * |
| 304 | * @param fieldId protocol-independent header field Id |
Carmelo Cascone | 32a01b2 | 2017-07-18 10:51:07 -0400 | [diff] [blame] | 305 | * @param low range match low value |
| 306 | * @param high range match high value |
Frank Wang | df38321 | 2017-06-23 09:17:41 +0800 | [diff] [blame] | 307 | * @return this |
| 308 | */ |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 309 | public Builder matchRange(PiMatchFieldId fieldId, long low, long high) { |
Carmelo Cascone | 32a01b2 | 2017-07-18 10:51:07 -0400 | [diff] [blame] | 310 | fieldMatchMapBuilder.put(fieldId, new PiRangeFieldMatch(fieldId, copyFrom(low), copyFrom(high))); |
Frank Wang | df38321 | 2017-06-23 09:17:41 +0800 | [diff] [blame] | 311 | return this; |
| 312 | } |
Carmelo Cascone | 32a01b2 | 2017-07-18 10:51:07 -0400 | [diff] [blame] | 313 | |
Frank Wang | df38321 | 2017-06-23 09:17:41 +0800 | [diff] [blame] | 314 | /** |
| 315 | * Adds a range field match for the given fieldId, low and high. |
| 316 | * |
| 317 | * @param fieldId protocol-independent header field Id |
Carmelo Cascone | 32a01b2 | 2017-07-18 10:51:07 -0400 | [diff] [blame] | 318 | * @param low range match low value |
| 319 | * @param high range match high value |
Frank Wang | df38321 | 2017-06-23 09:17:41 +0800 | [diff] [blame] | 320 | * @return this |
| 321 | */ |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 322 | public Builder matchRange(PiMatchFieldId fieldId, byte[] low, byte[] high) { |
Carmelo Cascone | 32a01b2 | 2017-07-18 10:51:07 -0400 | [diff] [blame] | 323 | fieldMatchMapBuilder.put(fieldId, new PiRangeFieldMatch(fieldId, copyFrom(low), copyFrom(high))); |
Frank Wang | df38321 | 2017-06-23 09:17:41 +0800 | [diff] [blame] | 324 | return this; |
| 325 | } |
| 326 | |
| 327 | /** |
| 328 | * Builds a PiCriterion. |
| 329 | * |
| 330 | * @return PiCriterion |
| 331 | */ |
Carmelo Cascone | 770507f | 2017-09-14 20:58:04 +0200 | [diff] [blame] | 332 | public PiCriterion build() { |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 333 | ImmutableMap<PiMatchFieldId, PiFieldMatch> fieldMatchMap = fieldMatchMapBuilder.build(); |
Carmelo Cascone | 32a01b2 | 2017-07-18 10:51:07 -0400 | [diff] [blame] | 334 | checkArgument(fieldMatchMap.size() > 0, "Cannot build PI criterion with 0 field matches"); |
| 335 | return new PiCriterion(fieldMatchMap); |
Frank Wang | df38321 | 2017-06-23 09:17:41 +0800 | [diff] [blame] | 336 | } |
| 337 | } |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 338 | } |