| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
package android.net.vcn; |
| 17 |
|
| 18 |
import static com.android.internal.annotations.VisibleForTesting.Visibility; |
| 19 |
|
| 20 |
import android.annotation.IntDef; |
| 21 |
import android.annotation.NonNull; |
| 22 |
import android.annotation.Nullable; |
| 23 |
import android.os.PersistableBundle; |
| 24 |
import android.util.SparseArray; |
| 25 |
|
| 26 |
import com.android.internal.annotations.VisibleForTesting; |
| 27 |
import com.android.internal.util.IndentingPrintWriter; |
| 28 |
import com.android.internal.util.Preconditions; |
| 29 |
|
| 30 |
import java.lang.annotation.Retention; |
| 31 |
import java.lang.annotation.RetentionPolicy; |
| 32 |
import java.util.Map; |
| 33 |
import java.util.Objects; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
public abstract class VcnUnderlyingNetworkTemplate { |
| 45 |
|
| 46 |
static final int NETWORK_PRIORITY_TYPE_WIFI = 1; |
| 47 |
|
| 48 |
static final int NETWORK_PRIORITY_TYPE_CELL = 2; |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
public static final int MATCH_ANY = 0; |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
public static final int MATCH_REQUIRED = 1; |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
public static final int MATCH_FORBIDDEN = 2; |
| 70 |
|
| 71 |
|
| 72 |
@Retention(RetentionPolicy.SOURCE) |
| 73 |
@IntDef({MATCH_ANY, MATCH_REQUIRED, MATCH_FORBIDDEN}) |
| 74 |
public @interface MatchCriteria {} |
| 75 |
|
| 76 |
private static final SparseArray<String> MATCH_CRITERIA_TO_STRING_MAP = new SparseArray<>(); |
| 77 |
|
| 78 |
static { |
| 79 |
MATCH_CRITERIA_TO_STRING_MAP.put(MATCH_ANY, "MATCH_ANY"); |
| 80 |
MATCH_CRITERIA_TO_STRING_MAP.put(MATCH_REQUIRED, "MATCH_REQUIRED"); |
| 81 |
MATCH_CRITERIA_TO_STRING_MAP.put(MATCH_FORBIDDEN, "MATCH_FORBIDDEN"); |
| 82 |
} |
| 83 |
|
| 84 |
private static final String NETWORK_PRIORITY_TYPE_KEY = "mNetworkPriorityType"; |
| 85 |
private final int mNetworkPriorityType; |
| 86 |
|
| 87 |
|
| 88 |
static final String METERED_MATCH_KEY = "mMeteredMatchCriteria"; |
| 89 |
|
| 90 |
|
| 91 |
static final int DEFAULT_METERED_MATCH_CRITERIA = MATCH_ANY; |
| 92 |
|
| 93 |
private final int mMeteredMatchCriteria; |
| 94 |
|
| 95 |
|
| 96 |
public static final int DEFAULT_MIN_BANDWIDTH_KBPS = 0; |
| 97 |
|
| 98 |
|
| 99 |
static final String MIN_ENTRY_UPSTREAM_BANDWIDTH_KBPS_KEY = "mMinEntryUpstreamBandwidthKbps"; |
| 100 |
|
| 101 |
private final int mMinEntryUpstreamBandwidthKbps; |
| 102 |
|
| 103 |
|
| 104 |
static final String MIN_EXIT_UPSTREAM_BANDWIDTH_KBPS_KEY = "mMinExitUpstreamBandwidthKbps"; |
| 105 |
|
| 106 |
private final int mMinExitUpstreamBandwidthKbps; |
| 107 |
|
| 108 |
|
| 109 |
static final String MIN_ENTRY_DOWNSTREAM_BANDWIDTH_KBPS_KEY = |
| 110 |
"mMinEntryDownstreamBandwidthKbps"; |
| 111 |
|
| 112 |
private final int mMinEntryDownstreamBandwidthKbps; |
| 113 |
|
| 114 |
|
| 115 |
static final String MIN_EXIT_DOWNSTREAM_BANDWIDTH_KBPS_KEY = "mMinExitDownstreamBandwidthKbps"; |
| 116 |
|
| 117 |
private final int mMinExitDownstreamBandwidthKbps; |
| 118 |
|
| 119 |
|
| 120 |
VcnUnderlyingNetworkTemplate( |
| 121 |
int networkPriorityType, |
| 122 |
int meteredMatchCriteria, |
| 123 |
int minEntryUpstreamBandwidthKbps, |
| 124 |
int minExitUpstreamBandwidthKbps, |
| 125 |
int minEntryDownstreamBandwidthKbps, |
| 126 |
int minExitDownstreamBandwidthKbps) { |
| 127 |
mNetworkPriorityType = networkPriorityType; |
| 128 |
mMeteredMatchCriteria = meteredMatchCriteria; |
| 129 |
mMinEntryUpstreamBandwidthKbps = minEntryUpstreamBandwidthKbps; |
| 130 |
mMinExitUpstreamBandwidthKbps = minExitUpstreamBandwidthKbps; |
| 131 |
mMinEntryDownstreamBandwidthKbps = minEntryDownstreamBandwidthKbps; |
| 132 |
mMinExitDownstreamBandwidthKbps = minExitDownstreamBandwidthKbps; |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
static void validateMatchCriteria(int matchCriteria, String matchingCapability) { |
| 137 |
Preconditions.checkArgument( |
| 138 |
MATCH_CRITERIA_TO_STRING_MAP.contains(matchCriteria), |
| 139 |
"Invalid matching criteria: " + matchCriteria + " for " + matchingCapability); |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
static void validateMinBandwidthKbps(int minEntryBandwidth, int minExitBandwidth) { |
| 144 |
Preconditions.checkArgument( |
| 145 |
minEntryBandwidth >= 0, "Invalid minEntryBandwidth, must be >= 0"); |
| 146 |
Preconditions.checkArgument( |
| 147 |
minExitBandwidth >= 0, "Invalid minExitBandwidth, must be >= 0"); |
| 148 |
Preconditions.checkArgument( |
| 149 |
minEntryBandwidth >= minExitBandwidth, |
| 150 |
"Minimum entry bandwidth must be >= exit bandwidth"); |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
protected void validate() { |
| 155 |
validateMatchCriteria(mMeteredMatchCriteria, "mMeteredMatchCriteria"); |
| 156 |
validateMinBandwidthKbps(mMinEntryUpstreamBandwidthKbps, mMinExitUpstreamBandwidthKbps); |
| 157 |
validateMinBandwidthKbps(mMinEntryDownstreamBandwidthKbps, mMinExitDownstreamBandwidthKbps); |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
@NonNull |
| 162 |
@VisibleForTesting(visibility = Visibility.PROTECTED) |
| 163 |
public static VcnUnderlyingNetworkTemplate fromPersistableBundle( |
| 164 |
@NonNull PersistableBundle in) { |
| 165 |
Objects.requireNonNull(in, "PersistableBundle is null"); |
| 166 |
|
| 167 |
final int networkPriorityType = in.getInt(NETWORK_PRIORITY_TYPE_KEY); |
| 168 |
switch (networkPriorityType) { |
| 169 |
case NETWORK_PRIORITY_TYPE_WIFI: |
| 170 |
return VcnWifiUnderlyingNetworkTemplate.fromPersistableBundle(in); |
| 171 |
case NETWORK_PRIORITY_TYPE_CELL: |
| 172 |
return VcnCellUnderlyingNetworkTemplate.fromPersistableBundle(in); |
| 173 |
default: |
| 174 |
throw new IllegalArgumentException( |
| 175 |
"Invalid networkPriorityType:" + networkPriorityType); |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
@NonNull |
| 181 |
PersistableBundle toPersistableBundle() { |
| 182 |
final PersistableBundle result = new PersistableBundle(); |
| 183 |
|
| 184 |
result.putInt(NETWORK_PRIORITY_TYPE_KEY, mNetworkPriorityType); |
| 185 |
result.putInt(METERED_MATCH_KEY, mMeteredMatchCriteria); |
| 186 |
result.putInt(MIN_ENTRY_UPSTREAM_BANDWIDTH_KBPS_KEY, mMinEntryUpstreamBandwidthKbps); |
| 187 |
result.putInt(MIN_EXIT_UPSTREAM_BANDWIDTH_KBPS_KEY, mMinExitUpstreamBandwidthKbps); |
| 188 |
result.putInt(MIN_ENTRY_DOWNSTREAM_BANDWIDTH_KBPS_KEY, mMinEntryDownstreamBandwidthKbps); |
| 189 |
result.putInt(MIN_EXIT_DOWNSTREAM_BANDWIDTH_KBPS_KEY, mMinExitDownstreamBandwidthKbps); |
| 190 |
|
| 191 |
return result; |
| 192 |
} |
| 193 |
|
| 194 |
@Override |
| 195 |
public int hashCode() { |
| 196 |
return Objects.hash( |
| 197 |
mNetworkPriorityType, |
| 198 |
mMeteredMatchCriteria, |
| 199 |
mMinEntryUpstreamBandwidthKbps, |
| 200 |
mMinExitUpstreamBandwidthKbps, |
| 201 |
mMinEntryDownstreamBandwidthKbps, |
| 202 |
mMinExitDownstreamBandwidthKbps); |
| 203 |
} |
| 204 |
|
| 205 |
@Override |
| 206 |
public boolean equals(@Nullable Object other) { |
| 207 |
if (!(other instanceof VcnUnderlyingNetworkTemplate)) { |
| 208 |
return false; |
| 209 |
} |
| 210 |
|
| 211 |
final VcnUnderlyingNetworkTemplate rhs = (VcnUnderlyingNetworkTemplate) other; |
| 212 |
return mNetworkPriorityType == rhs.mNetworkPriorityType |
| 213 |
&& mMeteredMatchCriteria == rhs.mMeteredMatchCriteria |
| 214 |
&& mMinEntryUpstreamBandwidthKbps == rhs.mMinEntryUpstreamBandwidthKbps |
| 215 |
&& mMinExitUpstreamBandwidthKbps == rhs.mMinExitUpstreamBandwidthKbps |
| 216 |
&& mMinEntryDownstreamBandwidthKbps == rhs.mMinEntryDownstreamBandwidthKbps |
| 217 |
&& mMinExitDownstreamBandwidthKbps == rhs.mMinExitDownstreamBandwidthKbps; |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
static String getNameString(SparseArray<String> toStringMap, int key) { |
| 222 |
return toStringMap.get(key, "Invalid value " + key); |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
static String getMatchCriteriaString(int matchCriteria) { |
| 227 |
return getNameString(MATCH_CRITERIA_TO_STRING_MAP, matchCriteria); |
| 228 |
} |
| 229 |
|
| 230 |
|
| 231 |
abstract void dumpTransportSpecificFields(IndentingPrintWriter pw); |
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
public void dump(IndentingPrintWriter pw) { |
| 239 |
pw.println(this.getClass().getSimpleName() + ":"); |
| 240 |
pw.increaseIndent(); |
| 241 |
|
| 242 |
if (mMeteredMatchCriteria != DEFAULT_METERED_MATCH_CRITERIA) { |
| 243 |
pw.println("mMeteredMatchCriteria: " + getMatchCriteriaString(mMeteredMatchCriteria)); |
| 244 |
} |
| 245 |
if (mMinEntryUpstreamBandwidthKbps != DEFAULT_MIN_BANDWIDTH_KBPS) { |
| 246 |
pw.println("mMinEntryUpstreamBandwidthKbps: " + mMinEntryUpstreamBandwidthKbps); |
| 247 |
} |
| 248 |
if (mMinExitUpstreamBandwidthKbps != DEFAULT_MIN_BANDWIDTH_KBPS) { |
| 249 |
pw.println("mMinExitUpstreamBandwidthKbps: " + mMinExitUpstreamBandwidthKbps); |
| 250 |
} |
| 251 |
if (mMinEntryDownstreamBandwidthKbps != DEFAULT_MIN_BANDWIDTH_KBPS) { |
| 252 |
pw.println("mMinEntryDownstreamBandwidthKbps: " + mMinEntryDownstreamBandwidthKbps); |
| 253 |
} |
| 254 |
if (mMinExitDownstreamBandwidthKbps != DEFAULT_MIN_BANDWIDTH_KBPS) { |
| 255 |
pw.println("mMinExitDownstreamBandwidthKbps: " + mMinExitDownstreamBandwidthKbps); |
| 256 |
} |
| 257 |
dumpTransportSpecificFields(pw); |
| 258 |
|
| 259 |
pw.decreaseIndent(); |
| 260 |
} |
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
public int getMetered() { |
| 269 |
return mMeteredMatchCriteria; |
| 270 |
} |
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
public int getMinEntryUpstreamBandwidthKbps() { |
| 279 |
return mMinEntryUpstreamBandwidthKbps; |
| 280 |
} |
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
public int getMinExitUpstreamBandwidthKbps() { |
| 289 |
return mMinExitUpstreamBandwidthKbps; |
| 290 |
} |
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
public int getMinEntryDownstreamBandwidthKbps() { |
| 299 |
return mMinEntryDownstreamBandwidthKbps; |
| 300 |
} |
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
public int getMinExitDownstreamBandwidthKbps() { |
| 309 |
return mMinExitDownstreamBandwidthKbps; |
| 310 |
} |
| 311 |
|
| 312 |
|
| 313 |
public abstract Map<Integer, Integer> getCapabilitiesMatchCriteria(); |
| 314 |
} |