blob: 6879f802786e93c809ec082eb389077086809b2c [file] [log] [blame]
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -07001/*
2 * Copyright 2015 Open Networking Laboratory
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 */
16package org.onosproject.net.flow.criteria;
17
18import java.util.Objects;
19
20import static com.google.common.base.MoreObjects.toStringHelper;
21
22/**
23 * Implementation of Internet Protocol Number criterion (8 bits unsigned)
24 * integer.
25 */
26public final class IPProtocolCriterion implements Criterion {
27 private static final short MASK = 0xff;
28 private final short proto; // IP protocol number: 8 bits
29
30 /**
31 * Constructor.
32 *
33 * @param protocol the IP protocol (e.g., TCP=6, UDP=17) to match
34 * (8 bits unsigned integer)
35 */
36 IPProtocolCriterion(short protocol) {
37 this.proto = (short) (protocol & MASK);
38 }
39
40 @Override
41 public Type type() {
42 return Type.IP_PROTO;
43 }
44
45 /**
46 * Gets the IP protocol to match.
47 *
48 * @return the IP protocol to match (8 bits unsigned integer)
49 */
50 public short protocol() {
51 return proto;
52 }
53
54 @Override
55 public String toString() {
56 return toStringHelper(type().toString())
57 .add("protocol", proto).toString();
58 }
59
60 @Override
61 public int hashCode() {
Thomas Vachuska6c8eff32015-05-27 16:11:44 -070062 return Objects.hash(type().ordinal(), proto);
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -070063 }
64
65 @Override
66 public boolean equals(Object obj) {
67 if (this == obj) {
68 return true;
69 }
70 if (obj instanceof IPProtocolCriterion) {
71 IPProtocolCriterion that = (IPProtocolCriterion) obj;
72 return Objects.equals(proto, that.proto);
73 }
74 return false;
75 }
76}