Verify request against capacity
[sfa.git] / wsdl / gw2wsdl.py
index cf3fc68..4ee1257 100755 (executable)
@@ -37,13 +37,25 @@ except NameError:
     from sets import Set
     set = Set
 
+def filter_argname(argname):
+    global interface_options
+    if (not interface_options.lite or (argname!="cred")):
+        if (argname.find('(') != -1):
+            # The name has documentation in it :-/
+            brackright = argname.split('(')[1]
+            if (brackright.find(')') == -1):
+                    raise Exception("Please fix the argument %s to be well-formed.\n"%argname)
+            inbrack = brackright.split(')')[0]
+            argname = inbrack
+    return argname
+
 def fold_complex_type_names(acc, arg):
     name = arg.doc
     if (type(acc)==list):
         acc.append(name)
     else:
-        python_is_braindead = acc.doc
-        acc = [python_is_braindead,name]
+        p_i_b = acc.doc
+        acc = [p_i_b,name]
     return acc
 
 def fold_complex_type(acc, arg):
@@ -53,8 +65,8 @@ def fold_complex_type(acc, arg):
     if (type(acc)==list):
         acc.append(name)
     else:
-        python_is_braindead = name_complex_type(acc)
-        acc = [python_is_braindead,name]
+        p_i_b = name_complex_type(acc)
+        acc = [p_i_b,name]
     return acc
 
 def name_complex_type(arg):
@@ -81,6 +93,7 @@ def name_complex_type(arg):
         choice = complex_type.appendChild(types.createElement("xsd:choice"))
         for n,t in zip(inner_names,inner_types):
             element = choice.appendChild(types.createElement("element"))
+            n = filter_argname(n)
             element.setAttribute("name", n)
             element.setAttribute("type", "%s"%t)
             element.setAttribute("minOccurs","%d"%min_args)
@@ -91,15 +104,23 @@ def name_complex_type(arg):
         inner_type = name_complex_type(arg[0]) 
         num_types=num_types+1
         type_name = "Type%d"%num_types
-        complex_type = types_section.appendChild(types.createElement("xsd:simpleType"))
+        complex_type = types_section.appendChild(types.createElement("xsd:complexType"))
+        type_name = filter_argname(type_name)
         complex_type.setAttribute("name", type_name)
-        complex_content = complex_type.appendChild(types.createElement("xsd:list"))
-        complex_content.setAttribute("itemType",inner_type)
+        complex_content = complex_type.appendChild(types.createElement("xsd:complexContent"))
+        restriction = complex_content.appendChild(types.createElement("xsd:restriction"))
+        restriction.setAttribute("base","soapenc:Array")
+        attribute = restriction.appendChild(types.createElement("xsd:attribute"))
+        attribute.setAttribute("ref","soapenc:arrayType")
+        attribute.setAttribute("wsdl:arrayType","%s[]"%inner_type)
+
         return "xsdl:%s"%type_name
+
     elif type(arg) == DictType or arg == DictType or (inspect.isclass(arg) and issubclass(arg, dict)):
         num_types=num_types+1
         type_name = "Type%d"%num_types
         complex_type = types_section.appendChild(types.createElement("xsd:complexType"))
+        type_name = filter_argname(type_name)
         complex_type.setAttribute("name", type_name)
         complex_content = complex_type.appendChild(types.createElement("xsd:sequence"))
  
@@ -164,6 +185,7 @@ def add_wsdl_ports_and_bindings (wsdl):
         if (function.accepts):
             (min_args, max_args, defaults) = function.args()
             for (argname,argtype) in zip(max_args,function.accepts):
+                argname = filter_argname(argname)
                 arg_part = in_el.appendChild(wsdl.createElement("part"))
                 arg_part.setAttribute("name", argname)
                 arg_part.setAttribute("type", param_type(argtype))
@@ -173,7 +195,7 @@ def add_wsdl_ports_and_bindings (wsdl):
         out_el = wsdl.firstChild.appendChild(wsdl.createElement("message"))
         out_el.setAttribute("name", method + "_out")
         ret_part = out_el.appendChild(wsdl.createElement("part"))
-        ret_part.setAttribute("name", "returnvalue")
+        ret_part.setAttribute("name", "Result")
         ret_part.setAttribute("type", param_type(return_type))
 
         # Port connecting arguments with return type
@@ -225,17 +247,19 @@ def add_wsdl_ports_and_bindings (wsdl):
 
 def add_wsdl_service(wsdl):
     for service in services.keys():
-        service_el = wsdl.firstChild.appendChild(wsdl.createElement("service"))
-        service_el.setAttribute("name", service)
+        global interface_options
+        if (getattr(interface_options,service)):
+            service_el = wsdl.firstChild.appendChild(wsdl.createElement("service"))
+            service_el.setAttribute("name", service)
 
-        for method in services[service]:
-            name=method
-            servport_el = service_el.appendChild(wsdl.createElement("port"))
-            servport_el.setAttribute("name", name + "_port")
-            servport_el.setAttribute("binding", "tns:" + name + "_binding")
+            for method in services[service]:
+                    name=method
+                    servport_el = service_el.appendChild(wsdl.createElement("port"))
+                    servport_el.setAttribute("name", name + "_port")
+                    servport_el.setAttribute("binding", "tns:" + name + "_binding")
 
-            soapaddress = servport_el.appendChild(wsdl.createElement("soap:address"))
-            soapaddress.setAttribute("location", "%s/%s" % (globals.plc_ns,service))
+                    soapaddress = servport_el.appendChild(wsdl.createElement("soap:address"))
+                    soapaddress.setAttribute("location", "%s/%s" % (globals.plc_ns,service))
 
 
 def get_wsdl_definitions():
@@ -248,7 +272,7 @@ def get_wsdl_definitions():
         xmlns:xsdl="%s/2009/07/schema"
         xmlns:tns="%s/2009/07/sfa.wsdl"
         xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
-        xmlns:soapenc="http://schemas.xmlsoap.org/wsdl/soap/encoding"
+        xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
         xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"/>
         """ % (globals.plc_ns,globals.plc_ns,globals.plc_ns)
         
@@ -266,7 +290,7 @@ def get_wsdl_definitions_and_types():
         xmlns:xsdl="%s/2009/07/schema"
         xmlns:tns="%s/2009/07/sfa.wsdl"
         xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
-        xmlns:soapenc="http://schemas.xmlsoap.org/wsdl/soap/encoding"
+        xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
         xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
         <types>
             <xsd:schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="%s/2009/07/schema"/>
@@ -275,13 +299,30 @@ def get_wsdl_definitions_and_types():
     wsdl = xml.dom.minidom.parseString(wsdl_text_header)
     return wsdl
 
-    
-types = get_wsdl_definitions_and_types()
-
-wsdl = get_wsdl_definitions()
-add_wsdl_ports_and_bindings(wsdl)
-wsdl_types = wsdl.importNode(types.getElementsByTagName("types")[0], True)
-wsdl.firstChild.appendChild(wsdl_types)
-add_wsdl_service(wsdl)
-
-xml.dom.ext.PrettyPrint(wsdl)
+def main():
+    global types
+    global interface_options
+
+    parser = OptionParser()
+    parser.add_option("-r", "--registry", dest="registry", action="store_true", 
+                              help="Generate registry.wsdl", metavar="FILE")
+    parser.add_option("-s", "--slice-manager",
+                              action="store_true", dest="slicemgr", 
+                              help="Generate sm.wsdl")
+    parser.add_option("-a", "--aggregate", action="store_true", dest="aggregate",
+                              help="Generate am.wsdl")
+    parser.add_option("-l", "--lite", action="store_true", dest="lite",
+                              help="Generate LITE version of the interface, in which calls exclude credentials")
+    (interface_options, args) = parser.parse_args()
+
+    types = get_wsdl_definitions_and_types()
+    wsdl = get_wsdl_definitions()
+    add_wsdl_ports_and_bindings(wsdl)
+    wsdl_types = wsdl.importNode(types.getElementsByTagName("types")[0], True)
+    wsdl.firstChild.appendChild(wsdl_types)
+    add_wsdl_service(wsdl)
+
+    xml.dom.ext.PrettyPrint(wsdl)
+
+if __name__ == "__main__":
+        main()