| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
package android.net.vcn.persistablebundleutils; |
| 17 |
|
| 18 |
import android.annotation.NonNull; |
| 19 |
import android.net.ipsec.ike.IkeSessionParams; |
| 20 |
import android.net.ipsec.ike.IkeTunnelConnectionParams; |
| 21 |
import android.net.ipsec.ike.TunnelModeChildSessionParams; |
| 22 |
import android.os.PersistableBundle; |
| 23 |
|
| 24 |
import java.util.Objects; |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
public final class TunnelConnectionParamsUtils { |
| 32 |
private static final int EXPECTED_BUNDLE_KEY_CNT = 1; |
| 33 |
|
| 34 |
private static final String PARAMS_TYPE_IKE = "IKE"; |
| 35 |
|
| 36 |
|
| 37 |
@NonNull |
| 38 |
public static PersistableBundle toPersistableBundle(@NonNull IkeTunnelConnectionParams params) { |
| 39 |
final PersistableBundle result = new PersistableBundle(); |
| 40 |
|
| 41 |
result.putPersistableBundle( |
| 42 |
PARAMS_TYPE_IKE, |
| 43 |
IkeTunnelConnectionParamsUtils.serializeIkeParams( |
| 44 |
(IkeTunnelConnectionParams) params)); |
| 45 |
return result; |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
@NonNull |
| 50 |
public static IkeTunnelConnectionParams fromPersistableBundle(@NonNull PersistableBundle in) { |
| 51 |
Objects.requireNonNull(in, "PersistableBundle was null"); |
| 52 |
|
| 53 |
if (in.keySet().size() != EXPECTED_BUNDLE_KEY_CNT) { |
| 54 |
throw new IllegalArgumentException( |
| 55 |
String.format( |
| 56 |
"Expect PersistableBundle to have %d element but found: %s", |
| 57 |
EXPECTED_BUNDLE_KEY_CNT, in.keySet())); |
| 58 |
} |
| 59 |
|
| 60 |
if (in.get(PARAMS_TYPE_IKE) != null) { |
| 61 |
return IkeTunnelConnectionParamsUtils.deserializeIkeParams( |
| 62 |
in.getPersistableBundle(PARAMS_TYPE_IKE)); |
| 63 |
} |
| 64 |
|
| 65 |
throw new IllegalArgumentException( |
| 66 |
"Invalid Tunnel Connection Params type " + in.keySet().iterator().next()); |
| 67 |
} |
| 68 |
|
| 69 |
private static final class IkeTunnelConnectionParamsUtils { |
| 70 |
private static final String IKE_PARAMS_KEY = "IKE_PARAMS_KEY"; |
| 71 |
private static final String CHILD_PARAMS_KEY = "CHILD_PARAMS_KEY"; |
| 72 |
|
| 73 |
@NonNull |
| 74 |
public static PersistableBundle serializeIkeParams( |
| 75 |
@NonNull IkeTunnelConnectionParams ikeParams) { |
| 76 |
final PersistableBundle result = new PersistableBundle(); |
| 77 |
|
| 78 |
result.putPersistableBundle( |
| 79 |
IKE_PARAMS_KEY, |
| 80 |
IkeSessionParamsUtils.toPersistableBundle(ikeParams.getIkeSessionParams())); |
| 81 |
result.putPersistableBundle( |
| 82 |
CHILD_PARAMS_KEY, |
| 83 |
TunnelModeChildSessionParamsUtils.toPersistableBundle( |
| 84 |
ikeParams.getTunnelModeChildSessionParams())); |
| 85 |
return result; |
| 86 |
} |
| 87 |
|
| 88 |
@NonNull |
| 89 |
public static IkeTunnelConnectionParams deserializeIkeParams( |
| 90 |
@NonNull PersistableBundle in) { |
| 91 |
final PersistableBundle ikeBundle = in.getPersistableBundle(IKE_PARAMS_KEY); |
| 92 |
final PersistableBundle childBundle = in.getPersistableBundle(CHILD_PARAMS_KEY); |
| 93 |
Objects.requireNonNull(ikeBundle, "IkeSessionParams was null"); |
| 94 |
Objects.requireNonNull(ikeBundle, "TunnelModeChildSessionParams was null"); |
| 95 |
|
| 96 |
final IkeSessionParams ikeParams = |
| 97 |
IkeSessionParamsUtils.fromPersistableBundle(ikeBundle); |
| 98 |
final TunnelModeChildSessionParams childParams = |
| 99 |
TunnelModeChildSessionParamsUtils.fromPersistableBundle(childBundle); |
| 100 |
return new IkeTunnelConnectionParams(ikeParams, childParams); |
| 101 |
} |
| 102 |
} |
| 103 |
} |