Exemplo n.º 1
0
 def api_list(self, request, peering_point, name, as_set):
     if not WhoisCache.has_asset_members():
         return {
             "name":
             name,
             "prefix_list":
             "",
             "success":
             False,
             "message":
             _("AS-SET members cache is empty. Please update Whois Cache")
         }
     if not WhoisCache.has_origin_routes():
         return {
             "name":
             name,
             "prefix_list":
             "",
             "success":
             False,
             "message":
             _("Origin routes cache is empty. Please update Whois Cache")
         }
     if not WhoisCache.has_asset(as_set):
         return {
             "name": name,
             "prefix_list": "",
             "success": False,
             "message": _("Unknown AS-SET")
         }
     prefixes = WhoisCache.resolve_as_set_prefixes_maxlen(as_set)
     if not prefixes:
         return {
             "name": name,
             "prefix_list": "",
             "success": False,
             "message": _("Cannot resolve AS-SET prefixes")
         }
     try:
         pl = peering_point.profile.get_profile().generate_prefix_list(
             name, prefixes)
     except NotImplementedError:
         return {
             "name":
             name,
             "prefix_list":
             "",
             "success":
             False,
             "message":
             _("Prefix-list generator is not implemented for this profile")
         }
     return {
         "name": name,
         "prefix_list": pl,
         "success": True,
         "message": _("Prefix List built")
     }
Exemplo n.º 2
0
 def build_prefix_list(self, out, expression, name, profile):
     if not WhoisCache.has_asset_members():
         self.die(
             "AS-SET members cache is empty. Please update Whois Cache")
     if not WhoisCache.has_origin_routes():
         self.die("Origin routes cache is empty. Please update Whois Cache")
     if not WhoisCache.has_asset(expression):
         self.die("Unknown AS-SET")
     prefixes = WhoisCache.resolve_as_set_prefixes_maxlen(expression)
     if profile is None:
         ll = "\n".join(p[0] for p in prefixes)
     else:
         ll = profile.get_profile().generate_prefix_list(name, prefixes)
     if not ll.endswith("\n"):
         ll += "\n"
     out.write(ll)
Exemplo n.º 3
0
 def handle_prefix_list(self, as_set, name=None, profile=None, *args, **options):
     from noc.peer.models.whoiscache import WhoisCache
     from noc.sa.models.profile import Profile
     p = Profile.get_by_name(profile)
     if not p:
         self.die("Invalid profile %s" % profile)
     prefixes = WhoisCache.resolve_as_set_prefixes_maxlen(as_set[0])
     self.print(p.get_profile().generate_prefix_list(name, prefixes))
Exemplo n.º 4
0
    def get_data(self, request):
        def ppower(prefix):
            m = int(prefix.split("/")[1])
            if m <= powermask:
                return long(2 * (powermask - m))
            else:
                return 0

        powermask = 24
        r = []  # (Descption, as, filter, cone)
        peers = {}  # peer id -> peer
        cone_powers = {}  # peer id -> power
        uniq_powers = {}  # peer id -> power
        prefixes = {}  # Prefix -> set(peer ids)
        for p in Peer.objects.filter(status="A").exclude(import_filter="ANY"):
            peers[p.id] = p
            cone_powers[p.id] = 0
            for cp in WhoisCache.resolve_as_set_prefixes(p.import_filter,
                                                         optimize=True):
                # Get powers
                cone_powers[p.id] += ppower(cp)
                # Assign to prefixes
                for i in IP.prefix(cp).iter_cover(powermask):
                    pfx = i.prefix
                    try:
                        prefixes[pfx].add(p.id)
                    except KeyError:
                        prefixes[pfx] = set([p.id])
        # Calculate unique powers
        for pfx in prefixes:
            pfx_peers = prefixes[pfx]
            if len(pfx_peers) == 1:
                # Unique
                peer = list(pfx_peers)[0]
                try:
                    uniq_powers[peer] += 1
                except KeyError:
                    uniq_powers[peer] = 1
        # Build result
        for peer_id in peers:
            p = peers[peer_id]
            r += [(p.description, "AS%d" % p.remote_asn, p.import_filter,
                   cone_powers.get(peer_id, 0), uniq_powers.get(peer_id, 0))]
        r = sorted(r, key=lambda x: -x[4])
        return self.from_dataset(title=self.title,
                                 columns=[
                                     "Peer",
                                     "ASN",
                                     "Import Filter",
                                     TableColumn("Cone Power",
                                                 format="numeric",
                                                 align="right"),
                                     TableColumn("Uniq. Cone Power",
                                                 format="numeric",
                                                 align="right"),
                                 ],
                                 data=r)
Exemplo n.º 5
0
    def build_prefix_lists(cls):
        from noc.peer.models.peeringpoint import PeeringPoint
        from noc.peer.models.whoiscache import WhoisCache

        for pp in PeeringPoint.objects.all():
            profile = pp.profile
            for name, filter_exp in pp.generated_prefix_lists:
                prefixes = WhoisCache.resolve_as_set_prefixes_maxlen(
                    filter_exp)
                pl = profile.generate_prefix_list(name, prefixes)
                yield (pp, name, pl, prefixes)