GeniComponent.pm.in 12.3 KB
Newer Older
Leigh Stoller's avatar
Leigh 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 GeniComponent;

#
# Some simple ticket stuff.
#
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;
use GeniResponse;
Leigh Stoller's avatar
Leigh Stoller committed
24
use GeniRegistry;
Leigh Stoller's avatar
Leigh Stoller committed
25 26 27 28 29
use GeniTicket;
use GeniCredential;
use libdb qw(TBGetUniqueIndex);
use English;
use overload ('""' => 'Stringify');
Leigh Stoller's avatar
Leigh Stoller committed
30
use XML::Simple;
Leigh Stoller's avatar
Leigh Stoller committed
31 32 33 34 35 36 37 38 39 40 41

# Configure variables
my $TB		   = "@prefix@";
my $TBOPS          = "@TBOPSEMAIL@";
my $TBAPPROVAL     = "@TBAPPROVALEMAIL@";
my $TBAUDIT   	   = "@TBAUDITEMAIL@";
my $BOSSNODE       = "@BOSSNODE@";
my $OURDOMAIN      = "@OURDOMAIN@";
my $SIGNCRED	   = "$TB/sbin/signgenicred";
my $VERIFYCRED	   = "$TB/sbin/verifygenicred";

42
# Do not allocated nodes.
Leigh Stoller's avatar
Leigh Stoller committed
43
my $impotent       = 0;
44

Leigh Stoller's avatar
Leigh Stoller committed
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
# Cache of instances to avoid regenerating them.
my %components     = ();

#
# Lookup by idx, or uuid.
#
sub Lookup($$)
{
    my ($class, $token) = @_;
    my $query_result;
    my $idx;

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

	    ($idx) = $query_result->fetchrow_array();
    }
    else {
	return undef;
    }
    # Look in cache first
    return $components{"$idx"}
        if (exists($components{"$idx"}));

    $query_result =
	DBQueryWarn("select * from geni_components where idx='$idx'");
    
    return undef
	if (!$query_result || !$query_result->numrows);

    my $self              = {};
    $self->{'COMPONENT'}  = $query_result->fetchrow_hashref();
    bless($self, $class);
Leigh Stoller's avatar
Leigh Stoller committed
85 86 87 88 89

    #
    # Grab the certificate, since we will probably want it.
    #
    my $uuid = $self->{'COMPONENT'}->{'uuid'};
90 91 92
    my $certificate = GeniCertificate->Lookup($uuid);
    if (!defined($certificate)) {
	print STDERR "Could not find certificate for component  $idx\n";
Leigh Stoller's avatar
Leigh Stoller committed
93 94
	return undef;
    }
95
    $self->{'CERT'} = $certificate;
Leigh Stoller's avatar
Leigh Stoller committed
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
    
    # Add to cache. 
    $components{$self->{'COMPONENT'}->{'idx'}} = $self;
    
    return $self;
}

#
# Stringify for output.
#
sub Stringify($)
{
    my ($self) = @_;
    
    my $uuid = $self->uuid();
    my $idx  = $self->idx();

    return "[GeniComponent: $uuid, IDX: $idx]";
}

#
# Create a Geni component in the DB.
#
119
sub Create($$$)
Leigh Stoller's avatar
Leigh Stoller committed
120
{
121
    my ($class, $certificate, $url) = @_;
Leigh Stoller's avatar
Leigh Stoller committed
122 123 124 125

    my @insert_data = ();
    my $idx = TBGetUniqueIndex('next_component', 1);

126
    my $safe_hrn  = DBQuoteSpecial($certificate->hrn());
Leigh Stoller's avatar
Leigh Stoller committed
127
    my $safe_url  = DBQuoteSpecial($url);
128
    my $safe_uuid = DBQuoteSpecial($certificate->uuid());
Leigh Stoller's avatar
Leigh Stoller committed
129 130 131 132 133 134 135 136
    
    # Now tack on other stuff we need.
    push(@insert_data, "created=now()");
    push(@insert_data, "idx='$idx'");
    push(@insert_data, "hrn=$safe_hrn");
    push(@insert_data, "url=$safe_url");
    push(@insert_data, "uuid=$safe_uuid");

137 138 139 140 141
    if ($certificate->Store() != 0) {
	print STDERR "Could not store certificate for new component.\n";
	return undef;
    }

Leigh Stoller's avatar
Leigh Stoller committed
142
    # Insert into DB.
Leigh Stoller's avatar
Leigh Stoller committed
143
    DBQueryWarn("replace into geni_components set " . join(",", @insert_data))
Leigh Stoller's avatar
Leigh Stoller committed
144 145 146 147 148 149 150 151 152 153
	or return undef;

    return GeniComponent->Lookup($idx);
}
# accessors
sub field($$) { return ((! ref($_[0])) ? -1 : $_[0]->{'COMPONENT'}->{$_[1]}); }
sub idx($)		{ return field($_[0], "idx"); }
sub uuid($)		{ return field($_[0], "uuid"); }
sub url($)		{ return field($_[0], "url"); }
sub hrn($)		{ return field($_[0], "hrn"); }
154 155
sub cert($)		{ return $_[0]->{'CERT'}->cert(); }
sub GetCertificate($)   { return $_[0]->{'CERT'}; }
Leigh Stoller's avatar
Leigh Stoller committed
156

Leigh Stoller's avatar
Leigh Stoller committed
157 158 159 160 161 162 163 164 165 166 167 168
#
# Class method to lookup the component for a given resource (uuid) by
# looking in the resources table.
#
sub LookupByResource($$)
{
    my ($class, $uuid) = @_;

    return undef
	if (! ($uuid =~ /^\w+\-\w+\-\w+\-\w+\-\w+$/));

    my $query_result =
Leigh Stoller's avatar
Leigh Stoller committed
169
	DBQueryWarn("select component_uuid from geni_resources ".
Leigh Stoller's avatar
Leigh Stoller committed
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
		    "where resource_uuid='$uuid'");
	
    return undef
	if (! $query_result || !$query_result->numrows);

    my ($idx) = $query_result->fetchrow_array();

    return GeniComponent->Lookup($idx);
}

#
# Method to insert a new geni_resources record for the component.
#
sub NewResource($$)
{
    my ($self, $uuid) = @_;

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

Leigh Stoller's avatar
Leigh Stoller committed
190
    my $component_uuid = $self->uuid();
Leigh Stoller's avatar
Leigh Stoller committed
191 192 193 194
    $uuid = DBQuoteSpecial("$uuid");

    return -1
	if (! DBQueryWarn("replace into geni_resources set ".
Leigh Stoller's avatar
Leigh Stoller committed
195
			  "  resource_uuid=$uuid, ".
Leigh Stoller's avatar
Leigh Stoller committed
196
			  "  resource_type='node', ".
Leigh Stoller's avatar
Leigh Stoller committed
197 198
			  "  created=now(), ".
			  "  component_uuid='$component_uuid'"));
Leigh Stoller's avatar
Leigh Stoller committed
199 200 201 202

    return 0;
}

203 204 205 206 207 208 209 210 211 212 213 214 215
#
# Compare two component refs.
#
sub SameComponent($$)
{
    my ($self, $other) = @_;

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

    return $self->idx() == $other->idx();
}

Leigh Stoller's avatar
Leigh Stoller committed
216 217 218 219 220 221 222 223 224 225
#
# Refresh a class instance by reloading from the DB.
#
sub Refresh($)
{
    my ($self) = @_;

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

Leigh Stoller's avatar
Leigh Stoller committed
226 227
    my $idx  = $self->idx();
    my $uuid = $self->uuid();
Leigh Stoller's avatar
Leigh Stoller committed
228 229 230 231 232 233 234 235

    my $query_result =
	DBQueryWarn("select * from geni_components where idx=$idx");

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

    $self->{'COMPONENT'}  = $query_result->fetchrow_hashref();
Leigh Stoller's avatar
Leigh Stoller committed
236 237 238 239 240 241 242 243 244 245 246 247 248

    #
    # Grab the certificate, since we will probably want it.
    #
    $query_result = DBQueryWarn("select cert from geni_certificates ".
				"where uuid='$uuid'");
    if (!$query_result || !$query_result->numrows) {
	print STDERR "Could not find certificate for component $idx\n";
	return undef;
    }
    my ($cert) = $query_result->fetchrow_array();
    $self->{'COMPONENT'}->{'cert'} = $cert;
    
Leigh Stoller's avatar
Leigh Stoller committed
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
    return 0;
}

#
# Update Fields.
#
sub Update($$)
{
    my ($self, $argref) = @_;

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

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

    my $query = "update geni_components set ".
	join(",", map("$_=" . DBQuoteSpecial($argref->{$_}), keys(%{$argref})));

    $query .= " where idx='$idx'";

    return -1
	if (! DBQueryWarn($query));

    return Refresh($self);
}

276 277 278 279 280 281 282
#
# Create component from the ClearingHouse, by looking up the info.
#
sub CreateFromRegistry($$)
{
    my ($class, $uuid) = @_;

Leigh Stoller's avatar
Leigh Stoller committed
283 284 285 286
    my $clearinghouse = GeniRegistry::ClearingHouse->Create();
    return undef
	if (!defined($clearinghouse));

287 288
    my $blob;
    return undef
Leigh Stoller's avatar
Leigh Stoller committed
289
	if ($clearinghouse->Resolve($uuid, "CM", \$blob) != 0);
290

291 292 293 294 295 296 297 298 299
    my $certificate = GeniCertificate->LoadFromString($blob->{'gid'});
    return undef
	if (!defined($certificate));

    my $component = GeniComponent->Create($certificate, $blob->{'url'});
    $certificate->Delete()
	if (!defined($component));

    return $component;
300 301
}

Leigh Stoller's avatar
Leigh Stoller committed
302 303 304 305 306 307 308 309 310 311 312 313 314 315
#
# Create component from a certificate.
#
sub CreateFromCertificate($$$)
{
    my ($class, $certificate, $url) = @_;

    my $component = GeniComponent->Create($certificate, $url);
    $certificate->Delete()
	if (!defined($component));

    return $component;
}

Leigh Stoller's avatar
Leigh Stoller committed
316 317 318 319 320 321
#
# Discover Resources on the component. Returns a resource availability
# spec as an XML string (not a structure).
#
sub DiscoverResources($$$$$)
{
Leigh Stoller's avatar
Leigh Stoller committed
322
    my ($self, $slice, $user, $credential, $pref) = @_;
Leigh Stoller's avatar
Leigh Stoller committed
323 324 325

    # Must be a real reference. 
    return -1
Leigh Stoller's avatar
Leigh Stoller committed
326
	if (! (ref($self) && ref($slice) && ref($user) && ref($credential)));
Leigh Stoller's avatar
Leigh Stoller committed
327 328 329 330

    my $response =
	Genixmlrpc::CallMethodHTTP($self->url(),
				   $user,
331 332 333
				   "DiscoverResources",
				   { "credential"  => $credential->asString()
				   });
Leigh Stoller's avatar
Leigh Stoller committed
334
    
Leigh Stoller's avatar
Leigh Stoller committed
335
    return -1
Leigh Stoller's avatar
Leigh Stoller committed
336 337
	if (!defined($response) || $response->code() != GENIRESPONSE_SUCCESS);

338
    my $rspec = XMLin($response->value(), ForceArray => ["node", "interface"]);
Leigh Stoller's avatar
Leigh Stoller committed
339 340
    $$pref = $rspec;
    
Leigh Stoller's avatar
Leigh Stoller committed
341 342 343 344 345 346
    return 0;
}

#
# Get a Ticket from a component;
#
347
sub GetTicket($$$$$;$)
Leigh Stoller's avatar
Leigh Stoller committed
348
{
Leigh Stoller's avatar
Leigh Stoller committed
349
    my ($self, $slice, $rspec_xml, $context, $credential, $vtopo) = @_;
Leigh Stoller's avatar
Leigh Stoller committed
350 351

    # Must be a real reference. 
Leigh Stoller's avatar
Leigh Stoller committed
352
    return undef
Leigh Stoller's avatar
Leigh Stoller committed
353 354
	if (! ref($self));

355
    my $args = { "credential"   => $credential->asString(),
356 357 358 359
		 "impotent"     => $impotent,
		 "rspec"        => $rspec_xml };
    $args->{'virtual_topology'} = $vtopo
	if (defined($vtopo));
Leigh Stoller's avatar
Leigh Stoller committed
360

Leigh Stoller's avatar
Leigh Stoller committed
361
    my $response = 				       
Leigh Stoller's avatar
Leigh Stoller committed
362
	Genixmlrpc::CallMethod($self->url(), $context, "GetTicket", $args);
Leigh Stoller's avatar
Leigh Stoller committed
363 364 365 366 367 368 369 370 371 372
    
    return undef
	if (!defined($response));

    return undef
	if ($response->code() != GENIRESPONSE_SUCCESS);

    #
    # Convert this into a (signed) ticket object.
    #
Leigh Stoller's avatar
Leigh Stoller committed
373
    return GeniTicket->CreateFromSignedTicket($response->value(), $self, 1);
Leigh Stoller's avatar
Leigh Stoller committed
374 375
}

Leigh Stoller's avatar
Leigh Stoller committed
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
#
# Release a Ticket on a component;
#
sub ReleaseTicket($$$$)
{
    my ($self, $slice, $context, $ticket) = @_;

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

    my $args = { "ticket"   => $ticket->asString() };

    my $response = 				       
	Genixmlrpc::CallMethod($self->url(), $context, "ReleaseTicket", $args);
    
    return -1
	if (!defined($response));

    return -1
	if ($response->code() != GENIRESPONSE_SUCCESS);

    return 0;
}

Leigh Stoller's avatar
Leigh Stoller committed
401 402 403
#
# Create a sliver, given a ticket.
#
Leigh Stoller's avatar
Leigh Stoller committed
404
sub CreateSliver($$$$;$)
Leigh Stoller's avatar
Leigh Stoller committed
405
{
Leigh Stoller's avatar
Leigh Stoller committed
406
    my ($self, $slice, $ticket, $context, $extraargs) = @_;
Leigh Stoller's avatar
Leigh Stoller committed
407 408

    # Must be a real reference. 
Leigh Stoller's avatar
Leigh Stoller committed
409
    return undef
Leigh Stoller's avatar
Leigh Stoller committed
410 411
	if (! ref($self));

Leigh Stoller's avatar
Leigh Stoller committed
412 413 414 415 416
    my $args = { "ticket"    => $ticket->asString(),
		 "impotent"  => $impotent };
    $args->{'extraargs'} = $extraargs
	if (defined($extraargs));

Leigh Stoller's avatar
Leigh Stoller committed
417
    my $response =
Leigh Stoller's avatar
Leigh Stoller committed
418
      Genixmlrpc::CallMethod($self->url(), $context, "RedeemTicket", $args);
Leigh Stoller's avatar
Leigh Stoller committed
419
    
Leigh Stoller's avatar
Leigh Stoller committed
420
    return undef
Leigh Stoller's avatar
Leigh Stoller committed
421 422
	if (!defined($response));

Leigh Stoller's avatar
Leigh Stoller committed
423
    return undef
Leigh Stoller's avatar
Leigh Stoller committed
424 425 426 427 428
	if ($response->code() != GENIRESPONSE_SUCCESS);

    #
    # We get back a signed credential, which has the sliver uuid inside.
    #
Leigh Stoller's avatar
Leigh Stoller committed
429
    my $credential = GeniCredential->CreateFromSigned($response->value(), 1);
Leigh Stoller's avatar
Leigh Stoller committed
430 431
    if (!defined($credential)) {
	print STDERR "Could not create local credential object.\n";
Leigh Stoller's avatar
Leigh Stoller committed
432
	return undef;
Leigh Stoller's avatar
Leigh Stoller committed
433 434
    }

Leigh Stoller's avatar
Leigh Stoller committed
435 436 437 438
    my $sliver = GeniSliver::Client->Create($slice, $ticket->owner_uuid(),
					    $ticket->rspec(),
					    $credential, $self);

Leigh Stoller's avatar
Leigh Stoller committed
439 440
    if (!defined($sliver)) {
	print STDERR "Could not create local sliver object.\n";
Leigh Stoller's avatar
Leigh Stoller committed
441
	return undef;
Leigh Stoller's avatar
Leigh Stoller committed
442
    }
Leigh Stoller's avatar
Leigh Stoller committed
443
    return $sliver;
Leigh Stoller's avatar
Leigh Stoller committed
444 445 446 447 448 449 450
}

#
# Destroy a sliver. 
#
sub DestroySliver($$$)
{
Leigh Stoller's avatar
Leigh Stoller committed
451
    my ($self, $sliver, $context) = @_;
Leigh Stoller's avatar
Leigh Stoller committed
452 453 454 455 456

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

Leigh Stoller's avatar
Leigh Stoller committed
457
    my $credential = $sliver->GetCredential($context->user());
Leigh Stoller's avatar
Leigh Stoller committed
458 459 460 461
    return -1
	if (!defined($credential));
    
    my $response =
Leigh Stoller's avatar
Leigh Stoller committed
462 463 464 465
	Genixmlrpc::CallMethod($self->url(), $context,
			       "DeleteSliver",
			       { "impotent"    => $impotent,
				 "credential"  => $credential->asString() });
Leigh Stoller's avatar
Leigh Stoller committed
466 467 468 469 470 471 472 473
    
    if ($response->code() != GENIRESPONSE_SUCCESS) {
	print STDERR "Could not destroy sliver $sliver\n";
	return -1;
    }
    return 0;
}

Leigh Stoller's avatar
Leigh Stoller committed
474 475 476 477 478
#
# Start a sliver. 
#
sub StartSliver($$$)
{
Leigh Stoller's avatar
Leigh Stoller committed
479
    my ($self, $sliver, $context) = @_;
Leigh Stoller's avatar
Leigh Stoller committed
480 481 482 483 484

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

Leigh Stoller's avatar
Leigh Stoller committed
485
    my $credential = $sliver->GetCredential($context->user());
Leigh Stoller's avatar
Leigh Stoller committed
486 487 488 489
    return -1
	if (!defined($credential));
    
    my $response =
Leigh Stoller's avatar
Leigh Stoller committed
490 491 492 493
	Genixmlrpc::CallMethod($self->url(), $context,
			       "StartSliver",
			       { "impotent"    => $impotent,
				 "credential"  => $credential->asString() });
Leigh Stoller's avatar
Leigh Stoller committed
494 495 496 497 498 499 500 501
    
    if ($response->code() != GENIRESPONSE_SUCCESS) {
	print STDERR "Could not start sliver $sliver\n";
	return -1;
    }
    return 0;
}

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
#
# Split a sliver. 
#
sub SplitSliver($$$$)
{
    my ($self, $sliver, $context, $pref) = @_;

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

    my $credential = $sliver->GetCredential($context->user());
    return -1
	if (!defined($credential));

    my $slice = $sliver->GetSlice();
    return -1
	if (!defined($slice));
    
    my $response =
	Genixmlrpc::CallMethod($self->url(), $context,
			       "SplitSliver",
			       { "credential"  => $credential->asString() });
    
    if ($response->code() != GENIRESPONSE_SUCCESS) {
	print STDERR "Could not split sliver $sliver\n";
	return -1;
    }

    #
    # We get back signed credentials, which has the sliver uuid inside.
    #
    my @slivers = ();
    
    foreach my $credential (@{ $response->value() }) {
	my $credential = GeniCredential->CreateFromSigned($credential, 1);
	if (!defined($credential)) {
	    print STDERR "Could not create local credential object.\n";
	    return -1;
	}

	my $s = GeniSliver::Client->Create($slice,
					   $credential->owner_uuid(),
					   undef,
					   $credential, $self);

	if (!defined($s)) {
	    print STDERR "Could not create local sliver object.\n";
	    return -1;
	}
	$s->SetAggregate($sliver);
	push(@slivers, $s);
    }
    @$pref = @slivers;
    return 0;
}

Leigh Stoller's avatar
Leigh Stoller committed
559 560 561
# _Always_ make sure that this 1 is at the end of the file...
1;