aboutsummaryrefslogtreecommitdiff
path: root/portal-gui/scripts/load_csv.py
diff options
context:
space:
mode:
authorMichael Elkins <melkins@tislabs.com>2010-07-27 20:45:21 +0000
committerMichael Elkins <melkins@tislabs.com>2010-07-27 20:45:21 +0000
commit0b23a6b2f42a37db3e615a828d148e274221c8f4 (patch)
treef9a8f873db0d3054cfebe54081cf054abdb94d75 /portal-gui/scripts/load_csv.py
parent25665694c204d928d6b78bebfa7d84c421a11e18 (diff)
strictly enforce non-overlapping resource ranges in AddressRange and Asn object trees.
add cert member to Roa class to hold a pointer to the resource cert from which all prefixes in the roa derive svn path=/portal-gui/rpkigui/myrpki/forms.py; revision=3418
Diffstat (limited to 'portal-gui/scripts/load_csv.py')
-rwxr-xr-xportal-gui/scripts/load_csv.py41
1 files changed, 16 insertions, 25 deletions
diff --git a/portal-gui/scripts/load_csv.py b/portal-gui/scripts/load_csv.py
index 76fbaeb2..61ec15b6 100755
--- a/portal-gui/scripts/load_csv.py
+++ b/portal-gui/scripts/load_csv.py
@@ -48,25 +48,19 @@ print 'processing csv files for resource handle', handle
conf = models.Conf.objects.get(handle=handle)
# every parent has a favorite
-def best_child(parent, parent_range):
+def best_child(address_range, parent, parent_range):
'''Return the child address range that is the closest match, or
returns the arguments if no children.'''
- best = None
- best_range = None
- for q in parent.children.all():
- if best is None:
- best = q
- best_range = q.as_resource_range()
- else:
- t = q.as_resource_range()
- if t.min >= best_range.min and t.max <= best_range.max:
- best = q
- best_range = t
- if best:
- if best.children.all():
- best, best_range = best_child(best, best_range)
- return (best, best_range)
-
+ if address_range == parent_range:
+ return (parent, parent_range)
+ for q in list(parent.children.all()): # force strict evaluation
+ t = q.as_resource_range()
+ if t.min <= address_range.min and t.max >= address_range.max:
+ return best_child(address_range, q, t)
+ # check for overlap
+ if t.min <= address_range.min <= t.max or t.min <= address_range.max <= t.max:
+ raise RuntimeError, \
+ 'can not handle overlapping ranges: %s and %s' % (address_range, t)
return parent, parent_range
def get_or_create_prefix(address_range):
@@ -92,7 +86,7 @@ def get_or_create_prefix(address_range):
address_range,)
# find the best match among the children + grandchildren
- prefix, prefix_range = best_child(prefix, prefix_range)
+ prefix, prefix_range = best_child(address_range, prefix, prefix_range)
print 'best match for %s is %s' % (address_range, prefix)
if prefix_range.min != address_range.min or prefix_range.max != address_range.max:
@@ -100,7 +94,6 @@ def get_or_create_prefix(address_range):
print 'creating new range'
prefix = models.AddressRange.objects.create(lo=str(address_range.min),
hi=str(address_range.max), parent=prefix)
-
return prefix
def get_or_create_asn(asn):
@@ -108,18 +101,14 @@ def get_or_create_asn(asn):
from_cert__parent__in=conf.parents.all())
if not asn_set:
raise RuntimeError, '%s does not match any received AS range' % (asn,)
- best = None
- for a in asn_set:
- if best is None:
- best = a
- elif a.lo >= best.lo and a.hi <= best.hi:
- best = a
+ best = best_child(asn, asn_set[0], asn_set[0].as_resource_range())[0]
print 'best match for %s is %s' % (asn, best)
if best.lo != asn.min or best.hi != asn.max:
best = models.Asn.objects.create(lo=asn.min, hi=asn.max, parent=best)
return best
def do_asns():
+ print 'processing', asn_csv
for child_handle, asn in csv_reader(asn_csv, columns=2):
asn_range = rpki.resource_set.resource_range_as.parse_str(asn)
child = conf.children.get(handle=child_handle)
@@ -127,6 +116,7 @@ def do_asns():
child.asn.add(asn)
def do_prefixes():
+ print 'processing', prefix_csv
for child_handle, prefix in csv_reader(prefix_csv, columns=2):
child = conf.children.get(handle=child_handle)
try:
@@ -138,6 +128,7 @@ def do_prefixes():
obj.save()
def do_roas():
+ print 'processing', roa_csv
for prefix, asn, group in csv_reader(roa_csv, columns=3):
try:
rs = rpki.resource_set.roa_prefix_ipv4.parse_str(prefix)