GeniCM.pm.in 37.2 KB
Newer Older
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
#!/usr/bin/perl -wT
#
# EMULAB-COPYRIGHT
# Copyright (c) 2008 University of Utah and the Flux Group.
# 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 libtestbed;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
34 35 36
# Hate to import all this crap; need a utility library.
use libdb qw(TBGetUniqueIndex TBcheck_dbslot TBDB_CHECKDBSLOT_ERROR);
use User;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
37
use Node;
38
use Interface;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
39 40
use English;
use Data::Dumper;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
41
use XML::Simple;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
42
use Experiment;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
43 44 45 46 47 48 49 50

# Configure variables
my $TB		   = "@prefix@";
my $TBOPS          = "@TBOPSEMAIL@";
my $TBAPPROVAL     = "@TBAPPROVALEMAIL@";
my $TBAUDIT   	   = "@TBAUDITEMAIL@";
my $BOSSNODE       = "@BOSSNODE@";
my $OURDOMAIN      = "@OURDOMAIN@";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
51
my $CREATEEXPT     = "$TB/bin/batchexp";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
52
my $ENDEXPT        = "$TB/bin/endexp";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
53
my $NALLOC	   = "$TB/bin/nalloc";
54
my $NFREE	   = "$TB/bin/nfree";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
55
my $AVAIL	   = "$TB/sbin/avail";
56
my $PTOPGEN	   = "$TB/libexec/ptopgen";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
57 58
my $TBSWAP	   = "$TB/bin/tbswap";
my $SWAPEXP	   = "$TB/bin/swapexp";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
59

Leigh B. Stoller's avatar
Leigh B. Stoller committed
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 122 123 124 125

#
# Respond to a GetTicket request. 
#
sub Resolve($)
{
    my ($argref) = @_;
    my $uuid       = $argref->{'uuid'};
    my $hrn        = $argref->{'hrn'};
    my $cred       = $argref->{'credential'};
    my $type       = $argref->{'type'};

    if (! defined($cred)) {
	return GeniResponse->MalformedArgsResponse();
    }
    if (! (defined($type) && ($type =~ /^(Node)$/))) {
	return GeniResponse->MalformedArgsResponse();
    }
    # Allow lookup by uuid or hrn.
    if (! (defined($uuid) || defined($hrn))) {
	return GeniResponse->MalformedArgsResponse();
    }
    if (defined($uuid) && !($uuid =~ /^[-\w]*$/)) {
	return GeniResponse->MalformedArgsResponse();
    }
    if (defined($hrn) && !($hrn =~ /^[-\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 =~ /\./) {
		($hrn) = ($hrn =~ /\.(\w*)$/);
	    }
	    $node= Node->Lookup($hrn);
	}
	if (!defined($node)) {
	    return GeniResponse->Create(GENIRESPONSE_SEARCHFAILED,
					undef, "Nothing here by that name");
	}
	
	# Return a blob.
	my $blob = { "hrn"          => "${OURDOMAIN}." . $node->node_id(),
		     "uuid"         => $node->uuid(),
126
		     "role"	    => $node->role(),
Leigh B. Stoller's avatar
Leigh B. Stoller committed
127 128 129 130 131 132 133 134 135 136 137 138 139
		   };

	#
	# 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) {
140 141 142
	    next
		if (!defined($interface->switch_id()));
		
Leigh B. Stoller's avatar
Leigh B. Stoller committed
143 144 145 146 147 148
	    my $iblob = { "uuid"	=> $interface->uuid(),
			  "iface"	=> $interface->iface(),
			  "type"	=> $interface->type(),
			  "card"	=> $interface->card(),
			  "port"	=> $interface->port(),
			  "role"	=> $interface->role(),
149 150
			  "IP"		=> $interface->IP() || "",
			  "mask"	=> $interface->mask() || "",
Leigh B. Stoller's avatar
Leigh B. Stoller committed
151
			  "MAC"		=> $interface->mac(),
152 153 154 155 156
			  "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
157 158 159 160 161 162 163 164 165 166 167 168
		      };

	    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
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
#
# 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.
184
    if ($user_uuid ne $credential->owner_uuid()) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
185 186 187 188 189
	return GeniResponse->Create(GENIRESPONSE_FORBIDDEN, undef,
				    "Invalid credentials for operation");
    }

    #
190
    # Use ptopgen in xml mode to spit back an xml file. 
Leigh B. Stoller's avatar
Leigh B. Stoller committed
191
    #
192
    if (! open(AVAIL, "$PTOPGEN -x -g -p GeniSlices |")) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
193 194 195
	return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
				    "Could not start avail");
    }
196
    my $xml = "";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
197
    while (<AVAIL>) {
198
	$xml .= $_;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
199 200 201 202 203
    }
    close(AVAIL);

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

Leigh B. Stoller's avatar
Leigh B. Stoller committed
205
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
206
# Respond to a GetTicket request. 
Leigh B. Stoller's avatar
Leigh B. Stoller committed
207 208 209 210 211
#
sub GetTicket($)
{
    my ($argref) = @_;
    my $rspec      = $argref->{'rspec'};
Leigh B. Stoller's avatar
Leigh B. Stoller committed
212
    my $impotent   = $argref->{'impotent'};
213
    my $cred       = $argref->{'credential'};
214
    my $vtopo      = $argref->{'virtual_topology'};
Leigh B. Stoller's avatar
Leigh B. Stoller committed
215
    my $owner_uuid = $ENV{'GENIUSER'};
Leigh B. Stoller's avatar
Leigh B. Stoller committed
216

217 218
    if (! defined($cred)) {
	return GeniResponse->MalformedArgsResponse();
Leigh B. Stoller's avatar
Leigh B. Stoller committed
219
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
220 221 222
    if (! (defined($rspec) && ($rspec =~ /^[-\w]+$/))) {
	GeniResponse->Create(GENIRESPONSE_BADARGS, undef,
			     "Improper rspec");	
Leigh B. Stoller's avatar
Leigh B. Stoller committed
223
    }
224
    $rspec = XMLin($rspec, ForceArray => ["node", "link"]);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
225

Leigh B. Stoller's avatar
Leigh B. Stoller committed
226 227 228
    $impotent = 0
	if (!defined($impotent));

229
    my $credential = GeniCredential->CreateFromSigned($cred);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
230 231 232 233
    if (!defined($credential)) {
	return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
				    "Could not create GeniCredential object");
    }
234 235 236 237 238 239 240
    my $slice_uuid = $credential->this_uuid();
    my $user_uuid  = $credential->owner_uuid();
    
    #
    # Make sure the credential was issued to the caller.
    #
    if ($credential->owner_uuid() ne $ENV{'GENIUUID'}) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
241
	return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
242
				    "This is not your credential!");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
243
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
244

Leigh B. Stoller's avatar
Leigh B. Stoller committed
245 246 247 248 249 250 251
    #
    # See if we have a record of this slice in the DB. If not, then we have
    # to go to the ClearingHouse to find its record, so that we can find out
    # who the SA for it is.
    #
    my $slice = GeniSlice->Lookup($slice_uuid);
    if (!defined($slice)) {
252
	$slice = CreateSliceFromRegistry($slice_uuid);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
253 254 255 256 257
	if (!defined($slice)) {
	    print STDERR "No slice $slice_uuid in the ClearingHouse\n";
	    return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
			    "Could not get slice info from ClearingHouse");
	}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
258
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
259
    else {
260
	UpdateSliceFromRegistry($slice) == 0 or
Leigh B. Stoller's avatar
Leigh B. Stoller committed
261 262 263
	    return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
			"Could not update slice info from ClearingHouse");
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
264

Leigh B. Stoller's avatar
Leigh B. Stoller committed
265
    #
Leigh B. Stoller's avatar
Leigh B. Stoller committed
266 267
    # Ditto the user.
    #
268
    my $user = GeniUser->Lookup($user_uuid);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
269
    if (!defined($user)) {
270
	$user = CreateUserFromRegistry($user_uuid);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
271
	if (!defined($user)) {
272
	    print STDERR "No user $user_uuid in the ClearingHouse\n";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
273 274 275 276 277
	    return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
			    "Could not get user info from ClearingHouse");
	}
    }

278
    my $experiment = GeniExperiment($slice_uuid);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
279
    if (!defined($experiment)) {
280 281
	return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
				    "Internal Error");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
282 283
    }

Leigh B. Stoller's avatar
Leigh B. Stoller committed
284
    #
Leigh B. Stoller's avatar
Leigh B. Stoller committed
285 286
    # 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
287 288 289 290
    # cannot be granted.
    #
    # XXX Simpleminded ... assumes only physical nodes for now. Need to deal
    # with virtual nodes (vservers on shared nodes, planetlab nodes, etc).
Leigh B. Stoller's avatar
Leigh B. Stoller committed
291
    #
Leigh B. Stoller's avatar
Leigh B. Stoller committed
292
    my @nodeids = ();
Leigh B. Stoller's avatar
Leigh B. Stoller committed
293 294 295
    my $pid     = $experiment->pid();
    my $eid     = $experiment->eid();

296 297
    foreach my $ref (@{$rspec->{'node'}}) {
	my $resource_uuid = $ref->{'uuid'};
298 299
	my $node = Node->Lookup($resource_uuid);
	if (!defined($node)) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
300
	    return GeniResponse->Create(GENIRESPONSE_BADARGS, undef,
301
					"Bad resource_uuid $resource_uuid");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
302
	}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
303 304 305 306 307 308 309 310 311

	#
	# See if the node is already reserved.
	#
	my $reservation = $node->Reservation();
	if (defined($reservation)) {
	    return GeniResponse->Create(GENIRESPONSE_UNAVAILABLE, undef,
				"$resource_uuid ($node) is not available");
	}
312
	push(@nodeids, $node->node_id());
Leigh B. Stoller's avatar
Leigh B. Stoller committed
313
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
314 315

    #
Leigh B. Stoller's avatar
Leigh B. Stoller committed
316
    # Create the ticket first, before allocating the node.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
317
    #
Leigh B. Stoller's avatar
Leigh B. Stoller committed
318
    my $ticket = GeniTicket->Create($slice, $user, $rspec);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
319 320 321 322
    if (!defined($ticket)) {
	return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
				    "Could not create GeniTicket object");
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
323
    # Nalloc might fail if the node gets picked up by someone else.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
324
    if (!$impotent) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
325
	system("$NALLOC $pid $eid @nodeids");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
326 327 328 329 330 331 332 333 334 335
	if (($? >> 8) < 0) {
	    $ticket->Delete();
	    return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
					"Allocation failure");
	}
	elsif (($? >> 8) > 0) {
	    $ticket->Delete();
	    return GeniResponse->Create(GENIRESPONSE_UNAVAILABLE, undef,
					"Could not allocate node\n");
	}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
336
    }
337 338 339 340 341 342 343
    if (defined($vtopo) && $experiment->InsertVirtTopo($vtopo) != 0) {
	# Release will free the nodes.
	$ticket->Release();
	return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
				    "Could not insert virt topology");
    }
    
Leigh B. Stoller's avatar
Leigh B. Stoller committed
344
    if ($ticket->Sign() != 0) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
345
	# Release will free the nodes.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
346
	$ticket->Release();
Leigh B. Stoller's avatar
Leigh B. Stoller committed
347 348 349
	return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
				    "Could not sign Ticket");
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
350 351 352 353 354 355 356
    return GeniResponse->Create(GENIRESPONSE_SUCCESS,
				$ticket->asString());
}

#
# Create a sliver.
#
357
sub RedeemTicket($)
Leigh B. Stoller's avatar
Leigh B. Stoller committed
358 359
{
    my ($argref) = @_;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
360
    my $ticket     = $argref->{'ticket'};
361
    my $impotent   = $argref->{'impotent'};
Leigh B. Stoller's avatar
Leigh B. Stoller committed
362
    my $extraargs  = $argref->{'extraargs'};
363 364 365

    $impotent = 0
	if (!defined($impotent));
Leigh B. Stoller's avatar
Leigh B. Stoller committed
366 367 368 369 370 371 372 373 374 375 376 377

    if (! (defined($ticket) &&
	   !TBcheck_dbslot($ticket, "default", "text",
			   TBDB_CHECKDBSLOT_ERROR))) {
	return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
				    "ticket: ". TBFieldErrorString());
    }
    $ticket = GeniTicket->CreateFromSignedTicket($ticket);
    if (!defined($ticket)) {
	return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
				    "Could not create GeniTicket object");
    }
378 379 380 381 382 383 384
    
    #
    # Make sure the credential was issued to the caller.
    #
    if ($ticket->owner_uuid() ne $ENV{'GENIUUID'}) {
	return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
				    "This is not your credential!");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
385
    }
386 387
    return ModifySliver(undef, $ticket, $ticket->rspec(), $impotent);
}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
388

389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
#
# Update a sliver with a different resource set.
#
sub UpdateSliver($)
{
    my ($argref) = @_;
    my $cred        = $argref->{'credential'};
    my $rspec       = $argref->{'rspec'};
    my $impotent    = $argref->{'impotent'};

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

    if (!defined($cred)) {
	return GeniResponse->Create(GENIRESPONSE_BADARGS);
    }
    if (! (defined($rspec) && ($rspec =~ /^[-\w]+$/))) {
	GeniResponse->Create(GENIRESPONSE_BADARGS, undef, "Improper rspec");
    }
    $rspec = XMLin($rspec, ForceArray => ["node", "link"]);

    my $credential = GeniCredential->CreateFromSigned($cred);
    if (!defined($credential)) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
412
	return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
				    "Could not create GeniCredential object");
    }
    my $sliver_uuid = $credential->this_uuid();
    my $user_uuid   = $credential->owner_uuid();
    
    #
    # 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!");
    }
    my $sliver = GeniSliver->Lookup($sliver_uuid);
    if (defined($sliver)) {
	return GeniResponse->Create(GENIRESPONSE_BADARGS, undef,
				    "Only aggregates for now");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
429
    }
430 431 432 433 434 435 436
    my $aggregate = GeniAggregate->Lookup($sliver_uuid);
    if (!defined($aggregate)) {
	return GeniResponse->Create(GENIRESPONSE_BADARGS, undef,
				    "No such aggregate $sliver_uuid");
    }
    return ModifySliver($aggregate, $credential, $rspec, $impotent);
}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
437

438 439 440 441 442 443 444 445 446 447 448 449
#
# Utility function for above routines.
#
sub ModifySliver($$$$)
{
    my ($object, $credential, $rspec, $impotent) = @_;

    my $owner_uuid = $credential->owner_uuid();
    my $message    = "Error creating sliver/aggregate";
    my $slice_uuid;
    my $aggregate;
    
450 451 452 453
    #
    # See if we have a record of this slice in the DB. If not, throw an
    # error; might change later.
    #
454 455 456 457 458 459 460 461
    if (defined($object)) {
	# We get the slice via the sliver/aggregate.
	$slice_uuid = $object->slice_uuid();
    }
    else {
	$slice_uuid = $credential->slice_uuid();
    }
    my $slice = GeniSlice->Lookup($slice_uuid);
462 463 464 465 466 467 468 469 470 471
    if (!defined($slice)) {
	return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
				    "No slice record for slice");
    }

    #
    # Ditto the user.
    #
    my $owner = GeniUser->Lookup($owner_uuid);
    if (!defined($owner)) {
472 473 474 475 476 477
	$owner = CreateUserFromRegistry($owner_uuid);
	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");
	}
478 479
    }

480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 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
    my $experiment = GeniExperiment($slice_uuid);
    if (!defined($experiment)) {
	return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
				    "No local experiment for slice");
    }
    print Dumper($rspec);

    #
    # Figure out what nodes to allocate or free. 
    #
    my %nodelist = ();
    my %linklist = ();
    my %toalloc  = ();
    my @tofree   = ();
    my $pid      = $experiment->pid();
    my $eid      = $experiment->eid();

    #
    # Find current nodes and record their uuids. 
    #
    if (defined($object)) {
	if ($object->type() eq "Link") {
	    return GeniResponse->Create(GENIRESPONSE_UNSUPPORTED, undef,
					"Only node aggregates allowed");
	}
	my @slivers;
	if ($object->SliverList(\@slivers) != 0) {
	    return GeniResponse->Create(GENIRESPONSE_ERROR, undef);
	}
	foreach my $s (@slivers) {
	    if (ref($s) eq "GeniSliver::Node") {
		$nodelist{$s->resource_uuid()} = $s;
	    }
	    elsif (ref($s) eq "GeniAggregate::Link") {
		$linklist{$s->uuid()} = $s;
	    }
	    else {
		return GeniResponse->Create(GENIRESPONSE_UNSUPPORTED, undef,
					    "Only nodes or links allowed");
	    }
	}
    }

    #
    # Figure out what nodes need to be allocated.
    #
    foreach my $ref (@{$rspec->{'node'}}) {
	my $resource_uuid = $ref->{'uuid'};
	my $node = Node->Lookup($resource_uuid);
	if (!defined($node)) {
	    return GeniResponse->Create(GENIRESPONSE_BADARGS, undef,
					"Bad resource_uuid $resource_uuid");
	}

	#
	# See if the node is already reserved. 
	#
	my $reservation = $node->Reservation();
	if (defined($reservation)) {
	    # Reserved during previous operation on the sliver.
	    next
		if ($reservation->SameExperiment($experiment));
	    
	    return GeniResponse->Create(GENIRESPONSE_UNAVAILABLE, undef,
				"$resource_uuid ($node) is not available");
	}
	#
	# Sanity check on the list of already allocated nodes.
	#
	foreach my $s (values(%nodelist)) {
	    if ($resource_uuid eq $s->resource_uuid()) {
		print STDERR
		    "$resource_uuid is not supposed to be allocated\n";
		return GeniResponse->Create(GENIRESPONSE_ERROR, undef);
	    }
	}
	$toalloc{$resource_uuid} = $node->node_id();
    }

    #
    # What nodes need to be released?
    #
    foreach my $s (values(%nodelist)) {
	my $node_uuid = $s->resource_uuid();
	my $node      = Node->Lookup($node_uuid);
	my $needfree  = 1;
	
	foreach my $ref (@{$rspec->{'node'}}) {
	    my $resource_uuid = $ref->{'uuid'};
	    if ($node_uuid eq $resource_uuid) {
		$needfree = 0;
		last;
	    }
	}
	if ($needfree) {
	    #
	    # Not yet.
	    #
	    my @dlist;
	    if ($s->DependentSlivers(\@dlist) != 0) {
		return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
					    "Could not get DependentSlivers");
	    }
	    if (@dlist) {
		return GeniResponse->Create(GENIRESPONSE_REFUSED, undef,
					    "Must tear dow dependent slivers");
	    }
	    push(@tofree, $s);
	}
    }

Leigh B. Stoller's avatar
Leigh B. Stoller committed
591 592 593 594 595 596
    #
    # Create an emulab nonlocal user for tmcd.
    #
    $owner->BindToSlice($slice) == 0
	or return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
				       "Error binding user to slice");
597

Leigh B. Stoller's avatar
Leigh B. Stoller committed
598 599 600 601 602 603 604 605
    # Bind the other users too.
    my @userbindings;
    if ($slice->UserBindings(\@userbindings) != 0) {
	return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
				    "Error binding users to slice");
    }
    foreach my $otheruuid (@userbindings) {
	my $otheruser = GeniUser->Lookup($otheruuid);
606 607 608 609 610 611 612
	if (!defined($otheruser)) {
	    $otheruser = CreateUserFromRegistry($otheruuid);
	    if (!defined($otheruser)) {
		print STDERR "No user $otheruser, cannot bind to slice\n";
		next;
	    }
	}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
613
	if ($otheruser->BindToSlice($slice) != 0) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
614 615 616 617
	    print STDERR "Could not bind $otheruser to $slice\n";
	}
    }

Leigh B. Stoller's avatar
Leigh B. Stoller committed
618
    #
Leigh B. Stoller's avatar
Leigh B. Stoller committed
619
    # We are actually an Aggregate, so return an aggregate of slivers,
620
    # even if there is just one node. This makes sliceupdate easier.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
621
    #
622 623 624 625 626 627
    if (defined($object)) {
	$aggregate = $object;
    }
    else {
	$aggregate = GeniAggregate->Create($slice, $owner,
					   "Aggregate", $slice->hrn());
628 629
	if (!defined($aggregate)) {
	    return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
630 631 632 633 634 635 636 637 638 639 640 641 642
				"Could not create GeniAggregate object");
	}
    }

    # Nalloc might fail if the node gets picked up by someone else.
    if (values(%toalloc) && !$impotent) {
	my @list = values(%toalloc);
	system("$NALLOC $pid $eid @list");
	if ($?) {
	    # Nothing to deallocate if this fails.
	    %toalloc = undef;
	    $message = "Allocation failure";
	    goto bad;
643
	}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
644
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
645

646 647 648 649 650 651 652 653 654 655 656 657
    #
    # We need to tear down links that are no longer in the rspec or
    # have changed.
    #
    foreach my $s (values(%linklist)) {
	my $needfree  = 1;

	if (! exists($rspec->{'link'}->{$s->hrn()})) {
	    $s->UnProvision();
	    $s->Delete();
	    next;
	}
658

659 660 661 662 663 664 665 666 667 668 669 670 671 672 673
	my $delete     = 0;
	my @interfaces = ();
	if ($s->SliverList(\@interfaces) != 0) {
	    $message = "Failed to get sliverlist for $s";
	    goto bad;
	}
	foreach my $i (@interfaces) {
	    my $node_uuid   = $i->resource_uuid();
	    my $iface_name  = $i->rspec()->{'iface_name'};

	    my $linkendpoints =
		$rspec->{'link'}->{$s->hrn()}->{'LinkEndPoints'};
	}
    }
    
Leigh B. Stoller's avatar
Leigh B. Stoller committed
674
    #
Leigh B. Stoller's avatar
Leigh B. Stoller committed
675 676
    # Now for each resource (okay, node) in the ticket create a sliver and
    # add it to the aggregate.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
677
    #
678
    my %slivers = ();
679
    foreach my $ref (@{$rspec->{'node'}}) {
680
	my $resource_uuid = $ref->{'uuid'};
681 682 683 684
	# Already in the aggregate?
	next
	    if (grep {$_ eq $resource_uuid} keys(%nodelist));
	
685 686 687
	my $node = Node->Lookup($resource_uuid);
	if (!defined($node)) {
	    $message = "Unknown resource_uuid in ticket: $resource_uuid";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
688 689
	    goto bad;
	}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
690 691 692 693
	my $sliver = GeniSliver::Node->Create($slice,
					      $owner->uuid(),
					      $resource_uuid,
					      $ref);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
694 695 696 697
	if (!defined($sliver)) {
	    $message = "Could not create GeniSliver object for $resource_uuid";
	    goto bad;
	}
698
	$slivers{$resource_uuid} = $sliver;
699 700 701 702 703 704 705

	#
	# Remove this from %toalloc; if there is an error, the slivers are
	# deleted and the node released there. We only delete nodes that
	# have not turned into slivers yet. Ick.
	#
	delete($toalloc{$resource_uuid});
Leigh B. Stoller's avatar
Leigh B. Stoller committed
706
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
707

708 709 710 711 712
    #
    # Now do the links. For each link, we have to add a sliver for the
    # interfaces, and then combine those two interfaces into an aggregate,
    # and then that aggregate goes into the aggregate for toplevel sliver.
    #
713
    foreach my $linkname (keys(%{$rspec->{'link'}})) {
714 715 716 717 718 719
	my @linkslivers  = ();
	if (! ($linkname =~ /^[-\w]*$/)) {
	    $message = "Bad name for link: $linkname";
	    goto bad;
	}

720 721
	my $linkaggregate = GeniAggregate::Link->Create($slice, $owner,
							$linkname);
722 723 724 725 726 727 728
	if (!defined($linkaggregate)) {
	    $message = "Could not create link aggregate for $linkname";
	    goto bad;
	}
	$slivers{$linkaggregate->uuid()} = $linkaggregate;

	my $linkendpoints =
729
	    $rspec->{'link'}->{$linkname}->{'LinkEndPoints'};
730
	
731 732 733 734
	foreach my $ifacename (keys(%{ $linkendpoints })) {
	    my $iface      = $linkendpoints->{$ifacename};
	    my $node_uuid  = $iface->{'node_uuid'};
	    my $iface_name = $iface->{'iface_name'};
735
	    my $nodesliver = $slivers{$node_uuid} || $nodelist{$node_uuid};
736 737 738 739 740 741 742 743 744
	    if (!defined($nodesliver)) {
		$message = "Link $linkname specifies a non-existent node";
		goto bad;
	    }
	    my $nodeobject= Node->Lookup($node_uuid);
	    if (!defined($nodeobject)) {
		$message = "Could not find node object for $node_uuid";
		goto bad;
	    }
745
	    my $interface = Interface->LookupByIface($nodeobject, $iface_name);
746
	    if (!defined($interface)) {
747
		$message = "No such interface $iface_name on node $nodeobject";
748 749
		goto bad;
	    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
750 751 752
	    my $sliver = GeniSliver::Interface->Create($slice,
						       $owner->uuid(),
						       $interface->uuid(),
753
						       $node_uuid,
754
						       $iface);
755 756 757 758 759 760 761 762 763 764 765 766
	    if (!defined($sliver)) {
		$message = "Could not create GeniSliver ".
		    "$interface in $linkname";
		goto bad;
	    }
	    if ($sliver->SetAggregate($linkaggregate) != 0) {
		$message = "Could not add link sliver $sliver to $aggregate";
		goto bad;
	    }
	}
    }

Leigh B. Stoller's avatar
Leigh B. Stoller committed
767
    #
768 769
    # Now do the provisioning (note that we actually allocated the
    # node above when the ticket was granted). Then add the sliver to
770
    # the aggregate. 
Leigh B. Stoller's avatar
Leigh B. Stoller committed
771
    #
772
    foreach my $sliver (values(%slivers)) {
773
	if (!$impotent && $sliver->Provision() != 0) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
774 775
	    $message = "Could not provision $sliver";
	    goto bad;
776

Leigh B. Stoller's avatar
Leigh B. Stoller committed
777
	}
778 779
	if ($sliver->SetAggregate($aggregate) != 0) {
	    $message = "Could not set aggregate for $sliver to $aggregate";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
780 781 782 783
	    goto bad;
	}
    }

Leigh B. Stoller's avatar
Leigh B. Stoller committed
784
    #
785
    # The API states we return a credential to control the aggregate.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
786
    #
787 788
    if (ref($credential) eq "GeniTicket") {
	my $ticket  = $credential;
789
	$credential = $aggregate->NewCredential($owner);
790 791 792 793 794 795 796 797 798 799 800 801 802 803
	if (!defined($credential)) {
	    $message = "Could not create credential";
	    goto bad;
	}
    
	#
	# The last step is to delete the ticket, since it is no longer needed.
	# and will cause less confusion if it is not in the DB.
	#
	if ($ticket->Delete() != 0) {
	    print STDERR "Error deleting $ticket for $slice\n";
	}
	return GeniResponse->Create(GENIRESPONSE_SUCCESS,
				    $credential->asString());
Leigh B. Stoller's avatar
Leigh B. Stoller committed
804
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
805
    #
806
    # Free any slivers that were no longer wanted.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
807
    #
808 809
    if (@tofree) {

Leigh B. Stoller's avatar
Leigh B. Stoller committed
810
    }
811
    return GeniResponse->Create(GENIRESPONSE_SUCCESS);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
812 813

  bad:
814
    foreach my $sliver (values(%slivers)) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
815 816 817 818
	$sliver->UnProvision()
	    if (! $impotent);
	$sliver->Delete();
    }
819 820 821 822 823
    if (values(%toalloc)) {
	my @list = values(%toalloc);

	system("export NORELOAD=1; $NFREE -x -q $pid $eid @list");
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
824
    $aggregate->Delete()
825
	if (defined($aggregate) && !defined($object));
Leigh B. Stoller's avatar
Leigh B. Stoller committed
826 827
    
    return GeniResponse->Create(GENIRESPONSE_ERROR, undef, $message);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
828 829
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863
#
# Release a ticket.
#
sub ReleaseTicket($)
{
    my ($argref) = @_;
    my $ticket     = $argref->{'ticket'};

    if (! (defined($ticket) &&
	   !TBcheck_dbslot($ticket, "default", "text",
			   TBDB_CHECKDBSLOT_ERROR))) {
	return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
				    "ticket: ". TBFieldErrorString());
    }
    $ticket = GeniTicket->CreateFromSignedTicket($ticket);
    if (!defined($ticket)) {
	return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
				    "Could not create GeniTicket object");
    }
    
    #
    # Make sure the credential was issued to the caller.
    #
    if ($ticket->owner_uuid() ne $ENV{'GENIUUID'}) {
	return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
				    "This is not your credential!");
    }
    if ($ticket->Release() != 0) {
	print STDERR "Error releasing $ticket\n";
	return GeniResponse->Create(GENIRESPONSE_ERROR);
    }
    return GeniResponse->Create(GENIRESPONSE_SUCCESS);
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
864
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
865
# Start a sliver (not sure what this means yet, so reboot for now).
Leigh B. Stoller's avatar
Leigh B. Stoller committed
866
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
867
sub StartSliver($)
Leigh B. Stoller's avatar
Leigh B. Stoller committed
868 869
{
    my ($argref) = @_;
870 871
    my $cred        = $argref->{'credential'};
    my $impotent    = $argref->{'impotent'};
Leigh B. Stoller's avatar
Leigh B. Stoller committed
872 873 874

    $impotent = 0
	if (!defined($impotent));
Leigh B. Stoller's avatar
Leigh B. Stoller committed
875

876
    if (!defined($cred)) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
877 878
	return GeniResponse->Create(GENIRESPONSE_BADARGS);
    }
879
    my $credential = GeniCredential->CreateFromSigned($cred);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
880 881 882 883
    if (!defined($credential)) {
	return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
				    "Could not create GeniCredential object");
    }
884 885 886 887 888 889 890 891 892 893
    my $sliver_uuid = $credential->this_uuid();
    my $user_uuid   = $credential->owner_uuid();
    
    #
    # 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!");
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
894 895
    my $sliver = GeniSliver->Lookup($sliver_uuid);
    if (!defined($sliver)) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
896 897 898 899 900 901
	# Might be an aggregate instead.
	$sliver = GeniAggregate->Lookup($sliver_uuid);
	if (!defined($sliver)) {
	    return GeniResponse->Create(GENIRESPONSE_BADARGS, undef,
				    "No such sliver/aggregate $sliver_uuid");
	}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
902
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
903
    if (!$impotent) {
904
	$sliver->Start() == 0 or
Leigh B. Stoller's avatar
Leigh B. Stoller committed
905 906 907 908 909 910 911 912 913
	    return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
				"Could not start sliver/aggregate");
    }
    return GeniResponse->Create(GENIRESPONSE_SUCCESS);
}

#
# Destroy a sliver/aggregate.
#
914
sub DeleteSliver($)
Leigh B. Stoller's avatar
Leigh B. Stoller committed
915 916
{
    my ($argref) = @_;
917 918
    my $cred        = $argref->{'credential'};
    my $impotent    = $argref->{'impotent'};
Leigh B. Stoller's avatar
Leigh B. Stoller committed
919 920 921 922

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

923
    if (!defined($cred)) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
924 925
	return GeniResponse->Create(GENIRESPONSE_BADARGS);
    }
926
    my $credential = GeniCredential->CreateFromSigned($cred);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
927
    if (!defined($credential)) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
928
	return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
Leigh B. Stoller's avatar
Leigh B. Stoller committed
929
				    "Could not create GeniCredential object");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
930
    }
931 932
    my $sliver_uuid = $credential->this_uuid();
    my $user_uuid   = $credential->owner_uuid();
933
    my @slivers     = ();
934 935 936 937 938 939 940 941
    
    #
    # 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!");
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
942 943 944
    my $sliver = GeniSliver->Lookup($sliver_uuid);
    if (!defined($sliver)) {
	# Might be an aggregate instead.
945 946
	my $aggregate = GeniAggregate->Lookup($sliver_uuid);
	if (!defined($aggregate)) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
947 948 949
	    return GeniResponse->Create(GENIRESPONSE_BADARGS, undef,
				    "No such sliver/aggregate $sliver_uuid");
	}
950
	push(@slivers, $aggregate);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
951
    }
952 953
    else {
	# 
954 955 956 957
	# Find dependent slivers first (say, links on a node). For now,
	# do not allow this sliver to be torn down until the dependent
	# sliver(s) are torn down first. Eventually we want to tear them
	# down here in the proper order, hence the code below.
958 959
	#
	if ($sliver->DependentSlivers(\@slivers) != 0) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
960
	    return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
961 962
					"Could not get DependentSlivers");
	}
963 964 965 966 967
	if (@slivers) {
	    return GeniResponse->Create(GENIRESPONSE_REFUSED, undef,
					"Must tear dow dependent slivers");
	}
	@slivers = (@slivers, $sliver);	
968 969 970 971 972 973 974 975 976 977 978
    }

    foreach $sliver (@slivers) {
	if (!$impotent) {
	    $sliver->UnProvision() == 0 or
		return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
					    "Could not unprovision sliver");
	}
	$sliver->Delete() == 0 or
	    return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
					"Could not delete sliver");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
979
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
980
    return GeniResponse->Create(GENIRESPONSE_SUCCESS);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
981
}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
982

983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003
#
# Utility Routines.
#
# Create a slice from the ClearingHouse, by looking up the info.
#
sub CreateSliceFromRegistry($)
{
    my ($slice_uuid) = @_;

    my $blob;
    return undef
	if (GeniCHClient::LookupSlice($slice_uuid, \$blob) != 0);

    my $authority = GeniAuthority->Lookup($blob->{'sa_uuid'});
    if (!defined($authority)) {
	$authority = CreateAuthorityFromRegistry($blob->{'sa_uuid'});
	if (!defined($authority)) {
	    print STDERR "Could not create new authority record\n";
	    return undef;
	}
    }
1004

Leigh B. Stoller's avatar
Leigh B. Stoller committed
1005 1006
    print Dumper($blob);

1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029
    #
    # The problem with HRNs is that people will tend to reuse them!
    # So check to see if we have a slice with the hrn, and if so
    # we need to check with the registry to see if its still active.
    # Destroy it locally if not active.
    #
    my $slice = GeniSlice->Lookup($blob->{'hrn'});
    if (defined($slice)) {
	return $slice
	    if ($slice->uuid() eq $slice_uuid);
	
	my $blob2;
	if (GeniCHClient::LookupSlice($slice->uuid(), \$blob2) == 0) {
	    print STDERR "hrn already in use for $slice!\n";
	    return undef;
	}
	return undef
	    if (CleanupDeadSlice($slice) != 0);
    }
    $slice = GeniSlice->Create($blob->{'hrn'},
			       $blob->{'uuid'},
			       $blob->{'creator_uuid'},
			       $blob->{'cert'}, $authority);
1030 1031 1032 1033 1034 1035 1036 1037 1038
    return undef
	if (!defined($slice));

    # Add the user bindings.
    foreach my $uuid (@{ $blob->{'userbindings'} }) {
	my $user = GeniUser->Lookup($uuid);
	if (!defined($user)) {
	    $user = CreateUserFromRegistry($uuid);
	    if (!defined($user)) {
1039
		print STDERR "No user $uuid locally or in the ClearingHouse\n";
1040 1041 1042
		next;
	    }
	}
1043
	$slice->BindUser($user) == 0
1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076
	    or print STDERR
	    "Could not insert user binding for $uuid to slice $slice_uuid\n";
    }
    return $slice;
}

#
# Update slice from the ClearingHouse, by looking up the info.
#
sub UpdateSliceFromRegistry($)
{
    my ($slice) = @_;
    my $slice_uuid = $slice->uuid();

    my $blob;
    return -1
	if (GeniCHClient::LookupSlice($slice_uuid, \$blob) != 0);

    DBQueryWarn("delete from geni_bindings ".
		"where slice_uuid='$slice_uuid'")
	or print STDERR
	"Could not delete user bindings from slice $slice_uuid\n";

    # Add the user bindings.
    foreach my $uuid (@{ $blob->{'userbindings'} }) {
	my $user = GeniUser->Lookup($uuid);
	if (!defined($user)) {
	    $user = CreateUserFromRegistry($uuid);
	    if (!defined($user)) {
		print STDERR "No user $uuid in the ClearingHouse\n";
		next;
	    }
	}
1077
	$slice->BindUser($user) == 0
1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090
	    or print STDERR
	    "Could not insert user binding for $uuid to slice $slice_uuid\n";
    }
    return 0;
}

#
# Create a user from the ClearingHouse, by looking up the info.
#
sub CreateUserFromRegistry($)
{
    my ($uuid) = @_;

1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103
    #
    # Check Emulab users table first.
    #
    my $user = User->LookupByUUID($uuid);
    if (defined($user)) {
	my $geniuser = GeniUser->CreateFromLocal($user);
	if (!defined($geniuser)) {
	    print STDERR "Could not create geniuser from $user\n";
	    return undef;
	}
	return $geniuser;
    }

1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115
    my $blob;
    return undef
	if (GeniCHClient::LookupUser($uuid, \$blob) != 0);

    my $authority = GeniAuthority->Lookup($blob->{'sa_uuid'});
    if (!defined($authority)) {
	$authority = CreateAuthorityFromRegistry($blob->{'sa_uuid'});
	if (!defined($authority)) {
	    print STDERR "Could not create new authority record\n";
	    return undef;
	}
    }
1116 1117
    print STDERR Dumper($blob);
    
1118 1119 1120 1121 1122 1123
    return GeniUser->Create($blob->{'hrn'},
			    $blob->{'uid'},
			    $blob->{'uuid'},
			    $blob->{'name'},
			    $blob->{'email'},
			    $blob->{'cert'},
1124
			    $authority, $blob->{'sliverkeys'});
1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143
}

#
# Create authority from the ClearingHouse, by looking up the info.
#
sub CreateAuthorityFromRegistry($)
{
    my ($uuid) = @_;

    my $blob;
    return undef
	if (GeniCHClient::Resolve($uuid, "SA", \$blob) != 0);

    return GeniAuthority->Create($uuid,
				 $blob->{'hrn'},
				 $blob->{'url'},
				 $blob->{'cert'},
				 $blob->{'uuid_prefix'}, "sa");
}
1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161

#
# Cleanup a dead slice, releasing all the stuff associated with it.
#
sub CleanupDeadSlice($)
{
    my ($slice) = @_;

    print STDERR "Cleaning up dead slice $slice\n";

    #
    # Find any aggregates and tear them down.
    #
    my @aggregates;
    if (GeniAggregate->SliceAggregates($slice, \@aggregates) != 0) {
	print STDERR "Could not get dead aggregates for $slice.\n";
	return -1;
    }
1162 1163 1164 1165 1166
    #
    # Link aggregates first.
    #
    my @nonlinks = ();
    
1167
    foreach my $aggregate (@aggregates) {
1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181
	if ($aggregate->type() ne "Link") {
	    push(@nonlinks, $aggregate);
	    next;
	}
	if ($aggregate->UnProvision() != 0) {
	    print STDERR "Could not UnProvision $aggregate\n";
	    return -1;
	}
	if ($aggregate->Delete() != 0) {
	    print STDERR "Could not delete $aggregate\n";
	    return -1;
	}
    }
    foreach my $aggregate (@nonlinks) {
1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226
	if ($aggregate->UnProvision() != 0) {
	    print STDERR "Could not UnProvision $aggregate\n";
	    return -1;
	}
	if ($aggregate->Delete() != 0) {
	    print STDERR "Could not delete $aggregate\n";
	    return -1;
	}
    }

    #
    # Are there any slivers left after killing the aggregates? 
    #
    my @slivers;
    if (GeniSliver->SliceSlivers($slice, \@slivers) != 0) {
	print STDERR "Could not get dead slivers for $slice.\n";
	return -1;
    }
    foreach my $sliver (@slivers) {
	if ($sliver->UnProvision() != 0) {
	    print STDERR "Could not UnProvision $sliver\n";
	    return -1;
	}
	if ($sliver->Delete() != 0) {
	    print STDERR "Could not delete $sliver\n";
	    return -1;
	}
    }

    #
    # And lastly, any tickets that were not instantiated. 
    #
    my @tickets;
    if (GeniTicket->SliceTickets($slice, \@tickets) != 0) {
	print STDERR "Could not get dead tickets for $slice.\n";
	return -1;
    }
    foreach my $ticket (@tickets) {
	print STDERR "Releasing $ticket\n";
	
	if ($ticket->Release() != 0) {
	    print STDERR "Could not delete $ticket\n";
	    return -1;
	}
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237

    my $experiment = $slice->GetExperiment();
    if (!defined($experiment)) {
	print STDERR "Could not get experiment for $slice\n";
	return -1;
    }
    # Note the -h option; allows experiment with no NS file.
    system("$ENDEXPT -q -w " . $experiment->idx());
    return -1
	if ($?);
    
1238 1239 1240 1241 1242 1243
    if ($slice->Delete() != 0) {
	print STDERR "Could not delete $slice\n";
	return -1;
    }
    return 0;
}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288

#
# Remove a record, specifically a slice on this component.
#
sub DeleteSlice($)
{
    my ($argref) = @_;
    my $credential = $argref->{'credential'};

    if (! defined($credential)) {
	return GeniResponse->MalformedArgsResponse();
    }

    $credential = GeniCredential->CreateFromSigned($credential);
    if (!defined($credential)) {
	return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
				    "Could not create GeniCredential object");
    }

    my $slice_uuid = $credential->this_uuid();
    
    #
    # 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!");
    }

    #
    # See if we have a record of this slice in the DB. If not, then we have
    # to go to the ClearingHouse to find its record, so that we can find out
    # who the SA for it is.
    #
    my $slice = GeniSlice->Lookup($slice_uuid);
    if (!defined($slice)) {
	return GeniResponse->Create(GENIRESPONSE_BADARGS, undef,
			    "No such slice on this component: $slice_uuid");
    }
    if (CleanupDeadSlice($slice) != 0) {
	return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
				    "Could not cleanup slice");
    }
    return GeniResponse->Create(GENIRESPONSE_SUCCESS);
}
1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312