blob: 24ef9576f6ca178cf7c9e01d8dc15951da07003e [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;
Carmelo Cascone87892e22017-11-13 16:01:29 -080022import org.onosproject.net.pi.model.PiMatchFieldId;
Carmelo Cascone1022a4e2017-05-25 00:16:18 -040023import 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/**
Carmelo Cascone87892e22017-11-13 16:01:29 -080029 * Instance of a longest-prefix field match in a protocol-independent pipeline.
Carmelo Cascone1022a4e2017-05-25 00:16:18 -040030 */
31@Beta
32public final class PiLpmFieldMatch extends PiFieldMatch {
33
34 private final ImmutableByteSequence value;
35 private final int prefixLength;
36
37 /**
38 * Creates a new LPM field match.
39 *
40 * @param fieldId field identifier
41 * @param value value
42 * @param prefixLength prefix length
43 */
Carmelo Cascone87892e22017-11-13 16:01:29 -080044 public PiLpmFieldMatch(PiMatchFieldId fieldId, ImmutableByteSequence value, int prefixLength) {
Carmelo Cascone1022a4e2017-05-25 00:16:18 -040045 super(fieldId);
46 this.value = checkNotNull(value);
47 this.prefixLength = prefixLength;
48 checkArgument(value.size() > 0, "Value must have non-zero size");
49 checkArgument(prefixLength >= 0, "Prefix length must be a non-negative integer");
50 }
51
52 @Override
53 public PiMatchType type() {
54 return PiMatchType.LPM;
55 }
56
57 /**
58 * Returns the value matched by this field.
59 *
60 * @return a byte sequence value
61 */
62 public ImmutableByteSequence value() {
63 return value;
64 }
65
66 /**
67 * Returns the prefix length to be matched.
68 *
69 * @return an integer value
70 */
71 public int prefixLength() {
72 return prefixLength;
73 }
74
75 @Override
76 public boolean equals(Object o) {
77 if (this == o) {
78 return true;
79 }
80 if (o == null || getClass() != o.getClass()) {
81 return false;
82 }
83 PiLpmFieldMatch that = (PiLpmFieldMatch) o;
84 return prefixLength == that.prefixLength &&
85 Objects.equal(value, that.value) &&
86 Objects.equal(this.fieldId(), that.fieldId());
87 }
88
89 @Override
90 public int hashCode() {
91 return Objects.hashCode(this.fieldId(), value, prefixLength);
92 }
93
94 @Override
95 public String toString() {
96 return this.fieldId().toString() + '=' + value.toString()
97 + '/' + String.valueOf(prefixLength);
98 }
99}