1#!/usr/bin/env python3 2# 3# Copyright 2018 Google, Inc. 4# 5# Licensed under the Apache License, Version 2.0 (the "License"); 6# you may not use this file except in compliance with the License. 7# You may obtain a copy of the License at 8# 9# http://www.apache.org/licenses/LICENSE-2.0 10# 11# Unless required by applicable law or agreed to in writing, software 12# distributed under the License is distributed on an "AS IS" BASIS, 13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14# See the License for the specific language governing permissions and 15# limitations under the License. 16 17import json 18import logging 19import pprint 20import requests 21import time 22 23from acts import asserts 24from acts import signals 25from acts import utils 26from acts.test_utils.wifi import wifi_constants 27 28"""This file consists of all the helper methods needed to interact with the 29 Datastore @ https://chaos-188802.appspot.com/ used for Android Interop 30 testing. 31""" 32 33DATASTORE_HOST = "https://chaos-188802.appspot.com" 34 35# The Datastore defines the following paths for operating methods. 36ADD_DEVICE = "devices/new" 37REMOVE_DEVICE = "devices/delete" 38LOCK_DEVICE = "devices/lock" 39UNLOCK_DEVICE = "devices/unlock" 40SHOW_DEVICE = "devices/" 41GET_DEVICES = "devices/" 42 43# HTTP content type. JSON encoded with UTF-8 character encoding. 44HTTP_HEADER = {'content-type': 'application/json'} 45 46def add_device(name, ap_label, lab_label): 47 """Add a device(AP or Packet Capturer) in datastore. 48 49 Args: 50 name: string, hostname of the device. 51 ap_label: string, AP brand name. 52 lab_label: string, lab label for AP. 53 Returns: 54 True if device was added successfully; 0 otherwise. 55 """ 56 request = DATASTORE_HOST + '/' + ADD_DEVICE 57 logging.debug("Request = %s" % request) 58 response = requests.post(request, 59 headers=HTTP_HEADER, 60 data=json.dumps({"hostname":name, 61 "ap_label":ap_label, 62 "lab_label":lab_label})) 63 if response.json()['result'] == 'success': 64 logging.info("Added device %s to datastore" % name) 65 return True 66 return False 67 68def remove_device(name): 69 """Delete a device(AP or Packet Capturer) in datastore. 70 71 Args: 72 name: string, hostname of the device to delete. 73 Returns: 74 True if device was deleted successfully; 0 otherwise. 75 """ 76 request = DATASTORE_HOST + '/' + REMOVE_DEVICE 77 logging.debug("Request = %s" % request) 78 response = requests.put(request, 79 headers=HTTP_HEADER, 80 data=json.dumps({"hostname":name})) 81 result_str = "%s deleted." % name 82 if result_str in response.text: 83 logging.info("Removed device %s from datastore" % name) 84 return True 85 return False 86 87def lock_device(name, admin): 88 """Lock a device(AP or Packet Capturer) in datastore. 89 90 Args: 91 name: string, hostname of the device in datastore. 92 admin: string, unique admin name for locking. 93 Returns: 94 True if operation was successful; 0 otherwise. 95 """ 96 request = DATASTORE_HOST + '/' + LOCK_DEVICE 97 logging.debug("Request = %s" % request) 98 response = requests.put(request, 99 headers=HTTP_HEADER, 100 data=json.dumps({"hostname":name, "locked_by":admin})) 101 if response.json()['result']: 102 logging.info("Locked device %s in datastore" % name) 103 return True 104 return False 105 106def unlock_device(name): 107 """Un-lock a device(AP or Packet Capturer) in datastore. 108 109 Args: 110 name: string, hostname of the device in datastore. 111 Returns: 112 True if operation was successful; 0 otherwise. 113 """ 114 request = DATASTORE_HOST + '/' + UNLOCK_DEVICE 115 logging.debug("Request = %s" % request) 116 response = requests.put(request, 117 headers=HTTP_HEADER, 118 data=json.dumps({"hostname":name})) 119 if response.json()['result']: 120 logging.info("Finished un-locking AP %s in datastore" % name) 121 return True 122 return False 123 124def show_device(name): 125 """Show device properties for a given device(AP or Packet Capturer). 126 127 Args: 128 name: string, hostname of the device in datastore to fetch info. 129 Returns: dict of device name:value properties if successful; 130 None otherwise. 131 """ 132 request = DATASTORE_HOST + '/' + SHOW_DEVICE + name 133 logging.debug("Request = %s" % request) 134 response = requests.get(request) 135 if 'error' in response.text: 136 return None 137 return response.json() 138 139def get_devices(): 140 """Get a list of all devices in the datastore. 141 142 Returns: dict of all devices' name:value properties if successful; 143 None otherwise. 144 """ 145 request = DATASTORE_HOST + '/' + GET_DEVICES 146 logging.debug("Request = %s" % request) 147 response = requests.get(request) 148 if 'error' in response.text: 149 return None 150 return response.json() 151