blob: a978bfa6dd9dfae22206982f1a49d444c93235ab [file] [log] [blame]
Carmelo Cascone0e896a02017-07-31 07:22:27 +02001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Carmelo Cascone0e896a02017-07-31 07:22:27 +02003 *
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.net.pi.runtime;
18
19import com.google.common.annotations.Beta;
20import com.google.common.base.Objects;
21import com.google.common.collect.ImmutableMap;
Carmelo Cascone87892e22017-11-13 16:01:29 -080022import org.onosproject.net.pi.model.PiMatchFieldId;
Carmelo Cascone0e896a02017-07-31 07:22:27 +020023
24import java.util.Collection;
25import java.util.Optional;
26import java.util.StringJoiner;
27
Carmelo Cascone0e896a02017-07-31 07:22:27 +020028/**
29 * Representation of all field matches of an entry of a match+action table of a protocol-independent pipeline.
30 */
31@Beta
32public final class PiMatchKey {
33
Carmelo Cascone7f75be42017-09-07 14:37:02 +020034 public static final PiMatchKey EMPTY = builder().build();
35
Carmelo Cascone87892e22017-11-13 16:01:29 -080036 private final ImmutableMap<PiMatchFieldId, PiFieldMatch> fieldMatches;
Carmelo Cascone0e896a02017-07-31 07:22:27 +020037
Carmelo Cascone87892e22017-11-13 16:01:29 -080038 private PiMatchKey(ImmutableMap<PiMatchFieldId, PiFieldMatch> fieldMatches) {
Carmelo Cascone0e896a02017-07-31 07:22:27 +020039 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 Cascone87892e22017-11-13 16:01:29 -080057 public Optional<PiFieldMatch> fieldMatch(PiMatchFieldId fieldId) {
Carmelo Cascone0e896a02017-07-31 07:22:27 +020058 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 Cascone87892e22017-11-13 16:01:29 -080099 private final ImmutableMap.Builder<PiMatchFieldId, PiFieldMatch> fieldMatchesBuilder = ImmutableMap.builder();
Carmelo Cascone0e896a02017-07-31 07:22:27 +0200100
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 Cascone87892e22017-11-13 16:01:29 -0800133 ImmutableMap<PiMatchFieldId, PiFieldMatch> fieldMatches = fieldMatchesBuilder.build();
Carmelo Cascone0e896a02017-07-31 07:22:27 +0200134 return new PiMatchKey(fieldMatches);
135 }
136 }
137}