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.checkNotNull; |
| 25 | |
| 26 | /** |
| 27 | * A structure representing the UE Session on the UPF-programmable device. |
| 28 | * Provides means to set up the UE Session in the uplink direction. |
| 29 | */ |
| 30 | @Beta |
| 31 | public final class SessionUplink implements UpfEntity { |
| 32 | // Match Keys |
| 33 | private final Ip4Address tunDestAddr; // The tunnel destination address (N3/S1U IPv4 address) |
| 34 | private final Integer teid; // The Tunnel Endpoint ID that this UeSession matches on |
| 35 | |
| 36 | // Action parameters |
| 37 | private final boolean dropping; // Used to convey dropping information |
| 38 | |
| 39 | private SessionUplink(Ip4Address tunDestAddr, |
| 40 | Integer teid, |
| 41 | boolean drop) { |
| 42 | this.tunDestAddr = tunDestAddr; |
| 43 | this.teid = teid; |
| 44 | this.dropping = drop; |
| 45 | } |
| 46 | |
| 47 | public static Builder builder() { |
| 48 | return new Builder(); |
| 49 | } |
| 50 | |
| 51 | public boolean equals(Object object) { |
| 52 | if (object == this) { |
| 53 | return true; |
| 54 | } |
| 55 | if (object == null) { |
| 56 | return false; |
| 57 | } |
| 58 | if (getClass() != object.getClass()) { |
| 59 | return false; |
| 60 | } |
| 61 | |
| 62 | SessionUplink that = (SessionUplink) object; |
| 63 | |
| 64 | return this.dropping == that.dropping && |
| 65 | Objects.equals(tunDestAddr, that.tunDestAddr) && |
| 66 | Objects.equals(teid, that.teid); |
| 67 | } |
| 68 | |
| 69 | public int hashCode() { |
| 70 | return Objects.hash(tunDestAddr, teid, dropping); |
| 71 | } |
| 72 | |
| 73 | @Override |
| 74 | public String toString() { |
| 75 | return "UESessionUL{" + matchString() + " -> " + actionString(); |
| 76 | } |
| 77 | |
| 78 | private String matchString() { |
| 79 | return "Match(tun_dst_addr=" + this.tunDstAddr() + ", TEID=" + this.teid() + ")"; |
| 80 | } |
| 81 | |
| 82 | private String actionString() { |
| 83 | StringBuilder actionStrBuilder = new StringBuilder("("); |
| 84 | if (this.needsDropping()) { |
| 85 | actionStrBuilder.append("DROP"); |
| 86 | |
| 87 | } else { |
| 88 | actionStrBuilder.append("FWD"); |
| 89 | } |
| 90 | return actionStrBuilder.append(")").toString(); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * True if this UE Session needs dropping of the uplink traffic. |
| 95 | * |
| 96 | * @return true if the UE Session needs dropping. |
| 97 | */ |
| 98 | public boolean needsDropping() { |
| 99 | return dropping; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Get the tunnel destination IP address in the uplink UE session (N3/S1U IP address). |
| 104 | * |
| 105 | * @return UE IP address |
| 106 | */ |
| 107 | public Ip4Address tunDstAddr() { |
| 108 | return tunDestAddr; |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Get the identifier of the GTP tunnel that this UE Session rule matches on. |
| 113 | * |
| 114 | * @return GTP tunnel ID |
| 115 | */ |
| 116 | public Integer teid() { |
| 117 | return teid; |
| 118 | } |
| 119 | |
| 120 | @Override |
| 121 | public UpfEntityType type() { |
| 122 | return UpfEntityType.SESSION_UPLINK; |
| 123 | } |
| 124 | |
| 125 | public static class Builder { |
| 126 | private Ip4Address tunDstAddr = null; |
| 127 | private Integer teid = null; |
| 128 | private boolean drop = false; |
| 129 | |
| 130 | public Builder() { |
| 131 | |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Set the tunnel destination IP address (N3/S1U address) that this UE Session rule matches on. |
| 136 | * |
| 137 | * @param tunDstAddr The tunnel destination IP address |
| 138 | * @return This builder object |
| 139 | */ |
| 140 | public Builder withTunDstAddr(Ip4Address tunDstAddr) { |
| 141 | this.tunDstAddr = tunDstAddr; |
| 142 | return this; |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Set the identifier of the GTP tunnel that this UE Session rule matches on. |
| 147 | * |
| 148 | * @param teid GTP tunnel ID |
| 149 | * @return This builder object |
| 150 | */ |
| 151 | public Builder withTeid(Integer teid) { |
| 152 | this.teid = teid; |
| 153 | return this; |
| 154 | } |
| 155 | |
| 156 | |
| 157 | /** |
| 158 | * Sets whether to drop uplink UE session traffic or not. |
| 159 | * |
| 160 | * @param drop True if request to buffer, false otherwise |
| 161 | * @return This builder object |
| 162 | */ |
| 163 | public Builder needsDropping(boolean drop) { |
| 164 | this.drop = drop; |
| 165 | return this; |
| 166 | } |
| 167 | |
| 168 | public SessionUplink build() { |
| 169 | // Match keys are required. |
| 170 | checkNotNull(tunDstAddr, "Tunnel destination must be provided"); |
| 171 | checkNotNull(teid, "TEID must be provided"); |
| 172 | return new SessionUplink(tunDstAddr, teid, drop); |
| 173 | } |
| 174 | } |
| 175 | } |