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 requests 20 21from acts import asserts 22from acts import signals 23from acts import utils 24from acts_contrib.test_utils.wifi import wifi_constants 25"""This file consists of all the helper methods needed to interact with the 26 Datastore @ https://chaos-188802.appspot.com/ used for Android Interop 27 testing. 28""" 29 30DATASTORE_HOST = "https://chaos-188802.appspot.com" 31 32# The Datastore defines the following paths for operating methods. 33ADD_DEVICE = "devices/new" 34REMOVE_DEVICE = "devices/delete" 35LOCK_DEVICE = "devices/lock" 36UNLOCK_DEVICE = "devices/unlock" 37SHOW_DEVICE = "devices/" 38GET_DEVICES = "devices/" 39 40# HTTP content type. JSON encoded with UTF-8 character encoding. 41HTTP_HEADER = {'content-type': 'application/json'} 42 43 44def add_device(name, ap_label, lab_label): 45 """Add a device(AP or Packet Capturer) in datastore. 46 47 Args: 48 name: string, hostname of the device. 49 ap_label: string, AP brand name. 50 lab_label: string, lab label for AP. 51 Returns: 52 True if device was added successfully; 0 otherwise. 53 """ 54 request = DATASTORE_HOST + '/' + ADD_DEVICE 55 logging.debug("Request = %s" % request) 56 response = requests.post(request, 57 headers=HTTP_HEADER, 58 data=json.dumps({ 59 "hostname": name, 60 "ap_label": ap_label, 61 "lab_label": lab_label 62 })) 63 if response.json()['result'] == 'success': 64 logging.info("Added device %s to datastore" % name) 65 return True 66 return False 67 68 69def remove_device(name): 70 """Delete a device(AP or Packet Capturer) in datastore. 71 72 Args: 73 name: string, hostname of the device to delete. 74 Returns: 75 True if device was deleted successfully; 0 otherwise. 76 """ 77 request = DATASTORE_HOST + '/' + REMOVE_DEVICE 78 logging.debug("Request = %s" % request) 79 response = requests.put(request, 80 headers=HTTP_HEADER, 81 data=json.dumps({"hostname": name})) 82 result_str = "%s deleted." % name 83 if result_str in response.text: 84 logging.info("Removed device %s from datastore" % name) 85 return True 86 return False 87 88 89def lock_device(name, admin): 90 """Lock a device(AP or Packet Capturer) in datastore. 91 92 Args: 93 name: string, hostname of the device in datastore. 94 admin: string, unique admin name for locking. 95 Returns: 96 True if operation was successful; 0 otherwise. 97 """ 98 request = DATASTORE_HOST + '/' + LOCK_DEVICE 99 logging.debug("Request = %s" % request) 100 response = requests.put(request, 101 headers=HTTP_HEADER, 102 data=json.dumps({ 103 "hostname": name, 104 "locked_by": admin 105 })) 106 if response.json()['result']: 107 logging.info("Locked device %s in datastore" % name) 108 return True 109 return False 110 111 112def unlock_device(name): 113 """Un-lock a device(AP or Packet Capturer) in datastore. 114 115 Args: 116 name: string, hostname of the device in datastore. 117 Returns: 118 True if operation was successful; 0 otherwise. 119 """ 120 request = DATASTORE_HOST + '/' + UNLOCK_DEVICE 121 logging.debug("Request = %s" % request) 122 response = requests.put(request, 123 headers=HTTP_HEADER, 124 data=json.dumps({"hostname": name})) 125 if response.json()['result']: 126 logging.info("Finished un-locking AP %s in datastore" % name) 127 return True 128 return False 129 130 131def show_device(name): 132 """Show device properties for a given device(AP or Packet Capturer). 133 134 Args: 135 name: string, hostname of the device in datastore to fetch info. 136 Returns: dict of device name:value properties if successful; 137 None otherwise. 138 """ 139 request = DATASTORE_HOST + '/' + SHOW_DEVICE + name 140 logging.debug("Request = %s" % request) 141 response = requests.get(request) 142 if 'error' in response.text: 143 return None 144 return response.json() 145 146 147def get_devices(): 148 """Get a list of all devices in the datastore. 149 150 Returns: dict of all devices' name:value properties if successful; 151 None otherwise. 152 """ 153 request = DATASTORE_HOST + '/' + GET_DEVICES 154 logging.debug("Request = %s" % request) 155 response = requests.get(request) 156 if 'error' in response.text: 157 return None 158 return response.json() 159