1 /*
2  * Copyright (C) 2019 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.cluster;
17 
18 import com.android.tradefed.config.Configuration;
19 import com.android.tradefed.result.ITestInvocationListener;
20 import com.android.tradefed.targetprep.ITargetPreparer;
21 import com.android.tradefed.util.MultiMap;
22 
23 import org.json.JSONArray;
24 import org.json.JSONException;
25 import org.json.JSONObject;
26 
27 import java.util.ArrayList;
28 import java.util.List;
29 
30 /**
31  * A class to model a TradefedConfigObject message of TFC API.
32  *
33  * <p>A TradefedConfigObject message is a part of a TestEnvironment message and used to pass down
34  * extra {@link ITargetPreparer} or {@link ITestInvocationListener} to be added to a cluster command
35  * launcher config.
36  */
37 public class TradefedConfigObject {
38 
39     /**
40      * A list of configuration object types which can be injected to a cluster command config.
41      *
42      * <p>This must be a subset of configuration object types defined in {@link Configuration}.
43      */
44     public static enum Type {
45         UNKNOWN,
46         TARGET_PREPARER,
47         RESULT_REPORTER
48     }
49 
50     private final Type mType;
51     private final String mClassName;
52     private final MultiMap<String, String> mOptionValues;
53 
TradefedConfigObject(Type type, String className, MultiMap<String, String> optionValues)54     TradefedConfigObject(Type type, String className, MultiMap<String, String> optionValues) {
55         mType = type;
56         mClassName = className;
57         mOptionValues = optionValues;
58     }
59 
getType()60     public Type getType() {
61         return mType;
62     }
63 
getClassName()64     public String getClassName() {
65         return mClassName;
66     }
67 
getOptionValues()68     public MultiMap<String, String> getOptionValues() {
69         return mOptionValues;
70     }
71 
fromJson(JSONObject json)72     public static TradefedConfigObject fromJson(JSONObject json) throws JSONException {
73         MultiMap<String, String> optionValues = new MultiMap<>();
74         JSONArray arr = json.optJSONArray("option_values");
75         if (arr != null) {
76             for (int i = 0; i < arr.length(); i++) {
77                 JSONObject pair = arr.getJSONObject(i);
78                 String key = pair.getString("key");
79                 JSONArray valueArr = pair.optJSONArray("values");
80                 if (valueArr == null) {
81                     optionValues.put(key, null);
82                 } else {
83                     for (int j = 0; j < valueArr.length(); j++) {
84                         optionValues.put(key, valueArr.getString(j));
85                     }
86                 }
87             }
88         }
89         Type type = Type.valueOf(json.optString("type", Type.UNKNOWN.name()));
90         return new TradefedConfigObject(type, json.getString("class_name"), optionValues);
91     }
92 
fromJsonArray(JSONArray arr)93     public static List<TradefedConfigObject> fromJsonArray(JSONArray arr) throws JSONException {
94         List<TradefedConfigObject> objs = new ArrayList<>();
95         for (int i = 0; i < arr.length(); i++) {
96             objs.add(fromJson(arr.getJSONObject(i)));
97         }
98         return objs;
99     }
100 }
101