Home | History | Annotate | Download | only in metrics
      1 /*
      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 
     17 package android.net.metrics;
     18 
     19 import android.annotation.SystemApi;
     20 import android.os.Parcel;
     21 import android.os.Parcelable;
     22 
     23 /**
     24  * An event logged when the APF packet socket receives an RA packet.
     25  * {@hide}
     26  */
     27 @SystemApi
     28 public final class RaEvent implements Parcelable {
     29 
     30     /** {@hide} */
     31     public static final long NO_LIFETIME = -1L;
     32 
     33     // Lifetime in seconds of options found in a single RA packet.
     34     // When an option is not set, the value of the associated field is -1;
     35     public final long routerLifetime;
     36     public final long prefixValidLifetime;
     37     public final long prefixPreferredLifetime;
     38     public final long routeInfoLifetime;
     39     public final long rdnssLifetime;
     40     public final long dnsslLifetime;
     41 
     42     /** {@hide} */
     43     public RaEvent(long routerLifetime, long prefixValidLifetime, long prefixPreferredLifetime,
     44             long routeInfoLifetime, long rdnssLifetime, long dnsslLifetime) {
     45         this.routerLifetime = routerLifetime;
     46         this.prefixValidLifetime = prefixValidLifetime;
     47         this.prefixPreferredLifetime = prefixPreferredLifetime;
     48         this.routeInfoLifetime = routeInfoLifetime;
     49         this.rdnssLifetime = rdnssLifetime;
     50         this.dnsslLifetime = dnsslLifetime;
     51     }
     52 
     53     private RaEvent(Parcel in) {
     54         routerLifetime          = in.readLong();
     55         prefixValidLifetime     = in.readLong();
     56         prefixPreferredLifetime = in.readLong();
     57         routeInfoLifetime       = in.readLong();
     58         rdnssLifetime           = in.readLong();
     59         dnsslLifetime           = in.readLong();
     60     }
     61 
     62     @Override
     63     public void writeToParcel(Parcel out, int flags) {
     64         out.writeLong(routerLifetime);
     65         out.writeLong(prefixValidLifetime);
     66         out.writeLong(prefixPreferredLifetime);
     67         out.writeLong(routeInfoLifetime);
     68         out.writeLong(rdnssLifetime);
     69         out.writeLong(dnsslLifetime);
     70     }
     71 
     72     @Override
     73     public int describeContents() {
     74         return 0;
     75     }
     76 
     77     @Override
     78     public String toString() {
     79         return new StringBuilder("RaEvent(lifetimes: ")
     80                 .append(String.format("router=%ds, ", routerLifetime))
     81                 .append(String.format("prefix_valid=%ds, ", prefixValidLifetime))
     82                 .append(String.format("prefix_preferred=%ds, ", prefixPreferredLifetime))
     83                 .append(String.format("route_info=%ds, ", routeInfoLifetime))
     84                 .append(String.format("rdnss=%ds, ", rdnssLifetime))
     85                 .append(String.format("dnssl=%ds)", dnsslLifetime))
     86                 .toString();
     87     }
     88 
     89     public static final Parcelable.Creator<RaEvent> CREATOR = new Parcelable.Creator<RaEvent>() {
     90         public RaEvent createFromParcel(Parcel in) {
     91             return new RaEvent(in);
     92         }
     93 
     94         public RaEvent[] newArray(int size) {
     95             return new RaEvent[size];
     96         }
     97     };
     98 
     99     /** {@hide} */
    100     public static class Builder {
    101 
    102         long routerLifetime          = NO_LIFETIME;
    103         long prefixValidLifetime     = NO_LIFETIME;
    104         long prefixPreferredLifetime = NO_LIFETIME;
    105         long routeInfoLifetime       = NO_LIFETIME;
    106         long rdnssLifetime           = NO_LIFETIME;
    107         long dnsslLifetime           = NO_LIFETIME;
    108 
    109         public Builder() {
    110         }
    111 
    112         public RaEvent build() {
    113             return new RaEvent(routerLifetime, prefixValidLifetime, prefixPreferredLifetime,
    114                     routeInfoLifetime, rdnssLifetime, dnsslLifetime);
    115         }
    116 
    117         public Builder updateRouterLifetime(long lifetime) {
    118             routerLifetime = updateLifetime(routerLifetime, lifetime);
    119             return this;
    120         }
    121 
    122         public Builder updatePrefixValidLifetime(long lifetime) {
    123             prefixValidLifetime = updateLifetime(prefixValidLifetime, lifetime);
    124             return this;
    125         }
    126 
    127         public Builder updatePrefixPreferredLifetime(long lifetime) {
    128             prefixPreferredLifetime = updateLifetime(prefixPreferredLifetime, lifetime);
    129             return this;
    130         }
    131 
    132         public Builder updateRouteInfoLifetime(long lifetime) {
    133             routeInfoLifetime = updateLifetime(routeInfoLifetime, lifetime);
    134             return this;
    135         }
    136 
    137         public Builder updateRdnssLifetime(long lifetime) {
    138             rdnssLifetime = updateLifetime(rdnssLifetime, lifetime);
    139             return this;
    140         }
    141 
    142         public Builder updateDnsslLifetime(long lifetime) {
    143             dnsslLifetime = updateLifetime(dnsslLifetime, lifetime);
    144             return this;
    145         }
    146 
    147         private long updateLifetime(long currentLifetime, long newLifetime) {
    148             if (currentLifetime == RaEvent.NO_LIFETIME) {
    149                 return newLifetime;
    150             }
    151             return Math.min(currentLifetime, newLifetime);
    152         }
    153     }
    154 }
    155