GeniAggregate.pm.in 24.2 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-2009 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;
27
use emutil;
28
use Data::Dumper;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
29
30
31
32
33
34
35
36
37
38
39
use English;
use overload ('""' => 'Stringify');
use XML::Simple;

# Configure variables
my $TB		   = "@prefix@";
my $TBOPS          = "@TBOPSEMAIL@";
my $TBAPPROVAL     = "@TBAPPROVALEMAIL@";
my $TBAUDIT   	   = "@TBAUDITEMAIL@";
my $BOSSNODE       = "@BOSSNODE@";
my $OURDOMAIN      = "@OURDOMAIN@";
40
my $PGENIDOMAIN    = "@PROTOGENI_DOMAIN@";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
41
42
my $SIGNCRED	   = "$TB/sbin/signgenicred";
my $VERIFYCRED	   = "$TB/sbin/verifygenicred";
43
my $NODEREBOOT	   = "$TB/bin/node_reboot";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
44
45
46

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

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

    if ($token =~ /^\d+$/) {
	$idx = $token;
    }
    elsif ($token =~ /^\w+\-\w+\-\w+\-\w+\-\w+$/) {
	$query_result =
	    DBQueryWarn("select idx from geni_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
86
    $self->{'SLICE'}      = undef;
87
    $self->{'PARENT'}     = undef;
88
89
90

    # Bless into sub package if called for.
    my $type = $self->{'AGGREGATE'}->{'type'};
91
    if (defined($type) && $type ne "" && $type ne "Aggregate") {
92
93
94
95
96
	bless($self, $class . "::" . $type);
    }
    else {
	bless($self, $class);
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132

    #
    # 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();
    my $idx  = $self->idx();

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

#
# 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.
#
133
sub Create($$$$$$)
Leigh B. Stoller's avatar
Leigh B. Stoller committed
134
{
135
    my ($class, $slice, $owner, $aggregate_type, $hrn, $nickname) = @_;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
136
137
138
139
140
141
    my @insert_data = ();

    # Every aggregate gets a new unique index.
    my $idx = TBGetUniqueIndex('next_aggregate', 1);

    # Create a cert pair, which gives us a new uuid.
142
    my $certificate = GeniCertificate->Create("aggregate", $hrn, $TBOPS);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
143
    if (!defined($certificate)) {
144
145
	print STDERR "GeniAggregate::Create: ".
	    "Could not generate new certificate and UUID for $hrn\n";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
146
147
148
	return undef;
    }
    my $uuid        = $certificate->uuid();
149
150
    my $slice_uuid  = $slice->uuid();
    my $owner_uuid  = $owner->uuid();
Leigh B. Stoller's avatar
Leigh B. Stoller committed
151
152
153
    $aggregate_type = "Aggregate"
    	if (! defined($aggregate_type));

Leigh B. Stoller's avatar
Leigh B. Stoller committed
154
155
156
    # Now tack on other stuff we need.
    push(@insert_data, "created=now()");
    push(@insert_data, "idx='$idx'");
157
    push(@insert_data, "hrn=" . DBQuoteSpecial($hrn));
158
    push(@insert_data, "nickname=" . DBQuoteSpecial($nickname));
Leigh B. Stoller's avatar
Leigh B. Stoller committed
159
160
161
    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
162
    push(@insert_data, "type='$aggregate_type'");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
163
164
165
166
167
168
169

    # Insert into DB.
    if (!DBQueryWarn("insert into geni_aggregates set " .
		     join(",", @insert_data))) {
	$certificate->Delete();
	return undef;
    }
170
171
172
173
174
175
176
177
178
    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
179
180
181
182
183
}
# accessors
sub field($$) { return ((! ref($_[0])) ? -1 : $_[0]->{'AGGREGATE'}->{$_[1]}); }
sub idx($)		{ return field($_[0], "idx"); }
sub uuid($)		{ return field($_[0], "uuid"); }
184
sub nickname($)		{ return field($_[0], "nickname"); }
185
sub type($)		{ return field($_[0], "type"); }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
186
187
188
189
sub slice_uuid($)	{ return field($_[0], "slice_uuid"); }
sub creator_uuid($)	{ return field($_[0], "creator_uuid"); }
sub created($)		{ return field($_[0], "created"); }
sub credential_idx($)	{ return field($_[0], "credential_idx"); }
190
sub aggregate_idx($)	{ return field($_[0], "aggregate_idx"); }
191
sub status($)		{ return field($_[0], "status"); }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
192
193
194
sub cert($)		{ return $_[0]->{'CERTIFICATE'}->cert(); }
sub GetCertificate($)   { return $_[0]->{'CERTIFICATE'}; }

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

198
199
200
201
#
# Destroy all the slivers in the aggregate, and then the aggregate if there
# is nothing in it. Leave it around if something goes wrong.
#
202
sub Delete($$)
203
{
204
    my ($self, $purge) = @_;
205
206
207
208
209
210
211
212
213
214
215
216
    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") {
217
	    print STDERR "Could not delete 'broken' $sliver from $self\n";
218
	    $broken++;
219
	    last;
220
	}
221
	if ($sliver->Delete($purge) != 0) {
222
223
224
	    print STDERR "Could not delete $sliver from $self\n";
	    $sliver->SetStatus("broken");
	    $broken++;
225
	    last;
226
227
228
229
230
	}
    }
    return -1
	if ($broken);
    
231
232
233
234
    if (GeniUsage->DestroyAggregate($self, $purge)) {
	print STDERR "GeniAggregate::Delete: ".
	    "GeniUsage->DestroyAggregate($self) failed\n";
    }
235
236
237
238
239
240
241
242
243
244
    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;
    
245
246
247
    # Delete from cache. 
    delete($aggregates{$idx});

248
249
250
    return 0;
}

251
252
253
254
255
256
257
#
# Cons up an hrn.
#
sub hrn($)
{
    my ($self) = @_;

258
259
260
261
262
    my $hrn = field($self, "hrn");

    if (defined($hrn) && $hrn ne "") {
	return $hrn;
    }
263
    return "${PGENIDOMAIN}.aggregate_" . $self->idx();
264
265
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#
# 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;
}

292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#
# 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
319
320
321
322
323
324
325
326
327
328
329
#
# List of slivers for this aggregate.
#
sub SliverList($$)
{
    my ($self, $pref) = @_;
    my @result = ();
    
    return -1
	if (! (ref($self) && ref($pref)));

330
331
    my $idx  = $self->idx();
    my $uuid = $self->uuid();
Leigh B. Stoller's avatar
Leigh B. Stoller committed
332
    my $query_result =
333
334
	DBQueryWarn("select idx from geni_slivers ".
		    "where aggregate_uuid='$uuid'");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
335
336
337
338
339
340
341
342
343
344
345
    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);
    }
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364

    #
    # 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
365
366
367
368
369
    @$pref = @result;
    return 0;
    
}

370
371
372
373
374
375
376
377
378
379
#
# Set the aggregate for an aggregate.
#
sub SetAggregate($$)
{
    my ($self, $aggregate) = @_;

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

380
381
382
    my $idx      = $self->idx();
    my $agg_idx  = $aggregate->idx();
    my $agg_uuid = $aggregate->uuid();
383
384
385
386
387
388

    return -1
	if (!DBQueryWarn("update geni_aggregates set ".
			 "  aggregate_idx='$agg_idx' ".
			 "where idx='$idx'"));
    
389
390
391
392
393
394
395
    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";
    }

396
397
398
399
400
401
    $self->{'AGGREGATE'}->{'aggregate_idx'} = $agg_idx;
    $self->{'PARENT'} = $aggregate;
    return 0;
}

#
402
# Get the aggregate for an aggregate.
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
#
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;
}

424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
#
# 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;
}

442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
#
# 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;
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
#
# 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;
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
#
# 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;
    }
506
507
508
    if (defined($self->nickname())) {
	$credential->AddExtension("nickname", $self->nickname());
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
509
510
511
512
513
514
515
516
    if ($credential->Sign($self->GetCertificate()) != 0) {
	print STDERR "Could not sign credential for $self, $owner\n";
	return undef;
    }
    return $credential;
}

#
517
518
519
520
521
522
523
# 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
524
#
525
sub Start($)
Leigh B. Stoller's avatar
Leigh B. Stoller committed
526
527
528
529
530
531
532
533
534
535
536
{
    my ($self) = @_;

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

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

    my %reboots = ();

Leigh B. Stoller's avatar
Leigh B. Stoller committed
540
    foreach my $sliver (@slivers) {
541
542
543
544
545
546
547
548
549
	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;
	}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
550
551
552
553
554
555
	# Remote/shared nodes are handled special.
	if ($node->isremotenode() ||
	    ($node->isvirtnode() && $node->sharing_mode())) {
	    $sliver->Start() == 0
		or return -1;
	    next;
556
557
558
559
560
561
	}

	# We assume local nodes are not shared, so reboot pnode; this
	# gets all the vnodes on that pnode.
	if ($node->isvirtnode()) {
	    $reboots{$node->phys_nodeid()} = 1;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
562
	}
563
564
565
566
567
568
569
570
571
572
573
574
575
	# 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} = 1;
    }
    my @node_ids = keys(%reboots);

    if (@node_ids) {
	#
	# Should waiting be an option?
	#
	system("$NODEREBOOT -s @node_ids");
	return -1
	    if ($?);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
576
577
578
579
    }
    return 0;
}

580
581
582
#
# Provision all the slivers in the aggregate.
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
583
sub Provision($;$)
584
{
Leigh B. Stoller's avatar
Leigh B. Stoller committed
585
    my ($self, $extraargs) = @_;
586
587
588
589
590
591
592
593
594
595

    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
596
	if ($sliver->Provision($extraargs) != 0) {
597
598
599
600
601
602
603
	    print STDERR "Could not provision $sliver in $self\n";
	    next;
	}
    }
    return 0;
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
#
# Unprovision all the slivers in the aggregate.
#
sub UnProvision($)
{
    my ($self) = @_;

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

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

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

Leigh B. Stoller's avatar
Leigh B. Stoller committed
627
    foreach my $sliver (@slivers) {
628
629
630
	if (ref($sliver) eq "GeniAggregate::Link" ||
	    ref($sliver) eq "GeniAggregate::Tunnel") {
	    push(@links, $sliver);
631
	}
632
	elsif (ref($sliver) eq "GeniAggregate") {
633
634
	    print STDERR "Unprovision: Unknown aggregate $sliver in $self\n";
	    return -1;
635
636
637
638
639
640
	}
	elsif (ref($sliver) eq "GeniSliver::Node") {
	    push(@nodes, $sliver);
	}
    }
    foreach my $sliver (@links) {
641
642
643
644
645
646
	if ($sliver->UnProvision() != 0) {
	    print STDERR "Could not unprovision $sliver in $self\n";
	    $sliver->SetStatus("broken");
	    next;
	}
    }
647
    foreach my $sliver (@nodes) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
648
649
	if ($sliver->UnProvision() != 0) {
	    print STDERR "Could not unprovision $sliver in $self\n";
650
	    $sliver->SetStatus("broken");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
651
652
653
654
655
656
	    next;
	}
    }
    return 0;
}

657
############################################################################
Leigh B. Stoller's avatar
Leigh B. Stoller committed
658
#
659
# Link aggregates need special handling.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
660
#
661
662
663
664
665
666
667
668
669
670
671
672
package GeniAggregate::Link;
use vars qw(@ISA);
@ISA = "GeniAggregate";

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

673
sub Create($$$)
674
{
675
676
677
678
679
    my ($class, $slice, $owner, $linkname) = @_;

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

682
    return GeniAggregate->Create($slice, $owner, "Link", $hrn, $linkname);
683
684
685
}

#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
686
687
# Provision all the slivers in the aggregate. For links, this is done
# for the entire aggregate (experiment) at once.
688
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
689
sub Provision($;$)
Leigh B. Stoller's avatar
Leigh B. Stoller committed
690
{
Leigh B. Stoller's avatar
Leigh B. Stoller committed
691
    my ($self, $extraargs) = @_;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
692
693
694
695

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

Leigh B. Stoller's avatar
Leigh B. Stoller committed
696
    $self->SetStatus("ready");
697
698
699
    return 0;

  bad:
Leigh B. Stoller's avatar
Leigh B. Stoller committed
700
    return -1
701
}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
702

703
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
704
705
# Unprovision all the slivers in the aggregate. For links, this is done
# for the entire aggregate (experiment) at once.
706
707
708
709
710
711
712
713
#
sub UnProvision($)
{
    my ($self) = @_;

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

Leigh B. Stoller's avatar
Leigh B. Stoller committed
714
715
716
    return 0;
}

717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
#
# Nothing to do yet.
#
sub Start($)
{
    my ($self) = @_;

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

    return 0;
}

#
# Nothing to do yet.
#
sub Stop($)
{
    my ($self) = @_;

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

    return 0;
}


Leigh B. Stoller's avatar
Leigh B. Stoller committed
744
745
746
747
748
749
750
751
752
753
754
755
756
757
############################################################################
#
# 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;
758
use GeniUtil;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
759
760
761
762
763
764
use Experiment;
use Interface;
use Data::Dumper;

sub Create($$$$$$)
{
765
766
    my ($class, $slice, $owner, $node1sliver, $node2sliver,
	$linkrspec, $node1rspec, $node2rspec) = @_;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
767
768
    my $clearinghouse;

769
    my $linkname = $linkrspec->{"nickname"} || $linkrspec->{"virtual_id"};
Leigh B. Stoller's avatar
Leigh B. Stoller committed
770
771
    return undef
	if (!defined($linkname));
772
773
    my @interfaces = @{ $linkrspec->{'interface_ref'} };
    
Leigh B. Stoller's avatar
Leigh B. Stoller committed
774
775
776
777
778
779
780
781
782
    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
    #
783
    my $hrn = "${PGENIDOMAIN}." . $slice->slicename() . "." . $linkname;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798

    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;
    }

799
800
    my $iface1ref = $interfaces[0];
    my $iface2ref = $interfaces[1];
Leigh B. Stoller's avatar
Leigh B. Stoller committed
801
802

    # These are the ips of the tunnel.
803
804
    my $ip1      = $iface1ref->{'tunnel_ip'};
    my $ip2      = $iface2ref->{'tunnel_ip'};
Leigh B. Stoller's avatar
Leigh B. Stoller committed
805
806
807
808
809
810
811
812
    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)) {
813
814
815
816
817
	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
818
819
820
821
822
823
824
825
826
827
828
	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 {
829
	my $component = GeniComponent->Lookup($node1rspec->{'component_uuid'});
Leigh B. Stoller's avatar
Leigh B. Stoller committed
830
	my $blob;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
831
	
Leigh B. Stoller's avatar
Leigh B. Stoller committed
832
	if (!defined($component)) {
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
	    #
	    # Need to ask the clearinghouse where this node comes from.
	    #
	    $clearinghouse = GeniRegistry::ClearingHouse->Create();
	
	    if ($clearinghouse->Resolve($node1rspec->{'component_uuid'},
					"Component", \$blob) != 0) {
		print STDERR "Could not lookup node at clearinghouse\n";
		print STDERR Dumper($node1rspec);
		goto bad;
	    }
	    my $certificate = GeniCertificate->LoadFromString($blob->{'gid'});
	    goto bad
		if (!defined($certificate));
	
Leigh B. Stoller's avatar
Leigh B. Stoller committed
848
	    $component = GeniComponent->CreateFromCertificate($certificate);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
	    if (!defined($component)) {
		print STDERR "Could not create component from $certificate\n";
		goto bad;
	    }
	}
	my $credential = GeniRegistry::Client->CreateCredential($component);
	if (!defined($credential)) {
	    print STDERR "Could not create a credential for $component\n";
	    goto bad;

	}
	my $registry = GeniRegistry::Client->Create($component, undef,
						    $credential);
	if (!defined($registry)) {
	    print STDERR "Could not create a registry client for $component\n";
	    goto bad;
	}
866
	$registry->Resolve($node1rspec->{'component_uuid'}, "Node", \$blob);
867
868
	$ctrlip1 = $blob->{'physctrl'}
	    if (defined($blob) && exists($blob->{'physctrl'}));
Leigh B. Stoller's avatar
Leigh B. Stoller committed
869

870
	if (!defined($ctrlip1)) {
871
	    print STDERR "Could not get control IP for ".
872
		"$node1rspec->{'component_uuid'} at $component\n";
873
874
	    goto bad;
	}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
875
876
    }
    if (defined($node2sliver)) {
877
878
879
880
881
	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
882
883
884
885
886
887
888
889
890
891
892
893
	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 {
	my $blob;
894
	my $component = GeniComponent->Lookup($node2rspec->{'component_uuid'});
Leigh B. Stoller's avatar
Leigh B. Stoller committed
895
	if (!defined($component)) {
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
	    #
	    # Need to ask the clearinghouse where this node comes from.
	    #
	    $clearinghouse = GeniRegistry::ClearingHouse->Create();
	
	    if ($clearinghouse->Resolve($node2rspec->{'component_uuid'},
					"Component", \$blob) != 0) {
		print STDERR "Could not lookup node at clearinghouse\n";
		print STDERR Dumper($node2rspec);
		goto bad;
	    }
	    my $certificate = GeniCertificate->LoadFromString($blob->{'gid'});
	    goto bad
		if (!defined($certificate));
	
Leigh B. Stoller's avatar
Leigh B. Stoller committed
911
	    $component = GeniComponent->CreateFromCertificate($certificate);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
	    if (!defined($component)) {
		print STDERR "Could not create component from $certificate\n";
		goto bad;
	    }
	}
	my $credential = GeniRegistry::Client->CreateCredential($component);
	if (!defined($credential)) {
	    print STDERR "Could not create a credential for $component\n";
	    goto bad;

	}
	my $registry = GeniRegistry::Client->Create($component, undef,
						    $credential);
	if (!defined($registry)) {
	    print STDERR "Could not create a registry client for $component\n";
	    goto bad;
	}
929
	$registry->Resolve($node2rspec->{'component_uuid'}, "Node", \$blob);
930
931
	$ctrlip2 = $blob->{'physctrl'}
	    if (defined($blob) && exists($blob->{'physctrl'}));
Leigh B. Stoller's avatar
Leigh B. Stoller committed
932

933
	if (!defined($ctrlip2)) {
934
	    print STDERR "Could not get control IP for ".
935
		"$node2rspec->{'component_uuid'} at $component\n";
936
937
	    goto bad;
	}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
938
    }
939
    # print STDERR "$ip1, $ip2, $ctrlip1, $ctrlip2\n";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965

    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);
	$iface1->SetAttribute("tunnel_unit", $iface1->memberid());
	$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);
	$iface2->SetAttribute("tunnel_unit", $iface2->memberid());
	$iface2->SetAttribute("tunnel_style", "gre");
    }
    return $aggregate;

  bad:
    $tunnel->Destroy()
	if (defined($tunnel));
966
    $aggregate->Delete(GENI_PURGEFLAG)
Leigh B. Stoller's avatar
Leigh B. Stoller committed
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
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
	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.
#
sub Start($)
{
    my ($self) = @_;

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

    return 0;
}

#
# Nothing to do yet.
#
sub Stop($)
{
    my ($self) = @_;

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

    return 0;
}


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