Carmelo Cascone | 2ea177b | 2016-02-25 18:38:42 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014-2016 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 | |
| 17 | package org.onosproject.bmv2.api; |
| 18 | |
| 19 | import com.google.common.base.MoreObjects; |
| 20 | import com.google.common.collect.Lists; |
| 21 | import org.p4.bmv2.thrift.BmMatchParam; |
| 22 | import org.p4.bmv2.thrift.BmMatchParamExact; |
| 23 | import org.p4.bmv2.thrift.BmMatchParamLPM; |
| 24 | import org.p4.bmv2.thrift.BmMatchParamTernary; |
| 25 | import org.p4.bmv2.thrift.BmMatchParamType; |
| 26 | import org.p4.bmv2.thrift.BmMatchParamValid; |
| 27 | |
| 28 | import java.nio.ByteBuffer; |
| 29 | import java.util.Arrays; |
| 30 | import java.util.Collections; |
| 31 | import java.util.List; |
| 32 | import java.util.Objects; |
| 33 | |
| 34 | /** |
| 35 | * Bmv2 match key representation. |
| 36 | */ |
| 37 | public final class Bmv2MatchKey { |
| 38 | |
| 39 | private final List<BmMatchParam> matchParams; |
| 40 | |
| 41 | /** |
| 42 | * Creates a new match key. |
| 43 | * |
| 44 | * @param matchParams The ordered list of match parameters |
| 45 | */ |
| 46 | private Bmv2MatchKey(List<BmMatchParam> matchParams) { |
| 47 | this.matchParams = matchParams; |
| 48 | } |
| 49 | |
| 50 | public static Builder builder() { |
| 51 | return new Builder(); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Returns the match parameters defined for this match key (read-only). |
| 56 | * |
| 57 | * @return match parameters |
| 58 | */ |
| 59 | public final List<BmMatchParam> bmMatchParams() { |
| 60 | return Collections.unmodifiableList(matchParams); |
| 61 | } |
| 62 | |
| 63 | @Override |
| 64 | public final int hashCode() { |
| 65 | return Objects.hashCode(matchParams); |
| 66 | } |
| 67 | |
| 68 | @Override |
| 69 | public final boolean equals(Object obj) { |
| 70 | if (this == obj) { |
| 71 | return true; |
| 72 | } |
| 73 | if (obj == null || getClass() != obj.getClass()) { |
| 74 | return false; |
| 75 | } |
| 76 | final Bmv2MatchKey other = (Bmv2MatchKey) obj; |
| 77 | |
| 78 | return Objects.equals(this.matchParams, other.matchParams); |
| 79 | } |
| 80 | |
| 81 | @Override |
| 82 | public final String toString() { |
| 83 | return MoreObjects.toStringHelper(this) |
| 84 | .addValue(matchParams) |
| 85 | .toString(); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Builder of a Bmv2 match key. |
| 90 | */ |
| 91 | public static final class Builder { |
| 92 | |
| 93 | private List<BmMatchParam> matchParams; |
| 94 | |
| 95 | private Builder() { |
| 96 | this.matchParams = Lists.newArrayList(); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Adds an exact match parameter. |
| 101 | * |
| 102 | * @param key a ByteBuffer value |
| 103 | * @return this |
| 104 | */ |
| 105 | public Builder withExact(ByteBuffer key) { |
| 106 | this.matchParams.add( |
| 107 | new BmMatchParam(BmMatchParamType.EXACT) |
| 108 | .setExact(new BmMatchParamExact(key))); |
| 109 | return this; |
| 110 | } |
| 111 | |
| 112 | |
| 113 | /** |
| 114 | * Adds a longest prefix match parameter. |
| 115 | * |
| 116 | * @param key a ByteBuffer value |
| 117 | * @param prefixLength an integer value |
| 118 | * @return this |
| 119 | */ |
| 120 | public Builder withLpm(ByteBuffer key, int prefixLength) { |
| 121 | this.matchParams.add( |
| 122 | new BmMatchParam(BmMatchParamType.LPM) |
| 123 | .setLpm(new BmMatchParamLPM(key, prefixLength))); |
| 124 | return this; |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Adds a ternary match parameter. |
| 129 | * |
| 130 | * @param key a ByteBuffer value |
| 131 | * @param mask an ByteBuffer value |
| 132 | * @return this |
| 133 | */ |
| 134 | public Builder withTernary(ByteBuffer key, ByteBuffer mask) { |
| 135 | this.matchParams.add( |
| 136 | new BmMatchParam(BmMatchParamType.TERNARY). |
| 137 | setTernary(new BmMatchParamTernary(key, mask))); |
| 138 | return this; |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Adds a ternary match parameter where all bits are don't-care. |
| 143 | * |
| 144 | * @param byteLength an integer value representing the length in byte of the parameter |
| 145 | * @return this |
| 146 | */ |
| 147 | public Builder withWildcard(int byteLength) { |
| 148 | byte[] zeros = new byte[byteLength]; |
| 149 | Arrays.fill(zeros, (byte) 0); |
| 150 | return this.withTernary(ByteBuffer.wrap(zeros), ByteBuffer.wrap(zeros)); |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Adds a valid match parameter. |
| 155 | * |
| 156 | * @param key a boolean value |
| 157 | * @return this |
| 158 | */ |
| 159 | public Builder withValid(boolean key) { |
| 160 | this.matchParams.add( |
| 161 | new BmMatchParam(BmMatchParamType.VALID) |
| 162 | .setValid(new BmMatchParamValid(key))); |
| 163 | return this; |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Builds a new match key object. |
| 168 | * |
| 169 | * @return match key |
| 170 | */ |
| 171 | public Bmv2MatchKey build() { |
| 172 | return new Bmv2MatchKey(this.matchParams); |
| 173 | } |
| 174 | } |
| 175 | } |