1 /*
2  * Copyright (C) 2022 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 package com.google.android.iwlan;
17 
18 import android.support.annotation.NonNull;
19 import android.support.annotation.Nullable;
20 
21 import com.google.android.iwlan.IwlanDataService.IwlanDataServiceProvider;
22 
23 import java.net.InetAddress;
24 import java.util.Objects;
25 
26 public interface TunnelMetricsInterface {
27     /** Called for logging the tunnel is opened. */
onOpened(OnOpenedMetrics metricsData)28     void onOpened(OnOpenedMetrics metricsData);
29     /** Called for logging the tunnel is closed or bring up failed. */
onClosed(OnClosedMetrics metricsData)30     void onClosed(OnClosedMetrics metricsData);
31 
32     class TunnelMetricsData {
33         private final String mApnName;
34         private final String mEpdgServerAddress;
35         private final int mEpdgServerSelectionDuration;
36         private final int mIkeTunnelEstablishmentDuration;
37         private IwlanDataServiceProvider mIwlanDataServiceProvider;
38         private final boolean mIsNetworkValidated;
39 
TunnelMetricsData(Builder builder)40         protected TunnelMetricsData(Builder builder) {
41             this.mApnName = builder.mApnName;
42             this.mEpdgServerAddress = builder.mEpdgServerAddress;
43             this.mEpdgServerSelectionDuration = builder.mEpdgServerSelectionDuration;
44             this.mIkeTunnelEstablishmentDuration = builder.mIkeTunnelEstablishmentDuration;
45             this.mIsNetworkValidated = builder.mIsNetworkValidated;
46         }
47 
48         @Nullable
getApnName()49         public String getApnName() {
50             return mApnName;
51         }
52 
53         @Nullable
getEpdgServerAddress()54         public String getEpdgServerAddress() {
55             return mEpdgServerAddress;
56         }
57 
getEpdgServerSelectionDuration()58         public int getEpdgServerSelectionDuration() {
59             return mEpdgServerSelectionDuration;
60         }
61 
getIkeTunnelEstablishmentDuration()62         public int getIkeTunnelEstablishmentDuration() {
63             return mIkeTunnelEstablishmentDuration;
64         }
65 
getIwlanDataServiceProvider()66         public IwlanDataServiceProvider getIwlanDataServiceProvider() {
67             return mIwlanDataServiceProvider;
68         }
69 
setIwlanDataServiceProvider(IwlanDataServiceProvider dsp)70         public void setIwlanDataServiceProvider(IwlanDataServiceProvider dsp) {
71             mIwlanDataServiceProvider = dsp;
72         }
73 
isNetworkValidated()74         public boolean isNetworkValidated() {
75             return mIsNetworkValidated;
76         }
77 
78         public static class Builder<T extends Builder> {
79             @Nullable private String mApnName = null;
80             @Nullable private String mEpdgServerAddress = null;
81             private int mEpdgServerSelectionDuration = 0;
82             private int mIkeTunnelEstablishmentDuration = 0;
83             private boolean mIsNetworkValidated = false;
84 
85             /** Default constructor for Builder. */
Builder()86             public Builder() {}
87 
setApnName(@onNull String apnName)88             public T setApnName(@NonNull String apnName) {
89                 mApnName = Objects.requireNonNull(apnName, "apnName must not be null");
90                 return (T) this;
91             }
92 
setEpdgServerAddress(InetAddress epdgAddress)93             public T setEpdgServerAddress(InetAddress epdgAddress) {
94                 mEpdgServerAddress = epdgAddress == null ? null : epdgAddress.getHostAddress();
95                 return (T) this;
96             }
97 
setEpdgServerSelectionDuration(int epdgServerSelectionDuration)98             public T setEpdgServerSelectionDuration(int epdgServerSelectionDuration) {
99                 mEpdgServerSelectionDuration = epdgServerSelectionDuration;
100                 return (T) this;
101             }
102 
setIkeTunnelEstablishmentDuration(int ikeTunnelEstablishmentDuration)103             public T setIkeTunnelEstablishmentDuration(int ikeTunnelEstablishmentDuration) {
104                 mIkeTunnelEstablishmentDuration = ikeTunnelEstablishmentDuration;
105                 return (T) this;
106             }
107 
108             /** whether underlying network is validated */
setIsNetworkValidated(boolean isNetworkValidated)109             public T setIsNetworkValidated(boolean isNetworkValidated) {
110                 mIsNetworkValidated = isNetworkValidated;
111                 return (T) this;
112             }
113 
build()114             public TunnelMetricsData build() {
115                 if (mApnName == null) {
116                     throw new IllegalArgumentException("Necessary parameter missing.");
117                 }
118                 return new TunnelMetricsData(this);
119             }
120         }
121     }
122 
123     class OnOpenedMetrics extends TunnelMetricsData {
124 
OnOpenedMetrics(Builder builder)125         protected OnOpenedMetrics(Builder builder) {
126             super(builder);
127         }
128 
129         public static class Builder extends TunnelMetricsData.Builder<Builder> {
130 
Builder()131             public Builder() {}
132 
build()133             public OnOpenedMetrics build() {
134                 return new OnOpenedMetrics(this);
135             }
136         }
137     }
138 
139     class OnClosedMetrics extends TunnelMetricsData {
140 
OnClosedMetrics(Builder builder)141         protected OnClosedMetrics(Builder builder) {
142             super(builder);
143         }
144 
145         public static class Builder extends TunnelMetricsData.Builder<Builder> {
146 
Builder()147             public Builder() {}
148 
build()149             public OnClosedMetrics build() {
150                 return new OnClosedMetrics(this);
151             }
152         }
153     }
154 }
155