blob: 34276aa869f78284fb703990513bb47d23c37d9f [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.collect.Lists;
22import org.onlab.util.ImmutableByteSequence;
23
24import java.util.Collections;
25import java.util.List;
26import java.util.Objects;
27
28import static com.google.common.base.Preconditions.checkArgument;
29import static com.google.common.base.Preconditions.checkNotNull;
30
31/**
Carmelo Cascone17fc9e42016-05-31 11:29:21 -070032 * A match key of a BMv2 match-action table entry.
Carmelo Casconeaa8b6292016-04-13 14:27:06 -070033 */
Carmelo Cascone9e39e312016-06-16 14:47:09 -070034@Beta
Carmelo Casconeaa8b6292016-04-13 14:27:06 -070035public final class Bmv2MatchKey {
36
37 private final List<Bmv2MatchParam> matchParams;
38
39 private Bmv2MatchKey(List<Bmv2MatchParam> matchParams) {
40 // ban constructor
41 this.matchParams = matchParams;
42 }
43
Carmelo Cascone17fc9e42016-05-31 11:29:21 -070044 /**
45 * Returns a new match key builder.
46 *
47 * @return a match key builder
48 */
Carmelo Casconeaa8b6292016-04-13 14:27:06 -070049 public static Builder builder() {
50 return new Builder();
51 }
52
53 /**
Carmelo Cascone17fc9e42016-05-31 11:29:21 -070054 * Returns the list of match parameters of this match key.
Carmelo Casconeaa8b6292016-04-13 14:27:06 -070055 *
56 * @return list match parameters
57 */
58 public final List<Bmv2MatchParam> matchParams() {
59 return Collections.unmodifiableList(matchParams);
60 }
61
62 @Override
63 public final int hashCode() {
64 return Objects.hashCode(matchParams);
65 }
66
67 @Override
68 public final boolean equals(Object obj) {
69 if (this == obj) {
70 return true;
71 }
72 if (obj == null || getClass() != obj.getClass()) {
73 return false;
74 }
75 final Bmv2MatchKey other = (Bmv2MatchKey) obj;
76
77 return Objects.equals(this.matchParams, other.matchParams);
78 }
79
80 @Override
81 public final String toString() {
82 return MoreObjects.toStringHelper(this)
83 .addValue(matchParams)
84 .toString();
85 }
86
87 /**
Carmelo Cascone17fc9e42016-05-31 11:29:21 -070088 * Builder of a BMv2 match key.
Carmelo Casconeaa8b6292016-04-13 14:27:06 -070089 */
90 public static final class Builder {
91
92 private List<Bmv2MatchParam> matchParams;
93
94 private Builder() {
95 this.matchParams = Lists.newArrayList();
96 }
97
Carmelo Cascone17fc9e42016-05-31 11:29:21 -070098 /**
99 * Adds a match parameter to the match key.
100 *
101 * @param param a match parameter
102 * @return this
103 */
Carmelo Casconeaa8b6292016-04-13 14:27:06 -0700104 public Builder add(Bmv2MatchParam param) {
105 this.matchParams.add(checkNotNull(param));
106 return this;
107 }
108
109 /**
110 * Adds a ternary match parameter where all bits are don't-care.
111 *
112 * @param byteLength length in bytes of the parameter
113 * @return this
114 */
115 public Builder withWildcard(int byteLength) {
116 checkArgument(byteLength > 0, "length must be a positive integer");
117 return add(new Bmv2TernaryMatchParam(
118 ImmutableByteSequence.ofZeros(byteLength),
119 ImmutableByteSequence.ofZeros(byteLength)));
120 }
121
122 /**
123 * Builds a new match key object.
124 *
125 * @return match key
126 */
127 public Bmv2MatchKey build() {
128 return new Bmv2MatchKey(this.matchParams);
129 }
130 }
131}