Source code for perfsim.equipments.topology

#  Copyright (C) 2020 Michel Gokan Khan
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License along
#  with this program; if not, write to the Free Software Foundation, Inc.,
#  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
#  This file is a part of the PerfSim project, which is now open source and available under the GPLv2.
#  Written by Michel Gokan Khan, February 2020


from __future__ import annotations

from copy import deepcopy
from typing import TYPE_CHECKING, Dict

from perfsim import Host, Router, Transmission, Observable, TopologyPrototype, TopologyLogObserver, TopologyLink

if TYPE_CHECKING:
    from perfsim import Simulation


[docs] class Topology(TopologyPrototype, Observable): """ This class represents a network topology. A network topology is the arrangement of the various elements (links, nodes, etc.) of a computer network. It has a name and a dictionary of nodes (hosts and routers) that are part of the topology. """ #: The dictionary of hosts that are part of the topology. The key is the host name and the value is the host object. before_recalculate_transmissions_bw_on_all_links: str def __init__(self, name: str, simulation: 'Simulation', egress_err: float, ingress_err: float, incoming_graph_data=None, hosts: Dict[str, Host] = None, routers: Dict[str, Router] = None, links: Dict[str, TopologyLink] = None, copy: bool = False, **attr): if copy: hosts = deepcopy(hosts) routers = deepcopy(routers) if links is not None: for link in links.values(): link.source = TopologyLink.get_node_from_dicts_by_name(node=link.source, hosts_dict=hosts, routers_dict=routers) link.destination = TopologyLink.get_node_from_dicts_by_name(node=link.destination, hosts_dict=hosts, routers_dict=routers) super().__init__(name=name, egress_err=egress_err, ingress_err=ingress_err, incoming_graph_data=deepcopy(incoming_graph_data) if copy else incoming_graph_data, hosts=hosts, routers=routers, links=links, **attr) self.sim = simulation Observable.__init__(self) if self.sim.debug_level > 0: self.attach_observer(TopologyLogObserver(topology=self))
[docs] def register_events(self): """ Register the events for the topology. :return: """ self.register_event(event_name="before_recalculate_transmissions_bw_on_all_links")
[docs] @classmethod def from_prototype(cls, simulation: Simulation, prototype: TopologyPrototype, copy: bool = False): """ Create a new topology from a prototype. :param simulation: :param prototype: :param copy: :return: """ return cls(name=prototype.name, simulation=simulation, egress_err=prototype.egress_err, ingress_err=prototype.ingress_err, incoming_graph_data=prototype.incoming_graph_data, hosts=prototype.hosts_dict, routers=prototype.routers_dict, links=prototype.topology_links_dict, copy=copy, **prototype.attr)