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: return "aggregate"
46 elif self.interface_options.slicemgr: return "slicemgr"
47 elif self.interface_options.registry: return "registry"
48 elif self.interface_options.component: return "component"
49 else: return "unknown"
51 def filter_argname(self,argname):
52 if (not self.interface_options.lite or (argname!="cred")):
53 if (argname.find('(') != -1):
54 # The name has documentation in it :-/
55 brackright = argname.split('(')[1]
56 if (brackright.find(')') == -1):
57 raise Exception("Please fix the argument %s to be well-formed.\n"%argname)
58 inbrack = brackright.split(')')[0]
62 def fold_complex_type_names(self,acc, arg):
71 def fold_complex_type(self,acc, arg):
72 name = self.name_complex_type(arg)
73 self.complex_types[arg]=name
77 p_i_b = self.name_complex_type(acc)
81 def name_complex_type(self,arg):
83 types_section = self.types.getElementsByTagName("xsd:schema")[0]
86 if (isinstance(arg, Mixed)):
87 inner_types = reduce(self.fold_complex_type, arg)
88 inner_names = reduce(self.fold_complex_type_names, arg)
89 if (inner_types[-1]=="none"):
90 inner_types=inner_types[:-1]
95 self.num_types=self.num_types+1
96 type_name = "Type%d"%self.num_types
97 complex_type = types_section.appendChild(self.types.createElement("xsd:complexType"))
98 complex_type.setAttribute("name", type_name)
100 choice = complex_type.appendChild(self.types.createElement("xsd:choice"))
101 for n,t in zip(inner_names,inner_types):
102 element = choice.appendChild(self.types.createElement("element"))
103 n = self.filter_argname(n)
104 element.setAttribute("name", n)
105 element.setAttribute("type", "%s"%t)
106 element.setAttribute("minOccurs","%d"%min_args)
107 return "xsdl:%s"%type_name
108 elif (isinstance(arg, Parameter)):
109 return (self.name_simple_type(arg.type))
110 elif type(arg) == ListType or type(arg) == TupleType:
111 inner_type = self.name_complex_type(arg[0])
112 self.num_types=self.num_types+1
113 type_name = "Type%d"%self.num_types
114 complex_type = types_section.appendChild(self.types.createElement("xsd:complexType"))
115 type_name = self.filter_argname(type_name)
116 complex_type.setAttribute("name", type_name)
117 complex_content = complex_type.appendChild(self.types.createElement("xsd:complexContent"))
118 restriction = complex_content.appendChild(self.types.createElement("xsd:restriction"))
119 restriction.setAttribute("base","soapenc:Array")
120 attribute = restriction.appendChild(self.types.createElement("xsd:attribute"))
121 attribute.setAttribute("ref","soapenc:arrayType")
122 attribute.setAttribute("wsdl:arrayType","%s[]"%inner_type)
124 return "xsdl:%s"%type_name
126 elif type(arg) == DictType or arg == DictType or (inspect.isclass(arg) and issubclass(arg, dict)):
127 self.num_types=self.num_types+1
128 type_name = "Type%d"%self.num_types
129 complex_type = types_section.appendChild(self.types.createElement("xsd:complexType"))
130 type_name = self.filter_argname(type_name)
131 complex_type.setAttribute("name", type_name)
132 complex_content = complex_type.appendChild(self.types.createElement("xsd:sequence"))
135 inner_type = self.name_complex_type(arg.fields[k])
136 element=complex_content.appendChild(self.types.createElement("xsd:element"))
137 element.setAttribute("name",k)
138 element.setAttribute("type",inner_type)
140 return "xsdl:%s"%type_name
142 return (self.name_simple_type(arg))
144 def name_simple_type(self,arg_type):
145 # A Parameter is reported as an instance, even though it is serialized as a type <>
146 if type(arg_type) == InstanceType:
147 return (self.name_simple_type(arg_type.type))
150 if arg_type == DictType:
152 if arg_type in (ListType, TupleType):
153 return "xsd:arrayType"
154 elif arg_type == IntType or arg_type == LongType:
156 elif arg_type == bool:
158 elif arg_type == FloatType:
160 elif arg_type in StringTypes:
164 raise SoapError, "Cannot handle %s objects" % arg_type
166 def param_type(self, arg):
167 return (self.name_complex_type(arg))
169 def add_wsdl_ports_and_bindings (self):
170 for method in apistub.methods:
172 # Skip system. methods
173 if "system." in method:
176 function = apistub.callable(method) # Commented documentation
177 #lines = ["// " + line.strip() for line in function.__doc__.strip().split("\n")]
178 #print "\n".join(lines)
182 in_el = self.wsdl.lastChild.appendChild(self.wsdl.createElement("message"))
183 in_el.setAttribute("name", method + "_in")
185 for service_name in function.interfaces:
186 if (self.services.has_key(service_name)):
187 if (not method in self.services[service_name]):
188 self.services[service_name].append(method)
190 self.services[service_name]=[method]
194 if (function.accepts):
195 (min_args, max_args, defaults) = function.args()
196 for (argname,argtype) in zip(max_args,function.accepts):
197 argname = self.filter_argname(argname)
198 arg_part = in_el.appendChild(self.wsdl.createElement("part"))
199 arg_part.setAttribute("name", argname)
200 arg_part.setAttribute("type", self.param_type(argtype))
203 return_type = function.returns
204 out_el = self.wsdl.lastChild.appendChild(self.wsdl.createElement("message"))
205 out_el.setAttribute("name", method + "_out")
206 ret_part = out_el.appendChild(self.wsdl.createElement("part"))
207 ret_part.setAttribute("name", "Result")
208 ret_part.setAttribute("type", self.param_type(return_type))
210 # Port connecting arguments with return type
212 port_el = self.wsdl.lastChild.appendChild(self.wsdl.createElement("portType"))
213 port_el.setAttribute("name", method + "_port")
215 op_el = port_el.appendChild(self.wsdl.createElement("operation"))
216 op_el.setAttribute("name", method)
217 inp_el=self.wsdl.createElement("input")
218 inp_el.setAttribute("message","tns:" + method + "_in")
219 inp_el.setAttribute("name",method+"_request")
220 op_el.appendChild(inp_el)
221 out_el = self.wsdl.createElement("output")
222 out_el.setAttribute("message","tns:" + method + "_out")
223 out_el.setAttribute("name",method+"_response")
224 op_el.appendChild(out_el)
228 bind_el = self.wsdl.lastChild.appendChild(self.wsdl.createElement("binding"))
229 bind_el.setAttribute("name", method + "_binding")
230 bind_el.setAttribute("type", "tns:" + method + "_port")
232 soap_bind = bind_el.appendChild(self.wsdl.createElement("soap:binding"))
233 soap_bind.setAttribute("style", "rpc")
234 soap_bind.setAttribute("transport","http://schemas.xmlsoap.org/soap/http")
237 wsdl_op = bind_el.appendChild(self.wsdl.createElement("operation"))
238 wsdl_op.setAttribute("name", method)
239 wsdl_op.appendChild(self.wsdl.createElement("soap:operation")).setAttribute("soapAction",
243 wsdl_input = wsdl_op.appendChild(self.wsdl.createElement("input"))
244 input_soap_body = wsdl_input.appendChild(self.wsdl.createElement("soap:body"))
245 input_soap_body.setAttribute("use", "encoded")
246 input_soap_body.setAttribute("namespace", "urn:" + method)
247 input_soap_body.setAttribute("encodingStyle","http://schemas.xmlsoap.org/soap/encoding/")
250 wsdl_output = wsdl_op.appendChild(self.wsdl.createElement("output"))
251 output_soap_body = wsdl_output.appendChild(self.wsdl.createElement("soap:body"))
252 output_soap_body.setAttribute("use", "encoded")
253 output_soap_body.setAttribute("namespace", "urn:" + method)
254 output_soap_body.setAttribute("encodingStyle","http://schemas.xmlsoap.org/soap/encoding/")
257 def add_wsdl_services(self):
258 for service in self.services.keys():
259 if (getattr(self.interface_options,service)):
260 service_el = self.wsdl.lastChild.appendChild(self.wsdl.createElement("service"))
261 service_el.setAttribute("name", service)
263 for method in self.services[service]:
265 servport_el = service_el.appendChild(self.wsdl.createElement("port"))
266 servport_el.setAttribute("name", name + "_port")
267 servport_el.setAttribute("binding", "tns:" + name + "_binding")
269 soapaddress = servport_el.appendChild(self.wsdl.createElement("soap:address"))
270 soapaddress.setAttribute("location", "%s/%s" % (globals.plc_ns,service))
273 def compute_wsdl_definitions(self):
274 wsdl_text_header = """
275 <?xml-stylesheet type="text/xsl" href="wsdl2html.xsl"?>
278 targetNamespace="%s/sfa.wsdl"
279 xmlns="http://schemas.xmlsoap.org/wsdl/"
280 xmlns:xsd="http://www.w3.org/2001/XMLSchema"
281 xmlns:xsdl="%s/schema"
282 xmlns:tns="%s/sfa.wsdl"
283 xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
284 xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
285 xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"/>
286 """ % (self.interface_name(),globals.plc_ns,globals.plc_ns,globals.plc_ns)
288 self.wsdl = xml.dom.minidom.parseString(wsdl_text_header)
291 def compute_wsdl_definitions_and_types(self):
292 wsdl_text_header = """
293 <?xml-stylesheet type="text/xsl" href="wsdl2html.xsl"?>
296 targetNamespace="%s/sfa.wsdl"
297 xmlns="http://schemas.xmlsoap.org/wsdl/"
298 xmlns:xsd="http://www.w3.org/2001/XMLSchema"
299 xmlns:xsdl="%s/schema"
300 xmlns:tns="%s/sfa.wsdl"
301 xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
302 xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
303 xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
305 <xsd:schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="%s/schema"/>
307 </wsdl:definitions> """ % (self.interface_name(),globals.plc_ns, globals.plc_ns, globals.plc_ns, globals.plc_ns)
308 self.types = xml.dom.minidom.parseString(wsdl_text_header)
311 def add_wsdl_types(self):
312 wsdl_types = self.wsdl.importNode(self.types.getElementsByTagName("types")[0], True)
313 self.wsdl.lastChild.appendChild(wsdl_types)
315 def generate_wsdl(self):
316 self.compute_wsdl_definitions_and_types()
317 self.compute_wsdl_definitions()
318 self.add_wsdl_ports_and_bindings()
319 self.add_wsdl_types()
320 self.add_wsdl_services()
322 def pretty_print(self):
324 xml.dom.ext.PrettyPrint(self.wsdl)
326 raise Exception("Empty WSDL")
329 parser = OptionParser()
330 parser.add_option("-r", "--registry", dest="registry", action="store_true",
331 help="Generate registry.wsdl", metavar="FILE")
332 parser.add_option("-s", "--slice-manager",
333 action="store_true", dest="slicemgr",
334 help="Generate sm.wsdl")
335 parser.add_option("-a", "--aggregate", action="store_true", dest="aggregate",
336 help="Generate am.wsdl")
337 parser.add_option("-c", "--component", action="store_true", dest="component",
338 help="Generate cm.wsdl")
339 parser.add_option("-g", "--geni-aggregate", action="store_true", dest="geni_am",
340 help="Generate gm.wsdl")
341 parser.add_option("-l", "--lite", action="store_true", dest="lite",
342 help="Generate LITE version of the interface, in which calls exclude credentials")
343 (interface_options, args) = parser.parse_args()
345 gen = WSDLGen(interface_options)
350 if __name__ == "__main__":