blob: 29d527968e50600d5282a113e9bd8e482e64e306 [file] [log] [blame]
Carmelo Casconeaa8b6292016-04-13 14:27:06 -07001/*
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
17package org.onosproject.bmv2.api.runtime;
18
19import com.google.common.base.MoreObjects;
20import com.google.common.base.Objects;
21import org.onlab.util.ImmutableByteSequence;
22
23import static com.google.common.base.Preconditions.checkArgument;
24import static com.google.common.base.Preconditions.checkNotNull;
25
26/**
27 * Representation of a Bmv2 longest prefix match (LPM) parameter.
28 */
29public class Bmv2LpmMatchParam implements Bmv2MatchParam {
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}