blob: cdcfcd97e411379351b4506d90d4f717616cbb8e [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.collect.Lists;
21import org.onlab.util.ImmutableByteSequence;
22
23import java.util.Collections;
24import java.util.List;
25import java.util.Objects;
26
27import static com.google.common.base.Preconditions.checkArgument;
28import static com.google.common.base.Preconditions.checkNotNull;
29
30/**
31 * Bmv2 match key representation.
32 */
33public final class Bmv2MatchKey {
34
35 private final List<Bmv2MatchParam> matchParams;
36
37 private Bmv2MatchKey(List<Bmv2MatchParam> matchParams) {
38 // ban constructor
39 this.matchParams = matchParams;
40 }
41
42 public static Builder builder() {
43 return new Builder();
44 }
45
46 /**
47 * Returns an immutable view of the ordered list of match parameters of this
48 * match key.
49 *
50 * @return list match parameters
51 */
52 public final List<Bmv2MatchParam> matchParams() {
53 return Collections.unmodifiableList(matchParams);
54 }
55
56 @Override
57 public final int hashCode() {
58 return Objects.hashCode(matchParams);
59 }
60
61 @Override
62 public final boolean equals(Object obj) {
63 if (this == obj) {
64 return true;
65 }
66 if (obj == null || getClass() != obj.getClass()) {
67 return false;
68 }
69 final Bmv2MatchKey other = (Bmv2MatchKey) obj;
70
71 return Objects.equals(this.matchParams, other.matchParams);
72 }
73
74 @Override
75 public final String toString() {
76 return MoreObjects.toStringHelper(this)
77 .addValue(matchParams)
78 .toString();
79 }
80
81 /**
82 * Builder of a Bmv2 match key.
83 */
84 public static final class Builder {
85
86 private List<Bmv2MatchParam> matchParams;
87
88 private Builder() {
89 this.matchParams = Lists.newArrayList();
90 }
91
92 public Builder add(Bmv2MatchParam param) {
93 this.matchParams.add(checkNotNull(param));
94 return this;
95 }
96
97 /**
98 * Adds a ternary match parameter where all bits are don't-care.
99 *
100 * @param byteLength length in bytes of the parameter
101 * @return this
102 */
103 public Builder withWildcard(int byteLength) {
104 checkArgument(byteLength > 0, "length must be a positive integer");
105 return add(new Bmv2TernaryMatchParam(
106 ImmutableByteSequence.ofZeros(byteLength),
107 ImmutableByteSequence.ofZeros(byteLength)));
108 }
109
110 /**
111 * Builds a new match key object.
112 *
113 * @return match key
114 */
115 public Bmv2MatchKey build() {
116 return new Bmv2MatchKey(this.matchParams);
117 }
118 }
119}