Home | History | Annotate | Download | only in rpc
      1 package autotest.moblab.rpc;
      2 
      3 import com.google.gwt.json.client.JSONNull;
      4 import com.google.gwt.json.client.JSONNumber;
      5 import com.google.gwt.json.client.JSONObject;
      6 import com.google.gwt.json.client.JSONString;
      7 import com.google.gwt.json.client.JSONValue;
      8 
      9 import autotest.common.JsonRpcCallback;
     10 import autotest.common.JsonRpcProxy;
     11 import autotest.common.SimpleCallback;
     12 
     13 import com.google.gwt.user.client.Window;
     14 import java.util.LinkedList;
     15 import java.util.List;
     16 import java.util.Map;
     17 
     18 /**
     19  * A helper class for moblab RPC call.
     20  */
     21 public class MoblabRpcHelper {
     22   public static final String RPC_PARAM_CLOUD_STORAGE_INFO = "cloud_storage_info";
     23   public static final String RPC_PARAM_WIFI_INFO = "wifi_info";
     24 
     25   private MoblabRpcHelper() {}
     26 
     27   /**
     28    * Fetches config data.
     29    */
     30   public static void fetchConfigData(final SimpleCallback callback) {
     31     fetchConfigData(new JsonRpcCallback() {
     32       @Override
     33       public void onSuccess(JSONValue result) {
     34         if (callback != null)
     35           callback.doCallback(result);
     36       }
     37     });
     38   }
     39 
     40   /**
     41    * Fetch config data.
     42    */
     43   public static void fetchConfigData(final JsonRpcCallback callback) {
     44     JsonRpcProxy rpcProxy = JsonRpcProxy.getProxy();
     45     rpcProxy.rpcCall("get_config_values", null, callback);
     46   }
     47 
     48   /**
     49    * Submits config data.
     50    */
     51   public static void submitConfigData(JSONObject configValues, JsonRpcCallback callback) {
     52     JSONObject params = new JSONObject();
     53     params.put("config_values", configValues);
     54     JsonRpcProxy rpcProxy = JsonRpcProxy.getProxy();
     55     rpcProxy.rpcCall("update_config_handler", params, callback);
     56   }
     57 
     58   /**
     59    * Resets config data on Moblab.
     60    */
     61   public static void resetConfigData(final JsonRpcCallback callback) {
     62     JsonRpcProxy rpcProxy = JsonRpcProxy.getProxy();
     63     rpcProxy.rpcCall("reset_config_settings", null, callback);
     64   }
     65 
     66   /**
     67    * Reboot Moblab device.
     68    */
     69   public static void rebootMoblab(final JsonRpcCallback callback) {
     70     JsonRpcProxy rpcProxy = JsonRpcProxy.getProxy();
     71     rpcProxy.rpcCall("reboot_moblab", null, callback);
     72   }
     73 
     74   /**
     75    * Fetches the server network information.
     76    */
     77   public static void fetchNetworkInfo(
     78       final MoblabRpcCallbacks.FetchNetworkInfoRpcCallback callback) {
     79     JsonRpcProxy rpcProxy = JsonRpcProxy.getProxy();
     80     rpcProxy.rpcCall("get_network_info", null, new JsonRpcCallback() {
     81       @Override
     82       public void onSuccess(JSONValue result) {
     83         NetworkInfo networkInfo = new NetworkInfo();
     84         networkInfo.fromJson(result.isObject());
     85         callback.onNetworkInfoFetched(networkInfo);
     86       }
     87     });
     88   }
     89 
     90   /**
     91    * Fetches the cloud storage configuration information.
     92    */
     93   public static void fetchCloudStorageInfo(
     94       final MoblabRpcCallbacks.FetchCloudStorageInfoCallback callback) {
     95     JsonRpcProxy rpcProxy = JsonRpcProxy.getProxy();
     96     rpcProxy.rpcCall("get_cloud_storage_info", null, new JsonRpcCallback() {
     97       @Override
     98       public void onSuccess(JSONValue result) {
     99         CloudStorageInfo info = new CloudStorageInfo();
    100         info.fromJson(result.isObject());
    101         callback.onCloudStorageInfoFetched(info);
    102       }
    103     });
    104   }
    105 
    106   /**
    107    * Validates the cloud storage configuration information.
    108    */
    109   public static void validateCloudStorageInfo(CloudStorageInfo cloudStorageInfo,
    110       final MoblabRpcCallbacks.ValidateCloudStorageInfoCallback callback) {
    111     JSONObject params = new JSONObject();
    112     params.put(RPC_PARAM_CLOUD_STORAGE_INFO, cloudStorageInfo.toJson());
    113     JsonRpcProxy rpcProxy = JsonRpcProxy.getProxy();
    114     rpcProxy.rpcCall("validate_cloud_storage_info", params, new JsonRpcCallback() {
    115       @Override
    116       public void onSuccess(JSONValue result) {
    117         OperationStatus status = new OperationStatus();
    118         status.fromJson(result.isObject());
    119         callback.onCloudStorageInfoValidated(status);
    120       }
    121     });
    122   }
    123 
    124   /**
    125    * Submits the wizard configuration data.
    126    */
    127   public static void submitWizardConfigData(Map<String, JSONObject> configDataMap,
    128       final MoblabRpcCallbacks.SubmitWizardConfigInfoCallback callback) {
    129     JSONObject params = new JSONObject();
    130     if (configDataMap.containsKey(RPC_PARAM_CLOUD_STORAGE_INFO)) {
    131       params.put(RPC_PARAM_CLOUD_STORAGE_INFO, configDataMap.get(RPC_PARAM_CLOUD_STORAGE_INFO));
    132     } else {
    133       params.put(RPC_PARAM_CLOUD_STORAGE_INFO, new JSONObject());
    134     }
    135     if (configDataMap.containsKey(RPC_PARAM_WIFI_INFO)) {
    136       params.put(RPC_PARAM_WIFI_INFO, configDataMap.get(RPC_PARAM_WIFI_INFO));
    137     } else {
    138       params.put(RPC_PARAM_WIFI_INFO, new JSONObject());
    139     }
    140     JsonRpcProxy rpcProxy = JsonRpcProxy.getProxy();
    141     rpcProxy.rpcCall("submit_wizard_config_info", params, new JsonRpcCallback() {
    142       @Override
    143       public void onSuccess(JSONValue result) {
    144         OperationStatus status = new OperationStatus();
    145         status.fromJson(result.isObject());
    146         callback.onWizardConfigInfoSubmitted(status);
    147       }
    148     });
    149   }
    150 
    151   /**
    152    * Fetches the version information.
    153    */
    154   public static void fetchMoblabBuildVersionInfo(
    155       final MoblabRpcCallbacks.FetchVersionInfoCallback callback) {
    156     JsonRpcProxy rpcProxy = JsonRpcProxy.getProxy();
    157     rpcProxy.rpcCall("get_version_info", null, new JsonRpcCallback() {
    158           @Override
    159           public void onSuccess(JSONValue result) {
    160             VersionInfo info = new VersionInfo();
    161             info.fromJson(result.isObject());
    162             callback.onVersionInfoFetched(info);
    163           }
    164         });
    165   }
    166 
    167   /**
    168    * Apply update and reboot Moblab device
    169    */
    170   public static void updateMoblab(final JsonRpcCallback callback) {
    171     JsonRpcProxy rpcProxy = JsonRpcProxy.getProxy();
    172     rpcProxy.rpcCall("update_moblab", null, callback);
    173   }
    174 
    175    /**
    176    * Get information about the DUT's connected to the moblab.
    177    */
    178   public static void fetchDutInformation(
    179       final MoblabRpcCallbacks.FetchConnectedDutInfoCallback callback) {
    180     JsonRpcProxy rpcProxy = JsonRpcProxy.getProxy();
    181     rpcProxy.rpcCall("get_connected_dut_info", null, new JsonRpcCallback() {
    182       @Override
    183       public void onSuccess(JSONValue result) {
    184         ConnectedDutInfo info = new ConnectedDutInfo();
    185         info.fromJson(result.isObject());
    186         callback.onFetchConnectedDutInfoSubmitted(info);
    187       }
    188     });
    189   }
    190 
    191   /**
    192    * Enroll a device into the autotest syste.
    193    * @param dutIpAddress ipAddress of the new DUT.
    194    * @param callback Callback execute when the RPC is complete.
    195    */
    196   public static void addMoblabDut(String dutIpAddress,
    197       final MoblabRpcCallbacks.LogActionCompleteCallback callback) {
    198     JsonRpcProxy rpcProxy = JsonRpcProxy.getProxy();
    199     JSONObject params = new JSONObject();
    200     params.put("ipaddress", new JSONString(dutIpAddress));
    201     rpcProxy.rpcCall("add_moblab_dut", params, new JsonRpcCallback() {
    202       @Override
    203       public void onSuccess(JSONValue result) {
    204         boolean didSucceed = result.isArray().get(0).isBoolean().booleanValue();
    205         String information = result.isArray().get(1).isString().stringValue();
    206         callback.onLogActionComplete(didSucceed, information);
    207       }
    208     });
    209   }
    210 
    211   /**
    212    * Remove a device from the autotest system.
    213    * @param dutIpAddress ipAddress of the DUT to remove.
    214    * @param callback Callback to execute when the RPC is complete.
    215    */
    216   public static void removeMoblabDut(String dutIpAddress,
    217       final MoblabRpcCallbacks.LogActionCompleteCallback callback) {
    218     JsonRpcProxy rpcProxy = JsonRpcProxy.getProxy();
    219     JSONObject params = new JSONObject();
    220     params.put("ipaddress", new JSONString(dutIpAddress));
    221     rpcProxy.rpcCall("remove_moblab_dut", params, new JsonRpcCallback() {
    222       @Override
    223       public void onSuccess(JSONValue result) {
    224         boolean didSucceed = result.isArray().get(0).isBoolean().booleanValue();
    225         String information = result.isArray().get(1).isString().stringValue();
    226         callback.onLogActionComplete(didSucceed, information);
    227       }
    228     });
    229   }
    230 
    231   /**
    232    * Add a label to a specific DUT.
    233    * @param dutIpAddress  ipAddress of the device to have the new label applied.
    234    * @param labelName the label to apply
    235    * @param callback callback to execute when the RPC is complete.
    236    */
    237   public static void addMoblabLabel(String dutIpAddress, String labelName,
    238       final MoblabRpcCallbacks.LogActionCompleteCallback callback) {
    239     JsonRpcProxy rpcProxy = JsonRpcProxy.getProxy();
    240     JSONObject params = new JSONObject();
    241     params.put("ipaddress", new JSONString(dutIpAddress));
    242     params.put("label_name", new JSONString(labelName));
    243     rpcProxy.rpcCall("add_moblab_label", params, new JsonRpcCallback() {
    244       @Override
    245       public void onSuccess(JSONValue result) {
    246         boolean didSucceed = result.isArray().get(0).isBoolean().booleanValue();
    247         String information = result.isArray().get(1).isString().stringValue();
    248         callback.onLogActionComplete(didSucceed, information);
    249       }
    250     });
    251   }
    252 
    253   /**
    254    * Remove a label from a specific DUT.
    255    * @param dutIpAddress ipAddress of the device to have the label removed.
    256    * @param labelName the label to remove
    257    * @param callback callback to execute when the RPC is complete.
    258    */
    259   public static void removeMoblabLabel(String dutIpAddress, String labelName,
    260       final MoblabRpcCallbacks.LogActionCompleteCallback callback) {
    261     JsonRpcProxy rpcProxy = JsonRpcProxy.getProxy();
    262     JSONObject params = new JSONObject();
    263     params.put("ipaddress", new JSONString(dutIpAddress));
    264     params.put("label_name", new JSONString(labelName));
    265     rpcProxy.rpcCall("remove_moblab_label", params, new JsonRpcCallback() {
    266       @Override
    267       public void onSuccess(JSONValue result) {
    268         boolean didSucceed = result.isArray().get(0).isBoolean().booleanValue();
    269         String information = result.isArray().get(1).isString().stringValue();
    270         callback.onLogActionComplete(didSucceed, information);
    271       }
    272     });
    273   }
    274 
    275   /**
    276    * add an attribute to a specific dut.
    277    * @param dutIpAddress  ipaddress of the device to have the new attribute applied.
    278    * @param attributeName the attribute name
    279    * @param attributeValue the attribute value to be associated with the name
    280    * @param callback callback to execute when the rpc is complete.
    281    */
    282   public static void setMoblabAttribute(String dutIpAddress, String attributeName,
    283       String attributeValue, final MoblabRpcCallbacks.LogActionCompleteCallback callback) {
    284     JsonRpcProxy rpcProxy = JsonRpcProxy.getProxy();
    285     JSONObject params = new JSONObject();
    286     params.put("ipaddress", new JSONString(dutIpAddress));
    287     params.put("attribute", new JSONString(attributeName));
    288     params.put("value", new JSONString(attributeValue));
    289     rpcProxy.rpcCall("set_host_attrib", params, new JsonRpcCallback() {
    290       @Override
    291       public void onSuccess(JSONValue result) {
    292         boolean didSucceed = result.isArray().get(0).isBoolean().booleanValue();
    293         String information = result.isArray().get(1).isString().stringValue();
    294         callback.onLogActionComplete(didSucceed, information);
    295       }
    296     });
    297   }
    298 
    299   /**
    300    * remove an attribute from a specific dut.
    301    * @param dutIpAddress  ipaddress of the device to have the new attribute applied.
    302    * @param attributeName the attribute name
    303    * @param callback callback to execute when the rpc is complete.
    304    */
    305   public static void removeMoblabAttribute(String dutIpAddress, String attributeName,
    306       final  MoblabRpcCallbacks.LogActionCompleteCallback callback) {
    307     JsonRpcProxy rpcProxy = JsonRpcProxy.getProxy();
    308     JSONObject params = new JSONObject();
    309     params.put("ipaddress", new JSONString(dutIpAddress));
    310     params.put("attribute", new JSONString(attributeName));
    311     rpcProxy.rpcCall("delete_host_attrib", params, new JsonRpcCallback() {
    312       @Override
    313       public void onSuccess(JSONValue result) {
    314         boolean didSucceed = result.isArray().get(0).isBoolean().booleanValue();
    315         String information = result.isArray().get(1).isString().stringValue();
    316         callback.onLogActionComplete(didSucceed, information);
    317       }
    318     });
    319   }
    320 
    321 
    322   public static void fetchConnectedBoards(
    323       final MoblabRpcCallbacks.FetchConnectedBoardsCallback callback) {
    324     JsonRpcProxy rpcProxy = JsonRpcProxy.getProxy();
    325     rpcProxy.rpcCall("get_connected_boards", null, new JsonRpcCallback() {
    326       @Override
    327       public void onSuccess(JSONValue result) {
    328         List<ConnectedBoard> boards = new LinkedList<ConnectedBoard>();
    329         int boardListSize = result.isArray().size();
    330         for (int i = 0; i < boardListSize; i++) {
    331           ConnectedBoard board = new ConnectedBoard();
    332           board.fromJson(result.isArray().get(i).isObject());
    333           boards.add(board);
    334         }
    335         callback.onFetchConnectedBoardsSubmitted(boards);
    336       }
    337     });
    338   }
    339 
    340   public static void fetchConnectedPools(
    341       final MoblabRpcCallbacks.FetchConnectedPoolsCallback callback) {
    342     JsonRpcProxy rpcProxy = JsonRpcProxy.getProxy();
    343     rpcProxy.rpcCall("get_connected_pools", null, new JsonRpcCallback() {
    344       @Override
    345       public void onSuccess(JSONValue result) {
    346         List<String> pools = new LinkedList<String>();
    347         int poolListSize = result.isArray().size();
    348         for (int i = 0; i < poolListSize; i++) {
    349           pools.add(result.isArray().get(i).isString().stringValue());
    350         }
    351         callback.onFetchConnectedPoolsSubmitted(pools);
    352       }
    353     });
    354   }
    355 
    356   public static void fetchBuildsForBoard(String board_name,
    357       final MoblabRpcCallbacks.FetchBuildsForBoardCallback callback) {
    358     JsonRpcProxy rpcProxy = JsonRpcProxy.getProxy();
    359     JSONObject params = new JSONObject();
    360     params.put("board_name", new JSONString(board_name));
    361     rpcProxy.rpcCall("get_builds_for_board", params, new JsonRpcCallback() {
    362       @Override
    363       public void onSuccess(JSONValue result) {
    364         List<String> builds = new LinkedList<String>();
    365         for (int i = 0; i < result.isArray().size(); i++) {
    366           builds.add(result.isArray().get(i).isString().stringValue());
    367         }
    368         callback.onFetchBuildsForBoardCallbackSubmitted(builds);
    369       }
    370     });
    371   }
    372 
    373   public static void fetchFirmwareForBoard(String board_name,
    374       final MoblabRpcCallbacks.FetchFirmwareForBoardCallback callback) {
    375     JsonRpcProxy rpcProxy = JsonRpcProxy.getProxy();
    376     JSONObject params = new JSONObject();
    377     params.put("board_name", new JSONString(board_name));
    378     rpcProxy.rpcCall("get_firmware_for_board", params, new JsonRpcCallback() {
    379       @Override
    380       public void onSuccess(JSONValue result) {
    381         List<String> firmwareBuilds = new LinkedList<String>();
    382         for (int i = 0; i < result.isArray().size(); i++) {
    383           firmwareBuilds.add(result.isArray().get(i).isString().stringValue());
    384         }
    385         callback.onFetchFirmwareForBoardCallbackSubmitted(firmwareBuilds);
    386       }
    387     });
    388   }
    389 
    390   public static void runSuite(String board, String model,
    391       String build, String suite, String pool, String rwFirmware,
    392       String roFirmware, String suiteArgs, String bugId, String partId,
    393       final MoblabRpcCallbacks.RunSuiteCallback callback) {
    394     JsonRpcProxy rpcProxy = JsonRpcProxy.getProxy();
    395     JSONObject params = new JSONObject();
    396     params.put("board", new JSONString(board));
    397     params.put("model", new JSONString(model));
    398     params.put("build", new JSONString(build));
    399     params.put("suite", new JSONString(suite));
    400     params.put("pool", new JSONString(pool));
    401     params.put("rw_firmware", new JSONString(rwFirmware));
    402     params.put("ro_firmware", new JSONString(roFirmware));
    403     params.put("suite_args", new JSONString(suiteArgs));
    404     params.put("bug_id", new JSONString(bugId));
    405     params.put("part_id", new JSONString(partId));
    406     rpcProxy.rpcCall("run_suite", params, new JsonRpcCallback() {
    407       @Override
    408       public void onSuccess(JSONValue result) {
    409         callback.onRunSuiteComplete();
    410       }
    411     });
    412   }
    413 
    414   /**
    415    * Fetches the DUT wifi configuration information to use in tests.
    416    */
    417   public static void fetchWifiInfo(
    418       final MoblabRpcCallbacks.FetchWifiInfoCallback callback) {
    419     JsonRpcProxy rpcProxy = JsonRpcProxy.getProxy();
    420     rpcProxy.rpcCall("get_dut_wifi_info", null, new JsonRpcCallback() {
    421       @Override
    422       public void onSuccess(JSONValue result) {
    423         WifiInfo info = new WifiInfo();
    424         info.fromJson(result.isObject());
    425         callback.onWifiInfoFetched(info);
    426       }
    427     });
    428   }
    429 
    430 
    431 }
    432