Carmelo Cascone | aa8b629 | 2016-04-13 14:27:06 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-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.bmv2.api.runtime; |
| 18 | |
| 19 | import com.google.common.base.MoreObjects; |
| 20 | import com.google.common.base.Objects; |
| 21 | import org.onlab.util.ImmutableByteSequence; |
| 22 | |
| 23 | import static com.google.common.base.Preconditions.checkArgument; |
| 24 | import static com.google.common.base.Preconditions.checkNotNull; |
| 25 | |
| 26 | /** |
Carmelo Cascone | 17fc9e4 | 2016-05-31 11:29:21 -0700 | [diff] [blame] | 27 | * Representation of a BMv2 longest prefix match (LPM) parameter. |
Carmelo Cascone | aa8b629 | 2016-04-13 14:27:06 -0700 | [diff] [blame] | 28 | */ |
Carmelo Cascone | 17fc9e4 | 2016-05-31 11:29:21 -0700 | [diff] [blame] | 29 | public final class Bmv2LpmMatchParam implements Bmv2MatchParam { |
Carmelo Cascone | aa8b629 | 2016-04-13 14:27:06 -0700 | [diff] [blame] | 30 | |
| 31 | private final ImmutableByteSequence value; |
| 32 | private final int prefixLength; |
| 33 | |
| 34 | /** |
| 35 | * Creates a new LPM parameter using the given byte sequence value and |
| 36 | * prefix length. |
| 37 | * |
| 38 | * @param value a byte sequence value |
| 39 | * @param prefixLength an integer value |
| 40 | */ |
| 41 | public Bmv2LpmMatchParam(ImmutableByteSequence value, int prefixLength) { |
| 42 | checkArgument(prefixLength >= 0, "prefix length cannot be negative"); |
| 43 | this.value = checkNotNull(value); |
| 44 | this.prefixLength = prefixLength; |
| 45 | } |
| 46 | |
| 47 | @Override |
| 48 | public Bmv2MatchParam.Type type() { |
| 49 | return Type.LPM; |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Returns the byte sequence value of this parameter. |
| 54 | * |
| 55 | * @return a byte sequence value |
| 56 | */ |
| 57 | public ImmutableByteSequence value() { |
| 58 | return this.value; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Returns the prefix length of this parameter. |
| 63 | * |
| 64 | * @return an integer value |
| 65 | */ |
| 66 | public int prefixLength() { |
| 67 | return this.prefixLength; |
| 68 | } |
| 69 | |
| 70 | @Override |
| 71 | public int hashCode() { |
| 72 | return Objects.hashCode(value, prefixLength); |
| 73 | } |
| 74 | |
| 75 | @Override |
| 76 | public boolean equals(Object obj) { |
| 77 | if (this == obj) { |
| 78 | return true; |
| 79 | } |
| 80 | if (obj == null || getClass() != obj.getClass()) { |
| 81 | return false; |
| 82 | } |
| 83 | final Bmv2LpmMatchParam other = (Bmv2LpmMatchParam) obj; |
| 84 | return Objects.equal(this.value, other.value) |
| 85 | && Objects.equal(this.prefixLength, other.prefixLength); |
| 86 | } |
| 87 | |
| 88 | @Override |
| 89 | public String toString() { |
| 90 | return MoreObjects.toStringHelper(this) |
| 91 | .add("value", value) |
| 92 | .add("prefixLength", prefixLength) |
| 93 | .toString(); |
| 94 | } |
| 95 | } |