blob: 713d5325b7b3e6d467796e20349dd87898d672e2 [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
Carmelo Cascone9e39e312016-06-16 14:47:09 -070019import com.google.common.annotations.Beta;
Carmelo Casconeaa8b6292016-04-13 14:27:06 -070020import com.google.common.base.MoreObjects;
21import com.google.common.base.Objects;
22import org.onlab.util.ImmutableByteSequence;
23
24import static com.google.common.base.Preconditions.checkNotNull;
25import static com.google.common.base.Preconditions.checkState;
26
27/**
Carmelo Cascone17fc9e42016-05-31 11:29:21 -070028 * Representation of a BMv2 ternary match parameter.
Carmelo Casconeaa8b6292016-04-13 14:27:06 -070029 */
Carmelo Cascone9e39e312016-06-16 14:47:09 -070030@Beta
Carmelo Cascone17fc9e42016-05-31 11:29:21 -070031public final class Bmv2TernaryMatchParam implements Bmv2MatchParam {
Carmelo Casconeaa8b6292016-04-13 14:27:06 -070032
33 private final ImmutableByteSequence value;
34 private final ImmutableByteSequence mask;
35
36 /**
37 * Creates a new ternary match parameter using the given byte sequences of
38 * value and mask.
39 *
40 * @param value a byte sequence value
41 * @param mask a byte sequence value
42 */
43 public Bmv2TernaryMatchParam(ImmutableByteSequence value,
44 ImmutableByteSequence mask) {
45 this.value = checkNotNull(value, "value cannot be null");
46 this.mask = checkNotNull(mask, "value cannot be null");
47 checkState(value.size() == mask.size(),
48 "value and mask must have equal size");
49 }
50
51 @Override
52 public Type type() {
53 return Type.TERNARY;
54 }
55
56 /**
57 * Returns the byte sequence value of by this parameter.
58 *
59 * @return a byte sequence value
60 */
61 public ImmutableByteSequence value() {
62 return this.value;
63 }
64
65 /**
66 * Returns the byte sequence mask of by this parameter.
67 *
68 * @return a byte sequence value
69 */
70 public ImmutableByteSequence mask() {
71 return this.mask;
72 }
73
74 @Override
75 public int hashCode() {
76 return Objects.hashCode(value, mask);
77 }
78
79 @Override
80 public boolean equals(Object obj) {
81 if (this == obj) {
82 return true;
83 }
84 if (obj == null || getClass() != obj.getClass()) {
85 return false;
86 }
87 final Bmv2TernaryMatchParam other = (Bmv2TernaryMatchParam) obj;
88 return Objects.equal(this.value, other.value)
89 && Objects.equal(this.mask, other.mask);
90 }
91
92 @Override
93 public String toString() {
94 return MoreObjects.toStringHelper(this)
95 .add("value", value)
96 .add("mask", mask)
97 .toString();
98 }
99}