blob: b6c39aa9a391c8498ddf44759c28a65804ed668c [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 * Range field match in a protocol-independent pipeline.
29 */
30@Beta
31public final class PiRangeFieldMatch extends PiFieldMatch {
32
33 private final ImmutableByteSequence lowValue;
34 private final ImmutableByteSequence highValue;
35
36 /**
37 * Creates a new range field match for the given low and high value.
38 *
39 * @param fieldId field identifier
40 * @param lowValue low value
41 * @param highValue high value
42 */
43 public PiRangeFieldMatch(PiHeaderFieldId fieldId, ImmutableByteSequence lowValue,
44 ImmutableByteSequence highValue) {
45 super(fieldId);
46 this.lowValue = checkNotNull(lowValue);
47 this.highValue = checkNotNull(highValue);
48 checkArgument(lowValue.size() == highValue.size() && lowValue.size() > 0,
49 "Low and high values must have the same non-zero size.");
50 }
51
52 @Override
53 public PiMatchType type() {
54 return PiMatchType.RANGE;
55 }
56
57 /**
58 * Returns the low value of this range field match.
59 *
60 * @return low value
61 */
62 public ImmutableByteSequence lowValue() {
63 return lowValue;
64 }
65
66 /**
67 * Returns the high value of this range field match.
68 *
69 * @return high value
70 */
71 public ImmutableByteSequence highValue() {
72 return highValue;
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 PiRangeFieldMatch that = (PiRangeFieldMatch) o;
84 return Objects.equal(this.fieldId(), that.fieldId()) &&
85 Objects.equal(lowValue, that.lowValue) &&
86 Objects.equal(highValue, that.highValue);
87 }
88
89 @Override
90 public int hashCode() {
91 return Objects.hashCode(this.fieldId(), lowValue, highValue);
92 }
93
94 @Override
95 public String toString() {
96 return this.fieldId().toString() + '=' + lowValue.toString() + "--" + highValue.toString();
97 }
98}