blob: 8a56e93ddb2c51d92b6e7259d4158c89bbd8560f [file] [log] [blame]
Di Qian38c02a72019-11-18 19:14:07 -08001/*
2 * Copyright (C) 2019 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 */
16package com.android.tradefed.cluster;
17
18import com.android.tradefed.command.CommandScheduler;
19
20import org.json.JSONArray;
21import org.json.JSONException;
22import org.json.JSONObject;
23
24import java.util.ArrayList;
25import java.util.HashMap;
26import java.util.List;
27import java.util.Map;
28
29/** A class to encapsulate cluster host events to be uploaded. */
30public class ClusterHostEvent implements IClusterEvent {
31 private long mTimestamp;
32 private String mHostName;
33 private String mTfVersion;
34 private String mClusterId;
35 private List<String> mNextClusterIds;
36 private List<ClusterDeviceInfo> mDeviceInfos = new ArrayList<>();
37 private Map<String, String> mData = new HashMap<>();
Xing Dai81f6f9f2019-12-03 14:01:32 -080038 private String mLabName;
Di Qian38c02a72019-11-18 19:14:07 -080039 public static final String EVENT_QUEUE = "host-event-queue";
40
41 /** Enums of the different types of host events. */
42 public enum HostEventType {
43 DeviceSnapshot("DeviceSnapshot", "DEVICE_SNAPSHOT"),
44 HostStateChanged("HostStateChanged", "HOST_STATE_CHANGED");
45 private final String mName;
46 // The AndroidEngProdAPIName should map to
47 // https://cs.corp.google.com/piper///depot/google3/google/internal/android/engprod/proto/test/v1/androidtest.proto?rcl=221372995&l=80
48 private final String mAndroidEngProdAPIName;
49
50 private HostEventType(final String name, final String androidEngProdAPIName) {
51 mName = name;
52 mAndroidEngProdAPIName = androidEngProdAPIName;
53 }
54
55 @Override
56 public String toString() {
57 return mName;
58 }
59
60 public String getAndroidEngProdAPIName() {
61 return mAndroidEngProdAPIName;
62 }
63 }
64
65 private HostEventType mType;
66 private CommandScheduler.HostState mHostState = CommandScheduler.HostState.UNKNOWN;
67
68 private ClusterHostEvent() {}
69
70 public long getTimestamp() {
71 return mTimestamp;
72 }
73
74 public String getHostName() {
75 return mHostName;
76 }
77
78 public String getTfVersion() {
79 return mTfVersion;
80 }
81
82 public String getClusterId() {
83 return mClusterId;
84 }
85
86 public CommandScheduler.HostState getHostState() {
87 return mHostState;
88 }
89
90 public long getTfStartTime() {
91 return ClusterHostUtil.getTfStartTimeMillis();
92 }
93
94 public List<ClusterDeviceInfo> getDeviceInfos() {
95 return mDeviceInfos;
96 }
97
98 public Map<String, String> getData() {
99 return mData;
100 }
101
102 public HostEventType getType() {
103 return mType;
104 }
105
106 public List<String> getNextClusterIds() {
107 return mNextClusterIds;
108 }
109
Xing Dai81f6f9f2019-12-03 14:01:32 -0800110 public String getLabName() {
111 return mLabName;
112 }
113
Di Qian38c02a72019-11-18 19:14:07 -0800114 public static class Builder {
115 private HostEventType mType;
116 private long mTimestamp = System.currentTimeMillis();
117 private String mHostName;
118 private String mTfVersion;
119 private String mClusterId;
120 private List<String> mNextClusterIds;
121 private List<ClusterDeviceInfo> mDeviceInfos = new ArrayList<ClusterDeviceInfo>();
122 private Map<String, String> mData = new HashMap<>();
123 private CommandScheduler.HostState mHostState = CommandScheduler.HostState.UNKNOWN;
Xing Dai81f6f9f2019-12-03 14:01:32 -0800124 private String mLabName;
Di Qian38c02a72019-11-18 19:14:07 -0800125
126 public Builder() {}
127
128 public Builder setHostEventType(final HostEventType type) {
129 mType = type;
130 return this;
131 }
132
133 public Builder setTimestamp(final long timestamp) {
134 mTimestamp = timestamp;
135 return this;
136 }
137
138 public Builder setClusterId(final String clusterId) {
139 mClusterId = clusterId;
140 return this;
141 }
142
143 public Builder setHostName(final String hostname) {
144 mHostName = hostname;
145 return this;
146 }
147
148 public Builder setTfVersion(final String tfVersion) {
149 mTfVersion = tfVersion;
150 return this;
151 }
152
153 public Builder addDeviceInfo(final ClusterDeviceInfo deviceInfo) {
154 mDeviceInfos.add(deviceInfo);
155 return this;
156 }
157
158 public Builder addDeviceInfos(List<ClusterDeviceInfo> deviceInfos) {
159 mDeviceInfos.addAll(deviceInfos);
160 return this;
161 }
162
163 public Builder setData(final String name, final String value) {
164 mData.put(name, value);
165 return this;
166 }
167
168 public Builder setData(Map<String, String> data) {
169 mData.putAll(data);
170 return this;
171 }
172
173 public Builder setNextClusterIds(List<String> nexClusterIds) {
174 mNextClusterIds = nexClusterIds;
175 return this;
176 }
177
178 public Builder setHostState(CommandScheduler.HostState state) {
179 mHostState = state;
180 return this;
181 }
182
Xing Dai81f6f9f2019-12-03 14:01:32 -0800183 public Builder setLabName(String labName) {
184 mLabName = labName;
185 return this;
186 }
187
Di Qian38c02a72019-11-18 19:14:07 -0800188 public ClusterHostEvent build() {
189 final ClusterHostEvent event = new ClusterHostEvent();
190 event.mType = mType;
191 event.mTimestamp = mTimestamp;
192 event.mHostName = mHostName;
193 event.mTfVersion = mTfVersion;
194 event.mClusterId = mClusterId;
195 event.mDeviceInfos = new ArrayList<>(mDeviceInfos);
196 event.mData = new HashMap<>(mData);
197 event.mNextClusterIds = mNextClusterIds;
198 event.mHostState = mHostState;
Xing Dai81f6f9f2019-12-03 14:01:32 -0800199 event.mLabName = mLabName;
Di Qian38c02a72019-11-18 19:14:07 -0800200 return event;
201 }
202 }
203
204 /** {@inheritDoc} */
205 @Override
206 public JSONObject toJSON() throws JSONException {
207 final JSONObject json = new JSONObject();
208 // event time should be in POSIX timestamp.
209 json.put("time", this.getTimestamp() / 1000);
210 if (this.getType() != null) json.put("type", this.getType().toString());
211 json.put("hostname", this.getHostName());
212 json.put("tf_version", this.getTfVersion());
213 json.put("cluster", this.getClusterId());
214 JSONArray deviceInfos = new JSONArray();
215 for (ClusterDeviceInfo d : this.getDeviceInfos()) {
216 deviceInfos.put(d.toJSON());
217 }
218 json.put("device_infos", deviceInfos);
219 json.put("data", new JSONObject(this.getData()));
220 if (this.getNextClusterIds() != null) {
221 json.put("next_cluster_ids", new JSONArray(this.getNextClusterIds()));
222 }
Xing Dai81f6f9f2019-12-03 14:01:32 -0800223 if (this.getLabName() != null) json.put("lab_name", this.getLabName());
Di Qian38c02a72019-11-18 19:14:07 -0800224 json.put("state", this.getHostState().toString());
225 json.put("tf_start_time_seconds", this.getTfStartTime() / 1000);
226 return json;
227 }
228}