1 /**
2  * Copyright 2021 The Android Open Source Project
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 android.telephony.data;
18 
19 import android.annotation.IntRange;
20 import android.annotation.NonNull;
21 import android.annotation.Nullable;
22 import android.os.Parcel;
23 import android.os.Parcelable;
24 
25 import java.util.ArrayList;
26 import java.util.List;
27 import java.util.Objects;
28 
29 /**
30  * Represents a single URSP rule as defined in 3GPP TS 24.526. URSP stands for UE Route Selection
31  * Policy. In 5G, network can provide URSP information to devices which provides information on
32  * what connection parameters should be used for what traffic.
33  */
34 public final class UrspRule implements Parcelable {
35     /**
36      * The min acceptable value for the precedence of a URSP rule.
37      * @hide
38      */
39     public static final int MIN_URSP_PRECEDENCE = 0;
40 
41     /**
42      * The max acceptable value for the precedence of a URSP rule.
43      * @hide
44      */
45     public static final int MAX_URSP_PRECEDENCE = 255;
46 
47     @IntRange(from = MIN_URSP_PRECEDENCE, to = MAX_URSP_PRECEDENCE)
48     private final int mPrecedence;
49     private final List<TrafficDescriptor> mTrafficDescriptors;
50     private final List<RouteSelectionDescriptor> mRouteSelectionDescriptor;
51 
52     /** @hide */
UrspRule(int precedence, List<TrafficDescriptor> trafficDescriptors, List<RouteSelectionDescriptor> routeSelectionDescriptor)53     public UrspRule(int precedence, List<TrafficDescriptor> trafficDescriptors,
54             List<RouteSelectionDescriptor> routeSelectionDescriptor) {
55         mPrecedence = precedence;
56         mTrafficDescriptors = new ArrayList<>();
57         mTrafficDescriptors.addAll(trafficDescriptors);
58         mRouteSelectionDescriptor = new ArrayList<>();
59         mRouteSelectionDescriptor.addAll(routeSelectionDescriptor);
60     }
61 
UrspRule(Parcel p)62     private UrspRule(Parcel p) {
63         mPrecedence = p.readInt();
64         mTrafficDescriptors = p.createTypedArrayList(TrafficDescriptor.CREATOR);
65         mRouteSelectionDescriptor = p.createTypedArrayList(RouteSelectionDescriptor.CREATOR);
66     }
67 
68     /**
69      * Precedence value in the range of 0 to 255. Higher value has lower precedence.
70      * @return the precedence value for this URSP rule.
71      */
72     @IntRange(from = MIN_URSP_PRECEDENCE, to = MAX_URSP_PRECEDENCE)
getPrecedence()73     public int getPrecedence() {
74         return mPrecedence;
75     }
76 
77     /**
78      * These traffic descriptors are used as a matcher for network requests.
79      * @return the traffic descriptors which are associated to this URSP rule.
80      */
getTrafficDescriptors()81     public @NonNull List<TrafficDescriptor> getTrafficDescriptors() {
82         return mTrafficDescriptors;
83     }
84 
85     /**
86      * List of routes (connection parameters) that must be used by the device for requests matching
87      * a traffic descriptor.
88      * @return the route selection descriptors which are associated to this URSP rule.
89      */
getRouteSelectionDescriptor()90     public @NonNull List<RouteSelectionDescriptor> getRouteSelectionDescriptor() {
91         return mRouteSelectionDescriptor;
92     }
93 
94     @Override
writeToParcel(@onNull Parcel dest, int flags)95     public void writeToParcel(@NonNull Parcel dest, int flags) {
96         dest.writeInt(mPrecedence);
97         dest.writeTypedList(mTrafficDescriptors, flags);
98         dest.writeTypedList(mRouteSelectionDescriptor, flags);
99     }
100 
101     public static final @NonNull Parcelable.Creator<UrspRule> CREATOR =
102             new Parcelable.Creator<UrspRule>() {
103                 @Override
104                 public UrspRule createFromParcel(Parcel source) {
105                     return new UrspRule(source);
106                 }
107 
108                 @Override
109                 public UrspRule[] newArray(int size) {
110                     return new UrspRule[size];
111                 }
112             };
113 
114     @Override
describeContents()115     public int describeContents() {
116         return 0;
117     }
118 
119     @Override
equals(@ullable Object o)120     public boolean equals(@Nullable Object o) {
121         if (this == o) return true;
122         if (o == null || getClass() != o.getClass()) return false;
123         UrspRule that = (UrspRule) o;
124         return mPrecedence == that.mPrecedence
125                 && mTrafficDescriptors.size() == that.mTrafficDescriptors.size()
126                 && mTrafficDescriptors.containsAll(that.mTrafficDescriptors)
127                 && mRouteSelectionDescriptor.size() == that.mRouteSelectionDescriptor.size()
128                 && mRouteSelectionDescriptor.containsAll(that.mRouteSelectionDescriptor);
129     }
130 
131     @Override
hashCode()132     public int hashCode() {
133         return Objects.hash(mPrecedence, mTrafficDescriptors, mRouteSelectionDescriptor);
134     }
135 
136     @Override
toString()137     public String toString() {
138         return "{.precedence = " + mPrecedence + ", .trafficDescriptors = " + mTrafficDescriptors
139                 + ", .routeSelectionDescriptor = " + mRouteSelectionDescriptor + "}";
140     }
141 }
142