blob: a3cf6628392bc70e708c16e060ae254c12f6605a [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/**
27 * A structure representing a GTP tunnel peer.
28 * 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
33public final class GtpTunnelPeer implements UpfEntity {
34 // 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
41 private GtpTunnelPeer(byte tunPeerId, Ip4Address src, Ip4Address dst, short srcPort) {
42 this.tunPeerId = tunPeerId;
43 this.src = src;
44 this.dst = dst;
45 this.srcPort = srcPort;
46 }
47
48 public static GtpTunnelPeer.Builder builder() {
49 return new GtpTunnelPeer.Builder();
50 }
51
52 @Override
53 public String toString() {
54 return String.format("GTP-Tunnel-Peer(%s -> src:%s, dst:%s srcPort:%s)",
55 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
71 GtpTunnelPeer that = (GtpTunnelPeer) object;
72 return (this.tunPeerId == that.tunPeerId &&
73 this.src.equals(that.src) &&
74 this.dst.equals(that.dst) &&
75 (this.srcPort == that.srcPort));
76 }
77
78 @Override
79 public int hashCode() {
80 return Objects.hash(src, dst, srcPort);
81 }
82
83 /**
84 * Get the ID of the GTP tunnel peer.
85 *
86 * @return GTP tunnel peer ID
87 */
88 public byte tunPeerId() {
89 return tunPeerId;
90 }
91
92 /**
93 * Get the source IP address of this unidirectional GTP tunnel.
94 *
95 * @return tunnel source IP
96 */
97 public Ip4Address src() {
98 return this.src;
99 }
100
101 /**
102 * Get the destination address of this unidirectional GTP tunnel.
103 *
104 * @return tunnel destination IP
105 */
106 public Ip4Address dst() {
107 return this.dst;
108 }
109
110 /**
111 * Get the source L4 port of this unidirectional GTP tunnel.
112 *
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 /**
135 * Set the ID of the GTP Tunnel peer.
136 *
137 * @param tunPeerId GTP tunnel peer ID
138 * @return This builder object
139 */
140 public GtpTunnelPeer.Builder withTunnelPeerId(byte tunPeerId) {
141 this.tunPeerId = tunPeerId;
142 return this;
143 }
144
145 /**
146 * Set the source IP address of the unidirectional GTP tunnel.
147 *
148 * @param src GTP tunnel source IP
149 * @return This builder object
150 */
151 public GtpTunnelPeer.Builder withSrcAddr(Ip4Address src) {
152 this.src = src;
153 return this;
154 }
155
156 /**
157 * Set the destination IP address of the unidirectional GTP tunnel.
158 *
159 * @param dst GTP tunnel destination IP
160 * @return This builder object
161 */
162 public GtpTunnelPeer.Builder withDstAddr(Ip4Address dst) {
163 this.dst = dst;
164 return this;
165 }
166
167 /**
168 * Set the source port of this unidirectional GTP tunnel.
169 *
170 * @param srcPort tunnel source port
171 * @return this builder object
172 */
173 public GtpTunnelPeer.Builder withSrcPort(short srcPort) {
174 this.srcPort = srcPort;
175 return this;
176 }
177
178 public GtpTunnelPeer build() {
179 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");
182 return new GtpTunnelPeer(this.tunPeerId, this.src, this.dst, srcPort);
183 }
184
185 }
186
187}