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.databroker;
18 
19 import android.car.telemetry.MetricsConfigKey;
20 
21 import com.android.car.telemetry.TelemetryProto;
22 
23 /** Interface for the data path. Handles data forwarding from publishers to subscribers */
24 public interface DataBroker {
25 
26     /**
27      * Interface for receiving notification that script finished.
28      */
29     interface ScriptFinishedCallback {
30         /**
31          * Listens to script finished event.
32          *
33          * @param key that uniquely identifies the config whose script finished.
34          */
onScriptFinished(MetricsConfigKey key)35         void onScriptFinished(MetricsConfigKey key);
36     }
37 
38     /**
39      * Adds an active {@link com.android.car.telemetry.TelemetryProto.MetricsConfig} that is pending
40      * execution. When updating the MetricsConfig to a newer version, the caller must call
41      * {@link #removeMetricsConfig(String)} first to clear the old MetricsConfig.
42      * TODO(b/191378559): Define behavior when metricsConfig contains invalid config
43      *
44      * @param key the unique identifier of the MetricsConfig.
45      * @param metricsConfig to be added and queued for execution.
46      */
addMetricsConfig(MetricsConfigKey key, TelemetryProto.MetricsConfig metricsConfig)47     void addMetricsConfig(MetricsConfigKey key, TelemetryProto.MetricsConfig metricsConfig);
48 
49     /**
50      * Removes a {@link com.android.car.telemetry.TelemetryProto.MetricsConfig} and all its
51      * relevant subscriptions.
52      *
53      * @param key the unique identifier of the MetricsConfig to be removed.
54      */
removeMetricsConfig(MetricsConfigKey key)55     void removeMetricsConfig(MetricsConfigKey key);
56 
57     /**
58      * Removes all {@link com.android.car.telemetry.TelemetryProto.MetricsConfig}s and
59      * subscriptions.
60      */
removeAllMetricsConfigs()61     void removeAllMetricsConfigs();
62 
63     /**
64      * Adds a {@link ScriptExecutionTask} to the priority queue. This method will schedule the
65      * next task if a task is not currently running.
66      */
addTaskToQueue(ScriptExecutionTask task)67     void addTaskToQueue(ScriptExecutionTask task);
68 
69     /**
70      * Checks system health state and executes a task if condition allows.
71      */
scheduleNextTask()72     void scheduleNextTask();
73 
74     /**
75      * Sets callback for notifying script finished.
76      *
77      * @param callback script finished callback.
78      */
setOnScriptFinishedCallback(ScriptFinishedCallback callback)79     void setOnScriptFinishedCallback(ScriptFinishedCallback callback);
80 
81     /**
82      * Sets the priority which affects which subscribers can consume data. Invoked by controller to
83      * indicate system health state and which subscribers can be consumed. If controller does not
84      * set the priority, it will be defaulted to 1. A smaller priority number indicates higher
85      * priority. Range 0 - 100. A priority of 0 means the script should run regardless of system
86      * health conditions.
87      */
setTaskExecutionPriority(int priority)88     void setTaskExecutionPriority(int priority);
89 }
90