rspec2genilib.in 20.1 KB
Newer Older
1 2 3
#!/usr/bin/perl -w

#
Leigh Stoller's avatar
Leigh Stoller committed
4
# Copyright (c) 2000-2017 University of Utah and the Flux Group.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
# 
# {{{EMULAB-LICENSE
# 
# This file is part of the Emulab network testbed software.
# 
# This file is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
# 
# This file is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
# License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this file.  If not, see <http://www.gnu.org/licenses/>.
# 
# }}}
#
use strict;
use English;
use Getopt::Std;
use File::Temp qw(tempfile :mktemp :POSIX );
use POSIX qw(:signal_h);
use POSIX ":sys_wait_h";
use File::stat;
use Data::Dumper;

#
# Convert rspec to geni lib.
#
sub usage()
{
Leigh Stoller's avatar
Leigh Stoller committed
39 40
    print STDERR "Usage: rspec2genilib ".
	"[-o filename] [[-r] -s rspecfile] rspec\n";
41
    print STDERR "Options:\n";
42 43 44
    print STDERR "  -d       - Turn on debugging\n";
    print STDERR "  -o file  - Specify output file for geni-lib\n";
    print STDERR "  -s file  - Specify output file for post geni-lib rspec\n";
45
    print STDERR "  -r       - Regression test; run geni-lib, compare rspecs\n";
Leigh Stoller's avatar
Leigh Stoller committed
46
    print STDERR "  -t       - Do not add stub docstring (for regression)\n";
47
    print STDERR "  -p       - Permissive mode, ignore unsupported stuff\n";
48 49
    exit(-1);
}
50
my $optlist    = "do:rs:tp";
51
my $debug      = 0;
Leigh Stoller's avatar
Leigh Stoller committed
52
my $regress    = 0;
Leigh Stoller's avatar
Leigh Stoller committed
53
my $nodocstr   = 0;
54
my $permissive = 0;
55
my $ofile;
Leigh Stoller's avatar
Leigh Stoller committed
56
my $rfile;
57 58 59 60

#
# Configure variables
#
Leigh Stoller's avatar
Leigh Stoller committed
61 62 63 64
my $TB         = "@prefix@";
my $TBOPS      = "@TBOPSEMAIL@";
my $XMLLINT    = "/usr/local/bin/xmllint";
my $RUNGENILIB = "$TB/bin/rungenilib";
65 66 67 68 69 70

# Locals
my $rspecfile;

# Protos
sub fatal($);
Leigh Stoller's avatar
Leigh Stoller committed
71
sub GetTextOrFail($$);
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91

#
# Turn off line buffering on output
#
$| = 1;

#
# Untaint the path
# 
$ENV{'PATH'} = "$TB/bin:$TB/sbin:/bin:/usr/bin:/sbin:/usr/sbin";
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};

if ($UID == 0) {
    die("Please do not run this as root!");
}

#
# Testbed Support libraries
#
use lib "@prefix@/lib";
Leigh Stoller's avatar
Leigh Stoller committed
92
use emutil;
93
use GeniXML;
Leigh Stoller's avatar
Leigh Stoller committed
94 95
use GeniHRN;
use APT_Rspec;
96 97 98 99 100 101 102 103 104 105 106 107

#
# Parse command arguments. Once we return from getopts, all that should
# left are the required arguments.
#
my %options = ();
if (! getopts($optlist, \%options)) {
    usage();
}
if (defined($options{"d"})) {
    $debug = 1;
}
108
if (defined($options{"t"})) {
Leigh Stoller's avatar
Leigh Stoller committed
109
    $nodocstr = 1;
110
}
111 112 113
if (defined($options{"o"})) {
    $ofile = $options{"o"};
}
114 115 116
if (defined($options{"p"})) {
    $permissive = 1;
}
Leigh Stoller's avatar
Leigh Stoller committed
117
if (defined($options{"r"})) {
Leigh Stoller's avatar
Leigh Stoller committed
118 119
    $regress  = 1;
    $nodocstr = 1;
Leigh Stoller's avatar
Leigh Stoller committed
120 121 122 123
    if (defined($options{"s"})) {
	$rfile = $options{"s"};
    }
}
124 125 126 127 128 129
if (@ARGV != 1) {
    usage();
}
$rspecfile = $ARGV[0];

#
Leigh Stoller's avatar
Leigh Stoller committed
130
# Must taint check.
131 132 133 134 135 136 137
#
if ($rspecfile =~ /^([-\w\/\.]+)$/) {
    $rspecfile = $1;
}
else {
    die("Bad data in argument: $rspecfile.");
}
Leigh Stoller's avatar
Leigh Stoller committed
138

139 140 141
my $xmlerrors = `$XMLLINT --noout $rspecfile 2>&1`;
if ($?) {
    print STDERR $xmlerrors;
142
    fatal("rspec is not well formed");    
143
}
144
my $rspec = APT_Rspec->new($rspecfile, $permissive);
Leigh Stoller's avatar
Leigh Stoller committed
145 146
fatal("Could not create rspec object")
    if (!defined($rspec));
147 148

#
Leigh Stoller's avatar
Leigh Stoller committed
149
# Generate statements for each node.
150
#
Leigh Stoller's avatar
Leigh Stoller committed
151
sub GenerateNodeStatements($)
152
{
Leigh Stoller's avatar
Leigh Stoller committed
153
    my ($rspec) = @_;
154

155
    foreach my $node (@{$rspec->nodelist()}) {
Leigh Stoller's avatar
Leigh Stoller committed
156 157 158
	my $client_id = $node->client_id();
	my $ntag      = $node->tag();
	my $ntype     = $node->type();
159
	
Leigh Stoller's avatar
Leigh Stoller committed
160 161 162 163 164
	#
	# Create the nodes.
	#
	if ($ntype eq "emulab-xen") {
	    $node->addStatement("$ntag = request.XenVM('$client_id')");
165 166 167 168 169 170 171 172

	    #
	    # This is the only time we need to spit this out, since
	    # the default is False.
	    #
	    if (defined($node->{'exclusive'}) && $node->{'exclusive'}) {
		$node->addTagStatement("exclusive = True");
	    }
173
	    
Leigh Stoller's avatar
Leigh Stoller committed
174 175
	    if (defined($node->{'xen_settings'})) {
		my $settings = $node->{'xen_settings'};
176 177

		foreach my $setting (sort(keys(%{$settings}))) {
Leigh Stoller's avatar
Leigh Stoller committed
178 179 180 181 182 183 184 185 186 187
		    my $value = $settings->{$setting};
		    if ($setting eq "ram") {
			$node->addTagStatement("ram = $value");
		    }
		    elsif ($setting eq "cores") {
			$node->addTagStatement("cores = $value");
		    }
		    elsif ($setting eq "disk") {
			$node->addTagStatement("disk = $value");
		    }
188 189
		}
	    }
Leigh Stoller's avatar
Leigh Stoller committed
190 191 192 193 194 195 196 197
	    if (defined($node->{'xen_ptype'})) {
		my $ptype = $node->{'xen_ptype'};
		$node->addTagStatement("xen_ptype = '$ptype'");
	    }
	    if (defined($node->{'instantiate_on'})) {
		my $vhost = $node->{'instantiate_on'};
		$node->addTagStatement("InstantiateOn('$vhost')");
	    }
198
	}
Leigh Stoller's avatar
Leigh Stoller committed
199 200 201 202 203 204 205 206 207 208 209 210 211 212
	elsif ($ntype eq "delay") {
	    #
	    # Bridges are also special, see comment above for blockstore.
	    #
	    my ($iface0, $iface1) = values(%{$node->{'ifaces'}});
	    my (undef,$if0) = split(":", $iface0->{'client_id'});
	    my (undef,$if1) = split(":", $iface1->{'client_id'});

	    $node->addStatement(
		"$ntag = request.Bridge('$client_id', '$if0', '$if1')");

	    #
	    # And the pipes. Just two of them.
	    #
213 214 215
	    foreach my $k (sort(keys(%{$node->pipes()}))) {
		my $p = $node->pipes()->{$k};
		
Leigh Stoller's avatar
Leigh Stoller committed
216 217 218 219 220 221 222 223 224 225
		my $pname = ($p->{'iface_id'} eq $if0
			     ? "pipe('$if0')" : "pipe('$if1')");

		$node->addTagStatement("${pname}.bandwidth = ". $p->capacity())
		    if (defined($p->capacity()));
		$node->addTagStatement("${pname}.latency = " . $p->latency())
		    if (defined($p->latency()));
		$node->addTagStatement("${pname}.lossrate = " . $p->lossrate())
		    if (defined($p->lossrate()));
	    }
226
	}
Leigh Stoller's avatar
Leigh Stoller committed
227 228 229 230 231 232 233
	elsif ($ntype eq "emulab-blockstore") {
	    # attributes.
	    my $bstore = $node->{'blockstores'}->{$client_id};
	    my $mount  = ($bstore->{'mount'} ?
			  "'" . $bstore->{'mount'} . "'" : "None");
	    # The node and the blockstore are the same.
	    $ntag = $node->{'tag'} = $bstore->{'tag'};
234
	    
235
	    #
Leigh Stoller's avatar
Leigh Stoller committed
236 237 238 239 240
	    # Blockstores are special. We want to use the same iface names
	    # from the rspec, not the internal names that geni-lib uses,
	    # This ensures that the rspec->geni-lib->rspec ... path uses
	    # consistent naming. And we know that the blockstore has only one
	    # interface, so that makes it easier.
241
	    #
Leigh Stoller's avatar
Leigh Stoller committed
242 243 244
	    my ($iface) = values(%{$node->{'ifaces'}});
	    my ($tmp,$if) = split(":", $iface->{'client_id'});
	    $if = $tmp if (!defined($if));
245

Leigh Stoller's avatar
Leigh Stoller committed
246 247
	    $node->addStatement(
		"$ntag = request.RemoteBlockstore('$client_id',$mount,'$if')");
248
	}
Leigh Stoller's avatar
Leigh Stoller committed
249 250
	else {
	    $node->addStatement("$ntag = request.RawPC('$client_id')");
251 252
	}

Leigh Stoller's avatar
Leigh Stoller committed
253 254 255 256 257 258
	#
	# Attributes and flags.
	#
	if (defined($node->{'component_id'})) {
	    my $component_id = $node->{'component_id'};
	    $node->addTagStatement("component_id = '$component_id'");
259
	}
Leigh Stoller's avatar
Leigh Stoller committed
260 261 262
	if (defined($node->{'component_manager_id'})) {
	    my $manager_id = $node->{'component_manager_id'};
	    $node->addTagStatement("component_manager_id = '$manager_id'");
263
	}
Leigh Stoller's avatar
Leigh Stoller committed
264 265
	if ($node->{'routable_control_ip'}) {
	    $node->addTagStatement("routable_control_ip = True");
266
	}
Leigh Stoller's avatar
Leigh Stoller committed
267 268 269
	if (defined($node->{'hardware_type'})) {
	    my $htype = $node->{'hardware_type'};
	    $node->addTagStatement("hardware_type = '$htype'");
270
	}
Leigh Stoller's avatar
Leigh Stoller committed
271 272 273
	if (defined($node->{'disk_image'})) {
	    my $name = $node->{'disk_image'};
	    $node->addTagStatement("disk_image = '$name'");
Leigh Stoller's avatar
Leigh Stoller committed
274
	}
275 276 277 278
	if (defined($node->{'adb_target'})) {
	    my $target_id = $node->{'adb_target'};
	    $node->addTagStatement("adb_target = '$target_id'");
	}
Leigh Stoller's avatar
Leigh Stoller committed
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
	if (defined($node->{'jacks_site'})) {
	    my $site = $node->{'jacks_site'};
	    $node->addTagStatement("Site('$site')");
	}

	#
	# Services.
	#
	foreach my $service (@{ $node->{'services'} }) {
	    my $type = $service->{'type'};

	    SWITCH: for (lc($type)) {
		/^install$/i && do {
		    my $url  =  $service->{'url'};
		    my $path =  $service->{'path'};
		    $node->addTagStatement(
			"addService(pg.Install('$url','$path'))");
		    last SWITCH;
		};
		/^execute$/i && do {
		    my $shell = $service->{'shell'};
		    my $cmd   = $service->{'cmd'};
301
		    $cmd =~ s/\'/\\'/g;
Leigh Stoller's avatar
Leigh Stoller committed
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
		    $node->addTagStatement(
			"addService(pg.Execute('$shell','$cmd'))");
		    last SWITCH;
		};
		/^program-agent$/i && do {
		    my $vname       = $service->{"name"};
		    my $command     = $service->{"command"};
		    my $directory   = $service->{"directory"};
		    my $onexpstart  = $service->{"onexpstart"} ? "True":"False";
		    if (defined($directory) && $directory ne "") {
			$directory = "'" . $directory . "'";
		    }
		    else {
			$directory = "None";
		    }
		    $node->addTagStatement(
			"addService(emulab.ProgramAgent('$vname', ".
			"'$command', $directory, $onexpstart))");
		    last SWITCH;
		};
322 323
	    }
	}
324
	#
Leigh Stoller's avatar
Leigh Stoller committed
325
	# Desires.
326
	#
327
	foreach my $desire (sort(keys(%{ $node->{'desires'} }))) {
Leigh Stoller's avatar
Leigh Stoller committed
328 329
	    my $weight = $node->{'desires'}->{$desire};
	    $node->addTagStatement("Desire('$desire','$weight')");
330
	}
Leigh Stoller's avatar
Leigh Stoller committed
331
	
332
	#
Leigh Stoller's avatar
Leigh Stoller committed
333
	# Interfaces.
334
	#
335 336
	foreach my $iface (@{$node->ifacelist()}) {
	    my $iface_id  = $iface->{'client_id'};
Leigh Stoller's avatar
Leigh Stoller committed
337 338 339 340 341 342 343 344
	    my $itag      = $iface->{'tag'};
	    my $ip        = $iface->{'ip'};
	    my $mask      = $iface->{'mask'};

	    #
	    # geni-lib is going to name the interfaces as node_id:iface_id
	    # so we have to careful to strip existing node_id from the id
	    # in case the rspec came from a geni-lib script. But geni-lib
345 346 347
	    # will not prepend the node_id if it is already in : format,
	    # so if the user named it whacky:iface_id, we are going to
	    # use that.
Leigh Stoller's avatar
Leigh Stoller committed
348
	    #
349 350 351 352 353
	    if ($iface_id =~ /^([^:]*):(.*)$/) {
		if ($1 eq $client_id) {
		    $iface_id = $2;
		}
	    }
Leigh Stoller's avatar
Leigh Stoller committed
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
	    my $addr = ($ip && $mask ? "pg.IPv4Address('$ip','$mask')" : undef);

	    #
	    # Again, bridges and blockstores are special. We have to find
	    # the internally created interface, not add another one.
	    #
	    if ($ntype eq "delay") {
		$node->addStatement("$itag = ${ntag}.interface('$iface_id')");
		if ($addr) {
		    $node->addStatement("${itag}.addAddress($addr)");
		}
	    }
	    elsif ($ntype eq "emulab-blockstore") {
		$node->addStatement("$itag = ${ntag}.interface");
	    }
	    else {
		$node->addStatement("$itag = ${ntag}.addInterface('$iface_id'" .
				    (defined($addr) ? ", $addr" : "") . ")");
	    }
	}
	foreach my $id (sort(keys(%{$node->{'blockstores'}}))) {
	    my $blockstore = $node->{'blockstores'}->{$id};
	    my $bsname     = $blockstore->{'name'};
	    my $btag       = $blockstore->{'tag'};
	    my $mount      = ($blockstore->{'mount'} ?
			      "'" . $blockstore->{'mount'} . "'" : "None");

	    # Create local blockstore on node.
	    if ($ntype ne "emulab-blockstore") {
		$node->addStatement(
		    "$btag = ${ntag}.Blockstore('$bsname', $mount)");
	    }
	    # Now the attributes.
	    my $readonly  = $blockstore->{'readonly'};
	    my $size      = $blockstore->{'size'};
	    my $placement = $blockstore->{'placement'};
390
	    my $rwclone   = $blockstore->{'rwclone'};
Leigh Stoller's avatar
Leigh Stoller committed
391 392 393 394 395 396 397 398
	    my $dataset   = $blockstore->{'dataset'};

	    if (defined($readonly) && $readonly) {
		$node->addStatement("${btag}.readonly = True");
	    }
	    if (defined($size)) {
		$node->addStatement("${btag}.size = '$size'");
	    }
399 400
	    # Do not not spit out default.
	    if (defined($placement) && $placement ne "any") {
Leigh Stoller's avatar
Leigh Stoller committed
401 402 403 404
		$node->addStatement("${btag}.placement = '$placement'");
	    }
	    if (defined($dataset)) {
		$node->addStatement("${btag}.dataset = '$dataset'");
405
	    }
406 407 408 409
	    # Do not not spit out default (false).
	    if (defined($rwclone) && $rwclone) {
		$node->addStatement("${btag}.rwclone = True");
	    }
410 411 412
	}
    }
}
Leigh Stoller's avatar
Leigh Stoller committed
413 414 415
sub SpitNodeStatements($$)
{
    my ($rspec, $where) = @_;
416

417
    foreach my $node (@{$rspec->nodelist()}) {
Leigh Stoller's avatar
Leigh Stoller committed
418 419 420 421 422 423
	my $client_id = $node->client_id();
	print $where "# Node $client_id\n";
	foreach my $statement (@{$node->statements()}) {
	    print $where "$statement\n";
	}
	print $where "\n";
424 425 426 427
    }
}

#
Leigh Stoller's avatar
Leigh Stoller committed
428
# Generate statements for each node.
429
#
Leigh Stoller's avatar
Leigh Stoller committed
430 431 432 433
sub GenerateLinkStatements($)
{
    my ($rspec) = @_;
    
434
    foreach my $link (@{$rspec->linklist()}) {
Leigh Stoller's avatar
Leigh Stoller committed
435 436 437 438 439 440 441 442 443 444 445 446 447
	my $client_id = $link->client_id();
	my $ltag      = $link->tag();
	my $ltype     = $link->type();

	if (defined($ltype)) {
	    if (0 && $ltype eq "lan") {
		$link->addStatement("$ltag = request.LAN('$client_id')");
	    }
	    elsif ($ltype eq "vlan" || $ltype eq "lan") {
		$link->addStatement(
		    "$ltag = request.Link('$client_id', 'vlan')");
	    }
	    elsif ($ltype eq "egre-tunnel") {
448
		$link->addStatement("$ltag = request.L2GRE('$client_id')");
Leigh Stoller's avatar
Leigh Stoller committed
449 450
	    }
	    elsif ($ltype eq "gre-tunnel") {
451
		$link->addStatement("$ltag = request.L3GRE('$client_id')");
Leigh Stoller's avatar
Leigh Stoller committed
452 453 454 455 456 457 458 459
	    }
	    else {
		fatal("Unknown link type $ltype for $client_id");
	    }
	}
	else {
	    $link->addStatement("$ltag = request.Link('$client_id')");
	}
460
	#
Leigh Stoller's avatar
Leigh Stoller committed
461
	# Link properties
462
	#
Leigh Stoller's avatar
Leigh Stoller committed
463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515
	if (defined($link->{'component_id'})) {
	    my $id = $link->{'component_id'};
	    $link->addTagStatement("component_id = '$id'");
	}
	if (defined($link->{'component_manager_id'})) {
	    my $id = $link->{'component_manager_id'};
	    $link->addTagStatement("component_manager_id = '$id'");
	}
	if (defined($link->{'openflow_controller'})) {
	    my $host = $link->{'openflow_controller'}->{'host'};
	    my $port = $link->{'openflow_controller'}->{'port'};
	    $link->addTagStatement(
			     "addChild(emulab.OFController('$host', $port))");
	}
	if (defined($link->{'best_effort'})) {
	    $link->addTagStatement("best_effort = True");
	}
	if (defined($link->{'shared_vlan'})) {
	    my $name = $link->{'shared_vlan'};
	    $link->addTagStatement("connectSharedVlan('$name')");
	}
	if (defined($link->{'force_shaping'})) {
	    $link->addTagStatement("setForceShaping()");
	}
	if (defined($link->{'trivial_ok'})) {
	    $link->addTagStatement("trivial_ok = True");
	}
	if (defined($link->{'vlan_tagging'})) {
	    my $istagged = ($link->{'vlan_tagging'} ? "True" : "False");
	    $link->addTagStatement("vlan_tagging = $istagged");
	}
	if (defined($link->{'link_multiplexing'})) {
	    my $emulated = ($link->{'link_multiplexing'} ? "True" : "False");
	    $link->addTagStatement("link_multiplexing = $emulated");
	}
	if (defined($link->{'interswitch'}) && !$link->{'interswitch'}) {
	    $link->addTagStatement("setNoInterSwitchLinks()");
	}
	if (defined($link->{'protocol'})) {
	    my $protocol = $link->{'protocol'};
	    $link->addTagStatement("protocol = '$protocol'");
	}
	if (defined($link->{'nomac_learning'})) {
	    $link->addTagStatement("disableMACLearning()");
	}
	if (defined($link->{'jacks_site'})) {
	    my $site = $link->{'jacks_site'};
	    $link->addTagStatement("Site('$site')");
	}
	# Managers
	foreach my $urn (@{$link->{'component_managers'}}) {
	    $link->addTagStatement("addComponentManager('$urn')");
	}
516 517
	
	#
Leigh Stoller's avatar
Leigh Stoller committed
518
	# Shaping.
519
	#
520
	foreach my $key (sort(keys(%{$link->properties()}))) {
Leigh Stoller's avatar
Leigh Stoller committed
521 522 523 524 525 526 527 528 529 530 531
	    my $property = $link->{'properties'}->{$key};
	    my $dest     = $property->{'dest'};
	    my $iface    = $rspec->getIface($property->source());

	    foreach my $p ("bandwidth", "latency", "plr") {
		if (defined($property->{$p})) {
		    my $pval = $property->{$p};
		    $iface->addTagStatement("$p = $pval");
		}
	    }
	}
532

533
	foreach my $iface (@{$link->ifacelist()}) {
Leigh Stoller's avatar
Leigh Stoller committed
534 535
	    my $client_id = $iface->{'client_id'};
	    my $itag      = $iface->{'tag'};
536

Leigh Stoller's avatar
Leigh Stoller committed
537 538 539 540
	    foreach my $statement (@{$iface->{'statements'}}) {
		$link->addStatement($statement);
	    }
	    $link->addTagStatement("addInterface($itag)");
541 542
	}
    }
Leigh Stoller's avatar
Leigh Stoller committed
543 544 545 546
}
sub SpitLinkStatements($$)
{
    my ($rspec, $where) = @_;
547

548
    foreach my $link (@{$rspec->linklist()}) {
Leigh Stoller's avatar
Leigh Stoller committed
549 550 551 552
	my $client_id = $link->client_id();
	print $where "# Link $client_id\n";
	foreach my $statement (@{$link->statements()}) {
	    print $where "$statement\n";
553
	}
Leigh Stoller's avatar
Leigh Stoller committed
554
	print $where "\n";
555
    }
Leigh Stoller's avatar
Leigh Stoller committed
556 557 558 559 560 561
}

sub SpitTopLevelStatements($$)
{
    my ($rspec, $where) = @_;

562 563 564
    foreach my $attribute (@{$rspec->toplevel_elements()}) {
	my $name = $attribute->name();
	my $val  = $attribute->value();
Leigh Stoller's avatar
Leigh Stoller committed
565 566 567
	
        SWITCH: for (lc($name)) {
	    /^password$/i && do {
568
		print $where "password = emulab.Password('$val')\n";
Leigh Stoller's avatar
Leigh Stoller committed
569 570 571 572
		print $where "request.addResource(password)\n";
		last SWITCH;
	    };
	    /^routable_pool$/i && do {
573 574 575 576 577
		my $pool = $val;
		my $id    = $pool->client_id();
		my $count = $pool->count();
		my $type  = $pool->type();
		print $where "request.AddressPool('$id', $count, '$type')\n";
Leigh Stoller's avatar
Leigh Stoller committed
578 579 580
		last SWITCH;
	    };
	    /^collocate_factor$/i && do {
581
		print $where "request.setCollocateFactor($val)\n";
Leigh Stoller's avatar
Leigh Stoller committed
582 583 584
		last SWITCH;
	    };
	    /^packing_strategy$/i && do {
585
		print $where "request.setPackingStrategy('$val')\n";
Leigh Stoller's avatar
Leigh Stoller committed
586 587 588
		last SWITCH;
	    };
	    /^routing_style$/i && do {
589
		print $where "request.setRoutingStyle('$val')\n";
Leigh Stoller's avatar
Leigh Stoller committed
590 591 592
		last SWITCH;
	    };
	    /^delay_image$/i && do {
593
		print $where "request.setDelayImage('$val')\n";
Leigh Stoller's avatar
Leigh Stoller committed
594 595 596
		last SWITCH;
	    };
	    fatal("toplevel element $name is not supported");
597 598 599
	}
    }
}
Leigh Stoller's avatar
Leigh Stoller committed
600 601 602 603 604 605 606

sub SpitTour($$)
{
    my ($rspec, $where) = @_;
    my $description  = $rspec->description();
    my $instructions = $rspec->instructions();

Leigh Stoller's avatar
Leigh Stoller committed
607 608 609 610 611
    return
	if ($nodocstr &&
	    ! ((defined($description) && $description->{"text"} ne "") ||
	       (defined($instructions) && $instructions->{"text"} ne "")));

Leigh Stoller's avatar
Leigh Stoller committed
612 613 614 615
    my $docstring = '"""';

    if (defined($description)) {
	$docstring .= $description->{'text'};
616 617
    }
    else {
Leigh Stoller's avatar
Leigh Stoller committed
618 619 620 621 622 623
	if ($nodocstr) {
	    $docstring .= "\n";
	}
	else {
	    $docstring .= "Please give this script a description.";
	}
624
    }
Leigh Stoller's avatar
Leigh Stoller committed
625
    if (defined($instructions)) {
Leigh Stoller's avatar
Leigh Stoller committed
626 627
	my $text = $instructions->{'text'};
	$docstring .= "\n\n" . "Instructions:" . "\n" . $text;
628
    }
Leigh Stoller's avatar
Leigh Stoller committed
629 630 631
    $docstring .= '"""';
    print $where $docstring . "\n\n";
}
632

Leigh Stoller's avatar
Leigh Stoller committed
633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653
sub SpitSteps($$)
{
    my ($rspec, $where) = @_;
    my $steps = $rspec->steps();
    return
	if (!defined($steps) || !scalar(@{$steps}));

    print $where "# Create a Tour object and the steps to it\n";
    print $where "tour = emulab.Tour()\n";
    print $where "tour.useDocstring()\n";
    foreach my $step (@{$steps}) {
	my $type = $step->{"type"};
	my $id   = $step->{"id"};
	my $desc = $step->{"description"};
	$desc =~ s/\"/\\"/g;
	print $where "tour.addStep('$id', '$type', \"$desc\")\n";
    }
    print $where "request.addTour(tour)\n\n\n";
    
}

Leigh Stoller's avatar
Leigh Stoller committed
654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715
sub SpitPreamble($)
{
    my ($where) = @_;

    print $where
	"#\n" .
	"# NOTE: This code was machine converted. An actual human would not\n".
	"#       write code like this!\n".
	"#\n\n" .
	"# Import the Portal object.\n" .
	"import geni.portal as portal\n" .
	"# Import the ProtoGENI library.\n" .
	"import geni.rspec.pg as pg\n" .
	"# Import the Emulab specific extensions.\n" .
	"import geni.rspec.emulab as emulab\n" .
	"\n" .
	"# Create a portal object,\n".
	"pc = portal.Context()\n\n".
	"# Create a Request object to start building the RSpec.\n".
	"request = pc.makeRequestRSpec()\n\n";
}

#
# Regression test the geni-lib code by running it and comparing it.
#
sub RunRegression($$$)
{
    my ($rspecfile, $genilibfile, $outrspecfile) = @_;

    my $output = emutil::ExecQuiet("$RUNGENILIB $genilibfile");
    if ($?) {
	print STDERR $output;
	print STDERR "*** $RUNGENILIB failed\n";
	return -1;
    }
    if ($debug) {
	print $output;
    }
    my $rspec1 = eval { APT_Rspec->new($rspecfile) };
    if ($@) {
	print STDERR $@;
	print STDERR "*** Could not parse source rspec into object\n";
	return -1;
    }
    my $rspec2 = eval { APT_Rspec->new($output) };
    if ($@) {
	print STDERR $@;
	print STDERR "*** Could not parse output rspec into object\n";
	return -1;
    }
    if ($rspec1->Compare($rspec2)) {
	print STDERR "*** rspec comparison failed\n";
	return -1;
    }
    if ($outrspecfile) {
	if (open(RS, ">$outrspecfile")) {
	    print RS $output;
	    close(RS);
	}
	else {
	    print STDERR "Could not open outputfile for rspec\n";
	    return -1;
716 717
	}
    }
Leigh Stoller's avatar
Leigh Stoller committed
718
    return 0;
719
}
Leigh Stoller's avatar
Leigh Stoller committed
720
    
721
#
Leigh Stoller's avatar
Leigh Stoller committed
722
# Well no errors, lets generate the geni code for the nodes and links.
723
#
Leigh Stoller's avatar
Leigh Stoller committed
724 725
GenerateNodeStatements($rspec);
GenerateLinkStatements($rspec);
726 727

#
Leigh Stoller's avatar
Leigh Stoller committed
728 729 730 731 732 733 734
# Spew rspec to file.
# 
my ($outfd, $filename) = tempfile("/tmp/rspec2genilibXXXXX", UNLINK => 1);
if (!defined($outfd)) {
    fatal("Could not open temporary file for result rspec");
    return -1;
}
Leigh Stoller's avatar
Leigh Stoller committed
735
SpitTour($rspec, $outfd);
Leigh Stoller's avatar
Leigh Stoller committed
736
SpitPreamble($outfd);
Leigh Stoller's avatar
Leigh Stoller committed
737
SpitSteps($rspec, $outfd);
Leigh Stoller's avatar
Leigh Stoller committed
738 739 740 741 742 743
SpitNodeStatements($rspec, $outfd);
SpitLinkStatements($rspec, $outfd);
SpitTopLevelStatements($rspec, $outfd);
print $outfd "\n";
print $outfd "# Print the generated rspec\n";
print $outfd "pc.printRequestRSpec(request)\n";
744

Leigh Stoller's avatar
Leigh Stoller committed
745 746 747
if ($regress) {
    if (RunRegression($rspecfile, $filename, $rfile)) {
	exit(-1);
748 749
    }
}
750

751
if (defined($ofile)) {
Leigh Stoller's avatar
Leigh Stoller committed
752
    system("/bin/cat $filename > $ofile");
753 754
}
else {
Leigh Stoller's avatar
Leigh Stoller committed
755
    system("/bin/cat $filename");
756
}
757 758 759 760 761 762
exit(0);

sub fatal($) {
    my ($mesg) = $_[0];

    print STDERR "*** $0:\n".
Leigh Stoller's avatar
Leigh Stoller committed
763
	         "*** $mesg\n";
764 765
    exit(-1);
}