3 # Sapan Bhatia <sapanb@cs.princeton.edu>
5 # Generates a WSDL for sfa
11 import xml.dom.minidom
16 from optparse import OptionParser
18 from sfa.storage.parameter import Parameter, Mixed
20 plc_ns="http://www.planet-lab.org/sfa"
22 class SoapError(Exception):
23 def __init__(self, value):
26 return repr(self.value)
40 def __init__(self, interface_options):
41 self.interface_options = interface_options
43 def interface_name (self):
44 if self.interface_options.aggregate and \
45 self.interface_options.slicemgr and \
46 self.interface_options.registry:
48 if self.interface_options.aggregate: return "aggregate"
49 elif self.interface_options.slicemgr: return "slicemgr"
50 elif self.interface_options.registry: return "registry"
51 elif self.interface_options.component: return "component"
52 else: return "unknown"
54 def filter_argname(self,argname):
55 if (not self.interface_options.lite or (argname!="cred")):
56 if (argname.find('(') != -1):
57 # The name has documentation in it :-/
58 brackright = argname.split('(')[1]
59 if (brackright.find(')') == -1):
60 raise Exception("Please fix the argument %s to be well-formed.\n"%argname)
61 inbrack = brackright.split(')')[0]
65 # def fold_complex_type_names(self,acc, arg):
67 # if (type(acc)==list):
74 # def fold_complex_type(self,acc, arg):
75 # name = self.name_complex_type(arg)
76 # self.complex_types[arg]=name
77 # if (type(acc)==list):
80 # p_i_b = self.name_complex_type(acc)
84 def name_complex_type(self,arg):
86 types_section = self.types.getElementsByTagName("xsd:schema")[0]
89 if (isinstance(arg, Mixed)):
90 # inner_types = reduce(self.fold_complex_type, arg)
91 # inner_names = reduce(self.fold_complex_type_names, arg)
92 inner_types = [ self.name_complex_type(x) for x in arg ]
93 inner_names = [ x.doc for x in arg ]
94 if (inner_types[-1]=="none"):
95 inner_types=inner_types[:-1]
101 type_name = "Type%d"%self.num_types
102 complex_type = types_section.appendChild(self.types.createElement("xsd:complexType"))
103 complex_type.setAttribute("name", type_name)
105 choice = complex_type.appendChild(self.types.createElement("xsd:choice"))
106 for (n,t) in zip(inner_names,inner_types):
107 element = choice.appendChild(self.types.createElement("element"))
108 n = self.filter_argname(n)
109 element.setAttribute("name", n)
110 element.setAttribute("type", "%s"%t)
111 element.setAttribute("minOccurs","%d"%min_args)
112 return "xsdl:%s"%type_name
113 elif (isinstance(arg, Parameter)):
114 return (self.name_simple_type(arg.type))
115 elif type(arg) in ( ListType , TupleType ):
116 inner_type = self.name_complex_type(arg[0])
117 self.num_types=self.num_types+1
118 type_name = "Type%d"%self.num_types
119 complex_type = types_section.appendChild(self.types.createElement("xsd:complexType"))
120 type_name = self.filter_argname(type_name)
121 complex_type.setAttribute("name", type_name)
122 complex_content = complex_type.appendChild(self.types.createElement("xsd:complexContent"))
123 restriction = complex_content.appendChild(self.types.createElement("xsd:restriction"))
124 restriction.setAttribute("base","soapenc:Array")
125 attribute = restriction.appendChild(self.types.createElement("xsd:attribute"))
126 attribute.setAttribute("ref","soapenc:arrayType")
127 attribute.setAttribute("wsdl:arrayType","%s[]"%inner_type)
129 return "xsdl:%s"%type_name
131 elif type(arg) == DictType or arg == DictType or (inspect.isclass(arg) and issubclass(arg, dict)):
132 self.num_types=self.num_types+1
133 type_name = "Type%d"%self.num_types
134 complex_type = types_section.appendChild(self.types.createElement("xsd:complexType"))
135 type_name = self.filter_argname(type_name)
136 complex_type.setAttribute("name", type_name)
137 complex_content = complex_type.appendChild(self.types.createElement("xsd:sequence"))
140 inner_type = self.name_complex_type(arg.fields[k])
141 element=complex_content.appendChild(self.types.createElement("xsd:element"))
142 element.setAttribute("name",k)
143 element.setAttribute("type",inner_type)
145 return "xsdl:%s"%type_name
147 return (self.name_simple_type(arg))
149 def name_simple_type(self,arg_type):
150 # A Parameter is reported as an instance, even though it is serialized as a type <>
151 if type(arg_type) == InstanceType:
152 return (self.name_simple_type(arg_type.type))
155 if arg_type == DictType:
157 if arg_type in (ListType, TupleType):
158 return "xsd:arrayType"
159 elif arg_type == IntType or arg_type == LongType:
161 elif arg_type == bool:
163 elif arg_type == FloatType:
165 elif arg_type in StringTypes:
169 raise SoapError, "Cannot handle %s objects" % arg_type
171 def param_type(self, arg):
172 return (self.name_complex_type(arg))
174 def add_wsdl_ports_and_bindings (self):
175 for method in apistub.methods:
177 # Skip system. methods
178 if "system." in method:
181 function = apistub.callable(method) # Commented documentation
182 #lines = ["// " + line.strip() for line in function.__doc__.strip().split("\n")]
183 #print "\n".join(lines)
187 in_el = self.wsdl.lastChild.appendChild(self.wsdl.createElement("message"))
188 in_el.setAttribute("name", method + "_in")
190 for service_name in function.interfaces:
191 if (self.services.has_key(service_name)):
192 if (not method in self.services[service_name]):
193 self.services[service_name].append(method)
195 self.services[service_name]=[method]
199 if (function.accepts):
200 (min_args, max_args, defaults) = function.args()
201 for (argname,argtype) in zip(max_args,function.accepts):
202 argname = self.filter_argname(argname)
203 arg_part = in_el.appendChild(self.wsdl.createElement("part"))
204 arg_part.setAttribute("name", argname)
205 arg_part.setAttribute("type", self.param_type(argtype))
208 return_type = function.returns
209 out_el = self.wsdl.lastChild.appendChild(self.wsdl.createElement("message"))
210 out_el.setAttribute("name", method + "_out")
211 ret_part = out_el.appendChild(self.wsdl.createElement("part"))
212 ret_part.setAttribute("name", "Result")
213 ret_part.setAttribute("type", self.param_type(return_type))
215 # Port connecting arguments with return type
217 port_el = self.wsdl.lastChild.appendChild(self.wsdl.createElement("portType"))
218 port_el.setAttribute("name", method + "_port")
220 op_el = port_el.appendChild(self.wsdl.createElement("operation"))
221 op_el.setAttribute("name", method)
222 inp_el=self.wsdl.createElement("input")
223 inp_el.setAttribute("message","tns:" + method + "_in")
224 inp_el.setAttribute("name",method+"_request")
225 op_el.appendChild(inp_el)
226 out_el = self.wsdl.createElement("output")
227 out_el.setAttribute("message","tns:" + method + "_out")
228 out_el.setAttribute("name",method+"_response")
229 op_el.appendChild(out_el)
233 bind_el = self.wsdl.lastChild.appendChild(self.wsdl.createElement("binding"))
234 bind_el.setAttribute("name", method + "_binding")
235 bind_el.setAttribute("type", "tns:" + method + "_port")
237 soap_bind = bind_el.appendChild(self.wsdl.createElement("soap:binding"))
238 soap_bind.setAttribute("style", "rpc")
239 soap_bind.setAttribute("transport","http://schemas.xmlsoap.org/soap/http")
242 wsdl_op = bind_el.appendChild(self.wsdl.createElement("operation"))
243 wsdl_op.setAttribute("name", method)
244 wsdl_op.appendChild(self.wsdl.createElement("soap:operation")).setAttribute("soapAction",
248 wsdl_input = wsdl_op.appendChild(self.wsdl.createElement("input"))
249 input_soap_body = wsdl_input.appendChild(self.wsdl.createElement("soap:body"))
250 input_soap_body.setAttribute("use", "encoded")
251 input_soap_body.setAttribute("namespace", "urn:" + method)
252 input_soap_body.setAttribute("encodingStyle","http://schemas.xmlsoap.org/soap/encoding/")
255 wsdl_output = wsdl_op.appendChild(self.wsdl.createElement("output"))
256 output_soap_body = wsdl_output.appendChild(self.wsdl.createElement("soap:body"))
257 output_soap_body.setAttribute("use", "encoded")
258 output_soap_body.setAttribute("namespace", "urn:" + method)
259 output_soap_body.setAttribute("encodingStyle","http://schemas.xmlsoap.org/soap/encoding/")
262 def add_wsdl_services(self):
263 for service in self.services.keys():
264 if (getattr(self.interface_options,service)):
265 service_el = self.wsdl.lastChild.appendChild(self.wsdl.createElement("service"))
266 service_el.setAttribute("name", service)
268 for method in self.services[service]:
270 servport_el = service_el.appendChild(self.wsdl.createElement("port"))
271 servport_el.setAttribute("name", name + "_port")
272 servport_el.setAttribute("binding", "tns:" + name + "_binding")
274 soapaddress = servport_el.appendChild(self.wsdl.createElement("soap:address"))
275 soapaddress.setAttribute("location", "%s/%s" % (plc_ns,service))
278 def compute_wsdl_definitions(self):
279 wsdl_text_header = """
280 <?xml-stylesheet type="text/xsl" href="wsdl2html.xsl"?>
283 targetNamespace="%s/sfa.wsdl"
284 xmlns="http://schemas.xmlsoap.org/wsdl/"
285 xmlns:xsd="http://www.w3.org/2001/XMLSchema"
286 xmlns:xsdl="%s/schema"
287 xmlns:tns="%s/sfa.wsdl"
288 xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
289 xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
290 xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"/>
291 """ % (self.interface_name(),plc_ns,plc_ns,plc_ns)
293 self.wsdl = xml.dom.minidom.parseString(wsdl_text_header)
296 def compute_wsdl_definitions_and_types(self):
297 wsdl_text_header = """
298 <?xml-stylesheet type="text/xsl" href="wsdl2html.xsl"?>
301 targetNamespace="%s/sfa.wsdl"
302 xmlns="http://schemas.xmlsoap.org/wsdl/"
303 xmlns:xsd="http://www.w3.org/2001/XMLSchema"
304 xmlns:xsdl="%s/schema"
305 xmlns:tns="%s/sfa.wsdl"
306 xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
307 xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
308 xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
310 <xsd:schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="%s/schema"/>
312 </wsdl:definitions> """ % (self.interface_name(),plc_ns, plc_ns, plc_ns, plc_ns)
313 self.types = xml.dom.minidom.parseString(wsdl_text_header)
316 def add_wsdl_types(self):
317 wsdl_types = self.wsdl.importNode(self.types.getElementsByTagName("types")[0], True)
318 self.wsdl.lastChild.appendChild(wsdl_types)
320 def generate_wsdl(self):
321 self.compute_wsdl_definitions_and_types()
322 self.compute_wsdl_definitions()
323 self.add_wsdl_ports_and_bindings()
324 self.add_wsdl_types()
325 self.add_wsdl_services()
327 def pretty_print(self):
329 print xml.dom.minidom.Document.toprettyxml(self.wsdl)
331 raise Exception("Empty WSDL")
334 parser = OptionParser()
335 parser.add_option("-r", "--registry", dest="registry", action="store_true",
336 help="Generate registry.wsdl", metavar="FILE")
337 parser.add_option("-s", "--slice-manager",
338 action="store_true", dest="slicemgr",
339 help="Generate sm.wsdl")
340 parser.add_option("-a", "--aggregate", action="store_true", dest="aggregate",
341 help="Generate am.wsdl")
342 parser.add_option("-c", "--component", action="store_true", dest="component",
343 help="Generate cm.wsdl")
344 parser.add_option("-g", "--geni-aggregate", action="store_true", dest="geni_am",
345 help="Generate gm.wsdl")
346 parser.add_option("-l", "--lite", action="store_true", dest="lite",
347 help="Generate LITE version of the interface, in which calls exclude credentials")
348 (interface_options, args) = parser.parse_args()
350 gen = WSDLGen(interface_options)
355 if __name__ == "__main__":