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.pi.runtime; |
| 18 | |
| 19 | import com.google.common.annotations.Beta; |
| 20 | import com.google.common.base.Objects; |
| 21 | import org.onlab.util.ImmutableByteSequence; |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 22 | import org.onosproject.net.pi.model.PiMatchFieldId; |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 23 | import org.onosproject.net.pi.model.PiMatchType; |
| 24 | |
| 25 | import static com.google.common.base.Preconditions.checkArgument; |
| 26 | import static com.google.common.base.Preconditions.checkNotNull; |
| 27 | |
| 28 | /** |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 29 | * Instance of a ternary field match in a protocol-independent pipeline. |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 30 | */ |
| 31 | @Beta |
| 32 | public final class PiTernaryFieldMatch extends PiFieldMatch { |
| 33 | |
| 34 | private final ImmutableByteSequence value; |
| 35 | private final ImmutableByteSequence mask; |
| 36 | |
| 37 | /** |
| 38 | * Creates a new ternary field match. |
| 39 | * |
| 40 | * @param fieldId field identifier |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 41 | * @param value value |
| 42 | * @param mask mask |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 43 | */ |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 44 | public PiTernaryFieldMatch(PiMatchFieldId fieldId, ImmutableByteSequence value, |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 45 | ImmutableByteSequence mask) { |
| 46 | super(fieldId); |
| 47 | this.value = checkNotNull(value); |
| 48 | this.mask = checkNotNull(mask); |
| 49 | checkArgument(value.size() == mask.size() && value.size() > 0, |
| 50 | "Value and mask must have same non-zero size"); |
| 51 | } |
| 52 | |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 53 | /** |
| 54 | * Returns the value matched by this field. |
| 55 | * |
| 56 | * @return an immutable byte sequence |
| 57 | */ |
| 58 | public ImmutableByteSequence value() { |
| 59 | return value; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Returns the mask used to match this field. |
| 64 | * |
| 65 | * @return an immutable byte sequence |
| 66 | */ |
| 67 | public ImmutableByteSequence mask() { |
| 68 | return mask; |
| 69 | } |
| 70 | |
| 71 | @Override |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 72 | public PiMatchType type() { |
| 73 | return PiMatchType.TERNARY; |
| 74 | } |
| 75 | |
| 76 | @Override |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 77 | public boolean equals(Object o) { |
| 78 | if (this == o) { |
| 79 | return true; |
| 80 | } |
| 81 | if (o == null || getClass() != o.getClass()) { |
| 82 | return false; |
| 83 | } |
| 84 | PiTernaryFieldMatch that = (PiTernaryFieldMatch) o; |
| 85 | return Objects.equal(this.fieldId(), that.fieldId()) && |
| 86 | Objects.equal(value, that.value) && |
| 87 | Objects.equal(mask, that.mask); |
| 88 | } |
| 89 | |
| 90 | @Override |
| 91 | public int hashCode() { |
| 92 | return Objects.hashCode(this.fieldId(), value, mask); |
| 93 | } |
| 94 | |
| 95 | @Override |
| 96 | public String toString() { |
| 97 | return this.fieldId().toString() + '=' + value.toString() + "&&&" + mask.toString(); |
| 98 | } |
| 99 | } |