Created
April 19, 2022 21:05
-
-
Save karthick18/abea484c6179008893e4093bea709595 to your computer and use it in GitHub Desktop.
create 3 switch with 3 hosts config with networkx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import collections | |
import networkx | |
class KindPort(object): | |
delimiter = "/" | |
def __init__(self, kind, name, port): | |
self.kind = kind | |
self.name = name | |
self.port = port | |
def __str__(self): | |
return '{}{}{}{}{}'.format(self.kind, self.delimiter, | |
self.name, self.delimiter, | |
self.port) | |
def __hash__(self): | |
return hash( (self.kind, self.name, self.port) ) | |
def __eq__(self, other): | |
return isinstance(other, self.__class__) and \ | |
self.kind == other.kind and self.name == other.name and self.port == other.port | |
def __ne__(self, other): | |
return not self.__eq__(other) | |
class Port(object): | |
def __init__(self, name, number): | |
self.name = name | |
self.number = number | |
def __str__(self): | |
return '{}-{}'.format(self.name, self.number) | |
def __hash__(self): | |
return hash( (self.name, self.number) ) | |
def __eq__(self, other): | |
return isinstance(other, self.__class__) and \ | |
self.name == other.name and self.number == other.number | |
def __ne__(self, other): | |
return not self.__eq__(other) | |
kindConfig = collections.namedtuple('KindConfig', ('kind', 'name', 'ports')) | |
kindPortConfig = [ | |
kindConfig('Switch', 's1', 2), | |
kindConfig('Switch', 's2', 3), | |
kindConfig('Switch', 's3', 4), | |
kindConfig('Host', 'h1', 1), | |
kindConfig('Host', 'h2', 2), | |
kindConfig('Host', 'h3', 3), | |
] | |
kindPortMap = {} | |
maxPorts = 1 | |
for config in kindPortConfig: | |
if config.ports > maxPorts: | |
maxPorts = config.ports | |
portFields = tuple(('p{}'.format(p) for p in range(1, maxPorts+1))) | |
ports = collections.namedtuple('Ports', portFields, defaults=('',) * len(portFields)) | |
kindObjectFields = tuple(('port{}'.format(p) for p in range(1, maxPorts+1))) | |
kindObjects = collections.namedtuple('Kinds', kindObjectFields, defaults=(None,) * len(kindObjectFields)) | |
for config in kindPortConfig: | |
key = config.name | |
portList = ('{}-eth{}'.format(config.name, p) for p in range(1, config.ports+1)) | |
kindPortMap[key] = ports(*portList)._asdict() | |
kindObjectList = () | |
for p in range(1, config.ports+1): | |
port = '{}-eth{}'.format(config.name, p) | |
obj = KindPort(config.kind, config.name, Port(port, p)) | |
kindObjectList += (obj,) | |
kindPortMap[key] = kindObjects(*kindObjectList)._asdict() | |
g = networkx.Graph() | |
# connect the local ports | |
connections = [] | |
for config in kindPortConfig: | |
key = config.name | |
for p in range(1, config.ports+1): | |
k1 = kindPortMap[key]['port{}'.format(p)] | |
for p2 in range(p+1, config.ports+1): | |
k2 = kindPortMap[key]['port{}'.format(p2)] | |
connections.append((k1, k2, 1)) | |
cross_connects = [ | |
( ('h1', 'port1'), ('s1', 'port1'), 1 ), | |
( ('h2', 'port2'), ('s2', 'port2'), 1 ), | |
( ('s1', 'port2'), ('s2', 'port1'), 2 ), | |
( ('s2', 'port3'), ('s3', 'port1'), 3 ), | |
( ('s3', 'port3'), ('h3', 'port3'), 1 ), | |
] | |
g.add_weighted_edges_from(connections) | |
cross_connections = [] | |
for x1, x2, weight in cross_connects: | |
key, port = x1[0], x1[1] | |
k1 = kindPortMap[key][port] | |
key, port = x2[0], x2[1] | |
k2 = kindPortMap[key][port] | |
cross_connections.append( (k1, k2, weight) ) | |
g.add_weighted_edges_from(cross_connections) | |
nodes = g.number_of_nodes() | |
print('Graph nodes: {}'.format(nodes)) | |
edges = g.number_of_edges() | |
print('Graph edges: {}'.format(edges)) | |
for n, neighbors in g.adjacency(): | |
for neigh, attr in neighbors.items(): | |
print('Node', n, 'Neighbor', neigh, 'Cost', attr['weight']) | |
source = KindPort('Host', 'h1', Port('h1-eth1', 1)) | |
target = KindPort('Host', 'h3', Port('h3-eth3', 3)) | |
path = networkx.shortest_path(g, source=source, target=target) | |
paths = list(map(lambda p: str(p), path)) | |
print('Shortest path from', source, 'to', target, '=', '->'.join(paths)) | |
#simplePaths = networkx.shortest_simple_paths(g, source=source, target=target) | |
#for index, path in enumerate(simplePaths): | |
# paths = list(map(lambda p: str(p), path)) | |
# print('Shortest path index', index, 'from', source, 'to', target, '=', '->'.join(paths)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment