Daniele Moro | 5e66f98 | 2021-06-11 16:41:48 +0200 | [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 org.onlab.packet.Ip4Address; |
| 20 | import org.onlab.util.ImmutableByteSequence; |
| 21 | |
| 22 | import java.util.Objects; |
| 23 | |
| 24 | import static com.google.common.base.Preconditions.checkNotNull; |
| 25 | |
| 26 | /** |
| 27 | * A structure representing a unidirectional GTP tunnel. |
| 28 | */ |
| 29 | public final class GtpTunnel { |
| 30 | private final Ip4Address src; // The source address of the unidirectional tunnel |
| 31 | private final Ip4Address dst; // The destination address of the unidirectional tunnel |
| 32 | private final ImmutableByteSequence teid; // Tunnel Endpoint Identifier |
| 33 | private final short srcPort; // Tunnel destination port, default 2152 |
| 34 | |
| 35 | private GtpTunnel(Ip4Address src, Ip4Address dst, ImmutableByteSequence teid, |
| 36 | Short srcPort) { |
| 37 | this.src = src; |
| 38 | this.dst = dst; |
| 39 | this.teid = teid; |
| 40 | this.srcPort = srcPort; |
| 41 | } |
| 42 | |
| 43 | public static GtpTunnelBuilder builder() { |
| 44 | return new GtpTunnelBuilder(); |
| 45 | } |
| 46 | |
| 47 | @Override |
| 48 | public String toString() { |
| 49 | return String.format("GTP-Tunnel(%s -> %s, TEID:%s)", |
| 50 | src.toString(), dst.toString(), teid.toString()); |
| 51 | } |
| 52 | |
| 53 | @Override |
| 54 | public boolean equals(Object obj) { |
| 55 | if (obj == this) { |
| 56 | return true; |
| 57 | } |
| 58 | if (obj == null) { |
| 59 | return false; |
| 60 | } |
| 61 | if (getClass() != obj.getClass()) { |
| 62 | return false; |
| 63 | } |
| 64 | GtpTunnel that = (GtpTunnel) obj; |
| 65 | |
| 66 | return (this.src.equals(that.src) && |
| 67 | this.dst.equals(that.dst) && |
| 68 | this.teid.equals(that.teid) && |
| 69 | (this.srcPort == that.srcPort)); |
| 70 | } |
| 71 | |
| 72 | @Override |
| 73 | public int hashCode() { |
| 74 | return Objects.hash(src, dst, teid, srcPort); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Get the source IP address of this unidirectional GTP tunnel. |
| 79 | * |
| 80 | * @return tunnel source IP |
| 81 | */ |
| 82 | public Ip4Address src() { |
| 83 | return this.src; |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Get the destination address of this unidirectional GTP tunnel. |
| 88 | * |
| 89 | * @return tunnel destination IP |
| 90 | */ |
| 91 | public Ip4Address dst() { |
| 92 | return this.dst; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Get the ID of this unidirectional GTP tunnel. |
| 97 | * |
| 98 | * @return tunnel ID |
| 99 | */ |
| 100 | public ImmutableByteSequence teid() { |
| 101 | return this.teid; |
| 102 | } |
| 103 | |
| 104 | |
| 105 | /** |
| 106 | * Get the source L4 port of this unidirectional GTP tunnel. |
| 107 | * |
| 108 | * @return tunnel source port |
| 109 | */ |
| 110 | public Short srcPort() { |
| 111 | return this.srcPort; |
| 112 | } |
| 113 | |
| 114 | public static class GtpTunnelBuilder { |
| 115 | private Ip4Address src; |
| 116 | private Ip4Address dst; |
| 117 | private ImmutableByteSequence teid; |
| 118 | private short srcPort = 2152; // Default value is equal to GTP tunnel dst port |
| 119 | |
| 120 | public GtpTunnelBuilder() { |
| 121 | this.src = null; |
| 122 | this.dst = null; |
| 123 | this.teid = null; |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Set the source IP address of the unidirectional GTP tunnel. |
| 128 | * |
| 129 | * @param src GTP tunnel source IP |
| 130 | * @return This builder object |
| 131 | */ |
| 132 | public GtpTunnelBuilder setSrc(Ip4Address src) { |
| 133 | this.src = src; |
| 134 | return this; |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Set the destination IP address of the unidirectional GTP tunnel. |
| 139 | * |
| 140 | * @param dst GTP tunnel destination IP |
| 141 | * @return This builder object |
| 142 | */ |
| 143 | public GtpTunnelBuilder setDst(Ip4Address dst) { |
| 144 | this.dst = dst; |
| 145 | return this; |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Set the identifier of this unidirectional GTP tunnel. |
| 150 | * |
| 151 | * @param teid tunnel ID |
| 152 | * @return This builder object |
| 153 | */ |
| 154 | public GtpTunnelBuilder setTeid(ImmutableByteSequence teid) { |
| 155 | this.teid = teid; |
| 156 | return this; |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Set the identifier of this unidirectional GTP tunnel. |
| 161 | * |
| 162 | * @param teid tunnel ID |
| 163 | * @return This builder object |
| 164 | */ |
| 165 | public GtpTunnelBuilder setTeid(long teid) { |
| 166 | this.teid = ImmutableByteSequence.copyFrom(teid); |
| 167 | return this; |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Set the source port of this unidirectional GTP tunnel. |
| 172 | * |
| 173 | * @param srcPort tunnel source port |
| 174 | * @return this builder object |
| 175 | */ |
| 176 | public GtpTunnelBuilder setSrcPort(short srcPort) { |
| 177 | this.srcPort = srcPort; |
| 178 | return this; |
| 179 | } |
| 180 | |
| 181 | public GtpTunnel build() { |
| 182 | checkNotNull(src, "Tunnel source address cannot be null"); |
| 183 | checkNotNull(dst, "Tunnel destination address cannot be null"); |
| 184 | checkNotNull(teid, "Tunnel TEID cannot be null"); |
| 185 | return new GtpTunnel(this.src, this.dst, this.teid, srcPort); |
| 186 | } |
| 187 | } |
| 188 | } |