aboutsummaryrefslogtreecommitdiff
path: root/scripts/irbe-cli.py
diff options
context:
space:
mode:
authorRob Austein <sra@hactrn.net>2007-09-21 05:06:34 +0000
committerRob Austein <sra@hactrn.net>2007-09-21 05:06:34 +0000
commiteec99329d04610689e27ccbfc885a184a8985110 (patch)
tree62ef90836956caf9cf5d0549b38f854d3c4bc644 /scripts/irbe-cli.py
parent142af92f8957881026b05238d334f5245912db86 (diff)
Refactor left-right CLI client, maybe I got it right this time.
svn path=/scripts/irbe-cli.py; revision=1002
Diffstat (limited to 'scripts/irbe-cli.py')
-rwxr-xr-xscripts/irbe-cli.py95
1 files changed, 84 insertions, 11 deletions
diff --git a/scripts/irbe-cli.py b/scripts/irbe-cli.py
index c2377022..522b2086 100755
--- a/scripts/irbe-cli.py
+++ b/scripts/irbe-cli.py
@@ -6,20 +6,93 @@ Command line IR back-end control program.
The query back-channel is handled by a separate program.
"""
-import sys, lxml.etree, lxml.sax
+import getopt, sys, lxml.etree, lxml.sax
import rpki.left_right, rpki.relaxng, rpki.cms, rpki.https, rpki.x509, rpki.config
-dispatch = dict((x.element_name, x)
- for x in (rpki.left_right.self_elt,
- rpki.left_right.bsc_elt,
- rpki.left_right.parent_elt,
- rpki.left_right.child_elt,
- rpki.left_right.repository_elt,
- rpki.left_right.route_origin_elt))
+class cmd_mixin(object):
+ """Left-right protocol mix-in for command line client."""
+
+ def client_getopt(self, argv):
+ """Parse options for this class."""
+ opts, args = getopt.getopt(argv, "", [x + "=" for x in self.attributes + self.elements] + list(self.booleans))
+ for o, a in opts:
+ o = o[2:]
+ handler = getattr(self, "client_query_" + o, None)
+ if handler is not None:
+ handler(a)
+ elif o in self.booleans:
+ setattr(self, o, True)
+ else:
+ assert o in self.attributes
+ setattr(self, o, a)
+ return args
+
+ def client_query_action(self, arg):
+ """Special handler for --action option."""
+ self.action = arg
+ self.type = "query"
+
+ def client_query_peer_ta(self, arg):
+ """Special handler for --peer_ta option."""
+ self.peer_ta = rpki.x509.X509(Auto_file=arg)
+
+ def client_reply_decode(self):
+ pass
+
+ def client_reply_show(self):
+ self.client_reply_decode()
+ print self.element_name
+ for i in self.attributes + self.elements:
+ if getattr(self, i) is not None:
+ print " %s: %s" % (i, getattr(self, i))
+
+class self_elt(cmd_mixin, rpki.left_right.self_elt):
+ def client_query_extension_preference(self, arg):
+ """--extension_preferences option."""
+ k,v = arg.split("=", 1)
+ pref = rpki.left_right.extension_preference_elt()
+ pref.name = k
+ pref.value = v
+ self.prefs.append(pref)
+
+class bsc_elt(cmd_mixin, rpki.left_right.bsc_elt):
+ def client_query_signing_cert(self, arg):
+ """--signing_cert option."""
+ self.signing_cert.append(rpki.x509.X509(Auto_file=arg))
+
+class parent_elt(cmd_mixin, rpki.left_right.parent_elt):
+ pass
+
+class child_elt(cmd_mixin, rpki.left_right.child_elt):
+ pass
+
+class repository_elt(cmd_mixin, rpki.left_right.repository_elt):
+ pass
+
+class route_origin_elt(cmd_mixin, rpki.left_right.route_origin_elt):
+
+ def client_query_as_number(self, arg):
+ """Handle autonomous sequence numbers."""
+ self.as_number = long(arg)
+
+ def client_query_ipv4(self, arg):
+ """Handle IPv4 addresses."""
+ self.ipv4 = resource_set.resource_set_ipv4(arg)
+
+ def client_query_ipv6(self, arg):
+ """Handle IPv6 addresses."""
+ self.ipv6 = resource_set.resource_set_ipv6(arg)
+
+class msg(rpki.left_right.msg):
+ pdus = dict((x.element_name, x)
+ for x in (self_elt, bsc_elt, parent_elt, child_elt, repository_elt, route_origin_elt))
+
+class sax_handler(rpki.left_right.sax_handler):
+ pdu = msg
def usage():
print "Usage:", sys.argv[0]
- for k,v in dispatch.iteritems():
+ for k,v in msg.pdus.items():
print " ", k, \
" ".join(["--" + x + "=" for x in v.attributes + v.elements]), \
" ".join(["--" + x for x in v.booleans])
@@ -52,7 +125,7 @@ def main():
while argv:
try:
- q_pdu = dispatch[argv[0]]()
+ q_pdu = msg.pdus[argv[0]]()
except KeyError:
usage()
argv = q_pdu.client_getopt(argv[1:])
@@ -88,7 +161,7 @@ def main():
print "Received:"
print r_xml
- handler = rpki.left_right.sax_handler()
+ handler = sax_handler()
lxml.sax.saxify(r_elt, handler)
r_msg = handler.result