blob: e2e8c3de10c8698847f05729841bf8f0c46c51b7 [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;
22
23import java.util.Collection;
24import java.util.Optional;
25import java.util.StringJoiner;
26
Carmelo Cascone0e896a02017-07-31 07:22:27 +020027/**
28 * Representation of all field matches of an entry of a match+action table of a protocol-independent pipeline.
29 */
30@Beta
31public final class PiMatchKey {
32
Carmelo Cascone7f75be42017-09-07 14:37:02 +020033 public static final PiMatchKey EMPTY = builder().build();
34
Carmelo Cascone0e896a02017-07-31 07:22:27 +020035 private final ImmutableMap<PiHeaderFieldId, PiFieldMatch> fieldMatches;
36
37 private PiMatchKey(ImmutableMap<PiHeaderFieldId, PiFieldMatch> fieldMatches) {
38 this.fieldMatches = fieldMatches;
39 }
40
41 /**
42 * Returns the collection of field matches of this match key.
43 *
44 * @return collection of field matches
45 */
46 public Collection<PiFieldMatch> fieldMatches() {
47 return fieldMatches.values();
48 }
49
50 /**
51 * If present, returns the field match associated with the given header field identifier.
52 *
53 * @param fieldId field identifier
54 * @return optional field match
55 */
56 public Optional<PiFieldMatch> fieldMatch(PiHeaderFieldId fieldId) {
57 return Optional.ofNullable(fieldMatches.get(fieldId));
58 }
59
60 @Override
61 public boolean equals(Object o) {
62 if (this == o) {
63 return true;
64 }
65 if (!(o instanceof PiMatchKey)) {
66 return false;
67 }
68 PiMatchKey that = (PiMatchKey) o;
69 return Objects.equal(fieldMatches, that.fieldMatches);
70 }
71
72 @Override
73 public int hashCode() {
74 return Objects.hashCode(fieldMatches);
75 }
76
77 @Override
78 public String toString() {
79 StringJoiner stringFieldMatches = new StringJoiner(", ", "{", "}");
80 this.fieldMatches.values().forEach(f -> stringFieldMatches.add(f.toString()));
81 return stringFieldMatches.toString();
82 }
83
84 /**
85 * Returns a new builder of match keys.
86 *
87 * @return match key builder
88 */
89 public static Builder builder() {
90 return new Builder();
91 }
92
93 /**
94 * Builder of match keys.
95 */
96 public static final class Builder {
97
98 private final ImmutableMap.Builder<PiHeaderFieldId, PiFieldMatch> fieldMatchesBuilder = ImmutableMap.builder();
99
100 private Builder() {
101 // hides constructor.
102 }
103
104 /**
105 * Adds one field match to this match key.
106 *
107 * @param fieldMatch field match
108 * @return this
109 */
110 public Builder addFieldMatch(PiFieldMatch fieldMatch) {
111 this.fieldMatchesBuilder.put(fieldMatch.fieldId(), fieldMatch);
112 return this;
113 }
114
115 /**
116 * Adds many field matches to this match key.
117 *
118 * @param fieldMatches collection of field matches
119 * @return this
120 */
121 public Builder addFieldMatches(Collection<PiFieldMatch> fieldMatches) {
122 fieldMatches.forEach(this::addFieldMatch);
123 return this;
124 }
125
126 /**
127 * Creates a new match key.
128 *
129 * @return match key
130 */
131 public PiMatchKey build() {
132 ImmutableMap<PiHeaderFieldId, PiFieldMatch> fieldMatches = fieldMatchesBuilder.build();
Carmelo Cascone0e896a02017-07-31 07:22:27 +0200133 return new PiMatchKey(fieldMatches);
134 }
135 }
136}