tosinski | 36ba33a | 2021-11-22 16:53:00 +0100 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.net.behaviour.upf; |
| 18 | |
| 19 | import com.google.common.annotations.Beta; |
| 20 | import org.onlab.packet.Ip4Address; |
| 21 | |
| 22 | import java.util.Objects; |
| 23 | |
| 24 | import static com.google.common.base.Preconditions.checkArgument; |
| 25 | |
| 26 | /** |
Daniele Moro | f3a5ab0 | 2022-01-26 16:47:08 +0100 | [diff] [blame] | 27 | * A structure representing a UPF GTP tunnel peer. |
tosinski | 36ba33a | 2021-11-22 16:53:00 +0100 | [diff] [blame] | 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 |
Daniele Moro | f3a5ab0 | 2022-01-26 16:47:08 +0100 | [diff] [blame] | 33 | public final class UpfGtpTunnelPeer implements UpfEntity { |
tosinski | 36ba33a | 2021-11-22 16:53:00 +0100 | [diff] [blame] | 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 | |
Daniele Moro | f3a5ab0 | 2022-01-26 16:47:08 +0100 | [diff] [blame] | 41 | private UpfGtpTunnelPeer(byte tunPeerId, Ip4Address src, Ip4Address dst, short srcPort) { |
tosinski | 36ba33a | 2021-11-22 16:53:00 +0100 | [diff] [blame] | 42 | this.tunPeerId = tunPeerId; |
| 43 | this.src = src; |
| 44 | this.dst = dst; |
| 45 | this.srcPort = srcPort; |
| 46 | } |
| 47 | |
Daniele Moro | f3a5ab0 | 2022-01-26 16:47:08 +0100 | [diff] [blame] | 48 | public static UpfGtpTunnelPeer.Builder builder() { |
| 49 | return new UpfGtpTunnelPeer.Builder(); |
tosinski | 36ba33a | 2021-11-22 16:53:00 +0100 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | @Override |
| 53 | public String toString() { |
Daniele Moro | f3a5ab0 | 2022-01-26 16:47:08 +0100 | [diff] [blame] | 54 | return String.format("UpfGtpTunnelPeer(tunn_peer_id=%s -> src=%s, dst=%s src_port=%s)", |
tosinski | 36ba33a | 2021-11-22 16:53:00 +0100 | [diff] [blame] | 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 | |
Daniele Moro | f3a5ab0 | 2022-01-26 16:47:08 +0100 | [diff] [blame] | 71 | UpfGtpTunnelPeer that = (UpfGtpTunnelPeer) object; |
| 72 | return this.tunPeerId == that.tunPeerId && |
tosinski | 36ba33a | 2021-11-22 16:53:00 +0100 | [diff] [blame] | 73 | this.src.equals(that.src) && |
| 74 | this.dst.equals(that.dst) && |
Daniele Moro | f3a5ab0 | 2022-01-26 16:47:08 +0100 | [diff] [blame] | 75 | this.srcPort == that.srcPort; |
tosinski | 36ba33a | 2021-11-22 16:53:00 +0100 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | @Override |
| 79 | public int hashCode() { |
Daniele Moro | f3a5ab0 | 2022-01-26 16:47:08 +0100 | [diff] [blame] | 80 | return Objects.hash(tunPeerId, src, dst, srcPort); |
tosinski | 36ba33a | 2021-11-22 16:53:00 +0100 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | /** |
Daniele Moro | f3a5ab0 | 2022-01-26 16:47:08 +0100 | [diff] [blame] | 84 | * Get the ID of the UPF GTP tunnel peer. |
tosinski | 36ba33a | 2021-11-22 16:53:00 +0100 | [diff] [blame] | 85 | * |
| 86 | * @return GTP tunnel peer ID |
| 87 | */ |
| 88 | public byte tunPeerId() { |
| 89 | return tunPeerId; |
| 90 | } |
| 91 | |
| 92 | /** |
Daniele Moro | f3a5ab0 | 2022-01-26 16:47:08 +0100 | [diff] [blame] | 93 | * Get the source IP address of this unidirectional UPF GTP tunnel. |
tosinski | 36ba33a | 2021-11-22 16:53:00 +0100 | [diff] [blame] | 94 | * |
| 95 | * @return tunnel source IP |
| 96 | */ |
| 97 | public Ip4Address src() { |
| 98 | return this.src; |
| 99 | } |
| 100 | |
| 101 | /** |
Daniele Moro | f3a5ab0 | 2022-01-26 16:47:08 +0100 | [diff] [blame] | 102 | * Get the destination address of this unidirectional UPF GTP tunnel. |
tosinski | 36ba33a | 2021-11-22 16:53:00 +0100 | [diff] [blame] | 103 | * |
| 104 | * @return tunnel destination IP |
| 105 | */ |
| 106 | public Ip4Address dst() { |
| 107 | return this.dst; |
| 108 | } |
| 109 | |
| 110 | /** |
Daniele Moro | f3a5ab0 | 2022-01-26 16:47:08 +0100 | [diff] [blame] | 111 | * Get the source L4 port of this unidirectional UPF GTP tunnel. |
tosinski | 36ba33a | 2021-11-22 16:53:00 +0100 | [diff] [blame] | 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 | /** |
Daniele Moro | f3a5ab0 | 2022-01-26 16:47:08 +0100 | [diff] [blame] | 135 | * Set the ID of the UPF GTP Tunnel peer. |
tosinski | 36ba33a | 2021-11-22 16:53:00 +0100 | [diff] [blame] | 136 | * |
| 137 | * @param tunPeerId GTP tunnel peer ID |
| 138 | * @return This builder object |
| 139 | */ |
Daniele Moro | f3a5ab0 | 2022-01-26 16:47:08 +0100 | [diff] [blame] | 140 | public UpfGtpTunnelPeer.Builder withTunnelPeerId(byte tunPeerId) { |
tosinski | 36ba33a | 2021-11-22 16:53:00 +0100 | [diff] [blame] | 141 | this.tunPeerId = tunPeerId; |
| 142 | return this; |
| 143 | } |
| 144 | |
| 145 | /** |
Daniele Moro | f3a5ab0 | 2022-01-26 16:47:08 +0100 | [diff] [blame] | 146 | * Set the source IP address of the unidirectional UPF GTP tunnel. |
tosinski | 36ba33a | 2021-11-22 16:53:00 +0100 | [diff] [blame] | 147 | * |
| 148 | * @param src GTP tunnel source IP |
| 149 | * @return This builder object |
| 150 | */ |
Daniele Moro | f3a5ab0 | 2022-01-26 16:47:08 +0100 | [diff] [blame] | 151 | public UpfGtpTunnelPeer.Builder withSrcAddr(Ip4Address src) { |
tosinski | 36ba33a | 2021-11-22 16:53:00 +0100 | [diff] [blame] | 152 | this.src = src; |
| 153 | return this; |
| 154 | } |
| 155 | |
| 156 | /** |
Daniele Moro | f3a5ab0 | 2022-01-26 16:47:08 +0100 | [diff] [blame] | 157 | * Set the destination IP address of the unidirectional UPF GTP tunnel. |
tosinski | 36ba33a | 2021-11-22 16:53:00 +0100 | [diff] [blame] | 158 | * |
| 159 | * @param dst GTP tunnel destination IP |
| 160 | * @return This builder object |
| 161 | */ |
Daniele Moro | f3a5ab0 | 2022-01-26 16:47:08 +0100 | [diff] [blame] | 162 | public UpfGtpTunnelPeer.Builder withDstAddr(Ip4Address dst) { |
tosinski | 36ba33a | 2021-11-22 16:53:00 +0100 | [diff] [blame] | 163 | this.dst = dst; |
| 164 | return this; |
| 165 | } |
| 166 | |
| 167 | /** |
Daniele Moro | f3a5ab0 | 2022-01-26 16:47:08 +0100 | [diff] [blame] | 168 | * Set the source port of this unidirectional UPF GTP tunnel. |
tosinski | 36ba33a | 2021-11-22 16:53:00 +0100 | [diff] [blame] | 169 | * |
| 170 | * @param srcPort tunnel source port |
| 171 | * @return this builder object |
| 172 | */ |
Daniele Moro | f3a5ab0 | 2022-01-26 16:47:08 +0100 | [diff] [blame] | 173 | public UpfGtpTunnelPeer.Builder withSrcPort(short srcPort) { |
tosinski | 36ba33a | 2021-11-22 16:53:00 +0100 | [diff] [blame] | 174 | this.srcPort = srcPort; |
| 175 | return this; |
| 176 | } |
| 177 | |
Daniele Moro | f3a5ab0 | 2022-01-26 16:47:08 +0100 | [diff] [blame] | 178 | public UpfGtpTunnelPeer build() { |
tosinski | 36ba33a | 2021-11-22 16:53:00 +0100 | [diff] [blame] | 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"); |
Daniele Moro | f3a5ab0 | 2022-01-26 16:47:08 +0100 | [diff] [blame] | 182 | return new UpfGtpTunnelPeer(this.tunPeerId, this.src, this.dst, srcPort); |
tosinski | 36ba33a | 2021-11-22 16:53:00 +0100 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | } |
| 186 | |
| 187 | } |