• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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.compatibility.common.tradefed.util;
17 
18 import com.android.compatibility.common.tradefed.build.CompatibilityBuildHelper;
19 import com.android.compatibility.common.util.DynamicConfig;
20 import com.android.tradefed.build.IBuildInfo;
21 import com.android.tradefed.error.HarnessRuntimeException;
22 import com.android.tradefed.log.LogUtil.CLog;
23 import com.android.tradefed.result.error.InfraErrorIdentifier;
24 import org.xmlpull.v1.XmlPullParserException;
25 
26 import java.io.File;
27 import java.io.IOException;
28 import java.util.List;
29 import java.util.Map;
30 
31 /**
32  * Utility to read the data from a dynamic config file.
33  */
34 public class DynamicConfigFileReader {
35 
36     /**
37      * Returns the value of a key from a downloaded file.
38      *
39      * @param file The file downloaded, can be retrieve via
40      *        {@link CompatibilityBuildHelper#getDynamicConfigFiles()}
41      * @param key the key inside the file which value we want to return
42      * @return the value associated to the key in the config file provided.
43      */
getValueFromConfig(File file, String key)44     public static String getValueFromConfig(File file, String key)
45             throws XmlPullParserException, IOException {
46         DynamicConfig config = new DynamicConfig();
47         config.initializeConfig(file);
48         return config.getValue(key);
49     }
50 
51     /**
52      * Returns the multiple values of a key from a downloaded file.
53      *
54      * @param file The file downloaded, can be retrieve via
55      *        {@link CompatibilityBuildHelper#getDynamicConfigFiles()}
56      * @param key the key inside the file which values we want to return
57      * @return the values associated to the key in the config file provided.
58      */
getValuesFromConfig(File file, String key)59     public static List<String> getValuesFromConfig(File file, String key)
60             throws XmlPullParserException, IOException {
61         DynamicConfig config = new DynamicConfig();
62         config.initializeConfig(file);
63         return config.getValues(key);
64     }
65 
66     /**
67      * Returns the value of a key from the build info and module targeted.
68      *
69      * @param info the {@link IBuildInfo} of the run.
70      * @param moduleName the name of the module we need the dynamic file from.
71      * @param key the key inside the file which value we want to return
72      * @return the value associated to the key in the dynamic config associated with the module.
73      */
getValueFromConfig(IBuildInfo info, String moduleName, String key)74     public static String getValueFromConfig(IBuildInfo info, String moduleName, String key)
75             throws XmlPullParserException, IOException {
76         CompatibilityBuildHelper helper = new CompatibilityBuildHelper(info);
77         File dynamicConfig = helper.getDynamicConfigFiles().get(moduleName);
78         if (dynamicConfig == null) {
79             CLog.w("Config file %s, not found in the map of dynamic configs.", moduleName);
80             return null;
81         }
82         return getValueFromConfig(dynamicConfig, key);
83     }
84 
85     /**
86      * Returns the multiple values of a key from the build info and module targeted.
87      *
88      * @param info the {@link IBuildInfo} of the run.
89      * @param moduleName the name of the module we need the dynamic file from.
90      * @param key the key inside the file which values we want to return
91      * @return the values associated to the key in the dynamic config associated with the module.
92      */
getValuesFromConfig(IBuildInfo info, String moduleName, String key)93     public static List<String> getValuesFromConfig(IBuildInfo info, String moduleName, String key)
94             throws XmlPullParserException, IOException {
95         CompatibilityBuildHelper helper = new CompatibilityBuildHelper(info);
96         File dynamicConfig = helper.getDynamicConfigFiles().get(moduleName);
97         if (dynamicConfig == null) {
98             String message =
99                     String.format(
100                             "Dynamic config file %s, not found in the map of configured dynamic"
101                                     + " configs.",
102                             moduleName);
103             CLog.w(message);
104             throw new HarnessRuntimeException(
105                     message, InfraErrorIdentifier.CONFIGURED_ARTIFACT_NOT_FOUND);
106         }
107         return getValuesFromConfig(dynamicConfig, key);
108     }
109 
110     /**
111      * Returns the multiple values of a key from the build info and modules targeted.
112      *
113      * @param info the {@link IBuildInfo} of the run.
114      * @param suiteNames list of suite names we need the dynamic file from.
115      * @param key the key inside the file which values we want to return
116      * @return the values associated to the key in the dynamic config associated with the module.
117      */
getValuesFromConfig(IBuildInfo info, List<String> suiteNames, String key)118     public static List<String> getValuesFromConfig(IBuildInfo info, List<String> suiteNames,
119             String key)
120             throws XmlPullParserException, IOException {
121         CompatibilityBuildHelper helper = new CompatibilityBuildHelper(info);
122         Map<String, File> dynamicConfigFiles = helper.getDynamicConfigFiles();
123         CLog.i("config files: %s", dynamicConfigFiles);
124         for (String suiteName : suiteNames) {
125             File dynamicConfig = dynamicConfigFiles.get(suiteName);
126             if (dynamicConfig != null) {
127                 return getValuesFromConfig(dynamicConfig, key);
128             }
129         }
130         String message =
131                 String.format(
132                         "Dynamic config files %s, not found in the map of configured dynamic"
133                                 + " configs: %s",
134                         suiteNames, dynamicConfigFiles);
135         CLog.w(message);
136         throw new HarnessRuntimeException(
137                 message, InfraErrorIdentifier.CONFIGURED_ARTIFACT_NOT_FOUND);
138     }
139 }
140