2013-11-04 06:06:11 -08:00
|
|
|
#!/usr/bin/python
|
2013-11-13 16:07:15 -08:00
|
|
|
#
|
|
|
|
# Copyright 2013. Cumulus Networks, Inc.
|
|
|
|
# Author: Roopa Prabhu, roopa@cumulusnetworks.com
|
|
|
|
#
|
|
|
|
# graph --
|
|
|
|
# graph helper module for ifupdown
|
|
|
|
#
|
2013-11-04 06:06:11 -08:00
|
|
|
|
|
|
|
import logging
|
|
|
|
from collections import deque
|
2014-01-16 06:46:17 -08:00
|
|
|
try:
|
|
|
|
from gvgen import *
|
|
|
|
except ImportError, e:
|
|
|
|
pass
|
2013-11-04 06:06:11 -08:00
|
|
|
|
|
|
|
class graph():
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.logger = logging.getLogger('ifupdown.' +
|
|
|
|
self.__class__.__name__)
|
|
|
|
|
|
|
|
@classmethod
|
2014-01-17 23:10:12 -08:00
|
|
|
def topological_sort_graphs_all(cls, dependency_graphs, indegrees):
|
2013-11-04 06:06:11 -08:00
|
|
|
S = []
|
|
|
|
Q = deque()
|
|
|
|
|
|
|
|
for ifname,indegree in indegrees.items():
|
|
|
|
if indegree == 0:
|
|
|
|
Q.append(ifname)
|
|
|
|
|
2014-01-17 23:10:12 -08:00
|
|
|
while len(Q) != 0:
|
|
|
|
# initialize queue
|
|
|
|
x = Q.popleft()
|
|
|
|
|
|
|
|
# Get dependents of x
|
|
|
|
dlist = dependency_graphs.get(x)
|
|
|
|
if dlist == None or len(dlist) == 0:
|
|
|
|
S.append(x)
|
|
|
|
continue
|
|
|
|
|
|
|
|
for y in dlist:
|
|
|
|
indegrees[y] = indegrees.get(y) - 1
|
|
|
|
if indegrees.get(y) == 0:
|
|
|
|
Q.append(y)
|
|
|
|
|
|
|
|
S.append(x)
|
|
|
|
|
|
|
|
for ifname,indegree in indegrees.items():
|
|
|
|
if indegree != 0:
|
|
|
|
raise Exception('cycle found involving iface %s' %ifname +
|
|
|
|
' (indegree %d)' %indegree)
|
|
|
|
|
|
|
|
return S
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def topological_sort_graph(cls, dependency_graph, indegrees, rootifname):
|
|
|
|
S = []
|
|
|
|
Q = deque()
|
|
|
|
|
|
|
|
Q.append(rootifname)
|
|
|
|
|
2013-11-04 06:06:11 -08:00
|
|
|
while len(Q) != 0:
|
|
|
|
# initialize queue
|
|
|
|
x = Q.popleft()
|
|
|
|
|
|
|
|
# Get dependents of x
|
|
|
|
dlist = dependency_graph.get(x)
|
|
|
|
if dlist == None or len(dlist) == 0:
|
|
|
|
S.append(x)
|
|
|
|
continue
|
|
|
|
|
|
|
|
for y in dlist:
|
|
|
|
indegrees[y] = indegrees.get(y) - 1
|
|
|
|
if indegrees.get(y) == 0:
|
|
|
|
Q.append(y)
|
|
|
|
|
|
|
|
S.append(x)
|
|
|
|
|
2014-01-17 23:10:12 -08:00
|
|
|
return S
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def topological_sort_graphs(cls, dependency_graphs, indegrees):
|
|
|
|
""" Sorts graph one at a time merges all the sorted graph
|
|
|
|
lists and returns a combined list
|
|
|
|
|
|
|
|
"""
|
|
|
|
sorted_graphs_list = []
|
|
|
|
for ifname,indegree in indegrees.items():
|
|
|
|
if indegree == 0:
|
|
|
|
sorted_graphs_list += cls.topological_sort_graph(
|
|
|
|
dependency_graphs, indegrees, ifname)
|
2013-11-04 06:06:11 -08:00
|
|
|
# If some indegrees are non zero, we have a cycle
|
|
|
|
for ifname,indegree in indegrees.items():
|
|
|
|
if indegree != 0:
|
2013-11-13 16:07:15 -08:00
|
|
|
raise Exception('cycle found involving iface %s' %ifname +
|
|
|
|
' (indegree %d)' %indegree)
|
2013-11-04 06:06:11 -08:00
|
|
|
|
2014-01-17 23:10:12 -08:00
|
|
|
return sorted_graphs_list
|
2014-01-16 06:46:17 -08:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def generate_dots(cls, dependency_graph, indegrees):
|
2014-02-15 21:39:13 -08:00
|
|
|
gvgraph = GvGen()
|
|
|
|
graphnodes = {}
|
|
|
|
for v in dependency_graph.keys():
|
|
|
|
graphnodes[v] = gvgraph.newItem(v)
|
2014-01-16 06:46:17 -08:00
|
|
|
|
2014-02-15 21:39:13 -08:00
|
|
|
for i, v in graphnodes.items():
|
|
|
|
dlist = dependency_graph.get(i, [])
|
|
|
|
if not dlist:
|
|
|
|
continue
|
|
|
|
for d in dlist:
|
|
|
|
gvgraph.newLink(v, graphnodes.get(d))
|
|
|
|
gvgraph.dot()
|