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

#
# Some simple ticket stuff.
#
use strict;
use Exporter;
use vars qw(@ISA @EXPORT);

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

# Must come after package declaration!
use lib '@prefix@/lib';
use GeniDB;
use GeniCredential;
use GeniCertificate;
use GeniSliver;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
25
use GeniSlice;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
use libdb qw(TBGetUniqueIndex);
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@";
my $SIGNCRED	   = "$TB/sbin/signgenicred";
my $VERIFYCRED	   = "$TB/sbin/verifygenicred";

# Cache of instances to avoid regenerating them.
my %aggregates     = ();

#
# 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
81
    $self->{'SLICE'}      = undef;
82
    $self->{'PARENT'}     = undef;
83
84
85
86
87
88
89
90
91

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

    #
    # 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.
#
128
sub Create($$;$)
Leigh B. Stoller's avatar
Leigh B. Stoller committed
129
{
130
    my ($class, $ticket, $aggregate_type) = @_;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
    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.
    my $certificate = GeniCertificate->Create("aggregate");
    if (!defined($certificate)) {
	print STDERR "Could not generate new certificate and UUID!\n";
	return undef;
    }
    my $uuid        = $certificate->uuid();
    my $slice_uuid  = $ticket->slice_uuid();
    my $owner_uuid  = $ticket->owner_uuid();

    # Now tack on other stuff we need.
    push(@insert_data, "created=now()");
    push(@insert_data, "idx='$idx'");
    push(@insert_data, "uuid='$uuid'");
    push(@insert_data, "creator_uuid='$owner_uuid'");
    push(@insert_data, "slice_uuid='$slice_uuid'");
152
153
    push(@insert_data, "type='$aggregate_type'")
	if (defined($aggregate_type));
Leigh B. Stoller's avatar
Leigh B. Stoller committed
154
155
156
157
158
159
160
161
162
163
164
165
166

    # Insert into DB.
    if (!DBQueryWarn("insert into geni_aggregates set " .
		     join(",", @insert_data))) {
	$certificate->Delete();
	return undef;
    }
    return GeniAggregate->Lookup($idx);
}
# accessors
sub field($$) { return ((! ref($_[0])) ? -1 : $_[0]->{'AGGREGATE'}->{$_[1]}); }
sub idx($)		{ return field($_[0], "idx"); }
sub uuid($)		{ return field($_[0], "uuid"); }
167
sub type($)		{ return field($_[0], "type"); }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
168
169
170
171
172
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"); }
sub ticket_idx($)	{ return field($_[0], "ticket_idx"); }
173
sub aggregate_idx($)	{ return field($_[0], "aggregate_idx"); }
174
sub status($)		{ return field($_[0], "status"); }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
175
176
177
sub cert($)		{ return $_[0]->{'CERTIFICATE'}->cert(); }
sub GetCertificate($)   { return $_[0]->{'CERTIFICATE'}; }

178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#
# Destroy all the slivers in the aggregate, and then the aggregate if there
# is nothing in it. Leave it around if something goes wrong.
#
sub Delete($)
{
    my ($self) = @_;
    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") {
	    $broken++;
	    next;
	}
	if ($sliver->Delete() != 0) {
	    print STDERR "Could not delete $sliver from $self\n";
	    $sliver->SetStatus("broken");
	    $broken++;
	    next;
	}
    }
    return -1
	if ($broken);
    
    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;
    
    return 0;
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#
# List of slivers for this aggregate.
#
sub SliverList($$)
{
    my ($self, $pref) = @_;
    my @result = ();
    
    return -1
	if (! (ref($self) && ref($pref)));

    my $idx = $self->idx();
    my $query_result =
	DBQueryWarn("select idx from geni_slivers where aggregate_idx='$idx'");
    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);
    }
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266

    #
    # 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
267
268
269
270
271
    @$pref = @result;
    return 0;
    
}

272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#
# Set the aggregate for an aggregate.
#
sub SetAggregate($$)
{
    my ($self, $aggregate) = @_;

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

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

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

#
296
# Get the aggregate for an aggregate.
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#
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;
}

318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#
# 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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
#
# Get the credential for the aggregate.
#
sub GetCredential($)
{
    my ($self) = @_;

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

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

    if (!defined($self->credential_idx())) {
	print STDERR "No credential associated with $self\n";
	return undef;
    }
    my $credential = GeniCredential->Lookup($self->credential_idx());
    if (!defined($credential)) {
	print STDERR "Could not get credential object associated with $self\n";
	return undef;
    }
    $self->{'CREDENTIAL'} = $credential;
    return $credential;
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
#
# 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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
#
# 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;
    }
    if ($credential->Sign($self->GetCertificate()) != 0) {
	print STDERR "Could not sign credential for $self, $owner\n";
	return undef;
    }
    return $credential;
}

#
# Start all the slivers in the aggregate.
#
417
sub Start($)
Leigh B. Stoller's avatar
Leigh B. Stoller committed
418
419
420
421
422
423
424
425
426
427
428
429
{
    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;
    }
    foreach my $sliver (@slivers) {
430
	if ($sliver->Start() != 0) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
431
432
433
434
435
436
437
	    print STDERR "Could not start $sliver in $self\n";
	    next;
	}
    }
    return 0;
}

438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
#
# Provision all the slivers in the aggregate.
#
sub Provision($)
{
    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;
    }
    foreach my $sliver (@slivers) {
	if ($sliver->Provision() != 0) {
	    print STDERR "Could not provision $sliver in $self\n";
	    next;
	}
    }
    return 0;
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
#
# 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;
    }
    foreach my $sliver (@slivers) {
	if ($sliver->UnProvision() != 0) {
	    print STDERR "Could not unprovision $sliver in $self\n";
480
	    $sliver->SetStatus("broken");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
481
482
483
484
485
486
	    next;
	}
    }
    return 0;
}

487
############################################################################
Leigh B. Stoller's avatar
Leigh B. Stoller committed
488
#
489
# Link aggregates need special handling.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
490
#
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
package GeniAggregate::Link;
use vars qw(@ISA);
@ISA = "GeniAggregate";

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

sub Create($$)
{
    my ($class, $ticket) = @_;

    return GeniAggregate->Create($ticket, "Link");
}

#
# Provision all the slivers in the aggregate.
#
sub Provision($)
Leigh B. Stoller's avatar
Leigh B. Stoller committed
514
515
516
517
518
519
520
521
522
523
524
{
    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;
    }
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559

    my $experiment = Experiment->Lookup($self->slice_uuid());
    if (!defined($experiment)) {
	print STDERR "Could not map $self to its experiment\n";
	return -1;
    }
    my $sliver0    = $slivers[0];
    my $sliver1    = $slivers[1];
    my $interface0 = Interface->LookupByUUID($sliver0->resource_uuid());
    my $interface1 = Interface->LookupByUUID($sliver1->resource_uuid());
    if (! defined($interface0)) {
	print STDERR "Could not map $sliver0 to its object\n";
	return -1;
    }
    if (! defined($interface1)) {
	print STDERR "Could not map $sliver1 to its object\n";
	return -1;
    }

    my $vlan = VLan->Create($experiment, $self->uuid());
    if (!defined($vlan)) {
	print STDERR "Could not create vlan for $self\n";
	return -1;
    }
    if (! $vlan->AddMember($interface0->node_id(), $interface0->iface())) {
	print STDERR "$self: Could not add $interface0 to $vlan\n";
	goto bad;
    }
    if (! $vlan->AddMember($interface1->node_id(), $interface1->iface())) {
	print STDERR "$self: Could not add $interface1 to $vlan\n";
	goto bad;
    }
    if ($vlan->Instantiate() != 0) {
	print STDERR "$self: Could not instantiate $vlan on switches\n";
	goto bad;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
560
    }
561
562
563
564
565
566
567
    return 0;

  bad:
    $vlan->UnInstantiate()
	if (defined($vlan));
    $vlan->Destroy()
	if (defined($vlan));
Leigh B. Stoller's avatar
Leigh B. Stoller committed
568
    return -1
569
}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
570

571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
#
# 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;
    }
    my $experiment = Experiment->Lookup($self->slice_uuid());
    if (!defined($experiment)) {
	print STDERR "Could not map $self to its experiment\n";
	return -1;
    }
    my $sliver0    = $slivers[0];
    my $sliver1    = $slivers[1];
    my $interface0 = Interface->LookupByUUID($sliver0->resource_uuid());
    my $interface1 = Interface->LookupByUUID($sliver1->resource_uuid());
    if (! defined($interface0)) {
	print STDERR "Could not map $sliver0 to its object\n";
	return -1;
    }
    if (! defined($interface1)) {
	print STDERR "Could not map $sliver1 to its object\n";
	return -1;
    }
    my $vlan = VLan->Lookup($experiment, $self->uuid());
    if (! defined($vlan)) {
	print STDERR "Could not map self to its vlan object\n";
	return -1;
    }
    if ($vlan->UnInstantiate() != 0) {
	print STDERR "Could not uninstantiate $vlan\n";
	return -1;
    }
    if ($vlan->Destroy() != 0) {
	print STDERR "Could not destroy $vlan\n";
	return -1;
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
616
617
618
    return 0;
}

619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
#
# 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
646
647
648
# _Always_ make sure that this 1 is at the end of the file...
1;