blob: 74d3e5ce7ed9ecb0f0f5014663bf8270c29d3763 [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 TCP port criterion (16 bits unsigned integer).
24 */
25public final class TcpPortCriterion implements Criterion {
26 private static final int MASK = 0xffff;
27 private final int tcpPort; // Port value: 16 bits
28 private final Type type;
29
30 /**
31 * Constructor.
32 *
33 * @param tcpPort the TCP port to match (16 bits unsigned integer)
34 * @param type the match type. Should be either Type.TCP_SRC or
35 * Type.TCP_DST
36 */
37 TcpPortCriterion(int tcpPort, Type type) {
38 this.tcpPort = tcpPort & MASK;
39 this.type = type;
40 }
41
42 @Override
43 public Type type() {
44 return this.type;
45 }
46
47 /**
48 * Gets the TCP port to match.
49 *
50 * @return the TCP port to match (16 bits unsigned integer)
51 */
52 public int tcpPort() {
53 return this.tcpPort;
54 }
55
56 @Override
57 public String toString() {
58 return toStringHelper(type().toString())
59 .add("tcpPort", tcpPort).toString();
60 }
61
62 @Override
63 public int hashCode() {
64 return Objects.hash(type, tcpPort);
65 }
66
67 @Override
68 public boolean equals(Object obj) {
69 if (this == obj) {
70 return true;
71 }
72 if (obj instanceof TcpPortCriterion) {
73 TcpPortCriterion that = (TcpPortCriterion) obj;
74 return Objects.equals(tcpPort, that.tcpPort) &&
75 Objects.equals(type, that.type);
76 }
77 return false;
78 }
79}