1 /*
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  */
16 package com.android.tradefed.cluster;
17 
18 import com.android.tradefed.config.GlobalConfiguration;
19 import com.android.tradefed.config.IConfiguration;
20 import com.android.tradefed.config.Option;
21 import com.android.tradefed.config.OptionClass;
22 import com.android.tradefed.util.MultiMap;
23 
24 import com.google.common.annotations.VisibleForTesting;
25 
26 import java.io.File;
27 import java.util.ArrayList;
28 import java.util.HashMap;
29 import java.util.List;
30 import java.util.Map;
31 
32 /*
33  * A {@link IClusterOptions} implementation which contains cluster-related options.
34  */
35 @OptionClass(alias = "cluster", global_namespace = false)
36 public class ClusterOptions implements IClusterOptions {
37 
38     /**
39      * The unique configuration object type name. Used to retrieve the singleton instance from the
40      * {@link GlobalConfiguration}.
41      *
42      * @see IConfiguration#getConfigurationObject(String)
43      */
44     public static final String TYPE_NAME = "cluster_options";
45 
46     @Option(name = "service-url", description = "the base url of the tradefed cluster REST API")
47     public String mServiceUrl = null;
48 
49     // TODO: The "service-account-keyfile" option should be HostOption (AOSP HostOptions.java).
50     @Option(
51             name = "service-account-keyfile",
52             description =
53                     "The service account json key file. "
54                             + "This is used by tradefed test scheduler (e.g. Tradefed Cluster) to "
55                             + "authenticate the tradefed host. "
56                             + "See google doc for the definition and how service account key works. "
57                             + "https://cloud.google.com/iam/docs/service-accounts ")
58     private File mSchedulerServiceAccountKeyfile = null;
59 
60     @Option(name = "cluster", description = "the cluster id for this TF instance", mandatory = true)
61     public String mClusterId = null;
62 
63     @Option(
64             name = "next-cluster",
65             description =
66                     "seconadary clusters for this TF instance to run commands from. If "
67                             + "this option is set, TF will try to lease commands from these clusters in "
68                             + "the order they are specified if it still has available devices after "
69                             + "leasing commands from the primary cluster.")
70     public List<String> mNextClusterIds = new ArrayList<>();
71 
72     @Option(name = "run-target-format", description = "the format for labelling run targets.")
73     private String mRunTargetFormat = null;
74 
75     @Option(name = "disable-device-monitor", description = "disable Cluster device reporting")
76     private boolean mIsDeviceMonitorDisabled = false;
77 
78     @Option(
79             name = "device-monitor-interval",
80             isTimeVal = true,
81             description = "the time interval between each device snapshot")
82     private long mDeviceMonitorSnapshotInterval = 60 * 1000;
83 
84     @Option(
85             name = "device-group",
86             description =
87                     "A multi-map from device group to device serials."
88                             + " The key is a device group name and value is device serial.")
89     private MultiMap<String, String> mDeviceGroup = new MultiMap<String, String>();
90 
91     @Option(
92             name = "device-tag",
93             description =
94                     "A map for tagging device serials; each device may "
95                             + "have one tag. This can be used for reporting in run-target")
96     private Map<String, String> mDeviceTag = new HashMap<>();
97 
98     @Option(
99             name = "check-flashing-permits-on-lease",
100             description = "Check available flashing permits when leasing tasks")
101     private boolean mCheckFlashingPermitsOnLease = true;
102 
103     @Option(
104             name = "invocation-heartbeat-interval",
105             isTimeVal = true,
106             description = "The time interval between invocation heartbeats")
107     private long mInvocationHeartbeatInterval = 5 * 60 * 1000;
108 
109     @Option(name = "upload-invocation-status", description = "Upload invocation status to TFC")
110     private Boolean mShouldUploadInvocationStatus = false;
111 
112     @Option(
113             name = "check-command-state",
114             description = "Check cluster command state to detect canceled invocations")
115     private boolean mCheckCommandState = false;
116 
117     @Option(name = "connect-timeout", description = "HTTP connect timeout.", isTimeVal = true)
118     private int mConnectTimeout = 60000;
119 
120     @Option(name = "read-timeout", description = "HTTP read timeout.", isTimeVal = true)
121     private int mReadTimeout = 60000;
122 
123     @Option(name = "label", description = "Labels to describe the host.")
124     private List<String> mLabels = new ArrayList<>();
125 
126     @Option(name = "lab-name", description = "The name of the lab the host belong to.")
127     private String mLabName;
128 
129     @Option(
130             name = "collect-early-test-summary",
131             description = "Collect early test summary from ITestSummaryListener to scheduler.")
132     private boolean mCollectEarlyTestSummary = false;
133 
134     /** {@inheritDoc} */
135     @Override
getServiceUrl()136     public String getServiceUrl() {
137         return mServiceUrl;
138     }
139 
140     /** {@inheritDoc} */
141     @Override
getClusterId()142     public String getClusterId() {
143         return mClusterId;
144     }
145 
146     /** {@inheritDoc} */
147     @Override
getNextClusterIds()148     public List<String> getNextClusterIds() {
149         return mNextClusterIds;
150     }
151 
152     /** {@inheritDoc} */
153     @Override
getDeviceGroup()154     public MultiMap<String, String> getDeviceGroup() {
155         return mDeviceGroup;
156     }
157 
158     /** {@inheritDoc} */
159     @Override
getDeviceTag()160     public Map<String, String> getDeviceTag() {
161         return mDeviceTag;
162     }
163 
164     /** {@inheritDoc} */
165     @Override
checkFlashingPermitsOnLease()166     public boolean checkFlashingPermitsOnLease() {
167         return mCheckFlashingPermitsOnLease;
168     }
169 
170     /** {@inheritDoc} */
171     @Override
getRunTargetFormat()172     public String getRunTargetFormat() {
173         return mRunTargetFormat;
174     }
175 
176     /** {@inheritDoc} */
177     @Override
isDeviceMonitorDisabled()178     public boolean isDeviceMonitorDisabled() {
179         return mIsDeviceMonitorDisabled;
180     }
181 
182     /** {@inheritDoc} */
183     @Override
getDeviceMonitorSnapshotInterval()184     public long getDeviceMonitorSnapshotInterval() {
185         return mDeviceMonitorSnapshotInterval;
186     }
187 
188     /**
189      * Set the base url of the tradefed cluster REST API.
190      *
191      * <p>Exposed for testing.
192      */
setServiceUrl(String url)193     void setServiceUrl(String url) {
194         mServiceUrl = url;
195     }
196 
197     /**
198      * Set the cluster id for this TF instance.
199      *
200      * <p>Exposed for testing.
201      */
setClusterId(String id)202     void setClusterId(String id) {
203         mClusterId = id;
204     }
205 
206     /**
207      * Set the format for labelling run targets.
208      *
209      * <p>Exposed for testing.
210      */
setRunTargetFormat(String format)211     void setRunTargetFormat(String format) {
212         mRunTargetFormat = format;
213     }
214 
215     /**
216      * Set whether Cluster device reporting is disabled.
217      *
218      * <p>Exposed for testing.
219      */
setDeviceMonitorDisabled(boolean disabled)220     void setDeviceMonitorDisabled(boolean disabled) {
221         mIsDeviceMonitorDisabled = disabled;
222     }
223 
224     /**
225      * Set the time interval between each device snapshot in ms.
226      *
227      * <p>Exposed for testing.
228      */
setDeviceMonitorSnapshotInterval(long interval)229     void setDeviceMonitorSnapshotInterval(long interval) {
230         mDeviceMonitorSnapshotInterval = interval;
231     }
232 
233     /**
234      * Set whether the scheduler should check if there are available flashing permits.
235      *
236      * <p>Exposed for testing.
237      */
setCheckFlashingPermitsLease(boolean checkFlashingPermitsLease)238     void setCheckFlashingPermitsLease(boolean checkFlashingPermitsLease) {
239         mCheckFlashingPermitsOnLease = checkFlashingPermitsLease;
240     }
241 
242     /**
243      * Set whether the scheduler should collect early test summary.
244      *
245      * <p>Exposed for testing.
246      */
setCollectEarlyTestSummary(boolean collectEarlyTestSummary)247     void setCollectEarlyTestSummary(boolean collectEarlyTestSummary) {
248         mCollectEarlyTestSummary = collectEarlyTestSummary;
249     }
250 
251     /** {@inheritDoc} */
252     @Override
getInvocationHeartbeatInterval()253     public long getInvocationHeartbeatInterval() {
254         return mInvocationHeartbeatInterval;
255     }
256 
257     /** {@inheritDoc} */
258     @Override
shouldUploadInvocationStatus()259     public Boolean shouldUploadInvocationStatus() {
260         return mShouldUploadInvocationStatus;
261     }
262 
263     /** Set the service account key file. */
264     @VisibleForTesting
setSchedulerServiceAccountKeyfile(File keyFile)265     void setSchedulerServiceAccountKeyfile(File keyFile) {
266         mSchedulerServiceAccountKeyfile = keyFile;
267     }
268 
269     /** {@inheritDoc} */
270     @Override
getSchedulerServiceAccountKeyfile()271     public File getSchedulerServiceAccountKeyfile() {
272         return mSchedulerServiceAccountKeyfile;
273     }
274 
275     /** {@inheritDoc} */
276     @Override
getSchedulerServiceUrl()277     public String getSchedulerServiceUrl() {
278         return mServiceUrl;
279     }
280 
281     /** {@inheritDoc} */
282     @Override
getConnectTimeout()283     public int getConnectTimeout() {
284         return mConnectTimeout;
285     }
286 
287     /** {@inheritDoc} */
288     @Override
getReadTimeout()289     public int getReadTimeout() {
290         return mReadTimeout;
291     }
292 
293     @Override
checkCommandState()294     public boolean checkCommandState() {
295         return mCheckCommandState;
296     }
297 
298     @VisibleForTesting
setCheckCommandState(boolean checkCommandState)299     void setCheckCommandState(boolean checkCommandState) {
300         mCheckCommandState = checkCommandState;
301     }
302 
303     /** {@inheritDoc} */
304     @Override
getLabels()305     public List<String> getLabels() {
306         return new ArrayList<>(mLabels);
307     }
308 
309     /** {@inheritDoc} */
310     @Override
getLabName()311     public String getLabName() {
312         return mLabName;
313     }
314 
315     /** {@inheritDoc} */
316     @Override
shouldCollectEarlyTestSummary()317     public boolean shouldCollectEarlyTestSummary() {
318         return mCollectEarlyTestSummary;
319     }
320 }
321