def experimental(params=None): if params: resource = f'experimental/{params}' data = ApiService().list(resource, size=None) return data else: data = ApiService().list('experimental/object', size=None) return data
def __init__(self, my_app): self.api = ApiService() self.master_module = MasterModule() self.my_app = my_app self.press_time = [0] * settings.NUMBER_OF_PRESSES self.current_time = [0] * settings.NUMBER_OF_PRESSES self.is_open_started = [False] * settings.NUMBER_OF_PRESSES self.init_values()
def qosSetup(): baseUrl = 'http://0.0.0.0:8080' datapath = '0000000000000001' api = ApiService(baseUrl) # Put db address ovsdbEndpoint = '/v1.0/conf/switches/' + datapath + '/ovsdb_addr' ovsdbData = "tcp:127.0.0.1:6632" api.put(ovsdbEndpoint, ovsdbData) sleep(2) # Post Queue queueEndpoint = '/qos/queue/' + datapath queueData = { "port_name": "s1-eth1", "type": "linux-htb", "max_rate": "1000000", "queues": [{ "max_rate": "100000" }, { "max_rate": "500000" }, { "min_rate": "800000" }] } api.post(queueEndpoint, queueData) # Post Qos Rule ruleEndpoint = '/qos/rules/' + datapath #ruleData = {"match": {"ip_dscp": "26"}, # "actions": {"queue": "1"}} ruleData1 = { "match": { "nw_dst": "10.0.0.1", "nw_proto": "UDP", "tp_dst": "5001" }, "actions": { "queue": "0" } } ruleData2 = { "match": { "nw_dst": "10.0.0.1", "nw_proto": "UDP", "tp_dst": "5002" }, "actions": { "queue": "1" } } api.post(ruleEndpoint, ruleData1) api.post(ruleEndpoint, ruleData2)
def get_resource(resource, id): data = ApiService().get(resource, id) return render_template('result.html', data=data, resource=resource, id=id, resources=resources)
from flask import Flask from api import ApiService app = Flask(__name__) class Envs: TEST = 'test' PROD = 'prod' DEV = 'dev' __configs = { TEST: 'settings.dev_settings', PROD: 'settings.dev_settings', DEV: 'settings.dev_settings', } def get_config(self, name): return self.__configs.get(name) envs = Envs() config_object = envs.get_config(os.environ.get("ENVIRON", default=Envs.TEST)) app.config.from_object(config_object) ApiService.setup_blueprints(app) if __name__ == '__main__': app.run()
class Simulation: SIMULATION_TIME = 5 QOS_RULES = '/qos/rules/' QOS_QUEUES = '/qos/queues/' OVSDB_CONF = '/v1.0/conf/switches/' OVSDB_SERVER = "tcp:172.18.0.10:6632" def __init__(self, controllerIp, controllerPort): self.controllerUrl = self.controller_query(controllerIp, controllerPort) self.api = ApiService(self.controllerUrl) def switch_query(self, switch): ''' switch datapath creator ''' datapath = '000000000000000' + str(switch) return datapath def controller_query(self, ip, port): controllerUrl = 'http://' + ip + ':' + port return controllerUrl def conf_ovsdb_ovs(self, switches): i = 1 for switch in switches: s = 's' + str(i) loggerService.info('*** Configurando OpenFlow13 en ' + s) switch.cmd('ovs-vsctl set Bridge ' + s + ' protocols=OpenFlow13') loggerService.info('*** Configurando OVSDB port') switch.cmd('ovs-vsctl set-manager ptcp:6632') i = i + 1 def deleteQoS(self, switches): loggerService.info('*** Borrando QoS rules y queues...') for switch in switches: switch.cmdPrint('ovs-vsctl --all destroy QoS') switch.cmdPrint('ovs-vsctl --all destroy queue') # Tagging de campo Type of Service con DSCP def dscp_mark(self, switch): datapath = self.switch_query(switch) api = self.api # Reglas DSCP endpoint = self.QOS_RULES + datapath rules = [{ "match": { "nw_dst": "10.0.0.1", "nw_proto": "UDP", "tp_dst": "5001" }, "actions": { "mark": "26" } }, { "match": { "nw_dst": "10.0.0.1", "nw_proto": "UDP", "tp_dst": "5002" }, "actions": { "mark": "10" } }, { "match": { "nw_dst": "10.0.0.1", "nw_proto": "UDP", "tp_dst": "5003" }, "actions": { "mark": "12" } }] # Queries API for rule in rules: api.post(endpoint, rule) # iperfTest simulate best effort traffic between hosts def iperfTest(self, hosts, testPorts, time): loggerService.debug("Inicializar simulacion de trafico con IPerf") server = hosts[0] client = hosts[1] for port in testPorts: #iperf Server server.cmdPrint('iperf -s -u -p ' + str(port) + ' -i 1 > results' + str(port) + ' &') for port in testPorts: #iperf Client client.cmdPrint('iperf -c ' + server.IP() + ' -u -t ' + str(time) + ' -i 1 -p ' + str(port) + ' -b 1M &') sleep(self.SIMULATION_TIME) client.cmdPrint('killall -9 iperf') server.cmdPrint('killall -9 iperf') def pingAll(self, net): loggerService.debug("Comenzamos pingall ") net.pingAll() def qosSetup(self, test, switch): ''' Configurar qos queues en switches ''' datapath = self.switch_query(switch) endpoint = self.OVSDB_CONF + datapath + '/ovsdb_addr' self.api.put(endpoint, self.OVSDB_SERVER) sleep(2) # Post Queue if test == 1: queueEndpoint = '/qos/queue/' + datapath queueData = { "port_name": "s" + str(switch) + "-eth1", "type": "linux-htb", "max_rate": "1000000", "queues": [{ "max_rate": "100000" }, { "max_rate": "200000" }, { "max_rate": "300000" }, { "min_rate": "800000" }] } self.api.post(queueEndpoint, queueData) else: queueEndpoint = '/qos/queue/' + datapath queueData = { "port_name": "s" + str(switch) + "-eth1", "type": "linux-htb", "max_rate": "1000000", "queues": [{ "max_rate": "200000" }, { "max_rate": "300000" }, { "max_rate": "100000" }, { "min_rate": "800000" }] } self.api.post(queueEndpoint, queueData) endpoint = self.QOS_RULES + datapath rules = [{ "match": { "ip_dscp": "26" }, "actions": { "queue": "0" } }, { "match": { "ip_dscp": "10" }, "actions": { "queue": "1" } }, { "match": { "ip_dscp": "12" }, "actions": { "queue": "2" } }] for rule in rules: try: self.api.post(endpoint, rule) except Exception as e: loggerService.error( 'Error configurando reglas qos en controlador')
def __init__(self, controllerIp, controllerPort): self.controllerUrl = self.controller_query(controllerIp, controllerPort) self.api = ApiService(self.controllerUrl)
def colors(size=None): data = ApiService().list('color', size) colors = format_color_data(data) return render_template('colors.html', colors=colors)
def get_images(keyword, size=None): data = ApiService().find_images_by_keyword(keyword, size) return data
def list_resource(resource, size=None): data = ApiService().list(resource, size) return render_template('page.html', data=data, resource=resource, resources=resources)
def load_search(keyword, size=None, page=None): data = ApiService().find_by_keyword(keyword, size, page) return data
class AppService: def __init__(self, my_app): self.api = ApiService() self.master_module = MasterModule() self.my_app = my_app self.press_time = [0] * settings.NUMBER_OF_PRESSES self.current_time = [0] * settings.NUMBER_OF_PRESSES self.is_open_started = [False] * settings.NUMBER_OF_PRESSES self.init_values() def set_label(self, label, value): setattr(self.my_app, label, value) def init_state_message_window(self): state_string = self.master_module.get_state_string() active_input_modules = string_between_chars(s=state_string, start='I', end='R') active_relay_modules = string_between_chars(s=state_string, start='R', end='L') try: input_string = str(bin(int(active_input_modules))[2:]).zfill(settings.NUMBER_OF_PRESSES)[::-1] relay_string = str(bin(int(active_relay_modules))[2:]).zfill(settings.NUMBER_OF_PRESSES)[::-1] except: input_string = '' relay_string = '' input_modules_status = ['ACTIVE' if status == '1' else 'DISABLE' for status in list(input_string)] relay_modules_status = ['ACTIVE' if status == '1' else 'DISABLE' for status in list(relay_string)] for index, status in enumerate(input_modules_status): self.my_app.message_labels_input_modules.append('INP {} {}'.format(index + 1, status)) for index, status in enumerate(relay_modules_status): self.my_app.message_labels_relay_modules.append('REL {} {}'.format(index + 1, status)) def init_values(self): presses = self.api.get_endpoint_data('presses') self.set_label('system_status', 'STARTING') for index, press in enumerate(presses): self.press_time[index] = deepcopy(int(press['press_time'])) self.current_time[index] = deepcopy(int(press['press_time'])) time = get_time_format(self.press_time[index]) self.set_label('press_{}_mold_label'.format(index + 1), press['mold']) self.set_label('press_{}_time_label'.format(index + 1), time) self.set_label('press_{}_state_label'.format(index + 1), 'READY') self.init_state_message_window() def handle_labels_from_control_string(self, control_string): if not control_string: return if not control_string.startswith('S'): return switchgear = settings.SWITCHGEAR_STATE.get(string_between_chars(s=control_string, start='S', end='A'), 'ERROR') self.set_label('system_status', switchgear) for index, info in enumerate(settings.PRESSES_INFO): time_already_pressed = int(string_between_chars(s=control_string, start=info[1], end=info[2])) self.current_time[index] = max(self.press_time[index] - time_already_pressed, 0) current_time = get_time_format(self.current_time[index]) current_state = settings.PRESS_STATE.get(string_between_chars(s=control_string, start=info[0], end=info[1]), 'ERROR') self.set_label('press_{}_state_label'.format(index + 1), current_state) self.set_label('press_{}_time_label'.format(index + 1), current_time) def main_handling(self): readed_string = self.master_module.get_status_string() self.handle_labels_from_control_string(readed_string) for index, remaining_time in enumerate(self.current_time): if remaining_time == 0: if not self.is_open_started[index]: self.master_module.open_press(index) self.is_open_started[index] = True else: self.is_open_started[index] = False