def put(self, name, device_id): soft_limit = request.args.get('soft_limit') abort_when_not_int(soft_limit) computation_node = abort_when_node_not_found(name) if int(soft_limit) < 0: log.error(str.format('Number is not positive: {}', soft_limit)) abort(400) if not any(d['id'] == device_id for d in computation_node['backend_info']['devices']): log.error('There is no such device: %s', device_id) abort(404) limit_info = { 'name': name, 'device_id': device_id, 'soft_limit': soft_limit } upsert_result = database.replace_soft_limit_for_device( name, device_id, limit_info) if upsert_result.modified_count: log.info( 'Power limit for device %s:%s was already set in a database to %s', name, device_id, soft_limit) log.info('Stored power limit info %s', limit_info) else: log.info('Stored power limit info %s on id %s', limit_info, upsert_result.upserted_id) return 'Soft limit successfully set', 201
def delete(self, name): result_node_info = database.delete_computation_node_info(name) result_power_limit_info = database.delete_power_limit_infos(name) if not result_node_info: log.info('No such computation node %s', name) abort(404) if not result_power_limit_info: log.info('No such power limit info for node %s', name) abort(404) address = result_node_info.get('address') port = result_node_info.get('port') abort_when_port_invalid(port) for device in result_node_info['backend_info']['devices']: try: response = delete_power_limit(address, port, device['id'], device['Type']) log.info('Device %s deletion info: %s', device['id'], response) except requests.exceptions.ConnectionError: log.error('Connection could not be established to %s:%s', address, port) abort(406) log.info( 'Successfully deleted node %s info and its power limit: %s %s', name, result_node_info, result_power_limit_info) return 204
def delete(self, name, device_id): node_info = abort_when_node_not_found(name) address = node_info['address'] port = node_info['port'] [device_type] = [ d['Type'] for d in node_info['backend_info']['devices'] if d['id'] == device_id ] result = database.delete_power_limit_info(name, device_id) if not result: log.info('No such device %s:%s', name, device_id) abort(404) try: response = delete_power_limit(address, port, device_id, device_type) log.info('Power limit for device %s deletion info: %s', device_id, response) except requests.exceptions.ConnectionError: log.error('Connection could not be established to %s:%s', address, port) return 'Warning: power limit was deleted from database but could not be deleted from device', 406 log.info( 'Successfully removed power limit for device %s:%s power limit info: %s', name, device_id, result) return result, 200
def delete(self, name, device_id): computation_node = abort_when_node_not_found(name) address = computation_node['address'] port = computation_node['port'] if not any(d['id'] == device_id for d in computation_node['backend_info']['devices']): log.error('There is no such device: %s', device_id) abort(404) [device_type] = [ d['Type'] for d in computation_node['backend_info']['devices'] if d['id'] == device_id ] result = database.delete_rule(name, device_id) try: resp = delete_power_limit(address, port, device_id, device_type) log.info('Power limit for device %s deletion info: %s', device_id, resp) except requests.exceptions.ConnectionError: log.error('Connection could not be established to node %s:%s', address, port) return 'Warning: power limit was deleted from database but could not be deleted from device!', 406 return result, 200
def abort_when_port_invalid(port): try: parsed_port = int(port) if 0 > parsed_port > 65535: log.error(str.format('Port number is not valid: {}', parsed_port)) abort(400) except ValueError: log.error(str.format('Port number is not valid: {}', port)) abort(400)
def put(self, name, device_id): power_limit = request.args.get('power_limit') abort_when_not_int(power_limit) computation_node = abort_when_node_not_found(name) if not any(d['id'] == device_id for d in computation_node['backend_info']['devices']): log.error('There is no such device: %s', device_id) abort(404) limit_info = { 'name': name, 'device_id': device_id, 'power_limit': power_limit } [device_type] = [d['Type'] for d in computation_node['backend_info']['devices'] if d['id'] == device_id] previous_limit_data = get_power_limit(computation_node['address'], computation_node['port'], device_id, device_type) previous_limit_info = json.loads(previous_limit_data.content.decode('ascii')) previous_limit = previous_limit_info[0]['PowerLimit'] upsert_result = database.replace_power_limit_for_device(name, device_id, limit_info) if upsert_result.modified_count: log.info('Power limit for device %s:%s was already set in a database to %s', name, device_id, power_limit) log.info('Stored power limit info %s', limit_info) else: log.info('Stored power limit info %s on id %s', limit_info, upsert_result.upserted_id) try: response = put_power_limit(computation_node['address'], computation_node['port'], device_id, device_type, power_limit) log.info(response.text) response_object = json.loads(response.content.decode('ascii')) if not response_object[0]['Success']: log.info('Could not set power limit on device %s:%s, restoring previous value: %s', name, device_id, previous_limit) try: limit_info['power_limit'] = int(previous_limit) upsert_result = database.replace_power_limit_for_device(name, device_id, limit_info) if upsert_result.modified_count: log.info('Power limit for device %s:%s was already set in a database to %s', name, device_id, power_limit) log.info('Restored previous power limit %s', limit_info) else: log.info('Restored previous power limit info %s:%s', limit_info, upsert_result.upserted_id) except ValueError: pass return response_object[0]['ErrorMessage'], 406 except requests.exceptions.ConnectionError: log.error('Connection could not be established to %s:%s', computation_node['address'], computation_node['port']) return 'Failed to set power limit on device, but added to database', 201 return 'Power limit successfully set', 201
def put(self, name, device_id): statistics_interval = request.args.get('statistics_interval') abort_when_not_int(statistics_interval) statistics_interval = int(statistics_interval) computation_node = abort_when_node_not_found(name) if not any(d['id'] == device_id for d in computation_node['backend_info']['devices']): log.error('There is no such device: %s', device_id) abort(404) set_statistics_interval(name, device_id, statistics_interval) return 'Statistics gathering interval successfully set', 201
def delete(self, name, device_id): node_info = abort_when_node_not_found(name) address = node_info['address'] port = node_info['port'] [device_type] = [d['Type'] for d in node_info['backend_info']['devices'] if d['id'] == device_id] result = database.delete_power_limit_info(name, device_id) if not result: log.info('No such device %s:%s', name, device_id) abort(404) try: response = delete_power_limit(address, port, device_id, device_type) log.info('Power limit for device %s deletion info: %s', device_id, response) except requests.exceptions.ConnectionError: log.error('Connection could not be established to %s:%s', address, port) return 'Warning: power limit was deleted from database but could not be deleted from device', 406 log.info('Successfully removed power limit for device %s:%s power limit info: %s', name, device_id, result) return result, 200
def put(self, name): address = request.args.get('address') port = request.args.get('port') abort_when_port_invalid(port) node_by_ip = database.get_computation_node_info_by_address( address, port) if node_by_ip and node_by_ip.get('name') != name: log.warning('Node with IP: %s:%s is present in database: %s', address, port, node_by_ip) try: response = get_node_information(address, port) except requests.exceptions.ConnectionError: log.error('Connection could not be established to %s:%s', address, port) abort(406) log.info('Response %s:', response.text) backend_info = json.loads(response.text) node_info = { 'name': name, 'address': address, 'port': port, 'backend_info': backend_info } upsert_result = database.replace_computation_node_info(name, node_info) if upsert_result.modified_count: log.info('Node %s was already present in a database', name) log.info('Stored Node info %s', node_info) else: log.info('Stored Node info %s on id %s', node_info, upsert_result.upserted_id) for device in backend_info['devices']: set_statistics_interval(name, device['id'], 1) return name, 201
def put(self, name: str, device_id: str): rule_type = request.args.get('rule_type') rule_params = request.json if rule_type not in AVAILABLE_RULE_TYPES: abort(400) computation_node = abort_when_node_not_found(name) if not any(d['id'] == device_id for d in computation_node['backend_info']['devices']): log.error('There is no such device: %s', device_id) abort(404) limit = rule_params.get("limit") if isinstance(limit, float): computation_node, dev_type = get_com_node_and_dev_type( name, device_id) constraints = get_constraints( computation_node['address'], computation_node['port'], device_id, dev_type).json()[0]["PowerLimitConstraints"] lower = constraints["lower"] upper = constraints["upper"] rule_params["limit"] = int((upper - lower) * limit + lower) if rule_type == "Withdrawable": previous_rule = database.get_rule_for_device(name, device_id) if previous_rule: if previous_rule["rule_type"] == "Withdrawable": previous_rule = previous_rule["rule_params"].get( "previous_rule", None) rule_params["previous_rule"] = previous_rule database.replace_rule_for_device( name, device_id, { 'name': name, 'device_id': device_id, 'rule_type': rule_type, 'rule_params': rule_params }) return 'Rule successfully set', 201
def put(self, name, device_id): statistics_interval = request.args.get('statistics_interval') abort_when_not_int(statistics_interval) statistics_interval = int(statistics_interval) computation_node = abort_when_node_not_found(name) if not any(d['id'] == device_id for d in computation_node['backend_info']['devices']): log.error('There is no such device: %s', device_id) abort(404) current_interval_info = database.get_stats_interval_info(name, device_id) if current_interval_info: old_interval = datetime.timedelta(minutes=current_interval_info['interval']) old_next_measurement = datetime.datetime.strptime(current_interval_info['next_measurement'], '%Y-%m-%dT%H:%M') new_interval = datetime.timedelta(minutes=statistics_interval) new_next_measurement = (old_next_measurement - old_interval + new_interval) interval_info = current_interval_info interval_info['interval'] = statistics_interval new_next_measurement_str = new_next_measurement.strftime('%Y-%m-%dT%H:%M') # pylint: disable=no-member interval_info['next_measurement'] = new_next_measurement_str else: interval_info = { 'name': name, 'device_id': device_id, 'interval': statistics_interval, 'next_measurement': (datetime.datetime.utcnow() + datetime.timedelta( minutes=statistics_interval)).strftime('%Y-%m-%dT%H:%M') # pylint: disable=no-member } upsert_result = database.replace_stats_interval_info(name, device_id, interval_info) if upsert_result.modified_count: log.info('Statistics gathering interval for device %s:%s was already set in a database', name, device_id) log.info('Stored statistics gathering interval info %s', interval_info) else: log.info('Stored statistics gathering interval info %s on id %s', interval_info, upsert_result.upserted_id) return 'Statistics gathering interval successfully set', 201
def abort_when_device_not_found(device_id, computation_node): if not any(d['id'] == device_id for d in computation_node['backend_info']['devices']): log.error('There is no such device: %s', device_id) abort(404)
def put(self, name, device_id): power_limit = request.args.get('power_limit') abort_when_not_int(power_limit) computation_node = abort_when_node_not_found(name) if not any(d['id'] == device_id for d in computation_node['backend_info']['devices']): log.error('There is no such device: %s', device_id) abort(404) limit_info = { 'name': name, 'device_id': device_id, 'power_limit': power_limit } [device_type] = [ d['Type'] for d in computation_node['backend_info']['devices'] if d['id'] == device_id ] previous_limit_data = get_power_limit(computation_node['address'], computation_node['port'], device_id, device_type) previous_limit_info = json.loads( previous_limit_data.content.decode('ascii')) previous_limit = previous_limit_info[0]['PowerLimit'] upsert_result = database.replace_power_limit_for_device( name, device_id, limit_info) if upsert_result.modified_count: log.info( 'Power limit for device %s:%s was already set in a database to %s', name, device_id, power_limit) log.info('Stored power limit info %s', limit_info) else: log.info('Stored power limit info %s on id %s', limit_info, upsert_result.upserted_id) try: response = put_power_limit(computation_node['address'], computation_node['port'], device_id, device_type, power_limit) log.info(response.text) response_object = json.loads(response.content.decode('ascii')) if not response_object[0]['Success']: log.info( 'Could not set power limit on device %s:%s, restoring previous value: %s', name, device_id, previous_limit) try: limit_info['power_limit'] = int(previous_limit) upsert_result = database.replace_power_limit_for_device( name, device_id, limit_info) if upsert_result.modified_count: log.info( 'Power limit for device %s:%s was already set in a database to %s', name, device_id, power_limit) log.info('Restored previous power limit %s', limit_info) else: log.info('Restored previous power limit info %s:%s', limit_info, upsert_result.upserted_id) except ValueError: pass return response_object[0]['ErrorMessage'], 406 except requests.exceptions.ConnectionError: log.error('Connection could not be established to %s:%s', computation_node['address'], computation_node['port']) return 'Failed to set power limit on device, but added to database', 201 return 'Power limit successfully set', 201
def abort_when_not_int(number): try: int(number) except ValueError: log.error(str.format('Number is not valid: {}', number)) abort(400)