# $Id$ """ Copyright (C) 2010, 2011 SPARTA, Inc. dba Cobham Analytic Solutions Permission to use, copy, modify, and distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. THE SOFTWARE IS PROVIDED "AS IS" AND SPARTA DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL SPARTA BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. """ from rpki.gui.app import misc, models from rpki import resource_set class AllocationTree(object): '''Virtual class representing a tree of unallocated resource ranges. Keeps track of which subsets of a resource range have been allocated.''' def __init__(self, resource): self.resource = resource self.range = resource.as_resource_range() self.need_calc = True def calculate(self): if self.need_calc: self.children = [] self.alloc = self.__class__.set_type() self.unalloc = self.__class__.set_type() if self.is_allocated(): self.alloc.append(self.range) else: for child in self.resource.children.all(): c = self.__class__(child) if c.unallocated(): self.children.append(c) self.alloc = self.alloc.union(c.alloc) total = self.__class__.set_type() total.append(self.range) self.unalloc = total.difference(self.alloc) self.need_calc=False def unallocated(self): self.calculate() return self.unalloc def as_ul(self): '''Returns a string of the tree as an unordered HTML list.''' s = [] s.append('%s' % (self.resource.get_absolute_url(), self.resource)) # when the unallocated range is a subset of the current range, # display the missing ranges u = self.unallocated() if len(u) != 1 or self.range != u[0]: s.append(' (missing: ') s.append(', '.join(str(x) for x in u)) s.append(')') # quick access links if self.resource.parent: s.append(' | delete' % (self.resource.get_absolute_url(),)) s.append(' | give' % (self.resource.get_absolute_url(),)) if self.range.min != self.range.max: s.append(' | split' % (self.resource.get_absolute_url(),)) # add type-specific actions a = self.supported_actions() if a: s.extend(a) if self.children: s.append('\n