1 /*
2  * Copyright (C) 2016 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.invoker;
17 
18 import com.android.tradefed.build.IBuildInfo;
19 import com.android.tradefed.config.ConfigurationDescriptor;
20 import com.android.tradefed.device.ITestDevice;
21 import com.android.tradefed.device.ITestDevice.RecoveryMode;
22 import com.android.tradefed.testtype.suite.ITestSuite;
23 import com.android.tradefed.util.MultiMap;
24 import com.android.tradefed.util.UniqueMultiMap;
25 
26 import java.io.Serializable;
27 import java.util.List;
28 import java.util.Map;
29 
30 /**
31  * Holds information about the Invocation for the tests to access if needed. Tests should not modify
32  * the context contained here so only getters will be available, except for the context attributes
33  * for reporting purpose.
34  */
35 public interface IInvocationContext extends Serializable {
36 
37     public enum TimingEvent {
38         FETCH_BUILD,
39         SETUP;
40     }
41 
42     /**
43      * Return the number of devices allocated for the invocation.
44      */
getNumDevicesAllocated()45     public int getNumDevicesAllocated();
46 
47     /**
48      * Add a ITestDevice to be tracked by the meta data when the device is allocated.
49      * will set the build info to null in the map.
50      *
51      * @param deviceName the device configuration name to associate with the {@link ITestDevice}
52      * @param testDevice to be added to the allocated devices.
53      */
addAllocatedDevice(String deviceName, ITestDevice testDevice)54     public void addAllocatedDevice(String deviceName, ITestDevice testDevice);
55 
56     /**
57      * Track a map of configuration device name associated to a {@link ITestDevice}. Doesn't clear
58      * the previous tracking before adding.
59      *
60      * @param deviceWithName the {@link Map} of additional device to track
61      */
addAllocatedDevice(Map<String, ITestDevice> deviceWithName)62     public void addAllocatedDevice(Map<String, ITestDevice> deviceWithName);
63 
64     /**
65      * Return the map of Device/build info association
66      */
getDeviceBuildMap()67     public Map<ITestDevice, IBuildInfo> getDeviceBuildMap();
68 
69     /**
70      * Return all the allocated device tracked for this invocation.
71      */
getDevices()72     public List<ITestDevice> getDevices();
73 
74     /**
75      * Return all the {@link IBuildInfo} tracked for this invocation.
76      */
getBuildInfos()77     public List<IBuildInfo> getBuildInfos();
78 
79     /**
80      * Return the list of serials of the device tracked in this invocation
81      */
getSerials()82     public List<String> getSerials();
83 
84     /**
85      * Return the list of device config names of the device tracked in this invocation
86      */
getDeviceConfigNames()87     public List<String> getDeviceConfigNames();
88 
89     /**
90      * Return the {@link ITestDevice} associated with the device configuration name provided.
91      */
getDevice(String deviceName)92     public ITestDevice getDevice(String deviceName);
93 
94     /**
95      * Returns the {@link ITestDevice} associated with the serial provided.
96      * Refrain from using too much as it's not the fastest lookup.
97      */
getDeviceBySerial(String serial)98     public ITestDevice getDeviceBySerial(String serial);
99 
100     /**
101      * Returns the name of the device set in the xml configuration from the {@link ITestDevice}.
102      * Returns null, if ITestDevice cannot be matched.
103      */
getDeviceName(ITestDevice device)104     public String getDeviceName(ITestDevice device);
105 
106     /**
107      * Return the {@link IBuildInfo} associated with the device configuration name provided. Returns
108      * null, if the deviceName cannot be matched.
109      */
getBuildInfo(String deviceName)110     public IBuildInfo getBuildInfo(String deviceName);
111 
112     /**
113      * Return the {@link IBuildInfo} associated with the {@link ITestDevice}
114      */
getBuildInfo(ITestDevice testDevice)115     public IBuildInfo getBuildInfo(ITestDevice testDevice);
116 
117     /**
118      * Add a {@link IBuildInfo} to be tracked with the device configuration name.
119      *
120      * @param deviceName the device configuration name
121      * @param buildinfo a {@link IBuildInfo} associated to the device configuration name.
122      */
addDeviceBuildInfo(String deviceName, IBuildInfo buildinfo)123     public void addDeviceBuildInfo(String deviceName, IBuildInfo buildinfo);
124 
125     /**
126      * Add an Invocation attribute.
127      */
addInvocationAttribute(String attributeName, String attributeValue)128     public void addInvocationAttribute(String attributeName, String attributeValue);
129 
130     /** Add several invocation attributes at once through a {@link UniqueMultiMap}. */
addInvocationAttributes(UniqueMultiMap<String, String> attributesMap)131     public void addInvocationAttributes(UniqueMultiMap<String, String> attributesMap);
132 
133     /** Returns a copy of the map containing all the invocation attributes. */
getAttributes()134     public MultiMap<String, String> getAttributes();
135 
136     /** Add a invocation timing metric. */
addInvocationTimingMetric(TimingEvent timingEvent, Long durationMillis)137     public void addInvocationTimingMetric(TimingEvent timingEvent, Long durationMillis);
138 
139     /** Returns the map containing the invocation timing metrics. */
getInvocationTimingMetrics()140     public Map<TimingEvent, Long> getInvocationTimingMetrics();
141 
142     /** Sets the descriptor associated with the test configuration that launched the invocation */
setConfigurationDescriptor(ConfigurationDescriptor configurationDescriptor)143     public void setConfigurationDescriptor(ConfigurationDescriptor configurationDescriptor);
144 
145     /**
146      * Returns the descriptor associated with the test configuration that launched the invocation
147      */
getConfigurationDescriptor()148     public ConfigurationDescriptor getConfigurationDescriptor();
149 
150     /**
151      * Sets the invocation context of module while being executed as part of a {@link ITestSuite}
152      */
setModuleInvocationContext(IInvocationContext invocationContext)153     public void setModuleInvocationContext(IInvocationContext invocationContext);
154 
155     /**
156      * Returns the invocation context of module while being executed as part of a {@link ITestSuite}
157      */
getModuleInvocationContext()158     public IInvocationContext getModuleInvocationContext();
159 
160     /** Returns the invocation test-tag. */
getTestTag()161     public String getTestTag();
162 
163     /**
164      * Sets the invocation test-tag.
165      */
setTestTag(String testTag)166     public void setTestTag(String testTag);
167 
168     /**
169      * Sets the {@link RecoveryMode} of all the devices part of the context
170      */
setRecoveryModeForAllDevices(RecoveryMode mode)171     public void setRecoveryModeForAllDevices(RecoveryMode mode);
172 
173     /**
174      * Add a serial to be tracked as assigned to one of the shard running some tests.
175      *
176      * @param index the index of the shard using the serials
177      * @param serials The list of serials to be tracked.
178      */
addSerialsFromShard(Integer index, List<String> serials)179     public void addSerialsFromShard(Integer index, List<String> serials);
180 
181     /**
182      * Returns the Map of all tracked serials and their shard involved in sharding. Empty if not a
183      * sharded invocation.
184      */
getShardsSerials()185     public Map<Integer, List<String>> getShardsSerials();
186 }
187