Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 80cde857 authored by Cody Kesting's avatar Cody Kesting
Browse files

Define add and remove for VCN policy listeners.

Bug: 175729529
Test: atest FrameworksVcnTests
Change-Id: I61dd789fe4a4ebd1ca95866494d0e42e97c202cc
parent 90f27fc8
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@

package android.net.vcn;

import android.net.vcn.IVcnUnderlyingNetworkPolicyListener;
import android.net.vcn.VcnConfig;
import android.os.ParcelUuid;

@@ -25,4 +26,7 @@ import android.os.ParcelUuid;
interface IVcnManagementService {
    void setVcnConfig(in ParcelUuid subscriptionGroup, in VcnConfig config, in String opPkgName);
    void clearVcnConfig(in ParcelUuid subscriptionGroup);

    void addVcnUnderlyingNetworkPolicyListener(in IVcnUnderlyingNetworkPolicyListener listener);
    void removeVcnUnderlyingNetworkPolicyListener(in IVcnUnderlyingNetworkPolicyListener listener);
}
+22 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2020 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 */
interface IVcnUnderlyingNetworkPolicyListener {
    void onPolicyChanged();
}
 No newline at end of file
+107 −0
Original line number Diff line number Diff line
@@ -25,7 +25,12 @@ import android.os.ParcelUuid;
import android.os.RemoteException;
import android.os.ServiceSpecificException;

import com.android.internal.annotations.VisibleForTesting;

import java.io.IOException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executor;

/**
 * VcnManager publishes APIs for applications to configure and manage Virtual Carrier Networks.
@@ -60,6 +65,11 @@ import java.io.IOException;
public final class VcnManager {
    @NonNull private static final String TAG = VcnManager.class.getSimpleName();

    @VisibleForTesting
    public static final Map<
                    VcnUnderlyingNetworkPolicyListener, VcnUnderlyingNetworkPolicyListenerBinder>
            REGISTERED_POLICY_LISTENERS = new ConcurrentHashMap<>();

    @NonNull private final Context mContext;
    @NonNull private final IVcnManagementService mService;

@@ -136,4 +146,101 @@ public final class VcnManager {
            throw e.rethrowFromSystemServer();
        }
    }

    // TODO: make VcnUnderlyingNetworkPolicyListener @SystemApi
    /**
     * VcnUnderlyingNetworkPolicyListener 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 VcnUnderlyingNetworkPolicyListener {
        /**
         * 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.
     *
     * @param executor the Executor that will be used for invoking all calls to the specified
     *     Listener
     * @param listener the VcnUnderlyingNetworkPolicyListener to be added
     * @throws SecurityException if the caller does not have permission NETWORK_FACTORY
     * @throws IllegalArgumentException if the specified VcnUnderlyingNetworkPolicyListener is
     *     already registered
     * @hide
     */
    @RequiresPermission(android.Manifest.permission.NETWORK_FACTORY)
    public void addVcnUnderlyingNetworkPolicyListener(
            @NonNull Executor executor, @NonNull VcnUnderlyingNetworkPolicyListener 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 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();
        }
    }

    /**
     * Remove the specified VcnUnderlyingNetworkPolicyListener from VcnManager.
     *
     * <p>If the specified listener is not currently registered, this is a no-op.
     *
     * @param listener the VcnUnderlyingNetworkPolicyListener that will be removed
     * @hide
     */
    public void removeVcnUnderlyingNetworkPolicyListener(
            @NonNull VcnUnderlyingNetworkPolicyListener 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();
        }
    }

    /**
     * Binder wrapper for added VcnUnderlyingNetworkPolicyListeners to receive signals from System
     * Server.
     *
     * @hide
     */
    private static class VcnUnderlyingNetworkPolicyListenerBinder
            extends IVcnUnderlyingNetworkPolicyListener.Stub {
        @NonNull private final Executor mExecutor;
        @NonNull private final VcnUnderlyingNetworkPolicyListener mListener;

        private VcnUnderlyingNetworkPolicyListenerBinder(
                Executor executor, VcnUnderlyingNetworkPolicyListener listener) {
            mExecutor = executor;
            mListener = listener;
        }

        @Override
        public void onPolicyChanged() {
            mExecutor.execute(() -> mListener.onPolicyChanged());
        }
    }
}
+17 −0
Original line number Diff line number Diff line
@@ -26,6 +26,7 @@ import android.app.AppOpsManager;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.vcn.IVcnManagementService;
import android.net.vcn.IVcnUnderlyingNetworkPolicyListener;
import android.net.vcn.VcnConfig;
import android.os.Binder;
import android.os.Handler;
@@ -495,4 +496,20 @@ public class VcnManagementService extends IVcnManagementService.Stub {
            return Collections.unmodifiableMap(mVcns);
        }
    }

    /** Adds the provided listener for receiving VcnUnderlyingNetworkPolicy updates. */
    @Override
    public void addVcnUnderlyingNetworkPolicyListener(
            IVcnUnderlyingNetworkPolicyListener listener) {
        // TODO(b/175739863): implement policy listener registration
        throw new UnsupportedOperationException("Not yet implemented");
    }

    /** Removes the provided listener from receiving VcnUnderlyingNetworkPolicy updates. */
    @Override
    public void removeVcnUnderlyingNetworkPolicyListener(
            IVcnUnderlyingNetworkPolicyListener listener) {
        // TODO(b/175739863): implement policy listener unregistration
        throw new UnsupportedOperationException("Not yet implemented");
    }
}
+106 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2020 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 static androidx.test.InstrumentationRegistry.getContext;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;

import android.content.Context;
import android.net.vcn.VcnManager.VcnUnderlyingNetworkPolicyListener;

import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;

import java.util.concurrent.Executor;

public class VcnManagerTest {
    private static final Executor INLINE_EXECUTOR = Runnable::run;

    private IVcnManagementService mMockVcnManagementService;
    private VcnUnderlyingNetworkPolicyListener mMockPolicyListener;

    private Context mContext;
    private VcnManager mVcnManager;

    @Before
    public void setUp() {
        mMockVcnManagementService = mock(IVcnManagementService.class);
        mMockPolicyListener = mock(VcnUnderlyingNetworkPolicyListener.class);

        mContext = getContext();
        mVcnManager = new VcnManager(mContext, mMockVcnManagementService);
    }

    @Test
    public void testAddVcnUnderlyingNetworkPolicyListener() throws Exception {
        mVcnManager.addVcnUnderlyingNetworkPolicyListener(INLINE_EXECUTOR, mMockPolicyListener);

        ArgumentCaptor<IVcnUnderlyingNetworkPolicyListener> captor =
                ArgumentCaptor.forClass(IVcnUnderlyingNetworkPolicyListener.class);
        verify(mMockVcnManagementService).addVcnUnderlyingNetworkPolicyListener(captor.capture());

        assertTrue(VcnManager.REGISTERED_POLICY_LISTENERS.containsKey(mMockPolicyListener));

        IVcnUnderlyingNetworkPolicyListener listenerWrapper = captor.getValue();
        listenerWrapper.onPolicyChanged();
        verify(mMockPolicyListener).onPolicyChanged();
    }

    @Test
    public void testRemoveVcnUnderlyingNetworkPolicyListener() throws Exception {
        mVcnManager.addVcnUnderlyingNetworkPolicyListener(INLINE_EXECUTOR, mMockPolicyListener);

        mVcnManager.removeVcnUnderlyingNetworkPolicyListener(mMockPolicyListener);

        assertFalse(VcnManager.REGISTERED_POLICY_LISTENERS.containsKey(mMockPolicyListener));
        verify(mMockVcnManagementService)
                .addVcnUnderlyingNetworkPolicyListener(
                        any(IVcnUnderlyingNetworkPolicyListener.class));
    }

    @Test
    public void testRemoveVcnUnderlyingNetworkPolicyListenerUnknownListener() throws Exception {
        mVcnManager.removeVcnUnderlyingNetworkPolicyListener(mMockPolicyListener);

        assertFalse(VcnManager.REGISTERED_POLICY_LISTENERS.containsKey(mMockPolicyListener));
        verify(mMockVcnManagementService, never())
                .addVcnUnderlyingNetworkPolicyListener(
                        any(IVcnUnderlyingNetworkPolicyListener.class));
    }

    @Test(expected = NullPointerException.class)
    public void testAddVcnUnderlyingNetworkPolicyListenerNullExecutor() throws Exception {
        mVcnManager.addVcnUnderlyingNetworkPolicyListener(null, mMockPolicyListener);
    }

    @Test(expected = NullPointerException.class)
    public void testAddVcnUnderlyingNetworkPolicyListenerNullListener() throws Exception {
        mVcnManager.addVcnUnderlyingNetworkPolicyListener(INLINE_EXECUTOR, null);
    }

    @Test(expected = NullPointerException.class)
    public void testRemoveVcnUnderlyingNetworkPolicyListenerNullListener() {
        mVcnManager.removeVcnUnderlyingNetworkPolicyListener(null);
    }
}