ulggraph.py 3.15 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
#!/usr/bin/env python
#
# ULG - Universal Looking Glass
# by Tomas Hlavacek (tomas.hlavacek@nic.cz)
# last udate: June 21 2012
#
# 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 3 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, see <http://www.gnu.org/licenses/>.


# Imports
import defaults

import gv
from pygraph.classes.digraph import digraph
from pygraph.readwrite.dot import write

USED_FILL_COLOR = '#DC738A'
29 30 31
USED_LINE_COLOR = 'red'
RECONLY_LINE_COLOR = 'gray'
REC_LINE_COLOR = 'blue'
32 33
FONT_SIZE = 10
LABEL_FONT_SIZE = 8
34 35
REC_LINE_WIDTH = 0.5
USED_LINE_WIDTH = 1.2
36 37 38 39

def bgp_graph_gen(graphdata,start=None,end=None):
	gr = digraph()

40 41
	def transform_nodename(name):
		return name.replace(":", "")
42

43 44
	def transform_label(s):
		return "".join(["&#%d;"%ord(x) for x in s])
45

46 47 48 49 50 51 52 53 54 55
	def add_node(graph,v,fillcolor=None,shape='ellipse'):
		vid = transform_nodename(v)
		if(not graph.has_node(vid)):
			params = [('fontsize',FONT_SIZE),('shape',shape),('label',transform_label(v))]
			if(fillcolor):
				params = params + [('style','filled'),('fillcolor',fillcolor)]

			graph.add_node(vid,attrs=params)

	def add_edge(graph,v1,v2,color='black',style='solid',penwidth=REC_LINE_WIDTH,label=None):
56 57 58
		if(v1==v2):
			return

59 60 61 62 63 64 65 66 67 68
		vid1 = transform_nodename(v1)
		vid2 = transform_nodename(v2)

		if(not graph.has_edge((vid1,vid2))):
			params = [('color',color),('style',style),('penwidth',penwidth)]
			if(label):
				params = params + [('fontsize',LABEL_FONT_SIZE),('label',transform_label(" "+label+"  "))]

			graph.add_edge((vid1,vid2),attrs=params)

69 70 71


	for gd in graphdata:
72 73 74 75 76 77 78 79
		if(start):
			add_node(gr,start,shape='box',fillcolor=USED_FILL_COLOR)
			add_node(gr,gd[0][0],fillcolor=USED_FILL_COLOR)
			add_edge(gr,start,gd[0][0],color=USED_LINE_COLOR,penwidth=USED_LINE_WIDTH,style='dashed')


		vparams = {}
		eparams = {}
80
		if(gd[1]['recuse']):
81 82 83
			eparams['color']=USED_LINE_COLOR
			eparams['penwidth']=USED_LINE_WIDTH
			vparams['fillcolor']=USED_FILL_COLOR
84
		elif(gd[1]['reconly']):
85 86 87
			eparams['color']=RECONLY_LINE_COLOR
			eparams['style']='dotted'
			eparams['penwidth']=REC_LINE_WIDTH
88
		else:
89 90
			eparams['penwidth']=REC_LINE_WIDTH
			eparams['color']=REC_LINE_COLOR
91 92 93 94

		i = 0
		while (i < len(gd[0])):
		       	if(i+1 < len(gd[0])):
95 96 97
				add_node(gr,gd[0][i],**vparams)
				add_node(gr,gd[0][i+1],**vparams)

98
				if((i==0) and ('peer' in gd[1])):
99
					add_edge(gr,gd[0][i],gd[0][i+1],label=gd[1]['peer'],**eparams)
100
				else:
101
					add_edge(gr,gd[0][i],gd[0][i+1],**eparams)
102 103
			i = i+1

104 105 106
		if(end):
			add_node(gr,end,shape='box',fillcolor=USED_FILL_COLOR)
			add_edge(gr,gd[0][-1],end,color=USED_LINE_COLOR,penwidth=USED_LINE_WIDTH,style='dashed')
107 108 109 110 111

	dot = write(gr)
	gvv = gv.readstring(dot)
	gv.layout(gvv,'dot')
	gv.render(gvv,'png')