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