remove component/install. pyopenssl is already packaged and others are provided by...
[sfa.git] / wsdl / gw2wsdl.py
1 #!/usr/bin/python
2 #
3 # Sapan Bhatia <sapanb@cs.princeton.edu>
4 #
5 # Generates a WSDL for geniwrapper
6 # Current limitations:
7 # - Invalid for the following reasons 
8 # - The types are python types, not WSDL types
9
10 import os, sys
11 import time
12 import pdb
13 import xml.dom.minidom
14 import xml.dom.ext
15 import globals
16 import apistub
17
18 from geni.util.auth import Auth
19 from geni.util.parameter import Parameter,Mixed,python_type,xmlrpc_type
20
21
22 try:
23     set
24 except NameError:
25     from sets import Set
26     set = Set
27
28 # Class functions
29
30 def param_type(param):
31
32 #     if isinstance(param, Mixed) and len(param):
33 #         subtypes = [param_type(subparam) for subparam in param]
34 #         return " or ".join(subtypes)
35 #     elif isinstance(param, (list, tuple, set)) and len(param):
36 #         return "array of " + " or ".join([param_type(subparam) for subparam in param])
37 #     else:
38 #         return xmlrpc_type(python_type(param))
39     return "some type - todo"
40
41
42 def add_wsdl_ports_and_bindings (wsdl):
43     for method in apistub.methods:
44         # Skip system. methods
45         if "system." in method:
46             continue
47
48         function = apistub.callable(method) # Commented documentation
49         #lines = ["// " + line.strip() for line in function.__doc__.strip().split("\n")]
50         #print "\n".join(lines)
51         #print
52
53         
54         in_el = wsdl.firstChild.appendChild(wsdl.createElement("wsdl:message"))
55         in_el.setAttribute("name", method + "_in")
56
57         for service_name in function.interfaces:
58             if (services.has_key(service_name)):
59                 if (not method in services[service_name]):
60                     services[service_name].append(method)
61             else:
62                 services[service_name]=[method]
63
64         # Arguments
65
66         if (function.accepts):
67             (min_args, max_args, defaults) = function.args()
68             for (argname,argtype) in zip(max_args,function.accepts):
69                 arg_part = in_el.appendChild(wsdl.createElement("wsdl:part"))
70                 arg_part.setAttribute("name", argname)
71                 arg_part.setAttribute("type", param_type(argtype))
72                 
73         # Return type            
74         return_type = function.returns
75         out_el = wsdl.firstChild.appendChild(wsdl.createElement("wsdl:message"))
76         out_el.setAttribute("name", method + "_out")
77         ret_part = out_el.appendChild(wsdl.createElement("wsdl:part"))
78         ret_part.setAttribute("name", "returnvalue")
79         ret_part.setAttribute("type", param_type(return_type))
80
81         # Port connecting arguments with return type
82
83         port_el = wsdl.firstChild.appendChild(wsdl.createElement("wsdl:portType"))
84         port_el.setAttribute("name", method + "_port")
85         
86         op_el = port_el.appendChild(wsdl.createElement("wsdl:operation"))
87         op_el.setAttribute("name", method)
88         op_el.appendChild(wsdl.createElement("wsdl:input")).setAttribute("message","tns:" + method + "_in")
89         op_el.appendChild(wsdl.createElement("wsdl:output")).setAttribute("message","tns:" + method + "_out")
90
91         # Bindings
92
93         bind_el = wsdl.firstChild.appendChild(wsdl.createElement("wsdl:binding"))
94         bind_el.setAttribute("name", method + "_binding")
95         bind_el.setAttribute("type", "tns:" + method + "_port")
96         
97         soap_bind = bind_el.appendChild(wsdl.createElement("soap:binding"))
98         soap_bind.setAttribute("style", "rpc")
99         soap_bind.setAttribute("transport","http://schemas.xmlsoap.org/soap/http")
100
101         
102         wsdl_op = bind_el.appendChild(wsdl.createElement("wsdl:operation"))
103         wsdl_op.setAttribute("name", method)
104         wsdl_op.appendChild(wsdl.createElement("soap:operation")).setAttribute("soapAction",
105                 "urn:" + method)
106
107         
108         wsdl_input = wsdl_op.appendChild(wsdl.createElement("wsdl:input"))
109         input_soap_body = wsdl_input.appendChild(wsdl.createElement("soap:body"))
110         input_soap_body.setAttribute("use", "encoded")
111         input_soap_body.setAttribute("namespace", "urn:" + method)
112         input_soap_body.setAttribute("encodingStyle","http://schemas.xmlsoap.org/soap/encoding/")
113
114         
115         wsdl_output = wsdl_op.appendChild(wsdl.createElement("wsdl:output"))
116         output_soap_body = wsdl_output.appendChild(wsdl.createElement("soap:body"))
117         output_soap_body.setAttribute("use", "encoded")
118         output_soap_body.setAttribute("namespace", "urn:" + method)
119         output_soap_body.setAttribute("encodingStyle","http://schemas.xmlsoap.org/soap/encoding/")
120         
121
122 def add_wsdl_service(wsdl):
123     for service in services.keys():
124         service_el = wsdl.firstChild.appendChild(wsdl.createElement("wsdl:service"))
125         service_el.setAttribute("name", service)
126
127         for method in services[service]:
128             name=method
129             servport_el = service_el.appendChild(wsdl.createElement("wsdl:port"))
130             servport_el.setAttribute("name", name + "_port")
131             servport_el.setAttribute("binding", "tns:" + name + "_binding")
132
133             soapaddress = servport_el.appendChild(wsdl.createElement("soap:address"))
134             soapaddress.setAttribute("location", "%s/%s" % (globals.plc_ns,service))
135
136
137 def get_wsdl_definitions():
138     wsdl_text_header = """
139         <wsdl:definitions
140         name="auto_generated"
141         targetNamespace="%s"
142         xmlns:xsd="http://www.w3.org/2000/10/XMLSchema"
143         xmlns:tns="xmlns:%s"
144         xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
145         xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"/>""" % (globals.plc_ns,globals.plc_ns)
146         
147     wsdl = xml.dom.minidom.parseString(wsdl_text_header)
148
149     return wsdl
150     
151
152 services = {}
153
154 wsdl = get_wsdl_definitions()
155 add_wsdl_ports_and_bindings(wsdl)
156 add_wsdl_service(wsdl)
157
158
159 xml.dom.ext.PrettyPrint(wsdl)
160