class NatMonitor(): def __init__(self, tenant_id, graph_id, vnf_id): self.natController = NatController() self.interfaceController = InterfaceController() self.natTableController = NatTableController() self.tenant_id = tenant_id self.graph_id = graph_id self.vnf_id = vnf_id self.configuration_interface = None self.interfacesMonitor = None self.natTableMonitor = None def set_initial_configuration(self, initial_configuration): curr_interfaces = self.interfaceController.get_interfaces() self.interfacesMonitor = InterfacesMonitor(self, curr_interfaces) curr_natTable = self.natController.get_nat_table() self.natTableMonitor = NatTableMonitor(self, curr_natTable) logging.debug("Setting initial configuration...") self.natController.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.natController.get_interface_ipv4Configuration_address( configuration_interface) def start(self): threads = [] threads.append( Thread(target=self.interfacesMonitor.start_monitoring, args=())) threads.append( Thread(target=self.natTableMonitor.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))
def put(self): """ Update the nat configuration """ try: natController = NatController() json_data = json.loads(request.data.decode()) natController.set_configuration(json_data) return Response(status=202) except Exception as err: return Response(json.dumps(str(err)), status=500, mimetype="application/json")
def get(self): """ Gets the status of the nat """ try: natController = NatController() json_data = json.dumps(natController.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 get(self): """ Gets the private-interface """ try: natController = NatController() json_data = json.dumps(natController.get_private_interface_id()) 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 get(self, id): """ Get the mac address """ try: natController = NatController() json_data = json.dumps(natController.get_arp_table_mac_address(id)) 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 post(self): """ Add an arp entry """ try: natController = NatController() json_data = json.loads(request.data.decode()) natController.add_arp_entry(json_data) return Response(status=202) 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.natController = NatController() self.interfaceController = InterfaceController() self.natTableController = NatTableController() self.tenant_id = tenant_id self.graph_id = graph_id self.vnf_id = vnf_id self.configuration_interface = None self.interfacesMonitor = None self.natTableMonitor = None
def delete(self, id): """ Remove the configuration of an interface """ try: natController = NatController() natController.delete_arp_entry(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 delete(self, id): """ Remove a Floating IP """ try: natController = NatController() natController.delete_floating_ip(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 mac address """ try: natController = NatController() mac_address = request.data.decode() natController.update_arp_table_mac_address(id, mac_address) 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 a Floating IP """ try: natController = NatController() json_data = json.loads(request.data.decode()) natController.update_floating_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 put(self, id): """ Update the Floating IP public address parameter """ try: natController = NatController() json_data = request.data.decode() natController.update_floating_ip_public_address(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 get(self, id=None): """ Gets the Floating IP public address parameter """ try: natController = NatController() json_data = json.dumps( natController.get_floating_ip_public_address(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, id=None): """ Get a Floating IP """ try: natController = NatController() if id is None: json_data = json.dumps(natController.get_all_floating_ip()) else: json_data = json.dumps(natController.get_floating_ip(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")