/* * Copyright (C) 2016 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.android.tradefed.invoker; import com.android.tradefed.build.BuildInfo; import com.android.tradefed.build.IBuildInfo; import com.android.tradefed.device.ITestDevice; import com.android.tradefed.device.ITestDevice.RecoveryMode; import com.android.tradefed.log.LogUtil.CLog; import com.android.tradefed.util.MultiMap; import com.android.tradefed.util.UniqueMultiMap; import java.util.ArrayList; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; /** * Generic implementation of a {@link IInvocationContext}. */ public class InvocationContext implements IInvocationContext { private Map mAllocatedDeviceAndBuildMap; /** Map of the configuration device name and the actual {@link ITestDevice} **/ private Map mNameAndDeviceMap; private Map mNameAndBuildinfoMap; private final UniqueMultiMap mInvocationAttributes = new UniqueMultiMap(); /** Invocation test-tag **/ private String mTestTag; /** * Creates a {@link BuildInfo} using default attribute values. */ public InvocationContext() { mAllocatedDeviceAndBuildMap = new HashMap(); // Use LinkedHashMap to ensure key ordering by insertion order mNameAndDeviceMap = new LinkedHashMap(); mNameAndBuildinfoMap = new LinkedHashMap(); } /** * {@inheritDoc} */ @Override public int getNumDevicesAllocated() { return mAllocatedDeviceAndBuildMap.size(); } /** * {@inheritDoc} */ @Override public void addAllocatedDevice(String devicename, ITestDevice testDevice) { mNameAndDeviceMap.put(devicename, testDevice); } /** * {@inheritDoc} */ @Override public void addAllocatedDevice(Map deviceWithName) { mNameAndDeviceMap.putAll(deviceWithName); } /** * {@inheritDoc} */ @Override public Map getDeviceBuildMap() { return mAllocatedDeviceAndBuildMap; } /** * {@inheritDoc} */ @Override public List getDevices() { return new ArrayList(mNameAndDeviceMap.values()); } /** * {@inheritDoc} */ @Override public List getBuildInfos() { return new ArrayList(mNameAndBuildinfoMap.values()); } /** * {@inheritDoc} */ @Override public List getSerials() { List listSerials = new ArrayList(); for (ITestDevice testDevice : mNameAndDeviceMap.values()) { listSerials.add(testDevice.getSerialNumber()); } return listSerials; } /** * {@inheritDoc} */ @Override public List getDeviceConfigNames() { List listNames = new ArrayList(); listNames.addAll(mNameAndDeviceMap.keySet()); return listNames; } /** * {@inheritDoc} */ @Override public ITestDevice getDevice(String deviceName) { return mNameAndDeviceMap.get(deviceName); } /** * {@inheritDoc} */ @Override public IBuildInfo getBuildInfo(String deviceName) { return mNameAndBuildinfoMap.get(deviceName); } /** * {@inheritDoc} */ @Override public void addDeviceBuildInfo(String deviceName, IBuildInfo buildinfo) { mNameAndBuildinfoMap.put(deviceName, buildinfo); mAllocatedDeviceAndBuildMap.put(getDevice(deviceName), buildinfo); } /** * {@inheritDoc} */ @Override public IBuildInfo getBuildInfo(ITestDevice testDevice) { return mAllocatedDeviceAndBuildMap.get(testDevice); } /** * {@inheritDoc} */ @Override public void addInvocationAttribute(String attributeName, String attributeValue) { mInvocationAttributes.put(attributeName, attributeValue); } /** * {@inheritDoc} */ @Override public MultiMap getAttributes() { return mInvocationAttributes; } /** * {@inheritDoc} */ @Override public ITestDevice getDeviceBySerial(String serial) { for (ITestDevice testDevice : mNameAndDeviceMap.values()) { if (testDevice.getSerialNumber().equals(serial)) { return testDevice; } } CLog.d("Device with serial '%s', not found in the metadata", serial); return null; } /** {@inheritDoc} */ @Override public String getDeviceName(ITestDevice device) { for (String name : mNameAndDeviceMap.keySet()) { if (device.equals(getDevice(name))) { return name; } } CLog.d( "Device with serial '%s' doesn't match a name in the metadata", device.getSerialNumber()); return null; } /** {@inheritDoc} */ @Override public String getTestTag() { return mTestTag; } /** * {@inheritDoc} */ @Override public void setTestTag(String testTag) { mTestTag = testTag; } /** * {@inheritDoc} */ @Override public void setRecoveryModeForAllDevices(RecoveryMode mode) { for (ITestDevice device : getDevices()) { device.setRecoveryMode(mode); } } }