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.config; 17 18 import com.android.tradefed.build.IBuildProvider; 19 import com.android.tradefed.build.StubBuildProvider; 20 import com.android.tradefed.device.DeviceSelectionOptions; 21 import com.android.tradefed.device.IDeviceRecovery; 22 import com.android.tradefed.device.IDeviceSelection; 23 import com.android.tradefed.device.TestDeviceOptions; 24 import com.android.tradefed.device.WaitDeviceRecovery; 25 import com.android.tradefed.log.LogUtil.CLog; 26 import com.android.tradefed.targetprep.ITargetPreparer; 27 28 import java.util.ArrayList; 29 import java.util.Arrays; 30 import java.util.HashMap; 31 import java.util.List; 32 import java.util.Map; 33 34 /** 35 * A concrete {@link IDeviceConfiguration} implementation that stores the loaded device 36 * configuration objects in its attributes. 37 */ 38 public class DeviceConfigurationHolder implements IDeviceConfiguration { 39 private final String mDeviceName; 40 private final boolean mIsFake; 41 42 private IBuildProvider mBuildProvider = new StubBuildProvider(); 43 private List<ITargetPreparer> mListTargetPreparer = new ArrayList<>(); 44 private IDeviceRecovery mDeviceRecovery = new WaitDeviceRecovery(); 45 private IDeviceSelection mDeviceSelection = new DeviceSelectionOptions(); 46 private TestDeviceOptions mTestDeviceOption = new TestDeviceOptions(); 47 48 private Map<Object, Integer> mFreqMap = new HashMap<>(); 49 DeviceConfigurationHolder()50 public DeviceConfigurationHolder() { 51 this("", false); 52 } 53 DeviceConfigurationHolder(String deviceName)54 public DeviceConfigurationHolder(String deviceName) { 55 this(deviceName, false); 56 } 57 DeviceConfigurationHolder(String deviceName, boolean isFake)58 public DeviceConfigurationHolder(String deviceName, boolean isFake) { 59 mDeviceName = deviceName; 60 mIsFake = isFake; 61 } 62 63 /** 64 * {@inheritDoc} 65 */ 66 @Override getDeviceName()67 public String getDeviceName() { 68 return mDeviceName; 69 } 70 71 /** {@inheritDoc} */ 72 @Override isFake()73 public boolean isFake() { 74 return mIsFake; 75 } 76 77 /** 78 * {@inheritDoc} 79 */ 80 @Override addSpecificConfig(Object config)81 public void addSpecificConfig(Object config) throws ConfigurationException { 82 if (config instanceof IBuildProvider) { 83 mBuildProvider = (IBuildProvider) config; 84 } else if (config instanceof ITargetPreparer){ 85 if (isFake()) { 86 throw new ConfigurationException( 87 "cannot specify a target_preparer for a isFake=true device."); 88 } 89 mListTargetPreparer.add((ITargetPreparer) config); 90 } else if (config instanceof IDeviceRecovery) { 91 mDeviceRecovery = (IDeviceRecovery) config; 92 } else if (config instanceof IDeviceSelection) { 93 mDeviceSelection = (IDeviceSelection) config; 94 } else if (config instanceof TestDeviceOptions) { 95 mTestDeviceOption = (TestDeviceOptions) config; 96 } else { 97 throw new ConfigurationException(String.format("Cannot add %s class " 98 + "to a device specific definition", config.getClass())); 99 } 100 } 101 102 @Override removeObjectType(String type)103 public void removeObjectType(String type) throws ConfigurationException { 104 if (Configuration.BUILD_PROVIDER_TYPE_NAME.equals(type)) { 105 mBuildProvider = null; 106 } else if (Configuration.TARGET_PREPARER_TYPE_NAME.equals(type)) { 107 mListTargetPreparer.clear(); 108 } else if (Configuration.DEVICE_RECOVERY_TYPE_NAME.equals(type)) { 109 mDeviceRecovery = null; 110 } else if (Configuration.DEVICE_REQUIREMENTS_TYPE_NAME.equals(type)) { 111 mDeviceSelection = null; 112 } else if (Configuration.DEVICE_OPTIONS_TYPE_NAME.equals(type)) { 113 mTestDeviceOption = null; 114 } else { 115 throw new ConfigurationException( 116 String.format("'%s' type is not supported by DeviceConfigurationHolder", type)); 117 } 118 } 119 120 /** {@inheritDoc} */ 121 @Override addFrequency(Object config, Integer frequency)122 public void addFrequency(Object config, Integer frequency) { 123 mFreqMap.put(config, frequency); 124 } 125 126 /** {@inheritDoc} */ 127 @Override getFrequency(Object config)128 public Integer getFrequency(Object config) { 129 return mFreqMap.get(config); 130 } 131 132 /** {@inheritDoc} */ 133 @Override getAllObjects()134 public List<Object> getAllObjects() { 135 List<Object> allObject = new ArrayList<Object>(); 136 allObject.add(mBuildProvider); 137 allObject.addAll(mListTargetPreparer); 138 allObject.add(mDeviceRecovery); 139 allObject.add(mDeviceSelection); 140 allObject.add(mTestDeviceOption); 141 return allObject; 142 } 143 144 /** {@inheritDoc} */ 145 @Override getAllObjectOfType(String configType)146 public List<Object> getAllObjectOfType(String configType) { 147 switch (configType) { 148 case Configuration.BUILD_PROVIDER_TYPE_NAME: 149 return Arrays.asList(mBuildProvider); 150 case Configuration.TARGET_PREPARER_TYPE_NAME: 151 return new ArrayList<>(mListTargetPreparer); 152 case Configuration.DEVICE_RECOVERY_TYPE_NAME: 153 return Arrays.asList(mDeviceRecovery); 154 case Configuration.DEVICE_REQUIREMENTS_TYPE_NAME: 155 return Arrays.asList(mDeviceSelection); 156 case Configuration.DEVICE_OPTIONS_TYPE_NAME: 157 return Arrays.asList(mTestDeviceOption); 158 default: 159 return new ArrayList<>(); 160 } 161 } 162 163 /** 164 * {@inheritDoc} 165 */ 166 @Override getBuildProvider()167 public IBuildProvider getBuildProvider() { 168 return mBuildProvider; 169 } 170 171 /** 172 * {@inheritDoc} 173 */ 174 @Override getTargetPreparers()175 public List<ITargetPreparer> getTargetPreparers() { 176 return mListTargetPreparer; 177 } 178 179 /** 180 * {@inheritDoc} 181 */ 182 @Override getDeviceRecovery()183 public IDeviceRecovery getDeviceRecovery() { 184 return mDeviceRecovery; 185 } 186 187 /** 188 * {@inheritDoc} 189 */ 190 @Override getDeviceRequirements()191 public IDeviceSelection getDeviceRequirements() { 192 // We should never question what to allocate for fake placeholder. Only null device would 193 // do. 194 if (isFake()) { 195 DeviceSelectionOptions select = new DeviceSelectionOptions(); 196 select.setNullDeviceRequested(true); 197 return select; 198 } 199 return mDeviceSelection; 200 } 201 202 /** 203 * {@inheritDoc} 204 */ 205 @Override getDeviceOptions()206 public TestDeviceOptions getDeviceOptions() { 207 return mTestDeviceOption; 208 } 209 210 /** 211 * {@inheritDoc} 212 */ 213 @Override clone()214 public IDeviceConfiguration clone() { 215 return clone(getDeviceName()); 216 } 217 218 @Override clone(String newName)219 public IDeviceConfiguration clone(String newName) { 220 IDeviceConfiguration newDeviceConfig = new DeviceConfigurationHolder(newName); 221 for (Object obj : getAllObjects()) { 222 try { 223 newDeviceConfig.addSpecificConfig(obj); 224 } catch (ConfigurationException e) { 225 // Shoud never happen. 226 CLog.e(e); 227 } 228 } 229 return newDeviceConfig; 230 } 231 } 232