• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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<ITargetPreparer>();
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     /** {@inheritDoc} */
103     @Override
addFrequency(Object config, Integer frequency)104     public void addFrequency(Object config, Integer frequency) {
105         mFreqMap.put(config, frequency);
106     }
107 
108     /** {@inheritDoc} */
109     @Override
getFrequency(Object config)110     public Integer getFrequency(Object config) {
111         return mFreqMap.get(config);
112     }
113 
114     /** {@inheritDoc} */
115     @Override
getAllObjects()116     public List<Object> getAllObjects() {
117         List<Object> allObject = new ArrayList<Object>();
118         allObject.add(mBuildProvider);
119         allObject.addAll(mListTargetPreparer);
120         allObject.add(mDeviceRecovery);
121         allObject.add(mDeviceSelection);
122         allObject.add(mTestDeviceOption);
123         return allObject;
124     }
125 
126     /** {@inheritDoc} */
127     @Override
getAllObjectOfType(String configType)128     public List<Object> getAllObjectOfType(String configType) {
129         switch (configType) {
130             case Configuration.BUILD_PROVIDER_TYPE_NAME:
131                 return Arrays.asList(mBuildProvider);
132             case Configuration.TARGET_PREPARER_TYPE_NAME:
133                 return new ArrayList<>(mListTargetPreparer);
134             case Configuration.DEVICE_RECOVERY_TYPE_NAME:
135                 return Arrays.asList(mDeviceRecovery);
136             case Configuration.DEVICE_REQUIREMENTS_TYPE_NAME:
137                 return Arrays.asList(mDeviceSelection);
138             case Configuration.DEVICE_OPTIONS_TYPE_NAME:
139                 return Arrays.asList(mTestDeviceOption);
140             default:
141                 return new ArrayList<>();
142         }
143     }
144 
145     /**
146      * {@inheritDoc}
147      */
148     @Override
getBuildProvider()149     public IBuildProvider getBuildProvider() {
150         return mBuildProvider;
151     }
152 
153     /**
154      * {@inheritDoc}
155      */
156     @Override
getTargetPreparers()157     public List<ITargetPreparer> getTargetPreparers() {
158         return mListTargetPreparer;
159     }
160 
161     /**
162      * {@inheritDoc}
163      */
164     @Override
getDeviceRecovery()165     public IDeviceRecovery getDeviceRecovery() {
166         return mDeviceRecovery;
167     }
168 
169     /**
170      * {@inheritDoc}
171      */
172     @Override
getDeviceRequirements()173     public IDeviceSelection getDeviceRequirements() {
174         // We should never question what to allocate for fake placeholder. Only null device would
175         // do.
176         if (isFake()) {
177             DeviceSelectionOptions select = new DeviceSelectionOptions();
178             select.setNullDeviceRequested(true);
179             return select;
180         }
181         return mDeviceSelection;
182     }
183 
184     /**
185      * {@inheritDoc}
186      */
187     @Override
getDeviceOptions()188     public TestDeviceOptions getDeviceOptions() {
189         return mTestDeviceOption;
190     }
191 
192     /**
193      * {@inheritDoc}
194      */
195     @Override
clone()196     public IDeviceConfiguration clone() {
197         IDeviceConfiguration newDeviceConfig = new DeviceConfigurationHolder(getDeviceName());
198         for (Object obj : getAllObjects()) {
199             try {
200                 newDeviceConfig.addSpecificConfig(obj);
201             } catch (ConfigurationException e) {
202                 // Shoud never happen.
203                 CLog.e(e);
204             }
205         }
206         return newDeviceConfig;
207     }
208 }
209