6 from urlparse import urlparse
10 from optparse import OptionParser
12 from sfa.client.sfi import Sfi
13 from sfa.util.sfalogging import sfa_logger,sfa_logger_goes_to_console
14 import sfa.util.xmlrpcprotocol as xmlrpcprotocol
16 def url_hostname_port (url):
19 # 1(netloc) returns the hostname+port part
20 parts=urlparse(url)[1].split(":")
23 return (url,parts[0],'80')
25 return (url,parts[0],parts[1])
30 def __init__ (self,url):
33 (self._url,self.hostname,self.port)=url_hostname_port(url)
34 self.ip=socket.gethostbyname(self.hostname)
37 # traceback.print_exc()
38 self.hostname="unknown"
48 # this is used as a key for creating graph nodes and to avoid duplicates
50 return "%s:%s"%(self.ip,self.port)
52 # connect to server and trigger GetVersion
53 def get_version(self):
56 # dummy to meet Sfi's expectations for its 'options' field
59 options=DummyOptions()
64 key_file = client.get_key_file()
65 cert_file = client.get_cert_file(key_file)
67 sfa_logger().info('issuing get version at %s'%url)
68 server=xmlrpcprotocol.get_server(url, key_file, cert_file, options)
69 self._version=server.GetVersion()
71 # traceback.print_exc()
77 def multi_lines_label(*lines):
78 result='<<TABLE BORDER="0" CELLBORDER="0"><TR><TD>' + \
79 '</TD></TR><TR><TD>'.join(lines) + \
81 # print 'multilines=',result
84 # default is for when we can't determine the type of the service
85 # typically the server is down, or we can't authenticate, or it's too old code
86 shapes = {"registry": "diamond", "slicemgr":"ellipse", "aggregate":"box", 'default':'plaintext'}
87 abbrevs = {"registry": "REG", "slicemgr":"SA", "aggregate":"AM", 'default':'[unknown interface]'}
89 # return a dictionary that translates into the node's attr
90 def get_layout (self):
92 ### retrieve cached GetVersion
93 version=self.get_version()
94 # set the href; xxx would make sense to try and 'guess' the web URL, not the API's one...
95 layout['href']=self.url()
96 ### set html-style label
97 ### see http://www.graphviz.org/doc/info/shapes.html#html
98 # if empty the service is unreachable
103 try: abbrev=Interface.abbrevs[version['interface']]
104 except: abbrev=Interface.abbrevs['default']
106 if 'hrn' in version: label += " %s"%version['hrn']
107 else: label += "[no hrn]"
108 if 'code_tag' in version:
109 label += " %s"%version['code_tag']
110 if 'testbed' in version:
111 label += " (%s)"%version['testbed']
112 layout['label']=Interface.multi_lines_label(self.url(),label)
114 try: shape=Interface.shapes[version['interface']]
115 except: shape=Interface.shapes['default']
116 layout['shape']=shape
117 ### fill color to outline wrongly configured bodies
118 if 'geni_api' not in version and 'sfa' not in version:
119 layout['style']='filled'
120 layout['fillcolor']='gray'
125 # provide the entry points (a list of interfaces)
129 def graph (self,entry_points):
130 graph=pygraphviz.AGraph(directed=True)
131 self.scan(entry_points,graph)
134 # scan from the given interfaces as entry points
135 def scan(self,interfaces,graph):
136 if not isinstance(interfaces,list):
137 interfaces=[interfaces]
139 # remember node to interface mapping
141 # add entry points right away using the interface uid's as a key
144 graph.add_node(i.uid())
145 node2interface[graph.get_node(i.uid())]=i
147 # keep on looping until we reach a fixed point
148 # don't worry about abels and shapes that will get fixed later on
150 for interface in to_scan:
151 # performing xmlrpc call
152 version=interface.get_version()
153 # 'sfa' is expected if the call succeeded at all
154 # 'peers' is needed as well as AMs typically don't have peers
155 if 'sfa' in version and 'peers' in version:
156 # proceed with neighbours
157 for (next_name,next_url) in version['peers'].items():
158 next_interface=Interface(next_url)
159 # locate or create node in graph
161 # if found, we're good with this one
162 next_node=graph.get_node(next_interface.uid())
164 # otherwise, let's move on with it
165 graph.add_node(next_interface.uid())
166 next_node=graph.get_node(next_interface.uid())
167 node2interface[next_node]=next_interface
168 to_scan.append(next_interface)
169 graph.add_edge(interface.uid(),next_interface.uid())
170 scanned.append(interface)
171 to_scan.remove(interface)
172 # we've scanned the whole graph, let's get the labels and shapes right
173 for node in graph.nodes():
174 interface=node2interface.get(node,None)
176 for (k,v) in interface.get_layout().items():
179 sfa_logger().error("MISSED interface with node %s"%node)
182 default_outfiles=['sfa.png','sfa.svg','sfa.dot']
185 sfa_logger_goes_to_console()
186 usage="%prog [options] url-entry-point(s)"
187 parser=OptionParser(usage=usage)
188 parser.add_option("-o","--output",action='append',dest='outfiles',default=[],
189 help="output filenames (cumulative) - defaults are %r"%default_outfiles)
190 (options,args)=parser.parse_args()
194 if not options.outfiles:
195 options.outfiles=default_outfiles
197 entries = [ Interface(entry) for entry in args ]
198 g=scanner.graph(entries)
199 sfa_logger().info("creating layout")
201 for outfile in options.outfiles:
202 sfa_logger().info("drawing in %s"%outfile)
204 sfa_logger().info("done")
206 if __name__ == '__main__':