GeniSlice.pm.in 32.8 KB
Newer Older
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1 2
#!/usr/bin/perl -wT
#
3
# Copyright (c) 2008-2013 University of Utah and the Flux Group.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
# 
# {{{GENIPUBLIC-LICENSE
# 
# GENI Public License
# 
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and/or hardware specification (the "Work") to
# deal in the Work without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Work, and to permit persons to whom the Work
# is furnished to do so, subject to the following conditions:
# 
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Work.
# 
# THE WORK IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE WORK OR THE USE OR OTHER DEALINGS
# IN THE WORK.
# 
# }}}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
29 30 31 32 33
#
package GeniSlice;

use strict;
use Exporter;
34
use vars qw(@ISA);
35
@ISA = "Exporter";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
36 37 38

# Must come after package declaration!
use GeniDB;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
39
use GeniRegistry;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
40 41
use GeniAuthority;
use GeniCredential;
42
use GeniCertificate;
43
use GeniUser;
44
use GeniHRN;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
45
use English;
46
use libEmulab;
47
use emutil qw(TBGetUniqueIndex);
48
use Date::Parse;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
49
use Data::Dumper;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
50
use vars qw();
51
use overload ('""' => 'Stringify');
Leigh B. Stoller's avatar
Leigh B. Stoller committed
52 53 54 55 56 57 58 59 60

# Configure variables
my $TB		   = "@prefix@";
my $TBOPS          = "@TBOPSEMAIL@";
my $TBAPPROVAL     = "@TBAPPROVALEMAIL@";
my $TBAUDIT   	   = "@TBAUDITEMAIL@";
my $BOSSNODE       = "@BOSSNODE@";
my $CONTROL	   = "@USERNODE@";
my $OURDOMAIN      = "@OURDOMAIN@";
61
my $PGENIDOMAIN    = "@PROTOGENI_DOMAIN@";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
62 63 64

# Cache of instances to avoid regenerating them.
my %slices     = ();
65
BEGIN { use GeniUtil; GeniUtil::AddCache(\%slices); }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
66 67 68 69 70 71 72 73 74 75 76 77 78
my $debug      = 0;

# Little helper and debug function.
sub mysystem($)
{
    my ($command) = @_;

    print STDERR "Running '$command'\n"
	if ($debug);
    return system($command);
}

#
79
# Lookup by idx, URN or uuid.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
80 81 82 83
#
sub Lookup($$)
{
    my ($class, $token) = @_;
84 85
    my $query_result;
    my $idx;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
86

87 88
    return $slices{"$token"}
        if (exists($slices{"$token"}));
Leigh B. Stoller's avatar
Leigh B. Stoller committed
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 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
    if( GeniHRN::IsValid( $token ) ) {
	my ($authority, $type, $id) = GeniHRN::Parse( $token );

	return undef if $type ne "slice";
	
	if( GeniHRN::Authoritative( $token, "@OURDOMAIN@" ) ) {
	    # Good: this is one of our slices, and we can confidently
	    # transform the name for direct lookup.  This method is
	    # very general, and will even resolve a URN to a (deprecated)
	    # pre-URN slice.
	    $query_result =
		DBQueryWarn("select idx from geni_slices ".
			    "where hrn='@PROTOGENI_DOMAIN@.$id'");
	} else {
	    # Somebody else's slice.  We'll have to look up the slice
	    # by its certificate, which will work only for post-URN slices.
	    $query_result = DBQueryWarn(
		"SELECT geni_slices.idx FROM geni_slices, geni_certificates " .
		"WHERE geni_slices.uuid = geni_certificates.uuid AND " .
		"geni_certificates.urn='$token';" );
	}

	return undef if (! $query_result || !$query_result->numrows);

	($idx) = $query_result->fetchrow_array();
    }
    elsif ($token =~ /^\d+$/) {
	$idx = $token;
    }
    elsif ($token =~ /^\w+\-\w+\-\w+\-\w+\-\w+$/) {
	$query_result =
	    DBQueryWarn("select idx from geni_slices ".
			"where uuid='$token'");
	    return undef
		if (! $query_result || !$query_result->numrows);

	    ($idx) = $query_result->fetchrow_array();
    }
    elsif ($token =~ /^[-\w\.]*$/) {
	$query_result =
	    DBQueryWarn("select idx from geni_slices ".
			"where hrn='$token'");
	    return undef
		if (! $query_result || !$query_result->numrows);

	    ($idx) = $query_result->fetchrow_array();
    }
    else {
	return undef;
    }

    $query_result =
	DBQueryWarn("select * from geni_slices where idx='$idx'");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
143
    return undef
144
	if (!$query_result || !$query_result->numrows);
145

146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
    my $self         = {};
    $self->{'SLICE'} = $query_result->fetchrow_hashref();
    bless($self, $class);

    #
    # Grab the certificate, since we will probably want it.
    #
    my $uuid = $self->{'SLICE'}->{'uuid'};
    my $certificate = GeniCertificate->Lookup($uuid);
    if (!defined($certificate)) {
	print STDERR "Could not find certificate for slice $idx ($uuid)\n";
	return undef;
    }
    $self->{'CERT'}     = $certificate;
    $self->{'BINDINGS'} = undef;
    $self->{'LOCKED'}   = 0;
162
    $self->{'STITCHLOCKED'} = 0;
163
    
Leigh B. Stoller's avatar
Leigh B. Stoller committed
164
    # Add to cache. 
165 166 167 168
    $slices{$self->idx()}  = $self;
    $slices{$self->urn()}  = $self;
    $slices{$self->uuid()} = $self;
    $slices{$self->hrn()}  = $self;
169
    
170
    return $self;
171 172 173 174 175
}

#
# Class function to create new Geni slice and return the object.
#
176
sub Create($$$$;$$)
177
{
178
    my ($class, $certificate, $creator, $authority, $exptidx, $lock) = @_;
179
    my @insert_data = ();
180 181 182 183 184 185 186
    # This is in hours.
    my $default_slice_lifetime;
    if (!libEmulab::GetSiteVar('protogeni/default_slice_lifetime', 
			       \$default_slice_lifetime)) {
	# Cannot get the value, default it to 6 hours
	$default_slice_lifetime = 6;
    }
187

188 189
    # Every slice gets a new unique index.
    my $idx = TBGetUniqueIndex('next_exptidx', 1);
190

191 192 193 194 195 196 197 198
    if (!defined($authority)) {
	print STDERR "Need to specify an authority!\n";
	return undef;
    }
    my $sa_uuid = $authority->uuid();

    # Now tack on other stuff we need.
    push(@insert_data, "created=now()");
199 200
    push(@insert_data, "expires=DATE_ADD(now(), ".
	                       "INTERVAL $default_slice_lifetime HOUR)");
201 202 203
    push(@insert_data, "sa_uuid='$sa_uuid'");
    push(@insert_data, "exptidx=$exptidx")
	if (defined($exptidx));
204 205 206 207 208 209 210

    # This is no good; should not create slices locked without
    # making sure this is really the creator. see below
    if (defined($lock) && $lock) {
	push(@insert_data, "locked=now()");
	push(@insert_data, "stitch_locked=now()");
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
211
    
212 213 214 215
    my $safe_hrn   = DBQuoteSpecial($certificate->hrn());
    my $safe_uuid  = DBQuoteSpecial($certificate->uuid());
    push(@insert_data, "hrn=$safe_hrn");
    push(@insert_data, "uuid=$safe_uuid");
216 217 218 219 220 221 222 223 224 225 226 227

    if (defined($creator)) {
	my $safe_cuuid = DBQuoteSpecial($creator->uuid());
	my $safe_curn  = DBQuoteSpecial($creator->urn());
	push(@insert_data, "creator_uuid=$safe_cuuid");
	push(@insert_data, "creator_urn=$safe_curn");
    }
    else {
	# No creator means this did not come from the actual user.
	push(@insert_data, "creator_uuid=''");
	push(@insert_data, "isplaceholder='1'");
    }
228 229 230 231 232 233

    if ($certificate->Store() != 0) {
	print STDERR "Could not store certificate for new slice.\n";
	return undef;
    }

234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
    #
    # Lock the table and look for an existing entry. If there is one,
    # and it is the placeholder entry, overwrite it.
    #
    DBQueryWarn("lock tables geni_slices write")
	or return undef;
    my $query_result =
	DBQueryWarn("select idx,isplaceholder from geni_slices " .
		    "where uuid=$safe_uuid or hrn=$safe_hrn");
    if (!$query_result) {
	DBQueryWarn("unlock tables");
	return undef;
    }
    if ($query_result->numrows) {
	if ($query_result->numrows != 1) {
	    my $urn  = $certificate->urn();
	    my $uuid = $certificate->uuid();
	    my $hrn  = $certificate->hrn();
	    print STDERR "Too many rows for $uuid/$hrn/$urn\n";
	    DBQueryWarn("unlock tables");
	    return undef;
	}
	my ($curidx,$isplaceholder) = $query_result->fetchrow_array();
	#
	# If the existing entry is a placeholder, and we got the creator,
	# then replace the existing entry to make it a non-placeholder.
	#
	if ($isplaceholder && defined($creator)) {
	    push(@insert_data, "idx='$curidx'");
	    
	    if (! DBQueryWarn("replace into geni_slices set " .
			      "  isplaceholder=0, ".
			      join(",", @insert_data))) {
		DBQueryWarn("unlock tables");
		return undef;
	    }
	    $idx = $curidx;
	}
272 273
	# Slice exists, no inserts needed. Just lookup. 
	goto lookup;
274 275 276
    }
    # Do this after above code since we might need to overwrite entry.
    push(@insert_data, "idx='$idx'");
277

278 279 280 281 282 283
    # Insert into DB.
    if (! DBQueryWarn("insert into geni_slices set " .
		      join(",", @insert_data))) {
        DBQueryWarn("unlock tables");
	return undef;
    }
284
  lookup:
285
    DBQueryWarn("unlock tables");
Leigh B Stoller's avatar
Leigh B Stoller committed
286 287 288 289
    my $slice = GeniSlice->Lookup($idx);
    return undef
	if (!defined($slice));

290
    $slice->{'LOCKED'} = $slice->{'STITCHLOCKED'} = $$
Leigh B Stoller's avatar
Leigh B Stoller committed
291 292
	if (defined($lock) && $lock);
    return $slice;
293 294 295 296 297 298
}
# accessors
sub field($$) { return ((! ref($_[0])) ? -1 : $_[0]->{'SLICE'}->{$_[1]}); }
sub idx($)		{ return field($_[0], "idx"); }
sub hrn($)		{ return field($_[0], "hrn"); }
sub uuid($)		{ return field($_[0], "uuid"); }
299
sub publicid($)		{ return field($_[0], "publicid"); }
300 301 302 303
sub creator_uuid($)	{ return field($_[0], "creator_uuid"); }
sub creator_urn($)	{ return field($_[0], "creator_urn"); }
sub created($)		{ return field($_[0], "created"); }
sub shutdown($)		{ return field($_[0], "shutdown"); }
304
sub isshutdown($)	{ return field($_[0], "isshutdown"); }
305 306 307 308 309
sub expires($)		{ return field($_[0], "expires"); }
sub sa_uuid($)		{ return field($_[0], "sa_uuid"); }
sub exptidx($)		{ return field($_[0], "exptidx"); }
sub needsfirewall($)	{ return field($_[0], "needsfirewall"); }
sub registered($)	{ return field($_[0], "registered"); }
310
sub isplaceholder($)	{ return field($_[0], "isplaceholder"); }
311
sub monitor_pid($)	{ return field($_[0], "monitor_pid"); }
312 313
sub speaksfor_urn($)	{ return field($_[0], "speaksfor_urn"); }
sub speaksfor_uuid($)	{ return field($_[0], "speaksfor_uuid"); }
314 315 316
sub cert($)		{ return $_[0]->{'CERT'}->cert(); }
sub GetCertificate($)   { return $_[0]->{'CERT'}; }
sub LOCKED($)           { return $_[0]->{'LOCKED'}; }
317
sub STITCHLOCKED($)     { return $_[0]->{'STITCHLOCKED'}; }
318

319 320 321 322 323 324 325 326 327
#
# Stringify for output.
#
sub Stringify($)
{
    my ($self) = @_;
    
    my $hrn = $self->hrn();
    my $idx = $self->idx();
328

329
    return "[GeniSlice: $hrn, IDX: $idx]";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
330
}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
331

Leigh B. Stoller's avatar
Leigh B. Stoller committed
332
#
333
# Delete the slice, as for registration errors.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
334
#
335
sub Delete($)
Leigh B. Stoller's avatar
Leigh B. Stoller committed
336 337
{
    my ($self) = @_;
338 339 340 341 342 343 344 345 346

    return -1
	if (! ref($self));

    my $uuid = $self->uuid();
    my $idx  = $self->idx();

    DBQueryWarn("delete from geni_bindings where slice_uuid='$uuid'")
	or return -1;
347 348 349 350
    DBQueryWarn("delete from geni_certificates where uuid='$uuid'")
	or return -1;
    DBQueryWarn("delete from geni_slices where idx='$idx'")
	or return -1;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
351

352 353
    # Delete from cache. 
    delete($slices{$idx});
354 355 356
    delete($slices{$self->urn()});
    delete($slices{$self->uuid()});
    delete($slices{$self->hrn()});
357

358
    return 0;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
359 360
}

361 362 363 364 365 366 367 368 369 370 371 372
# The slicename is the last token in the hrn.
sub slicename($)
{
    my ($self) = @_;

    my ($slicename) = ($self->hrn() =~ /^.*\.(\w*)$/);
    $slicename = $self->hrn()
	if (!defined($slicename));

    return $slicename;
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
373 374 375 376 377 378
#
# Return the URN. This is complicated by the fact that the DB does
# not store the urn, but is in the certificate. Further, it might
# be a slice from an SA not doing URNs yet, in which case set it to
# the uuid and hope for the best.
#
379 380 381
sub urn($)
{
    my ($self) = @_;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
382
    my $urn = $self->GetCertificate()->urn();
383

Leigh B. Stoller's avatar
Leigh B. Stoller committed
384 385 386 387
    return $urn
	if (defined($urn) && $urn ne "");

    return $self->uuid();
388 389
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
#
# Lookup slice by the experiment it is related to.
#
sub LookupByExperiment($$)
{
    my ($class, $experiment) = @_;

    my $exptidx = $experiment->idx();
    my $query_result =
	DBQueryWarn("select idx from geni_slices ".
		    "where exptidx='$exptidx'");
    return undef
	if (!defined($query_result) || !$query_result->numrows);

    my ($idx) = $query_result->fetchrow_array();
    return GeniSlice->Lookup($idx);
}

408 409 410 411 412 413
#
# Lookup all slice(s) of a specified creator.
#
sub LookupByCreator($$)
{
    my ($class, $creator) = @_;
414
    
415 416 417 418 419 420
    my $creator_uuid = $creator->uuid();
    my $query_result =
	DBQueryWarn("select idx from geni_slices ".
		    "where creator_uuid='$creator_uuid'");
    return undef unless defined($query_result);

421 422 423 424 425 426 427
    my @result = ();
    while (my ($idx) = $query_result->fetchrow_array()) {
	my $slice = GeniSlice->Lookup($idx);
	push(@result, $slice)
	    if (defined($slice));
    }
    return @result;
428 429
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
#
# We lock at a very coarse grain, mostly in the CM. When a slice is busy
# we cannot expire things from it.
#
sub Lock($)
{
    my ($self) = @_;
    my $idx    = $self->idx();

    # We already have it locked.
    return 0
	if ($self->LOCKED());

    DBQueryWarn("lock tables geni_slices write")
	or return -1;

446
    # Slice lock always takes both locks. Holder can drop the stitch lock.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
447
    my $query_result =
448 449 450
	DBQueryWarn("select locked,stitch_locked from geni_slices ".
		    "where idx='$idx' and ".
		    "      locked is null and stitch_locked is null");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
451 452 453 454 455
    if (!$query_result || !$query_result->numrows) {
	DBQueryWarn("unlock tables");
	return 1;
    }
    $query_result =
456 457
	DBQueryWarn("update geni_slices set ".
		    "locked=now(),stitch_locked=now() where idx='$idx'");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
458 459 460 461 462
    DBQueryWarn("unlock tables");

    return 1
	if (!$query_result);
    $self->{'LOCKED'} = $$;
463
    $self->{'STITCHLOCKED'} = $$;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
464 465 466 467 468 469 470 471 472 473
    return 0;
}
sub UnLock($)
{
    my ($self) = @_;
    my $idx    = $self->idx();

    return 1
	if (!$self->LOCKED());

474 475
    DBQueryWarn("update geni_slices set ".
		"locked=NULL,stitch_locked=NULL where idx='$idx'")
Leigh B. Stoller's avatar
Leigh B. Stoller committed
476 477 478
	or return -1;
    
    $self->{'LOCKED'} = 0;
479 480 481 482
    $self->{'STITCHLOCKED'} = 0;
    return 0;
}

483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502
#
# Wait for lock with timeout (in seconds).
#
sub WaitForLock($$)
{
    my ($self, $timeout) = @_;

    return $self->Lock()
	if (! $timeout);
	
    while ($timeout >= 0) {
	return 0
	    if ($self->Lock() == 0);
	
	$timeout--;
	sleep(1);
    }
    return -1;
}

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
#
# The stitching lock is used solely for controlling concurrency related
# to stitching.
#
sub StitchLock($)
{
    my ($self) = @_;
    my $idx    = $self->idx();

    # We already have it locked.
    return 0
	if ($self->STITCHLOCKED());

    DBQueryWarn("lock tables geni_slices write")
	or return -1;

    my $query_result =
	DBQueryWarn("select stitch_locked from geni_slices ".
		    "where idx='$idx' and ".
		    "      stitch_locked is null");
    if (!$query_result || !$query_result->numrows) {
	DBQueryWarn("unlock tables");
	return 1;
    }
    $query_result =
	DBQueryWarn("update geni_slices set ".
		    " stitch_locked=now() where idx='$idx'");
    DBQueryWarn("unlock tables");

    return 1
	if (!$query_result);
    $self->{'STITCHLOCKED'} = $$;
    return 0;
}
sub StitchUnLock($)
{
    my ($self) = @_;
    my $idx    = $self->idx();

    return 1
	if (!$self->STITCHLOCKED());

    DBQueryWarn("update geni_slices set ".
		" stitch_locked=NULL where idx='$idx'")
	or return -1;
    
    $self->{'STITCHLOCKED'} = 0;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
550 551 552
    return 0;
}

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
#
# LockTables simply locks the geni_slices table.
#
sub LockTables($)
{
    my ($self) = @_;

    # Must be a real reference. 
    return -1
	if (! ref($self));

    DBQueryWarn("lock tables geni_slices write")
	or return -1;
	
    return 0;
}
sub UnLockTables($)
{
    my ($self) = @_;

    # Must be a real reference. 
    return -1
	if (! ref($self));

    DBQueryWarn("unlock tables")
	or return -1;
    return 0;
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
582 583
#
# Class function to create new Geni slice from a local experiment.
584
# We want to create the key pair so that we can sign credentials.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
585
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
586
sub CreateFromLocal($$$)
Leigh B. Stoller's avatar
Leigh B. Stoller committed
587
{
588
    my ($class, $experiment, $geniuser) = @_;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
589

590 591 592 593 594 595
    #
    # So we know who/what we are acting as.
    #
    my $EMULAB_PEMFILE = "@prefix@/etc/genisa.pem";
    my $certificate = GeniCertificate->LoadFromFile($EMULAB_PEMFILE);
    if (!defined($certificate)) {
596
	print STDERR "Could not load certificate from $EMULAB_PEMFILE\n";
597 598
	return undef;
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
599

600
    #
601
    # Need our own slice authority record.
602
    #
603
    my $authority = GeniAuthority->Lookup($certificate->uuid());
604 605 606 607
    if (!defined($authority)) {
	print STDERR "Could not find the local authority record\n";
	return undef;
    }
608 609

    #
610
    # This mirrors the code in GeniSA.pm
611
    #
612 613 614
    my $hrn = "CE" . $experiment->idx();
    my $urn = GeniHRN::Generate("@OURDOMAIN@", "slice", $hrn);
    $hrn = "${PGENIDOMAIN}.${hrn}";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
615 616

    #
617
    # Generate a certificate.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
618
    #
619 620
    $certificate = GeniCertificate->Create({'urn'  => $urn,
					    'hrn'  => $hrn,
621
					    'showuuid' => 1,
622
					    'email'=> $geniuser->email()});
623
    if (!defined($certificate)) {
624
	print STDERR "GeniSlice::CreateFromLocal: ".
625
	    "Could not generate new certificate $experiment\n";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
626 627
	return undef;
    }
628
    # Create the slice as locked.
629
    my $slice = GeniSlice->Create($certificate, $geniuser,
630
				  $authority, $experiment->idx(), 1);
631 632 633 634
    $certificate->Delete()
	if (!defined($slice));

    return $slice;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
635 636 637 638 639 640 641 642
}

#
# Register a local slice at the clearinghouse;
#
sub Register($)
{
    my ($self) = @_;
643
    my $idx    = $self->idx();
Leigh B. Stoller's avatar
Leigh B. Stoller committed
644 645 646 647

    return -1
	if (! ref($self));

Leigh B. Stoller's avatar
Leigh B. Stoller committed
648 649 650
    my $clearinghouse = GeniRegistry::ClearingHouse->Create();
    return -1
	if (!defined($clearinghouse));
651

652 653 654 655 656
    my $creator = GeniUser->Lookup($self->creator_uuid(), 1);
    if (!defined($creator)) {
	print STDERR "Could not find creator for $self\n";
	return -1;
    }
657 658 659 660 661 662 663 664
    if (!$clearinghouse->RegisterSlice($creator->urn(), $self->expires(),
				       $self->cert(), {})) {
	DBQueryWarn("update geni_slices set registered=1 where idx='$idx'")
	    or return -1;
	return 0;
    }
    $self->SetRegisteredFlag(1);
    return -1;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
665 666
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
667
#
668
# Remove a local slice at the clearinghouse;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
669
#
670
sub UnRegister($)
Leigh B. Stoller's avatar
Leigh B. Stoller committed
671 672 673 674
{
    my ($self) = @_;

    return -1
675 676
	if (! ref($self));

Leigh B. Stoller's avatar
Leigh B. Stoller committed
677 678 679
    my $clearinghouse = GeniRegistry::ClearingHouse->Create();
    return -1
	if (!defined($clearinghouse));
680

681 682 683 684 685
    return -1
	if ($clearinghouse->RemoveSlice($self->urn()));
    $self->SetRegisteredFlag(0);

    return 0;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
686 687
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
688 689 690 691 692 693 694 695 696 697
#
# Flush from our little cache, as for the expire daemon.
#
sub Flush($)
{
    my ($self) = @_;

    delete($slices{$self->idx()});
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
698
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
699
# Return the emulab experiment for this slice.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
700
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
701
sub GetExperiment($)
Leigh B. Stoller's avatar
Leigh B. Stoller committed
702
{
Leigh B. Stoller's avatar
Leigh B. Stoller committed
703
    my ($self) = @_;
704
    require Experiment;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
705

Leigh B. Stoller's avatar
Leigh B. Stoller committed
706 707
    return undef
	if (!ref($self));
Leigh B. Stoller's avatar
Leigh B. Stoller committed
708

Leigh B. Stoller's avatar
Leigh B. Stoller committed
709
    return Experiment->Lookup($self->uuid());
Leigh B. Stoller's avatar
Leigh B. Stoller committed
710 711
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
712 713 714 715 716 717 718 719 720 721 722 723 724
#
# Return the slice authority for this slice.
#
sub SliceAuthority($)
{
    my ($self) = @_;

    return undef
	if (!ref($self));

    return GeniAuthority->Lookup($self->sa_uuid());
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
725
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
726
# Check if the given SA is the actual SA for the slice.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
727
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
728
sub IsSliceAuthority($$)
Leigh B. Stoller's avatar
Leigh B. Stoller committed
729
{
Leigh B. Stoller's avatar
Leigh B. Stoller committed
730
    my ($self, $authority) = @_;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
731

Leigh B. Stoller's avatar
Leigh B. Stoller committed
732 733
    return 0
	if (! (ref($self) && ref($authority)));
Leigh B. Stoller's avatar
Leigh B. Stoller committed
734

Leigh B. Stoller's avatar
Leigh B. Stoller committed
735
    return 1
736
	if ($self->sa_uuid() == $authority->uuid());
Leigh B. Stoller's avatar
Leigh B. Stoller committed
737 738
    
    return 0;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
739 740
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
741
#
742 743
# Server side of binding users to slices; insert entries into the bindings
# table.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
744 745 746 747 748 749 750 751
# 
sub BindUser($$)
{
    my ($self, $target_user) = @_;

    return -1
	if (! (ref($self) && ref($target_user)));

752 753 754 755 756 757 758
    my $slice_uuid = $self->uuid();
    my $user_uuid  = $target_user->uuid();

    DBQueryWarn("replace into geni_bindings set ".
		" created=now(), slice_uuid='$slice_uuid', ".
		" user_uuid='$user_uuid'")
	or return -1;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
759 760 761 762 763 764 765 766 767 768 769
    
    return 0;
}

sub UnBindUser($$)
{
    my ($self, $target_user) = @_;

    return -1
	if (! (ref($self) && ref($target_user)));

770 771
    my $slice_uuid = $self->uuid();
    my $user_uuid  = $target_user->uuid();
Leigh B. Stoller's avatar
Leigh B. Stoller committed
772

773 774 775 776
    DBQueryWarn("delete from geni_bindings ".
		"where slice_uuid='$slice_uuid' and user_uuid='$user_uuid'")
	or return -1;
    
Leigh B. Stoller's avatar
Leigh B. Stoller committed
777 778 779
    return 0;
}

780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798
#
# Unbind all users.
#
sub UnBindUsers($)
{
    my ($self) = @_;

    return -1
	if (! ref($self));

    my $slice_uuid = $self->uuid();

    DBQueryWarn("delete from geni_bindings ".
		"where slice_uuid='$slice_uuid'")
	or return -1;
    
    return 0;
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
799
#
800 801
# Return the user bindings for a slice, as a list of uuids. Do not look
# them up here since this routine is called from the CH.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
802 803 804 805 806 807 808 809 810 811
#
sub UserBindings($$)
{
    my ($self, $pref) = @_;
    
    return -1
	if (! (ref($self) && ref($pref)));

    my $uuid = $self->uuid();

812
    if (!defined($self->{'BINDINGS'})) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
813 814 815 816 817 818
	my $query_result =
	    DBQueryWarn("select user_uuid from geni_bindings ".
			"where slice_uuid='$uuid'");
	return -1
	    if (!$query_result);

819
	my @bindings = ();
Leigh B. Stoller's avatar
Leigh B. Stoller committed
820 821

	while (my ($user_uuid) = $query_result->fetchrow_array()) {
822
	    push(@bindings, $user_uuid);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
823
	}
824
	$self->{'BINDINGS'} = \@bindings;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
825
    }
826
    @$pref = @{ $self->{'BINDINGS'} };
Leigh B. Stoller's avatar
Leigh B. Stoller committed
827 828 829
    return 0;
}

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 864 865 866 867 868 869 870 871
#
# Is the user bound to a slice.
#
sub IsBound($$)
{
    my ($self, $user) = @_;
    
    return -1
	if (! (ref($self) && ref($user)));

    my $slice_uuid = $self->uuid();
    my $user_uuid  = $user->uuid();

    my $query_result =
	DBQueryWarn("select user_uuid from geni_bindings ".
		    "where slice_uuid='$slice_uuid' and ".
		    "      user_uuid='$user_uuid'");
    return 0
	if (!$query_result);
    return $query_result->numrows();
}

#
# Set the expiration time for a slice. 
#
sub SetExpiration($$)
{
    my ($self, $expires) = @_;
    my $uuid = $self->uuid();

    if ($expires =~ /^\d+$/) {
	$expires = "FROM_UNIXTIME($expires)";
    }
    else {
	$expires = "'$expires'";
    }
    my $query_result =
	DBQueryWarn("update geni_slices set expires=$expires " .
		    "where uuid='$uuid'");
    
    return -1
	if (!$query_result);
872 873 874 875 876 877 878 879

    # Has to be in the correct format.
    $query_result =
	DBQueryWarn("select expires from geni_slices where uuid='$uuid'");
    return -1
	if (!$query_result || !$query_result->numrows);
    ($expires) = $query_result->fetchrow_array();
    
880
    $self->{'SLICE'}->{'expires'} = $expires;
881 882 883
    return 0;
}

884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902
#
# Is slice expired?
#
sub IsExpired($)
{
    my ($self) = @_;

    return 0
	if (! ref($self));

    my $slice_expires = $self->expires();
    return 0
	if (!defined($slice_expires));
    
    $slice_expires = str2time($slice_expires);

    return (time() >= $slice_expires);
}

903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926
#
# Set the speaksfor stuff.
#
sub SetSpeaksFor($$)
{
    my ($self, $speaksfor) = @_;
    my $uuid = $self->uuid();
    my $safe_speaksfor_uuid = DBQuoteSpecial($speaksfor->owner_uuid());
    my $safe_speaksfor_urn  = DBQuoteSpecial($speaksfor->owner_urn());

    print "GeniSlice->SetSpeaksFor($self, $speaksfor)\n";

    return -1
	if (!DBQueryWarn("update geni_slices set " .
			 "  speaksfor_uuid=$safe_speaksfor_uuid , ".
			 "  speaksfor_urn=$safe_speaksfor_urn ".
			 "where uuid='$uuid'"));

    $self->{'SLICE'}->{'speaksfor_urn'}  = $speaksfor->owner_urn();
    $self->{'SLICE'}->{'speaksfor_uuid'} = $speaksfor->owner_uuid();

    return 0;
}

927 928 929 930 931 932 933 934 935 936 937 938 939 940 941
#
# Set the shutdown field.
#
sub SetShutdown($$)
{
    my ($self, $shutdown) = @_;
    my $uuid = $self->uuid();
    my $when;

    if ($shutdown) {
	$when = "now()";
    }
    else {
	$when = "NULL";
    }
942 943 944
    #
    # Always clear isshutdown so that expire_daemon knows of change.
    #
945
    my $query_result =
946
	DBQueryWarn("update geni_slices set shutdown=$when, isshutdown=0 " .
947 948 949 950
		    "where uuid='$uuid'");
    
    return -1
	if (!$query_result);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
951

952
    # XXX Wrong format, but harmless.
953 954
    $self->{'SLICE'}->{'shutdown'}   = ($shutdown ? time() : undef);
    $self->{'SLICE'}->{'isshutdown'} = 0;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
955 956 957
    return 0;
}

Leigh B Stoller's avatar
Leigh B Stoller committed
958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981
#
# Set the experiment pointer from a slice to the emulab experiment.
#
sub SetExperiment($$)
{
    my ($self, $experiment) = @_;
    my $uuid  = $self->uuid();
    my $token = "NULL";

    if (defined($experiment)) {
	$token = "'" . $experiment->idx() . "'";
    }
    my $query_result =
	DBQueryWarn("update geni_slices set exptidx=$token " .
		    "where uuid='$uuid'");
    
    return -1
	if (!$query_result);

    $self->{'SLICE'}->{'exptidx'} =
	(defined($experiment) ? $experiment->idx() : undef);
    return 0;
}

982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002
#
# Set the needsfirewall field.
#
sub SetFirewallFlag($$)
{
    my ($self, $needsfirewall) = @_;
    my $uuid = $self->uuid();

    $needsfirewall = ($needsfirewall ? 1 : 0);

    my $query_result =
	DBQueryWarn("update geni_slices set needsfirewall='$needsfirewall' " .
		    "where uuid='$uuid'");
    
    return -1
	if (!$query_result);

    $self->{'SLICE'}->{'needsfirewall'} = $needsfirewall;
    return 0;
}

1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023
#
# Set the registered field.
#
sub SetRegisteredFlag($$)
{
    my ($self, $registered) = @_;
    my $uuid  = $self->uuid();

    $registered = ($registered ? 1 : 0);

    my $query_result =
	DBQueryWarn("update geni_slices set registered='$registered' " .
		    "where uuid='$uuid'");
    
    return -1
	if (!$query_result);

    $self->{'SLICE'}->{'registered'} = $registered;
    return 0;
}

1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044
#
# Set the isshutdown field.
#
sub SetShutdownFlag($$)
{
    my ($self, $isshutdown) = @_;
    my $uuid  = $self->uuid();

    $isshutdown = ($isshutdown ? 1 : 0);

    my $query_result =
	DBQueryWarn("update geni_slices set isshutdown='$isshutdown' " .
		    "where uuid='$uuid'");
    
    return -1
	if (!$query_result);

    $self->{'SLICE'}->{'isshutdown'} = $isshutdown;
    return 0;
}

1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068
#
# Convert from placeholder to real slice. Caller will have it locked.
#
sub ConvertPlaceholder($$)
{
    my ($self, $user) = @_;
    my $uuid  = $self->uuid();

    my $safe_cuuid = DBQuoteSpecial($user->uuid());
    my $safe_curn  = DBQuoteSpecial($user->urn());

    my $query_result =
	DBQueryWarn("update geni_slices set isplaceholder=0, " .
		    "  creator_uuid=$safe_cuuid, creator_urn=$safe_curn ".
		    "where uuid='$uuid'");
    return -1
	if (!$query_result);

    $self->{'SLICE'}->{'isplaceholder'} = 0;
    $self->{'SLICE'}->{'creator_urn'}   = $user->urn();
    $self->{'SLICE'}->{'creator_uuid'}  = $user->uuid();
    return 0;
}

1069 1070 1071 1072 1073 1074
#
# Delete all slices for an authority.
#
sub DeleteAll($$)
{
    my ($class, $authority) = @_;
1075
    require GeniAggregate;
1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118

    my $uuid = $authority->uuid();
    my $query_result =
	DBQueryWarn("select uuid from geni_slices ".
		    "where sa_uuid='$uuid'");

    return -1
	if (! $query_result);
    return 0
	if (!$query_result->numrows);

    while (my ($uuid) = $query_result->fetchrow_array()) {
	my $slice = GeniSlice->Lookup($uuid);
	if (!defined($slice)) {
	    print STDERR "Could not lookup slice $uuid\n";
	    return -1;
	}
	#
	# Do not allow active slices to be deleted.
	#
	my $aggregate = GeniAggregate->SliceAggregate($slice);
	if (defined($aggregate)) {
	    print STDERR "Cannot delete active slice $slice:\n";
	    return -1;
	}
	my @slivers;
	if (GeniSliver->SliceSlivers($slice, \@slivers) != 0) {
	    print STDERR "Cannot lookup slivers for $slice:\n";
	    return -1;
	}
	if (@slivers) {
	    print STDERR "Cannot delete active slice $slice:\n";
	    return -1;
	}
	if ($slice->Delete() != 0) {
	    print STDERR "Could not delete $slice\n";
	    return -1;
	}
    }

    return 0;
}

1119 1120 1121
#
# List all slices, optionally for an authority.
#
1122
sub ListAll($$;$)
1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151
{
    my ($class, $pref, $authority) = @_;
    my @result = ();
    @$pref = ();

    my $query = "select uuid from geni_slices ";
    if (defined($authority)) {
	my $sa_uuid = $authority->uuid();
	$query .= "where sa_uuid='$sa_uuid'";
    }
    my $query_result = DBQueryWarn($query);

    return -1
	if (! $query_result);
    return 0
	if (!$query_result->numrows);

    while (my ($uuid) = $query_result->fetchrow_array()) {
	my $slice = GeniSlice->Lookup($uuid);
	if (!defined($slice)) {
	    print STDERR "Could not lookup slice $uuid\n";
	    return -1;
	}
	push(@result, $slice);
    }
    @$pref = @result;
    return 0;
}

1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186
#
# Set/Clear the monitor pid.
#
sub SetMonitorPid($$)
{
    my ($self, $pid) = @_;
    my $uuid = $self->uuid();

    my $query_result =
	DBQueryWarn("update geni_slices set monitor_pid='$pid' " .
		    "where uuid='$uuid'");
    
    return -1
	if (!$query_result);

    $self->{'SLICE'}->{'monitor_pid'} = $pid;
    return 0;
}

sub ClearMonitorPid($)
{
    my ($self) = @_;
    my $uuid = $self->uuid();

    my $query_result =
	DBQueryWarn("update geni_slices set monitor_pid='0' " .
		    "where uuid='$uuid'");
    
    return -1
	if (!$query_result);

    $self->{'SLICE'}->{'monitor_pid'} = 0;
    return 0;
}

1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202
sub GetMonitorPid($)
{
    my ($self) = @_;
    my $uuid = $self->uuid();

    my $query_result =
	DBQueryWarn("select monitor_pid from geni_slices " .
		    "where uuid='$uuid'");
    return -1
	if (!$query_result || !$query_result->numrows);
    my ($pid) = $query_result->fetchrow_array();

    $self->{'SLICE'}->{'monitor_pid'} = $pid;
    return $pid;
}

1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226
#
# Set the public ID.
#
sub SetPublicID($)
{
    my ($self) = @_;
    require libtestbed;

    my $uuid = $self->uuid();
    my $publicid = libtestbed::TBGenSecretKey();
    return -1
	if (!defined($publicid) || $publicid eq "");
    
    my $query_result =
	DBQueryWarn("update geni_slices set publicid='$publicid' " .
		    "where uuid='$uuid'");
    
    return -1
	if (!$query_result);

    $self->{'SLICE'}->{'publicid'} = $publicid;
    return 0;
}

1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 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 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359