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