Skip to content

Commit

Permalink
change to facilitate the implementation of json api calls
Browse files Browse the repository at this point in the history
following an attempt with #1240 this code provide all the glue code to
make it easier to convert all the api call to json
  • Loading branch information
thomas-mangin committed Oct 17, 2024
1 parent 0cea371 commit c479e53
Show file tree
Hide file tree
Showing 13 changed files with 550 additions and 466 deletions.
31 changes: 21 additions & 10 deletions src/exabgp/application/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,12 @@


class AnswerStream:
done = '\n%s\n' % Answer.done
error = '\n%s\n' % Answer.error
shutdown = '\n%s\n' % Answer.error
text_done = '\n%s\n' % Answer.text_done
text_error = '\n%s\n' % Answer.text_error
text_shutdown = '\n%s\n' % Answer.text_error
json_done = '\n%s\n' % Answer.json_done
json_error = '\n%s\n' % Answer.json_error
json_shutdown = '\n%s\n' % Answer.json_error
buffer_size = Answer.buffer_size + 2


Expand Down Expand Up @@ -174,13 +177,21 @@ def cmdline(cmdarg):
break

# we read some data but it is not ending by a new line (ie: not a command completion)
if rbuffer[-1] != 10: # \n
if rbuffer[-1] != ord('\n'):
continue
if AnswerStream.done.endswith(rbuffer.decode()[-len(AnswerStream.done) :]):

if AnswerStream.done.endswith(rbuffer.decode()[-len(AnswerStream.text_done) :]):
break
if AnswerStream.error.endswith(rbuffer.decode()[-len(AnswerStream.text_error) :]):
break
if AnswerStream.shutdown.endswith(rbuffer.decode()[-len(AnswerStream.text_shutdown) :]):
break

if AnswerStream.done.endswith(rbuffer.decode()[-len(AnswerStream.json_done) :]):
break
if AnswerStream.error.endswith(rbuffer.decode()[-len(AnswerStream.error) :]):
if AnswerStream.error.endswith(rbuffer.decode()[-len(AnswerStream.json_error) :]):
break
if AnswerStream.shutdown.endswith(rbuffer.decode()[-len(AnswerStream.shutdown) :]):
if AnswerStream.shutdown.endswith(rbuffer.decode()[-len(AnswerStream.json_shutdown) :]):
break

renamed = ['']
Expand Down Expand Up @@ -293,15 +304,15 @@ def cmdline(cmdarg):
while b'\n' in buf:
line, buf = buf.split(b'\n', 1)
string = line.decode()
if string == Answer.done:
if string == Answer.text_done or string == Answer.json_done:
done = True
break
if string == Answer.shutdown:
if string == Answer.text_shutdown or string == Answer.json_shutdown:
sys.stderr.write('ExaBGP is shutting down, command aborted\n')
sys.stderr.flush()
done = True
break
if string == Answer.error:
if string == Answer.text_error or string == Answer.json_error:
done = True
sys.stderr.write('ExaBGP returns an error (see ExaBGP\'s logs for more information)\n')
sys.stderr.write('use help for a list of available commands\n')
Expand Down
3 changes: 2 additions & 1 deletion src/exabgp/application/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from exabgp.environment import getconf

from exabgp.configuration.configuration import Configuration
from exabgp.bgp.neighbor import NeighborTemplate

from exabgp.debug import trace_interceptor
from exabgp.logger import log
Expand Down Expand Up @@ -61,7 +62,7 @@ def cmdline(cmdarg):
if cmdarg.neighbor:
log.warning('checking neighbors', 'configuration')
for name, neighbor in config.neighbors.items():
reparsed = neighbor.string()
reparsed = NeighborTemplate.configuration(neighbor)
log.debug(reparsed, configuration)
log.info(f'\u2713 neighbor {name.split()[1]}', 'configuration')

Expand Down
Loading

0 comments on commit c479e53

Please sign in to comment.