blob: cb34981e93a1fc78fe635efdb0a8979dc4ee2801 [file] [log] [blame]
Daniele Moroc6f2f7f2020-12-18 10:55:57 +01001/*
2 * Copyright 2020-present Open Networking Foundation
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.net.pi.runtime;
18
19import com.google.common.annotations.Beta;
20import com.google.common.base.Objects;
21import org.onlab.util.ImmutableByteSequence;
22import org.onosproject.net.pi.model.PiMatchFieldId;
23import org.onosproject.net.pi.model.PiMatchType;
24
25import static com.google.common.base.Preconditions.checkArgument;
26import static com.google.common.base.Preconditions.checkNotNull;
27
28/**
29 * Instance of an optional field match in a protocol-independent pipeline.
30 * The optional match field behaves like an exact match if specified, otherwise
31 * it is logically equivalent to a wildcard match.
32 */
33@Beta
34public final class PiOptionalFieldMatch extends PiFieldMatch {
35
36 private final ImmutableByteSequence value;
37
38 /**
39 * Creates an optional field match.
40 *
41 * @param fieldId field identifier
42 * @param value value
43 */
44 public PiOptionalFieldMatch(PiMatchFieldId fieldId, ImmutableByteSequence value) {
45 super(fieldId);
46 this.value = checkNotNull(value);
47 checkArgument(value.size() > 0, "Value can't have size 0");
48 }
49
50 @Override
51 public PiMatchType type() {
52 return PiMatchType.OPTIONAL;
53 }
54
55 /**
56 * Returns the byte sequence value to be matched.
57 *
58 * @return an immutable byte sequence
59 */
60 public ImmutableByteSequence value() {
61 return value;
62 }
63
64 @Override
65 public boolean equals(Object o) {
66 if (this == o) {
67 return true;
68 }
69 if (o == null || getClass() != o.getClass()) {
70 return false;
71 }
72 PiOptionalFieldMatch that = (PiOptionalFieldMatch) o;
73 return Objects.equal(this.fieldId(), that.fieldId()) &&
74 Objects.equal(value, that.value);
75 }
76
77 @Override
78 public int hashCode() {
79 return Objects.hashCode(this.fieldId(), value);
80 }
81
82 @Override
83 public String toString() {
84 return this.fieldId().toString() + "=" + value.toString();
85 }
86}