<commit_before>"""
OpenNSA Vector path finder (the nordunet-surfnet variant)

Here the word vector means a topology urn and an associated cost.

Author: <NAME> <<EMAIL>>

Copyright: NORDUnet (2011-2014)
"""

from twisted.python import log



LOG_SYSTEM = 'topology.gns'

DEFAULT_MAX_COST = 5



class _NSAVector:

    def __init__(self, cost, topology_urns, vectors):
        assert type(cost) is int, 'cost param must be integer'
        assert cost >= 0, 'cost param must be plus-zero'
        assert type(topology_urns) is list, 'topology urns must be a list'
        assert type(vectors) is dict, 'reachable_urns_cost must be a dict'
        self.cost = cost
        self.topology_urns = topology_urns
        self.vectors = vectors


    def getVectors(self):
        vectors = {}
        for topo_urn, vcost in self.vectors.items():
            vectors[topo_urn] = self.cost + vcost
        for topo_urn in self.topology_urns:
            vectors[topo_urn] = self.cost
        return vectors



class RouteVectors:

    def __init__(self, local_networks, blacklist_networks=None, max_cost=DEFAULT_MAX_COST):

        # networks hosted by the nsa itself, we want these in the vectors (though currently not used for anything),
        # but don't want to export/use them in reachability
        self.local_networks = local_networks
        self.blacklist_networks = blacklist_networks if not blacklist_networks is None else []
        self.max_cost = max_cost

        # this is a set of vectors we keep for each peer
        self.vectors = {} # nsa_urn -> _NSAVector

        # this is the calculated shortest paths, should be recalculated when new information gets available
        self._shortest_paths = {} # topology_urn -> ( nsa_urn, cost)

        self.subscribers = []

    # -- updates

    def callOnUpdate(self, f):
        self.subscribers.append(f)


    def updated(self):
        for f in self.subscribers:
            f()

    # -- vector stuff

    def getProvider(self, topology_urn):

        for nsa_urn, nsa_vector in self.vectors.items():
            if topology_urn in nsa_vector.topology_urns:
                return nsa_urn


    def updateVector(self, nsa_urn, nsa_cost, topology_urns, vectors):
        # we need a way to keep the local topology urns out of this

        self.vectors[nsa_urn] = _NSAVector(nsa_cost, topology_urns, vectors)
        self._calculateVectors()
        self.updated()


    def deleteVector(self, nsa_urn):
        try:
            self.vectors.pop(nsa_urn)
            self._calculateVectors()
        except KeyError:
            log.msg('Tried to delete non-existing vector for %s' % nsa_urn)


    def _calculateVectors(self):

        paths = {}
        for nsa_urn, nsa_vector in self.vectors.items():
            topo_vectors = nsa_vector.getVectors()

            for topo_urn, cost in topo_vectors.items():
                if topo_urn in self.local_networks:
                    continue # skip local networks
                if topo_urn in self.blacklist_networks:
                    log.msg('Skipping network %s in vector calculation, is blacklisted' % topo_urn, system=LOG_SYSTEM)
                    continue
                if cost > self.max_cost:
                    log.msg('Skipping network %s in vector calculation, cost %i exceeds max cost %i' % (topo_urn, cost, self.max_cost), system=LOG_SYSTEM)
                    continue
                if not topo_urn in paths:
                    paths[topo_urn] = (nsa_urn, cost)
                elif cost < paths[topo_urn]:<commit_msg>fix bug comparing costs in when calculating route vectors<commit_after>"""
OpenNSA Vector path finder (the nordunet-surfnet variant)

Here the word vector means a topology urn and an associated cost.

Author: <NAME> <<EMAIL>>

Copyright: NORDUnet (2011-2014)
"""

from twisted.python import log



LOG_SYSTEM = 'topology.gns'

DEFAULT_MAX_COST = 5



class _NSAVector:

    def __init__(self, cost, topology_urns, vectors):
        assert type(cost) is int, 'cost param must be integer'
        assert cost >= 0, 'cost param must be plus-zero'
        assert type(topology_urns) is list, 'topology urns must be a list'
        assert type(vectors) is dict, 'reachable_urns_cost must be a dict'
        self.cost = cost
        self.topology_urns = topology_urns
        self.vectors = vectors


    def getVectors(self):
        vectors = {}
        for topo_urn, vcost in self.vectors.items():
            vectors[topo_urn] = self.cost + vcost
        for topo_urn in self.topology_urns:
            vectors[topo_urn] = self.cost
        return vectors



class RouteVectors:

    def __init__(self, local_networks, blacklist_networks=None, max_cost=DEFAULT_MAX_COST):

        # networks hosted by the nsa itself, we want these in the vectors (though currently not used for anything),
        # but don't want to export/use them in reachability
        self.local_networks = local_networks
        self.blacklist_networks = blacklist_networks if not blacklist_networks is None else []
        self.max_cost = max_cost

        # this is a set of vectors we keep for each peer
        self.vectors = {} # nsa_urn -> _NSAVector

        # this is the calculated shortest paths, should be recalculated when new information gets available
        self._shortest_paths = {} # topology_urn -> ( nsa_urn, cost)

        self.subscribers = []

    # -- updates

    def callOnUpdate(self, f):
        self.subscribers.append(f)


    def updated(self):
        for f in self.subscribers:
            f()

    # -- vector stuff

    def getProvider(self, topology_urn):

        for nsa_urn, nsa_vector in self.vectors.items():
            if topology_urn in nsa_vector.topology_urns:
                return nsa_urn


    def updateVector(self, nsa_urn, nsa_cost, topology_urns, vectors):
        # we need a way to keep the local topology urns out of this

        self.vectors[nsa_urn] = _NSAVector(nsa_cost, topology_urns, vectors)
        self._calculateVectors()
        self.updated()


    def deleteVector(self, nsa_urn):
        try:
            self.vectors.pop(nsa_urn)
            self._calculateVectors()
        except KeyError:
            log.msg('Tried to delete non-existing vector for %s' % nsa_urn)


    def _calculateVectors(self):

        paths = {}
        for nsa_urn, nsa_vector in self.vectors.items():
            topo_vectors = nsa_vector.getVectors()

            for topo_urn, cost in topo_vectors.items():
                if topo_urn in self.local_networks:
                    continue # skip local networks
                if topo_urn in self.blacklist_networks:
                    log.msg('Skipping network %s in vector calculation, is blacklisted' % topo_urn, system=LOG_SYSTEM)
                    continue
                if cost > self.max_cost:
                    log.msg('Skipping network %s in vector calculation, cost %i exceeds max cost %i' % (topo_urn, cost, self.max_cost), system=LOG_SYSTEM)
                    continue
                if not topo_urn in paths:
                    paths[topo_urn] = (nsa_urn, cost)
                elif cost < paths[topo_urn][1]:
{'fraction_non_alphanumeric': 0.07813590065578345, 'fraction_numerical': 0.0029300962745918793, 'mean_word_length': 2.313915857605178, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6851914', 'n_tokens_mistral': 2240, 'n_tokens_neox': 2072, 'n_words': 643}