rspec2genilib.in 19.9 KB
Newer Older
1 2 3 4 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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
#!/usr/bin/perl -w

#
# Copyright (c) 2000-2016 University of Utah and the Flux Group.
# 
# {{{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()
{
    print STDERR "Usage: rspec2genilib [-o filename] rspec\n";
    print STDERR "Options:\n";
    print STDERR"  -o file  : Specify output file\n";
    exit(-1);
}
my $optlist    = "do:";
my $debug      = 0;
my $ofile;

#
# Configure variables
#
my $TB       = "@prefix@";
my $TBOPS    = "@TBOPSEMAIL@";
my $XMLLINT  = "/usr/local/bin/xmllint";

# Locals
my $rspecfile;
57 58
my $NodeCounter  = 0;
my $BsCounter    = 0;
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121

# Protos
sub fatal($);

#
# 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";
use GeniXML;

#
# 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;
}
if (defined($options{"o"})) {
    $ofile = $options{"o"};
}
if (@ARGV != 1) {
    usage();
}
$rspecfile = $ARGV[0];

#
# Must taint check!
#
if ($rspecfile =~ /^([-\w\/\.]+)$/) {
    $rspecfile = $1;
}
else {
    die("Bad data in argument: $rspecfile.");
}
my $xmlerrors = `$XMLLINT --noout $rspecfile 2>&1`;
if ($?) {
    print STDERR $xmlerrors;
    Fatal("rspec is not well formed");
}
my $rspec = GeniXML::ParseFile($rspecfile);
if (! defined($rspec)) {
    Fatal("Could not parse rspec");
}
my $genilib =
122 123 124 125 126
    "#\n" .
    "# NOTE: This code was machine converted from an Emulab experiment that\n".
    "#       was originally written in NS syntax. An actual human would never\n".
    "#       write code like this!\n".
    "#\n" .
127
    "import geni.portal\n" .
128
    "import geni.rspec.pg as RSPEC\n" .
129 130
    "import geni.rspec.igext as IG\n" .
    "\n" .
131 132 133 134
    "rspec  = RSPEC.Request()\n" .
    "portal = geni.portal.Context()\n\n";

sub AddStatement($)
135 136 137 138 139
{
    $genilib .= $_[0] . "\n";
}

#
140
# Top level elements.
141
#
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
my %Nodes       = ();
my %Ifaces      = ();
my %Links       = ();

sub AddNode($$) {
    my ($client_id, $type) = @_;

    $Nodes{$client_id} = {
	"client_id"    => $client_id,
	"type"         => $type,
	"statements"   => [],
	"ifaces"       => {},
	"blockstores"  => {},
	"pipes"        => {},
	"tag"          => "node" . $NodeCounter++,
    };
    return $Nodes{$client_id};
}
sub AddNodeStatement($$) {
    push(@{$Nodes{$_[0]}->{'statements'}}, $_[1]);
}
sub AddIface($$$$) {
    my ($node_id, $client_id, $ip, $mask) = @_;
    
    $Ifaces{$client_id} = {
	"client_id"  => $client_id,
	"node_id"    => $node_id,
	"ip"         => $ip,
	"mask"       => $mask,
	"statements" => [],
	"tag"        => "iface" . scalar(keys(%Ifaces)),
    };
    return $Ifaces{$client_id};
}
sub AddIfaceToNode($$)
{
    my ($node_id, $client_id) = @_;
    $Nodes{$node_id}->{'ifaces'}->{$client_id} = $Ifaces{$client_id};
}
sub AddIfaceStatement($$) {
    my ($iface_id, $statement) = @_;
    
    push(@{$Ifaces{$iface_id}->{'statements'}}, $statement);
}
sub AddBlockStore($$$$) {
    my ($node_id, $bsname, $class, $mount) = @_;

    $Nodes{$node_id}->{'blockstores'}->{$bsname} = {
	"bsname"     => $bsname,
	"class"      => $class,
	"mount"      => $mount,
	"statements" => [],
	"tag"        => "bs" . $BsCounter++,
    };
}
sub AddBlockstoreStatement($$$) {
    my ($node_id, $bsname, $statement) = @_;
    
    push(@{$Nodes{$node_id}->{'blockstores'}->{$bsname}->{'statements'}},
	 $statement);
}
sub AddBridgePipe($$$$$) {
    my ($node_id, $source, $capacity, $latency, $loss) = @_;
    my (undef,$iface_id) = split(":", $source);

    $Nodes{$node_id}->{'pipes'}->{$iface_id} = {
	"iface_id"   => $iface_id,
	"capacity"   => $capacity,
	"latency"    => $latency,
	"lossrate"   => $loss,
    };
}
sub AddLink($$) {
    my ($client_id, $type) = @_;

    $Links{$client_id} = {
	"client_id"    => $client_id,
	"type"         => $type,
	"isbslink"     => 0,
	"bsnode"       => undef,
	"ifaces"       => {},
	"statements"   => [],
	"tag"          => "link" . scalar(keys(%Links)),
    };
    return $Links{$client_id};
}
sub AddLinkStatement($$) {
    push(@{$Links{$_[0]}->{'statements'}}, $_[1]);
}
sub AddLinkIface($$) {
    $Links{$_[0]}->{'ifaces'}->{$_[1]} = $Ifaces{$_[1]};
233 234
}

235 236 237
#
# Process nodes.
#
238 239 240 241 242 243
foreach my $noderef (GeniXML::FindNodes("n:node", $rspec)->get_nodelist()) {
    my $client_id   = GeniXML::GetVirtualId($noderef);
    my $sliver_type = GeniXML::GetVirtualizationSubtype($noderef);
    my $exclusive   = GeniXML::GetExclusive($noderef);
    
    if ($sliver_type eq "emulab-xen") {
244 245
	AddNode($client_id, "emulab-xen");
	
246 247 248 249 250 251
	if (GeniXML::HasXenSettings($noderef)) {
	    my $xensettings = GeniXML::GetXenSettings($noderef);
	    
	    foreach my $setting (keys(%$xensettings)) {
		my $value = $xensettings->{$setting};
		if ($setting eq "ram") {
252
		    AddNodeStatement($client_id, "ram = $value");
253 254
		}
		elsif ($setting eq "cores") {
255
		    AddNodeStatement($client_id, "cores = $value");
256 257
		}
		elsif ($setting eq "disk") {
258
		    AddNodeStatement($client_id, "disk = $value");
259 260 261 262 263 264
		}
	    }
	}
	# xen_ptype is how we force to a node type.
	if (defined(GeniXML::XenPtype($noderef))) {
	    my $ptype = GeniXML::XenPtype($noderef);
265
	    AddNodeStatement($client_id, "xen_ptype = '$ptype'");
266 267
	}
    }
Leigh Stoller's avatar
Leigh Stoller committed
268
    elsif ($sliver_type eq "emulab-blockstore") {
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
	AddNode($client_id, "emulab-blockstore");
    }
    elsif ($sliver_type eq "delay") {
	AddNode($client_id, "delay");
	
	#
	# We need the pipes. 
	#
	my @pipes = GeniXML::GetDelayPipes($noderef);
	if (!@pipes) {
	    fatal("No pipes for bridge node.");
	}
	foreach my $pipe (@pipes) {
	    #
	    # Not all params need to be defined.
	    #
	    my $source   = GetText("source", $pipe);
	    my $capacity = GetText("capacity", $pipe);
	    my $latency  = GetText("latency", $pipe);
	    my $lossrate = GetText("packet_loss", $pipe);

	    AddBridgePipe($client_id, $source, $capacity, $latency, $lossrate)
	}
Leigh Stoller's avatar
Leigh Stoller committed
292
    }
293
    else {
294
	AddNode($client_id, "raw");
295 296
    }
    if (defined($exclusive)) {
297 298
	AddNodeStatement($client_id, "exclusive = " .
			 ($exclusive ? "True" : "False"));
299 300
    }
    if (GeniXML::GetNodeId($noderef)) {
301
	AddNodeStatement($client_id, "component_id = '" .
302 303 304
	    GeniXML::GetNodeId($noderef) . "'");
    }
    if (GeniXML::GetManagerId($noderef)) {
305
	AddNodeStatement($client_id, "component_manager_id = '" .
306 307 308 309 310
	    GeniXML::GetManagerId($noderef) . "'");
    }
    if (GeniXML::FindFirst("n:hardware_type", $noderef)) {
	my $element = GeniXML::FindFirst("n:hardware_type", $noderef);
	my $htype   = GeniXML::GetText("name", $element);
311
	AddNodeStatement($client_id, "hardware_type = '$htype'");
312 313
    }
    if (GeniXML::RoutableControlIP($noderef)) {
314
	AddNodeStatement($client_id, "routable_control_ip = True");
315 316 317 318 319
    }
    
    #
    # Disk image.
    #
320 321 322 323 324 325
    if ($sliver_type ne "emulab-blockstore") {
	my $diskref = GeniXML::GetDiskImage($noderef);
	if (defined($diskref)) {
	    my $name  = GeniXML::GetText("name", $diskref);
	    AddNodeStatement($client_id, "disk_image = '$name'");
	}
326
    }
327
    
328 329 330 331 332 333 334 335 336
    #
    # Services.
    #
    foreach my $service (GeniXML::GetServices($noderef)) {
	my $type = $service->{'type'};
	
	if ($type eq "execute") {
	    my $shell = $service->{'shell'};
	    my $cmd   = $service->{'cmd'};
337 338 339

	    AddNodeStatement($client_id,
			     "addService(RSPEC.Execute('$shell','$cmd'))");
340 341 342 343
	}
	elsif ($type eq "install") {
	    my $url  =  $service->{'url'};
	    my $path =  $service->{'dir'};
344 345 346

	    AddNodeStatement($client_id,
			     "addService(RSPEC.Install('$url','$path'))");
347 348
	}
    }
349 350 351 352 353 354 355 356 357 358
    #
    # Desires.
    #
    foreach my $desire (GeniXML::FindNodesNS("n:fd", $noderef,
				       $GeniXML::EMULAB_NS)->get_nodelist()) {
	my $name    = GeniXML::GetText("name", $desire);
	my $weight  = GeniXML::GetText("weight", $desire);

	AddNodeStatement($client_id, "Desire('$name','$weight')");
    }
359 360
    
    #
361
    # Interfaces. 
362
    #
363
    foreach my $ifaceref (GeniXML::FindNodes("n:interface",
Leigh Stoller's avatar
Leigh Stoller committed
364
					     $noderef)->get_nodelist()) {
365 366 367 368 369 370 371 372 373
	my $virtual_id = GeniXML::GetInterfaceId($ifaceref);
	my $ip   = GeniXML::GetIp($ifaceref, $noderef);
	my $mask = GeniXML::GetMask($ifaceref, $noderef);

	AddIface($client_id, $virtual_id, $ip, $mask);
	AddIfaceToNode($client_id, $virtual_id);
	if (GeniXML::GetVnodeId($ifaceref)) {
	    AddIfaceStatement($virtual_id, "component_id = '" .
			      GeniXML::GetManagerId($ifaceref) . "'");
374 375
	}
    }
Leigh Stoller's avatar
Leigh Stoller committed
376 377 378 379 380 381 382 383 384 385 386 387
    #
    # Blockstores. 
    #
    foreach my $blockref (GeniXML::FindNodesNS("n:blockstore", $noderef,
				       $GeniXML::EMULAB_NS)->get_nodelist()) {
	my $bsname    = GeniXML::GetText("name", $blockref);
	my $class     = GeniXML::GetText("class", $blockref);
	my $mount     = GeniXML::GetText("mountpoint", $blockref);
	my $readonly  = GeniXML::GetText("readonly", $blockref);
	my $dataset   = GeniXML::GetText("dataset", $blockref);
	my $placement = GeniXML::GetText("placement", $blockref);
	my $size      = GeniXML::GetText("size", $blockref);
388
	my @statements=();
Leigh Stoller's avatar
Leigh Stoller committed
389

390 391 392
	# XXX
	if ($sliver_type eq "emulab-blockstore") {
	    $bsname = $client_id;
Leigh Stoller's avatar
Leigh Stoller committed
393
	}
394 395
	AddBlockStore($client_id, $bsname, $class, $mount);

Leigh Stoller's avatar
Leigh Stoller committed
396
	if (defined($readonly) && $readonly) {
397
	    AddBlockstoreStatement($client_id, $bsname, "readonly = True");
Leigh Stoller's avatar
Leigh Stoller committed
398 399
	}
	if (defined($size)) {
400
	    AddBlockstoreStatement($client_id, $bsname, "size = '$size'");
Leigh Stoller's avatar
Leigh Stoller committed
401 402
	}
	if (defined($placement)) {
403
	    AddBlockstoreStatement($client_id, $bsname, "placement = '$placement'");
Leigh Stoller's avatar
Leigh Stoller committed
404 405
	}
	if (defined($dataset)) {
406
	    AddBlockstoreStatement($client_id, $bsname, "dataset = '$dataset'");
Leigh Stoller's avatar
Leigh Stoller committed
407 408
	}
    }
409 410 411 412 413 414 415 416
}
#
# Links ...
#
foreach my $linkref (GeniXML::FindNodes("n:link", $rspec)->get_nodelist()) {
    my $client_id   = GeniXML::GetVirtualId($linkref);
    my $linktype    = GeniXML::LinkType($linkref);

417
    my $link = AddLink($client_id, $linktype);
418 419 420 421 422

    #
    # Link properties
    #
    if (GeniXML::GetManagerId($linkref)) {
423
	AddLinkStatement($client_id, "component_manager_id = '" .
424 425 426
	    GeniXML::GetManagerId($linkref) . "'");
    }
    if (GeniXML::HasBestEffortSetting($linkref)) {
427
	AddLinkStatement($client_id, "best_effort = True");
428 429 430
    }
    if (GeniXML::HasTrivialSetting($linkref)) {
	my $istrivial = (GeniXML::TrivialOkay($linkref) ? "True" : "False");
431
	AddLinkStatement($client_id, "trivial_ok = $istrivial");
432 433 434
    }
    if (GeniXML::HasTagSetting($linkref)) {
	my $istagged = (GeniXML::IsTaggedLan($linkref) ? "True" : "False");
435
	AddLinkStatement($client_id, "vlan_tagging = $istagged");
436 437
    }
    if (GeniXML::IsMultiplexedLan($linkref)) {
438
	AddLinkStatement($client_id, "link_multiplexing = True");
439 440 441
    }
    if (GeniXML::GetText("protocol", $linkref)) {
	my $protocol = GeniXML::GetText("protocol", $linkref);
442
	AddLinkStatement($client_id, "protocol = '$protocol'");
443 444 445 446 447 448 449 450 451 452 453 454 455 456
    }
    #
    # Shaping.
    #
    my @properties = GeniXML::GetLinkProperties($linkref);
    if (@properties) {
	foreach my $property (@properties) {
	    my $source   = GetText("source_id", $property);
	    my $dest     = GetText("dest_id", $property);
	    my $capacity = GetText("capacity", $property);
	    my $plr      = GetText("packet_loss", $property);
	    my $latency  = GetText("latency", $property);

	    if (defined($capacity)) {
457
		AddIfaceStatement($source, "bandwidth = $capacity");
458 459
	    }
	    if (defined($latency)) {
460
		AddIfaceStatement($source, "latency = $latency");
461 462
	    }
	    if (defined($plr)) {
463
		AddIfaceStatement($source, "plr = $plr");
464 465 466 467 468 469
	    }
	}
    }
    #
    # Interfaces.
    #
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
    # Another blockstore special case; Kirk would prefer not to have
    # IP addresses show through since they are ignored. So first go through
    # and see if any of the nodes is a blockstore, and if they are, kill
    # the IP address in all the interfaces.
    #
    my $isbslink = 0;
    
    foreach my $ifaceref (GeniXML::FindNodes("n:interface_ref",
					     $linkref)->get_nodelist()) {
	my $virtual_id = GeniXML::GetInterfaceId($ifaceref);
	my ($node_id,$iface_id) = split(":", $virtual_id);
	my $node = $Nodes{$node_id};

	if ($node->{'type'} eq "emulab-blockstore") {
	    $link->{'isbslink'} = $isbslink = 1;
	    $link->{'bsnode'}   = $node;
	}
	AddLinkIface($client_id, $virtual_id);
    }
    if ($isbslink) {
	#
	# Kirk would like the IPs to not show. 
	#
	foreach my $ifaceref (GeniXML::FindNodes("n:interface_ref",
						 $linkref)->get_nodelist()) {
495
	    my $virtual_id = GeniXML::GetInterfaceId($ifaceref);
496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671
	    if (exists($Ifaces{$virtual_id})) {
		$Ifaces{$virtual_id}->{'ip'} = undef;
	    }
	}
    }
}


#
# Now we can spit out stuff. We want to maintain the same order as the
# rspec for clarity.
#
# If we have any ifaces, we need to remember them in a global variable, since
# the same python object we create when adding to the node, has to be referenced
# again when adding it to the link. 
#

#
# Tour description and instructions.
#
my ($rspec_tour) = GeniXML::FindNodesNS("n:rspec_tour", $rspec,
					$GeniXML::RSPEC_TOUR)->get_nodelist();
if (defined($rspec_tour)) {
    my $description  = GeniXML::FindFirst("n:description", $rspec_tour);
    my $instructions = GeniXML::FindFirst("n:instructions", $rspec_tour);

    AddStatement("Tour = IG.Tour()");
    if (defined($description)) {
	my $text = $description->textContent();

	AddStatement("Tour.Description(IG.Tour.TEXT, '$text')");
    }
    if (defined($instructions)) {
	my $text = $instructions->textContent();

	AddStatement("Tour.Instructions(IG.Tour.TEXT, '$text')");
    }
    AddStatement("rspec.addTour(Tour)\n");
}

#
# Now nodes, in same order as rspec.
#
foreach my $noderef (GeniXML::FindNodes("n:node", $rspec)->get_nodelist()) {
    my $client_id   = GeniXML::GetVirtualId($noderef);
    my $node        = $Nodes{$client_id};
    my $ntag        = $node->{'tag'};
    my @code        = ("# Node $client_id");

    if ($node->{'type'} eq "emulab-xen") {
	push(@code, "$ntag = IG.XenVM('$client_id')");
    }
    elsif ($node->{'type'} eq "emulab-blockstore") {
	my $mount = $node->{'blockstores'}->{$client_id}->{'mount'};
	$ntag = $node->{'blockstores'}->{$client_id}->{'tag'};

	#
	# 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 bridge has only two
	# interfaces, so that makes it easier.
	#
	my ($iface) = values(%{$node->{'ifaces'}});
	my (undef,$if) = split(":", $iface->{'client_id'});
	
	push(@code, "$ntag = IG.RemoteBlockstore('$client_id', '$mount', '$if')");
    }
    elsif ($node->{'type'} 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'});
	
	push(@code, "$ntag = IG.Bridge('$client_id', '$if0', '$if1')");

	#
	# And the pipes. Just two of them.
	#
	foreach my $p (values(%{$node->{'pipes'}})) {
	    my $pname = ($p->{'iface_id'} eq $if0
			 ? "${ntag}.pipe('$if0')" : "${ntag}.pipe('$if1')");

	    push(@code, "${pname}.bandwidth = " . $p->{'capacity'})
		if (defined($p->{'capacity'}));
	    push(@code, "${pname}.latency = " . $p->{'latency'})
		if (defined($p->{'latency'}));
	    push(@code, "${pname}.lossrate = " . $p->{'lossrate'})
		if (defined($p->{'lossrate'}));
	}
    }
    else {
	push(@code, "$ntag = RSPEC.RawPC('$client_id')");
    }
    foreach my $statement (@{$node->{'statements'}}) {
	push(@code, "${ntag}." . $statement);
    }
    foreach my $id (sort(keys(%{$node->{'ifaces'}}))) {
	my $interface = $node->{'ifaces'}->{$id};
	my $client_id = $interface->{'client_id'};
	my $itag = $interface->{'tag'};
	my $ip   = $interface->{'ip'};
	my $mask = $interface->{'mask'};
	my (undef,$iface_id) = split(":", $client_id);
	my $addr = ($ip && $mask ? "RSPEC.IPv4Address('$ip','$mask')" : undef);

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

	if ($node->{'type'} eq "emulab-blockstore") {
	    foreach my $statement
		(@{$node->{'blockstores'}->{$client_id}->{'statements'}}) {
		push(@code, "${btag}." . $statement);
	    }
	}
	else {
	    push(@code, "$btag = ${ntag}.Blockstore('$bsname', '$mount') ");
	    foreach my $statement
		(@{$node->{'blockstores'}->{$bsname}->{'statements'}}) {
		push(@code, "${btag}." . $statement);
	    }
	}
    }
    push(@code, "rspec.addResource($ntag)");
    AddStatement(join("\n", @code));
    AddStatement("");
}
foreach my $linkref (GeniXML::FindNodes("n:link", $rspec)->get_nodelist()) {
    my $client_id   = GeniXML::GetVirtualId($linkref);
    my $link        = $Links{$client_id};
    my $ltag        = $link->{'tag'};
    my @code        = ("# Link $client_id");

    if (defined($link->{'type'}) && $link->{'type'} eq "lan") {
	push(@code, "$ltag = RSPEC.LAN('$client_id')");
    }
    else {
	push(@code, "$ltag = RSPEC.Link('$client_id')");
    }
    foreach my $statement (@{$link->{'statements'}}) {
	push(@code, "${ltag}." . $statement);
    }
    if (0 && $link->{'isbslink'}) {
	my $node = $link->{'bsnode'};
	my $bs   = $node->{'blockstores'}->{$node->{'client_id'}};
	my $btag = $bs->{'tag'};
	push(@code, "${ltag}.addInterface(${btag}.interface)");
    }
    foreach my $id (sort(keys(%{$link->{'ifaces'}}))) {
	my $interface = $link->{'ifaces'}->{$id};
	my $client_id = $interface->{'client_id'};
	my $itag      = $interface->{'tag'};
672

673 674
	foreach my $statement (@{$interface->{'statements'}}) {
	    push(@code, "${itag}." . $statement);
675
	}
676
	push(@code, "${ltag}.addInterface($itag)");
677
    }
678 679 680
    push(@code, "rspec.addResource($ltag)");
    AddStatement(join("\n", @code));
    AddStatement("");
681 682 683 684 685 686 687
}

#
# Special top level elements.
#
if (GeniXML::MultiplexFactor($rspec)) {
    my $factor = GeniXML::MultiplexFactor($rspec);
688
    AddStatement("rspec.setCollocateFactor($factor)");
689 690 691
}
if (GeniXML::PackingStrategy($rspec)) {
    my $packing_option = GeniXML::PackingStrategy($rspec);
692
    AddStatement("rspec.setPackingStrategy($packing_option)");
693 694 695 696 697 698
}
foreach my $pool (@{ GeniXML::GetAddressPools($rspec) }) {
    my $name  = $pool->{'client_id'};
    my $count = $pool->{'count'};
    my $cmurn = $pool->{'cmurn'};

699
    AddStatement("pool = IG.AddressPool('$name', $count)");
700
    if (defined($cmurn)) {
701
	AddStatement("pool.component_manager_id = '$cmurn'");
702
    }
703
    AddStatement("rspec.addResource(pool)");
704
}
705
AddStatement("portal.printRequestRSpec(rspec)");
706 707 708 709 710 711 712 713 714 715 716 717 718

print "$genilib";

exit(0);

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

    print STDERR "*** $0:\n".
	         "    $mesg\n";

    exit(-1);
}