Carmelo Cascone | 0e896a0 | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Carmelo Cascone | 0e896a0 | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 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.net.pi.runtime; |
| 18 | |
| 19 | import com.google.common.annotations.Beta; |
| 20 | import com.google.common.base.Objects; |
| 21 | import com.google.common.collect.ImmutableMap; |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 22 | import org.onosproject.net.pi.model.PiMatchFieldId; |
Carmelo Cascone | 0e896a0 | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 23 | |
| 24 | import java.util.Collection; |
| 25 | import java.util.Optional; |
| 26 | import java.util.StringJoiner; |
| 27 | |
Carmelo Cascone | 0e896a0 | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 28 | /** |
| 29 | * Representation of all field matches of an entry of a match+action table of a protocol-independent pipeline. |
| 30 | */ |
| 31 | @Beta |
| 32 | public final class PiMatchKey { |
| 33 | |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 34 | public static final PiMatchKey EMPTY = builder().build(); |
| 35 | |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 36 | private final ImmutableMap<PiMatchFieldId, PiFieldMatch> fieldMatches; |
Carmelo Cascone | 0e896a0 | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 37 | |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 38 | private PiMatchKey(ImmutableMap<PiMatchFieldId, PiFieldMatch> fieldMatches) { |
Carmelo Cascone | 0e896a0 | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 39 | this.fieldMatches = fieldMatches; |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Returns the collection of field matches of this match key. |
| 44 | * |
| 45 | * @return collection of field matches |
| 46 | */ |
| 47 | public Collection<PiFieldMatch> fieldMatches() { |
| 48 | return fieldMatches.values(); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * If present, returns the field match associated with the given header field identifier. |
| 53 | * |
| 54 | * @param fieldId field identifier |
| 55 | * @return optional field match |
| 56 | */ |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 57 | public Optional<PiFieldMatch> fieldMatch(PiMatchFieldId fieldId) { |
Carmelo Cascone | 0e896a0 | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 58 | return Optional.ofNullable(fieldMatches.get(fieldId)); |
| 59 | } |
| 60 | |
| 61 | @Override |
| 62 | public boolean equals(Object o) { |
| 63 | if (this == o) { |
| 64 | return true; |
| 65 | } |
| 66 | if (!(o instanceof PiMatchKey)) { |
| 67 | return false; |
| 68 | } |
| 69 | PiMatchKey that = (PiMatchKey) o; |
| 70 | return Objects.equal(fieldMatches, that.fieldMatches); |
| 71 | } |
| 72 | |
| 73 | @Override |
| 74 | public int hashCode() { |
| 75 | return Objects.hashCode(fieldMatches); |
| 76 | } |
| 77 | |
| 78 | @Override |
| 79 | public String toString() { |
| 80 | StringJoiner stringFieldMatches = new StringJoiner(", ", "{", "}"); |
| 81 | this.fieldMatches.values().forEach(f -> stringFieldMatches.add(f.toString())); |
| 82 | return stringFieldMatches.toString(); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Returns a new builder of match keys. |
| 87 | * |
| 88 | * @return match key builder |
| 89 | */ |
| 90 | public static Builder builder() { |
| 91 | return new Builder(); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Builder of match keys. |
| 96 | */ |
| 97 | public static final class Builder { |
| 98 | |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 99 | private final ImmutableMap.Builder<PiMatchFieldId, PiFieldMatch> fieldMatchesBuilder = ImmutableMap.builder(); |
Carmelo Cascone | 0e896a0 | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 100 | |
| 101 | private Builder() { |
| 102 | // hides constructor. |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Adds one field match to this match key. |
| 107 | * |
| 108 | * @param fieldMatch field match |
| 109 | * @return this |
| 110 | */ |
| 111 | public Builder addFieldMatch(PiFieldMatch fieldMatch) { |
| 112 | this.fieldMatchesBuilder.put(fieldMatch.fieldId(), fieldMatch); |
| 113 | return this; |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Adds many field matches to this match key. |
| 118 | * |
| 119 | * @param fieldMatches collection of field matches |
| 120 | * @return this |
| 121 | */ |
| 122 | public Builder addFieldMatches(Collection<PiFieldMatch> fieldMatches) { |
| 123 | fieldMatches.forEach(this::addFieldMatch); |
| 124 | return this; |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Creates a new match key. |
| 129 | * |
| 130 | * @return match key |
| 131 | */ |
| 132 | public PiMatchKey build() { |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 133 | ImmutableMap<PiMatchFieldId, PiFieldMatch> fieldMatches = fieldMatchesBuilder.build(); |
Carmelo Cascone | 0e896a0 | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 134 | return new PiMatchKey(fieldMatches); |
| 135 | } |
| 136 | } |
| 137 | } |