def get(self): """ Gets the status of the dhcp server """ try: dhcpController = DhcpController() json_data = json.dumps(dhcpController.get_full_status()) resp = Response(json_data, status=200, mimetype="application/json") return resp except Exception as err: return Response(json.dumps(str(err)), status=500, mimetype="application/json")
def put(self): """ Update the dhcp configuration """ try: dhcpController = DhcpController() json_data = json.loads(request.data.decode()) dhcpController.set_configuration(json_data) return Response(status=202) except Exception as err: return Response(json.dumps(str(err)), status=500, mimetype="application/json")
def post(self): """ Configure an interface """ try: dhcpController = DhcpController() json_data = json.loads(request.data.decode()) dhcpController.configure_interface(json_data) return Response(status=202) except Exception as err: return Response(json.dumps(str(err)), status=500, mimetype="application/json")
def get(self, id): """ Gets the dhcp client date validity """ try: dhcpController = DhcpController() json_data = json.dumps(dhcpController.get_client_valid_until(id)) resp = Response(json_data, status=200, mimetype="application/json") return resp except ValueError as ve: return Response(json.dumps(str(ve)), status=404, mimetype="application/json") except Exception as err: return Response(json.dumps(str(err)), status=500, mimetype="application/json")
def __init__(self, tenant_id, graph_id, vnf_id): self.dhcpController = DhcpController() self.interfaceController = InterfaceController() self.dhcpServerController = DhcpServerController() self.dhcpClientController = DhcpClientController() self.tenant_id = tenant_id self.graph_id = graph_id self.vnf_id = vnf_id self.configuration_interface = None self.interfacesMonitor = None self.dhcpServerMonitor = None self.dhcpClientsMonitor = None
def get(self, id=None): """ Gets the dhcp client """ try: dhcpController = DhcpController() if id is None: json_data = json.dumps(dhcpController.get_clients()) else: json_data = json.dumps(dhcpController.get_client(id)) resp = Response(json_data, status=200, mimetype="application/json") return resp except ValueError as ve: return Response(json.dumps(str(ve)), status=404, mimetype="application/json") except Exception as err: return Response(json.dumps(str(err)), status=500, mimetype="application/json")
def delete(self, id): """ Remove the configuration of an interface """ try: dhcpController = DhcpController() dhcpController.reset_interface(id) return Response(status=202) except ValueError as ve: return Response(json.dumps(str(ve)), status=404, mimetype="application/json") except Exception as err: return Response(json.dumps(str(err)), status=500, mimetype="application/json")
def put(self, id): """ Update the configuration of an interface """ try: dhcpController = DhcpController() json_data = json.loads(request.data.decode()) dhcpController.update_interface(id, json_data) return Response(status=202) except ValueError as ve: return Response(json.dumps(str(ve)), status=404, mimetype="application/json") except Exception as err: return Response(json.dumps(str(err)), status=500, mimetype="application/json")
def put(self): """ Update the default lease time parameter """ try: dhcpController = DhcpController() json_data = request.data.decode() dhcpController.update_default_lease_time(json_data) return Response(status=202) except ValueError as ve: return Response(json.dumps(str(ve)), status=404, mimetype="application/json") except Exception as err: return Response(json.dumps(str(err)), status=500, mimetype="application/json")
def put(self, id): """ Update the section end ip parameter """ try: dhcpController = DhcpController() json_data = request.data.decode() dhcpController.update_section_end_ip(id, json_data) return Response(status=202) except ValueError as ve: return Response(json.dumps(str(ve)), status=404, mimetype="application/json") except Exception as err: return Response(json.dumps(str(err)), status=500, mimetype="application/json")
def post(self): """ Add a section """ try: dhcpController = DhcpController() json_data = json.loads(request.data.decode()) dhcpController.add_section(json_data) return Response(status=202) except ValueError as ve: return Response(json.dumps(str(ve)), status=404, mimetype="application/json") except Exception as err: return Response(json.dumps(str(err)), status=500, mimetype="application/json")
def put(self, id): """ Update the default gw of an interface """ try: dhcpController = DhcpController() default_gw = request.data.decode() dhcpController.update_interface_ipv4Configuration_default_gw( id, default_gw) return Response(status=202) except ValueError as ve: return Response(json.dumps(str(ve)), status=404, mimetype="application/json") except Exception as err: return Response(json.dumps(str(err)), status=500, mimetype="application/json")
def get(self, id): """ Get the default gw address of an interface """ try: dhcpController = DhcpController() json_data = json.dumps( dhcpController.get_interface_ipv4Configuration_default_gw(id)) resp = Response(json_data, status=200, mimetype="application/json") return resp except ValueError as ve: return Response(json.dumps(str(ve)), status=404, mimetype="application/json") except Exception as err: return Response(json.dumps(str(err)), status=500, mimetype="application/json")
def get(self): """ Gets the max lease time parameter """ try: dhcpController = DhcpController() json_data = json.dumps( dhcpController.get_dhcp_server_configuration_max_lease_time()) resp = Response(json_data, status=200, mimetype="application/json") return resp except ValueError as ve: return Response(json.dumps(str(ve)), status=404, mimetype="application/json") except Exception as err: return Response(json.dumps(str(err)), status=500, mimetype="application/json")
class DhcpMonitor(): def __init__(self, tenant_id, graph_id, vnf_id): self.dhcpController = DhcpController() self.interfaceController = InterfaceController() self.dhcpServerController = DhcpServerController() self.dhcpClientController = DhcpClientController() self.tenant_id = tenant_id self.graph_id = graph_id self.vnf_id = vnf_id self.configuration_interface = None self.interfacesMonitor = None self.dhcpServerMonitor = None self.dhcpClientsMonitor = None def set_initial_configuration(self, initial_configuration): curr_interfaces = self.interfaceController.get_interfaces() self.interfacesMonitor = InterfacesMonitor(self, curr_interfaces) curr_dhcp_server_configuration = self.dhcpServerController.get_dhcp_server_configuration( ) self.dhcpServerMonitor = DhcpServerMonitor( self, curr_dhcp_server_configuration) curr_dhcp_clients = self.dhcpClientController.get_clients() self.dhcpClientsMonitor = DhcpClientsMonitor(self, curr_dhcp_clients) logging.debug("Setting initial configuration...") self.dhcpController.set_configuration(initial_configuration) logging.debug("Setting initial configuration...done!") def get_address_of_configuration_interface(self, configuration_interface): self.configuration_interface = configuration_interface return self.dhcpController.get_interface_ipv4Configuration_address( configuration_interface) def start(self): threads = [] threads.append( Thread(target=self.interfacesMonitor.start_monitoring, args=())) threads.append( Thread(target=self.dhcpServerMonitor.start_monitoring, args=())) threads.append( Thread(target=self.dhcpClientsMonitor.start_monitoring, args=())) # Start all threads for t in threads: t.start() # Wait for all of them to finish for t in threads: t.join() def publish_on_bus(self, url, method, data): msg = self.tenant_id + "." + self.graph_id + "." + self.vnf_id + "/" + url body = {} if method is not None: body['event'] = method.upper() else: body['event'] = "PERIODIC" body['timestamp'] = datetime.now().strftime('%Y-%m-%d %H:%M:%S') body['data'] = data MessageBusController().publish_on_bus( msg, json.dumps(body, indent=4, sort_keys=True))