1 /*
2  * Copyright 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.google.android.iwlan.epdg;
18 
19 import android.net.LinkAddress;
20 import android.telephony.data.ApnSetting;
21 import android.telephony.data.NetworkSliceInfo;
22 import android.util.Log;
23 
24 import com.google.auto.value.AutoValue;
25 
26 import java.net.InetAddress;
27 import java.util.List;
28 import java.util.Optional;
29 
30 @AutoValue
31 public abstract class TunnelLinkProperties {
32     private static final String TAG = TunnelLinkProperties.class.getSimpleName();
33 
internalAddresses()34     public abstract List<LinkAddress> internalAddresses();
35 
dnsAddresses()36     public abstract List<InetAddress> dnsAddresses();
37 
pcscfAddresses()38     public abstract List<InetAddress> pcscfAddresses();
39 
ifaceName()40     public abstract String ifaceName();
41 
sliceInfo()42     public abstract Optional<NetworkSliceInfo> sliceInfo();
43 
getProtocolType()44     public int getProtocolType() {
45         int protocolType = ApnSetting.PROTOCOL_UNKNOWN;
46         if (!internalAddresses().isEmpty()) {
47             boolean hasIpv4 = internalAddresses().stream().anyMatch(LinkAddress::isIpv4);
48             boolean hasIpv6 = internalAddresses().stream().anyMatch(LinkAddress::isIpv6);
49 
50             if (hasIpv4 && hasIpv6) {
51                 protocolType = ApnSetting.PROTOCOL_IPV4V6;
52             } else if (hasIpv4) {
53                 protocolType = ApnSetting.PROTOCOL_IP;
54             } else if (hasIpv6) {
55                 protocolType = ApnSetting.PROTOCOL_IPV6;
56             } else {
57                 Log.w(
58                         TAG,
59                         "Internal Addresses do not contain IPv4 or IPv6 addresses, set protocol"
60                                 + " type as: "
61                                 + protocolType);
62             }
63         } else {
64             Log.w(TAG, "Internal Addresses is empty, set protocol type as: " + protocolType);
65         }
66         return protocolType;
67     }
68 
builder()69     public static Builder builder() {
70         return new AutoValue_TunnelLinkProperties.Builder().setSliceInfo(Optional.empty());
71     }
72 
73     @AutoValue.Builder
74     public abstract static class Builder {
setInternalAddresses(List<LinkAddress> internalAddresses)75         public abstract Builder setInternalAddresses(List<LinkAddress> internalAddresses);
76 
setDnsAddresses(List<InetAddress> dnsAddresses)77         public abstract Builder setDnsAddresses(List<InetAddress> dnsAddresses);
78 
setPcscfAddresses(List<InetAddress> pcscfAddresses)79         public abstract Builder setPcscfAddresses(List<InetAddress> pcscfAddresses);
80 
setIfaceName(String ifaceName)81         public abstract Builder setIfaceName(String ifaceName);
82 
setSliceInfo(NetworkSliceInfo si)83         public Builder setSliceInfo(NetworkSliceInfo si) {
84             return setSliceInfo(Optional.ofNullable(si));
85         }
86 
setSliceInfo(Optional<NetworkSliceInfo> si)87         public abstract Builder setSliceInfo(Optional<NetworkSliceInfo> si);
88 
build()89         public abstract TunnelLinkProperties build();
90     }
91 }
92