1 /* 2 * Copyright (C) 2020 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.command; 17 18 import com.android.tradefed.device.ITestDevice; 19 20 import java.util.LinkedHashMap; 21 import java.util.Map; 22 23 /** Represents the results of an allocation attempt for a command. */ 24 public class DeviceAllocationResult { 25 26 private Map<String, String> mNotAllocatedReason = new LinkedHashMap<>(); 27 private Map<String, ITestDevice> mAllocatedDevices = new LinkedHashMap<>(); 28 29 /** returns whether or not the allocation was successful. */ wasAllocationSuccessful()30 public boolean wasAllocationSuccessful() { 31 return !mAllocatedDevices.isEmpty(); 32 } 33 34 /** Add devices that have been allocated. */ addAllocatedDevices(Map<String, ITestDevice> devices)35 public void addAllocatedDevices(Map<String, ITestDevice> devices) { 36 mAllocatedDevices.putAll(devices); 37 } 38 39 /** Add the reasons for not being allocated for each device config. */ addAllocationFailureReason(String deviceConfigName, Map<String, String> reasons)40 public void addAllocationFailureReason(String deviceConfigName, Map<String, String> reasons) { 41 mNotAllocatedReason.put(deviceConfigName, createReasonMessage(reasons)); 42 } 43 44 /** Returns the map of allocated devices */ getAllocatedDevices()45 public Map<String, ITestDevice> getAllocatedDevices() { 46 return mAllocatedDevices; 47 } 48 formattedReason()49 public String formattedReason() { 50 if (mNotAllocatedReason.size() == 1) { 51 return mNotAllocatedReason.values().iterator().next().toString(); 52 } 53 return mNotAllocatedReason.toString(); 54 } 55 createReasonMessage(Map<String, String> reasons)56 private String createReasonMessage(Map<String, String> reasons) { 57 StringBuilder sb = new StringBuilder(); 58 for (String serial : reasons.keySet()) { 59 String reason = reasons.get(serial); 60 if (reason == null) { 61 reason = "No reason provided"; 62 } 63 sb.append(String.format("device '%s': %s", serial, reason)); 64 } 65 if (reasons.isEmpty()) { 66 sb.append("No reason returned."); 67 } 68 return sb.toString(); 69 } 70 } 71