blob: 0d43f12e6537235d6962ac443ccd00d4de7f070d [file] [log] [blame]
Hugo Benichicfbf7412016-06-23 10:41:30 +09001/*
2 * Copyright (C) 2016 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
17package android.net.metrics;
18
Remi NGUYEN VAN39fbb922019-01-24 00:55:43 +090019import android.annotation.SystemApi;
20import android.annotation.TestApi;
Hugo Benichicfbf7412016-06-23 10:41:30 +090021import android.os.Parcel;
22import android.os.Parcelable;
23
24/**
25 * An event logged when the APF packet socket receives an RA packet.
26 * {@hide}
27 */
Remi NGUYEN VAN39fbb922019-01-24 00:55:43 +090028@SystemApi
29@TestApi
Remi NGUYEN VAN7b84fb32019-01-19 21:13:24 +090030public final class RaEvent implements IpConnectivityLog.Event {
Hugo Benichicfbf7412016-06-23 10:41:30 +090031
Remi NGUYEN VAN39fbb922019-01-24 00:55:43 +090032 private static final long NO_LIFETIME = -1L;
Hugo Benichi6ccd51a2016-07-04 09:22:30 +090033
Hugo Benichicfbf7412016-06-23 10:41:30 +090034 // Lifetime in seconds of options found in a single RA packet.
35 // When an option is not set, the value of the associated field is -1;
Remi NGUYEN VAN39fbb922019-01-24 00:55:43 +090036 /** @hide */
Hugo Benichicfbf7412016-06-23 10:41:30 +090037 public final long routerLifetime;
Remi NGUYEN VAN39fbb922019-01-24 00:55:43 +090038 /** @hide */
Hugo Benichicfbf7412016-06-23 10:41:30 +090039 public final long prefixValidLifetime;
Remi NGUYEN VAN39fbb922019-01-24 00:55:43 +090040 /** @hide */
Hugo Benichicfbf7412016-06-23 10:41:30 +090041 public final long prefixPreferredLifetime;
Remi NGUYEN VAN39fbb922019-01-24 00:55:43 +090042 /** @hide */
Hugo Benichicfbf7412016-06-23 10:41:30 +090043 public final long routeInfoLifetime;
Remi NGUYEN VAN39fbb922019-01-24 00:55:43 +090044 /** @hide */
Hugo Benichicfbf7412016-06-23 10:41:30 +090045 public final long rdnssLifetime;
Remi NGUYEN VAN39fbb922019-01-24 00:55:43 +090046 /** @hide */
Hugo Benichicfbf7412016-06-23 10:41:30 +090047 public final long dnsslLifetime;
48
Remi NGUYEN VAN39fbb922019-01-24 00:55:43 +090049 /** @hide */
Hugo Benichicfbf7412016-06-23 10:41:30 +090050 public RaEvent(long routerLifetime, long prefixValidLifetime, long prefixPreferredLifetime,
51 long routeInfoLifetime, long rdnssLifetime, long dnsslLifetime) {
52 this.routerLifetime = routerLifetime;
53 this.prefixValidLifetime = prefixValidLifetime;
54 this.prefixPreferredLifetime = prefixPreferredLifetime;
55 this.routeInfoLifetime = routeInfoLifetime;
56 this.rdnssLifetime = rdnssLifetime;
57 this.dnsslLifetime = dnsslLifetime;
58 }
59
Remi NGUYEN VAN39fbb922019-01-24 00:55:43 +090060 /** @hide */
Hugo Benichicfbf7412016-06-23 10:41:30 +090061 private RaEvent(Parcel in) {
62 routerLifetime = in.readLong();
63 prefixValidLifetime = in.readLong();
64 prefixPreferredLifetime = in.readLong();
65 routeInfoLifetime = in.readLong();
66 rdnssLifetime = in.readLong();
67 dnsslLifetime = in.readLong();
68 }
69
Remi NGUYEN VAN39fbb922019-01-24 00:55:43 +090070 /** @hide */
Hugo Benichicfbf7412016-06-23 10:41:30 +090071 @Override
72 public void writeToParcel(Parcel out, int flags) {
73 out.writeLong(routerLifetime);
74 out.writeLong(prefixValidLifetime);
75 out.writeLong(prefixPreferredLifetime);
76 out.writeLong(routeInfoLifetime);
77 out.writeLong(rdnssLifetime);
78 out.writeLong(dnsslLifetime);
79 }
80
Remi NGUYEN VAN39fbb922019-01-24 00:55:43 +090081 /** @hide */
Hugo Benichicfbf7412016-06-23 10:41:30 +090082 @Override
83 public int describeContents() {
84 return 0;
85 }
86
87 @Override
88 public String toString() {
89 return new StringBuilder("RaEvent(lifetimes: ")
90 .append(String.format("router=%ds, ", routerLifetime))
91 .append(String.format("prefix_valid=%ds, ", prefixValidLifetime))
92 .append(String.format("prefix_preferred=%ds, ", prefixPreferredLifetime))
93 .append(String.format("route_info=%ds, ", routeInfoLifetime))
94 .append(String.format("rdnss=%ds, ", rdnssLifetime))
95 .append(String.format("dnssl=%ds)", dnsslLifetime))
96 .toString();
97 }
98
Remi NGUYEN VAN39fbb922019-01-24 00:55:43 +090099 /** @hide */
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -0700100 public static final @android.annotation.NonNull Parcelable.Creator<RaEvent> CREATOR = new Parcelable.Creator<RaEvent>() {
Hugo Benichicfbf7412016-06-23 10:41:30 +0900101 public RaEvent createFromParcel(Parcel in) {
102 return new RaEvent(in);
103 }
104
105 public RaEvent[] newArray(int size) {
106 return new RaEvent[size];
107 }
108 };
Hugo Benichi6ccd51a2016-07-04 09:22:30 +0900109
Hugo Benichi6ccd51a2016-07-04 09:22:30 +0900110 public static class Builder {
111
112 long routerLifetime = NO_LIFETIME;
113 long prefixValidLifetime = NO_LIFETIME;
114 long prefixPreferredLifetime = NO_LIFETIME;
115 long routeInfoLifetime = NO_LIFETIME;
116 long rdnssLifetime = NO_LIFETIME;
117 long dnsslLifetime = NO_LIFETIME;
118
119 public Builder() {
120 }
121
122 public RaEvent build() {
123 return new RaEvent(routerLifetime, prefixValidLifetime, prefixPreferredLifetime,
124 routeInfoLifetime, rdnssLifetime, dnsslLifetime);
125 }
126
127 public Builder updateRouterLifetime(long lifetime) {
128 routerLifetime = updateLifetime(routerLifetime, lifetime);
129 return this;
130 }
131
132 public Builder updatePrefixValidLifetime(long lifetime) {
133 prefixValidLifetime = updateLifetime(prefixValidLifetime, lifetime);
134 return this;
135 }
136
137 public Builder updatePrefixPreferredLifetime(long lifetime) {
138 prefixPreferredLifetime = updateLifetime(prefixPreferredLifetime, lifetime);
139 return this;
140 }
141
142 public Builder updateRouteInfoLifetime(long lifetime) {
143 routeInfoLifetime = updateLifetime(routeInfoLifetime, lifetime);
144 return this;
145 }
146
147 public Builder updateRdnssLifetime(long lifetime) {
148 rdnssLifetime = updateLifetime(rdnssLifetime, lifetime);
149 return this;
150 }
151
152 public Builder updateDnsslLifetime(long lifetime) {
153 dnsslLifetime = updateLifetime(dnsslLifetime, lifetime);
154 return this;
155 }
156
157 private long updateLifetime(long currentLifetime, long newLifetime) {
158 if (currentLifetime == RaEvent.NO_LIFETIME) {
159 return newLifetime;
160 }
161 return Math.min(currentLifetime, newLifetime);
162 }
163 }
Hugo Benichicfbf7412016-06-23 10:41:30 +0900164}