| 1 | #!/usr/bin/env perl | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use XML::LibXML; | ||
| 7 | use Getopt::Long; | ||
| 8 | |||
| 9 | my ($src, $dst, $ns, %complex_types, %simple_types, %operations, %requests, %responses, $stub_definition); | ||
| 10 | |||
| 11 | GetOptions( | ||
| 12 | "src:s" => \$src, | ||
| 13 | "dst:s" => \$dst, | ||
| 14 | "namespace:s" => \$ns, | ||
| 15 | ); | ||
| 16 | |||
| 17 | $src = "SDK/vsphere-ws/wsdl/vim25/" unless $src; | ||
| 18 | $dst = "lib/VMOMI/" unless $dst; | ||
| 19 | $ns = "VMOMI" unless $ns; | ||
| 20 | |||
| 21 | ## Object Types (SimpleType|ComplexType) ## | ||
| 22 | foreach ('core-types.xsd', 'query-types.xsd', 'vim-types.xsd') { | ||
| 23 | my ($xml, @nodes); | ||
| 24 | |||
| 25 | $xml = XML::LibXML->load_xml(location => $src . "/$_") || die $!; | ||
| 26 | |||
| 27 | ## ComplexTypes | ||
| 28 | @nodes = $xml->getElementsByTagName('complexType'); | ||
| 29 | foreach my $node (@nodes) { | ||
| 30 | my $name = $node->getAttribute('name'); | ||
| 31 | |||
| 32 | # Ignore ManagedObjectReference | ||
| 33 | next if $name eq 'ManagedObjectReference'; | ||
| 34 | |||
| 35 | $complex_types{$name} = ComplexTypeDef->new($name, $node); | ||
| 36 | } | ||
| 37 | |||
| 38 | ## SimpleTypes | ||
| 39 | @nodes = $xml->getElementsByTagName('simpleType'); | ||
| 40 | foreach my $node (@nodes) { | ||
| 41 | my $name = $node->getAttribute('name'); | ||
| 42 | $simple_types{$name} = SimpleTypeDef->new($name, $node); | ||
| 43 | } | ||
| 44 | } | ||
| 45 | |||
| 46 | ## Generate ancestors list for ComplexTypes | ||
| 47 | foreach (keys %complex_types) { | ||
| 48 | my ($typedef, $parent, @ancestors); | ||
| 49 | |||
| 50 | $typedef = $complex_types{$_}; | ||
| 51 | $parent = $typedef->{'parent'}; | ||
| 52 | |||
| 53 | # Implicit understanding of ComplexType as the final ancestor | ||
| 54 | next if $parent eq 'ComplexType'; | ||
| 55 | |||
| 56 | while (defined $parent) { | ||
| 57 | my ($parent_typedef); | ||
| 58 | |||
| 59 | $parent_typedef = $complex_types{$parent}; | ||
| 60 | push @ancestors, $parent; | ||
| 61 | $parent = $parent_typedef->{'parent'}; | ||
| 62 | |||
| 63 | $parent = undef if $parent eq 'ComplexType'; | ||
| 64 | } | ||
| 65 | $complex_types{$_}->{'ancestors'} = \@ancestors; | ||
| 66 | } | ||
| 67 | |||
| 68 | ## Request Messages ## | ||
| 69 | foreach ('query-messagetypes.xsd', 'vim-messagetypes.xsd') { | ||
| 70 | my ($xml, @nodes); | ||
| 71 | |||
| 72 | $xml = XML::LibXML->load_xml(location => $src . "/$_") || die $!; | ||
| 73 | @nodes = $xml->getElementsByTagName('complexType'); | ||
| 74 | foreach my $node (@nodes) { | ||
| 75 | my $name = $node->getAttribute('name'); | ||
| 76 | $requests{$name} = RequestTypeDef->new($name, $node); | ||
| 77 | } | ||
| 78 | |||
| 79 | } | ||
| 80 | |||
| 81 | ## Response Messages ## | ||
| 82 | foreach ('vim.wsdl') { | ||
| 83 | my ($xml, $root, $schema, @nodes); | ||
| 84 | |||
| 85 | $xml = XML::LibXML->load_xml(location => $src . "/$_") || die $!; | ||
| 86 | $root = $xml->documentElement(); | ||
| 87 | $schema = $root->getChildrenByTagName('types')->shift->getChildrenByTagName('schema')->shift; | ||
| 88 | @nodes = $xml->getElementsByTagName('element'); | ||
| 89 | foreach my $node (@nodes) { | ||
| 90 | my $name = $node->getAttribute('name'); | ||
| 91 | |||
| 92 | # Fault responses are handled generically in the SDK logic | ||
| 93 | next if $name !~ m/Response$/; | ||
| 94 | |||
| 95 | $responses{$name} = ResponseTypeDef->new($name, $node); | ||
| 96 | } | ||
| 97 | } | ||
| 98 | |||
| 99 | ## Operations ## | ||
| 100 | foreach ('vim.wsdl') { | ||
| 101 | my ($xml, $root, $schema, @nodes); | ||
| 102 | |||
| 103 | $xml = XML::LibXML->load_xml(location => $src . "/$_") || die $!; | ||
| 104 | $root = $xml->documentElement(); | ||
| 105 | $schema = $root->getChildrenByTagName('types')->shift->getChildrenByTagName('schema')->shift; | ||
| 106 | |||
| 107 | |||
| 108 | @nodes = $schema->getChildrenByTagName('element'); | ||
| 109 | foreach my $node (@nodes) { | ||
| 110 | my ($name, $operation, $res_name, $res_type, $req_name, $req_type); | ||
| 111 | |||
| 112 | $name = $node->getAttribute('name'); | ||
| 113 | next if $name =~ m/(Response$|versionURI|Fault$)/; | ||
| 114 | |||
| 115 | $operation = OperationTypeDef->new($name, $node); | ||
| 116 | |||
| 117 | $req_name = $operation->{'req_type'}; | ||
| 118 | $req_type = $requests{$req_name}; | ||
| 119 | $operation->set_xargs($req_type); | ||
| 120 | |||
| 121 | $res_name = $operation->{'res_type'}; | ||
| 122 | $res_type = $responses{$res_name}; | ||
| 123 | $operation->set_returnval($res_type); | ||
| 124 | |||
| 125 | $operations{$name} = $operation; | ||
| 126 | } | ||
| 127 | } | ||
| 128 | |||
| 129 | ## Write class files | ||
| 130 | foreach ( (values %simple_types, values %complex_types) ) { | ||
| 131 | $_->write_file($ns, $dst); | ||
| 132 | } | ||
| 133 | |||
| 134 | ## Write SoapStub.pm | ||
| 135 | open FILE, ">$dst/SoapStub.pm" or die "failed to open '$dst/SoapStub.pm: $!'"; | ||
| 136 | |||
| 137 | $stub_definition .= "package $ns" . "::SoapStub;\n"; | ||
| 138 | $stub_definition .= "use parent '$ns" . "::SoapBase';\n\n"; | ||
| 139 | |||
| 140 | foreach ( sort keys %operations ) { | ||
| 141 | my ($operation, $name, $xargs, $returnval, $ret_type, $ret_array); | ||
| 142 | |||
| 143 | $operation = $operations{$_}; | ||
| 144 | $name = $operation->{'name'}; | ||
| 145 | $xargs = $operation->{'xargs'}; | ||
| 146 | $returnval = $operation->{'returnval'}; | ||
| 147 | |||
| 148 | $ret_type = defined $returnval->[0] ? "'" . $returnval->[0] . "'" : 'undef'; | ||
| 149 | $ret_array = defined $returnval->[1] ? $returnval->[1] : 0; | ||
| 150 | |||
| 151 | $stub_definition .= "sub $name {\n"; | ||
| 152 | $stub_definition .= " my (\$self, \%args) = \@_;\n"; | ||
| 153 | $stub_definition .= " my \$x_args = [ "; | ||
| 154 | |||
| 155 | # operation xargs | ||
| 156 | if (scalar @$xargs > 0) { | ||
| 157 | $stub_definition .= "\n"; | ||
| 158 | foreach my $x (@{ $operation->{'xargs'} || [ ] }) { | ||
| 159 | my ($x_name, $x_type, $is_array, $is_mandatory) = @$x; | ||
| 160 | |||
| 161 | $x_type = defined $x_type ? "'$x_type'" : 'undef'; | ||
| 162 | $stub_definition .= " ['$x_name', $x_type],\n"; | ||
| 163 | } | ||
| 164 | } | ||
| 165 | $stub_definition .= " ];\n"; | ||
| 166 | $stub_definition .= " return \$self->soap_call('$name', $ret_type, $ret_array, \$x_args, \\%args);\n"; | ||
| 167 | $stub_definition .= "}\n\n"; | ||
| 168 | } | ||
| 169 | |||
| 170 | $stub_definition .= "1;\n"; | ||
| 171 | |||
| 172 | print FILE $stub_definition; | ||
| 173 | close FILE; | ||
| 174 | |||
| 175 | |||
| 176 | package OperationTypeDef; | ||
| 177 | |||
| 178 | sub new { | ||
| 179 | my ($class, $cname, $node) = @_; | ||
| 180 | my ($self, $type); | ||
| 181 | |||
| 182 | $self = { | ||
| 183 | name => $cname, | ||
| 184 | xargs => [ ], | ||
| 185 | returnval => [undef, 0], | ||
| 186 | req_type => undef, | ||
| 187 | res_type => undef, | ||
| 188 | }; | ||
| 189 | |||
| 190 | $type = $node->getAttribute('type'); | ||
| 191 | $type =~ s/^vim25://; | ||
| 192 | if ($type =~ m/^xsd:boolean/) { | ||
| 193 | $type = 'boolean'; | ||
| 194 | } | ||
| 195 | if ($type =~ m/^xsd:anyType/) { | ||
| 196 | $type = 'anyType'; | ||
| 197 | } | ||
| 198 | if ($type =~ m/^xsd:/) { | ||
| 199 | $type = undef; | ||
| 200 | } | ||
| 201 | |||
| 202 | $self->{'req_type'} = $type; | ||
| 203 | $self->{'res_type'} = $cname . "Response"; | ||
| 204 | |||
| 205 | return bless $self, $class; | ||
| 206 | } | ||
| 207 | |||
| 208 | sub set_returnval { | ||
| 209 | my ($self, $res_type) = @_; | ||
| 210 | my ($type, $is_array); | ||
| 211 | |||
| 212 | $type = $res_type->{'type'}; | ||
| 213 | $is_array = $res_type->{'is_array'}; | ||
| 214 | |||
| 215 | $self->{'returnval'} = [$type, $is_array]; | ||
| 216 | return; | ||
| 217 | } | ||
| 218 | |||
| 219 | sub set_xargs { | ||
| 220 | my ($self, $req_type) = @_; | ||
| 221 | my ($members); | ||
| 222 | |||
| 223 | $members = $req_type->{'members'}; | ||
| 224 | foreach (@{ $members || [ ] }) { | ||
| 225 | my ($name, $type, $min, $max, $is_array, $is_mandatory); | ||
| 226 | |||
| 227 | $name = $_->[0]; | ||
| 228 | $type = $_->[1]; | ||
| 229 | $min = $_->[2]; | ||
| 230 | $max = $_->[3]; | ||
| 231 | |||
| 232 | # mandatory? | ||
| 233 | $is_mandatory = (defined $min and $min eq "0") ? 0 : 1; | ||
| 234 | |||
| 235 | # array? | ||
| 236 | $is_array = defined $max ? 1 : 0; | ||
| 237 | |||
| 238 | push @{$self->{'xargs'}}, [$name, $type, $is_array, $is_mandatory]; | ||
| 239 | } | ||
| 240 | |||
| 241 | return; | ||
| 242 | } | ||
| 243 | |||
| 244 | |||
| 245 | 1; | ||
| 246 | |||
| 247 | package ResponseTypeDef; | ||
| 248 | |||
| 249 | sub new { | ||
| 250 | my ($class, $cname, $node) = @_; | ||
| 251 | my ($self, $return_node, $type, $min, $max, $is_array, $is_mandatory); | ||
| 252 | |||
| 253 | $self = { | ||
| 254 | name => $cname, | ||
| 255 | type => undef, | ||
| 256 | is_array => 0, | ||
| 257 | }; | ||
| 258 | |||
| 259 | $return_node = $node->getElementsByTagName('element')->shift; | ||
| 260 | if (defined $return_node) { | ||
| 261 | $type = $return_node->getAttribute('type'); | ||
| 262 | $min = $return_node->getAttribute('minOccurs'); | ||
| 263 | $max = $return_node->getAttribute('maxOccurs'); | ||
| 264 | |||
| 265 | $type =~ s/^vim25://; | ||
| 266 | if ($type =~ m/^xsd:boolean/) { | ||
| 267 | $type = 'boolean'; | ||
| 268 | } | ||
| 269 | if ($type =~ m/^xsd:anyType/) { | ||
| 270 | $type = 'anyType'; | ||
| 271 | } | ||
| 272 | if ($type =~ m/^xsd:/) { | ||
| 273 | $type = undef; | ||
| 274 | } | ||
| 275 | |||
| 276 | $self->{'type'} = $type; | ||
| 277 | $self->{'is_array'} = defined $max ? 1 : 0; | ||
| 278 | } | ||
| 279 | |||
| 280 | return bless $self, $class; | ||
| 281 | } | ||
| 282 | |||
| 283 | |||
| 284 | 1; | ||
| 285 | |||
| 286 | package RequestTypeDef; | ||
| 287 | |||
| 288 | sub new { | ||
| 289 | my ($class, $cname, $node) = @_; | ||
| 290 | |||
| 291 | my $self = { | ||
| 292 | name => $cname, | ||
| 293 | members => [ ], | ||
| 294 | }; | ||
| 295 | foreach my $member_node ( $node->getElementsByLocalName('element') ) { | ||
| 296 | my ($name, $type, $min, $max); | ||
| 297 | |||
| 298 | $name = $member_node->getAttribute('name'); | ||
| 299 | $type = $member_node->getAttribute('type'); | ||
| 300 | $min = $member_node->getAttribute('minOccurs'); | ||
| 301 | $max = $member_node->getAttribute('maxOccurs'); | ||
| 302 | |||
| 303 | $type =~ s/^vim25://; | ||
| 304 | if ($type =~ m/^xsd:boolean/) { | ||
| 305 | $type = 'boolean'; | ||
| 306 | } | ||
| 307 | if ($type =~ m/^xsd:anyType/) { | ||
| 308 | $type = 'anyType'; | ||
| 309 | } | ||
| 310 | if ($type =~ m/^xsd:/) { | ||
| 311 | $type = undef; | ||
| 312 | } | ||
| 313 | |||
| 314 | push @{ $self->{'members'} }, [$name, $type, $min, $max]; | ||
| 315 | } | ||
| 316 | |||
| 317 | return bless $self, $class; | ||
| 318 | } | ||
| 319 | |||
| 320 | 1; | ||
| 321 | |||
| 322 | package SimpleTypeDef; | ||
| 323 | |||
| 324 | sub new { | ||
| 325 | my ($class, $cname, $node) = @_; | ||
| 326 | my ($self, $restriction); | ||
| 327 | |||
| 328 | $self = { | ||
| 329 | name => $cname, | ||
| 330 | parent => 'SimpleType', | ||
| 331 | ancestors => [ ], | ||
| 332 | members => [ ], | ||
| 333 | }; | ||
| 334 | |||
| 335 | $restriction = $node->getElementsByLocalName('restriction')->shift; | ||
| 336 | if ($restriction->getAttribute('base') !~ m/^xsd:string/) { | ||
| 337 | die "unexpected base type: " . $restriction->getAttribute('base') . " in SimpleType '$cname'"; | ||
| 338 | } | ||
| 339 | # TODO: Parse restriction nodes as enum values for SimpleType for validation and/or constructor | ||
| 340 | |||
| 341 | return bless $self, $class; | ||
| 342 | } | ||
| 343 | |||
| 344 | sub write_file { | ||
| 345 | my ($self, $namespace, $path) = @_; | ||
| 346 | my ($class_name, $super_name, $fname, $class_definition); | ||
| 347 | |||
| 348 | $fname = $path . "/" . $self->{'name'} . ".pm"; | ||
| 349 | open FILE, ">$fname" or die "failed to open $fname: $!"; | ||
| 350 | |||
| 351 | $class_name = $self->{'name'}; | ||
| 352 | $super_name = $self->{'parent'}; | ||
| 353 | |||
| 354 | $class_definition .= "package $ns::$class_name;\n"; | ||
| 355 | $class_definition .= "use parent '$ns::$super_name';\n\n"; | ||
| 356 | $class_definition .= "1;\n"; | ||
| 357 | |||
| 358 | print FILE $class_definition; | ||
| 359 | close FILE; | ||
| 360 | } | ||
| 361 | |||
| 362 | 1; | ||
| 363 | |||
| 364 | package ComplexTypeDef; | ||
| 365 | |||
| 366 | sub new { | ||
| 367 | my ($class, $cname, $node) = @_; | ||
| 368 | my ($self, $super_node, $super_name); | ||
| 369 | |||
| 370 | $self = { | ||
| 371 | name => $cname, | ||
| 372 | parent => 'ComplexType', | ||
| 373 | ancestors => [ ], | ||
| 374 | members => [ ] | ||
| 375 | }; | ||
| 376 | |||
| 377 | # Determine class parent | ||
| 378 | $super_node = $node->getElementsByLocalName('extension')->shift; | ||
| 379 | if (defined $super_node) { | ||
| 380 | $super_name = $super_node->getAttribute('base'); | ||
| 381 | $super_name =~ s/^vim25://; | ||
| 382 | } | ||
| 383 | $self->{'parent'} = $super_name if defined $super_name; | ||
| 384 | |||
| 385 | # Iterate class members | ||
| 386 | foreach my $member_node ( $node->getElementsByLocalName('element') ) { | ||
| 387 | my ($name, $type, $min, $max, $is_array, $is_mandatory); | ||
| 388 | $name = $member_node->getAttribute('name'); | ||
| 389 | $type = $member_node->getAttribute('type'); | ||
| 390 | $min = $member_node->getAttribute('minOccurs'); | ||
| 391 | $max = $member_node->getAttribute('maxOccurs'); | ||
| 392 | |||
| 393 | # type | ||
| 394 | $type =~ s/^vim25://; | ||
| 395 | if ($type =~ m/^xsd:boolean/) { | ||
| 396 | $type = 'boolean'; | ||
| 397 | } | ||
| 398 | if ($type =~ m/^xsd:anyType/) { | ||
| 399 | $type = 'anyType'; | ||
| 400 | } | ||
| 401 | if ($type =~ m/^xsd:/) { | ||
| 402 | $type = undef; | ||
| 403 | } | ||
| 404 | |||
| 405 | # array? | ||
| 406 | $is_array = defined $max ? 1 : 0; | ||
| 407 | |||
| 408 | # mandatory? | ||
| 409 | $is_mandatory = defined $min and $min eq "0" ? 0 : 1; | ||
| 410 | |||
| 411 | push @{ $self->{'members'} }, [$name, $type, $is_array, $is_mandatory]; | ||
| 412 | } | ||
| 413 | |||
| 414 | return bless $self, $class; | ||
| 415 | } | ||
| 416 | |||
| 417 | sub write_file { | ||
| 418 | my ($self, $namespace, $path) = @_; | ||
| 419 | my ($class_name, $super_name, $fname, $class_definition, @ancestors, @members); | ||
| 420 | |||
| 421 | $fname = $path . "/" . $self->{'name'} . ".pm"; | ||
| 422 | open FILE, ">$fname" or die "failed to open $fname: $!"; | ||
| 423 | |||
| 424 | $class_name = $self->{'name'}; | ||
| 425 | $super_name = $self->{'parent'}; | ||
| 426 | |||
| 427 | $class_definition .= "package $ns::$class_name;\n"; | ||
| 428 | $class_definition .= "use parent '$ns::$super_name';\n\n"; | ||
| 429 | |||
| 430 | # Bug with OptionDef, present in 5.5 and possibly older versions? Current VMODL doesn't define DynamicData | ||
| 431 | # and will raise runtime errors when interfacing with vSphere prior to 6.0 | ||
| 432 | # https://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&externalId=2101118 | ||
| 433 | if ($class_name eq 'DynamicData') { | ||
| 434 | push @{ $self->{'members'} }, ['dynamicProperty', 'DynamicProperty', 1, 0]; | ||
| 435 | push @{ $self->{'members'} }, ['dynamicType', undef, 0, 0]; | ||
| 436 | } | ||
| 437 | |||
| 438 | # class ancestors | ||
| 439 | @ancestors = @{ $self->{'ancestors'} }; | ||
| 440 | $class_definition .= "our \@class_ancestors = ( "; | ||
| 441 | if (scalar @ancestors > 0) { | ||
| 442 | $class_definition .= "\n"; | ||
| 443 | foreach my $ancestor (@ancestors) { | ||
| 444 | $class_definition .= " '$ancestor',\n"; | ||
| 445 | } | ||
| 446 | } | ||
| 447 | $class_definition .= ");\n\n"; | ||
| 448 | |||
| 449 | # class members | ||
| 450 | @members = @{ $self->{'members'} }; | ||
| 451 | $class_definition .= "our \@class_members = ( "; | ||
| 452 | if (scalar @members > 0) { | ||
| 453 | $class_definition .= "\n"; | ||
| 454 | foreach my $member (@members) { | ||
| 455 | my ($name, $type, $is_array, $is_mandatory) = @$member; | ||
| 456 | |||
| 457 | $type = defined $type ? "'$type'" : 'undef'; | ||
| 458 | $class_definition .= " ['$name', $type, $is_array, $is_mandatory],\n"; | ||
| 459 | } | ||
| 460 | } | ||
| 461 | |||
| 462 | $class_definition .= ");\n\n"; | ||
| 463 | $class_definition .= "sub get_class_ancestors {\n"; | ||
| 464 | $class_definition .= " return \@class_ancestors;\n"; | ||
| 465 | $class_definition .= "}\n\n"; | ||
| 466 | $class_definition .= "sub get_class_members {\n"; | ||
| 467 | $class_definition .= " my \$class = shift;\n"; | ||
| 468 | $class_definition .= " my \@super_members = \$class->SUPER::get_class_members();\n"; | ||
| 469 | $class_definition .= " return (\@super_members, \@class_members);\n"; | ||
| 470 | $class_definition .= "}\n\n1;\n"; | ||
| 471 | |||
| 472 | print FILE $class_definition; | ||
| 473 | close FILE; | ||
| 474 | } | ||
| 475 | |||
| 476 | 1; |