blob: 87b87233615eceb18ba414885951bd8f4496c365 [file] [log] [blame]
Carmelo Cascone1022a4e2017-05-25 00:16:18 -04001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Carmelo Cascone1022a4e2017-05-25 00:16:18 -04003 *
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.PiMatchType;
23
24import static com.google.common.base.Preconditions.checkArgument;
25import static com.google.common.base.Preconditions.checkNotNull;
26
27/**
28 * Longest-prefix field match in a protocol-independent pipeline.
29 */
30@Beta
31public final class PiLpmFieldMatch extends PiFieldMatch {
32
33 private final ImmutableByteSequence value;
34 private final int prefixLength;
35
36 /**
37 * Creates a new LPM field match.
38 *
39 * @param fieldId field identifier
40 * @param value value
41 * @param prefixLength prefix length
42 */
43 public PiLpmFieldMatch(PiHeaderFieldId fieldId, ImmutableByteSequence value, int prefixLength) {
44 super(fieldId);
45 this.value = checkNotNull(value);
46 this.prefixLength = prefixLength;
47 checkArgument(value.size() > 0, "Value must have non-zero size");
48 checkArgument(prefixLength >= 0, "Prefix length must be a non-negative integer");
49 }
50
51 @Override
52 public PiMatchType type() {
53 return PiMatchType.LPM;
54 }
55
56 /**
57 * Returns the value matched by this field.
58 *
59 * @return a byte sequence value
60 */
61 public ImmutableByteSequence value() {
62 return value;
63 }
64
65 /**
66 * Returns the prefix length to be matched.
67 *
68 * @return an integer value
69 */
70 public int prefixLength() {
71 return prefixLength;
72 }
73
74 @Override
75 public boolean equals(Object o) {
76 if (this == o) {
77 return true;
78 }
79 if (o == null || getClass() != o.getClass()) {
80 return false;
81 }
82 PiLpmFieldMatch that = (PiLpmFieldMatch) o;
83 return prefixLength == that.prefixLength &&
84 Objects.equal(value, that.value) &&
85 Objects.equal(this.fieldId(), that.fieldId());
86 }
87
88 @Override
89 public int hashCode() {
90 return Objects.hashCode(this.fieldId(), value, prefixLength);
91 }
92
93 @Override
94 public String toString() {
95 return this.fieldId().toString() + '=' + value.toString()
96 + '/' + String.valueOf(prefixLength);
97 }
98}