From f4e28413a66516035c49bca0f475028540b60126 Mon Sep 17 00:00:00 2001 From: Rob Austein Date: Wed, 18 Jul 2007 19:59:00 +0000 Subject: Simplify XML svn path=/docs/left-right-xml; revision=774 --- scripts/left-right-protocol-samples/pdu.003.xml | 7 +- scripts/left-right-protocol-samples/pdu.011.xml | 3 +- scripts/left-right-protocol-samples/pdu.021.xml | 6 +- scripts/left-right-protocol-samples/pdu.023.xml | 9 +- scripts/left-right-protocol-samples/pdu.026.xml | 6 +- scripts/left-right-protocol-samples/pdu.028.xml | 6 +- scripts/left-right-protocol-samples/pdu.031.xml | 4 +- scripts/left-right-protocol-samples/pdu.033.xml | 5 +- scripts/left-right-protocol-samples/pdu.036.xml | 4 +- scripts/left-right-protocol-samples/pdu.038.xml | 7 +- scripts/left-right-protocol-samples/pdu.041.xml | 4 +- scripts/left-right-protocol-samples/pdu.043.xml | 4 +- scripts/left-right-protocol-samples/pdu.046.xml | 4 +- scripts/left-right-protocol-samples/pdu.048.xml | 4 +- scripts/left-right-protocol-samples/pdu.053.xml | 5 +- scripts/left-right-protocol-samples/pdu.056.xml | 4 +- scripts/left-right-protocol-samples/pdu.058.xml | 4 +- scripts/rpki/left_right.py | 277 +++++++++--------------- scripts/xml-parse-test.py | 2 +- 19 files changed, 128 insertions(+), 237 deletions(-) (limited to 'scripts') diff --git a/scripts/left-right-protocol-samples/pdu.003.xml b/scripts/left-right-protocol-samples/pdu.003.xml index ab6f2020..927f6636 100644 --- a/scripts/left-right-protocol-samples/pdu.003.xml +++ b/scripts/left-right-protocol-samples/pdu.003.xml @@ -1,12 +1,7 @@ - + Blue - - - - - diff --git a/scripts/left-right-protocol-samples/pdu.011.xml b/scripts/left-right-protocol-samples/pdu.011.xml index d7483ac3..46b6a952 100644 --- a/scripts/left-right-protocol-samples/pdu.011.xml +++ b/scripts/left-right-protocol-samples/pdu.011.xml @@ -1,8 +1,7 @@ - + Y2VydDEK - diff --git a/scripts/left-right-protocol-samples/pdu.021.xml b/scripts/left-right-protocol-samples/pdu.021.xml index e2f7a702..b267db4e 100644 --- a/scripts/left-right-protocol-samples/pdu.021.xml +++ b/scripts/left-right-protocol-samples/pdu.021.xml @@ -1,11 +1,7 @@ - + cGVlcl90YQo= - - - - diff --git a/scripts/left-right-protocol-samples/pdu.023.xml b/scripts/left-right-protocol-samples/pdu.023.xml index a49be475..d6db2e95 100644 --- a/scripts/left-right-protocol-samples/pdu.023.xml +++ b/scripts/left-right-protocol-samples/pdu.023.xml @@ -1,14 +1,7 @@ - + cGVlcl90YQo= - - - - - - - diff --git a/scripts/left-right-protocol-samples/pdu.026.xml b/scripts/left-right-protocol-samples/pdu.026.xml index 4cc1d527..589cbc63 100644 --- a/scripts/left-right-protocol-samples/pdu.026.xml +++ b/scripts/left-right-protocol-samples/pdu.026.xml @@ -1,11 +1,7 @@ - + cGVlcl90YQo= - - - - diff --git a/scripts/left-right-protocol-samples/pdu.028.xml b/scripts/left-right-protocol-samples/pdu.028.xml index 639fac9b..2430e1fb 100644 --- a/scripts/left-right-protocol-samples/pdu.028.xml +++ b/scripts/left-right-protocol-samples/pdu.028.xml @@ -1,11 +1,7 @@ - + cGVlcl90YQo= - - - - diff --git a/scripts/left-right-protocol-samples/pdu.031.xml b/scripts/left-right-protocol-samples/pdu.031.xml index d103a5d5..547eb8ca 100644 --- a/scripts/left-right-protocol-samples/pdu.031.xml +++ b/scripts/left-right-protocol-samples/pdu.031.xml @@ -1,9 +1,7 @@ - + cGVlcl90YQo= - - diff --git a/scripts/left-right-protocol-samples/pdu.033.xml b/scripts/left-right-protocol-samples/pdu.033.xml index ef2da60d..3894ee54 100644 --- a/scripts/left-right-protocol-samples/pdu.033.xml +++ b/scripts/left-right-protocol-samples/pdu.033.xml @@ -1,10 +1,7 @@ - + cGVlcl90YQo= - - - diff --git a/scripts/left-right-protocol-samples/pdu.036.xml b/scripts/left-right-protocol-samples/pdu.036.xml index d2729feb..4603c071 100644 --- a/scripts/left-right-protocol-samples/pdu.036.xml +++ b/scripts/left-right-protocol-samples/pdu.036.xml @@ -1,9 +1,7 @@ - + cGVlcl90YQo= - - diff --git a/scripts/left-right-protocol-samples/pdu.038.xml b/scripts/left-right-protocol-samples/pdu.038.xml index 4426db32..3c5c0f42 100644 --- a/scripts/left-right-protocol-samples/pdu.038.xml +++ b/scripts/left-right-protocol-samples/pdu.038.xml @@ -2,8 +2,7 @@ - cGVlcl90YQo= - - - + bsc_link="17" + child_db_id="child_db_id"> + cGVlcl90YQo= diff --git a/scripts/left-right-protocol-samples/pdu.041.xml b/scripts/left-right-protocol-samples/pdu.041.xml index d2146d79..ed9d1656 100644 --- a/scripts/left-right-protocol-samples/pdu.041.xml +++ b/scripts/left-right-protocol-samples/pdu.041.xml @@ -1,9 +1,7 @@ - - + cGVlcl90YQo= - diff --git a/scripts/left-right-protocol-samples/pdu.043.xml b/scripts/left-right-protocol-samples/pdu.043.xml index 89295e3c..2475b409 100644 --- a/scripts/left-right-protocol-samples/pdu.043.xml +++ b/scripts/left-right-protocol-samples/pdu.043.xml @@ -1,9 +1,7 @@ - - + cGVlcl90YQo= - diff --git a/scripts/left-right-protocol-samples/pdu.046.xml b/scripts/left-right-protocol-samples/pdu.046.xml index 5647370d..85b45d3c 100644 --- a/scripts/left-right-protocol-samples/pdu.046.xml +++ b/scripts/left-right-protocol-samples/pdu.046.xml @@ -1,9 +1,7 @@ - - + cGVlcl90YQo= - diff --git a/scripts/left-right-protocol-samples/pdu.048.xml b/scripts/left-right-protocol-samples/pdu.048.xml index 93e02635..bf9aad50 100644 --- a/scripts/left-right-protocol-samples/pdu.048.xml +++ b/scripts/left-right-protocol-samples/pdu.048.xml @@ -1,9 +1,7 @@ - - + cGVlcl90YQo= - diff --git a/scripts/left-right-protocol-samples/pdu.053.xml b/scripts/left-right-protocol-samples/pdu.053.xml index 5f18578b..17cfa222 100644 --- a/scripts/left-right-protocol-samples/pdu.053.xml +++ b/scripts/left-right-protocol-samples/pdu.053.xml @@ -1,8 +1,5 @@ - - - - + diff --git a/scripts/left-right-protocol-samples/pdu.056.xml b/scripts/left-right-protocol-samples/pdu.056.xml index 2fa85016..27f15914 100644 --- a/scripts/left-right-protocol-samples/pdu.056.xml +++ b/scripts/left-right-protocol-samples/pdu.056.xml @@ -1,7 +1,5 @@ - - - + diff --git a/scripts/left-right-protocol-samples/pdu.058.xml b/scripts/left-right-protocol-samples/pdu.058.xml index 7f07fa77..43382d69 100644 --- a/scripts/left-right-protocol-samples/pdu.058.xml +++ b/scripts/left-right-protocol-samples/pdu.058.xml @@ -1,7 +1,5 @@ - - - + diff --git a/scripts/rpki/left_right.py b/scripts/rpki/left_right.py index 5e183a6e..b0130927 100644 --- a/scripts/rpki/left_right.py +++ b/scripts/rpki/left_right.py @@ -10,18 +10,31 @@ class base_elt(object): Base type for left-right message elements. """ + attributes = () + booleans = () + def startElement(self, stack, name, attrs): - pass + self.read_attrs(attrs) def endElement(self, stack, name, text): stack.pop() - def attr_maybe(self, key): - val = getattr(self, key, None) - if val is None: - return '' - else: - return ' %s="%s"' % (key, val) + def read_attrs(self, attrs): + for key in self.attributes: + setattr(self, key, attrs.get(key, None)) + for key in self.booleans: + setattr(self, key, attrs.get(key, False)) + + def print_attrs(self): + xml ="" + for key in self.attributes: + val = getattr(self, key, None) + if val is not None: + xml += ' %s="%s"' % (key, val) + for key in self.booleans: + if getattr(self, key, False): + xml += ' %s="yes"' % key + return xml class extension_preference_elt(base_elt): """ @@ -42,6 +55,7 @@ class extension_preference_elt(base_elt): class self_elt(base_elt): + attributes = ("action", "self_id") booleans = ("rekey", "reissue", "revoke", "run_now", "publish_world_now") rekey = False @@ -59,45 +73,36 @@ class self_elt(base_elt): self.prefs.append(pref) stack.append(pref) pref.startElement(stack, name, attrs) - elif name in self.booleans: - setattr(self, name, True) else: assert name == "self", "Unexpected name %s, stack %s" % (name, stack) - self.action = attrs["action"] - self.self_id = attrs.get("self_id") + self.read_attrs(attrs) def endElement(self, stack, name, text): - if name not in self.booleans: - assert name == "self", "Unexpected name %s, stack %s" % (name, stack) - stack.pop() + assert name == "self", "Unexpected name %s, stack %s" % (name, stack) + stack.pop() def __str__(self): - xml = ' \n' % (self.action, self.attr_maybe("self_id")) + xml = ' \n' % self.print_attrs() for i in self.prefs: xml += str(i) - for i in self.booleans: - if getattr(self, i): - xml += ' <%s/>\n' % i return xml + ' \n' class bsc_elt(base_elt): + attributes = ("action", "self_id", "bsc_id", "key_type", "hash_alg", "key_length") + booleans = ("generate_keypair",) + generate_keypair = False + pkcs10_cert_request = None + public_key = None def __init__(self): self.signing_cert = [] def startElement(self, stack, name, attrs): - if name == "generate_keypair": - self.generate_keypair = True - self.key_type = attrs["key_type"] - self.hash_alg = attrs["hash_alg"] - self.key_length = attrs["key_length"] - elif not name in ("signing_cert", "public_key", "pkcs10_cert_request"): + if not name in ("signing_cert", "public_key", "pkcs10_cert_request"): assert name == "bsc", "Unexpected name %s, stack %s" % (name, stack) - self.action = attrs["action"] - self.self_id = attrs["self_id"] - self.bsc_id = attrs.get("bsc_id") + self.read_attrs(attrs) def endElement(self, stack, name, text): if name == "signing_cert": @@ -106,219 +111,157 @@ class bsc_elt(base_elt): self.public_key = base64.b64decode(text) elif name == "pkcs10_cert_request": self.pkcs10_cert_request = base64.b64decode(text) - elif name != "generate_keypair": + else: assert name == "bsc", "Unexpected name %s, stack %s" % (name, stack) stack.pop() def __str__(self): - xml = (' \n' - % (self.action, self.self_id, self.attr_maybe("bsc_id"))) + xml = ' \n' % self.print_attrs() for i in self.signing_cert: xml += ' ' + base64.b64encode(i) + '\n' - i = getattr(self, "pkcs10_cert_request", None) - if i is not None: - xml += ' ' + base64.b64encode(i) + '\n' - i = getattr(self, "public_key", None) - if i is not None: - xml += ' ' + base64.b64encode(i) + '\n' + if self.pkcs10_cert_request: + xml += ' ' + base64.b64encode(self.pkcs10_cert_request) + '\n' + if self.public_key: + xml += ' ' + base64.b64encode(self.public_key) + '\n' return xml + ' \n' class parent_elt(base_elt): - ids = ("bsc_link", "repository_link") - uris = ("peer_contact", "sia_base") + attributes = ("self_id", "parent_id", "bsc_link", "repository_link", "peer_contact", "sia_base") booleans = ("rekey", "reissue", "revoke") rekey = False reissue = False revoke = False + peer_ta = None def startElement(self, stack, name, attrs): - if name in self.uris: - setattr(self, name, attrs["uri"]) - elif name in self.ids: - setattr(self, name, attrs["id"]) - elif name in self.booleans: - setattr(self, name, True) - elif name != "peer_ta": + if name != "peer_ta": assert name == "parent", "Unexpected name %s, stack %s" % (name, stack) - self.action = attrs["action"] - self.self_id = attrs["self_id"] - self.parent_id = attrs.get("parent_id") + self.read_attrs(attrs) def endElement(self, stack, name, text): if name == "peer_ta": self.peer_ta = base64.b64decode(text) - elif name not in self.booleans + self.ids + self.uris: + else: assert name == "parent", "Unexpected name %s, stack %s" % (name, stack) stack.pop() def __str__(self): - xml = (' \n' - % (self.action, self.self_id, self.attr_maybe("parent_id"))) - i = getattr(self, "peer_ta", None) - if i is not None: - xml += ' ' + base64.b64encode(i) + '\n' - i = getattr(self, "peer_contact", None) - if i is not None: - xml += ' \n' % i - i = getattr(self, "sia_base", None) - if i is not None: - xml += ' \n' % i - i = getattr(self, "bsc_link", None) - if i is not None: - xml += ' \n' % i - i = getattr(self, "repository_link", None) - if i is not None: - xml += ' \n' % i - for i in self.booleans: - if getattr(self, i): - xml += ' <%s/>\n' % i + xml = ' \n' % self.print_attrs() + if self.peer_ta: + xml += ' ' + base64.b64encode(self.peer_ta) + '\n' return xml + ' \n' class child_elt(base_elt): - ids = ("bsc_link", "child_db_id") + attributes = ("action", "self_id", "child_id", "bsc_link", "child_db_id") booleans = ("reissue", ) reissue = False + peer_ta = None def startElement(self, stack, name, attrs): - if name in self.ids: - setattr(self, name, attrs["id"]) - elif name in self.booleans: - setattr(self, name, True) - elif name != "peer_ta": + if name != "peer_ta": assert name == "child", "Unexpected name %s, stack %s" % (name, stack) - self.action = attrs["action"] - self.self_id = attrs["self_id"] - self.child_id = attrs.get("child_id") + self.read_attrs(attrs) def endElement(self, stack, name, text): if name == "peer_ta": self.peer_ta = base64.b64decode(text) - elif name not in self.booleans + self.ids: + else: assert name == "child", "Unexpected name %s, stack %s" % (name, stack) stack.pop() def __str__(self): - xml = (' \n' - % (self.action, self.self_id, self.attr_maybe("child_id"))) + xml = ' \n' % self.print_attrs() i = getattr(self, "peer_ta", None) - if i is not None: - xml += ' ' + base64.b64encode(i) + '\n' - i = getattr(self, "bsc_link", None) - if i is not None: - xml += ' \n' % i - i = getattr(self, "child_db_id", None) - if i is not None: - xml += ' \n' % i - for i in self.booleans: - if getattr(self, i): - xml += ' <%s/>\n' % i + if self.peer_ta: + xml += ' ' + base64.b64encode(self.peer_ta) + '\n' return xml + ' \n' class repository_elt(base_elt): + attributes = ("action", "self_id", "repository_id", "bsc_link", "peer_contact") + + peer_ta = None + def startElement(self, stack, name, attrs): - if name == "bsc_link": - self.bsc_link = attrs["id"] - elif name == "peer_contact": - self.peer_contact = attrs["uri"] - elif name != "peer_ta": + if name != "peer_ta": assert name == "repository", "Unexpected name %s, stack %s" % (name, stack) - self.action = attrs["action"] - self.self_id = attrs["self_id"] - self.repository_id = attrs.get("repository_id") + self.read_attrs(attrs) def endElement(self, stack, name, text): if name == "peer_ta": self.peer_ta = base64.b64decode(text) - elif name not in ("bsc_link", "peer_contact"): + else: assert name == "repository", "Unexpected name %s, stack %s" % (name, stack) stack.pop() def __str__(self): - xml = (' \n' - % (self.action, self.self_id, self.attr_maybe("repository_id"))) - i = getattr(self, "peer_ta", None) - if i is not None: - xml += ' ' + base64.b64encode(i) + '\n' - i = getattr(self, "peer_contact", None) - if i is not None: - xml += ' \n' % i - i = getattr(self, "bsc_link", None) - if i is not None: - xml += ' \n' % i + xml = ' \n' % self.print_attrs() + if self.peer_ta: + xml += ' ' + base64.b64encode(self.peer_ta) + '\n' return xml + ' \n' class route_origin_elt(base_elt): + attributes = ("action", "self_id", "route_origin_id", "asn", "ipv4", "ipv6") + booleans = ("suppress_publication",) + suppress_publication = False ipv4 = None ipv6 = None def startElement(self, stack, name, attrs): - if name == "suppress_publication": - self.suppress_publication = True - elif name == "resources": - self.asn = long(attrs["asn"]) - if "ipv4" in attrs: - self.ipv4 = resource_set.resource_set_ipv4(attrs["ipv4"]) - if "ipv6" in attrs: - self.ipv6 = resource_set.resource_set_ipv6(attrs["ipv6"]) - else: - assert name == "route_origin", "Unexpected name %s, stack %s" % (name, stack) - self.action = attrs["action"] - self.self_id = attrs["self_id"] - self.route_origin_id = attrs.get("route_origin_id") + assert name == "route_origin", "Unexpected name %s, stack %s" % (name, stack) + self.read_attrs(attrs) + if self.asn is not None: + self.asn = long(self.asn) + if self.ipv4 is not None: + self.ipv4 = resource_set.resource_set_ipv4(self.ipv4) + if self.ipv6 is not None: + self.ipv6 = resource_set.resource_set_ipv6(self.ipv4) def endElement(self, stack, name, text): - if name not in ("suppress_publication", "resources"): - assert name == "route_origin", "Unexpected name %s, stack %s" % (name, stack) - stack.pop() + assert name == "route_origin", "Unexpected name %s, stack %s" % (name, stack) + stack.pop() def __str__(self): - xml = (' \n' - % (self.action, self.self_id, self.attr_maybe("route_origin_id"))) - asn = getattr(self, "asn", None) - if asn is not None: - xml += ' \n' + return ' \n' % self.print_attrs() class resource_class_elt(base_elt): + attributes = ("as", "req_as", "ipv4", "req_ipv4", "ipv6", "req_ipv6") + def startElement(self, stack, name, attrs): assert name == "resource_class", "Unexpected name %s, stack %s" % (name, stack) - if "as" in attrs: - self.as = resource_set.resource_set_as(attrs["as"]) - if "req_as" in attrs: - self.req_as = resource_set.resource_set_as(attrs["req_as"]) - if "ipv4" in attrs: - self.ipv4 = resource_set.resource_set_ipv4(attrs["ipv4"]) - if "req_ipv4" in attrs: - self.req_ipv4 = resource_set.resource_set_ipv4(attrs["req_ipv4"]) - if "ipv6" in attrs: - self.ipv6 = resource_set.resource_set_ipv6(attrs["ipv6"]) - if "req_ipv6" in attrs: - self.req_ipv6 = resource_set.resource_set_ipv6(attrs["req_ipv6"]) - + self.read_attrs(attrs) + if self.as is not None: + self.as = resource_set.resource_set_as(self.as) + if self.req_as is not None: + self.req_as = resource_set.resource_set_as(self.req_as) + if self.ipv4 is not None: + self.ipv4 = resource_set.resource_set_ipv4(self.ipv4) + if self.req_ipv4 is not None: + self.req_ipv4 = resource_set.resource_set_ipv4(self.req_ipv4) + if self.ipv6 is not None: + self.ipv6 = resource_set.resource_set_ipv6(self.ipv6) + if self.req_ipv6 is not None: + self.req_ipv6 = resource_set.resource_set_ipv6(self.req_ipv6) + + def endElement(self, stack, name, text): + assert name == "resource_class", "Unexpected name %s, stack %s" % (name, stack) + stack.pop() + def __str__(self): - xml = ' \n' + return ' \n' % self.print_attrs() class list_resources_elt(base_elt): + attributes = ("self_id", "child_id", "valid_until") + def __init__(self): self.resources = [] @@ -330,26 +273,24 @@ class list_resources_elt(base_elt): rc.startElement(stack, name, attrs) else: assert name == "list_resources", "Unexpected name %s, stack %s" % (name, stack) - self.self_id = attrs["self_id"] - self.child_id = attrs.get("child_id") - self.valid_until = attrs.get("valid_until") + self.read_attrs(attrs) def __str__(self): - xml = (' \n' - % (self.self_id, self.attr_maybe("child_id"), self.attr_maybe("valid_until"))) + xml = ' \n' % self.print_attrs() for i in self.resources: xml += str(i) return xml + ' \n' class report_error_elt(base_elt): + attributes = ("self_id", "error_code") + def startElement(self, stack, name, attrs): assert name == "report_error", "Unexpected name %s, stack %s" % (name, stack) - self.self_id = attrs["self_id"] - self.error_code = attrs["error_code"] + self.read_attrs(attrs) def __str__(self): - return ' \n' % (self.self_id, self.error_code) + return ' \n' % self.print_attrs() class msg(list): """ @@ -386,9 +327,7 @@ class msg(list): def __str__(self): return ('\n' - '\n' + '\n' '%s\n' % (self.spec_uri, self.version, self.type, "".join(map(str, self)))) diff --git a/scripts/xml-parse-test.py b/scripts/xml-parse-test.py index 1f7954c3..96ac55ff 100755 --- a/scripts/xml-parse-test.py +++ b/scripts/xml-parse-test.py @@ -2,7 +2,7 @@ import glob, rpki.up_down, rpki.left_right, rpki.relaxng, xml.sax -if False: +if True: files = glob.glob("up-down-protocol-samples/*.xml") files.sort() for f in files: -- cgit v1.2.3