blob: 18e5bf831178fdc067eeb2d8ac8b800c0211ad6f [file] [log] [blame]
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -07003 *
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 */
16package org.onosproject.net.flow.criteria;
17
18import java.util.Objects;
19
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -070020/**
21 * Implementation of Internet Protocol Number criterion (8 bits unsigned)
22 * integer.
23 */
24public final class IPProtocolCriterion implements Criterion {
25 private static final short MASK = 0xff;
26 private final short proto; // IP protocol number: 8 bits
27
28 /**
29 * Constructor.
30 *
31 * @param protocol the IP protocol (e.g., TCP=6, UDP=17) to match
32 * (8 bits unsigned integer)
33 */
34 IPProtocolCriterion(short protocol) {
35 this.proto = (short) (protocol & MASK);
36 }
37
38 @Override
39 public Type type() {
40 return Type.IP_PROTO;
41 }
42
43 /**
44 * Gets the IP protocol to match.
45 *
46 * @return the IP protocol to match (8 bits unsigned integer)
47 */
48 public short protocol() {
49 return proto;
50 }
51
52 @Override
53 public String toString() {
Jonathan Hartc7840bd2016-01-21 23:26:29 -080054 return type().toString() + SEPARATOR + proto;
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -070055 }
56
57 @Override
58 public int hashCode() {
Thomas Vachuska6c8eff32015-05-27 16:11:44 -070059 return Objects.hash(type().ordinal(), proto);
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -070060 }
61
62 @Override
63 public boolean equals(Object obj) {
64 if (this == obj) {
65 return true;
66 }
67 if (obj instanceof IPProtocolCriterion) {
68 IPProtocolCriterion that = (IPProtocolCriterion) obj;
69 return Objects.equals(proto, that.proto);
70 }
71 return false;
72 }
73}