rspec2genilib.in 21.8 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

# 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;
115
    fatal("rspec is not well formed");    
116 117 118
}
my $rspec = GeniXML::ParseFile($rspecfile);
if (! defined($rspec)) {
119
    fatal("Could not parse rspec");
120 121
}
my $genilib =
122
    "#\n" .
123
    "# NOTE: This code was machine converted. An actual human would never\n".
124
    "#       write code like this!\n".
125 126 127 128
    "#\n\n" .
    "# Import the Portal object.\n" .
    "import geni.portal as portal\n" .
    "# Import the ProtoGENI library.\n" .
129
    "import geni.rspec.pg as pg\n" .
130
    "# Import the Emulab specific extensions.\n" .
131
    "import geni.rspec.emulab as emulab\n" .
132
    "\n" .
133 134 135 136
    "# 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";
137 138

sub AddStatement($)
139 140 141 142 143
{
    $genilib .= $_[0] . "\n";
}

#
144
# Top level elements.
145
#
146 147 148
my %Nodes       = ();
my %Ifaces      = ();
my %Links       = ();
149 150
my $Description;
my $Instructions;
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 233 234 235 236 237 238

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]};
239 240
}

241 242 243
#
# Process nodes.
#
244 245 246 247 248 249
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") {
250 251
	AddNode($client_id, "emulab-xen");
	
252 253 254 255 256 257
	if (GeniXML::HasXenSettings($noderef)) {
	    my $xensettings = GeniXML::GetXenSettings($noderef);
	    
	    foreach my $setting (keys(%$xensettings)) {
		my $value = $xensettings->{$setting};
		if ($setting eq "ram") {
258
		    AddNodeStatement($client_id, "ram = $value");
259 260
		}
		elsif ($setting eq "cores") {
261
		    AddNodeStatement($client_id, "cores = $value");
262 263
		}
		elsif ($setting eq "disk") {
264
		    AddNodeStatement($client_id, "disk = $value");
265 266 267 268 269 270
		}
	    }
	}
	# xen_ptype is how we force to a node type.
	if (defined(GeniXML::XenPtype($noderef))) {
	    my $ptype = GeniXML::XenPtype($noderef);
271
	    AddNodeStatement($client_id, "xen_ptype = '$ptype'");
272 273
	}
    }
Leigh B Stoller's avatar
Leigh B Stoller committed
274
    elsif ($sliver_type eq "emulab-blockstore") {
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
	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 B Stoller's avatar
Leigh B Stoller committed
298
    }
299
    else {
300
	AddNode($client_id, "raw");
301 302
    }
    if (defined($exclusive)) {
303 304
	AddNodeStatement($client_id, "exclusive = " .
			 ($exclusive ? "True" : "False"));
305 306
    }
    if (GeniXML::GetNodeId($noderef)) {
307
	AddNodeStatement($client_id, "component_id = '" .
308 309 310
	    GeniXML::GetNodeId($noderef) . "'");
    }
    if (GeniXML::GetManagerId($noderef)) {
311
	AddNodeStatement($client_id, "component_manager_id = '" .
312 313 314 315 316
	    GeniXML::GetManagerId($noderef) . "'");
    }
    if (GeniXML::FindFirst("n:hardware_type", $noderef)) {
	my $element = GeniXML::FindFirst("n:hardware_type", $noderef);
	my $htype   = GeniXML::GetText("name", $element);
317
	AddNodeStatement($client_id, "hardware_type = '$htype'");
318 319
    }
    if (GeniXML::RoutableControlIP($noderef)) {
320
	AddNodeStatement($client_id, "routable_control_ip = True");
321 322 323 324 325
    }
    
    #
    # Disk image.
    #
326 327 328 329 330 331
    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'");
	}
332
    }
333
    
334 335 336 337 338 339 340 341 342
    #
    # Services.
    #
    foreach my $service (GeniXML::GetServices($noderef)) {
	my $type = $service->{'type'};
	
	if ($type eq "execute") {
	    my $shell = $service->{'shell'};
	    my $cmd   = $service->{'cmd'};
343 344

	    AddNodeStatement($client_id,
345
			     "addService(pg.Execute('$shell','$cmd'))");
346 347 348 349
	}
	elsif ($type eq "install") {
	    my $url  =  $service->{'url'};
	    my $path =  $service->{'dir'};
350 351

	    AddNodeStatement($client_id,
352
			     "addService(pg.Install('$url','$path'))");
353 354
	}
    }
355
    #
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
    # Program Agents.
    #
    foreach my $agent (GeniXML::GetProgramAgents($noderef)) {
	my $vname       = $agent->{"name"};
	my $command     = $agent->{"command"};
	my $directory   = $agent->{"directory"};
	my $onexpstart  = $agent->{"onexpstart"} ? "True" : "False";
	if (defined($directory) && $directory ne "") {
	    $directory = "'" . $directory . "'";
	}
	else {
	    $directory = "None";
	}
	AddNodeStatement($client_id,
		 "addService(emulab.ProgramAgent('$vname', '$command', ".
		 "$directory, $onexpstart))");
    }
    #
374 375 376 377 378 379 380 381 382
    # 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')");
    }
383 384
    
    #
385
    # Interfaces. 
386
    #
387
    foreach my $ifaceref (GeniXML::FindNodes("n:interface",
Leigh B Stoller's avatar
Leigh B Stoller committed
388
					     $noderef)->get_nodelist()) {
389 390 391 392 393 394 395 396 397
	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) . "'");
398 399
	}
    }
Leigh B Stoller's avatar
Leigh B Stoller committed
400 401 402 403 404 405 406 407 408 409 410 411
    #
    # 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);
412
	my @statements=();
Leigh B Stoller's avatar
Leigh B Stoller committed
413

414 415 416
	# XXX
	if ($sliver_type eq "emulab-blockstore") {
	    $bsname = $client_id;
Leigh B Stoller's avatar
Leigh B Stoller committed
417
	}
418 419
	AddBlockStore($client_id, $bsname, $class, $mount);

Leigh B Stoller's avatar
Leigh B Stoller committed
420
	if (defined($readonly) && $readonly) {
421
	    AddBlockstoreStatement($client_id, $bsname, "readonly = True");
Leigh B Stoller's avatar
Leigh B Stoller committed
422 423
	}
	if (defined($size)) {
424
	    AddBlockstoreStatement($client_id, $bsname, "size = '$size'");
Leigh B Stoller's avatar
Leigh B Stoller committed
425 426
	}
	if (defined($placement)) {
427
	    AddBlockstoreStatement($client_id, $bsname, "placement = '$placement'");
Leigh B Stoller's avatar
Leigh B Stoller committed
428 429
	}
	if (defined($dataset)) {
430
	    AddBlockstoreStatement($client_id, $bsname, "dataset = '$dataset'");
Leigh B Stoller's avatar
Leigh B Stoller committed
431 432
	}
    }
433 434 435 436 437 438 439 440
}
#
# Links ...
#
foreach my $linkref (GeniXML::FindNodes("n:link", $rspec)->get_nodelist()) {
    my $client_id   = GeniXML::GetVirtualId($linkref);
    my $linktype    = GeniXML::LinkType($linkref);

441
    my $link = AddLink($client_id, $linktype);
442 443 444 445 446

    #
    # Link properties
    #
    if (GeniXML::GetManagerId($linkref)) {
447
	AddLinkStatement($client_id, "component_manager_id = '" .
448 449 450
	    GeniXML::GetManagerId($linkref) . "'");
    }
    if (GeniXML::HasBestEffortSetting($linkref)) {
451
	AddLinkStatement($client_id, "best_effort = True");
452
    }
453 454 455
    if (GeniXML::HasForceShapingSetting($linkref)) {
	AddLinkStatement($client_id, "setForceShaping()");
    }
456 457
    if (GeniXML::HasTrivialSetting($linkref)) {
	my $istrivial = (GeniXML::TrivialOkay($linkref) ? "True" : "False");
458
	AddLinkStatement($client_id, "trivial_ok = $istrivial");
459 460 461
    }
    if (GeniXML::HasTagSetting($linkref)) {
	my $istagged = (GeniXML::IsTaggedLan($linkref) ? "True" : "False");
462
	AddLinkStatement($client_id, "vlan_tagging = $istagged");
463 464
    }
    if (GeniXML::IsMultiplexedLan($linkref)) {
465
	AddLinkStatement($client_id, "link_multiplexing = True");
466 467 468
    }
    if (GeniXML::GetText("protocol", $linkref)) {
	my $protocol = GeniXML::GetText("protocol", $linkref);
469
	AddLinkStatement($client_id, "protocol = '$protocol'");
470 471 472 473 474 475 476 477 478 479 480 481 482 483
    }
    #
    # 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)) {
484
		AddIfaceStatement($source, "bandwidth = $capacity");
485 486
	    }
	    if (defined($latency)) {
487
		AddIfaceStatement($source, "latency = $latency");
488 489
	    }
	    if (defined($plr)) {
490
		AddIfaceStatement($source, "plr = $plr");
491 492 493 494 495 496
	    }
	}
    }
    #
    # Interfaces.
    #
497 498 499 500 501 502 503 504 505 506
    # 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);
507 508 509 510 511 512 513 514 515
	my $node_id    = $Ifaces{$virtual_id}->{'node_id'};
	my $node       = $Nodes{$node_id};
	#
	# 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.
	#
	my ($tmp,$iface_id) = split(":", $virtual_id);
	$iface_id = $tmp if (!defined($iface_id));
516 517 518 519 520 521 522 523 524 525 526 527 528

	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()) {
529
	    my $virtual_id = GeniXML::GetInterfaceId($ifaceref);
530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552
	    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)) {
553 554
    $Description  = GeniXML::FindFirst("n:description", $rspec_tour);
    $Instructions = GeniXML::FindFirst("n:instructions", $rspec_tour);
555

556 557 558
    if (defined($Description)) {
	$Description = $Description->textContent();
	$Description =~ s/\"/\\"/g;
559
    }
560 561 562
    if (defined($Instructions)) {
	$Instructions = $Instructions->textContent();
	$Instructions =~ s/\"/\\"/g;
563 564 565 566 567 568 569 570 571 572 573 574 575
    }
}

#
# 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") {
576
	push(@code, "$ntag = emulab.XenVM('$client_id')");
577 578 579 580 581 582 583 584 585 586 587 588 589 590 591
    }
    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'});
	
592 593
	push(@code, "$ntag = emulab.RemoteBlockstore('$client_id', ".
	     "'$mount', '$if')");
594 595 596 597 598 599 600 601 602
    }
    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'});
	
603
	push(@code, "$ntag = emulab.Bridge('$client_id', '$if0', '$if1')");
604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620

	#
	# 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 {
621
	push(@code, "$ntag = request.RawPC('$client_id')");
622 623 624 625 626 627 628 629 630 631
    }
    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'};
632 633 634 635 636 637 638
	#
	# 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.
	#
	my ($tmp,$iface_id) = split(":", $client_id);
	$iface_id = $tmp if (!defined($iface_id));
639 640
	my $addr = ($ip && $mask ?
		    "pg.IPv4Address('$ip','$mask')" : undef);
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 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690

	#
	# 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);
	    }
	}
    }
    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") {
691
	push(@code, "$ltag = request.LAN('$client_id')");
692 693
    }
    else {
694
	push(@code, "$ltag = request.Link('$client_id')");
695 696 697 698 699 700 701 702 703 704 705 706 707 708
    }
    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'};
709

710 711
	foreach my $statement (@{$interface->{'statements'}}) {
	    push(@code, "${itag}." . $statement);
712
	}
713
	push(@code, "${ltag}.addInterface($itag)");
714
    }
715 716
    AddStatement(join("\n", @code));
    AddStatement("");
717 718 719 720 721 722 723
}

#
# Special top level elements.
#
if (GeniXML::MultiplexFactor($rspec)) {
    my $factor = GeniXML::MultiplexFactor($rspec);
724
    AddStatement("request.setCollocateFactor($factor)");
725 726 727
}
if (GeniXML::PackingStrategy($rspec)) {
    my $packing_option = GeniXML::PackingStrategy($rspec);
728 729 730 731 732 733 734 735 736
    AddStatement("request.setPackingStrategy('$packing_option')");
}
if (GeniXML::RoutingStyle($rspec)) {
    my $style = GeniXML::RoutingStyle($rspec);
    AddStatement("request.setRoutingStyle('$style')");
}
if (GeniXML::DelayImage($rspec)) {
    my $urn = GeniXML::DelayImage($rspec);
    AddStatement("request.setDelayImage('$urn')");
737 738 739 740 741 742
}
foreach my $pool (@{ GeniXML::GetAddressPools($rspec) }) {
    my $name  = $pool->{'client_id'};
    my $count = $pool->{'count'};
    my $cmurn = $pool->{'cmurn'};

743
    AddStatement("pool = emulab.AddressPool('$name', $count)");
744
    if (defined($cmurn)) {
745
	AddStatement("pool.component_manager_id = '$cmurn'");
746 747
    }
}
748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763
AddStatement("pc.printRequestRSpec(request)");

#
# Prepend the description and instructions using the doc string format.
#
if (defined($Description) || defined($Instructions)) {
    $Description = "This script has no description."
	if (!defined($Description));

    my $docstring = '"""' . $Description;
    if (defined($Instructions)) {
	$docstring .= "\n\n" . "Instructions:" . "\n" . $Instructions;
    }
    $docstring .= '"""';
    $genilib = $docstring . "\n\n" . $genilib;
}
764

765 766 767 768 769 770 771 772 773
if (defined($ofile)) {
    open(GENI, ">$ofile") or
	fatal("Could not open output file");
    print GENI $genilib;
    close(GENI);
}
else {
    print "$genilib";
}
774 775 776 777 778 779 780 781 782
exit(0);

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

    print STDERR "*** $0:\n".
	         "    $mesg\n";
    exit(-1);
}