1 /*
2  * Copyright (C) 2010 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.tradefed.config;
18 
19 import com.android.tradefed.build.IBuildProvider;
20 import com.android.tradefed.command.ICommandOptions;
21 import com.android.tradefed.config.ConfigurationDef.OptionDef;
22 import com.android.tradefed.device.IDeviceRecovery;
23 import com.android.tradefed.device.IDeviceSelection;
24 import com.android.tradefed.device.TestDeviceOptions;
25 import com.android.tradefed.device.metric.IMetricCollector;
26 import com.android.tradefed.log.ILeveledLogOutput;
27 import com.android.tradefed.result.ILogSaver;
28 import com.android.tradefed.result.ITestInvocationListener;
29 import com.android.tradefed.suite.checker.ISystemStatusChecker;
30 import com.android.tradefed.targetprep.ITargetPreparer;
31 import com.android.tradefed.targetprep.multi.IMultiTargetPreparer;
32 import com.android.tradefed.testtype.IRemoteTest;
33 import com.android.tradefed.util.keystore.IKeyStoreClient;
34 
35 import org.json.JSONArray;
36 import org.json.JSONException;
37 
38 import java.io.IOException;
39 import java.io.PrintStream;
40 import java.io.PrintWriter;
41 import java.util.List;
42 
43 /**
44  * Configuration information for a TradeFederation invocation.
45  *
46  * Each TradeFederation invocation has a single {@link IConfiguration}. An {@link IConfiguration}
47  * stores all the delegate objects that should be used during the invocation, and their associated
48  * {@link Option}'s
49  */
50 public interface IConfiguration {
51 
52     /** Returns the name of the configuration. */
getName()53     public String getName();
54 
55     /**
56      * Gets the {@link IBuildProvider} from the configuration.
57      *
58      * @return the {@link IBuildProvider} provided in the configuration
59      */
getBuildProvider()60     public IBuildProvider getBuildProvider();
61 
62     /**
63      * Gets the {@link ITargetPreparer}s from the configuration.
64      *
65      * @return the {@link ITargetPreparer}s provided in order in the configuration
66      */
getTargetPreparers()67     public List<ITargetPreparer> getTargetPreparers();
68 
69     /**
70      * Gets the {@link IRemoteTest}s to run from the configuration.
71      *
72      * @return the tests provided in the configuration
73      */
getTests()74     public List<IRemoteTest> getTests();
75 
76     /**
77      * Gets the {@link ITestInvocationListener}s to use from the configuration.
78      *
79      * @return the {@link ITestInvocationListener}s provided in the configuration.
80      */
getTestInvocationListeners()81     public List<ITestInvocationListener> getTestInvocationListeners();
82 
83     /**
84      * Gets the {@link IDeviceRecovery} to use from the configuration.
85      *
86      * @return the {@link IDeviceRecovery} provided in the configuration.
87      */
getDeviceRecovery()88     public IDeviceRecovery getDeviceRecovery();
89 
90     /**
91      * Gets the {@link TestDeviceOptions} to use from the configuration.
92      *
93      * @return the {@link TestDeviceOptions} provided in the configuration.
94      */
getDeviceOptions()95     public TestDeviceOptions getDeviceOptions();
96 
97     /**
98      * Gets the {@link ILeveledLogOutput} to use from the configuration.
99      *
100      * @return the {@link ILeveledLogOutput} provided in the configuration.
101      */
getLogOutput()102     public ILeveledLogOutput getLogOutput();
103 
104     /**
105      * Gets the {@link ILogSaver} to use from the configuration.
106      *
107      * @return the {@link ILogSaver} provided in the configuration.
108      */
getLogSaver()109     public ILogSaver getLogSaver();
110 
111     /**
112      * Gets the {@link IMultiTargetPreparer}s from the configuration.
113      *
114      * @return the {@link IMultiTargetPreparer}s provided in order in the configuration
115      */
getMultiTargetPreparers()116     public List<IMultiTargetPreparer> getMultiTargetPreparers();
117 
118     /**
119      * Gets the {@link IMultiTargetPreparer}s from the configuration that should be executed before
120      * any of the devices target_preparers.
121      *
122      * @return the {@link IMultiTargetPreparer}s provided in order in the configuration
123      */
getMultiPreTargetPreparers()124     public List<IMultiTargetPreparer> getMultiPreTargetPreparers();
125 
126     /**
127      * Gets the {@link ISystemStatusChecker}s from the configuration.
128      *
129      * @return the {@link ISystemStatusChecker}s provided in order in the configuration
130      */
getSystemStatusCheckers()131     public List<ISystemStatusChecker> getSystemStatusCheckers();
132 
133     /** Gets the {@link IMetricCollector}s from the configuration. */
getMetricCollectors()134     public List<IMetricCollector> getMetricCollectors();
135 
136     /**
137      * Gets the {@link ICommandOptions} to use from the configuration.
138      *
139      * @return the {@link ICommandOptions} provided in the configuration.
140      */
getCommandOptions()141     public ICommandOptions getCommandOptions();
142 
143     /** Returns the {@link ConfigurationDescriptor} provided in the configuration. */
getConfigurationDescription()144     public ConfigurationDescriptor getConfigurationDescription();
145 
146     /**
147      * Gets the {@link IDeviceSelection} to use from the configuration.
148      *
149      * @return the {@link IDeviceSelection} provided in the configuration.
150      */
getDeviceRequirements()151     public IDeviceSelection getDeviceRequirements();
152 
153     /**
154      * Generic interface to get the configuration object with the given type name.
155      *
156      * @param typeName the unique type of the configuration object
157      *
158      * @return the configuration object or <code>null</code> if the object type with given name
159      * does not exist.
160      */
getConfigurationObject(String typeName)161     public Object getConfigurationObject(String typeName);
162 
163     /**
164      * Similar to {@link #getConfigurationObject(String)}, but for configuration
165      * object types that support multiple objects.
166      *
167      * @param typeName the unique type name of the configuration object
168      *
169      * @return the list of configuration objects or <code>null</code> if the object type with
170      * given name does not exist.
171      */
getConfigurationObjectList(String typeName)172     public List<?> getConfigurationObjectList(String typeName);
173 
174     /**
175      * Gets the {@link IDeviceConfiguration}s from the configuration.
176      *
177      * @return the {@link IDeviceConfiguration}s provided in order in the configuration
178      */
getDeviceConfig()179     public List<IDeviceConfiguration> getDeviceConfig();
180 
181     /**
182      * Return the {@link IDeviceConfiguration} associated to the name provided, null if not found.
183      */
getDeviceConfigByName(String nameDevice)184     public IDeviceConfiguration getDeviceConfigByName(String nameDevice);
185 
186     /**
187      * Inject a option value into the set of configuration objects.
188      * <p/>
189      * Useful to provide values for options that are generated dynamically.
190      *
191      * @param optionName the option name
192      * @param optionValue the option value
193      * @throws ConfigurationException if failed to set the option's value
194      */
injectOptionValue(String optionName, String optionValue)195     public void injectOptionValue(String optionName, String optionValue)
196             throws ConfigurationException;
197 
198     /**
199      * Inject a option value into the set of configuration objects.
200      * <p/>
201      * Useful to provide values for options that are generated dynamically.
202      *
203      * @param optionName the option name
204      * @param optionKey the optional key for map options, or null
205      * @param optionValue the map option value
206      * @throws ConfigurationException if failed to set the option's value
207      */
injectOptionValue(String optionName, String optionKey, String optionValue)208     public void injectOptionValue(String optionName, String optionKey, String optionValue)
209             throws ConfigurationException;
210 
211     /**
212      * Inject a option value into the set of configuration objects.
213      * <p/>
214      * Useful to provide values for options that are generated dynamically.
215      *
216      * @param optionName the option name
217      * @param optionKey the optional key for map options, or null
218      * @param optionValue the map option value
219      * @param optionSource the source config that provided this option value
220      * @throws ConfigurationException if failed to set the option's value
221      */
injectOptionValueWithSource(String optionName, String optionKey, String optionValue, String optionSource)222     public void injectOptionValueWithSource(String optionName, String optionKey, String optionValue,
223             String optionSource) throws ConfigurationException;
224 
225     /**
226      * Inject multiple option values into the set of configuration objects.
227      * <p/>
228      * Useful to inject many option values at once after creating a new object.
229      *
230      * @param optionDefs a list of option defs to inject
231      * @throws ConfigurationException if failed to set option values
232      */
injectOptionValues(List<OptionDef> optionDefs)233     public void injectOptionValues(List<OptionDef> optionDefs) throws ConfigurationException;
234 
235     /**
236      * Create a copy of this object.
237      *
238      * @return a {link IConfiguration} copy
239      */
clone()240     public IConfiguration clone();
241 
242     /**
243      * Replace the current {@link IBuildProvider} in the configuration.
244      *
245      * @param provider the new {@link IBuildProvider}
246      */
setBuildProvider(IBuildProvider provider)247     public void setBuildProvider(IBuildProvider provider);
248 
249     /**
250      * Set the {@link ILeveledLogOutput}, replacing any existing value.
251      *
252      * @param logger
253      */
setLogOutput(ILeveledLogOutput logger)254     public void setLogOutput(ILeveledLogOutput logger);
255 
256     /**
257      * Set the {@link ILogSaver}, replacing any existing value.
258      *
259      * @param logSaver
260      */
setLogSaver(ILogSaver logSaver)261     public void setLogSaver(ILogSaver logSaver);
262 
263     /**
264      * Set the {@link IDeviceRecovery}, replacing any existing value.
265      *
266      * @param recovery
267      */
setDeviceRecovery(IDeviceRecovery recovery)268     public void setDeviceRecovery(IDeviceRecovery recovery);
269 
270     /**
271      * Set the {@link ITargetPreparer}, replacing any existing value.
272      *
273      * @param preparer
274      */
setTargetPreparer(ITargetPreparer preparer)275     public void setTargetPreparer(ITargetPreparer preparer);
276 
277     /**
278      * Set a {@link IDeviceConfiguration}, replacing any existing value.
279      *
280      * @param deviceConfig
281      */
setDeviceConfig(IDeviceConfiguration deviceConfig)282     public void setDeviceConfig(IDeviceConfiguration deviceConfig);
283 
284     /**
285      * Set the {@link IDeviceConfiguration}s, replacing any existing value.
286      *
287      * @param deviceConfigs
288      */
setDeviceConfigList(List<IDeviceConfiguration> deviceConfigs)289     public void setDeviceConfigList(List<IDeviceConfiguration> deviceConfigs);
290 
291     /**
292      * Convenience method to set a single {@link IRemoteTest} in this configuration, replacing any
293      * existing values
294      *
295      * @param test
296      */
setTest(IRemoteTest test)297     public void setTest(IRemoteTest test);
298 
299     /**
300      * Set the list of {@link IRemoteTest}s in this configuration, replacing any
301      * existing values
302      *
303      * @param tests
304      */
setTests(List<IRemoteTest> tests)305     public void setTests(List<IRemoteTest> tests);
306 
307     /**
308      * Set the list of {@link IMultiTargetPreparer}s in this configuration, replacing any
309      * existing values
310      *
311      * @param multiTargPreps
312      */
setMultiTargetPreparers(List<IMultiTargetPreparer> multiTargPreps)313     public void setMultiTargetPreparers(List<IMultiTargetPreparer> multiTargPreps);
314 
315     /**
316      * Convenience method to set a single {@link IMultiTargetPreparer} in this configuration,
317      * replacing any existing values
318      *
319      * @param multiTargPrep
320      */
setMultiTargetPreparer(IMultiTargetPreparer multiTargPrep)321     public void setMultiTargetPreparer(IMultiTargetPreparer multiTargPrep);
322 
323     /**
324      * Set the list of {@link IMultiTargetPreparer}s in this configuration that should be executed
325      * before any of the devices target_preparers, replacing any existing values
326      *
327      * @param multiPreTargPreps
328      */
setMultiPreTargetPreparers(List<IMultiTargetPreparer> multiPreTargPreps)329     public void setMultiPreTargetPreparers(List<IMultiTargetPreparer> multiPreTargPreps);
330 
331     /**
332      * Convenience method to set a single {@link IMultiTargetPreparer} in this configuration that
333      * should be executed before any of the devices target_preparers, replacing any existing values
334      *
335      * @param multiPreTargPreps
336      */
setMultiPreTargetPreparer(IMultiTargetPreparer multiPreTargPreps)337     public void setMultiPreTargetPreparer(IMultiTargetPreparer multiPreTargPreps);
338 
339     /**
340      * Set the list of {@link ISystemStatusChecker}s in this configuration, replacing any
341      * existing values
342      *
343      * @param systemCheckers
344      */
setSystemStatusCheckers(List<ISystemStatusChecker> systemCheckers)345     public void setSystemStatusCheckers(List<ISystemStatusChecker> systemCheckers);
346 
347     /**
348      * Convenience method to set a single {@link ISystemStatusChecker} in this configuration,
349      * replacing any existing values
350      *
351      * @param systemChecker
352      */
setSystemStatusChecker(ISystemStatusChecker systemChecker)353     public void setSystemStatusChecker(ISystemStatusChecker systemChecker);
354 
355     /**
356      * Set the list of {@link ITestInvocationListener}s, replacing any existing values
357      *
358      * @param listeners
359      */
setTestInvocationListeners(List<ITestInvocationListener> listeners)360     public void setTestInvocationListeners(List<ITestInvocationListener> listeners);
361 
362     /**
363      * Convenience method to set a single {@link ITestInvocationListener}
364      *
365      * @param listener
366      */
setTestInvocationListener(ITestInvocationListener listener)367     public void setTestInvocationListener(ITestInvocationListener listener);
368 
369     /** Set the list of {@link IMetricCollector}s, replacing any existing values. */
setDeviceMetricCollectors(List<IMetricCollector> collectors)370     public void setDeviceMetricCollectors(List<IMetricCollector> collectors);
371 
372     /**
373      * Set the {@link ICommandOptions}, replacing any existing values
374      *
375      * @param cmdOptions
376      */
setCommandOptions(ICommandOptions cmdOptions)377     public void setCommandOptions(ICommandOptions cmdOptions);
378 
379     /**
380      * Set the {@link IDeviceSelection}, replacing any existing values
381      *
382      * @param deviceSelection
383      */
setDeviceRequirements(IDeviceSelection deviceSelection)384     public void setDeviceRequirements(IDeviceSelection deviceSelection);
385 
386     /**
387      * Set the {@link TestDeviceOptions}, replacing any existing values
388      */
setDeviceOptions(TestDeviceOptions deviceOptions)389     public void setDeviceOptions(TestDeviceOptions deviceOptions);
390 
391     /**
392      * Generic method to set the config object with the given name, replacing any existing value.
393      *
394      * @param name the unique name of the config object type.
395      * @param configObject the config object
396      * @throws ConfigurationException if the configObject was not the correct type
397      */
setConfigurationObject(String name, Object configObject)398     public void setConfigurationObject(String name, Object configObject)
399             throws ConfigurationException;
400 
401     /**
402      * Generic method to set the config object list for the given name, replacing any existing
403      * value.
404      *
405      * @param name the unique name of the config object type.
406      * @param configList the config object list
407      * @throws ConfigurationException if any objects in the list are not the correct type
408      */
setConfigurationObjectList(String name, List<?> configList)409     public void setConfigurationObjectList(String name, List<?> configList)
410             throws ConfigurationException;
411 
412     /** Returns whether or not a configured device is tagged isFake=true or not. */
isDeviceConfiguredFake(String deviceName)413     public boolean isDeviceConfiguredFake(String deviceName);
414 
415     /**
416      * Set the config {@link Option} fields with given set of command line arguments
417      * <p/>
418      * {@link ArgsOptionParser} for expected format
419      *
420      * @param listArgs the command line arguments
421      * @return the unconsumed arguments
422      */
setOptionsFromCommandLineArgs(List<String> listArgs)423     public List<String> setOptionsFromCommandLineArgs(List<String> listArgs)
424             throws ConfigurationException;
425 
426     /**
427      * Set the config {@link Option} fields with given set of command line arguments
428      * <p/>
429      * See {@link ArgsOptionParser} for expected format
430      *
431      * @param listArgs the command line arguments
432      * @param keyStoreClient {@link IKeyStoreClient} to use.
433      * @return the unconsumed arguments
434      */
setOptionsFromCommandLineArgs(List<String> listArgs, IKeyStoreClient keyStoreClient)435     public List<String> setOptionsFromCommandLineArgs(List<String> listArgs,
436             IKeyStoreClient keyStoreClient)
437             throws ConfigurationException;
438 
439     /**
440      * Outputs a command line usage help text for this configuration to given printStream.
441      *
442      * @param importantOnly if <code>true</code> only print help for the important options
443      * @param out the {@link PrintStream} to use.
444      * @throws ConfigurationException
445      */
printCommandUsage(boolean importantOnly, PrintStream out)446     public void printCommandUsage(boolean importantOnly, PrintStream out)
447             throws ConfigurationException;
448 
449     /**
450      * Returns a JSON representation of this configuration.
451      * <p/>
452      * The return value is a JSONArray containing JSONObjects to represent each configuration
453      * object. Each configuration object entry has the following structure:
454      * <pre>
455      * {@code
456      *   &#123;
457      *     "alias": "device-unavail-email",
458      *     "name": "result_reporter",
459      *     "class": "com.android.tradefed.result.DeviceUnavailEmailResultReporter",
460      *     "options": [ ... ]
461      *   &#125;
462      * }
463      * </pre>
464      * The "options" entry is a JSONArray containing JSONObjects to represent each @Option annotated
465      * field. Each option entry has the following structure:
466      * <pre>
467      * {@code
468      *   &#123;
469      *     "updateRule": "LAST",
470      *     "isTimeVal": false,
471      *     "source": "google\/template\/reporters\/asit",
472      *     "importance": "IF_UNSET",
473      *     "description": "The envelope-sender address to use for the messages.",
474      *     "mandatory": false,
475      *     "name": "sender",
476      *     "javaClass": "java.lang.String",
477      *     "value": "tffail@google.com"
478      *   &#125;
479      * }
480      * </pre>
481      * Most of the values come from the @Option annotation. 'javaClass' is the name of the
482      * underlying java class for this option. 'value' is a JSON representation of the field's
483      * current value. 'source' is the set of config names which set the field's value. For regular
484      * objects or Collections, 'source' is a JSONArray containing each contributing config's name.
485      * For map fields, sources for each key are tracked individually and stored in a JSONObject.
486      * Each key / value pair in the JSONObject corresponds to a key in the map and an array of its
487      * source configurations.
488      *
489      * @throws JSONException
490      */
getJsonCommandUsage()491     public JSONArray getJsonCommandUsage() throws JSONException;
492 
493     /**
494      * Validate option values.
495      * <p/>
496      * Currently this will just validate that all mandatory options have been set
497      *
498      * @throws ConfigurationException if config is not valid
499      */
validateOptions()500     public void validateOptions() throws ConfigurationException;
501 
502     /**
503      * Sets the command line used to create this {@link IConfiguration}.
504      * This stores the whole command line, including the configuration name,
505      * unlike setOptionsFromCommandLineArgs.
506      *
507      * @param arrayArgs the command line
508      */
setCommandLine(String[] arrayArgs)509     public void setCommandLine(String[] arrayArgs);
510 
511     /**
512      * Gets the command line used to create this {@link IConfiguration}.
513      *
514      * @return the command line used to create this {@link IConfiguration}.
515      */
getCommandLine()516     public String getCommandLine();
517 
518     /**
519      * Gets the expanded XML file for the config with all options shown for this
520      * {@link IConfiguration} as a {@link String}.
521      *
522      * @param output the writer to print the xml to.
523      * @throws IOException
524      */
dumpXml(PrintWriter output)525     public void dumpXml(PrintWriter output) throws IOException;
526 
527     /**
528      * Gets the expanded XML file for the config with all options shown for this {@link
529      * IConfiguration} minus the objects filters by their key name.
530      *
531      * <p>Filter example: {@link Configuration#TARGET_PREPARER_TYPE_NAME}.
532      *
533      * @param output the writer to print the xml to.
534      * @param excludeFilters the list of object type that should not be dumped.
535      * @throws IOException
536      */
dumpXml(PrintWriter output, List<String> excludeFilters)537     public void dumpXml(PrintWriter output, List<String> excludeFilters) throws IOException;
538 }
539