Merge "Add util method to convert StaticIpConfiguration from proto" into main
This commit is contained in:
commit
32d4c35ae8
2 changed files with 81 additions and 0 deletions
|
|
@ -32,7 +32,11 @@ import com.android.server.network.configstore.proto.NetworkConfigStoreProto.Link
|
|||
import com.android.server.network.configstore.proto.NetworkConfigStoreProto.MeteredOverrideProto;
|
||||
import com.android.server.network.configstore.proto.NetworkConfigStoreProto.StaticIpv4ConfigurationProto;
|
||||
|
||||
import java.net.Inet4Address;
|
||||
import java.net.InetAddress;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* Static util methods for {@link ProtoConfig}.
|
||||
|
|
@ -173,4 +177,45 @@ public class ProtoConfigUtils {
|
|||
}
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts static IP configuration from a {@link StaticIpv4ConfigurationProto} proto object and
|
||||
* converts it to a corresponding {@link StaticIpConfiguration} framework object.
|
||||
*
|
||||
* @throws IllegalArgumentException if the {@code proto} does not contain a valid IPv4 address,
|
||||
* or any gateway/DNS server address is invalid.
|
||||
*/
|
||||
public static StaticIpConfiguration convertStaticIpConfigurationFromProto(
|
||||
StaticIpv4ConfigurationProto proto) {
|
||||
requireNonNull(proto, "Static IP configuration proto object cannot be null.");
|
||||
|
||||
final StaticIpConfiguration.Builder builder = new StaticIpConfiguration.Builder();
|
||||
|
||||
final LinkAddress linkAddr = convertLinkAddressFromProto(proto.getAddress());
|
||||
if (linkAddr.getAddress() instanceof Inet4Address) {
|
||||
builder.setIpAddress(linkAddr);
|
||||
} else {
|
||||
throw new IllegalArgumentException("Non-IPv4 link address, conversion failed.");
|
||||
}
|
||||
|
||||
if (proto.hasGateway()) {
|
||||
builder.setGateway(InetAddresses.parseNumericAddress(proto.getGateway()));
|
||||
}
|
||||
|
||||
final List<InetAddress> dnsServers = new ArrayList<>();
|
||||
for (String dnsServer : proto.getDnsServersList()) {
|
||||
dnsServers.add(InetAddresses.parseNumericAddress(dnsServer));
|
||||
}
|
||||
builder.setDnsServers(dnsServers);
|
||||
|
||||
// Filter out empty search domains.
|
||||
final String domains = proto.getSearchDomainsList().stream()
|
||||
.filter(domain -> !domain.isEmpty())
|
||||
.collect(Collectors.joining(","));
|
||||
if (!domains.isEmpty()) {
|
||||
builder.setDomains(domains);
|
||||
}
|
||||
|
||||
return builder.build();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -205,4 +205,40 @@ public class ProtoConfigUtilsTest {
|
|||
ProtoConfigUtils.convertStaticIpConfigurationToProto(null);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConvertProtoToStaticIpConfiguration() {
|
||||
LinkAddressProto linkAddrProto =
|
||||
LinkAddressProto.newBuilder()
|
||||
.setAddress(IP_ADDRESS_STRING)
|
||||
.setPrefixLength(PREFIX_LENGTH)
|
||||
.build();
|
||||
StaticIpv4ConfigurationProto staticIpConfigProto =
|
||||
StaticIpv4ConfigurationProto.newBuilder()
|
||||
.setAddress(linkAddrProto)
|
||||
.addDnsServers(DNS_IP_ADDR_1)
|
||||
.addDnsServers(DNS_IP_ADDR_2)
|
||||
.addSearchDomains(DOMAIN1)
|
||||
.addSearchDomains(DOMAIN2)
|
||||
.build();
|
||||
|
||||
StaticIpConfiguration actualConfig =
|
||||
ProtoConfigUtils.convertStaticIpConfigurationFromProto(staticIpConfigProto);
|
||||
|
||||
StaticIpConfiguration expectedConfig = new StaticIpConfiguration.Builder()
|
||||
.setIpAddress(new LinkAddress(LINK_ADDRESS_STRING))
|
||||
.setDnsServers(DNS_SERVERS)
|
||||
.setDomains(DOMAIN1 + "," + DOMAIN2)
|
||||
.build();
|
||||
|
||||
assertEquals(expectedConfig, actualConfig);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConvertProtoToStaticIpConfiguration_nullInput() {
|
||||
assertThrows("Static IP configuration proto object cannot be null.",
|
||||
NullPointerException.class, () -> {
|
||||
ProtoConfigUtils.convertStaticIpConfigurationFromProto(null);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue