Merge changes I1ceccedd,Ide2c7302 am: c70a7d6258 am: 788f94aff2

Original change: https://android-review.googlesource.com/c/platform/frameworks/base/+/1593102

MUST ONLY BE SUBMITTED BY AUTOMERGER

Change-Id: I396a6f333f7449a6fa680fac9b3c31d6dd8f5bec
This commit is contained in:
Cody Kesting
2021-02-23 23:21:23 +00:00
committed by Automerger Merge Worker
7 changed files with 262 additions and 67 deletions

View File

@@ -72,8 +72,7 @@ import java.util.concurrent.Executor;
public class VcnManager { public class VcnManager {
@NonNull private static final String TAG = VcnManager.class.getSimpleName(); @NonNull private static final String TAG = VcnManager.class.getSimpleName();
private static final Map< private static final Map<VcnNetworkPolicyListener, VcnUnderlyingNetworkPolicyListenerBinder>
VcnUnderlyingNetworkPolicyListener, VcnUnderlyingNetworkPolicyListenerBinder>
REGISTERED_POLICY_LISTENERS = new ConcurrentHashMap<>(); REGISTERED_POLICY_LISTENERS = new ConcurrentHashMap<>();
@NonNull private final Context mContext; @NonNull private final Context mContext;
@@ -93,13 +92,13 @@ public class VcnManager {
} }
/** /**
* Get all currently registered VcnUnderlyingNetworkPolicyListeners for testing purposes. * Get all currently registered VcnNetworkPolicyListeners for testing purposes.
* *
* @hide * @hide
*/ */
@VisibleForTesting(visibility = Visibility.PRIVATE) @VisibleForTesting(visibility = Visibility.PRIVATE)
@NonNull @NonNull
public static Map<VcnUnderlyingNetworkPolicyListener, VcnUnderlyingNetworkPolicyListenerBinder> public static Map<VcnNetworkPolicyListener, VcnUnderlyingNetworkPolicyListenerBinder>
getAllPolicyListeners() { getAllPolicyListeners() {
return Collections.unmodifiableMap(REGISTERED_POLICY_LISTENERS); return Collections.unmodifiableMap(REGISTERED_POLICY_LISTENERS);
} }
@@ -161,22 +160,15 @@ public class VcnManager {
} }
} }
// TODO: make VcnUnderlyingNetworkPolicyListener @SystemApi // TODO(b/180537630): remove all VcnUnderlyingNetworkPolicyListener refs once Telephony is using
// the new VcnNetworkPolicyListener API
/** /**
* VcnUnderlyingNetworkPolicyListener is the interface through which internal system components * VcnUnderlyingNetworkPolicyListener is the interface through which internal system components
* can register to receive updates for VCN-underlying Network policies from the System Server. * can register to receive updates for VCN-underlying Network policies from the System Server.
* *
* @hide * @hide
*/ */
public interface VcnUnderlyingNetworkPolicyListener { public interface VcnUnderlyingNetworkPolicyListener extends VcnNetworkPolicyListener {}
/**
* Notifies the implementation that the VCN's underlying Network policy has changed.
*
* <p>After receiving this callback, implementations MUST poll VcnManager for the updated
* VcnUnderlyingNetworkPolicy via VcnManager#getUnderlyingNetworkPolicy.
*/
void onPolicyChanged();
}
/** /**
* Add a listener for VCN-underlying network policy updates. * Add a listener for VCN-underlying network policy updates.
@@ -185,29 +177,14 @@ public class VcnManager {
* Listener * Listener
* @param listener the VcnUnderlyingNetworkPolicyListener to be added * @param listener the VcnUnderlyingNetworkPolicyListener to be added
* @throws SecurityException if the caller does not have permission NETWORK_FACTORY * @throws SecurityException if the caller does not have permission NETWORK_FACTORY
* @throws IllegalArgumentException if the specified VcnUnderlyingNetworkPolicyListener is * @throws IllegalStateException if the specified VcnUnderlyingNetworkPolicyListener is already
* already registered * registered
* @hide * @hide
*/ */
@RequiresPermission(android.Manifest.permission.NETWORK_FACTORY) @RequiresPermission(android.Manifest.permission.NETWORK_FACTORY)
public void addVcnUnderlyingNetworkPolicyListener( public void addVcnUnderlyingNetworkPolicyListener(
@NonNull Executor executor, @NonNull VcnUnderlyingNetworkPolicyListener listener) { @NonNull Executor executor, @NonNull VcnUnderlyingNetworkPolicyListener listener) {
requireNonNull(executor, "executor must not be null"); addVcnNetworkPolicyListener(executor, listener);
requireNonNull(listener, "listener must not be null");
VcnUnderlyingNetworkPolicyListenerBinder binder =
new VcnUnderlyingNetworkPolicyListenerBinder(executor, listener);
if (REGISTERED_POLICY_LISTENERS.putIfAbsent(listener, binder) != null) {
throw new IllegalArgumentException(
"Attempting to add a listener that is already in use");
}
try {
mService.addVcnUnderlyingNetworkPolicyListener(binder);
} catch (RemoteException e) {
REGISTERED_POLICY_LISTENERS.remove(listener);
throw e.rethrowFromSystemServer();
}
} }
/** /**
@@ -220,19 +197,7 @@ public class VcnManager {
*/ */
public void removeVcnUnderlyingNetworkPolicyListener( public void removeVcnUnderlyingNetworkPolicyListener(
@NonNull VcnUnderlyingNetworkPolicyListener listener) { @NonNull VcnUnderlyingNetworkPolicyListener listener) {
requireNonNull(listener, "listener must not be null"); removeVcnNetworkPolicyListener(listener);
VcnUnderlyingNetworkPolicyListenerBinder binder =
REGISTERED_POLICY_LISTENERS.remove(listener);
if (binder == null) {
return;
}
try {
mService.removeVcnUnderlyingNetworkPolicyListener(binder);
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
} }
/** /**
@@ -266,6 +231,107 @@ public class VcnManager {
} }
} }
// TODO: make VcnNetworkPolicyListener @SystemApi
/**
* VcnNetworkPolicyListener is the interface through which internal system components can
* register to receive updates for VCN-underlying Network policies from the System Server.
*
* @hide
*/
public interface VcnNetworkPolicyListener {
/**
* Notifies the implementation that the VCN's underlying Network policy has changed.
*
* <p>After receiving this callback, implementations should get the current {@link
* VcnNetworkPolicyResult} via {@link #applyVcnNetworkPolicy(NetworkCapabilities,
* LinkProperties)}.
*/
void onPolicyChanged();
}
/**
* Add a listener for VCN-underlying Network policy updates.
*
* @param executor the Executor that will be used for invoking all calls to the specified
* Listener
* @param listener the VcnNetworkPolicyListener to be added
* @throws SecurityException if the caller does not have permission NETWORK_FACTORY
* @throws IllegalStateException if the specified VcnNetworkPolicyListener is already registered
* @hide
*/
@RequiresPermission(android.Manifest.permission.NETWORK_FACTORY)
public void addVcnNetworkPolicyListener(
@NonNull Executor executor, @NonNull VcnNetworkPolicyListener listener) {
requireNonNull(executor, "executor must not be null");
requireNonNull(listener, "listener must not be null");
VcnUnderlyingNetworkPolicyListenerBinder binder =
new VcnUnderlyingNetworkPolicyListenerBinder(executor, listener);
if (REGISTERED_POLICY_LISTENERS.putIfAbsent(listener, binder) != null) {
throw new IllegalStateException("listener is already registered with VcnManager");
}
try {
mService.addVcnUnderlyingNetworkPolicyListener(binder);
} catch (RemoteException e) {
REGISTERED_POLICY_LISTENERS.remove(listener);
throw e.rethrowFromSystemServer();
}
}
/**
* Remove the specified VcnNetworkPolicyListener from VcnManager.
*
* <p>If the specified listener is not currently registered, this is a no-op.
*
* @param listener the VcnNetworkPolicyListener that will be removed
* @hide
*/
public void removeVcnNetworkPolicyListener(@NonNull VcnNetworkPolicyListener listener) {
requireNonNull(listener, "listener must not be null");
VcnUnderlyingNetworkPolicyListenerBinder binder =
REGISTERED_POLICY_LISTENERS.remove(listener);
if (binder == null) {
return;
}
try {
mService.removeVcnUnderlyingNetworkPolicyListener(binder);
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
/**
* Applies the network policy for a {@link android.net.Network} with the given parameters.
*
* <p>Prior to a new NetworkAgent being registered, or upon notification that Carrier VCN policy
* may have changed via {@link VcnNetworkPolicyListener#onPolicyChanged()}, a Network Provider
* MUST poll for the updated Network policy based on that Network's capabilities and properties.
*
* @param networkCapabilities the NetworkCapabilities to be used in determining the Network
* policy result for this Network.
* @param linkProperties the LinkProperties to be used in determining the Network policy result
* for this Network.
* @throws SecurityException if the caller does not have permission NETWORK_FACTORY
* @return the {@link VcnNetworkPolicyResult} to be used for this Network.
* @hide
*/
@NonNull
@RequiresPermission(android.Manifest.permission.NETWORK_FACTORY)
public VcnNetworkPolicyResult applyVcnNetworkPolicy(
@NonNull NetworkCapabilities networkCapabilities,
@NonNull LinkProperties linkProperties) {
requireNonNull(networkCapabilities, "networkCapabilities must not be null");
requireNonNull(linkProperties, "linkProperties must not be null");
final VcnUnderlyingNetworkPolicy policy =
getUnderlyingNetworkPolicy(networkCapabilities, linkProperties);
return new VcnNetworkPolicyResult(
policy.isTeardownRequested(), policy.getMergedNetworkCapabilities());
}
/** @hide */ /** @hide */
@Retention(RetentionPolicy.SOURCE) @Retention(RetentionPolicy.SOURCE)
@IntDef({ @IntDef({
@@ -415,18 +481,17 @@ public class VcnManager {
} }
/** /**
* Binder wrapper for added VcnUnderlyingNetworkPolicyListeners to receive signals from System * Binder wrapper for added VcnNetworkPolicyListeners to receive signals from System Server.
* Server.
* *
* @hide * @hide
*/ */
private static class VcnUnderlyingNetworkPolicyListenerBinder private static class VcnUnderlyingNetworkPolicyListenerBinder
extends IVcnUnderlyingNetworkPolicyListener.Stub { extends IVcnUnderlyingNetworkPolicyListener.Stub {
@NonNull private final Executor mExecutor; @NonNull private final Executor mExecutor;
@NonNull private final VcnUnderlyingNetworkPolicyListener mListener; @NonNull private final VcnNetworkPolicyListener mListener;
private VcnUnderlyingNetworkPolicyListenerBinder( private VcnUnderlyingNetworkPolicyListenerBinder(
Executor executor, VcnUnderlyingNetworkPolicyListener listener) { Executor executor, VcnNetworkPolicyListener listener) {
mExecutor = executor; mExecutor = executor;
mListener = listener; mListener = listener;
} }

View File

@@ -0,0 +1,20 @@
/*
* Copyright (C) 2021 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.net.vcn;
/** @hide */
parcelable VcnNetworkPolicyResult;

View File

@@ -0,0 +1,109 @@
/*
* Copyright (C) 2021 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.net.vcn;
import android.annotation.NonNull;
import android.net.NetworkCapabilities;
import android.os.Parcel;
import android.os.Parcelable;
import java.util.Objects;
/**
* VcnNetworkPolicyResult represents the Network policy result for a Network transport applying its
* VCN policy via {@link VcnManager#applyVcnNetworkPolicy(NetworkCapabilities, LinkProperties)}.
*
* <p>Transports that are bringing up networks capable of acting as a VCN's underlying network
* should query for policy state upon any capability changes (e.g. changing of TRUSTED bit), and
* when prompted by VcnManagementService via VcnNetworkPolicyListener.
*
* @hide
*/
public final class VcnNetworkPolicyResult implements Parcelable {
private final boolean mIsTearDownRequested;
private final NetworkCapabilities mNetworkCapabilities;
/**
* Constructs a VcnNetworkPolicyResult with the specified parameters.
*
* @hide
*/
public VcnNetworkPolicyResult(
boolean isTearDownRequested, @NonNull NetworkCapabilities networkCapabilities) {
Objects.requireNonNull(networkCapabilities, "networkCapabilities must be non-null");
mIsTearDownRequested = isTearDownRequested;
mNetworkCapabilities = networkCapabilities;
}
/**
* Returns whether this Carrier VCN policy policy result requires that the underlying Network
* should be torn down.
*/
public boolean isTeardownRequested() {
return mIsTearDownRequested;
}
/**
* Returns the NetworkCapabilities with Carrier VCN policy bits applied to the provided
* capabilities.
*/
@NonNull
public NetworkCapabilities getNetworkCapabilities() {
return mNetworkCapabilities;
}
@Override
public int hashCode() {
return Objects.hash(mIsTearDownRequested, mNetworkCapabilities);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof VcnNetworkPolicyResult)) return false;
final VcnNetworkPolicyResult that = (VcnNetworkPolicyResult) o;
return mIsTearDownRequested == that.mIsTearDownRequested
&& mNetworkCapabilities.equals(that.mNetworkCapabilities);
}
/** {@inheritDoc} */
@Override
public int describeContents() {
return 0;
}
/** {@inheritDoc} */
@Override
public void writeToParcel(@NonNull Parcel dest, int flags) {
dest.writeBoolean(mIsTearDownRequested);
dest.writeParcelable(mNetworkCapabilities, flags);
}
/** Implement the Parcelable interface */
public static final @NonNull Creator<VcnNetworkPolicyResult> CREATOR =
new Creator<VcnNetworkPolicyResult>() {
public VcnNetworkPolicyResult createFromParcel(Parcel in) {
return new VcnNetworkPolicyResult(in.readBoolean(), in.readParcelable(null));
}
public VcnNetworkPolicyResult[] newArray(int size) {
return new VcnNetworkPolicyResult[size];
}
};
}

View File

@@ -33,8 +33,7 @@ import java.util.Objects;
* @hide * @hide
*/ */
public final class VcnUnderlyingNetworkPolicy implements Parcelable { public final class VcnUnderlyingNetworkPolicy implements Parcelable {
private final boolean mIsTearDownRequested; private final VcnNetworkPolicyResult mVcnNetworkPolicyResult;
private final NetworkCapabilities mMergedNetworkCapabilities;
/** /**
* Constructs a VcnUnderlyingNetworkPolicy with the specified parameters. * Constructs a VcnUnderlyingNetworkPolicy with the specified parameters.
@@ -46,8 +45,13 @@ public final class VcnUnderlyingNetworkPolicy implements Parcelable {
Objects.requireNonNull( Objects.requireNonNull(
mergedNetworkCapabilities, "mergedNetworkCapabilities must be nonnull"); mergedNetworkCapabilities, "mergedNetworkCapabilities must be nonnull");
mIsTearDownRequested = isTearDownRequested; mVcnNetworkPolicyResult =
mMergedNetworkCapabilities = mergedNetworkCapabilities; new VcnNetworkPolicyResult(isTearDownRequested, mergedNetworkCapabilities);
}
private VcnUnderlyingNetworkPolicy(@NonNull VcnNetworkPolicyResult vcnNetworkPolicyResult) {
this.mVcnNetworkPolicyResult =
Objects.requireNonNull(vcnNetworkPolicyResult, "vcnNetworkPolicyResult");
} }
/** /**
@@ -55,7 +59,7 @@ public final class VcnUnderlyingNetworkPolicy implements Parcelable {
* be torn down. * be torn down.
*/ */
public boolean isTeardownRequested() { public boolean isTeardownRequested() {
return mIsTearDownRequested; return mVcnNetworkPolicyResult.isTeardownRequested();
} }
/** /**
@@ -64,12 +68,12 @@ public final class VcnUnderlyingNetworkPolicy implements Parcelable {
*/ */
@NonNull @NonNull
public NetworkCapabilities getMergedNetworkCapabilities() { public NetworkCapabilities getMergedNetworkCapabilities() {
return mMergedNetworkCapabilities; return mVcnNetworkPolicyResult.getNetworkCapabilities();
} }
@Override @Override
public int hashCode() { public int hashCode() {
return Objects.hash(mIsTearDownRequested, mMergedNetworkCapabilities); return Objects.hash(mVcnNetworkPolicyResult);
} }
@Override @Override
@@ -78,8 +82,7 @@ public final class VcnUnderlyingNetworkPolicy implements Parcelable {
if (!(o instanceof VcnUnderlyingNetworkPolicy)) return false; if (!(o instanceof VcnUnderlyingNetworkPolicy)) return false;
final VcnUnderlyingNetworkPolicy that = (VcnUnderlyingNetworkPolicy) o; final VcnUnderlyingNetworkPolicy that = (VcnUnderlyingNetworkPolicy) o;
return mIsTearDownRequested == that.mIsTearDownRequested return mVcnNetworkPolicyResult.equals(that.mVcnNetworkPolicyResult);
&& mMergedNetworkCapabilities.equals(that.mMergedNetworkCapabilities);
} }
/** {@inheritDoc} */ /** {@inheritDoc} */
@@ -91,16 +94,14 @@ public final class VcnUnderlyingNetworkPolicy implements Parcelable {
/** {@inheritDoc} */ /** {@inheritDoc} */
@Override @Override
public void writeToParcel(@NonNull Parcel dest, int flags) { public void writeToParcel(@NonNull Parcel dest, int flags) {
dest.writeBoolean(mIsTearDownRequested); dest.writeParcelable(mVcnNetworkPolicyResult, flags);
dest.writeParcelable(mMergedNetworkCapabilities, flags);
} }
/** Implement the Parcelable interface */ /** Implement the Parcelable interface */
public static final @NonNull Creator<VcnUnderlyingNetworkPolicy> CREATOR = public static final @NonNull Creator<VcnUnderlyingNetworkPolicy> CREATOR =
new Creator<VcnUnderlyingNetworkPolicy>() { new Creator<VcnUnderlyingNetworkPolicy>() {
public VcnUnderlyingNetworkPolicy createFromParcel(Parcel in) { public VcnUnderlyingNetworkPolicy createFromParcel(Parcel in) {
return new VcnUnderlyingNetworkPolicy( return new VcnUnderlyingNetworkPolicy(in.readParcelable(null));
in.readBoolean(), in.readParcelable(null));
} }
public VcnUnderlyingNetworkPolicy[] newArray(int size) { public VcnUnderlyingNetworkPolicy[] newArray(int size) {

View File

@@ -969,7 +969,7 @@ public class VcnGatewayConnection extends StateMachine {
mGatewayStatusCallback.onGatewayConnectionError( mGatewayStatusCallback.onGatewayConnectionError(
mConnectionConfig.getRequiredUnderlyingCapabilities(), mConnectionConfig.getRequiredUnderlyingCapabilities(),
VCN_ERROR_CODE_INTERNAL_ERROR, VCN_ERROR_CODE_INTERNAL_ERROR,
"java.lang.RuntimeException", RuntimeException.class.getName(),
"Received " "Received "
+ exception.getClass().getSimpleName() + exception.getClass().getSimpleName()
+ " with message: " + " with message: "
@@ -991,11 +991,11 @@ public class VcnGatewayConnection extends StateMachine {
} else if (exception instanceof IkeInternalException } else if (exception instanceof IkeInternalException
&& exception.getCause() instanceof IOException) { && exception.getCause() instanceof IOException) {
errorCode = VCN_ERROR_CODE_NETWORK_ERROR; errorCode = VCN_ERROR_CODE_NETWORK_ERROR;
exceptionClass = "java.io.IOException"; exceptionClass = IOException.class.getName();
exceptionMessage = exception.getCause().getMessage(); exceptionMessage = exception.getCause().getMessage();
} else { } else {
errorCode = VCN_ERROR_CODE_INTERNAL_ERROR; errorCode = VCN_ERROR_CODE_INTERNAL_ERROR;
exceptionClass = "java.lang.RuntimeException"; exceptionClass = RuntimeException.class.getName();
exceptionMessage = exceptionMessage =
"Received " "Received "
+ exception.getClass().getSimpleName() + exception.getClass().getSimpleName()

View File

@@ -207,7 +207,7 @@ public class VcnManagerTest {
cbBinder.onGatewayConnectionError( cbBinder.onGatewayConnectionError(
UNDERLYING_NETWORK_CAPABILITIES, UNDERLYING_NETWORK_CAPABILITIES,
VcnManager.VCN_ERROR_CODE_NETWORK_ERROR, VcnManager.VCN_ERROR_CODE_NETWORK_ERROR,
"java.net.UnknownHostException", UnknownHostException.class.getName(),
"exception_message"); "exception_message");
verify(mMockStatusCallback) verify(mMockStatusCallback)
.onGatewayConnectionError( .onGatewayConnectionError(

View File

@@ -46,6 +46,6 @@ public class VcnUnderlyingNetworkPolicyTest {
@Test @Test
public void testParcelUnparcel() { public void testParcelUnparcel() {
assertParcelSane(SAMPLE_NETWORK_POLICY, 2); assertParcelSane(SAMPLE_NETWORK_POLICY, 1);
} }
} }