1 /*
2  * Copyright (C) 2015 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.compatibility.common.util;
18 
19 import com.android.tradefed.build.IBuildInfo;
20 
21 import org.xmlpull.v1.XmlPullParserException;
22 
23 import java.io.File;
24 import java.io.IOException;
25 import java.util.List;
26 import java.util.Map;
27 
28 /**
29  * Load dynamic config for device side test cases
30  */
31 public class DynamicConfigHostSide {
32 
33     public static final String CONFIG_PATH_PREFIX = "DYNAMIC_CONFIG_FILE:";
34 
35     /**
36      * Returns the value of a key from a downloaded file.
37      *
38      * @param file The file downloaded, can be retrieve via
39      *        {@link #getDynamicConfigFile(IBuildInfo, String)}
40      * @param key the key inside the file which value we want to return
41      * @return the value associated to the key in the config file provided.
42      */
getValueFromConfig(File file, String key)43     public static String getValueFromConfig(File file, String key)
44             throws XmlPullParserException, IOException {
45         Map<String, List<String>> configMap = DynamicConfig.createConfigMap(file);
46         List<String> singleValue = configMap.get(key);
47         if (singleValue == null || singleValue.size() == 0 || singleValue.size() > 1) {
48             // key must exist in the map, and map to a list containing exactly one string
49             return null;
50         }
51         return singleValue.get(0);
52     }
53 
54     /**
55      * Returns a dynamic config file downloaded in {@link DynamicConfigPusher} the path is stored
56      * in the build info under a module name.
57      *
58      * @param info the invocation {@link IBuildInfo}
59      * @param moduleName the name of the module of the file.
60      * @return a {@link File} created from the downloaded file.
61      */
getDynamicConfigFile(IBuildInfo info, String moduleName)62     public static File getDynamicConfigFile(IBuildInfo info, String moduleName) {
63         String path = info.getBuildAttributes().get(CONFIG_PATH_PREFIX + moduleName);
64         if (path!= null && !path.isEmpty()) {
65             return new File(path);
66         }
67         return null;
68     }
69 }
70