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

#
use strict;
use Exporter;
use vars qw(@ISA @EXPORT);

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

use GeniDB;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
18
use GeniComponent;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
19
use GeniSlice;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
20
use GeniCredential;
21
use GeniCertificate;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
22
use GeniAggregate;
23
use GeniUsage;
24
use GeniHRN;
25
use GeniXML;
26
use emutil;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
27
use Experiment;
28
use OSinfo;
29
use Node;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
30
use English;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
31
use XML::Simple;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
32
33
use Data::Dumper;
use File::Temp qw(tempfile);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
34
use overload ('""' => 'Stringify');
Leigh B. Stoller's avatar
Leigh B. Stoller committed
35
36
37
38
39
40
41
42

# Configure variables
my $TB		   = "@prefix@";
my $TBOPS          = "@TBOPSEMAIL@";
my $TBAPPROVAL     = "@TBAPPROVALEMAIL@";
my $TBAUDIT   	   = "@TBAUDITEMAIL@";
my $BOSSNODE       = "@BOSSNODE@";
my $OURDOMAIN      = "@OURDOMAIN@";
43
my $PGENIDOMAIN    = "@PROTOGENI_DOMAIN@";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
44
my $SIGNCRED	   = "$TB/sbin/signgenicred";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
45
46
47
my $AVAIL	   = "$TB/sbin/avail";
my $NALLOC	   = "$TB/bin/nalloc";
my $NFREE	   = "$TB/bin/nfree";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
48
my $NODEREBOOT	   = "$TB/bin/node_reboot";
49
my $NAMEDSETUP     = "$TB/sbin/named_setup";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
50
51
my $PLABNODE       = "$TB/sbin/plabnodewrapper";
my $VNODESETUP     = "$TB/sbin/vnode_setup";
52
my $GENTOPOFILE    = "$TB/libexec/gentopofile";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
53
my $POWER          = "$TB/bin/power";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
54
55
56

# Cache of instances to avoid regenerating them.
my %slivers      = ();
57
BEGIN { use GeniUtil; GeniUtil::AddCache(\%slivers); }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
58
59

#
60
# Lookup by URN, idx, or uuid.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
61
62
63
64
65
#
sub Lookup($$)
{
    my ($class, $token) = @_;
    my $query_result;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
66
    my $idx;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
67

68
69
70
    if (GeniHRN::IsValid($token)) {
	my ($authority, $type, $id) = GeniHRN::Parse($token);
	return undef if $type ne "sliver";
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88

	if( GeniHRN::Authoritative($token, "@OURDOMAIN@") ) {
	    # Very simple: we put the index of our own slivers right
	    # in the name.
	    $idx = $id;
	} else {
	    # Look up the slivers's certificate.
	    $token = GeniHRN::Normalise( $token );
	    $query_result = DBQueryWarn(
		"SELECT geni_slivers.idx FROM geni_slivers, " .
		"geni_certificates WHERE geni_slivers.uuid = " .
		"geni_certificates.uuid AND " .
		"geni_certificates.urn='$token';" );

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

	    ($idx) = $query_result->fetchrow_array();
	}
89
90
    }
    elsif ($token =~ /^\d+$/) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
91
	$idx = $token;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
92
93
94
    }
    elsif ($token =~ /^\w+\-\w+\-\w+\-\w+\-\w+$/) {
	$query_result =
Leigh B. Stoller's avatar
Leigh B. Stoller committed
95
	    DBQueryWarn("select idx from geni_slivers ".
Leigh B. Stoller's avatar
Leigh B. Stoller committed
96
			"where uuid='$token'");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
97
98
99
100
	    return undef
		if (! $query_result || !$query_result->numrows);

	    ($idx) = $query_result->fetchrow_array();
Leigh B. Stoller's avatar
Leigh B. Stoller committed
101
102
103
104
    }
    else {
	return undef;
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
105
106
107
108
109
110
111
112

    # Look in cache first
    return $slivers{"$idx"}
        if (exists($slivers{"$idx"}));

    $query_result = DBQueryWarn("select * from geni_slivers ".
				"where idx='$idx'");

Leigh B. Stoller's avatar
Leigh B. Stoller committed
113
114
115
    return undef
	if (!$query_result || !$query_result->numrows);

Leigh B. Stoller's avatar
Leigh B. Stoller committed
116
117
    my $self              = {};
    $self->{'SLIVER'}     = $query_result->fetchrow_hashref();
118
    $self->{'SLICE'}      = undef;	# server
Leigh B. Stoller's avatar
Leigh B. Stoller committed
119
    $self->{'AGGREGATE'}  = undef;	# server
120
    $self->{'RSPEC'}      = undef;	# server
Leigh B. Stoller's avatar
Leigh B. Stoller committed
121
122
123

    my $rspec_string = $self->{'SLIVER'}->{'rspec_string'};
    if (defined($rspec_string) && $rspec_string ne "") {
124
125
	my $rspec = GeniXML::Parse($rspec_string);
	if (!defined($rspec)) {
126
127
128
	    return undef;
	}
	$self->{'RSPEC'} = $rspec;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
129
    }
130
131
132
133
134
135
136
137
138
139
140

    #
    # Grab the certificate, since we will probably want it.
    #
    my $uuid = $self->{'SLIVER'}->{'uuid'};
    my $certificate = GeniCertificate->Lookup($uuid);
    if (!defined($certificate)) {
	print STDERR "Could not find certificate for sliver $idx ($uuid)\n";
	return undef;
    }
    $self->{'CERTIFICATE'} = $certificate;
141
142
143
144

    # Bless into sub package if called for.
    my $resource_type = $self->{'SLIVER'}->{'resource_type'};
    if (defined($resource_type) && $resource_type ne "") {
145
	bless($self, $class . "::" . $resource_type);
146
147
148
149
    }
    else {
	bless($self, $class);
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
    
    # Add to cache. 
    $slivers{$self->{'SLIVER'}->{'idx'}} = $self;
    
    return $self;
}

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

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

#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
171
# Create a sliver record in the DB. On the client side we save the credential
Leigh B. Stoller's avatar
Leigh B. Stoller committed
172
# that allows control of it, for later operations.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
173
#
174
sub Create($$$$$$$$$)
Leigh B. Stoller's avatar
Leigh B. Stoller committed
175
{
176
177
    my ($class, $slice, $owner, $uuid,
	$resource_uuid, $resource_type, $resource_id,
178
	$hrn, $nickname, $rspec) = @_;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
179
180
181
182
    my @insert_data = ();

    # Every sliver gets a new unique index.
    my $idx = TBGetUniqueIndex('next_sliver', 1);
183
    my $urn = GeniHRN::Generate("@OURDOMAIN@", "sliver", $idx);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
184

185
186
187
188
189
190
    # Sanity check.
    my $certificate = GeniCertificate->Lookup($urn);
    if (defined($certificate)) {
	print STDERR "GeniSliver::Create: ".
	    "Already have a certificate for $hrn/$urn\n";
	return undef;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
191
    }
192
    $certificate = GeniCertificate->Create("sliver", $urn, $hrn, $TBOPS,$uuid);
193
194
    if (!defined($certificate)) {
	print STDERR "GeniSliver::Create: ".
195
	    "Could not generate new certificate for $hrn/$urn\n";
196
	return undef;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
197
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
198
    my $slice_uuid     = $slice->uuid();
199
    my $owner_uuid     = $owner->uuid();
Leigh B. Stoller's avatar
Leigh B. Stoller committed
200
201
202
203

    # Now tack on other stuff we need.
    push(@insert_data, "created=now()");
    push(@insert_data, "idx='$idx'");
204
205
206
    push(@insert_data, "hrn=" . DBQuoteSpecial($hrn));
    push(@insert_data, "nickname=" . DBQuoteSpecial($nickname))
	if (defined($nickname));
Leigh B. Stoller's avatar
Leigh B. Stoller committed
207
    push(@insert_data, "uuid='$uuid'");
208
209
    push(@insert_data, "resource_uuid='$resource_uuid'");
    push(@insert_data, "resource_type='$resource_type'");
210
    push(@insert_data, "resource_id='$resource_id'");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
211
212
213
    push(@insert_data, "creator_uuid='$owner_uuid'");
    push(@insert_data, "slice_uuid='$slice_uuid'");

Leigh B. Stoller's avatar
Leigh B. Stoller committed
214
    if (defined($rspec)) {
215
	my $rspec_string = GeniXML::Serialize($rspec);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
216
217
218
219
220
	my $safe_rspec   = DBQuoteSpecial($rspec_string);

	push(@insert_data, "rspec_string=$safe_rspec");
    }

Leigh B. Stoller's avatar
Leigh B. Stoller committed
221
    # Insert into DB.
222
223
224
    if (!DBQueryWarn("insert into geni_slivers set " .
		     join(",", @insert_data))) {
	$certificate->Delete();
Leigh B. Stoller's avatar
Leigh B. Stoller committed
225
226
227
	return undef;
    }

Leigh B. Stoller's avatar
Leigh B. Stoller committed
228
229
230
    my $sliver = GeniSliver->Lookup($idx);
    return undef
	if (!defined($sliver));
231
232
233
234
235

    if (GeniUsage->NewSliver($sliver, $slice, $owner)) {
	print STDERR
	    "GeniSliver::Create: GeniUsage->NewSliver($sliver) failed\n";
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
236
    $sliver->{'AGGREGATE'} = undef;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
237
    $sliver->{'SLICE'}     = undef;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
238
239

    return $sliver;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
240
241
242
243
244
}
# accessors
sub field($$) { return ((! ref($_[0])) ? -1 : $_[0]->{'SLIVER'}->{$_[1]}); }
sub idx($)		{ return field($_[0], "idx"); }
sub uuid($)		{ return field($_[0], "uuid"); }
245
246
sub hrn($)		{ return field($_[0], "hrn"); }
sub nickname($)		{ return field($_[0], "nickname"); }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
247
248
249
sub slice_uuid($)	{ return field($_[0], "slice_uuid"); }
sub creator_uuid($)	{ return field($_[0], "creator_uuid"); }
sub created($)		{ return field($_[0], "created"); }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
250
sub registered($)	{ return field($_[0], "registered"); }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
251
sub credential_idx($)	{ return field($_[0], "credential_idx"); }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
252
sub resource_uuid($)	{ return field($_[0], "resource_uuid"); }
253
sub resource_id($)	{ return field($_[0], "resource_id"); }
254
sub resource_type($)	{ return field($_[0], "resource_type"); }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
255
256
257
sub component_uuid($)	{ return field($_[0], "component_uuid"); }
sub aggregate_uuid($)	{ return field($_[0], "aggregate_uuid"); }
sub rspec_string($)     { return field($_[0], "rspec_string"); }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
258
sub status($)		{ return field($_[0], "status"); }
259
sub state($)		{ return field($_[0], "state"); }
260
261
sub cert($)		{ return $_[0]->{'CERTIFICATE'}->cert(); }
sub GetCertificate($)   { return $_[0]->{'CERTIFICATE'}; }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
262
sub rspec($)            { return $_[0]->{'RSPEC'}; }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
263

264
265
266
267
268
269
270
271
# Return the sliver URN. 
sub sliver_urn($)
{
    my ($self)  = @_;

    return GeniHRN::Generate("@OURDOMAIN@", "sliver", $self->idx());
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
272
273
274
#
# Delete the sliver. The sliver should not be provisioned when this done.
#
275
sub Delete($$)
Leigh B. Stoller's avatar
Leigh B. Stoller committed
276
{
277
    my ($self, $purge) = @_;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
278
279
280
281

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

282
283
    my $idx  = $self->idx();
    my $uuid = $self->uuid();
Leigh B. Stoller's avatar
Leigh B. Stoller committed
284

285
286
287
288
    if (GeniUsage->DestroySliver($self, $purge)) {
	print STDERR
	    "GeniSliver::Delete: GeniUsage->DestroySliver($self) failed\n";
    }
289
290
    DBQueryWarn("delete from geni_credentials where this_uuid='$uuid'")
	or return -1;
291
292
293
294
    if ($self->resource_type() ne "Node") {
	DBQueryWarn("delete from geni_certificates where uuid='$uuid'")
	    or return -1;
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
295
296
    DBQueryWarn("delete from geni_slivers where idx='$idx'")
	or return -1;
297

298
299
300
    # Delete from cache. 
    delete($slivers{$idx});
    
Leigh B. Stoller's avatar
Leigh B. Stoller committed
301
302
303
    return 0;
}

304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#
# Get the manifest for an aggregate. Returns the XML string.
#
sub GetManifest($$)
{
    my ($self, $asxml) = @_;

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

    my $manifest = $self->rspec();

    return $manifest
	if (!$asxml);
    
319
    my $xml = GeniXML::Serialize($self->rspec());
320
321
322
    return $xml;
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
323
324
325
326
327
328
329
330
331
332
#
# Set the aggregate for a sliver.
#
sub SetAggregate($$)
{
    my ($self, $aggregate) = @_;

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

Leigh B. Stoller's avatar
Leigh B. Stoller committed
333
334
    my $idx      = $self->idx();
    my $agg_uuid = $aggregate->uuid();
Leigh B. Stoller's avatar
Leigh B. Stoller committed
335
336
337

    return -1
	if (!DBQueryWarn("update geni_slivers set ".
Leigh B. Stoller's avatar
Leigh B. Stoller committed
338
			 "  aggregate_uuid='$agg_uuid' ".
Leigh B. Stoller's avatar
Leigh B. Stoller committed
339
340
			 "where idx='$idx'"));
    
341
342
343
344
345
346
347
    if (!DBQueryWarn("update sliver_history set ".
		     "  aggregate_uuid='$agg_uuid' ".
		     "where idx='$idx'")) {
	print STDERR "GeniSliver::SetAggregate: ".
	    "Failed to update sliver_history for $self\n";
    }

Leigh B. Stoller's avatar
Leigh B. Stoller committed
348
    $self->{'SLIVER'}->{'aggregate_uuid'} = $agg_uuid;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
349
350
351
352
    $self->{'AGGREGATE'} = $aggregate;
    return 0;
}

353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
#
# And clear the aggregate.
#
sub ClearAggregate($$)
{
    my ($self) = @_;

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

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

    return -1
	if (!DBQueryWarn("update geni_slivers set ".
			 "  aggregate_uuid=NULL ".
			 "where idx='$idx'"));
    
    $self->{'SLIVER'}->{'aggregate_uuid'} = undef;
    $self->{'AGGREGATE'} = undef;
    return 0;
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
375
376
377
378
379
380
381
382
383
384
385
386
#
# Get the aggregate for a sliver.
#
sub GetAggregate($)
{
    my ($self) = @_;

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

    return $self->{'AGGREGATE'} if (defined($self->{'AGGREGATE'}));
    return undef
Leigh B. Stoller's avatar
Leigh B. Stoller committed
387
	if (!defined($self->aggregate_uuid()));
Leigh B. Stoller's avatar
Leigh B. Stoller committed
388

Leigh B. Stoller's avatar
Leigh B. Stoller committed
389
    my $aggregate = GeniAggregate->Lookup($self->aggregate_uuid());
Leigh B. Stoller's avatar
Leigh B. Stoller committed
390
391
392
393
394
395
396
397
    if (!defined($aggregate)) {
	print STDERR "Could not get aggregate object associated with $self\n";
	return undef;
    }
    $self->{'AGGREGATE'} = $aggregate;
    return $aggregate;
}

398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
#
# Set the status for the sliver.
#
sub SetStatus($$)
{
    my ($self, $status) = @_;

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

    my $idx = $self->idx();
    
    return -1
	if (!DBQueryWarn("update geni_slivers set ".
			 "  status='$status' ".
			 "where idx='$idx'"));
    
    $self->{'SLIVER'}->{'status'} = $status;
    return 0;
}

419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
#
# Set the state for the sliver.
#
sub SetState($$)
{
    my ($self, $state) = @_;

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

    my $idx = $self->idx();
    
    return -1
	if (!DBQueryWarn("update geni_slivers set ".
			 "  state='$state' ".
			 "where idx='$idx'"));
    
    $self->{'SLIVER'}->{'state'} = $state;
    return 0;
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
440
441
442
443
444
445
446
447
448
449
450
451
452
#
# Get the experiment for the slice this sliver belongs to.
#
sub GetExperiment($)
{
    my ($self) = @_;

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

    return Experiment->Lookup($self->slice_uuid());
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
#
# Get the slice for the sliver.
#
sub GetSlice($)
{
    my ($self) = @_;

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

    return $self->{'SLICE'} if (defined($self->{'SLICE'}));

    if (!defined($self->slice_uuid())) {
	print STDERR "No slice associated with $self\n";
	return undef;
    }
    my $slice = GeniSlice->Lookup($self->slice_uuid());
    if (!defined($slice)) {
	print STDERR "Could not get slice object associated with $self\n";
	return undef;
    }
    $self->{'SLICE'} = $slice;
    return $slice;
}

478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
#
# The expiration time for a sliver is when the slice expires.
# The DB field is ignored.
#
sub expires($)
{
    my ($self) = @_;

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

    my $slice = $self->GetSlice();
    return undef
	if (!defined($slice));

    return $slice->expires();
}

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
#
# Look up a list of slivers for a locally instantiated slice. 
# Used by the CM.
#
sub SliceSlivers($$$)
{
    my ($class, $slice, $pref) = @_;

    my $slice_uuid = $slice->uuid();
    my @result = ();

    my $query_result =
	DBQueryWarn("select idx from geni_slivers ".
		    "where slice_uuid='$slice_uuid'");
    return -1
	if (!$query_result);

    while (my ($idx) = $query_result->fetchrow_array()) {
	my $sliver = GeniSliver->Lookup($idx);
	return -1
	    if (!defined($sliver));
	push(@result, $sliver);
    }
    @$pref = @result;
    return 0;
}

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
#
# Find slivers "dependent" on this sliver, as for interfaces on nodes.
#
sub DependentSlivers($$)
{
    my ($self, $pref) = @_;

    return -1
	if (! (ref($self) && ref($pref)));
    @$pref = ();

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

    my $query_result =
	DBQueryWarn("select idx from geni_slivers ".
		    "where idx!='$idx' and resource_uuid='$resource_uuid'");
    return -1
	if (!$query_result);

    my @result = ();
    while (my ($idx) = $query_result->fetchrow_array()) {
	my $sliver = GeniSliver->Lookup($idx);
	if (!defined($sliver)) {
	    print STDERR "Could not get sliver object for $idx\n";
	    return -1;
	}
	push(@result, $sliver);
    }
    @$pref = @result;
    return 0;
}

556
557
558
559
560
561
562
563
564
565
566
567
568
############################################################################
#
# The server side methods are in packages which inherit from above.
#
package GeniSliver::Node;
use vars qw(@ISA);
@ISA = "GeniSliver";

use GeniDB;
use GeniComponent;
use GeniSlice;
use GeniCredential;
use GeniCertificate;
569
use GeniUtil;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
570
use Experiment;
571
use emutil;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
572
use XML::Simple;
573
use libdb qw(TBDB_ALLOCSTATE_RES_INIT_DIRTY TBDB_NODESTATE_SHUTDOWN
Leigh B. Stoller's avatar
Leigh B. Stoller committed
574
575
	     TBResolveNextOSID TBDB_NODESTATE_ISUP TBDB_NODESTATE_TBFAILED);

576
# Error log for local physical node.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
sub ErrorLog($)
{
    my ($self)  = @_;
    my $bootlog = "";

    my $node = Node->Lookup($self->uuid());
    if (!defined($node)) {
	print STDERR "Could not map node $self to its object\n";
	return "";
    }
    if ($node->GetBootLog(\$bootlog)) {
	print STDERR "Could not get bootlog for $self ($node)\n";
	return "";
    }
    return $bootlog;
}
593

594
595
596
597
598
599
600
601
# Return the component URN. This is how a resource is resolved.
sub component_urn($)
{
    my ($self)  = @_;

    return GeniHRN::Generate("@OURDOMAIN@", "node", $self->resource_id())
}

602
sub Create($$$$$$)
603
{
604
    # $rspec is a LibXML element representing a single node.
605
    my ($class, $slice, $user, $resource_uuid, $rspec) = @_;
606
607
    my $virtualization_type = GeniXML::GetText("virtualization_type", $rspec);
    if (!defined($virtualization_type)) {
608
609
610
	print STDERR "Node does not contain a virtualization_type\n";
	return undef;
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
611
612
613
614

    my $experiment = $slice->GetExperiment();
    if (!defined($experiment)) {
	print STDERR "Could not map $slice to its experiment\n";
615
	return undef;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
616
    }
617

Leigh B. Stoller's avatar
Leigh B. Stoller committed
618
619
620
621
622
623
624
625
    #
    # the node is already allocated to the sliver, but still need to enter
    # a virt_nodes entry, and possibly more virt table entries, so that the
    # node will boot properly, and is otherwise controllable.
    #
    my $node = Node->Lookup($resource_uuid);
    if (!defined($node)) {
	print STDERR "Could not map node $resource_uuid to its object\n";
626
	return undef;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
627
    }
628
629
630

    #
    # An artifact of Emulab is that for shared/remote nodes, the physical
631
    # node is already allocated, but not to the current experiment.
632
633
634
    #
    if (! ($node->sharing_mode() ||
	   ($node->isremotenode() && $node->isvirtnode()))) {
635
636
637
638
639
640
	my $reservation = $node->Reservation();
	if (!defined($reservation)) {
	    print STDERR "$node was already released from $slice\n";
	    return undef;
	}
	if (! $reservation->SameExperiment($experiment)) {
641
	    print STDERR "$node is reserved to another, not $experiment\n";
642
643
644
	    # Signal error so we can look at what happened.
	    return undef;
	}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
645
    }
646
    my $nickname = GeniXML::GetVirtualId($rspec);
647
    if (!defined($nickname)) {
648
649
650
	print STDERR "Node does not contain a virtual_id\n";
	return undef;
    }
651
652
653
654
655
656
    my $hrn;
    my $sshdport;
    my $hostname;
    my $resource_id;
    my $sliver_uuid;

Leigh B. Stoller's avatar
Leigh B. Stoller committed
657
658
659
660
661
    #
    # The resource UUID refers to the physical node, but the virtualization
    # type might require a vnode.
    # 
    if ($virtualization_type eq "emulab-vnode") {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
662
663
664
	my $vnode = $experiment->VnameToNode($nickname);
	if (!defined($vnode)) {
	    print STDERR "Could not lookup node $nickname in $experiment\n";
665
	    return undef;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
666
	}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
667
668
	$hrn = "${PGENIDOMAIN}." . $vnode->node_id();
	$sliver_uuid = $vnode->uuid();
669
	$resource_id = $vnode->node_id();
670
	$hostname = GeniUtil::FindHostname($vnode->node_id());
671
	$sshdport = $vnode->sshdport();
Leigh B. Stoller's avatar
Leigh B. Stoller committed
672
    }
673
674
675
676
    else {
	$hrn = "${PGENIDOMAIN}." . $node->node_id();
	$sliver_uuid = $node->uuid();
	$resource_id = $node->node_id();
677
	$hostname = GeniUtil::FindHostname($node->node_id());
678
679
680
681
682
    }

    #
    # Add this stuff to the rspec (which becomes the manifest).
    #
683
684
685
    GeniXML::SetText("sliver_uuid", $rspec, $sliver_uuid);
    GeniXML::SetText("hostname", $rspec, $hostname);
    GeniXML::SetText("sshdport", $rspec, $sshdport) if (defined($sshdport));
686

687
    return GeniSliver->Create($slice, $user, $sliver_uuid, $resource_uuid,
688
			      "Node", $resource_id, $hrn, $nickname, $rspec);
689
690
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
691
692
#
# Provision a slice. We actually did this when the ticket was requested.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
693
# We fill in some virt table stuff so that tbswap will work.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
694
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
695
sub Provision($;$)
Leigh B. Stoller's avatar
Leigh B. Stoller committed
696
{
Leigh B. Stoller's avatar
Leigh B. Stoller committed
697
    my ($self, $extraargs) = @_;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
698
699
700
701
702
703
704

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

    #
    # the node is already allocated to the sliver, but still need to enter
    # a virt_nodes entry, and possibly more virt table entries, so that the
Leigh B. Stoller's avatar
Leigh B. Stoller committed
705
    # node will boot properly, and is otherwise controllable.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
706
707
708
709
710
711
    #
    my $experiment = Experiment->Lookup($self->slice_uuid());
    if (!defined($experiment)) {
	print STDERR "Could not map $self to its experiment\n";
	return -1;
    }
712
    my $uuid = $self->uuid();
Leigh B. Stoller's avatar
Leigh B. Stoller committed
713
    return 0
714
715
	if (!defined($uuid));
    my $node       = Node->Lookup($uuid);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
716
    if (!defined($node)) {
717
	print STDERR "Could not map node $uuid to its object\n";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
718
719
	return -1;
    }
720
    my $node_id     = $node->node_id();
Leigh B. Stoller's avatar
Leigh B. Stoller committed
721
722
    my $reservation = $node->Reservation();
    if (!defined($reservation)) {
723
	print STDERR "$node was already released from slice\n";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
724
725
	return -1;
    }
726
    if (! $reservation->SameExperiment($experiment)) {
727
	print STDERR "$node is reserved to another, not $experiment\n";
728
729
730
731
732
	# Signal error so we can look at what happened.
	return -1;
    }
    my $pid = $experiment->pid();
    my $eid = $experiment->eid();
733

734
    my $virt_type = GeniXML::GetText("virtualization_type", $self->rspec());
735
    if (!$node->isremotenode() &&
736
	defined($virt_type) &&
737
	$virt_type eq "emulab-vnode") {
738
739
740
741
	my $pnode = Node->Lookup($node->phys_nodeid());
	if (!defined($pnode)) {
	    print STDERR "Could not get pnode object for $node\n";
	    return -1;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
742
	}
743

744
745
	# Mark
	$node->ModifyReservation({"genisliver_idx" => $self->idx()});
746

747
748
749
	my $subtype = GeniXML::GetText("virtualization_subtype",
				       $self->rspec());
	if (defined($subtype)) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
750
751
752
	    if (!$pnode->sharing_mode()) {
		$pnode->ModifyReservation({"genisliver_idx" => $self->idx()});
	    }
753
	}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
754
755
    }
    else {
756
757
758
759
760
761
762
	#
	# For a "raw" node, there is no vnode, so this is the pnode
	# we need to mark for tmcd.
	#
	if ($node->ModifyReservation({"genisliver_idx" => $self->idx()})) {
	    return -1;
	}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
763
764
765
766
767
768
769
770

	#
	# Terrible place to do this. 
	#
	if ($node->OSSelect("<DEFAULT>", "def_boot_osid", 0) != 0) {
	    print STDERR "Could not os_select $node to default\n";
	    return -1;
	}
771
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
772
773
774
775
776
777
    return 0;
}

#
# Unprovision a sliver. 
#
778
sub UnProvision($;$)
Leigh B. Stoller's avatar
Leigh B. Stoller committed
779
{
780
    my ($self, $nophysfree) = @_;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
781
782
783

    return -1
	if (! ref($self));
784
785
    $nophysfree = 0
	if (!defined($nophysfree));
Leigh B. Stoller's avatar
Leigh B. Stoller committed
786
787

    my $experiment = Experiment->Lookup($self->slice_uuid());
Leigh B. Stoller's avatar
Leigh B. Stoller committed
788
789
790
791
    if (!defined($experiment)) {
	print STDERR "Could not map $self to its experiment\n";
	return -1;
    }
792
    my $uuid = $self->uuid();
Leigh B. Stoller's avatar
Leigh B. Stoller committed
793
    return 0
794
795
	if (!defined($uuid));
    my $node       = Node->Lookup($uuid);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
796
    if (!defined($node)) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
797
	# Lets call this nonfatal since it might be a virtnode that 
798
	print STDERR "Could not map node $uuid to its object\n";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
799
	return 0;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
800
801
802
803
804
805
806
    }
    my $reservation = $node->Reservation();
    if (!defined($reservation)) {
	print STDERR "$node was already released from $self\n";
	return 0;
    }
    if ($reservation->SameExperiment($experiment)) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
807
	my $node_id = $node->node_id();
Leigh B. Stoller's avatar
Leigh B. Stoller committed
808
809
	my $pid = $experiment->pid();
	my $eid = $experiment->eid();
Leigh B. Stoller's avatar
Leigh B. Stoller committed
810

811
	if ($node->isremotenode() && $node->isvirtnode()) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
812
	    system("$VNODESETUP -p -q -m -k $pid $eid $node_id");
813
	    if ($?) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
814
		print STDERR "$VNODESETUP -k failed on $node_id\n";
815
816
817
		return -1;
	    }
	}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
818
819
820
821
822
823
	elsif ($node->sharing_mode()) {
	    system("$VNODESETUP -j -q -m -k $pid $eid $node_id");
	    if ($?) {
		print STDERR "$VNODESETUP -k failed on $node_id\n";
		return -1;
	    }
824
	}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
825

826
827
	my $virt_type = GeniXML::GetText("virtualization_type",
					 $self->rspec());
828
	if (!$node->isremotenode() &&
829
	    defined($virt_type) &&
830
	    $virt_type eq "emulab-vnode") {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
831
832
833
834
835
836
	    my $pnode_id = $node->phys_nodeid();
	    my $pnode = Node->Lookup($pnode_id);
	    if (!defined($pnode)) {
		print STDERR "Could not get pnode object for $pnode_id\n";
		return -1;
	    }
837
838
839

	    #
	    # If this is the last virtnode on the physnode, release the
Leigh B. Stoller's avatar
Leigh B. Stoller committed
840
841
	    # physnode too. Unless its a shared host, in which case just
	    # deallocate the virtnode.
842
	    #
Leigh B. Stoller's avatar
Leigh B. Stoller committed
843
844
845
846
847
848
	    my @vnodes = ();
	    
	    if ($pnode->sharing_mode()) {
		$nophysfree = 1;
	    }
	    elsif ($pnode->VirtualNodes(\@vnodes) != 0) {
849
		print STDERR "Could not get vnode list for $pnode\n";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
850
851
		return -1;
	    }
852

853
	    if (scalar(@vnodes) > 1 || $nophysfree) {
854
		system("$NFREE -q $pid $eid $node_id");
855
856
857
		# Unincorporate the node. Bogus, needs fixing.
		$pnode->ModifyReservation({"genisliver_idx" => 0})
		    if (! ($pnode->sharing_mode() || scalar(@vnodes) > 1));
858
859
860
861
862
	    }
	    else {
		system("$NFREE -x -q $pid $eid $pnode_id");
		$pnode->Refresh();
	    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
863
864
	}
	else {
865
866
867
868
869
	    if ($nophysfree) {
		# Unincorporate the node. Bogus, needs fixing.
		$node->ModifyReservation({"genisliver_idx" => 0});
		goto skip;
	    }
870
	    system("$NFREE -q $pid $eid $node_id");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
871
	}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
872
873
	if ($?) {
	    print STDERR "Could not deallocate $node from $self\n";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
874
	    $node->Refresh();
Leigh B. Stoller's avatar
Leigh B. Stoller committed
875
876
	    return -1;
	}
877
      skip:
Leigh B. Stoller's avatar
Leigh B. Stoller committed
878
	$node->Refresh();
Leigh B. Stoller's avatar
Leigh B. Stoller committed
879
880
881
882
883
884
885
886
887
    }
    else {
	print STDERR "$node is reserved to another, not $self\n";
	# Signal error so we can look at what happened.
	return -1;
    }
    return 0;
}

888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
#
# Process a manifest.
#
sub ProcessManifest($$)
{
    my ($self, $manifest) = @_;

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

    my $experiment = Experiment->Lookup($self->slice_uuid());
    if (!defined($experiment)) {
	print STDERR "Could not map $self to its experiment\n";
	return -1;
    }
    my $pid = $experiment->pid();
    my $eid = $experiment->eid();
    
    my $uuid = $self->uuid();
    return 0
	if (!defined($uuid));
    my $node       = Node->Lookup($uuid);
    if (!defined($node)) {
	print STDERR "Could not map node $uuid to its object\n";
	return -1;
    }
    my $reservation = $node->Reservation();
    if (!defined($reservation)) {
	print STDERR "$node was already released from $self\n";
	return -1;
    }
    if ($reservation->SameExperiment($experiment)) {
	#
	# Find the corresponding node in the manifest.
	#
923
	foreach my $ref (GeniXML::FindNodes("n:node",
924
					    $manifest)->get_nodelist()) {
925
	    my $sliver_id = GeniXML::GetText("sliver_uuid", $ref);
926
	    if (defined($sliver_id) && $sliver_id eq $uuid) {
927
		# startup command.
928
929
		my $startupcmd = GeniXML::GetText("startup_command", $ref);
		if (defined($startupcmd)) {
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
		    if (! TBcheck_dbslot($startupcmd, "virt_nodes",
					 "startupcmd",
					 TBDB_CHECKDBSLOT_WARN|
					 TBDB_CHECKDBSLOT_ERROR)) {
			print STDERR "Invalid startup command '$startupcmd'\n";
			return -1;
		    }
		    $node->Update({"startupcmd" => $startupcmd});
		}
		return 0;
	    }
	}
    }
    else {
	print STDERR "$node is reserved to another, not $self\n";
	# Signal error so we can look at what happened.
	return -1;
    }
    return 0;
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
951
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
952
# Start (or restart) a node. Basically, a reboot.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
953
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
954
sub Start($$$)
Leigh B. Stoller's avatar
Leigh B. Stoller committed
955
{
Leigh B. Stoller's avatar
Leigh B. Stoller committed
956
    my ($self, $version, $restart) = @_;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
957
958
959
960
961
962
963
964
965

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

    my $experiment = Experiment->Lookup($self->slice_uuid());
    if (!defined($experiment)) {
	print STDERR "Could not map $self to its experiment\n";
	return -1;
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
966
967
968
    my $pid = $experiment->pid();
    my $eid = $experiment->eid();
    
969
    my $uuid = $self->uuid();
Leigh B. Stoller's avatar
Leigh B. Stoller committed
970
    return 0
971
972
	if (!defined($uuid));
    my $node       = Node->Lookup($uuid);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
973
    if (!defined($node)) {
974
	print STDERR "Could not map node $uuid to its object\n";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
975
976
977
978
979
980
981
982
	return -1;
    }
    my $reservation = $node->Reservation();
    if (!defined($reservation)) {
	print STDERR "$node was already released from $self\n";
	return -1;
    }
    if ($reservation->SameExperiment($experiment)) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
983
	my $node_id = $node->node_id();
Leigh B. Stoller's avatar
Leigh B. Stoller committed
984

Leigh B. Stoller's avatar
Leigh B. Stoller committed
985
986
987
	#
	# Reboot and wait?
	#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
988
989
990
991
992
993
994
995
	if ($node->isvirtnode() && $node->sharing_mode()) {
	    if ($node->eventstate() eq TBDB_NODESTATE_SHUTDOWN()) {
		system("$VNODESETUP -j -q -m $pid $eid $node_id");
	    }
	}
	else {
	    system("$NODEREBOOT -s $node_id");
	}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
996
997
	return -1
	    if ($?);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
998
999
1000
1001
1002
    }
    else {
	print STDERR "$node is reserved to another, not $self\n";
	# Signal error so we can look at what happened.
	return -1;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1003
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
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
    $self->SetState("started");
    return 0;
}
# And stop.
sub Stop($$)
{
    my ($self, $version) = @_;

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

    my $experiment = Experiment->Lookup($self->slice_uuid());
    if (!defined($experiment)) {
	print STDERR "Could not map $self to its experiment\n";
	return -1;
    }
    my $pid = $experiment->pid();
    my $eid = $experiment->eid();
    
    my $uuid = $self->uuid();
    return 0
	if (!defined($uuid));
    my $node       = Node->Lookup($uuid);
    if (!defined($node)) {
	print STDERR "Could not map node $uuid to its object\n";
	return -1;
    }
    my $reservation = $node->Reservation();
    if (!defined($reservation)) {
	print STDERR "$node was already released from $self\n";
	return -1;
    }
    if ($reservation->SameExperiment($experiment)) {
	my $node_id = $node->node_id();

	#
	# Reboot and wait?
	#
	if ($node->isvirtnode() && $node->sharing_mode()) {
	    if ($node->eventstate() eq TBDB_NODESTATE_SHUTDOWN()) {
		system("$VNODESETUP -j -k -q -m $pid $eid $node_id");
	    }
	}
	else {
	    system("$POWER off $node_id");
	}
	return -1
	    if ($?);
    }
    else {
	print STDERR "$node is reserved to another, not $self\n";
	# Signal error so we can look at what happened.
	return -1;
    }
    $self->SetState("stopped");
    return 0;
}

sub ComputeStatus($$)
{
    my ($self, $pref) = @_;
    my $status = undef;

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

Leigh B. Stoller's avatar
Leigh B. Stoller committed
1070
1071
1072
1073
1074
1075
1076
1077
    #
    # If the sliver is "broken" then call it failed.
    #
    if ($self->status() eq "broken") {
	$$pref = "failed";
	return 0;
    }

Leigh B. Stoller's avatar
Leigh B. Stoller committed
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
    my $uuid = $self->uuid();
    return -1
	if (!defined($uuid));
    my $node       = Node->Lookup($uuid);
    if (!defined($node)) {
	print STDERR "Could not map node $uuid to its object\n";
	return -1;
    }
    my $reservation = $node->Reservation();
    if (!defined($reservation)) {
	print STDERR "$node was already released from $self\n";
	return -1;
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1091
1092
1093
    #
    # Emulab does not return "unknown" ... we always know ...
    #
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1094
1095
1096
1097
1098
1099
1100
    my $eventstate = $node->eventstate();
    if ($eventstate eq TBDB_NODESTATE_ISUP()) {
	$status = "ready";
    }
    elsif ($eventstate eq TBDB_NODESTATE_TBFAILED()) {
	$status = "failed";
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1101
    elsif ($eventstate eq TBDB_NODESTATE_SHUTDOWN()) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1102
1103
	$status = "notready";
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1104
1105
1106
    else {
	$status = "changing";
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1107
1108
    $self->SetStatus($status);
    $$pref = $status;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1109
1110
    return 0;
}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1111

1112
##########################################################################
1113
#
1114
1115
1116
1117
1118
1119
1120
1121
1122
package GeniSliver::Interface;
use vars qw(@ISA);
@ISA = "GeniSliver";

use GeniDB;
use GeniComponent;
use GeniSlice;
use GeniCredential;
use GeniCertificate;
1123
use GeniUtil;
1124

1125
1126
1127
1128
1129
1130
1131
1132
1133
# Return the component URN. This is how a resource is resolved.
# Return the component URN. This is how a resource is resolved.
sub component_urn($)
{
    my ($self)  = @_;

    return GeniHRN::Generate("@OURDOMAIN@", "interface", $self->resource_id())
}

1134
1135
sub Create()
{
1136
    my ($class, $slice, $user, $nodeid, $iface, $linkname, $rspec) = @_;
1137

1138
1139
1140
1141
1142
1143
1144
    # These are silly; uuids should go away for interfaces. 
    my $interface_uuid = GeniUtil::NewUUID();
    my $component_urn  = GeniHRN::GenerateInterface($OURDOMAIN,
						    $nodeid, $iface);
    my $nickname       = "$linkname.$nodeid.$iface";
    my $hrn            = "${PGENIDOMAIN}.$nodeid.$iface";
    my $resource_id    = "$nodeid//$iface";
1145

1146
1147
1148
    #
    # Add this stuff to the rspec (which becomes the manifest).
    #
1149
1150
    GeniXML::SetText("sliver_uuid", $rspec, $interface_uuid);
    GeniXML::SetText("component_urn", $rspec, $component_urn);
1151

1152
    return GeniSliver->Create($slice, $user, $interface_uuid,
1153
			      $interface_uuid, "Interface", $resource_id,
1154
			      $hrn, $nickname, $rspec);
1155
1156
1157
1158
1159
1160
}

sub Provision($)
{
    my ($self) = @_;

1161
1162
1163
1164
    #
    # This is actually implemented in GeniAggregate since currently "link"
    # is the smallest entity; you cannot operate on an individual interface.
    #
1165
1166
1167
1168
1169
1170
1171
    return -1
	if (! ref($self));

    return 0;
}

#
1172
# Unprovision a single interface from a link/lan. 
1173
1174
1175
1176
1177
1178
1179
1180
#
sub UnProvision($)
{
    my ($self) = @_;

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

1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
    my $aggregate = $self->GetAggregate();
    if (!defined($aggregate)) {
	print STDERR "Could not find aggregate for $self\n";
	return -1;
    }

    #
    # This is terrible; we need an interface to remove ports form vlans,
    # so that we do not have to tear down the entire vlan and recreate.
    #
    if ($aggregate->UnProvision() != 0) {
	print STDERR "Could not unprovision $aggregate\n";
	return -1;
    }
    $self->ClearAggregate();
    if ($aggregate->Provision() != 0) {
	print STDERR "Could not provision $aggregate\n";
	return -1;
    }
1200
1201
1202
1203
    return 0;
}

#
1204
# Start a sliver.
1205
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1206
sub Start($$)
1207
1208
1209
{
    my ($self) = @_;

1210
1211
1212
1213
    #
    # This is actually implemented in GeniAggregate since currently "link"
    # is the smallest entity; you cannot operate on an individual interface.
    #
1214
1215
1216
1217
1218
    return -1
	if (! ref($self));

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

Leigh B. Stoller's avatar
Leigh B. Stoller committed
1220
1221
# _Always_ make sure that this 1 is at the end of the file...
1;