1 /*
2  * Copyright (C) 2021 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.device.internal;
18 
19 import com.android.tradefed.command.ICommandScheduler.IScheduledInvocationListener;
20 import com.android.tradefed.device.FreeDeviceState;
21 import com.android.tradefed.device.ITestDevice;
22 import com.android.tradefed.invoker.TestInformation;
23 import com.android.tradefed.service.internal.IRemoteScheduledListenersFeature;
24 import com.android.tradefed.testtype.ITestInformationReceiver;
25 
26 import com.proto.tradefed.feature.FeatureRequest;
27 import com.proto.tradefed.feature.FeatureResponse;
28 
29 import java.util.LinkedHashMap;
30 import java.util.List;
31 import java.util.Map;
32 
33 /** Feature server implementation for early device release. */
34 public class EarlyDeviceReleaseFeature
35         implements ITestInformationReceiver, IRemoteScheduledListenersFeature {
36 
37     public static final String EARLY_DEVICE_RELEASE_FEATURE_NAME = "earlyDeviceRelease";
38 
39     private TestInformation mTestInformation;
40     private List<IScheduledInvocationListener> mScheduledInvocationListeners;
41 
42     @Override
getName()43     public String getName() {
44         return EARLY_DEVICE_RELEASE_FEATURE_NAME;
45     }
46 
47     @Override
setTestInformation(TestInformation testInformation)48     public void setTestInformation(TestInformation testInformation) {
49         mTestInformation = testInformation;
50     }
51 
52     @Override
getTestInformation()53     public TestInformation getTestInformation() {
54         return mTestInformation;
55     }
56 
57     @Override
setListeners(List<IScheduledInvocationListener> listeners)58     public void setListeners(List<IScheduledInvocationListener> listeners) {
59         mScheduledInvocationListeners = listeners;
60     }
61 
62     @Override
getListeners()63     public List<IScheduledInvocationListener> getListeners() {
64         return mScheduledInvocationListeners;
65     }
66 
67     @Override
execute(FeatureRequest featureRequest)68     public FeatureResponse execute(FeatureRequest featureRequest) {
69         Map<String, String> deviceStatusMap = featureRequest.getArgsMap();
70         Map<ITestDevice, FreeDeviceState> deviceStates = new LinkedHashMap<>();
71         int index = 0;
72         for (Map.Entry<String, String> entry : deviceStatusMap.entrySet()) {
73             ITestDevice device = mTestInformation.getContext().getDevice(entry.getKey());
74             if (device == null) {
75                 device = mTestInformation.getContext().getDevices().get(index);
76             }
77             deviceStates.put(device, FreeDeviceState.valueOf(entry.getValue()));
78             index++;
79         }
80         mTestInformation.getContext().markReleasedEarly();
81         for (IScheduledInvocationListener listener : mScheduledInvocationListeners) {
82             listener.releaseDevices(mTestInformation.getContext(), deviceStates);
83         }
84 
85         return FeatureResponse.newBuilder().build();
86     }
87 }
88