misc.networkx
This Model Component provides functions and objects that make it easier to convert a NetworkX graph into a FIREWHEEL ExperimentGraph
.
Specifically, this is useful when using NetworkX Graph Generators.
This MC will only help build out a basic ExperimentGraph
and it is left to the users to then decorate the Vertecies
and create “real” edges based on the outline provided.
- Attribute Depends:
graph
- Model Component Depends:
base_objects
Example
The example shown below is a plugin.py
file for converting networkx.generators.classic.star_graph()
into a FIREWHEEL topology.
This is intentionally a simple example.
A more complex example, provided in tests.networkx, shows how users can make use of the networkx.random_internet_as_graph()
topology.
import netaddr
import networkx as nx
from misc.networkx import convert_nx_to_fw
from linux.ubuntu2204 import Ubuntu2204Server
from firewheel.control.experiment_graph import Vertex, AbstractPlugin
from base_objects import Switch
class Plugin(AbstractPlugin):
"""
This plugin provides an example of how to convert a NetworkX
based graph into a FIREWHEEL experiment.
"""
def run(self, num_nodes="10"):
"""
This example creates a NetworkX star network and converts it into a FIREWHEEL topology.
In this case the center of the Star will be a Switch and all the other nodes will
be Ubuntu servers.
Args:
num_nodes (str): Signifies the number of nodes in the network.
This should be convertible to an :py:data:`int`.
Raises:
RuntimeError: If the input parameters are improperly formatted.
"""
# Catch any issues with input parameters
try:
num_nodes = int(num_nodes)
except (TypeError, ValueError) as exc:
raise RuntimeError("The number of nodes should be an integer") from exc
# Create the star graph with the specified number of nodes
nx_net = nx.star_graph(num_nodes)
# Convert the NetworkX graph to FIREWHEEL Vertecies/Edges
convert_nx_to_fw(nx_net, self.g)
# In this topology, node labels are 0 to `num_nodes` with the center
# being 0. In this case, we can make that center node a Switch and
# then all the other nodes Ubuntu servers.
switch = self.g.find_vertex_by_id(0)
switch.name = "center-switch"
switch.decorate(Switch)
# Create a network for the VMs to communicate
network = netaddr.IPNetwork("10.0.0.0/8")
ips = network.iter_hosts()
# We can iterate through all the Vertecies and decorate them.
# Additionally, let's rename them
for node in self.g.get_vertices():
if node.graph_id == 0:
continue
# For each node, rename it and decorate it with the correct
# VM object
node.name = f"server-{node.graph_id}"
node.decorate(Ubuntu2204Server)
# Connect the Vertex to the Switch
node.connect(switch, next(ips), network.netmask)
Available Objects
This module contains all necessary Model Component Objects for misc.networkx.
- class misc.networkx.NxEdge(*args, **kwargs)[source]
Bases:
object
This class is intended to indicate that an
Edge
is a NetworkXEdge
in the graph. That is, theEdge
exists in the graph but won’t be used when the graph is instantiated. This is useful when generating graphs using NetworkX.- __init__(*args, **kwargs)
- misc.networkx.convert_nx_to_fw(nx_graph, fw_graph)[source]
This function enables converting a NetworkX
networkx.Graph
to afirewheel.control.experiment_graph.ExperimentGraph
. Specifically, nodes in the NetworkXnetworkx.Graph
are converted toVertecies
and the edges are converted toNxEdge
. The attribute dictionary for both NetworkX nodes/edges are preserved in a new attribute on theVertex
andEdge
callednx_data
.- Parameters:
nx_graph (networkx.Graph) – The
networkx.Graph
to convert.fw_graph (ExperimentGraph) – The FIREWHEEL
firewheel.control.experiment_graph.ExperimentGraph
where the new nodes/edges will be added.