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