def run(): #confd.set_debug(confd.TRACE, sys.stderr) (css, server_sid) = handle_netconf_server_list(cdb.LOCK_SESSION | cdb.LOCK_WAIT) d = handle_sync_to() try: while True: (type, sub_flags, sids) = cdb.read_subscription_socket2(css) if type == cdb.SUB_PREPARE: handle_sub_prepare(css, sids, sub_flags) if type == cdb.SUB_ABORT: handle_sub_abort(css, sids) if type == cdb.SUB_COMMIT: if sids[0] == server_sid: (new_css, server_sid) = handle_netconf_server_list(0) cdb.sync_subscription_socket(css, cdb.DONE_PRIORITY) css.close() css = new_css else: handle_sub_commit(css, sids, sub_flags) except KeyboardInterrupt: print("\nCtrl-C pressed") finally: css.close() d.finish()
def handle_sub_commit(css, sids, sub_flags): print("***Config updated --> Commit") processes = [] q = SimpleQueue() for sid in sids: print("confirm-commit for subid {}".format(sid)) p = Process(target=confirm_commit, args=(sid, q)) processes.append(p) p.start() for process in processes: process.join() if not q.empty(): sys.stderr.write("Confirm commit failed:\n{}\n".format('\n'.join( list(q.queue)))) cdb.sync_subscription_socket(css, cdb.DONE_PRIORITY)
def process_subscriptions(spoint, subsock): log.info("==>") sub_points = cdb.read_subscription_socket(subsock) for s in sub_points: if s == spoint: log.debug("our spoint=%i triggered" % spoint) flags = cdb.GET_MODS_INCLUDE_LISTS log.debug("subsock.fileno={} spoint={} flags={}".format( subsock.fileno(), spoint, flags)) modifications = _confd.cdb.get_modifications( subsock, spoint, cdb.GET_MODS_INCLUDE_LISTS, None) process_modifications(modifications) cdb.sync_subscription_socket(subsock, cdb.DONE_PRIORITY) log.info("<==")
def handle_sub_prepare(css, sids, sub_flags): print("***Config updated --> Prepare") processes = [] q = SimpleQueue() for sid in sids: flags = cdb.GET_MODS_INCLUDE_LISTS | cdb.GET_MODS_SUPPRESS_DEFAULTS mods = cdb.get_modifications(css, sid, flags, ROOT_PATH) if mods == []: print("no modifications for subid {}".format(sid)) else: print("edit-config and validate for subid {}".format(sid)) p = Process(target=process_modifications, args=(mods, sid, q)) processes.append(p) p.start() for process in processes: process.join() if not q.empty(): cdb.sub_abort_trans(css, confd.ERRCODE_APPLICATION, 0, 0, ', '.join(list(q.queue))) # TODO USE lxml to get the error string from the NETCONF error reply # TODO sub_abort_trans_info(...) return processes = [] for sid in sids: print("commit for subid {}".format(sid)) p = Process(target=commit, args=(sid, q)) processes.append(p) p.start() for process in processes: process.join() if not q.empty(): cdb.sub_abort_trans(css, confd.ERRCODE_APPLICATION, 0, 0, ', '.join(list(q.queue))) # TODO USE lxml to get the error string from the NETCONF error reply # TODO sub_abort_trans_info(...) return cdb.sync_subscription_socket(css, cdb.DONE_PRIORITY)
def run(): """ Main subscriber thread execution. """ confd_addr = '127.0.0.1' confd_port = _confd.PORT _confd.set_debug(_confd.SILENT, sys.stderr) sub_path = "/system/ip/route" # MAAPI socket to load schemas for a nicer print() calls maapi_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0) maapi.connect(maapi_sock, confd_addr, confd_port, sub_path) maapi.load_schemas(maapi_sock) # socket for subscription data iteration data_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0) cdb.connect(data_sock, cdb.DATA_SOCKET, confd_addr, confd_port, sub_path) # tailf:cdb-oper subscription socket oper_sub_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0) cdb.connect(oper_sub_sock, cdb.SUBSCRIPTION_SOCKET, confd_addr, confd_port, sub_path) oper_sub_point = cdb.oper_subscribe(oper_sub_sock, routes_ns.ns.hash, sub_path) cdb.subscribe_done(oper_sub_sock) try: read_list = [oper_sub_sock] write_list = [] error_list = [] print('entering poll loop') while True: read_socks = select.select(read_list, write_list, error_list, 1)[0] for rs in read_socks: # process only our cdb-oper subscription socket here... if rs.fileno() is not oper_sub_sock.fileno(): continue try: sub_points = cdb.read_subscription_socket(oper_sub_sock) for s in sub_points: if s != oper_sub_point: continue print("CDB operational subscription point triggered") cdb.start_session(data_sock, cdb.OPERATIONAL) cdb.set_namespace(data_sock, routes_ns.ns.hash) cdb.diff_iterate(oper_sub_sock, oper_sub_point, iterate_changes, 0, None) cdb.end_session(data_sock) cdb.sync_subscription_socket(oper_sub_sock, cdb.DONE_OPERATIONAL) except _confd.error.Error as e: if e.confd_errno is not _confd.ERR_EXTERNAL: raise e except KeyboardInterrupt: print("\nCtrl-C pressed\n") finally: data_sock.close() oper_sub_sock.close()
def run(): log.info("==>") # In C we use confd_init() which sets the debug-level, but for Python the # call to confd_init() is done when we do 'import confd'. # Therefore we need to set the ConfD debug level here (if we want it to be # different from the default debug level - CONFD_SILENT): _confd.set_debug(confd_debug_level, sys.stderr) sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0) subsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0) # maapi socket for load schemas maapisock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0) maapi.connect(maapisock, CONFD_ADDR, _confd.CONFD_PORT) maapi.load_schemas(maapisock) cdb.connect(sock, cdb.DATA_SOCKET, CONFD_ADDR, _confd.CONFD_PORT) sub_path = "/root/NodeB/RFHead" cdb.connect(subsock, cdb.SUBSCRIPTION_SOCKET, CONFD_ADDR, _confd.CONFD_PORT) spoint = cdb.subscribe(subsock, 3, root_ns.ns.hash, sub_path) cdb.subscribe_done(subsock) log.debug("Subscribed to path %s spoint=%i" % (sub_path, spoint)) read_db(sock) try: _r = [subsock] _w = [] _e = [] log.debug("subscok connected, starting ConfD loop") while (True): (r, w, e) = select.select(_r, _w, _e, 1) # log.debug("select triggered r=%r" % r) for rs in r: log.debug("rs.fileno=%i subscok.fileno=%i" % (rs.fileno(), subsock.fileno())) if rs.fileno() == subsock.fileno(): log.debug("subsock triggered") try: sub_points = cdb.read_subscription_socket(subsock) for s in sub_points: if s == spoint: log.debug("our spoint=%i triggered" % spoint) cdb.start_session(sock, cdb.RUNNING) cdb.set_namespace(sock, root_ns.ns.hash) cdb.diff_iterate(subsock, spoint, iter, _confd.ITER_WANT_PREV, sock) cdb.end_session(sock) # variant with diff_match not yet available # in python cdb.sync_subscription_socket( subsock, cdb.DONE_PRIORITY) except (_confd.error.Error) as e: if e.confd_errno is not _confd.ERR_EXTERNAL: raise e except KeyboardInterrupt: print("\nCtrl-C pressed\n") finally: sock.close() subsock.close() log.info("<==")
def ack(self): cdb.sync_subscription_socket(self.sock, cdb.DONE_PRIORITY)