blob: 6d7bc4b35ed98ddfd73b616e9d86c0b617bf26df [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 UDP port criterion (16 bits unsigned integer).
24 */
25public final class UdpPortCriterion implements Criterion {
Hyunsun Mooncf732fb2015-08-22 21:04:23 -070026 private final TpPort udpPort;
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 udpPort the UDP port to match
Andreas Gilbert75b882f72017-02-03 09:58:07 +010034 * @param mask the mask for the UDP port
35 * @param type the match type. Should be either Type.UDP_SRC_MASKED or
36 * Type.UDP_DST_MASKED
37 */
38 UdpPortCriterion(TpPort udpPort, TpPort mask, Type type) {
39 this.udpPort = udpPort;
40 this.mask = mask;
41 this.type = type;
42 }
43
44 /**
45 * Constructor.
46 *
47 * @param udpPort the UDP port to match
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -070048 * @param type the match type. Should be either Type.UDP_SRC or
49 * Type.UDP_DST
50 */
Hyunsun Mooncf732fb2015-08-22 21:04:23 -070051 UdpPortCriterion(TpPort udpPort, Type type) {
Andreas Gilbert75b882f72017-02-03 09:58:07 +010052 this(udpPort, 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 UDP port to match.
62 *
Hyunsun Mooncf732fb2015-08-22 21:04:23 -070063 * @return the UDP port to match
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -070064 */
Hyunsun Mooncf732fb2015-08-22 21:04:23 -070065 public TpPort udpPort() {
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -070066 return this.udpPort;
67 }
68
Andreas Gilbert75b882f72017-02-03 09:58:07 +010069 /**
70 * Gets the mask for the UDP port to match.
71 *
72 * @return the UDP 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 + udpPort + "/" + mask :
82 type().toString() + SEPARATOR + udpPort;
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(), udpPort, 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 UdpPortCriterion) {
96 UdpPortCriterion that = (UdpPortCriterion) obj;
97 return Objects.equals(udpPort, that.udpPort) &&
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}