blob: 04b216bd6838cb1eb5bd1488856b8f9df5c088a3 [file] [log] [blame]
tosinski36ba33a2021-11-22 16:53:00 +01001/*
2 * Copyright 2021-present Open Networking Foundation
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 */
16
17package org.onosproject.net.behaviour.upf;
18
19import com.google.common.annotations.Beta;
20import org.onlab.packet.Ip4Address;
21
22import java.util.Objects;
23
24import static com.google.common.base.Preconditions.checkArgument;
25
26/**
Daniele Morof3a5ab02022-01-26 16:47:08 +010027 * A structure representing a UPF GTP tunnel peer.
tosinski36ba33a2021-11-22 16:53:00 +010028 * The GTP Tunnel Peer is used by UPF to identify a second end of a GTP tunnel.
29 * The source and destination tunnel IPv4 addresses, and source UDP port are set
30 * based on the information from this structure.
31 */
32@Beta
Daniele Morof3a5ab02022-01-26 16:47:08 +010033public final class UpfGtpTunnelPeer implements UpfEntity {
tosinski36ba33a2021-11-22 16:53:00 +010034 // Match keys
35 private final byte tunPeerId;
36 // Action parameters
37 private final Ip4Address src; // The source address of the unidirectional tunnel
38 private final Ip4Address dst; // The destination address of the unidirectional tunnel
39 private final short srcPort; // Tunnel source port, default 2152
40
Daniele Morof3a5ab02022-01-26 16:47:08 +010041 private UpfGtpTunnelPeer(byte tunPeerId, Ip4Address src, Ip4Address dst, short srcPort) {
tosinski36ba33a2021-11-22 16:53:00 +010042 this.tunPeerId = tunPeerId;
43 this.src = src;
44 this.dst = dst;
45 this.srcPort = srcPort;
46 }
47
Daniele Morof3a5ab02022-01-26 16:47:08 +010048 public static UpfGtpTunnelPeer.Builder builder() {
49 return new UpfGtpTunnelPeer.Builder();
tosinski36ba33a2021-11-22 16:53:00 +010050 }
51
52 @Override
53 public String toString() {
Daniele Morof3a5ab02022-01-26 16:47:08 +010054 return String.format("UpfGtpTunnelPeer(tunn_peer_id=%s -> src=%s, dst=%s src_port=%s)",
tosinski36ba33a2021-11-22 16:53:00 +010055 tunPeerId, src.toString(), dst.toString(), srcPort);
56 }
57
58 public boolean equals(Object object) {
59 if (this == object) {
60 return true;
61 }
62
63 if (object == null) {
64 return false;
65 }
66
67 if (getClass() != object.getClass()) {
68 return false;
69 }
70
Daniele Morof3a5ab02022-01-26 16:47:08 +010071 UpfGtpTunnelPeer that = (UpfGtpTunnelPeer) object;
72 return this.tunPeerId == that.tunPeerId &&
tosinski36ba33a2021-11-22 16:53:00 +010073 this.src.equals(that.src) &&
74 this.dst.equals(that.dst) &&
Daniele Morof3a5ab02022-01-26 16:47:08 +010075 this.srcPort == that.srcPort;
tosinski36ba33a2021-11-22 16:53:00 +010076 }
77
78 @Override
79 public int hashCode() {
Daniele Morof3a5ab02022-01-26 16:47:08 +010080 return Objects.hash(tunPeerId, src, dst, srcPort);
tosinski36ba33a2021-11-22 16:53:00 +010081 }
82
83 /**
Daniele Morof3a5ab02022-01-26 16:47:08 +010084 * Get the ID of the UPF GTP tunnel peer.
tosinski36ba33a2021-11-22 16:53:00 +010085 *
86 * @return GTP tunnel peer ID
87 */
88 public byte tunPeerId() {
89 return tunPeerId;
90 }
91
92 /**
Daniele Morof3a5ab02022-01-26 16:47:08 +010093 * Get the source IP address of this unidirectional UPF GTP tunnel.
tosinski36ba33a2021-11-22 16:53:00 +010094 *
95 * @return tunnel source IP
96 */
97 public Ip4Address src() {
98 return this.src;
99 }
100
101 /**
Daniele Morof3a5ab02022-01-26 16:47:08 +0100102 * Get the destination address of this unidirectional UPF GTP tunnel.
tosinski36ba33a2021-11-22 16:53:00 +0100103 *
104 * @return tunnel destination IP
105 */
106 public Ip4Address dst() {
107 return this.dst;
108 }
109
110 /**
Daniele Morof3a5ab02022-01-26 16:47:08 +0100111 * Get the source L4 port of this unidirectional UPF GTP tunnel.
tosinski36ba33a2021-11-22 16:53:00 +0100112 *
113 * @return tunnel source port
114 */
115 public short srcPort() {
116 return this.srcPort;
117 }
118
119 @Override
120 public UpfEntityType type() {
121 return UpfEntityType.TUNNEL_PEER;
122 }
123
124 public static class Builder {
125 private Byte tunPeerId = null;
126 private Ip4Address src = null;
127 private Ip4Address dst = null;
128 private short srcPort = 2152; // Default value is equal to GTP tunnel dst port
129
130 public Builder() {
131
132 }
133
134 /**
Daniele Morof3a5ab02022-01-26 16:47:08 +0100135 * Set the ID of the UPF GTP Tunnel peer.
tosinski36ba33a2021-11-22 16:53:00 +0100136 *
137 * @param tunPeerId GTP tunnel peer ID
138 * @return This builder object
139 */
Daniele Morof3a5ab02022-01-26 16:47:08 +0100140 public UpfGtpTunnelPeer.Builder withTunnelPeerId(byte tunPeerId) {
tosinski36ba33a2021-11-22 16:53:00 +0100141 this.tunPeerId = tunPeerId;
142 return this;
143 }
144
145 /**
Daniele Morof3a5ab02022-01-26 16:47:08 +0100146 * Set the source IP address of the unidirectional UPF GTP tunnel.
tosinski36ba33a2021-11-22 16:53:00 +0100147 *
148 * @param src GTP tunnel source IP
149 * @return This builder object
150 */
Daniele Morof3a5ab02022-01-26 16:47:08 +0100151 public UpfGtpTunnelPeer.Builder withSrcAddr(Ip4Address src) {
tosinski36ba33a2021-11-22 16:53:00 +0100152 this.src = src;
153 return this;
154 }
155
156 /**
Daniele Morof3a5ab02022-01-26 16:47:08 +0100157 * Set the destination IP address of the unidirectional UPF GTP tunnel.
tosinski36ba33a2021-11-22 16:53:00 +0100158 *
159 * @param dst GTP tunnel destination IP
160 * @return This builder object
161 */
Daniele Morof3a5ab02022-01-26 16:47:08 +0100162 public UpfGtpTunnelPeer.Builder withDstAddr(Ip4Address dst) {
tosinski36ba33a2021-11-22 16:53:00 +0100163 this.dst = dst;
164 return this;
165 }
166
167 /**
Daniele Morof3a5ab02022-01-26 16:47:08 +0100168 * Set the source port of this unidirectional UPF GTP tunnel.
tosinski36ba33a2021-11-22 16:53:00 +0100169 *
170 * @param srcPort tunnel source port
171 * @return this builder object
172 */
Daniele Morof3a5ab02022-01-26 16:47:08 +0100173 public UpfGtpTunnelPeer.Builder withSrcPort(short srcPort) {
tosinski36ba33a2021-11-22 16:53:00 +0100174 this.srcPort = srcPort;
175 return this;
176 }
177
Daniele Morof3a5ab02022-01-26 16:47:08 +0100178 public UpfGtpTunnelPeer build() {
tosinski36ba33a2021-11-22 16:53:00 +0100179 checkArgument(tunPeerId != null, "Tunnel Peer ID must be provided");
180 checkArgument(src != null, "Tunnel source address cannot be null");
181 checkArgument(dst != null, "Tunnel destination address cannot be null");
Daniele Morof3a5ab02022-01-26 16:47:08 +0100182 return new UpfGtpTunnelPeer(this.tunPeerId, this.src, this.dst, srcPort);
tosinski36ba33a2021-11-22 16:53:00 +0100183 }
184
185 }
186
187}