| 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 | $stub_definition .= "use strict;\nuse warnings;\n\n"; | ||
| 140 | |||
| 141 | foreach ( sort keys %operations ) { | ||
| 142 | my ($operation, $name, $xargs, $returnval, $ret_type, $ret_array); | ||
| 143 | |||
| 144 | $operation = $operations{$_}; | ||
| 145 | $name = $operation->{'name'}; | ||
| 146 | $xargs = $operation->{'xargs'}; | ||
| 147 | $returnval = $operation->{'returnval'}; | ||
| 148 | |||
| 149 | $ret_type = defined $returnval->[0] ? "'" . $returnval->[0] . "'" : 'undef'; | ||
| 150 | $ret_array = defined $returnval->[1] ? $returnval->[1] : 0; | ||
| 151 | |||
| 152 | $stub_definition .= "sub $name {\n"; | ||
| 153 | $stub_definition .= " my (\$self, \%args) = \@_;\n"; | ||
| 154 | $stub_definition .= " my \$x_args = [ "; | ||
| 155 | |||
| 156 | # operation xargs | ||
| 157 | if (scalar @$xargs > 0) { | ||
| 158 | $stub_definition .= "\n"; | ||
| 159 | foreach my $x (@{ $operation->{'xargs'} || [ ] }) { | ||
| 160 | my ($x_name, $x_type, $is_array, $is_mandatory) = @$x; | ||
| 161 | |||
| 162 | $x_type = defined $x_type ? "'$x_type'" : 'undef'; | ||
| 163 | $stub_definition .= " ['$x_name', $x_type],\n"; | ||
| 164 | } | ||
| 165 | } | ||
| 166 | $stub_definition .= " ];\n"; | ||
| 167 | $stub_definition .= " return \$self->soap_call('$name', $ret_type, $ret_array, \$x_args, \\%args);\n"; | ||
| 168 | $stub_definition .= "}\n\n"; | ||
| 169 | } | ||
| 170 | |||
| 171 | $stub_definition .= "1;\n"; | ||
| 172 | |||
| 173 | print FILE $stub_definition; | ||
| 174 | close FILE; | ||
| 175 | |||
| 176 | |||
| 177 | package OperationTypeDef; | ||
| 178 | |||
| 179 | sub new { | ||
| 180 | my ($class, $cname, $node) = @_; | ||
| 181 | my ($self, $type); | ||
| 182 | |||
| 183 | $self = { | ||
| 184 | name => $cname, | ||
| 185 | xargs => [ ], | ||
| 186 | returnval => [undef, 0], | ||
| 187 | req_type => undef, | ||
| 188 | res_type => undef, | ||
| 189 | }; | ||
| 190 | |||
| 191 | $type = $node->getAttribute('type'); | ||
| 192 | $type =~ s/^vim25://; | ||
| 193 | if ($type =~ m/^xsd:boolean/) { | ||
| 194 | $type = 'boolean'; | ||
| 195 | } | ||
| 196 | if ($type =~ m/^xsd:anyType/) { | ||
| 197 | $type = 'anyType'; | ||
| 198 | } | ||
| 199 | if ($type =~ m/^xsd:/) { | ||
| 200 | $type = undef; | ||
| 201 | } | ||
| 202 | |||
| 203 | $self->{'req_type'} = $type; | ||
| 204 | $self->{'res_type'} = $cname . "Response"; | ||
| 205 | |||
| 206 | return bless $self, $class; | ||
| 207 | } | ||
| 208 | |||
| 209 | sub set_returnval { | ||
| 210 | my ($self, $res_type) = @_; | ||
| 211 | my ($type, $is_array); | ||
| 212 | |||
| 213 | $type = $res_type->{'type'}; | ||
| 214 | $is_array = $res_type->{'is_array'}; | ||
| 215 | |||
| 216 | $self->{'returnval'} = [$type, $is_array]; | ||
| 217 | return; | ||
| 218 | } | ||
| 219 | |||
| 220 | sub set_xargs { | ||
| 221 | my ($self, $req_type) = @_; | ||
| 222 | my ($members); | ||
| 223 | |||
| 224 | $members = $req_type->{'members'}; | ||
| 225 | foreach (@{ $members || [ ] }) { | ||
| 226 | my ($name, $type, $min, $max, $is_array, $is_mandatory); | ||
| 227 | |||
| 228 | $name = $_->[0]; | ||
| 229 | $type = $_->[1]; | ||
| 230 | $min = $_->[2]; | ||
| 231 | $max = $_->[3]; | ||
| 232 | |||
| 233 | # mandatory? | ||
| 234 | $is_mandatory = (defined $min and $min eq "0") ? 0 : 1; | ||
| 235 | |||
| 236 | # array? | ||
| 237 | $is_array = defined $max ? 1 : 0; | ||
| 238 | |||
| 239 | push @{$self->{'xargs'}}, [$name, $type, $is_array, $is_mandatory]; | ||
| 240 | } | ||
| 241 | |||
| 242 | return; | ||
| 243 | } | ||
| 244 | |||
| 245 | |||
| 246 | 1; | ||
| 247 | |||
| 248 | package ResponseTypeDef; | ||
| 249 | |||
| 250 | sub new { | ||
| 251 | my ($class, $cname, $node) = @_; | ||
| 252 | my ($self, $return_node, $type, $min, $max, $is_array, $is_mandatory); | ||
| 253 | |||
| 254 | $self = { | ||
| 255 | name => $cname, | ||
| 256 | type => undef, | ||
| 257 | is_array => 0, | ||
| 258 | }; | ||
| 259 | |||
| 260 | $return_node = $node->getElementsByTagName('element')->shift; | ||
| 261 | if (defined $return_node) { | ||
| 262 | $type = $return_node->getAttribute('type'); | ||
| 263 | $min = $return_node->getAttribute('minOccurs'); | ||
| 264 | $max = $return_node->getAttribute('maxOccurs'); | ||
| 265 | |||
| 266 | $type =~ s/^vim25://; | ||
| 267 | if ($type =~ m/^xsd:boolean/) { | ||
| 268 | $type = 'boolean'; | ||
| 269 | } | ||
| 270 | if ($type =~ m/^xsd:anyType/) { | ||
| 271 | $type = 'anyType'; | ||
| 272 | } | ||
| 273 | if ($type =~ m/^xsd:/) { | ||
| 274 | $type = undef; | ||
| 275 | } | ||
| 276 | |||
| 277 | $self->{'type'} = $type; | ||
| 278 | $self->{'is_array'} = defined $max ? 1 : 0; | ||
| 279 | } | ||
| 280 | |||
| 281 | return bless $self, $class; | ||
| 282 | } | ||
| 283 | |||
| 284 | |||
| 285 | 1; | ||
| 286 | |||
| 287 | package RequestTypeDef; | ||
| 288 | |||
| 289 | sub new { | ||
| 290 | my ($class, $cname, $node) = @_; | ||
| 291 | |||
| 292 | my $self = { | ||
| 293 | name => $cname, | ||
| 294 | members => [ ], | ||
| 295 | }; | ||
| 296 | foreach my $member_node ( $node->getElementsByLocalName('element') ) { | ||
| 297 | my ($name, $type, $min, $max); | ||
| 298 | |||
| 299 | $name = $member_node->getAttribute('name'); | ||
| 300 | $type = $member_node->getAttribute('type'); | ||
| 301 | $min = $member_node->getAttribute('minOccurs'); | ||
| 302 | $max = $member_node->getAttribute('maxOccurs'); | ||
| 303 | |||
| 304 | $type =~ s/^vim25://; | ||
| 305 | if ($type =~ m/^xsd:boolean/) { | ||
| 306 | $type = 'boolean'; | ||
| 307 | } | ||
| 308 | if ($type =~ m/^xsd:anyType/) { | ||
| 309 | $type = 'anyType'; | ||
| 310 | } | ||
| 311 | if ($type =~ m/^xsd:/) { | ||
| 312 | $type = undef; | ||
| 313 | } | ||
| 314 | |||
| 315 | push @{ $self->{'members'} }, [$name, $type, $min, $max]; | ||
| 316 | } | ||
| 317 | |||
| 318 | return bless $self, $class; | ||
| 319 | } | ||
| 320 | |||
| 321 | 1; | ||
| 322 | |||
| 323 | package SimpleTypeDef; | ||
| 324 | |||
| 325 | sub new { | ||
| 326 | my ($class, $cname, $node) = @_; | ||
| 327 | my ($self, $restriction); | ||
| 328 | |||
| 329 | $self = { | ||
| 330 | name => $cname, | ||
| 331 | parent => 'SimpleType', | ||
| 332 | ancestors => [ ], | ||
| 333 | members => [ ], | ||
| 334 | }; | ||
| 335 | |||
| 336 | $restriction = $node->getElementsByLocalName('restriction')->shift; | ||
| 337 | if ($restriction->getAttribute('base') !~ m/^xsd:string/) { | ||
| 338 | die "unexpected base type: " . $restriction->getAttribute('base') . " in SimpleType '$cname'"; | ||
| 339 | } | ||
| 340 | # TODO: Parse restriction nodes as enum values for SimpleType for validation and/or constructor | ||
| 341 | |||
| 342 | return bless $self, $class; | ||
| 343 | } | ||
| 344 | |||
| 345 | sub write_file { | ||
| 346 | my ($self, $namespace, $path) = @_; | ||
| 347 | my ($class_name, $super_name, $fname, $class_definition); | ||
| 348 | |||
| 349 | $fname = $path . "/" . $self->{'name'} . ".pm"; | ||
| 350 | open FILE, ">$fname" or die "failed to open $fname: $!"; | ||
| 351 | |||
| 352 | $class_name = $self->{'name'}; | ||
| 353 | $super_name = $self->{'parent'}; | ||
| 354 | |||
| 355 | $class_definition .= "package $ns::$class_name;\n"; | ||
| 356 | $class_definition .= "use parent '$ns::$super_name';\n\n"; | ||
| 357 | $class_definition .= "use strict;\nuse warnings;\n\n"; | ||
| 358 | $class_definition .= "1;\n"; | ||
| 359 | |||
| 360 | print FILE $class_definition; | ||
| 361 | close FILE; | ||
| 362 | } | ||
| 363 | |||
| 364 | 1; | ||
| 365 | |||
| 366 | package ComplexTypeDef; | ||
| 367 | |||
| 368 | sub new { | ||
| 369 | my ($class, $cname, $node) = @_; | ||
| 370 | my ($self, $super_node, $super_name); | ||
| 371 | |||
| 372 | $self = { | ||
| 373 | name => $cname, | ||
| 374 | parent => 'ComplexType', | ||
| 375 | ancestors => [ ], | ||
| 376 | members => [ ] | ||
| 377 | }; | ||
| 378 | |||
| 379 | # Determine class parent | ||
| 380 | $super_node = $node->getElementsByLocalName('extension')->shift; | ||
| 381 | if (defined $super_node) { | ||
| 382 | $super_name = $super_node->getAttribute('base'); | ||
| 383 | $super_name =~ s/^vim25://; | ||
| 384 | } | ||
| 385 | $self->{'parent'} = $super_name if defined $super_name; | ||
| 386 | |||
| 387 | # Iterate class members | ||
| 388 | foreach my $member_node ( $node->getElementsByLocalName('element') ) { | ||
| 389 | my ($name, $type, $min, $max, $is_array, $is_mandatory); | ||
| 390 | $name = $member_node->getAttribute('name'); | ||
| 391 | $type = $member_node->getAttribute('type'); | ||
| 392 | $min = $member_node->getAttribute('minOccurs'); | ||
| 393 | $max = $member_node->getAttribute('maxOccurs'); | ||
| 394 | |||
| 395 | # type | ||
| 396 | $type =~ s/^vim25://; | ||
| 397 | if ($type =~ m/^xsd:boolean/) { | ||
| 398 | $type = 'boolean'; | ||
| 399 | } | ||
| 400 | if ($type =~ m/^xsd:anyType/) { | ||
| 401 | $type = 'anyType'; | ||
| 402 | } | ||
| 403 | if ($type =~ m/^xsd:/) { | ||
| 404 | $type = undef; | ||
| 405 | } | ||
| 406 | |||
| 407 | # array? | ||
| 408 | $is_array = defined $max ? 1 : 0; | ||
| 409 | |||
| 410 | # mandatory? | ||
| 411 | $is_mandatory = defined $min and $min eq "0" ? 0 : 1; | ||
| 412 | |||
| 413 | push @{ $self->{'members'} }, [$name, $type, $is_array, $is_mandatory]; | ||
| 414 | } | ||
| 415 | |||
| 416 | return bless $self, $class; | ||
| 417 | } | ||
| 418 | |||
| 419 | sub write_file { | ||
| 420 | my ($self, $namespace, $path) = @_; | ||
| 421 | my ($class_name, $super_name, $fname, $class_definition, @ancestors, @members); | ||
| 422 | |||
| 423 | $fname = $path . "/" . $self->{'name'} . ".pm"; | ||
| 424 | open FILE, ">$fname" or die "failed to open $fname: $!"; | ||
| 425 | |||
| 426 | $class_name = $self->{'name'}; | ||
| 427 | $super_name = $self->{'parent'}; | ||
| 428 | |||
| 429 | $class_definition .= "package $ns::$class_name;\n"; | ||
| 430 | $class_definition .= "use parent '$ns::$super_name';\n\n"; | ||
| 431 | $class_definition .= "use strict;\nuse warnings;\n\n"; | ||
| 432 | |||
| 433 | # Bug with OptionDef, present in 5.5 and possibly older versions? Current VMODL doesn't define DynamicData | ||
| 434 | # and will raise runtime errors when interfacing with vSphere prior to 6.0 | ||
| 435 | # https://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&externalId=2101118 | ||
| 436 | if ($class_name eq 'DynamicData') { | ||
| 437 | push @{ $self->{'members'} }, ['dynamicProperty', 'DynamicProperty', 1, 0]; | ||
| 438 | push @{ $self->{'members'} }, ['dynamicType', undef, 0, 0]; | ||
| 439 | } | ||
| 440 | |||
| 441 | # class ancestors | ||
| 442 | @ancestors = @{ $self->{'ancestors'} }; | ||
| 443 | $class_definition .= "our \@class_ancestors = ( "; | ||
| 444 | if (scalar @ancestors > 0) { | ||
| 445 | $class_definition .= "\n"; | ||
| 446 | foreach my $ancestor (@ancestors) { | ||
| 447 | $class_definition .= " '$ancestor',\n"; | ||
| 448 | } | ||
| 449 | } | ||
| 450 | $class_definition .= ");\n\n"; | ||
| 451 | |||
| 452 | # class members | ||
| 453 | @members = @{ $self->{'members'} }; | ||
| 454 | $class_definition .= "our \@class_members = ( "; | ||
| 455 | if (scalar @members > 0) { | ||
| 456 | $class_definition .= "\n"; | ||
| 457 | foreach my $member (@members) { | ||
| 458 | my ($name, $type, $is_array, $is_mandatory) = @$member; | ||
| 459 | |||
| 460 | $type = defined $type ? "'$type'" : 'undef'; | ||
| 461 | $class_definition .= " ['$name', $type, $is_array, $is_mandatory],\n"; | ||
| 462 | } | ||
| 463 | } | ||
| 464 | |||
| 465 | $class_definition .= ");\n\n"; | ||
| 466 | $class_definition .= "sub get_class_ancestors {\n"; | ||
| 467 | $class_definition .= " return \@class_ancestors;\n"; | ||
| 468 | $class_definition .= "}\n\n"; | ||
| 469 | $class_definition .= "sub get_class_members {\n"; | ||
| 470 | $class_definition .= " my \$class = shift;\n"; | ||
| 471 | $class_definition .= " my \@super_members = \$class->SUPER::get_class_members();\n"; | ||
| 472 | $class_definition .= " return (\@super_members, \@class_members);\n"; | ||
| 473 | $class_definition .= "}\n\n1;\n"; | ||
| 474 | |||
| 475 | print FILE $class_definition; | ||
| 476 | close FILE; | ||
| 477 | } | ||
| 478 | |||
| 479 | 1; |