3 # Sapan Bhatia <sapanb@cs.princeton.edu>
5 # Generates a WSDL for sfa
11 import xml.dom.minidom
17 from optparse import OptionParser
19 from sfa.util.parameter import Parameter,Mixed
23 class SoapError(Exception):
24 def __init__(self, value):
27 return repr(self.value)
41 def __init__(self, interface_options):
42 self.interface_options = interface_options
44 def filter_argname(self,argname):
45 if (not self.interface_options.lite or (argname!="cred")):
46 if (argname.find('(') != -1):
47 # The name has documentation in it :-/
48 brackright = argname.split('(')[1]
49 if (brackright.find(')') == -1):
50 raise Exception("Please fix the argument %s to be well-formed.\n"%argname)
51 inbrack = brackright.split(')')[0]
55 def fold_complex_type_names(self,acc, arg):
64 def fold_complex_type(self,acc, arg):
65 name = self.name_complex_type(arg)
66 self.complex_types[arg]=name
70 p_i_b = self.name_complex_type(acc)
74 def name_complex_type(self,arg):
76 types_section = self.types.getElementsByTagName("xsd:schema")[0]
79 if (isinstance(arg, Mixed)):
80 inner_types = reduce(self.fold_complex_type, arg)
81 inner_names = reduce(self.fold_complex_type_names, arg)
82 if (inner_types[-1]=="none"):
83 inner_types=inner_types[:-1]
88 self.num_types=self.num_types+1
89 type_name = "Type%d"%self.num_types
90 complex_type = types_section.appendChild(self.types.createElement("xsd:complexType"))
91 complex_type.setAttribute("name", type_name)
93 choice = complex_type.appendChild(self.types.createElement("xsd:choice"))
94 for n,t in zip(inner_names,inner_types):
95 element = choice.appendChild(self.types.createElement("element"))
96 n = self.filter_argname(n)
97 element.setAttribute("name", n)
98 element.setAttribute("type", "%s"%t)
99 element.setAttribute("minOccurs","%d"%min_args)
100 return "xsdl:%s"%type_name
101 elif (isinstance(arg, Parameter)):
102 return (self.name_simple_type(arg.type))
103 elif type(arg) == ListType or type(arg) == TupleType:
104 inner_type = self.name_complex_type(arg[0])
105 self.num_types=self.num_types+1
106 type_name = "Type%d"%self.num_types
107 complex_type = types_section.appendChild(self.types.createElement("xsd:complexType"))
108 type_name = self.filter_argname(type_name)
109 complex_type.setAttribute("name", type_name)
110 complex_content = complex_type.appendChild(self.types.createElement("xsd:complexContent"))
111 restriction = complex_content.appendChild(self.types.createElement("xsd:restriction"))
112 restriction.setAttribute("base","soapenc:Array")
113 attribute = restriction.appendChild(self.types.createElement("xsd:attribute"))
114 attribute.setAttribute("ref","soapenc:arrayType")
115 attribute.setAttribute("wsdl:arrayType","%s[]"%inner_type)
117 return "xsdl:%s"%type_name
119 elif type(arg) == DictType or arg == DictType or (inspect.isclass(arg) and issubclass(arg, dict)):
120 self.num_types=self.num_types+1
121 type_name = "Type%d"%self.num_types
122 complex_type = types_section.appendChild(self.types.createElement("xsd:complexType"))
123 type_name = self.filter_argname(type_name)
124 complex_type.setAttribute("name", type_name)
125 complex_content = complex_type.appendChild(self.types.createElement("xsd:sequence"))
128 inner_type = self.name_complex_type(arg.fields[k])
129 element=complex_content.appendChild(self.types.createElement("xsd:element"))
130 element.setAttribute("name",k)
131 element.setAttribute("type",inner_type)
133 return "xsdl:%s"%type_name
135 return (self.name_simple_type(arg))
137 def name_simple_type(self,arg_type):
138 # A Parameter is reported as an instance, even though it is serialized as a type <>
139 if type(arg_type) == InstanceType:
140 return (self.name_simple_type(arg_type.type))
143 if arg_type == DictType:
145 if arg_type in (ListType, TupleType):
146 return "xsd:arrayType"
147 elif arg_type == IntType or arg_type == LongType:
149 elif arg_type == bool:
151 elif arg_type == FloatType:
153 elif arg_type in StringTypes:
157 raise SoapError, "Cannot handle %s objects" % arg_type
159 def param_type(self, arg):
160 return (self.name_complex_type(arg))
162 def add_wsdl_ports_and_bindings (self):
163 for method in apistub.methods:
165 # Skip system. methods
166 if "system." in method:
169 function = apistub.callable(method) # Commented documentation
170 #lines = ["// " + line.strip() for line in function.__doc__.strip().split("\n")]
171 #print "\n".join(lines)
175 in_el = self.wsdl.firstChild.appendChild(self.wsdl.createElement("message"))
176 in_el.setAttribute("name", method + "_in")
178 for service_name in function.interfaces:
179 if (self.services.has_key(service_name)):
180 if (not method in self.services[service_name]):
181 self.services[service_name].append(method)
183 self.services[service_name]=[method]
187 if (function.accepts):
188 (min_args, max_args, defaults) = function.args()
189 for (argname,argtype) in zip(max_args,function.accepts):
190 argname = self.filter_argname(argname)
191 arg_part = in_el.appendChild(self.wsdl.createElement("part"))
192 arg_part.setAttribute("name", argname)
193 arg_part.setAttribute("type", self.param_type(argtype))
196 return_type = function.returns
197 out_el = self.wsdl.firstChild.appendChild(self.wsdl.createElement("message"))
198 out_el.setAttribute("name", method + "_out")
199 ret_part = out_el.appendChild(self.wsdl.createElement("part"))
200 ret_part.setAttribute("name", "Result")
201 ret_part.setAttribute("type", self.param_type(return_type))
203 # Port connecting arguments with return type
205 port_el = self.wsdl.firstChild.appendChild(self.wsdl.createElement("portType"))
206 port_el.setAttribute("name", method + "_port")
208 op_el = port_el.appendChild(self.wsdl.createElement("operation"))
209 op_el.setAttribute("name", method)
210 inp_el=self.wsdl.createElement("input")
211 inp_el.setAttribute("message","tns:" + method + "_in")
212 inp_el.setAttribute("name",method+"_request")
213 op_el.appendChild(inp_el)
214 out_el = self.wsdl.createElement("output")
215 out_el.setAttribute("message","tns:" + method + "_out")
216 out_el.setAttribute("name",method+"_response")
217 op_el.appendChild(out_el)
221 bind_el = self.wsdl.firstChild.appendChild(self.wsdl.createElement("binding"))
222 bind_el.setAttribute("name", method + "_binding")
223 bind_el.setAttribute("type", "tns:" + method + "_port")
225 soap_bind = bind_el.appendChild(self.wsdl.createElement("soap:binding"))
226 soap_bind.setAttribute("style", "rpc")
227 soap_bind.setAttribute("transport","http://schemas.xmlsoap.org/soap/http")
230 wsdl_op = bind_el.appendChild(self.wsdl.createElement("operation"))
231 wsdl_op.setAttribute("name", method)
232 wsdl_op.appendChild(self.wsdl.createElement("soap:operation")).setAttribute("soapAction",
236 wsdl_input = wsdl_op.appendChild(self.wsdl.createElement("input"))
237 input_soap_body = wsdl_input.appendChild(self.wsdl.createElement("soap:body"))
238 input_soap_body.setAttribute("use", "encoded")
239 input_soap_body.setAttribute("namespace", "urn:" + method)
240 input_soap_body.setAttribute("encodingStyle","http://schemas.xmlsoap.org/soap/encoding/")
243 wsdl_output = wsdl_op.appendChild(self.wsdl.createElement("output"))
244 output_soap_body = wsdl_output.appendChild(self.wsdl.createElement("soap:body"))
245 output_soap_body.setAttribute("use", "encoded")
246 output_soap_body.setAttribute("namespace", "urn:" + method)
247 output_soap_body.setAttribute("encodingStyle","http://schemas.xmlsoap.org/soap/encoding/")
250 def add_wsdl_services(self):
251 for service in self.services.keys():
252 if (getattr(self.interface_options,service)):
253 service_el = self.wsdl.firstChild.appendChild(self.wsdl.createElement("service"))
254 service_el.setAttribute("name", service)
256 for method in self.services[service]:
258 servport_el = service_el.appendChild(self.wsdl.createElement("port"))
259 servport_el.setAttribute("name", name + "_port")
260 servport_el.setAttribute("binding", "tns:" + name + "_binding")
262 soapaddress = servport_el.appendChild(self.wsdl.createElement("soap:address"))
263 soapaddress.setAttribute("location", "%s/%s" % (globals.plc_ns,service))
266 def compute_wsdl_definitions(self):
267 wsdl_text_header = """
269 name="sfa_autogenerated"
270 targetNamespace="%s/2009/07/sfa.wsdl"
271 xmlns="http://schemas.xmlsoap.org/wsdl/"
272 xmlns:xsd="http://www.w3.org/2001/XMLSchema"
273 xmlns:xsdl="%s/2009/07/schema"
274 xmlns:tns="%s/2009/07/sfa.wsdl"
275 xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
276 xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
277 xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"/>
278 """ % (globals.plc_ns,globals.plc_ns,globals.plc_ns)
280 self.wsdl = xml.dom.minidom.parseString(wsdl_text_header)
283 def compute_wsdl_definitions_and_types(self):
284 wsdl_text_header = """
286 name="sfa_autogenerated"
287 targetNamespace="%s/2009/07/sfa.wsdl"
288 xmlns="http://schemas.xmlsoap.org/wsdl/"
289 xmlns:xsd="http://www.w3.org/2001/XMLSchema"
290 xmlns:xsdl="%s/2009/07/schema"
291 xmlns:tns="%s/2009/07/sfa.wsdl"
292 xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
293 xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
294 xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
296 <xsd:schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="%s/2009/07/schema"/>
298 </wsdl:definitions> """ % (globals.plc_ns, globals.plc_ns, globals.plc_ns, globals.plc_ns)
299 self.types = xml.dom.minidom.parseString(wsdl_text_header)
302 def add_wsdl_types(self):
303 wsdl_types = self.wsdl.importNode(self.types.getElementsByTagName("types")[0], True)
304 self.wsdl.firstChild.appendChild(wsdl_types)
306 def generate_wsdl(self):
307 self.compute_wsdl_definitions_and_types()
308 self.compute_wsdl_definitions()
309 self.add_wsdl_ports_and_bindings()
310 self.add_wsdl_types()
311 self.add_wsdl_services()
313 def pretty_print(self):
315 xml.dom.ext.PrettyPrint(self.wsdl)
317 raise Exception("Empty WSDL")
320 parser = OptionParser()
321 parser.add_option("-r", "--registry", dest="registry", action="store_true",
322 help="Generate registry.wsdl", metavar="FILE")
323 parser.add_option("-s", "--slice-manager",
324 action="store_true", dest="slicemgr",
325 help="Generate sm.wsdl")
326 parser.add_option("-a", "--aggregate", action="store_true", dest="aggregate",
327 help="Generate am.wsdl")
328 parser.add_option("-c", "--component", action="store_true", dest="component",
329 help="Generate cm.wsdl")
330 parser.add_option("-g", "--geni-aggregate", action="store_true", dest="geni_am",
331 help="Generate gm.wsdl")
332 parser.add_option("-l", "--lite", action="store_true", dest="lite",
333 help="Generate LITE version of the interface, in which calls exclude credentials")
334 (interface_options, args) = parser.parse_args()
336 gen = WSDLGen(interface_options)
341 if __name__ == "__main__":