GeniCM.pm.in 81.9 KB
Newer Older
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1 2
#!/usr/bin/perl -wT
#
3
# GENIPUBLIC-COPYRIGHT
4
# Copyright (c) 2008-2009 University of Utah and the Flux Group.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
# All rights reserved.
#
package GeniCM;

#
# The server side of the CM interface on remote sites. Also communicates
# with the GMC interface at Geni Central as a client.
#
use strict;
use Exporter;
use vars qw(@ISA @EXPORT);

@ISA    = "Exporter";
@EXPORT = qw ( );

# Must come after package declaration!
use lib '@prefix@/lib';
use GeniDB;
use Genixmlrpc;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
24 25
use GeniResponse;
use GeniTicket;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
26
use GeniCredential;
27
use GeniCertificate;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
28
use GeniSlice;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
29
use GeniAggregate;
30
use GeniAuthority;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
31
use GeniSliver;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
32
use GeniUser;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
33
use GeniRegistry;
34 35
use GeniUtil;
use libtestbed qw(SENDMAIL);
36
use emutil;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
37
# Hate to import all this crap; need a utility library.
38
use libdb qw(TBGetSiteVar);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
39
use User;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
40
use Node;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
41
use libadminctrl;
42
use Interface;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
43 44
use English;
use Data::Dumper;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
45
use XML::Simple;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
46 47 48
use Date::Parse;
use POSIX qw(strftime);
use Time::Local;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
49
use Experiment;
50
use Firewall;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
51 52 53 54 55 56 57 58

# Configure variables
my $TB		   = "@prefix@";
my $TBOPS          = "@TBOPSEMAIL@";
my $TBAPPROVAL     = "@TBAPPROVALEMAIL@";
my $TBAUDIT   	   = "@TBAUDITEMAIL@";
my $BOSSNODE       = "@BOSSNODE@";
my $OURDOMAIN      = "@OURDOMAIN@";
59
my $PGENIDOMAIN    = "@PROTOGENI_DOMAIN@";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
60
my $CREATEEXPT     = "$TB/bin/batchexp";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
61
my $ENDEXPT        = "$TB/bin/endexp";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
62
my $NALLOC	   = "$TB/bin/nalloc";
63
my $NFREE	   = "$TB/bin/nfree";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
64
my $AVAIL	   = "$TB/sbin/avail";
65
my $PTOPGEN	   = "$TB/libexec/ptopgen";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
66 67
my $TBSWAP	   = "$TB/bin/tbswap";
my $SWAPEXP	   = "$TB/bin/swapexp";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
68 69 70 71
my $PLABSLICE	   = "$TB/sbin/plabslicewrapper";
my $NAMEDSETUP     = "$TB/sbin/named_setup";
my $VNODESETUP     = "$TB/sbin/vnode_setup";
my $GENTOPOFILE    = "$TB/libexec/gentopofile";
72
my $EMULAB_PEMFILE = "@prefix@/etc/genicm.pem";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
73 74

#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
75
# Respond to a Resolve request. 
Leigh B. Stoller's avatar
Leigh B. Stoller committed
76 77 78 79 80 81 82
#
sub Resolve($)
{
    my ($argref) = @_;
    my $uuid       = $argref->{'uuid'};
    my $cred       = $argref->{'credential'};
    my $type       = $argref->{'type'};
Leigh B. Stoller's avatar
Leigh B. Stoller committed
83
    my $hrn;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
84 85 86 87 88 89 90 91

    if (! defined($cred)) {
	return GeniResponse->MalformedArgsResponse();
    }
    if (! (defined($type) && ($type =~ /^(Node)$/))) {
	return GeniResponse->MalformedArgsResponse();
    }
    # Allow lookup by uuid or hrn.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
92
    if (! defined($uuid)) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
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 122 123
	return GeniResponse->MalformedArgsResponse();
    }
    if (defined($uuid) && !($uuid =~ /^[-\w]*$/)) {
	return GeniResponse->MalformedArgsResponse();
    }

    my $credential = GeniCredential->CreateFromSigned($cred);
    if (!defined($credential)) {
	return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
				    "Could not create GeniCredential object");
    }
    
    #
    # Make sure the credential was issued to the caller, but no special
    # permission required to resolve component resources.
    #
    if ($credential->owner_uuid() ne $ENV{'GENIUUID'}) {
	return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
				    "This is not your credential!");
    }
    if ($type eq "Node") {
	my $node;
	
	if (defined($uuid)) {
	    $node= Node->Lookup($uuid);
	}
	else {
	    #
	    # We only want the last token for node lookup.
	    #
	    if ($hrn =~ /\./) {
124
		($hrn) = ($hrn =~ /\.([-\w]*)$/);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
125 126 127 128 129 130 131 132 133
	    }
	    $node= Node->Lookup($hrn);
	}
	if (!defined($node)) {
	    return GeniResponse->Create(GENIRESPONSE_SEARCHFAILED,
					undef, "Nothing here by that name");
	}
	
	# Return a blob.
134
	my $blob = { "hrn"          => "${PGENIDOMAIN}." . $node->node_id(),
Leigh B. Stoller's avatar
Leigh B. Stoller committed
135
		     "uuid"         => $node->uuid(),
136
		     "role"	    => $node->role(),
137
		     "hostname"     => $node->node_id() . ".${OURDOMAIN}",
138 139
		     "physctrl"     => 
			 Interface->LookupControl( $node->phys_nodeid() )->IP(),
Leigh B. Stoller's avatar
Leigh B. Stoller committed
140 141 142 143 144 145 146 147 148 149 150 151 152
		   };

	#
	# Get the list of interfaces for the node.
	#
	my @interfaces;
	if ($node->AllInterfaces(\@interfaces) != 0) {
	    return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
					"Could not get interfaces for $uuid");
	}

	my @iblobs = ();
	foreach my $interface (@interfaces) {
153 154 155
	    next
		if (!defined($interface->switch_id()));
		
Leigh B. Stoller's avatar
Leigh B. Stoller committed
156 157 158 159 160 161
	    my $iblob = { "uuid"	=> $interface->uuid(),
			  "iface"	=> $interface->iface(),
			  "type"	=> $interface->type(),
			  "card"	=> $interface->card(),
			  "port"	=> $interface->port(),
			  "role"	=> $interface->role(),
162 163
			  "IP"		=> $interface->IP() || "",
			  "mask"	=> $interface->mask() || "",
Leigh B. Stoller's avatar
Leigh B. Stoller committed
164
			  "MAC"		=> $interface->mac(),
165 166 167 168 169
			  "switch_id"   => "${OURDOMAIN}." . 
			      $interface->switch_id(),
			  "switch_card"	=> $interface->switch_card(),
			  "switch_port"	=> $interface->switch_port(),
			  "wire_type"	=> $interface->wire_type(),
Leigh B. Stoller's avatar
Leigh B. Stoller committed
170 171 172 173 174 175 176 177 178 179 180 181
		      };

	    push(@iblobs, $iblob);
	}
	$blob->{'interfaces'} = \@iblobs
	    if (@iblobs);
	
	return GeniResponse->Create(GENIRESPONSE_SUCCESS, $blob);
    }
    return GeniResponse->Create(GENIRESPONSE_UNSUPPORTED);
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
#
# Discover resources on this component, returning a resource availablity spec
#
sub DiscoverResources($)
{
    my ($argref) = @_;
    my $credential = $argref->{'credential'};
    my $user_uuid  = $ENV{'GENIUSER'};

    $credential = GeniCredential->CreateFromSigned($credential);
    if (!defined($credential)) {
	return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
				    "Could not create GeniCredential object");
    }
    # The credential owner/slice has to match what was provided.
197
    if ($user_uuid ne $credential->owner_uuid()) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
198 199 200 201
	return GeniResponse->Create(GENIRESPONSE_FORBIDDEN, undef,
				    "Invalid credentials for operation");
    }

Leigh B. Stoller's avatar
Leigh B. Stoller committed
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
    #
    # A sitevar controls whether external users can get any nodes.
    #
    my $allow_externalusers = 0;
    if (!TBGetSiteVar('protogeni/allow_externalusers', \$allow_externalusers)){
	# Cannot get the value, say no.
	$allow_externalusers = 0;
    }
    if (!$allow_externalusers) {
	my $user = GeniUser->Lookup($user_uuid, 1);
	# No record means the user is remote.
	if (!defined($user) || !$user->IsLocal()) {
	    return GeniResponse->Create(GENIRESPONSE_UNAVAILABLE, undef,
					"External users temporarily denied");
	}
    }

Leigh B. Stoller's avatar
Leigh B. Stoller committed
219
    #
220
    # Use ptopgen in xml mode to spit back an xml file. 
Leigh B. Stoller's avatar
Leigh B. Stoller committed
221
    #
222
    if (! open(AVAIL, "$PTOPGEN -x -g -r -p GeniSlices |")) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
223 224 225
	return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
				    "Could not start avail");
    }
226
    my $xml = "";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
227
    while (<AVAIL>) {
228
	$xml .= $_;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
229 230 231 232 233
    }
    close(AVAIL);

    return GeniResponse->Create(GENIRESPONSE_SUCCESS, $xml);
}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
234

Leigh B. Stoller's avatar
Leigh B. Stoller committed
235
#
236
# Update a ticket with a new rspec.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
237
#
238
sub UpdateTicket($)
Leigh B. Stoller's avatar
Leigh B. Stoller committed
239 240
{
    my ($argref) = @_;
241 242 243 244 245 246 247 248 249 250

    return GetTicket($argref, 1);
}

#
# Respond to a GetTicket request. 
#
sub GetTicket($;$)
{
    my ($argref, $isupdate) = @_;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
251
    my $rspec      = $argref->{'rspec'};
Leigh B. Stoller's avatar
Leigh B. Stoller committed
252
    my $impotent   = $argref->{'impotent'};
253
    my $cred       = $argref->{'credential'};
254 255
    my $tick       = $argref->{'ticket'};
    my $uuid       = $argref->{'uuid'};
256
    my $vtopo      = $argref->{'virtual_topology'};
Leigh B. Stoller's avatar
Leigh B. Stoller committed
257
    my $owner_uuid = $ENV{'GENIUSER'};
Leigh B. Stoller's avatar
Leigh B. Stoller committed
258
    my $response   = undef;
259 260 261 262 263 264 265 266
    my $didfwsetup = 0;
    my $ticket;

    # Default to no update
    $isupdate = 0
	if (!defined($isupdate));
    $impotent = 0
	if (!defined($impotent));
Leigh B. Stoller's avatar
Leigh B. Stoller committed
267

268 269
    if (! defined($cred)) {
	return GeniResponse->MalformedArgsResponse();
Leigh B. Stoller's avatar
Leigh B. Stoller committed
270
    }
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
    if (defined($uuid)) {
	if (!($uuid =~ /^[-\w]*$/)) {
	    return GeniResponse->Create(GENIRESPONSE_BADARGS, undef,
					"Improper ticket uuid: $uuid");
	}
	$ticket = GeniTicket->Lookup($uuid);
	if (!defined($ticket)) {
	    return GeniResponse->Create(GENIRESPONSE_BADARGS, undef,
					"No such ticket here: $uuid");
	}
    }
    elsif (defined($rspec)) {
	if (! ($rspec =~ /^[\040-\176\012\015\011]+$/)) {
	    return GeniResponse->Create(GENIRESPONSE_BADARGS, undef,
					"Improper rspec");
	}
	$rspec = XMLin($rspec, KeyAttr => [],
		       ForceArray => ["node", "link", "interface",
				      "interface_ref", "linkendpoints"]);
    }
    else {
	return GeniResponse->MalformedArgsResponse();
    }
    if ($isupdate) {
	$ticket = GeniTicket->CreateFromSignedTicket($tick);
	if (!defined($ticket)) {
	    return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
					"Could not create GeniTicket object");
	}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
300
    }
301
    my $credential = GeniCredential->CreateFromSigned($cred);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
302 303 304 305
    if (!defined($credential)) {
	return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
				    "Could not create GeniCredential object");
    }
306
    my $slice_uuid = $credential->target_uuid();
307
    my $user_uuid  = $credential->owner_uuid();
308
        
309 310 311
    #
    # Make sure the credential was issued to the caller.
    #
312
    if ($user_uuid ne $ENV{'GENIUUID'}) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
313
	return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
314
				    "This is not your credential!");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
315
    }
316 317 318 319 320 321 322
    
    defined($credential) &&
	($credential->HasPrivilege( "pi" ) or
	 $credential->HasPrivilege( "instantiate" ) or
	 $credential->HasPrivilege( "bind" ) or
	 return GeniResponse->Create( GENIRESPONSE_FORBIDDEN, undef,
				      "Insufficient privilege" ));
Leigh B. Stoller's avatar
Leigh B. Stoller committed
323

324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
    #
    # Deal with a duplicate ticket request, after verifying credential.
    #
    if (defined($uuid)) {
	#
	# Return the original ticket string, rather then creating and signing
	# a new version. Only the original requestor can get the ticket, which
	# can then be delegated if necessary. 
	#
	if ($user_uuid ne $ticket->owner_uuid()) {
	    return GeniResponse->Create(GENIRESPONSE_FORBIDDEN, undef,
					"Not your ticket!");
	}
	return GeniResponse->Create(GENIRESPONSE_SUCCESS, $ticket->asString());
    }
    
    #
    # We need this below to sign the ticket.
    #
    my $authority = GeniCertificate->LoadFromFile($EMULAB_PEMFILE);
    if (!defined($authority)) {
	print STDERR " Could not get uuid from $EMULAB_PEMFILE\n";
	return GeniResponse->Create(GENIRESPONSE_ERROR);
    }
348

Leigh B. Stoller's avatar
Leigh B. Stoller committed
349
    #
350
    # Create slice from the certificate.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
351 352 353
    #
    my $slice = GeniSlice->Lookup($slice_uuid);
    if (!defined($slice)) {
354 355 356 357 358
	if ($isupdate) {
	    print STDERR "Could not locate slice $slice_uuid for Update\n";
	    return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
					"No slice found for UpdateTicket");
	}
359
	$slice = CreateSliceFromCertificate($credential);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
360
	if (!defined($slice)) {
361
	    print STDERR "Could not create $slice_uuid\n";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
362
	    return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
363
					"Could not create slice");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
364
	}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
365 366
    }

Leigh B. Stoller's avatar
Leigh B. Stoller committed
367
    #
Leigh B. Stoller's avatar
Leigh B. Stoller committed
368 369
    # Ditto the user.
    #
370
    my $user = GeniUser->Lookup($user_uuid, 1);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
371
    if (!defined($user)) {
372 373 374 375 376
	if ($isupdate) {
	    print STDERR "Could not locate $user_uuid for UpdateTicket\n";
	    return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
					"No user found for UpdateTicket");
	}
377
	$user = CreateUserFromCertificate($credential->owner_cert());
Leigh B. Stoller's avatar
Leigh B. Stoller committed
378
	if (!defined($user)) {
379
	    print STDERR "No user $user_uuid in the ClearingHouse\n";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
380 381 382 383 384
	    return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
			    "Could not get user info from ClearingHouse");
	}
    }

Leigh B. Stoller's avatar
Leigh B. Stoller committed
385 386 387 388 389 390 391 392 393 394 395 396 397 398
    #
    # A sitevar controls whether external users can get any nodes.
    #
    my $allow_externalusers = 0;
    if (!TBGetSiteVar('protogeni/allow_externalusers', \$allow_externalusers)){
	# Cannot get the value, say no.
	$allow_externalusers = 0;
    }
    if (!$allow_externalusers && !$user->IsLocal()) {
	return GeniResponse->Create(GENIRESPONSE_UNAVAILABLE, undef,
				    "External users temporarily denied");
    }
    
    #
399
    # For now all tickets expire very quickly (minutes), but once the
Leigh B. Stoller's avatar
Leigh B. Stoller committed
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
    # ticket is redeemed, it will expire according to the rspec request.
    #
    if (exists($rspec->{'valid_until'})) {
	my $expires = $rspec->{'valid_until'};

	if (! ($expires =~ /^[-\w:.\/]+/)) {
	    return GeniResponse->Create(GENIRESPONSE_BADARGS, undef,
					"Illegal valid_until in rspec");
	}
	# Convert to a localtime.
	my $when = timegm(strptime($expires));
	if (!defined($when)) {
	    return GeniResponse->Create(GENIRESPONSE_BADARGS, undef,
					"Could not parse valid_until");
	}
	
	#
	# No more then 24 hours out ... Needs to be a sitevar?
	#
	my $diff = $when - time();
	if ($diff < (60 * 15) || $diff > (3600 * 24)) {
	    return GeniResponse->Create(GENIRESPONSE_BADARGS, undef,
					"valid_until out of range");
	}
    }
    else {
	# Give it a reasonable default for later when the ticket is redeemed.
	$rspec->{'valid_until'} =
	    POSIX::strftime("20%y-%m-%dT%H:%M:%S", gmtime(time() + (3600*1)));
    }
430 431 432 433 434 435 436

    #
    # Lock the ticket so it cannot be released.
    #
    if (defined($ticket) && $ticket->stored() && $ticket->Lock() != 0) {
	return GeniResponse->BusyResponse("ticket");
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
437 438 439 440 441 442
    
    #
    #
    # Lock the slice from further access.
    #
    if ($slice->Lock() != 0) {
443 444 445
	$ticket->UnLock()
	    if (defined($ticket) && $ticket->stored());
	return GeniResponse->BusyResponse("slice");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
446
    }
447 448 449
    # Shutdown slices get nothing.
    if ($slice->shutdown()) {
	$slice->UnLock();
450 451
	$ticket->UnLock()
	    if (defined($ticket) && $ticket->stored());
452 453 454
	return GeniResponse->Create(GENIRESPONSE_FORBIDDEN, undef,
				    "Slice has been shutdown");
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
455

456
    #
Leigh B. Stoller's avatar
Leigh B. Stoller committed
457
    # For now, there can be only a single toplevel aggregate per slice.
458
    # The existence of an aggregate means the slice is active here. 
Leigh B. Stoller's avatar
Leigh B. Stoller committed
459
    #
460 461 462 463 464 465
    if (!$isupdate) {
	my $aggregate = GeniAggregate->SliceAggregate($slice);
	if (defined($aggregate)) {
	    $response = GeniResponse->Create(GENIRESPONSE_BADARGS, undef,
				     "Already have an aggregate for slice");
	    goto bad;
466 467 468 469 470 471 472 473
	}
    }

    #
    # Firewall hack; just a flag in the rspec for now.
    #
    if (exists($rspec->{'needsfirewall'}) && $rspec->{'needsfirewall'}) {
	if ($slice->SetFirewallFlag($rspec->{'needsfirewall'}) != 0) {
474 475
	    $response = GeniResponse->Create(GENIRESPONSE_ERROR);
	    goto bad;
476 477
	}
    }
478 479 480 481 482 483 484 485 486 487 488 489 490 491 492

    #
    # Until a ticket is redeemed, there is no experiment.
    #
    my $slice_experiment = $slice->GetExperiment();

    # This is where nodes are parked until a ticket is redeemed.
    my $reserved_holding = Experiment->Lookup("GeniSlices", "reservations");
    if (!defined($reserved_holding)) {
	#
	# This experiment has to exist!
	#
	print STDERR "Could not find Geni reservations experiment!\n";
	$response = GeniResponse->Create(GENIRESPONSE_ERROR);
	goto bad;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
493 494
    }

Leigh B. Stoller's avatar
Leigh B. Stoller committed
495
    #
Leigh B. Stoller's avatar
Leigh B. Stoller committed
496 497
    # An rspec is a structure that requests specific nodes. If those
    # nodes are available, then reserve it. Otherwise the ticket
Leigh B. Stoller's avatar
Leigh B. Stoller committed
498 499
    # cannot be granted.
    #
500 501 502 503 504
    my %namemap  = ();
    my %tofree   = ();
    my %colomap  = ();
    my %ifacemap = ();
    my @nodeids  = ();
505
    my @dealloc;
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

    #
    # If this is a ticket update, we want to seed the namemap with
    # existing nodes. This is cause the rspec might refer to wildcards
    # that were already bound in a previous call. We also want to know
    # what nodes are currently reserved in case we have to release some.
    #
    if ($isupdate) {
	foreach my $ref (@{$ticket->rspec()->{'node'}}) {
	    my $resource_uuid = $ref->{'component_uuid'} || $ref->{'uuid'};
	    my $manager_uuid  = $ref->{'component_manager_uuid'};
	    my $node_nickname = $ref->{'virtual_id'} || $ref->{'nickname'};
	    my $colocate      = $ref->{'colocate'} || $ref->{'phys_nickname'};

	    # Let remote nodes pass through.
	    next
		if (defined($manager_uuid) && $manager_uuid ne $ENV{'MYUUID'});

	    my $node = Node->Lookup($resource_uuid);
	    if (!defined($node)) {
		$response = GeniResponse->Create(GENIRESPONSE_BADARGS, undef,
						 "Bad resource $resource_uuid in ticket");
		goto bad;
	    }

	    # Grab the reservation. We will want to release unused nodes,
	    # but only if not already assigned to the slice.
	    my $reservation = $node->Reservation();
	    if (defined($reservation)) {
		$tofree{$resource_uuid} = $node
		    if ($reservation->SameExperiment($reserved_holding));
	    }
	    $namemap{$node_nickname} = $node;
	    $colomap{$colocate} = $node
		if (defined($colocate));
	}
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
543

544
    foreach my $ref (@{$rspec->{'node'}}) {
545 546 547 548
	my $resource_uuid = $ref->{'component_uuid'} || $ref->{'uuid'};
	my $manager_uuid  = $ref->{'component_manager_uuid'};
	my $node_nickname = $ref->{'virtual_id'} || $ref->{'nickname'};
	my $colocate      = $ref->{'colocate'} || $ref->{'phys_nickname'};
Leigh B. Stoller's avatar
Leigh B. Stoller committed
549
	my $virtualization_type = $ref->{'virtualization_type'};
Leigh B. Stoller's avatar
Leigh B. Stoller committed
550 551
	my $node;

552 553 554 555 556 557 558 559 560 561
	# Let remote nodes pass through.
	next
	    if (defined($manager_uuid) && $manager_uuid ne $ENV{'MYUUID'});

	if (!defined($node_nickname)) {
	    $response = GeniResponse->Create(GENIRESPONSE_BADARGS, undef,
				     "Must provide a virtual_id for nodes");
	    goto bad;
	}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
562 563 564
	#
	# Mostly for debugging right now, allow a wildcard.
	#
565 566 567 568 569 570
	if (!defined($resource_uuid) || $resource_uuid eq "*") {
	    if (defined($colocate) && exists($colomap{$colocate})) {
		$node = $colomap{$colocate};
	    }
	    elsif ($isupdate && exists($namemap{$node_nickname})) {
		$node = $namemap{$node_nickname};
571 572 573 574
	    }
	    else {
		$node = FindFreeNode($virtualization_type, @nodeids);
	    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
575 576 577 578 579 580 581 582
	    
	    if (!defined($node)) {
		$response = GeniResponse->Create(GENIRESPONSE_UNAVAILABLE,
						 undef,
						 "No free nodes for wildcard");
		goto bad;
	    }
	    $resource_uuid = $node->uuid();
583 584
	    $ref->{'component_uuid'} = $node->uuid();
	    $ref->{'component_manager_uuid'} = $ENV{'MYUUID'};
Leigh B. Stoller's avatar
Leigh B. Stoller committed
585 586 587 588 589 590 591 592 593 594
	}
	else {
	    $node = Node->Lookup($resource_uuid);

	    if (!defined($node)) {
		$response =
		    GeniResponse->Create(GENIRESPONSE_BADARGS, undef,
					 "Bad resource $resource_uuid");
		goto bad;
	    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
595
	}
596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616

	#
	# Look for interface forward declarations that will be used later
	# in the link specifications. 
	#
	foreach my $linkref (@{$ref->{'interface'}}) {
	    my $component_id = $linkref->{"component_id"};
	    my $virtual_id   = $linkref->{"virtual_id"};

	    if (!defined($virtual_id)) {
		$response = GeniResponse->Create(GENIRESPONSE_BADARGS, undef,
			     "Must provide a virtual_id for interfaces");
		goto bad;
	    }
	    
	    $ifacemap{$node_nickname} = {}
	        if (!exists($ifacemap{$node_nickname}));

	    # Store reference so we can munge it below. 
	    $ifacemap{$node_nickname}->{$virtual_id} = $linkref;
	}
617
	
618 619 620 621 622 623
	#
	# Widearea nodes do not need to be allocated, but for now all
	# I allow is a plabdslice node.
	#
	if ($node->isremotenode()) {
	    if (! $node->isplabphysnode()) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
624 625 626 627
		$response =
		    GeniResponse->Create(GENIRESPONSE_BADARGS, undef,
					 "Only plab widearea nodes");
		goto bad;
628
	    }
629
	    goto skipalloc;
630
	}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
631 632

	#
633
	# See if the node is already reserved. 
Leigh B. Stoller's avatar
Leigh B. Stoller committed
634 635 636
	#
	my $reservation = $node->Reservation();
	if (defined($reservation)) {
637 638 639 640 641 642 643
	    # Reserved during previous ticket operation, or already
	    # assigned to the slice (ticket redeemed).
	    goto skipalloc
		if ($reservation->SameExperiment($reserved_holding) ||
		    (defined($slice_experiment) &&
		     $reservation->SameExperiment($slice_experiment)));
	    
Leigh B. Stoller's avatar
Leigh B. Stoller committed
644 645 646 647
	    $response =
		GeniResponse->Create(GENIRESPONSE_UNAVAILABLE, undef,
				     "$resource_uuid ($node) not available");
	    goto bad;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
648
	}
649 650 651 652 653
	# watch for duplicates, as for multiple vnodes on a pnode.
	push(@nodeids, $node->node_id())
	    if (! grep {$_ eq $node->node_id()} @nodeids);

      skipalloc:
654
	$namemap{$node_nickname} = $node
655
	    if (defined($node_nickname));
656 657 658 659
	$colomap{$colocate} = $node
	    if (defined($colocate));
	delete($tofree{$node->uuid()})
	    if ($isupdate && exists($tofree{$node->uuid()}));
Leigh B. Stoller's avatar
Leigh B. Stoller committed
660
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
661

Leigh B. Stoller's avatar
Leigh B. Stoller committed
662 663 664 665 666 667 668 669 670 671 672
    #
    # A sitevar controls how many total nodes external users can allocate.
    #
    # XXX All this policy stuff is a whack job for the initial release. 
    #
    my $max_externalnodes = 0;
    if (!TBGetSiteVar('protogeni/max_externalnodes', \$max_externalnodes)){
	# Cannot get the value, say none.
	$max_externalnodes = 0;
    }
    if (scalar(@nodeids) > $max_externalnodes) {
673 674 675
	$response = GeniResponse->Create(GENIRESPONSE_UNAVAILABLE, undef,
					 "Too many nodes; limited to $max_externalnodes");
	goto bad;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
676 677
    }
    # Check current usage by dipping into the libadminctrl library.
678 679 680
    my $curusage = libadminctrl::LoadCurrent($reserved_holding->creator(),
					     $reserved_holding->pid(),
					     $reserved_holding->gid());
Leigh B. Stoller's avatar
Leigh B. Stoller committed
681 682
    if (!defined($curusage)) {
	print STDERR "Could not get current usage from adminctl library\n";
683 684 685
	$response = GeniResponse->Create(GENIRESPONSE_UNAVAILABLE, undef,
					 "Temporarily unavailable");
	goto bad;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
686 687 688 689
    }
    if ($curusage->{"nodes"}->{'project'} + scalar(@nodeids) >=
	$max_externalnodes) {
	my $nodesleft = $max_externalnodes - $curusage->{"nodes"}->{'project'};
690 691 692
	$response = GeniResponse->Create(GENIRESPONSE_UNAVAILABLE, undef,
					 "Too many nodes; limited to $nodesleft");
	goto bad;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
693 694
    }

Leigh B. Stoller's avatar
Leigh B. Stoller committed
695
    # Nalloc might fail if the node gets picked up by someone else.
696
    if (@nodeids && !$impotent) {
697
	system("$NALLOC GeniSlices reservations @nodeids");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
698
	if (($? >> 8) < 0) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
699 700 701 702
	    $response =
		GeniResponse->Create(GENIRESPONSE_ERROR, undef,
				     "Allocation failure");
	    goto bad;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
703 704
	}
	elsif (($? >> 8) > 0) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
705 706 707 708
	    $response =
		GeniResponse->Create(GENIRESPONSE_UNAVAILABLE, undef,
				     "Could not allocate node");
	    goto bad;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
709
	}
710 711 712
	# In case the code below fails, before ticket is created.
	@dealloc = @nodeids;
    }
713 714 715
    goto skiplinks
	if (!exists($rspec->{'link'}));
    
716 717 718 719
    #
    # Now deal with links for wildcarded nodes. We need to fill in the
    # node_uuid.
    #
720 721 722 723
    foreach my $linkref (@{$rspec->{'link'}}) {
	my $nickname   = $linkref->{"nickname"} || $linkref->{"virtual_id"};
	my $interfaces = $linkref->{'linkendpoints'} ||
	    $linkref->{'interface_ref'};
724

725 726 727 728 729 730 731 732 733 734 735
	if (!defined($nickname)) {
	    $response = GeniResponse->Create(GENIRESPONSE_BADARGS, undef,
				     "Must provide a virtual_id for links");
	    goto bad;
	}
	
	foreach my $ref (@{ $interfaces }) {
	    my $node_nickname = $ref->{'virtual_node_id'} ||
		$ref->{'node_nickname'};
	    my $iface_id     = $ref->{'virtual_interface_id'} ||
		$ref->{'iface_name'};
736 737
	    my $istunnel      = (exists($linkref->{'link_type'}) &&
				 $linkref->{'link_type'} eq "tunnel");
738

739
	    if (!defined($node_nickname)) {
740 741 742 743 744 745 746 747 748
		$response =
		    GeniResponse->Create(GENIRESPONSE_ERROR, undef,
				 "$nickname: Need node id for links");
		goto bad;
	    }
	    if (!defined($iface_id)) {
		$response =
		    GeniResponse->Create(GENIRESPONSE_ERROR, undef,
				 "$nickname: Need interface id for links");
749 750 751
		goto bad;
	    }

752
	    if ($istunnel) {
753
		# Might be the other side. Skip for now; might bite later.
754 755
		next
		    if (!exists($namemap{$node_nickname}));
756

757 758 759 760 761 762 763 764 765
		# Not doing anything else.
		next;
	    }
	    if (!exists($namemap{$node_nickname})) {
		$response =
		    GeniResponse->Create(GENIRESPONSE_ERROR, undef,
				 "$nickname: No such virtual_node_id: ".
				 "$node_nickname:$iface_id");
		goto bad;
766
	    }
767
	    
768
	    #
769
	    # Sanity check the interface.
770
	    #
771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794
	    if (!exists($ifacemap{$node_nickname}->{$iface_id})) {
		$response =
		    GeniResponse->Create(GENIRESPONSE_ERROR, undef,
				 "$nickname: No such interface on component: ".
				 "$node_nickname:$iface_id");
		goto bad;
	    }
	    my $node = $namemap{$node_nickname};
	    my $iface_ref  = $ifacemap{$node_nickname}->{$iface_id};
	    my $iface_name = $iface_ref->{"component_id"};

	    # Grab all the interfaces.
	    my @interfaces;
	    if ($node->AllInterfaces(\@interfaces) != 0) {
		$response = GeniResponse->Create(GENIRESPONSE_ERROR, undef,
				 "Could not get interfaces for $node");
		goto bad;
	    }
	    # Make sure its valid, or fill in a wildcard.
	    foreach my $interface (@interfaces) {
		next
		    if (!defined($interface->switch_id()));
		next
		    if ($interface->role() ne "expt");
795

796 797 798 799 800 801
		if (defined($iface_name) && $iface_name ne "*") {

		}
		else {
		    $iface_name = $interface->iface();
		    $iface_ref->{"component_id"} = $iface_name;
802 803 804
		    last;
		}
	    }
805 806 807 808 809 810
	    if (!defined($iface_name)) {
		$response = GeniResponse->Create(GENIRESPONSE_ERROR, undef,
				 "$nickname: Could not assign wildcard: ".
				 "$node_nickname:$iface_id");
		goto bad;
	    }
811 812
	}
    }
813
  skiplinks:
814
    #
815
    # Create a new ticket.
816
    #
817 818
    my $newticket = GeniTicket->Create($authority, $user, $rspec);
    if (!defined($newticket)) {
819 820 821 822
	$response =
	    GeniResponse->Create(GENIRESPONSE_ERROR, undef,
				 "Could not create GeniTicket object");
	goto bad;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
823
    }
824
    if ($newticket->Sign() || $newticket->Store()) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
825 826 827
	$response = GeniResponse->Create(GENIRESPONSE_ERROR, undef,
					 "Could not sign Ticket");
	goto bad;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
828
    }
829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844

    if ($isupdate) {
	#
	# Delete (not release) the old ticket. 
	#
	$ticket->Delete(TICKET_RELEASED)
	    if ($ticket->stored());

	# And release any nodes we no longer wanted.
	if (keys(%tofree)) {
	    my @ids = map { $_->node_id() } values(%tofree);

	    system("export NORELOAD=1; ".
		   "$NFREE -x -q GeniSlices reservations @ids");
	}
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
845
    $slice->UnLock();
846
    return GeniResponse->Create(GENIRESPONSE_SUCCESS, $newticket->asString());
Leigh B. Stoller's avatar
Leigh B. Stoller committed
847
  bad:
848 849 850 851 852 853 854 855 856 857 858 859 860
    #
    # Have to be careful in a ticket update, to not release nodes that
    # might be referenced in the old ticket. 
    #
    if ($isupdate) {
	if (defined($newticket)) {
	    # Delete, not Release (which frees nodes).
	    $newticket->Delete(TICKET_PURGED);
	}
	if (@dealloc) {
	    system("export NORELOAD=1; ".
		   "$NFREE -x -q GeniSlices reservations @dealloc");
	}
861
    }
862 863 864 865 866 867 868 869 870
    else {
	# Release will free the nodes.
	if (defined($newticket)) {
	    $newticket->Release(TICKET_PURGED);
	}
	elsif (@dealloc) {
	    system("export NORELOAD=1; ".
		   "$NFREE -x -q GeniSlices reservations @dealloc");
	}
871
    }
872
  unlock:
Leigh B. Stoller's avatar
Leigh B. Stoller committed
873 874
    $slice->UnLock()
	if (defined($slice));
875 876
    $ticket->UnLock()
	if (defined($ticket) && $ticket->stored());
Leigh B. Stoller's avatar
Leigh B. Stoller committed
877
    return $response;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
878 879 880
}

#
881
# Redeem a ticket
Leigh B. Stoller's avatar
Leigh B. Stoller committed
882
#
883
sub RedeemTicket($)
Leigh B. Stoller's avatar
Leigh B. Stoller committed
884 885
{
    my ($argref) = @_;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
886

887
    return SliverWork($argref, 0);
888
}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
889

890
#
891
# Update a sliver
892 893 894 895
#
sub UpdateSliver($)
{
    my ($argref) = @_;
896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911

    return SliverWork($argref, 1);
}

#
# Utility function for above routines.
#
sub SliverWork($$)
{
    my ($argref, $isupdate) = @_;
    my $credstr    = $argref->{'credential'};
    my $ticketstr  = $argref->{'ticket'};
    my $impotent   = $argref->{'impotent'};
    my $keys       = $argref->{'keys'};
    my $extraargs  = $argref->{'extraargs'};
    my $didfwsetup = 0;
912 913 914 915

    $impotent = 0
	if (!defined($impotent));

916
    if (! (defined($credstr) && defined($ticketstr))) {
917 918
	return GeniResponse->Create(GENIRESPONSE_BADARGS);
    }
919
    my $credential = GeniCredential->CreateFromSigned($credstr);
920
    if (!defined($credential)) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
921
	return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
922 923 924 925 926 927 928 929 930
				    "Could not create GeniCredential object");
    }
    #
    # Make sure the credential was issued to the caller.
    #
    if ($credential->owner_uuid() ne $ENV{'GENIUUID'}) {
	return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
				    "This is not your credential!");
    }
931 932 933 934 935 936
    $credential->HasPrivilege( "pi" ) or
	$credential->HasPrivilege( "instantiate" ) or
	$credential->HasPrivilege( "control" ) or
	return GeniResponse->Create( GENIRESPONSE_FORBIDDEN, undef,
				     "Insufficient privilege" );

937 938 939 940 941 942 943 944
    my $owner_uuid = $credential->owner_uuid();
    my $owner_cert = $credential->owner_cert();
    my $message    = "Error creating sliver/aggregate";

    my $ticket = GeniTicket->CreateFromSignedTicket($ticketstr);
    if (!defined($ticket)) {
	return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
				    "Could not create GeniTicket object");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
945
    }
946 947 948 949
    # Only unredeemed tickets are stored in the DB.
    if (!$ticket->stored()) {
	return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
				    "This ticket was already redeemed!");
950
    }
951 952
    # Make sure the ticket was issued to the caller.
    if ($ticket->owner_uuid() ne $ENV{'GENIUUID'}) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
953
	return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998
				    "This is not your ticket!");
    }
    # Make sure the ticket target is this Manager
    if ($ticket->target_uuid() ne $ENV{'MYUUID'}) {
	return GeniResponse->Create(GENIRESPONSE_FORBIDDEN, undef,
				    "This ticket is for another authority!");
    }

    #
    # For now, there can be only a single toplevel aggregate per slice.
    #
    my $aggregate;
    my $slice;
    my $slice_uuid;
    
    if ($isupdate) {
	$aggregate = GeniAggregate->Lookup($credential->target_uuid());

	if (!defined($aggregate)) {
	    return GeniResponse->Create(GENIRESPONSE_BADARGS, undef,
					"Sliver cannot be found");
	}
	$slice_uuid = $aggregate->slice_uuid();
	$slice = GeniSlice->Lookup($slice_uuid);
	if (!defined($slice)) {
	    return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
					"No slice record for $slice_uuid");
	}
    }
    else {
	$slice_uuid = $credential->target_uuid();
	$slice = GeniSlice->Lookup($slice_uuid);
	if (!defined($slice)) {
	    return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
					"No slice record for $slice_uuid");
	}
	$aggregate  = GeniAggregate->SliceAggregate($slice);

	if (defined($aggregate)) {
	    return GeniResponse->Create(GENIRESPONSE_BADARGS, undef,
					"Already have an aggregate for slice");
	}
    }
    if ($ticket->Lock() != 0) {
	return GeniResponse->BusyResponse("ticket");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
999 1000
    }
    if ($slice->Lock() != 0) {
1001 1002 1003 1004 1005 1006 1007 1008 1009 1010
	$ticket->UnLock();
	return GeniResponse->BusyResponse("slice");
    }

    # Do not redeem an expired ticket.
    if ($ticket->Expired()) {
	$slice->UnLock();
	$ticket->UnLock();
	return GeniResponse->Create(GENIRESPONSE_EXPIRED, undef,
				    "Ticket has expired");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1011
    }
1012 1013 1014
    # Shutdown slices get nothing.
    if ($slice->shutdown()) {
	$slice->UnLock();
1015
	$ticket->UnLock();
1016 1017 1018
	return GeniResponse->Create(GENIRESPONSE_FORBIDDEN, undef,
				    "Slice has been shutdown");
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1019

1020
    #
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1021
    # Create the user.
1022 1023 1024
    #
    my $owner = GeniUser->Lookup($owner_uuid);
    if (!defined($owner)) {
1025
	$owner = CreateUserFromCertificate($owner_cert);
1026 1027 1028 1029 1030
	if (!defined($owner)) {
	    print STDERR "No user $owner_uuid in the ClearingHouse\n";
	    return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
					"No user record for $owner_uuid");
	}
1031
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1032 1033 1034
    if (!$owner->IsLocal() && defined($keys)) {
	$owner->Modify(undef, undef, $keys);
    }
1035

1036
    my $experiment = GeniExperiment($slice);
1037
    if (!defined($experiment)) {
1038 1039
	$slice->UnLock();
	$ticket->UnLock();
1040 1041 1042 1043 1044 1045 1046
	return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
				    "No local experiment for slice");
    }

    #
    # Figure out what nodes to allocate or free. 
    #
1047 1048 1049
    my %physnodes= ();
    my %nodemap  = ();
    my %linkmap  = ();
1050
    my %toalloc  = ();
1051
    my %colomap  = ();
1052
    my @allocated= ();
1053 1054 1055
    my @tofree   = ();
    my $pid      = $experiment->pid();
    my $eid      = $experiment->eid();
1056
    my $rspec    = $ticket->rspec();
1057
    my $needplabslice = 0;
1058 1059

    #
1060
    # Find current slivers and save.
1061
    #
1062
    if (defined($aggregate)) {
1063
	my @slivers;
1064 1065 1066
	if ($aggregate->SliverList(\@slivers) != 0) {
	    $message = "Could not get sliverlist for $aggregate";
	    goto bad;
1067 1068 1069
	}
	foreach my $s (@slivers) {
	    if (ref($s) eq "GeniSliver::Node") {
1070
		$nodemap{$s->nickname()} = $s;
1071
	    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1072 1073
	    elsif (ref($s) eq "GeniAggregate::Link" ||
		   ref($s) eq "GeniAggregate::Tunnel") {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1074 1075
		# XXX See the constructor in GeniAggregate.
		my ($linkname) = ($s->hrn() =~ /\.([-\w]*)$/);
1076
		$linkmap{$linkname} = $s;
1077 1078
	    }
	    else {
1079 1080
		$message = "Only nodes or links allowed";
		goto bad;
1081 1082 1083 1084
	    }
	}
    }

Leigh B. Stoller's avatar
Leigh B. Stoller committed
1085 1086 1087 1088 1089 1090 1091 1092
    #
    # Figure out new expiration time; this is the time at which we can
    # idleswap the slice out. 
    #
    if (exists($rspec->{'valid_until'})) {
	my $expires = $rspec->{'valid_until'};

	if (! ($expires =~ /^[-\w:.\/]+/)) {
1093 1094
	    $message = "Illegal valid_until in rspec";
	    goto bad;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1095 1096 1097 1098
	}
	# Convert to a localtime.
	my $when = timegm(strptime($expires));
	if (!defined($when)) {
1099 1100 1101
	    $message = "Could not parse valid_until";
	    goto bad;
					
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1102 1103 1104 1105 1106 1107
	}
	#
	# No more then 24 hours out ... Needs to be a sitevar?
	#
	my $diff = $when - time();
	if ($diff < (60 * 15) || $diff > (3600 * 24)) {
1108 1109
	    $message = "valid_until out of range";
	    goto bad;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1110 1111
	}
	if ($slice->SetExpiration($when) != 0) {
1112 1113
	    $message = "Could not set expiration time";
	    goto bad;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1114 1115 1116
	}
    }

1117 1118 1119 1120 1121 1122 1123 1124 1125 1126
    # Nodes are in this holding experiment.
    my $reserved_holding = Experiment->Lookup("GeniSlices", "reservations");
    if (!defined($reserved_holding)) {
	#
	# This experiment has to exist!
	#
	print STDERR "Could not find Geni reservations experiment!\n";
	goto bad;
    }

1127
    #
1128
    # Make sure all nodes requested are allocated. 
1129 1130
    #
    foreach my $ref (@{$rspec->{'node'}}) {
1131 1132 1133 1134 1135 1136 1137 1138
	my $resource_uuid = $ref->{'component_uuid'} || $ref->{'uuid'};
	my $node_nickname = $ref->{'virtual_id'} || $ref->{'nickname'};
	my $manager_uuid  = $ref->{'component_manager_uuid'};
	
	# Let remote nodes pass through.
	next
	    if (defined($manager_uuid) && $manager_uuid ne $ENV{'MYUUID'});

1139 1140
	my $node = Node->Lookup($resource_uuid);
	if (!defined($node)) {
1141 1142
	    $message = "Bad resource_uuid $resource_uuid";
	    goto bad;
1143 1144
	}

1145 1146 1147 1148 1149 1150
	#
	# Widearea nodes do not need to be allocated, but for now all
	# I allow is a plabdslice node.
	#
	if ($node->isremotenode()) {
	    if (! $node->isplabphysnode()) {
1151 1152
		$message = "Only plab widearea nodes";
		goto bad;
1153 1154