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

#
10
# Some simple aggregate stuff.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
11
12
13
14
15
16
17
18
19
20
21
22
23
#
use strict;
use Exporter;
use vars qw(@ISA @EXPORT);

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

# Must come after package declaration!
use GeniDB;
use GeniCredential;
use GeniCertificate;
use GeniSliver;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
24
use GeniSlice;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
25
use GeniRegistry;
26
use GeniUtil;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
27
use GeniUser;
28
use GeniComponent;
29
use GeniHRN;
30
use GeniXML;
31
use emutil;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
32
use Lan;
33
use Data::Dumper;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
34
35
36
use English;
use overload ('""' => 'Stringify');
use XML::Simple;
37
38
39
use POSIX qw(strftime);
use Time::Local;
use Date::Parse;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
40
41
42
43
44
45
46
47

# Configure variables
my $TB		   = "@prefix@";
my $TBOPS          = "@TBOPSEMAIL@";
my $TBAPPROVAL     = "@TBAPPROVALEMAIL@";
my $TBAUDIT   	   = "@TBAUDITEMAIL@";
my $BOSSNODE       = "@BOSSNODE@";
my $OURDOMAIN      = "@OURDOMAIN@";
48
my $PGENIDOMAIN    = "@PROTOGENI_DOMAIN@";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
49
50
my $SIGNCRED	   = "$TB/sbin/signgenicred";
my $VERIFYCRED	   = "$TB/sbin/verifygenicred";
51
my $NODEREBOOT	   = "$TB/bin/node_reboot";
52
my $VNODESETUP     = "$TB/sbin/vnode_setup";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
53
my $POWER          = "$TB/bin/power";
54
my $OSLOAD         = "$TB/bin/os_load";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
55
56
57
58
my $SNMPIT         = "$TB/bin/snmpit";
my $NAMEDSETUP     = "$TB/sbin/named_setup";
my $EXPORTS_SETUP  = "$TB/sbin/exports_setup";
my $GENTOPOFILE    = "$TB/libexec/gentopofile";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
59
60
61

# Cache of instances to avoid regenerating them.
my %aggregates     = ();
62
BEGIN { use GeniUtil; GeniUtil::AddCache(\%aggregates); }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
63
64

#
65
# Lookup by URN, idx, or uuid.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
66
67
68
69
70
71
72
#
sub Lookup($$)
{
    my ($class, $token) = @_;
    my $query_result;
    my $idx;

73
74
75
    if (GeniHRN::IsValid($token)) {
	my ($authority, $type, $id) = GeniHRN::Parse($token);
	return undef if $type ne "sliver";
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93

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

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

	    ($idx) = $query_result->fetchrow_array();
	}
94
95
    }
    elsif ($token =~ /^\d+$/) {
Leigh B. Stoller's avatar
Leigh B. 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
119
120
121
122
	$idx = $token;
    }
    elsif ($token =~ /^\w+\-\w+\-\w+\-\w+\-\w+$/) {
	$query_result =
	    DBQueryWarn("select idx from geni_aggregates ".
			"where uuid='$token'");
	    return undef
		if (! $query_result || !$query_result->numrows);

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

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

    my $self              = {};
    $self->{'AGGREGATE'}  = $query_result->fetchrow_hashref();
    $self->{'CREDENTIAL'} = undef;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
123
    $self->{'SLICE'}      = undef;
124
    $self->{'PARENT'}     = undef;
125
126
127

    # Bless into sub package if called for.
    my $type = $self->{'AGGREGATE'}->{'type'};
128
    if (defined($type) && $type ne "" && $type ne "Aggregate") {
129
130
131
132
133
	bless($self, $class . "::" . $type);
    }
    else {
	bless($self, $class);
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159

    #
    # Grab the certificate, since we will probably want it.
    #
    my $uuid = $self->{'AGGREGATE'}->{'uuid'};
    my $certificate = GeniCertificate->Lookup($uuid);
    if (!defined($certificate)) {
	print STDERR "Could not find certificate for aggregate $idx ($uuid)\n";
	return undef;
    }
    $self->{'CERTIFICATE'} = $certificate;
    
    # Add to cache. 
    $aggregates{$self->{'AGGREGATE'}->{'idx'}} = $self;
    
    return $self;
}

#
# Stringify for output.
#
sub Stringify($)
{
    my ($self) = @_;
    
    my $uuid = $self->uuid();
Leigh B. Stoller's avatar
Leigh B. Stoller committed
160
    my $hrn  = $self->hrn();
Leigh B. Stoller's avatar
Leigh B. Stoller committed
161
162
    my $idx  = $self->idx();

Leigh B. Stoller's avatar
Leigh B. Stoller committed
163
    return "[GeniAggregate: $hrn, IDX: $idx]";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
164
165
166
167
168
169
170
}

#
# Create a Geni aggregate in the DB. This happens on the server side only
# for now. The client side does not actually know its an aggregate, at
# least not yet.
#
171
sub Create($$$$$$)
Leigh B. Stoller's avatar
Leigh B. Stoller committed
172
{
173
    my ($class, $slice, $owner, $aggregate_type, $hrn, $nickname) = @_;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
174
175
176
    my @insert_data = ();

    # Every aggregate gets a new unique index.
177
    my $idx = TBGetUniqueIndex('next_sliver', 1);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
178
179

    # Create a cert pair, which gives us a new uuid.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
180
    my $urn = GeniHRN::Generate( "@OURDOMAIN@", "sliver", $idx );
181
    my $certificate = GeniCertificate->Create("aggregate", $urn, $hrn, $TBOPS);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
182
    if (!defined($certificate)) {
183
184
	print STDERR "GeniAggregate::Create: ".
	    "Could not generate new certificate and UUID for $hrn\n";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
185
186
187
	return undef;
    }
    my $uuid        = $certificate->uuid();
188
189
    my $slice_uuid  = $slice->uuid();
    my $owner_uuid  = $owner->uuid();
Leigh B. Stoller's avatar
Leigh B. Stoller committed
190
191
192
    $aggregate_type = "Aggregate"
    	if (! defined($aggregate_type));

Leigh B. Stoller's avatar
Leigh B. Stoller committed
193
194
195
    # Now tack on other stuff we need.
    push(@insert_data, "created=now()");
    push(@insert_data, "idx='$idx'");
196
    push(@insert_data, "hrn=" . DBQuoteSpecial($hrn));
197
    push(@insert_data, "nickname=" . DBQuoteSpecial($nickname));
Leigh B. Stoller's avatar
Leigh B. Stoller committed
198
199
200
    push(@insert_data, "uuid='$uuid'");
    push(@insert_data, "creator_uuid='$owner_uuid'");
    push(@insert_data, "slice_uuid='$slice_uuid'");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
201
    push(@insert_data, "type='$aggregate_type'");
202
203
    # Start out new aggregates, as new.
    push(@insert_data, "state='new'");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
204
205
206
207
208
209
210

    # Insert into DB.
    if (!DBQueryWarn("insert into geni_aggregates set " .
		     join(",", @insert_data))) {
	$certificate->Delete();
	return undef;
    }
211
212
213
214
215
216
217
218
219
    my $aggregate = GeniAggregate->Lookup($idx);
    return undef
	if (!defined($aggregate));

    if (GeniUsage->NewAggregate($aggregate, $slice, $owner)) {
	print STDERR "GeniAggregate::Create: ".
	    "GeniUsage->NewAggregate($aggregate) failed\n";
    }
    return $aggregate;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
220
221
222
223
224
}
# accessors
sub field($$) { return ((! ref($_[0])) ? -1 : $_[0]->{'AGGREGATE'}->{$_[1]}); }
sub idx($)		{ return field($_[0], "idx"); }
sub uuid($)		{ return field($_[0], "uuid"); }
225
sub nickname($)		{ return field($_[0], "nickname"); }
226
sub type($)		{ return field($_[0], "type"); }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
227
228
229
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
230
sub registered($)	{ return field($_[0], "registered"); }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
231
sub credential_idx($)	{ return field($_[0], "credential_idx"); }
232
sub aggregate_idx($)	{ return field($_[0], "aggregate_idx"); }
233
sub status($)		{ return field($_[0], "status"); }
234
sub state($)		{ return field($_[0], "state"); }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
235
236
237
sub cert($)		{ return $_[0]->{'CERTIFICATE'}->cert(); }
sub GetCertificate($)   { return $_[0]->{'CERTIFICATE'}; }

238
239
240
# An alias so that slivers look like aggregates.
sub resource_type($)	{ return field($_[0], "type"); }

241
242
243
244
245
246
247
248
# Return the URN.
sub urn($)
{
    my ($self) = @_;

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

249
250
251
252
#
# Destroy all the slivers in the aggregate, and then the aggregate if there
# is nothing in it. Leave it around if something goes wrong.
#
253
sub Delete($$)
254
{
255
    my ($self, $purge) = @_;
256
257
258
259
260
261
262
263
264
265
266
267
    my $broken = 0;

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

    my @slivers = ();
    if ($self->SliverList(\@slivers) != 0) {
	print STDERR "Could not get sliver list for $self\n";
	return -1;
    }
    foreach my $sliver (@slivers) {
	if ($sliver->status() eq "broken") {
268
	    print STDERR "Could not delete 'broken' $sliver from $self\n";
269
	    $broken++;
270
	    last;
271
	}
272
	if ($sliver->Delete($purge) != 0) {
273
274
275
	    print STDERR "Could not delete $sliver from $self\n";
	    $sliver->SetStatus("broken");
	    $broken++;
276
	    last;
277
278
279
280
281
	}
    }
    return -1
	if ($broken);
    
282
283
284
285
    if (GeniUsage->DestroyAggregate($self, $purge)) {
	print STDERR "GeniAggregate::Delete: ".
	    "GeniUsage->DestroyAggregate($self) failed\n";
    }
286
287
288
289
290
291
292
293
294
295
    my $idx  = $self->idx();
    my $uuid = $self->uuid();

    DBQueryWarn("delete from geni_credentials where this_uuid='$uuid'")
	or return -1;
    DBQueryWarn("delete from geni_certificates where uuid='$uuid'")
	or return -1;
    DBQueryWarn("delete from geni_aggregates where idx='$idx'")
	or return -1;
    
296
297
298
    # Delete from cache. 
    delete($aggregates{$idx});

299
300
301
    return 0;
}

302
303
304
305
306
307
308
#
# Cons up an hrn.
#
sub hrn($)
{
    my ($self) = @_;

309
310
311
312
313
    my $hrn = field($self, "hrn");

    if (defined($hrn) && $hrn ne "") {
	return $hrn;
    }
314
    return "${PGENIDOMAIN}.aggregate_" . $self->idx();
315
316
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#
# Look up toplevel aggregate for a locally instantiated slice. 
#
sub SliceAggregate($$)
{
    my ($class, $slice) = @_;

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

    my $query_result =
	DBQueryWarn("select idx from geni_aggregates ".
		    "where slice_uuid='$slice_uuid' and type='Aggregate'");
    return undef
	if (!$query_result);
    return undef
	if ($query_result->numrows != 1);

    my ($idx) = $query_result->fetchrow_array();
    my $aggregate = GeniAggregate->Lookup($idx);
    return undef
	if (!defined($aggregate));

    return $aggregate;
}

343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
#
# Look up a list of aggregates for a locally instantiated slice. 
# Used by the CM.
#
sub SliceAggregates($$$)
{
    my ($class, $slice, $pref) = @_;

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

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

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

Leigh B. Stoller's avatar
Leigh B. Stoller committed
370
371
372
373
374
375
376
377
378
379
380
#
# List of slivers for this aggregate.
#
sub SliverList($$)
{
    my ($self, $pref) = @_;
    my @result = ();
    
    return -1
	if (! (ref($self) && ref($pref)));

381
382
    my $idx  = $self->idx();
    my $uuid = $self->uuid();
Leigh B. Stoller's avatar
Leigh B. Stoller committed
383
    my $query_result =
384
385
	DBQueryWarn("select idx from geni_slivers ".
		    "where aggregate_uuid='$uuid'");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
386
387
388
389
390
391
392
393
394
395
396
    return -1
	if (!$query_result);

    while (my ($sliver_idx) = $query_result->fetchrow_array()) {
	my $sliver = GeniSliver->Lookup($sliver_idx);
	if (!defined($sliver)) {
	    print STDERR "Could not find sliver object for $sliver_idx\n";
	    return -1;
	}
	push(@result, $sliver);
    }
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415

    #
    # And any aggregates that are children.
    #
    $query_result =
	DBQueryWarn("select idx from geni_aggregates ".
		    "where aggregate_idx='$idx'");
    return -1
	if (!$query_result);

    while (my ($aggregate_idx) = $query_result->fetchrow_array()) {
	my $aggregate = GeniAggregate->Lookup($aggregate_idx);
	if (!defined($aggregate_idx)) {
	    print STDERR
		"Could not find aggregate object for $aggregate_idx\n";
	    return -1;
	}
	push(@result, $aggregate);
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
416
417
418
419
420
    @$pref = @result;
    return 0;
    
}

421
422
423
424
425
426
427
428
429
430
#
# Set the aggregate for an aggregate.
#
sub SetAggregate($$)
{
    my ($self, $aggregate) = @_;

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

431
432
433
    my $idx      = $self->idx();
    my $agg_idx  = $aggregate->idx();
    my $agg_uuid = $aggregate->uuid();
434
435
436
437
438
439

    return -1
	if (!DBQueryWarn("update geni_aggregates set ".
			 "  aggregate_idx='$agg_idx' ".
			 "where idx='$idx'"));
    
440
441
442
443
444
445
446
    if (!DBQueryWarn("update aggregate_history set ".
		     "  aggregate_uuid='$agg_uuid' ".
		     "where idx='$idx'")) {
	print STDERR "GeniAggregate::SetAggregate: ".
	    "Failed to update aggregate_history for $self\n";
    }

447
448
449
450
451
452
    $self->{'AGGREGATE'}->{'aggregate_idx'} = $agg_idx;
    $self->{'PARENT'} = $aggregate;
    return 0;
}

#
453
# Get the aggregate for an aggregate.
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
#
sub GetAggregate($)
{
    my ($self) = @_;

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

    return $self->{'PARENT'} if (defined($self->{'PARENT'}));
    return undef
	if (!defined($self->aggregate_idx()));

    my $aggregate = GeniAggregate->Lookup($self->aggregate_idx());
    if (!defined($aggregate)) {
	print STDERR "Could not get aggregate object associated with $self\n";
	return undef;
    }
    $self->{'PARENT'} = $aggregate;
    return $aggregate;
}

475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
#
# Is object in the aggregate.
#
sub IsMember($$)
{
    my ($self, $object) = @_;

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

    my $aggregate = $object->GetAggregate();
    return 0
	if (!$aggregate);
    return -1
	if ($self->idx() != $aggregate->idx());
    return 1;
}

493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
#
# Set the status for the aggregate
#
sub SetStatus($$)
{
    my ($self, $status) = @_;

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

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

514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
#
# Set the state for the aggregate
#
sub SetState($$)
{
    my ($self, $state) = @_;

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

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

Leigh B. Stoller's avatar
Leigh B. Stoller committed
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
#
# Set the registered datetime for the aggregate
#
sub SetRegistered($$)
{
    my ($self, $yesno) = @_;

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

    my $idx = $self->idx();
    my $val = ($yesno ? "now()" : "NULL");
    
    return -1
	if (!DBQueryWarn("update geni_aggregates set ".
			 "  registered=$val ".
			 "where idx='$idx'"));
    
    return 0;
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
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
#
# Get the slice for the aggregate.
#
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;
}

581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
#
# The expiration time for an aggregate 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();
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
#
# Get the creator for the aggregate.
#
sub GetCreator($)
{
    my ($self) = @_;

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

    if (!defined($self->creator_uuid())) {
	print STDERR "No creator associated with $self\n";
	return undef;
    }
    return GeniUser->Lookup($self->creator_uuid(), 1);
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
#
# Create a signed credential for this aggregate, issued to the provided user.
# The credential will grant all permissions for now.
#
# Should we store these credentials in the DB, recording what we hand out?
#
sub NewCredential($$)
{
    my ($self, $owner) = @_;

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

    my $credential = GeniCredential->Create($self, $owner);
    if (!defined($credential)) {
	print STDERR "Could not create credential for $self, $owner\n";
	return undef;
    }
634
635
636
    if (defined($self->nickname())) {
	$credential->AddExtension("nickname", $self->nickname());
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
637
638
639
640
641
642
643
    if ($credential->Sign($self->GetCertificate()) != 0) {
	print STDERR "Could not sign credential for $self, $owner\n";
	return undef;
    }
    return $credential;
}

644
645
646
#
# Get the manifest for an aggregate. Returns the XML string.
#
647
sub GetManifest($$)
648
{
649
    my ($self, $asxml) = @_;
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668

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

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

    my $query_result =
	DBQueryWarn("select manifest from geni_manifests ".
		    "where slice_uuid='$slice_uuid'");
    
    if (!$query_result || !$query_result->numrows) {
	print STDERR "GetManifest: Could not locate manifest for $self\n";
	return undef;
    }
    my ($xml) = $query_result->fetchrow_array();

669
670
    my $manifest = GeniXML::Parse($xml);
    if (!defined($manifest)) {
671
672
	return undef;
    }
673

674
675
676
    #
    # Update the manifest ticket to reflect the current expiration time.
    #
677
678
679
    my $valid_date = POSIX::strftime("20%y-%m-%dT%H:%M:%S",
				     gmtime(str2time($slice->expires())));
    GeniXML::SetText("valid_until", $manifest, $valid_date);
680

681
682
683
    return $manifest
	if (!$asxml);
    
684
    $xml = GeniXML::Serialize($manifest);
685
686
687
    return $xml;
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
688
#
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
# Process the manifest. Just hand off to the slivers.
#
sub ProcessManifest($$)
{
    my ($self, $manifest) = @_;

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

    my @slivers = ();
    if ($self->SliverList(\@slivers) != 0) {
	print STDERR "Could not get sliver list for $self\n";
	return -1;
    }

    foreach my $sliver (@slivers) {
	next
	    if (ref($sliver) ne "GeniSliver::Node");

	if ($sliver->ProcessManifest($manifest) != 0) {
	    return -1;
	}
    }
    return 0;
}

#
716
717
718
719
720
721
722
# Start all the slivers in the aggregate. Start is special since it
# sorta means reboot, and the only thing we reboot are nodes. And,
# since we might have multiple vnodes on a pnode, we want to be efficient
# about it.
#
# XXX Is is assumed that there is a single toplevel aggregate for the
# slice, so we can get all the nodes.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
723
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
724
sub Start($$$)
Leigh B. Stoller's avatar
Leigh B. Stoller committed
725
{
Leigh B. Stoller's avatar
Leigh B. Stoller committed
726
    my ($self, $version, $restart) = @_;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
727
728
729

    return -1
	if (! ref($self));
Leigh B. Stoller's avatar
Leigh B. Stoller committed
730
731
    $restart = 0
	if (!defined($restart));
Leigh B. Stoller's avatar
Leigh B. Stoller committed
732

733
734
735
736
737
738
739
740
    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();

Leigh B. Stoller's avatar
Leigh B. Stoller committed
741
742
743
744
745
    my @slivers = ();
    if ($self->SliverList(\@slivers) != 0) {
	print STDERR "Could not get sliver list for $self\n";
	return -1;
    }
746
747
748
    my %reboots  = ();
    my %vnodes   = ();
    my %poweron  = ();
749
    my %reloads  = ();
750

Leigh B. Stoller's avatar
Leigh B. Stoller committed
751
    foreach my $sliver (@slivers) {
752
753
754
755
756
757
758
759
	next
	    if (ref($sliver) ne "GeniSliver::Node");

	my $node = Node->Lookup($sliver->uuid());
	if (!defined($node)) {
	    print STDERR "Could not map $sliver to a node\n";
	    return -1;
	}
760
761
762
763
	my $reservation = $node->Reservation();
	if (!defined($reservation)) {
	    print STDERR "$node no long belongs to $self\n";
	    return -1;
764
	}
765
	if ($reservation->SameExperiment($experiment)) {
766
767
768
769
770
771
772
	    #
	    # Since this is an aggregate, some slivers may already be
	    # in the started state. Skip those, unless doing a restart.
	    #
	    next
		if ($sliver->state() eq "started" && !$restart);
	    
773
774
	    if ($node->isvirtnode()) {
		$vnodes{$node->node_id} = $node;
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798

		# A virtnode on a shared physical node needs nothing else.
		next
		    if ($node->sharing_mode());

		# But if non-shared, have to make sure that the phys node
		# gets loaded.
		my $physnodeid = $node->phys_nodeid();
		next
		    if (exists($poweron{$physnodeid}) ||
			exists($reboots{$physnodeid}) ||
			exists($reloads{$physnodeid}));
		$node = Node->Lookup($physnodeid);
		if (!defined($node)) {
		    print STDERR "Could not lookup $physnodeid\n";
		    return -1;
		}
	    }
	    #
	    # Look to see if local physical node was stopped (powered off).
	    #
	    if (!$node->isremotenode() &&
		$sliver->state() eq "stopped") {
		$poweron{$node->node_id} = $node;
799
800
	    }
	    else {
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
		# node_reboot is smart enough to know that if a pnode
		# is rebooted it can ignore the vnodes on it, so do
		# not optimize this here.
		$reboots{$node->node_id} = $node;
	    }
	    next
		if (!$node->imageable());

	    my $osinfo = OSinfo->Lookup($node->def_boot_osid());
	    if (!defined($osinfo)) {
		print STDERR "Could not get osinfo for $node\n";
		return -1;
	    }
	    print STDERR "$node wants to boot $osinfo.\n";
	    if ($osinfo->IsGeneric()) {
816
		#
817
		# Map generic OSID to the specific one.
818
		#
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
		my $tmp = $osinfo->ResolveNextOSID($experiment);
		if (!defined($tmp)) {
		    print STDERR "No next mapping for $osinfo on $node!\n";
		    return -1;
		}
		print STDERR "  Mapping $osinfo on $node to $tmp\n";
		$osinfo = $tmp;
	    }
	    #
	    # Make sure this OSID is actually loaded on the machine.
	    #
	    my $isloaded = $node->IsOSLoaded($osinfo);
	    if ($isloaded < 0) {
		print STDERR
		    "Error determining if $osinfo is loaded on $node\n";
		return -1;
	    }
	    if (! $isloaded) {
		print STDERR "  Setting up a reload for $node\n";
		
		my $image = $osinfo->MapToImage($node->type());
		if (!defined($image)) {
		    print STDERR "  No image for $osinfo on $node\n";
		    return -1;
843
		}
844
845
846
847
848
849
850
851
852
853
854
855
		if (!exists($reloads{$image->imageid()})) {
		    $reloads{$image->imageid()} = [ ];
		} 
		push(@{ $reloads{$image->imageid()} }, $node);
	    }
	    else {
		#
		# Make sure boot is set correctly.
		#
		if ($node->OSSelect($osinfo, "def_boot_osid", 0)) {
		    print STDERR "  Could not os_select $node to $osinfo\n";
		    return -1;
856
		}
857
858
859
860
861
862
863
864
865
866
867
868
869
870
	    }
	}
	else {
	    print STDERR "$node is reserved to another, not $self\n";
	    # Signal error so we can look at what happened.
	    return -1;
	}
    }
    #
    # Cull out vnodes that are going to get rebooted cause the
    # physnode is getting rebooted. 
    #
    my %tmp = %vnodes;
    foreach my $vnode (values(%vnodes)) {
871
872
	if (! (exists($reboots{$vnode->phys_nodeid()}) ||
	       exists($poweron{$vnode->phys_nodeid()}))) {
873
	    $tmp{$vnode->node_id()} = $vnode;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
874
	}
875
    }
876
877
    %vnodes = %tmp;

878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
    #
    # Setup the reloads. We do not reboot the nodes until below.
    #
    if (keys(%reloads)) {
	foreach my $imageid (keys(%reloads)) {
	    my @nodes = @{ $reloads{$imageid} };
	    my @node_ids = map { $_->node_id() } @nodes;

	    # No wait, no reboot. reload runs completely in the background.
	    system("$OSLOAD -s -r -m $imageid @node_ids");
	    return -1
		if ($?);
	}
    }

Leigh B. Stoller's avatar
Leigh B. Stoller committed
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
    if ($version >= 2) {
	if (system("$GENTOPOFILE $pid $eid")) {
	    print STDERR "$GENTOPOFILE failed\n";
	    return -1;
	}
	if (system("$EXPORTS_SETUP")) {
	    print STDERR "$EXPORTS_SETUP failed\n";
	    return -1;
	}
	# The nodes will not boot locally unless there is a DNS record.
	if (system("$NAMEDSETUP")) {
	    print STDERR "$NAMEDSETUP failed\n";
	    return -1;
	}
	if ($restart) {
	    my @diff = ();
	    my @same = ();
	    
	    if (Lan->CompareVlansWithSwitches($experiment, \@diff, \@same)) {
		print STDERR "CompareVlansWithSwitches failed!\n";
		return -1;
	    }
	    if (@diff) {
		system("$SNMPIT -f ". join(" ", map("-o $_", @diff)));
		if ($?) {
		    print STDERR "Failed to remove obsolete VLANs.\n";
		    return -1;
		}
	    }
	}
923
	system("$SNMPIT -q -t $pid $eid");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
924
925
926
927
928
929
	if ($?) {
	    print STDERR "$SNMPIT failed\n";
	    return -1;
	}
    }

930
    #
931
932
    # First power on any physical nodes that had been stopped.
    # Then reboot the physical nodes, then any leftover virtual nodes.
933
    #
934
935
936
937
938
939
940
941
942
943
944
945
    if (keys(%poweron)) {
	my @node_ids = keys(%poweron);

	#
	# Should waiting be an option?
	#
	system("$POWER on @node_ids");
	return -1
	    if ($?);
    }
    if (keys(%reboots)) {
	my @node_ids = keys(%reboots);
946
947
948
949
950
951
952
953
954
955

	#
	# Should waiting be an option?
	#
	system("$NODEREBOOT @node_ids");
	return -1
	    if ($?);
    }
    if (keys(%vnodes)) {
	my @node_ids = keys(%vnodes);
956
957
958
959

	#
	# Should waiting be an option?
	#
960
	system("$VNODESETUP -j -m $pid $eid @node_ids");
961
962
	return -1
	    if ($?);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
963
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
    #
    # Worked? Set the new state. Needs more thought ...
    #
    foreach my $sliver (@slivers) {
	$sliver->SetState("started")
	    if (ref($sliver) eq "GeniSliver::Node");
    }
    return 0;
}

#
# Stop all the slivers in the aggregate. Stop is brutal, better to
# use restart!
#
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 @slivers = ();
    if ($self->SliverList(\@slivers) != 0) {
	print STDERR "Could not get sliver list for $self\n";
	return -1;
    }
    my %pnodes = ();
    my %vnodes = ();

    foreach my $sliver (@slivers) {
	next
	    if (ref($sliver) ne "GeniSliver::Node");

	my $node = Node->Lookup($sliver->uuid());
	if (!defined($node)) {
	    print STDERR "Could not map $sliver to a node\n";
	    return -1;
	}
	my $reservation = $node->Reservation();
	if (!defined($reservation)) {
1012
	    print STDERR "$node no longer belongs to $self\n";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1013
1014
1015
	    return -1;
	}
	if ($reservation->SameExperiment($experiment)) {
1016
1017
1018
1019
1020
	    #
	    # Since this is an aggregate, some slivers may already be
	    # in the stopped state. Skip those.
	    #
	    next
1021
		if ($sliver->state() eq "stopped");
1022
	    
Leigh B. Stoller's avatar
Leigh B. Stoller committed
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
	    if ($node->isvirtnode()) {
		$vnodes{$node->node_id} = $node;
	    }
	    else {
		# node_reboot is smart enough to know that if a pnode
		# is rebooted it can ignore the vnodes on it, so do
		# not optimize this here.
		$pnodes{$node->node_id} = $node;
	    }
	}
	else {
	    print STDERR "$node is reserved to another, not $self\n";
	    # Signal error so we can look at what happened.
	    return -1;
	}
    }
    #
    # Cull out vnodes that are going to get killed cause the
    # physnode is getting powered down.
    #
    my %tmp = %vnodes;
    foreach my $vnode (values(%vnodes)) {
	if (!exists($pnodes{$vnode->phys_nodeid()})) {
	    $tmp{$vnode->node_id()} = $vnode;
	}
    }
    %vnodes = %tmp;

    if ($version >= 2) {
1052
	system("$SNMPIT -q -r $pid $eid");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
	if ($?) {
	    print STDERR "$SNMPIT failed\n";
	    return -1;
	}
    }
    
    #
    # Now power down the physical nodes, then any leftover virtual nodes.
    #
    if (keys(%pnodes)) {
	my @node_ids = keys(%pnodes);

	#
	# Should waiting be an option?
	#
	system("$POWER off @node_ids");
	return -1
	    if ($?);
    }
    if (keys(%vnodes)) {
	my @node_ids = keys(%vnodes);

	#
	# Should waiting be an option?
	#
	system("$VNODESETUP -j -k -m $pid $eid @node_ids");
	return -1
	    if ($?);
    }
    #
    # Worked? Set the new state. Needs more thought ...
    #
    foreach my $sliver (@slivers) {
	$sliver->SetState("stopped")
	    if (ref($sliver) eq "GeniSliver::Node");
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1089
1090
1091
    return 0;
}

1092
1093
1094
#
# Provision all the slivers in the aggregate.
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1095
sub Provision($;$)
1096
{
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1097
    my ($self, $extraargs) = @_;
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107

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

    my @slivers = ();
    if ($self->SliverList(\@slivers) != 0) {
	print STDERR "Could not get sliver list for $self\n";
	return -1;
    }
    foreach my $sliver (@slivers) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1108
	if ($sliver->Provision($extraargs) != 0) {
1109
1110
1111
1112
1113
1114
1115
	    print STDERR "Could not provision $sliver in $self\n";
	    next;
	}
    }
    return 0;
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
1116
1117
1118
#
# Unprovision all the slivers in the aggregate.
#
1119
sub UnProvision($;$)
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1120
{
1121
    my ($self, $nophysfree) = @_;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1122
1123
1124
1125
1126
1127
1128
1129
1130

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

    my @slivers = ();
    if ($self->SliverList(\@slivers) != 0) {
	print STDERR "Could not get sliver list for $self\n";
	return -1;
    }
1131
1132
1133
1134
1135

    #
    # Might be an aggregate that includes link aggregates. Lets do those
    # first to avoid work when tearing down the nodes.
    #
1136
1137
    my @links = ();
    my @nodes = ();
1138

Leigh B. Stoller's avatar
Leigh B. Stoller committed
1139
    foreach my $sliver (@slivers) {
1140
1141
1142
	if (ref($sliver) eq "GeniAggregate::Link" ||
	    ref($sliver) eq "GeniAggregate::Tunnel") {
	    push(@links, $sliver);
1143
	}
1144
	elsif (ref($sliver) eq "GeniAggregate") {
1145
1146
	    print STDERR "Unprovision: Unknown aggregate $sliver in $self\n";
	    return -1;
1147
1148
1149
1150
1151
1152
	}
	elsif (ref($sliver) eq "GeniSliver::Node") {
	    push(@nodes, $sliver);
	}
    }
    foreach my $sliver (@links) {
1153
1154
1155
1156
1157
1158
	if ($sliver->UnProvision() != 0) {
	    print STDERR "Could not unprovision $sliver in $self\n";
	    $sliver->SetStatus("broken");
	    next;
	}
    }
1159
    foreach my $sliver (@nodes) {
1160
	if ($sliver->UnProvision($nophysfree) != 0) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1161
	    print STDERR "Could not unprovision $sliver in $self\n";
1162
	    $sliver->SetStatus("broken");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1163
1164
1165
1166
1167
1168
	    next;
	}
    }
    return 0;
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
1169
1170
1171
1172
1173
1174
sub ComputeState($)
{
    my ($self) = @_;
    my $started = 0;
    my $stopped = 0;
    my $unknown = 0;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1175
1176
1177
1178
    my $ready   = 0;
    my $notready= 0;
    my $failed  = 0;
    my $changing= 0;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
    my $count   = 0;

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

    my @slivers = ();
    if ($self->SliverList(\@slivers) != 0) {
	print STDERR "Could not get sliver list for $self\n";
	return -1;
    }
    
    foreach my $sliver (@slivers) {
	#
	# Just nodes for now.
	#
	next
	    if (ref($sliver) ne "GeniSliver::Node");

	my $status;
	my $state = $sliver->state();
	if (!defined($state)) {
	    print STDERR "Could not determine state for $sliver in $self\n";
	    return -1;
	}
	if ($sliver->ComputeStatus(\$status)) {
	    print STDERR "Could not determine status for $sliver in $self\n";
	    return -1;
	}
	if ($state eq "started") {
	    $started++;
	}
1210
	elsif ($state eq "stopped" || $state eq "new") {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1211
1212
1213
1214
1215
	    $stopped++;
	}
	else {
	    $unknown++;
	}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
	if ($status eq "ready") {
	    $ready++;
	}
	elsif ($status eq "notready") {
	    $notready++;
	}
	elsif ($status eq "failed" ||
	       $status eq "broken") {
	    $failed++;
	}
	elsif ($status eq "changing") {
	    $changing++;
	}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
	$count++;
    }
    if ($stopped == $count) {
	$self->SetState("stopped");
    }
    elsif ($started == $count) {
	$self->SetState("started");
    }
    else {
	$self->SetState("mixed");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
    }
    if ($ready == $count) {
	$self->SetStatus("ready");
    }
    elsif ($notready == $count) {
	$self->SetStatus("notready");
    }
    elsif ($changing == $count) {
	$self->SetStatus("changing");
    }
    elsif ($failed) {
	$self->SetStatus("failed");
    }
    else {
	$self->SetStatus("mixed");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1254
1255
1256
1257
    }
    return 0;
}

1258
############################################################################
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1259
#
1260
# Link aggregates need special handling.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1261
#
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
package GeniAggregate::Link;
use vars qw(@ISA);
@ISA = "GeniAggregate";

use GeniDB;
use GeniSlice;
use GeniCredential;
use GeniCertificate;
use GeniAggregate;
use Experiment;
use Interface;

1274
sub Create($$$)
1275
{
1276
1277
1278
1279
1280
    my ($class, $slice, $owner, $linkname) = @_;

    #
    # Form an hrn using the slicename and linkname
    #
1281
    my $hrn = "${PGENIDOMAIN}." . $slice->slicename() . "." . $linkname;
1282

1283
    return GeniAggregate->Create($slice, $owner, "Link", $hrn, $linkname);
1284
1285
1286
}

#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1287
1288
# Provision all the slivers in the aggregate. For links, this is done
# for the entire aggregate (experiment) at once.
1289
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1290
sub Provision($;$)
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1291
{
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1292
    my ($self, $extraargs) = @_;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1293
1294
1295
1296

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

Leigh B. Stoller's avatar
Leigh B. Stoller committed
1297
    $self->SetStatus("ready");
1298
1299
1300
    return 0;

  bad:
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1301
    return -1
1302
}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1303

1304
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1305
1306
# Unprovision all the slivers in the aggregate. For links, this is done
# for the entire aggregate (experiment) at once.
1307
1308
1309
1310
1311
1312
1313
1314
#
sub UnProvision($)
{
    my ($self) = @_;

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

Leigh B. Stoller's avatar
Leigh B. Stoller committed
1315
1316
1317
    return 0;
}

1318
1319
1320
#
# Nothing to do yet.
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1321
sub Start($$)
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
{
    my ($self) = @_;

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

    return 0;
}

#
# Nothing to do yet.
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1334
sub Stop($$)
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
{
    my ($self) = @_;

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

    return 0;
}


Leigh B. Stoller's avatar
Leigh B. Stoller committed
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
############################################################################
#
# Tunnel aggregates need special handling too
#
package GeniAggregate::Tunnel;
use vars qw(@ISA);
@ISA = "GeniAggregate";

use GeniDB;
use GeniSlice;
use GeniCredential;
use GeniCertificate;
use GeniRegistry;
use GeniAggregate;
1359
use GeniUtil;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1360
1361
1362
1363
1364
1365
use Experiment;
use Interface;
use Data::Dumper;

sub Create($$$$$$)
{
1366
1367
    my ($class, $slice, $owner, $node1sliver, $node2sliver,
	$linkrspec, $node1rspec, $node2rspec) = @_;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1368
1369
    my $clearinghouse;

1370
    my $linkname = GeniXML::GetVirtualId($linkrspec);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1371
1372
    return undef
	if (!defined($linkname));
1373
    my @interfaces = GeniXML::FindNodes("n:interface_ref",
1374
					$linkrspec)->get_nodelist();
1375
    
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1376
1377
1378
1379
1380
1381
1382
1383
1384
    my $experiment = Experiment->Lookup($slice->uuid());
    if (!defined($experiment)) {
	print STDERR "Could not map $slice to its experiment\n";
	return -1;
    }

    #
    # Form an hrn using the slicename and linkname
    #
1385
    my $hrn = "${PGENIDOMAIN}." . $slice->slicename() . "." . $linkname;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400

    my $aggregate = GeniAggregate->Create($slice, $owner, "Tunnel",
					  $hrn, $linkname);
    goto bad
	if (!defined($aggregate));

    #
    # Create a tunnel entry in the lans table.
    #
    my $tunnel = Tunnel->Create($experiment, $aggregate->uuid(), "", "gre");
    if (!defined($tunnel)) {
	print STDERR "Could not create tunnel entry in lans table\n";
	return undef;
    }

1401
1402
    my $iface1ref = $interfaces[0];
    my $iface2ref = $interfaces[1];
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1403
1404

    # These are the ips of the tunnel.
1405
1406
    my $ip1      = GeniXML::GetText("tunnel_ip", $iface1ref);
    my $ip2      = GeniXML::GetText("tunnel_ip", $iface2ref);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1407
1408
1409
1410
1411
1412
1413
1414
    my $ctrlip1;
    my $ctrlip2;
    my $iface1;
    my $iface2;

    # We need the control network addresses, but it is possible that
    # one of the nodes is not on this testbed.
    if (defined($node1sliver)) {
1415
1416
1417
1418
1419
	my $node1 = Node->Lookup($node1sliver->resource_uuid());
	if (!defined($node1)) {
	    print STDERR "Tunnel: Could not lookup node for $node1sliver\n";
	    goto bad;
 	}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
	my $interface = Interface->LookupControl($node1);
	goto bad
	    if (!defined($interface));
	$ctrlip1 = $interface->IP();
	$iface1  = $tunnel->AddMember($node1);
	if (!defined($iface1)) {
	    print STDERR "Could not add $node1 to $tunnel\n";
	    goto bad;
	}
    }
    else {
1431
	my $node_uuid = GeniXML::GetNodeId($node1rspec);
1432
	my $component = GeniComponent->CreateFromRegistry($node_uuid);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1433
	
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1434
	if (!defined($component)) {
1435
	    print STDERR "Could not create component for $node_uuid\n";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1436
1437
	    goto bad;
	}
1438
1439
1440
	my $blob = $component->Resolve();
	if (!defined($blob)) {
	    print STDERR "Could not Resolve $component\n";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1441
1442
	    goto bad;
	}
1443
1444
	if (!exists($blob->{'physctrl'}) || !defined($blob->{'physctrl'})) {
	    print STDERR "Could not get control IP for $component\n";
1445
1446
	    goto bad;
	}
1447
	$ctrlip1 = $blob->{'physctrl'};
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1448
1449
    }
    if (defined($node2sliver)) {
1450
1451
1452
1453
1454
	my $node2 = Node->Lookup($node2sliver->resource_uuid());
	if (!defined($node2)) {
	    print STDERR "Tunnel: Could not lookup node for $node2sliver\n";
	    goto bad;
 	}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
	my $interface = Interface->LookupControl($node2);
	goto bad
	    if (!defined($interface));
	$ctrlip2 = $interface->IP();
	$iface2  = $tunnel->AddMember($node2);
	if (!defined($iface2)) {
	    print STDERR "Could not add $node2 to $tunnel\n";
	    goto bad;
	}
    }
    else {
1466
	my $node_uuid = GeniXML::GetNodeId($node2rspec);
1467
	my $component = GeniComponent->CreateFromRegistry($node_uuid);
1468
	
1469
1470
	if (!defined($component)) {
	    print STDERR "Could not create component for $node_uuid\n";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1471
1472
	    goto bad;
	}
1473
1474
1475
	my $blob = $component->Resolve();
	if (!defined($blob)) {
	    print STDERR "Could not Resolve $component\n";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1476
1477
	    goto bad;
	}
1478
1479
	if (!exists($blob->{'physctrl'}) || !defined($blob->{'physctrl'})) {
	    print STDERR "Could not get control IP for $component\n";
1480
1481
	    goto bad;
	}
1482
	$ctrlip2 = $blob->{'physctrl'};
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1483
    }
1484
    # print STDERR "$ip1, $ip2, $ctrlip1, $ctrlip2\n";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1485
1486
1487
1488
1489
1490
1491
1492

    if (defined($iface1)) {
	$iface1->SetAttribute("tunnel_ip", $ip1);
	$iface1->SetAttribute("tunnel_peerip", $ip2);
	$iface1->SetAttribute("tunnel_srcip", $ctrlip1);
	$iface1->SetAttribute("tunnel_dstip", $ctrlip2);
	$iface1->SetAttribute("tunnel_ipmask", "255.255.255.0");
	$iface1->SetAttribute("tunnel_lan", $linkname);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1493
	$iface1->SetAttribute("tunnel_unit", $iface1->memberid() + 1);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1494
1495
1496
1497
1498
1499
1500
1501
1502
	$iface1->SetAttribute("tunnel_style", "gre");
    }
    if (defined($iface2)) {
	$iface2->SetAttribute("tunnel_ip", $ip2);
	$iface2->SetAttribute("tunnel_peerip", $ip1);
	$iface2->SetAttribute("tunnel_srcip", $ctrlip2);
	$iface2->SetAttribute("tunnel_dstip", $ctrlip1);
	$iface2->SetAttribute("tunnel_ipmask", "255.255.255.0");
	$iface2->SetAttribute("tunnel_lan", $linkname);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1503
	$iface2->SetAttribute("tunnel_unit", $iface2->memberid() + 1);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1504
1505
1506
1507
1508
1509
1510
	$iface2->SetAttribute("tunnel_style", "gre");
    }
    return $aggregate;

  bad:
    $tunnel->Destroy()
	if (defined($tunnel));
1511
    $aggregate->Delete(GENI_PURGEFLAG)
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
	if (defined($aggregate));
    return undef;
}

#
# All the work done above.
#
sub Provision($)
{
    my ($self) = @_;

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

    $self->SetStatus("ready");
    return 0;
}

#
# Destroy the underlying tunnel in the lans table.
#
sub UnProvision($)
{
    my ($self) = @_;

    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 $tunnel = Tunnel->Lookup($experiment, $self->uuid());
    if (! defined($tunnel)) {
	print STDERR "No tunnel associated with $self\n";
	return 0;
    }
    if ($tunnel->Destroy() != 0) {
	print STDERR "Could not destroy $tunnel\n";
	return -1;
    }
    return 0;
}

#
# Nothing to do yet.
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1561
sub Start($$)
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
{
    my ($self) = @_;

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

    return 0;
}

#
# Nothing to do yet.
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1574
sub Stop($$)
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
{
    my ($self) = @_;

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

    return 0;
}


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