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.util.MultiMap;
23 import com.android.tradefed.util.UniqueMultiMap;
24 
25 import java.io.Serializable;
26 import java.util.HashMap;
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     /** Key used for storing associated invocation ID. */
38     public static final String INVOCATION_ID = "invocation-id";
39 
40     @Deprecated
41     public enum TimingEvent {
42         FETCH_BUILD,
43         SETUP;
44     }
45 
46     /** @return associated invocation ID or {@code null} if not linked to an invocation */
getInvocationId()47     public String getInvocationId();
48 
49     /**
50      * Return the number of devices allocated for the invocation.
51      */
getNumDevicesAllocated()52     public int getNumDevicesAllocated();
53 
54     /**
55      * Add a ITestDevice to be tracked by the meta data when the device is allocated.
56      * will set the build info to null in the map.
57      *
58      * @param deviceName the device configuration name to associate with the {@link ITestDevice}
59      * @param testDevice to be added to the allocated devices.
60      */
addAllocatedDevice(String deviceName, ITestDevice testDevice)61     public void addAllocatedDevice(String deviceName, ITestDevice testDevice);
62 
63     /**
64      * Track a map of configuration device name associated to a {@link ITestDevice}. Doesn't clear
65      * the previous tracking before adding.
66      *
67      * @param deviceWithName the {@link Map} of additional device to track
68      */
addAllocatedDevice(Map<String, ITestDevice> deviceWithName)69     public void addAllocatedDevice(Map<String, ITestDevice> deviceWithName);
70 
71     /**
72      * Return the map of Device/build info association
73      */
getDeviceBuildMap()74     public Map<ITestDevice, IBuildInfo> getDeviceBuildMap();
75 
76     /**
77      * Return all the allocated device tracked for this invocation.
78      */
getDevices()79     public List<ITestDevice> getDevices();
80 
81     /**
82      * Return all the {@link IBuildInfo} tracked for this invocation.
83      */
getBuildInfos()84     public List<IBuildInfo> getBuildInfos();
85 
86     /**
87      * Return the list of serials of the device tracked in this invocation
88      */
getSerials()89     public List<String> getSerials();
90 
91     /**
92      * Return the list of device config names of the device tracked in this invocation
93      */
getDeviceConfigNames()94     public List<String> getDeviceConfigNames();
95 
96     /**
97      * Return the {@link ITestDevice} associated with the device configuration name provided.
98      */
getDevice(String deviceName)99     public ITestDevice getDevice(String deviceName);
100 
101     /**
102      * Returns the {@link ITestDevice} associated with the serial provided.
103      * Refrain from using too much as it's not the fastest lookup.
104      */
getDeviceBySerial(String serial)105     public ITestDevice getDeviceBySerial(String serial);
106 
107     /**
108      * Returns the name of the device set in the xml configuration from the {@link ITestDevice}.
109      * Returns null, if ITestDevice cannot be matched.
110      */
getDeviceName(ITestDevice device)111     public String getDeviceName(ITestDevice device);
112 
113     /**
114      * Returns the name of device set in the xml configuration from the {@link IBuildInfo}. Returns
115      * null if the IBuildInfo cannot be matched
116      */
getBuildInfoName(IBuildInfo info)117     public String getBuildInfoName(IBuildInfo info);
118 
119     /**
120      * Return the {@link IBuildInfo} associated with the device configuration name provided. Returns
121      * null, if the deviceName cannot be matched.
122      */
getBuildInfo(String deviceName)123     public IBuildInfo getBuildInfo(String deviceName);
124 
125     /**
126      * Return the {@link IBuildInfo} associated with the {@link ITestDevice}
127      */
getBuildInfo(ITestDevice testDevice)128     public IBuildInfo getBuildInfo(ITestDevice testDevice);
129 
130     /**
131      * Add a {@link IBuildInfo} to be tracked with the device configuration name.
132      *
133      * @param deviceName the device configuration name
134      * @param buildinfo a {@link IBuildInfo} associated to the device configuration name.
135      */
addDeviceBuildInfo(String deviceName, IBuildInfo buildinfo)136     public void addDeviceBuildInfo(String deviceName, IBuildInfo buildinfo);
137 
138     /**
139      * Add an Invocation attribute.
140      */
addInvocationAttribute(String attributeName, String attributeValue)141     public void addInvocationAttribute(String attributeName, String attributeValue);
142 
143     /** Add several invocation attributes at once through a {@link UniqueMultiMap}. */
addInvocationAttributes(MultiMap<String, String> attributesMap)144     public void addInvocationAttributes(MultiMap<String, String> attributesMap);
145 
146     /** Returns a copy of the map containing all the invocation attributes. */
getAttributes()147     public MultiMap<String, String> getAttributes();
148 
149     /** Add a invocation timing metric. */
150     @Deprecated
addInvocationTimingMetric(TimingEvent timingEvent, Long durationMillis)151     public default void addInvocationTimingMetric(TimingEvent timingEvent, Long durationMillis) {
152         // Do nothing
153     }
154 
155     /** Returns the map containing the invocation timing metrics. */
156     @Deprecated
getInvocationTimingMetrics()157     public default Map<TimingEvent, Long> getInvocationTimingMetrics() {
158         return new HashMap<>();
159     }
160 
161     /** Sets the descriptor associated with the test configuration that launched the invocation */
setConfigurationDescriptor(ConfigurationDescriptor configurationDescriptor)162     public void setConfigurationDescriptor(ConfigurationDescriptor configurationDescriptor);
163 
164     /**
165      * Returns the descriptor associated with the test configuration that launched the invocation
166      */
getConfigurationDescriptor()167     public ConfigurationDescriptor getConfigurationDescriptor();
168 
169     /** Sets the invocation context of module while being executed as part of a suite. */
setModuleInvocationContext(IInvocationContext invocationContext)170     public void setModuleInvocationContext(IInvocationContext invocationContext);
171 
172     /** Returns the invocation context of module while being executed as part of a suite. */
getModuleInvocationContext()173     public IInvocationContext getModuleInvocationContext();
174 
175     /** Returns the invocation test-tag. */
getTestTag()176     public String getTestTag();
177 
178     /**
179      * Sets the invocation test-tag.
180      */
setTestTag(String testTag)181     public void setTestTag(String testTag);
182 
183     /** Returns whether or not the devices were released early and won't be used anymore. */
wasReleasedEarly()184     public boolean wasReleasedEarly();
185 
186     /** Mark the devices are getting released early. */
markReleasedEarly()187     public void markReleasedEarly();
188 
189     /**
190      * Sets the {@link RecoveryMode} of all the devices part of the context
191      */
setRecoveryModeForAllDevices(RecoveryMode mode)192     public void setRecoveryModeForAllDevices(RecoveryMode mode);
193 
194     /**
195      * Add a serial to be tracked as assigned to one of the shard running some tests.
196      *
197      * @param index the index of the shard using the serials
198      * @param serials The list of serials to be tracked.
199      */
addSerialsFromShard(Integer index, List<String> serials)200     public void addSerialsFromShard(Integer index, List<String> serials);
201 
202     /**
203      * Returns the Map of all tracked serials and their shard involved in sharding. Empty if not a
204      * sharded invocation.
205      */
getShardsSerials()206     public Map<Integer, List<String>> getShardsSerials();
207 
208     /** Serialize a the context instance into a protobuf. */
toProto()209     public com.android.tradefed.invoker.proto.InvocationContext.Context toProto();
210 }
211