1 /*
2  * Copyright (C) 2021 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.android.networkstack.metrics;
18 
19 import android.stats.connectivity.IpType;
20 import android.stats.connectivity.NudEventType;
21 import android.stats.connectivity.NudNeighborType;
22 
23 /**
24  * Class to record the network stack IpReachabilityMonitor metrics into statsd.
25  *
26  * This class is not thread-safe, and should always be accessed from the same thread.
27  *
28  * @hide
29  */
30 public class IpReachabilityMonitorMetrics {
31     private final NetworkIpReachabilityMonitorReported.Builder mStatsBuilder =
32             NetworkIpReachabilityMonitorReported.newBuilder();
33 
34     /**
35      * Write the NUD event type into mStatsBuilder.
36      */
setNudEventType(final NudEventType type)37     public void setNudEventType(final NudEventType type) {
38         mStatsBuilder.setEventType(type);
39     }
40 
41     /**
42      * Write the NUD probe type(IPv4 or IPv6) into mStatsBuilder.
43      */
setNudIpType(final IpType type)44     public void setNudIpType(final IpType type) {
45         mStatsBuilder.setIpType(type);
46     }
47 
48     /**
49      * Write the NUD probe neighbor type into mStatsBuilder.
50      */
setNudNeighborType(final NudNeighborType type)51     public void setNudNeighborType(final NudNeighborType type) {
52         mStatsBuilder.setNeighborType(type);
53     }
54 
55     /**
56      * Write the NetworkIpReachabilityMonitorReported proto into statsd.
57      */
statsWrite()58     public NetworkIpReachabilityMonitorReported statsWrite() {
59         final NetworkIpReachabilityMonitorReported stats = mStatsBuilder.build();
60         NetworkStackStatsLog.write(NetworkStackStatsLog.NETWORK_IP_REACHABILITY_MONITOR_REPORTED,
61                 stats.getEventType().getNumber(),
62                 stats.getIpType().getNumber(),
63                 stats.getNeighborType().getNumber());
64         return stats;
65     }
66 }
67