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.car.telemetry.publisher;
18 
19 import com.android.car.telemetry.TelemetryProto;
20 import com.android.car.telemetry.databroker.DataSubscriber;
21 
22 import java.util.List;
23 
24 /**
25  * Abstract class for publishers. It is 1-1 with data source and manages sending data to
26  * subscribers. Publisher stops itself when there are no subscribers.
27  *
28  * <p>Note that it doesn't map 1-1 to {@link com.android.car.telemetry.TelemetryProto.Publisher}
29  * configuration. Single publisher instance can send data as several
30  * {@link com.android.car.telemetry.TelemetryProto.Publisher} to subscribers.
31  *
32  * <p>The methods must be called from the telemetry thread.
33  */
34 public abstract class AbstractPublisher {
35     private final PublisherFailureListener mFailureListener;
36 
37     /**
38      * Listener for publisher failures, such as failing to connect to a underlying service or
39      * invalid Publisher configuration. When publishers fail, the affected configs should be
40      * disabled, because the associated scripts cannot receive data from the failed publishers.
41      */
42     public interface PublisherFailureListener {
43         /** Called by publishers when they fail. */
onPublisherFailure( AbstractPublisher publisher, List<TelemetryProto.MetricsConfig> affectedConfigs, Throwable error)44         void onPublisherFailure(
45                 AbstractPublisher publisher,
46                 List<TelemetryProto.MetricsConfig> affectedConfigs, Throwable error);
47     }
48 
AbstractPublisher(PublisherFailureListener failureListener)49     AbstractPublisher(PublisherFailureListener failureListener) {
50         mFailureListener = failureListener;
51     }
52 
53     /**
54      * Adds a subscriber that listens for data produced by this publisher.
55      *
56      * <p>DataBroker may call this method when a new {@code MetricsConfig} is added,
57      * {@code CarTelemetryService} is restarted or the device is restarted.
58      *
59      * @param subscriber a subscriber to receive data
60      * @throws IllegalArgumentException if the subscriber is invalid.
61      * @throws IllegalStateException if there are internal errors.
62      */
addDataSubscriber(DataSubscriber subscriber)63     public abstract void addDataSubscriber(DataSubscriber subscriber);
64 
65     /**
66      * Removes the subscriber from the publisher. Publisher stops if necessary.
67      *
68      * <p>It does nothing if subscriber is not found.
69      */
removeDataSubscriber(DataSubscriber subscriber)70     public abstract void removeDataSubscriber(DataSubscriber subscriber);
71 
72     /**
73      * Removes all the subscribers from the publisher. The publisher may stop.
74      *
75      * <p>This method also cleans-up internal publisher and the data source persisted state.
76      */
removeAllDataSubscribers()77     public abstract void removeAllDataSubscribers();
78 
79     /** Returns true if the publisher already has this data subscriber. */
hasDataSubscriber(DataSubscriber subscriber)80     public abstract boolean hasDataSubscriber(DataSubscriber subscriber);
81 
82     /**
83      * Notifies the failure Listener that this publisher failed. See
84      * {@link PublisherFailureListener} for details.
85      */
onPublisherFailure( List<TelemetryProto.MetricsConfig> affectedConfigs, Throwable error)86     protected void onPublisherFailure(
87             List<TelemetryProto.MetricsConfig> affectedConfigs, Throwable error) {
88         mFailureListener.onPublisherFailure(this, affectedConfigs, error);
89     }
90 }
91