Home | History | Annotate | Download | only in util
      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 
     17 package com.android.tradefed.util;
     18 
     19 import java.io.InputStream;
     20 import java.io.IOException;
     21 import java.util.Map;
     22 import java.util.NoSuchElementException;
     23 
     24 import com.android.tradefed.build.IBuildInfo;
     25 import com.android.tradefed.log.LogUtil.CLog;
     26 import com.android.tradefed.util.StreamUtil;
     27 
     28 import org.json.JSONException;
     29 import org.json.JSONObject;
     30 
     31 /**
     32  * Util to access a VTS config file.
     33  */
     34 public class VtsVendorConfigFileUtil {
     35     public static final String KEY_VENDOR_TEST_CONFIG_DEFAULT_TYPE =
     36             "vts-vendor-config:default-type";
     37     public static final String KEY_VENDOR_TEST_CONFIG_FILE_PATH = "vts-vendor-config:file-path";
     38     public static final String VENDOR_TEST_CONFIG_DEFAULT_TYPE = "prod";
     39     public static final String VENDOR_TEST_CONFIG_FILE_PATH_PROD =
     40             "/config/vts-tradefed-vendor-config-prod.json";
     41     public static final String VENDOR_TEST_CONFIG_FILE_PATH_TEST =
     42             "/config/vts-tradefed-vendor-config-test.json";
     43 
     44     private JSONObject vendorConfigJson = null;
     45 
     46     // The path of a VTS vendor config file (format: json).
     47     private String mVendorConfigFilePath = null;
     48 
     49     // The default config file type, e.g., `prod` or `test`.
     50     private String mDefaultType = VENDOR_TEST_CONFIG_DEFAULT_TYPE;
     51 
     52     /**
     53      * Returns the specified vendor config file path.
     54      */
     55     public String GetVendorConfigFilePath() {
     56         if (mVendorConfigFilePath == null) {
     57             if (mDefaultType.toLowerCase().equals(VENDOR_TEST_CONFIG_DEFAULT_TYPE)) {
     58                 mVendorConfigFilePath = VENDOR_TEST_CONFIG_FILE_PATH_PROD;
     59             } else {
     60                 mVendorConfigFilePath = VENDOR_TEST_CONFIG_FILE_PATH_TEST;
     61             }
     62         }
     63         return mVendorConfigFilePath;
     64     }
     65 
     66     /**
     67      * Loads a VTS vendor config file.
     68      *
     69      * @param configPath, the path of a config file.
     70      * @throws RuntimeException
     71      */
     72     public boolean LoadVendorConfig(String configPath) throws RuntimeException {
     73         if (configPath == null || configPath.length() == 0) {
     74             configPath = GetVendorConfigFilePath();
     75         }
     76         CLog.i("Loading vendor test config %s", configPath);
     77         InputStream config = getClass().getResourceAsStream(configPath);
     78         if (config == null) {
     79             CLog.e("Vendor test config file %s does not exist", configPath);
     80             return false;
     81         }
     82         try {
     83             String content = StreamUtil.getStringFromStream(config);
     84             if (content == null) {
     85                 CLog.e("Loaded vendor test config is empty");
     86                 return false;
     87             }
     88             CLog.i("Loaded vendor test config %s", content);
     89             vendorConfigJson = new JSONObject(content);
     90         } catch (IOException e) {
     91             throw new RuntimeException("Failed to read vendor config json file");
     92         } catch (JSONException e) {
     93             throw new RuntimeException("Failed to parse vendor config json data");
     94         }
     95         return true;
     96     }
     97 
     98     /**
     99      * Loads a VTS vendor config file.
    100      *
    101      * @param defaultType, The default config file type, e.g., `prod` or `test`.
    102      * @param vendorConfigFilePath, The path of a VTS vendor config file (format: json).
    103      * @throws RuntimeException
    104      */
    105     public boolean LoadVendorConfig(String defaultType, String vendorConfigFilePath)
    106             throws RuntimeException {
    107         mDefaultType = defaultType;
    108         mVendorConfigFilePath = vendorConfigFilePath;
    109         return LoadVendorConfig("");
    110     }
    111 
    112     /**
    113      * Loads a VTS vendor config file.
    114      *
    115      * @param defaultType, The default config file type, e.g., `prod` or `test`.
    116      * @param vendorConfigFilePath, The path of a VTS vendor config file (format: json).
    117      * @throws RuntimeException
    118      */
    119     public boolean LoadVendorConfig(IBuildInfo buildInfo) throws RuntimeException {
    120         Map<String, String> attrs = buildInfo.getBuildAttributes();
    121         if (attrs.containsKey(KEY_VENDOR_TEST_CONFIG_DEFAULT_TYPE)) {
    122             mDefaultType = attrs.get(KEY_VENDOR_TEST_CONFIG_DEFAULT_TYPE);
    123         } else {
    124             CLog.i("No default vendor test configuration provided. Defaulting to prod.");
    125         }
    126         mVendorConfigFilePath = attrs.get(KEY_VENDOR_TEST_CONFIG_FILE_PATH);
    127         return LoadVendorConfig(mDefaultType, mVendorConfigFilePath);
    128     }
    129 
    130     /**
    131      * Gets the value of a config variable.
    132      *
    133      * @param varName, the name of a variable.
    134      * @throws NoSuchElementException
    135      */
    136     public String GetVendorConfigVariable(String varName) throws NoSuchElementException {
    137         if (vendorConfigJson == null) {
    138             CLog.e("Vendor config json file invalid or not yet loaded.");
    139             throw new NoSuchElementException("config is empty");
    140         }
    141         try {
    142             return vendorConfigJson.getString(varName);
    143         } catch (JSONException e) {
    144             CLog.e("Vendor config file does not define %s", varName);
    145             throw new NoSuchElementException("config parsing error");
    146         }
    147     }
    148 
    149     /**
    150      * Returns the current vendor config json object.
    151      */
    152     public JSONObject GetVendorConfigJson() {
    153         return vendorConfigJson;
    154     }
    155 }
    156