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 from sfa.util.py23 import StringType
22 plc_ns="http://www.planet-lab.org/sfa"
24 class SoapError(Exception):
25 def __init__(self, value):
28 return repr(self.value)
42 def __init__(self, interface_options):
43 self.interface_options = interface_options
45 def interface_name (self):
46 if self.interface_options.aggregate and \
47 self.interface_options.slicemgr and \
48 self.interface_options.registry:
50 if self.interface_options.aggregate: return "aggregate"
51 elif self.interface_options.slicemgr: return "slicemgr"
52 elif self.interface_options.registry: return "registry"
53 elif self.interface_options.component: return "component"
54 else: return "unknown"
56 def filter_argname(self,argname):
57 if (not self.interface_options.lite or (argname!="cred")):
58 if (argname.find('(') != -1):
59 # The name has documentation in it :-/
60 brackright = argname.split('(')[1]
61 if (brackright.find(')') == -1):
62 raise Exception("Please fix the argument %s to be well-formed.\n"%argname)
63 inbrack = brackright.split(')')[0]
67 # def fold_complex_type_names(self,acc, arg):
69 # if (type(acc)==list):
76 # def fold_complex_type(self,acc, arg):
77 # name = self.name_complex_type(arg)
78 # self.complex_types[arg]=name
79 # if (type(acc)==list):
82 # p_i_b = self.name_complex_type(acc)
86 def name_complex_type(self,arg):
88 types_section = self.types.getElementsByTagName("xsd:schema")[0]
91 if (isinstance(arg, Mixed)):
92 # inner_types = reduce(self.fold_complex_type, arg)
93 # inner_names = reduce(self.fold_complex_type_names, arg)
94 inner_types = [ self.name_complex_type(x) for x in arg ]
95 inner_names = [ x.doc for x in arg ]
96 if (inner_types[-1]=="none"):
97 inner_types=inner_types[:-1]
103 type_name = "Type%d"%self.num_types
104 complex_type = types_section.appendChild(self.types.createElement("xsd:complexType"))
105 complex_type.setAttribute("name", type_name)
107 choice = complex_type.appendChild(self.types.createElement("xsd:choice"))
108 for (n,t) in zip(inner_names,inner_types):
109 element = choice.appendChild(self.types.createElement("element"))
110 n = self.filter_argname(n)
111 element.setAttribute("name", n)
112 element.setAttribute("type", "%s"%t)
113 element.setAttribute("minOccurs","%d"%min_args)
114 return "xsdl:%s"%type_name
115 elif (isinstance(arg, Parameter)):
116 return (self.name_simple_type(arg.type))
117 elif type(arg) in ( ListType , TupleType ):
118 inner_type = self.name_complex_type(arg[0])
119 self.num_types=self.num_types+1
120 type_name = "Type%d"%self.num_types
121 complex_type = types_section.appendChild(self.types.createElement("xsd:complexType"))
122 type_name = self.filter_argname(type_name)
123 complex_type.setAttribute("name", type_name)
124 complex_content = complex_type.appendChild(self.types.createElement("xsd:complexContent"))
125 restriction = complex_content.appendChild(self.types.createElement("xsd:restriction"))
126 restriction.setAttribute("base","soapenc:Array")
127 attribute = restriction.appendChild(self.types.createElement("xsd:attribute"))
128 attribute.setAttribute("ref","soapenc:arrayType")
129 attribute.setAttribute("wsdl:arrayType","%s[]"%inner_type)
131 return "xsdl:%s"%type_name
133 elif type(arg) == DictType or arg == DictType or (inspect.isclass(arg) and issubclass(arg, dict)):
134 self.num_types=self.num_types+1
135 type_name = "Type%d"%self.num_types
136 complex_type = types_section.appendChild(self.types.createElement("xsd:complexType"))
137 type_name = self.filter_argname(type_name)
138 complex_type.setAttribute("name", type_name)
139 complex_content = complex_type.appendChild(self.types.createElement("xsd:sequence"))
142 inner_type = self.name_complex_type(arg.fields[k])
143 element=complex_content.appendChild(self.types.createElement("xsd:element"))
144 element.setAttribute("name",k)
145 element.setAttribute("type",inner_type)
147 return "xsdl:%s"%type_name
149 return (self.name_simple_type(arg))
151 def name_simple_type(self,arg_type):
152 # A Parameter is reported as an instance, even though it is serialized as a type <>
153 if type(arg_type) == InstanceType:
154 return (self.name_simple_type(arg_type.type))
157 if arg_type == DictType:
159 if arg_type in (ListType, TupleType):
160 return "xsd:arrayType"
161 elif arg_type == IntType or arg_type == LongType:
163 elif arg_type == bool:
165 elif arg_type == FloatType:
167 elif issubclass(arg_type, StringType):
171 raise SoapError("Cannot handle %s objects" % arg_type)
173 def param_type(self, arg):
174 return (self.name_complex_type(arg))
176 def add_wsdl_ports_and_bindings (self):
177 for method in apistub.methods:
179 # Skip system. methods
180 if "system." in method:
183 function = apistub.callable(method) # Commented documentation
184 #lines = ["// " + line.strip() for line in function.__doc__.strip().split("\n")]
185 #print "\n".join(lines)
189 in_el = self.wsdl.lastChild.appendChild(self.wsdl.createElement("message"))
190 in_el.setAttribute("name", method + "_in")
192 for service_name in function.interfaces:
193 if (self.services.has_key(service_name)):
194 if (not method in self.services[service_name]):
195 self.services[service_name].append(method)
197 self.services[service_name]=[method]
201 if (function.accepts):
202 (min_args, max_args, defaults) = function.args()
203 for (argname,argtype) in zip(max_args,function.accepts):
204 argname = self.filter_argname(argname)
205 arg_part = in_el.appendChild(self.wsdl.createElement("part"))
206 arg_part.setAttribute("name", argname)
207 arg_part.setAttribute("type", self.param_type(argtype))
210 return_type = function.returns
211 out_el = self.wsdl.lastChild.appendChild(self.wsdl.createElement("message"))
212 out_el.setAttribute("name", method + "_out")
213 ret_part = out_el.appendChild(self.wsdl.createElement("part"))
214 ret_part.setAttribute("name", "Result")
215 ret_part.setAttribute("type", self.param_type(return_type))
217 # Port connecting arguments with return type
219 port_el = self.wsdl.lastChild.appendChild(self.wsdl.createElement("portType"))
220 port_el.setAttribute("name", method + "_port")
222 op_el = port_el.appendChild(self.wsdl.createElement("operation"))
223 op_el.setAttribute("name", method)
224 inp_el=self.wsdl.createElement("input")
225 inp_el.setAttribute("message","tns:" + method + "_in")
226 inp_el.setAttribute("name",method+"_request")
227 op_el.appendChild(inp_el)
228 out_el = self.wsdl.createElement("output")
229 out_el.setAttribute("message","tns:" + method + "_out")
230 out_el.setAttribute("name",method+"_response")
231 op_el.appendChild(out_el)
235 bind_el = self.wsdl.lastChild.appendChild(self.wsdl.createElement("binding"))
236 bind_el.setAttribute("name", method + "_binding")
237 bind_el.setAttribute("type", "tns:" + method + "_port")
239 soap_bind = bind_el.appendChild(self.wsdl.createElement("soap:binding"))
240 soap_bind.setAttribute("style", "rpc")
241 soap_bind.setAttribute("transport","http://schemas.xmlsoap.org/soap/http")
244 wsdl_op = bind_el.appendChild(self.wsdl.createElement("operation"))
245 wsdl_op.setAttribute("name", method)
246 wsdl_op.appendChild(self.wsdl.createElement("soap:operation")).setAttribute("soapAction",
250 wsdl_input = wsdl_op.appendChild(self.wsdl.createElement("input"))
251 input_soap_body = wsdl_input.appendChild(self.wsdl.createElement("soap:body"))
252 input_soap_body.setAttribute("use", "encoded")
253 input_soap_body.setAttribute("namespace", "urn:" + method)
254 input_soap_body.setAttribute("encodingStyle","http://schemas.xmlsoap.org/soap/encoding/")
257 wsdl_output = wsdl_op.appendChild(self.wsdl.createElement("output"))
258 output_soap_body = wsdl_output.appendChild(self.wsdl.createElement("soap:body"))
259 output_soap_body.setAttribute("use", "encoded")
260 output_soap_body.setAttribute("namespace", "urn:" + method)
261 output_soap_body.setAttribute("encodingStyle","http://schemas.xmlsoap.org/soap/encoding/")
264 def add_wsdl_services(self):
265 for service in self.services.keys():
266 if (getattr(self.interface_options,service)):
267 service_el = self.wsdl.lastChild.appendChild(self.wsdl.createElement("service"))
268 service_el.setAttribute("name", service)
270 for method in self.services[service]:
272 servport_el = service_el.appendChild(self.wsdl.createElement("port"))
273 servport_el.setAttribute("name", name + "_port")
274 servport_el.setAttribute("binding", "tns:" + name + "_binding")
276 soapaddress = servport_el.appendChild(self.wsdl.createElement("soap:address"))
277 soapaddress.setAttribute("location", "%s/%s" % (plc_ns,service))
280 def compute_wsdl_definitions(self):
281 wsdl_text_header = """
282 <?xml-stylesheet type="text/xsl" href="wsdl2html.xsl"?>
285 targetNamespace="%s/sfa.wsdl"
286 xmlns="http://schemas.xmlsoap.org/wsdl/"
287 xmlns:xsd="http://www.w3.org/2001/XMLSchema"
288 xmlns:xsdl="%s/schema"
289 xmlns:tns="%s/sfa.wsdl"
290 xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
291 xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
292 xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"/>
293 """ % (self.interface_name(),plc_ns,plc_ns,plc_ns)
295 self.wsdl = xml.dom.minidom.parseString(wsdl_text_header)
298 def compute_wsdl_definitions_and_types(self):
299 wsdl_text_header = """
300 <?xml-stylesheet type="text/xsl" href="wsdl2html.xsl"?>
303 targetNamespace="%s/sfa.wsdl"
304 xmlns="http://schemas.xmlsoap.org/wsdl/"
305 xmlns:xsd="http://www.w3.org/2001/XMLSchema"
306 xmlns:xsdl="%s/schema"
307 xmlns:tns="%s/sfa.wsdl"
308 xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
309 xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
310 xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
312 <xsd:schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="%s/schema"/>
314 </wsdl:definitions> """ % (self.interface_name(),plc_ns, plc_ns, plc_ns, plc_ns)
315 self.types = xml.dom.minidom.parseString(wsdl_text_header)
318 def add_wsdl_types(self):
319 wsdl_types = self.wsdl.importNode(self.types.getElementsByTagName("types")[0], True)
320 self.wsdl.lastChild.appendChild(wsdl_types)
322 def generate_wsdl(self):
323 self.compute_wsdl_definitions_and_types()
324 self.compute_wsdl_definitions()
325 self.add_wsdl_ports_and_bindings()
326 self.add_wsdl_types()
327 self.add_wsdl_services()
329 def pretty_print(self):
331 print xml.dom.minidom.Document.toprettyxml(self.wsdl)
333 raise Exception("Empty WSDL")
336 parser = OptionParser()
337 parser.add_option("-r", "--registry", dest="registry", action="store_true",
338 help="Generate registry.wsdl", metavar="FILE")
339 parser.add_option("-s", "--slice-manager",
340 action="store_true", dest="slicemgr",
341 help="Generate sm.wsdl")
342 parser.add_option("-a", "--aggregate", action="store_true", dest="aggregate",
343 help="Generate am.wsdl")
344 parser.add_option("-c", "--component", action="store_true", dest="component",
345 help="Generate cm.wsdl")
346 parser.add_option("-g", "--geni-aggregate", action="store_true", dest="geni_am",
347 help="Generate gm.wsdl")
348 parser.add_option("-l", "--lite", action="store_true", dest="lite",
349 help="Generate LITE version of the interface, in which calls exclude credentials")
350 (interface_options, args) = parser.parse_args()
352 gen = WSDLGen(interface_options)
357 if __name__ == "__main__":