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 interface_name (self):
45 if self.interface_options.aggregate and \
46 self.interface_options.slicemgr and \
47 self.interface_options.registry:
49 if self.interface_options.aggregate: return "aggregate"
50 elif self.interface_options.slicemgr: return "slicemgr"
51 elif self.interface_options.registry: return "registry"
52 elif self.interface_options.component: return "component"
53 else: return "unknown"
55 def filter_argname(self,argname):
56 if (not self.interface_options.lite or (argname!="cred")):
57 if (argname.find('(') != -1):
58 # The name has documentation in it :-/
59 brackright = argname.split('(')[1]
60 if (brackright.find(')') == -1):
61 raise Exception("Please fix the argument %s to be well-formed.\n"%argname)
62 inbrack = brackright.split(')')[0]
66 # def fold_complex_type_names(self,acc, arg):
68 # if (type(acc)==list):
75 # def fold_complex_type(self,acc, arg):
76 # name = self.name_complex_type(arg)
77 # self.complex_types[arg]=name
78 # if (type(acc)==list):
81 # p_i_b = self.name_complex_type(acc)
85 def name_complex_type(self,arg):
87 types_section = self.types.getElementsByTagName("xsd:schema")[0]
90 if (isinstance(arg, Mixed)):
91 # inner_types = reduce(self.fold_complex_type, arg)
92 # inner_names = reduce(self.fold_complex_type_names, arg)
93 inner_types = [ self.name_complex_type(x) for x in arg ]
94 inner_names = [ x.doc for x in arg ]
95 if (inner_types[-1]=="none"):
96 inner_types=inner_types[:-1]
102 type_name = "Type%d"%self.num_types
103 complex_type = types_section.appendChild(self.types.createElement("xsd:complexType"))
104 complex_type.setAttribute("name", type_name)
106 choice = complex_type.appendChild(self.types.createElement("xsd:choice"))
107 for (n,t) in zip(inner_names,inner_types):
108 element = choice.appendChild(self.types.createElement("element"))
109 n = self.filter_argname(n)
110 element.setAttribute("name", n)
111 element.setAttribute("type", "%s"%t)
112 element.setAttribute("minOccurs","%d"%min_args)
113 return "xsdl:%s"%type_name
114 elif (isinstance(arg, Parameter)):
115 return (self.name_simple_type(arg.type))
116 elif type(arg) in ( ListType , TupleType ):
117 inner_type = self.name_complex_type(arg[0])
118 self.num_types=self.num_types+1
119 type_name = "Type%d"%self.num_types
120 complex_type = types_section.appendChild(self.types.createElement("xsd:complexType"))
121 type_name = self.filter_argname(type_name)
122 complex_type.setAttribute("name", type_name)
123 complex_content = complex_type.appendChild(self.types.createElement("xsd:complexContent"))
124 restriction = complex_content.appendChild(self.types.createElement("xsd:restriction"))
125 restriction.setAttribute("base","soapenc:Array")
126 attribute = restriction.appendChild(self.types.createElement("xsd:attribute"))
127 attribute.setAttribute("ref","soapenc:arrayType")
128 attribute.setAttribute("wsdl:arrayType","%s[]"%inner_type)
130 return "xsdl:%s"%type_name
132 elif type(arg) == DictType or arg == DictType or (inspect.isclass(arg) and issubclass(arg, dict)):
133 self.num_types=self.num_types+1
134 type_name = "Type%d"%self.num_types
135 complex_type = types_section.appendChild(self.types.createElement("xsd:complexType"))
136 type_name = self.filter_argname(type_name)
137 complex_type.setAttribute("name", type_name)
138 complex_content = complex_type.appendChild(self.types.createElement("xsd:sequence"))
141 inner_type = self.name_complex_type(arg.fields[k])
142 element=complex_content.appendChild(self.types.createElement("xsd:element"))
143 element.setAttribute("name",k)
144 element.setAttribute("type",inner_type)
146 return "xsdl:%s"%type_name
148 return (self.name_simple_type(arg))
150 def name_simple_type(self,arg_type):
151 # A Parameter is reported as an instance, even though it is serialized as a type <>
152 if type(arg_type) == InstanceType:
153 return (self.name_simple_type(arg_type.type))
156 if arg_type == DictType:
158 if arg_type in (ListType, TupleType):
159 return "xsd:arrayType"
160 elif arg_type == IntType or arg_type == LongType:
162 elif arg_type == bool:
164 elif arg_type == FloatType:
166 elif arg_type in StringTypes:
170 raise SoapError, "Cannot handle %s objects" % arg_type
172 def param_type(self, arg):
173 return (self.name_complex_type(arg))
175 def add_wsdl_ports_and_bindings (self):
176 for method in apistub.methods:
178 # Skip system. methods
179 if "system." in method:
182 function = apistub.callable(method) # Commented documentation
183 #lines = ["// " + line.strip() for line in function.__doc__.strip().split("\n")]
184 #print "\n".join(lines)
188 in_el = self.wsdl.lastChild.appendChild(self.wsdl.createElement("message"))
189 in_el.setAttribute("name", method + "_in")
191 for service_name in function.interfaces:
192 if (self.services.has_key(service_name)):
193 if (not method in self.services[service_name]):
194 self.services[service_name].append(method)
196 self.services[service_name]=[method]
200 if (function.accepts):
201 (min_args, max_args, defaults) = function.args()
202 for (argname,argtype) in zip(max_args,function.accepts):
203 argname = self.filter_argname(argname)
204 arg_part = in_el.appendChild(self.wsdl.createElement("part"))
205 arg_part.setAttribute("name", argname)
206 arg_part.setAttribute("type", self.param_type(argtype))
209 return_type = function.returns
210 out_el = self.wsdl.lastChild.appendChild(self.wsdl.createElement("message"))
211 out_el.setAttribute("name", method + "_out")
212 ret_part = out_el.appendChild(self.wsdl.createElement("part"))
213 ret_part.setAttribute("name", "Result")
214 ret_part.setAttribute("type", self.param_type(return_type))
216 # Port connecting arguments with return type
218 port_el = self.wsdl.lastChild.appendChild(self.wsdl.createElement("portType"))
219 port_el.setAttribute("name", method + "_port")
221 op_el = port_el.appendChild(self.wsdl.createElement("operation"))
222 op_el.setAttribute("name", method)
223 inp_el=self.wsdl.createElement("input")
224 inp_el.setAttribute("message","tns:" + method + "_in")
225 inp_el.setAttribute("name",method+"_request")
226 op_el.appendChild(inp_el)
227 out_el = self.wsdl.createElement("output")
228 out_el.setAttribute("message","tns:" + method + "_out")
229 out_el.setAttribute("name",method+"_response")
230 op_el.appendChild(out_el)
234 bind_el = self.wsdl.lastChild.appendChild(self.wsdl.createElement("binding"))
235 bind_el.setAttribute("name", method + "_binding")
236 bind_el.setAttribute("type", "tns:" + method + "_port")
238 soap_bind = bind_el.appendChild(self.wsdl.createElement("soap:binding"))
239 soap_bind.setAttribute("style", "rpc")
240 soap_bind.setAttribute("transport","http://schemas.xmlsoap.org/soap/http")
243 wsdl_op = bind_el.appendChild(self.wsdl.createElement("operation"))
244 wsdl_op.setAttribute("name", method)
245 wsdl_op.appendChild(self.wsdl.createElement("soap:operation")).setAttribute("soapAction",
249 wsdl_input = wsdl_op.appendChild(self.wsdl.createElement("input"))
250 input_soap_body = wsdl_input.appendChild(self.wsdl.createElement("soap:body"))
251 input_soap_body.setAttribute("use", "encoded")
252 input_soap_body.setAttribute("namespace", "urn:" + method)
253 input_soap_body.setAttribute("encodingStyle","http://schemas.xmlsoap.org/soap/encoding/")
256 wsdl_output = wsdl_op.appendChild(self.wsdl.createElement("output"))
257 output_soap_body = wsdl_output.appendChild(self.wsdl.createElement("soap:body"))
258 output_soap_body.setAttribute("use", "encoded")
259 output_soap_body.setAttribute("namespace", "urn:" + method)
260 output_soap_body.setAttribute("encodingStyle","http://schemas.xmlsoap.org/soap/encoding/")
263 def add_wsdl_services(self):
264 for service in self.services.keys():
265 if (getattr(self.interface_options,service)):
266 service_el = self.wsdl.lastChild.appendChild(self.wsdl.createElement("service"))
267 service_el.setAttribute("name", service)
269 for method in self.services[service]:
271 servport_el = service_el.appendChild(self.wsdl.createElement("port"))
272 servport_el.setAttribute("name", name + "_port")
273 servport_el.setAttribute("binding", "tns:" + name + "_binding")
275 soapaddress = servport_el.appendChild(self.wsdl.createElement("soap:address"))
276 soapaddress.setAttribute("location", "%s/%s" % (globals.plc_ns,service))
279 def compute_wsdl_definitions(self):
280 wsdl_text_header = """
281 <?xml-stylesheet type="text/xsl" href="wsdl2html.xsl"?>
284 targetNamespace="%s/sfa.wsdl"
285 xmlns="http://schemas.xmlsoap.org/wsdl/"
286 xmlns:xsd="http://www.w3.org/2001/XMLSchema"
287 xmlns:xsdl="%s/schema"
288 xmlns:tns="%s/sfa.wsdl"
289 xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
290 xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
291 xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"/>
292 """ % (self.interface_name(),globals.plc_ns,globals.plc_ns,globals.plc_ns)
294 self.wsdl = xml.dom.minidom.parseString(wsdl_text_header)
297 def compute_wsdl_definitions_and_types(self):
298 wsdl_text_header = """
299 <?xml-stylesheet type="text/xsl" href="wsdl2html.xsl"?>
302 targetNamespace="%s/sfa.wsdl"
303 xmlns="http://schemas.xmlsoap.org/wsdl/"
304 xmlns:xsd="http://www.w3.org/2001/XMLSchema"
305 xmlns:xsdl="%s/schema"
306 xmlns:tns="%s/sfa.wsdl"
307 xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
308 xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
309 xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
311 <xsd:schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="%s/schema"/>
313 </wsdl:definitions> """ % (self.interface_name(),globals.plc_ns, globals.plc_ns, globals.plc_ns, globals.plc_ns)
314 self.types = xml.dom.minidom.parseString(wsdl_text_header)
317 def add_wsdl_types(self):
318 wsdl_types = self.wsdl.importNode(self.types.getElementsByTagName("types")[0], True)
319 self.wsdl.lastChild.appendChild(wsdl_types)
321 def generate_wsdl(self):
322 self.compute_wsdl_definitions_and_types()
323 self.compute_wsdl_definitions()
324 self.add_wsdl_ports_and_bindings()
325 self.add_wsdl_types()
326 self.add_wsdl_services()
328 def pretty_print(self):
330 xml.dom.ext.PrettyPrint(self.wsdl)
332 raise Exception("Empty WSDL")
335 parser = OptionParser()
336 parser.add_option("-r", "--registry", dest="registry", action="store_true",
337 help="Generate registry.wsdl", metavar="FILE")
338 parser.add_option("-s", "--slice-manager",
339 action="store_true", dest="slicemgr",
340 help="Generate sm.wsdl")
341 parser.add_option("-a", "--aggregate", action="store_true", dest="aggregate",
342 help="Generate am.wsdl")
343 parser.add_option("-c", "--component", action="store_true", dest="component",
344 help="Generate cm.wsdl")
345 parser.add_option("-g", "--geni-aggregate", action="store_true", dest="geni_am",
346 help="Generate gm.wsdl")
347 parser.add_option("-l", "--lite", action="store_true", dest="lite",
348 help="Generate LITE version of the interface, in which calls exclude credentials")
349 (interface_options, args) = parser.parse_args()
351 gen = WSDLGen(interface_options)
356 if __name__ == "__main__":