blob: 91b0cc2bcd9188eda2a4f33fbc518e54f95634f4 [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
Hyunsun Mooncf732fb2015-08-22 21:04:23 -070018import org.onlab.packet.TpPort;
19
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -070020import java.util.Objects;
21
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -070022/**
23 * Implementation of TCP port criterion (16 bits unsigned integer).
24 */
25public final class TcpPortCriterion implements Criterion {
Hyunsun Mooncf732fb2015-08-22 21:04:23 -070026 private final TpPort tcpPort;
Andreas Gilbert75b882f72017-02-03 09:58:07 +010027 private final TpPort mask;
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -070028 private final Type type;
29
30 /**
31 * Constructor.
32 *
Hyunsun Mooncf732fb2015-08-22 21:04:23 -070033 * @param tcpPort the TCP port to match
Andreas Gilbert75b882f72017-02-03 09:58:07 +010034 * @param mask the mask for the TCP port
35 * @param type the match type. Should be either Type.TCP_SRC_MASKED or
36 * Type.TCP_DST_MASKED
37 */
38 TcpPortCriterion(TpPort tcpPort, TpPort mask, Type type) {
39 this.tcpPort = tcpPort;
40 this.mask = mask;
41 this.type = type;
42 }
43
44 /**
45 * Constructor.
46 *
47 * @param tcpPort the TCP port to match
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -070048 * @param type the match type. Should be either Type.TCP_SRC or
49 * Type.TCP_DST
50 */
Hyunsun Mooncf732fb2015-08-22 21:04:23 -070051 TcpPortCriterion(TpPort tcpPort, Type type) {
Andreas Gilbert75b882f72017-02-03 09:58:07 +010052 this(tcpPort, null, type);
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -070053 }
54
55 @Override
56 public Type type() {
57 return this.type;
58 }
59
60 /**
61 * Gets the TCP port to match.
62 *
Hyunsun Mooncf732fb2015-08-22 21:04:23 -070063 * @return the TCP port to match
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -070064 */
Hyunsun Mooncf732fb2015-08-22 21:04:23 -070065 public TpPort tcpPort() {
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -070066 return this.tcpPort;
67 }
68
Andreas Gilbert75b882f72017-02-03 09:58:07 +010069 /**
70 * Gets the mask for the TCP port to match.
71 *
72 * @return the TCP port mask, null if not specified
73 */
74 public TpPort mask() {
75 return this.mask;
76 }
77
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -070078 @Override
79 public String toString() {
Andreas Gilbert75b882f72017-02-03 09:58:07 +010080 return (mask != null) ?
81 type().toString() + SEPARATOR + tcpPort + "/" + mask :
82 type().toString() + SEPARATOR + tcpPort;
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -070083 }
84
85 @Override
86 public int hashCode() {
Andreas Gilbert75b882f72017-02-03 09:58:07 +010087 return Objects.hash(type.ordinal(), tcpPort, mask);
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -070088 }
89
90 @Override
91 public boolean equals(Object obj) {
92 if (this == obj) {
93 return true;
94 }
95 if (obj instanceof TcpPortCriterion) {
96 TcpPortCriterion that = (TcpPortCriterion) obj;
97 return Objects.equals(tcpPort, that.tcpPort) &&
Andreas Gilbert75b882f72017-02-03 09:58:07 +010098 Objects.equals(mask, that.mask) &&
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -070099 Objects.equals(type, that.type);
100 }
101 return false;
102 }
103}