GeniCM.pm.in 32.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
#!/usr/bin/perl -wT
#
# EMULAB-COPYRIGHT
# Copyright (c) 2008 University of Utah and the Flux Group.
# All rights reserved.
#
package GeniCM;

#
# The server side of the CM interface on remote sites. Also communicates
# with the GMC interface at Geni Central as a client.
#
use strict;
use Exporter;
use vars qw(@ISA @EXPORT);

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

# Must come after package declaration!
use lib '@prefix@/lib';
use GeniDB;
use Genixmlrpc;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
24
25
use GeniResponse;
use GeniTicket;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
26
use GeniCredential;
27
use GeniCertificate;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
28
use GeniSlice;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
29
use GeniAggregate;
30
use GeniAuthority;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
31
use GeniSliver;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
32
use GeniUser;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
33
use libtestbed;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
34
35
36
# Hate to import all this crap; need a utility library.
use libdb qw(TBGetUniqueIndex TBcheck_dbslot TBDB_CHECKDBSLOT_ERROR);
use User;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
37
use Node;
38
use Interface;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
39
40
use English;
use Data::Dumper;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
41
use XML::Simple;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
42
use Experiment;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
43
44
45
46
47
48
49
50

# Configure variables
my $TB		   = "@prefix@";
my $TBOPS          = "@TBOPSEMAIL@";
my $TBAPPROVAL     = "@TBAPPROVALEMAIL@";
my $TBAUDIT   	   = "@TBAUDITEMAIL@";
my $BOSSNODE       = "@BOSSNODE@";
my $OURDOMAIN      = "@OURDOMAIN@";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
51
my $CREATEEXPT     = "$TB/bin/batchexp";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
52
my $ENDEXPT        = "$TB/bin/endexp";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
53
my $NALLOC	   = "$TB/bin/nalloc";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
54
my $AVAIL	   = "$TB/sbin/avail";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
55
56
my $TBSWAP	   = "$TB/bin/tbswap";
my $SWAPEXP	   = "$TB/bin/swapexp";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
57

Leigh B. Stoller's avatar
Leigh B. Stoller committed
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
86
87
88
89
90
91
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

#
# Respond to a GetTicket request. 
#
sub Resolve($)
{
    my ($argref) = @_;
    my $uuid       = $argref->{'uuid'};
    my $hrn        = $argref->{'hrn'};
    my $cred       = $argref->{'credential'};
    my $type       = $argref->{'type'};

    if (! defined($cred)) {
	return GeniResponse->MalformedArgsResponse();
    }
    if (! (defined($type) && ($type =~ /^(Node)$/))) {
	return GeniResponse->MalformedArgsResponse();
    }
    # Allow lookup by uuid or hrn.
    if (! (defined($uuid) || defined($hrn))) {
	return GeniResponse->MalformedArgsResponse();
    }
    if (defined($uuid) && !($uuid =~ /^[-\w]*$/)) {
	return GeniResponse->MalformedArgsResponse();
    }
    if (defined($hrn) && !($hrn =~ /^[-\w\.]*$/)) {
	return GeniResponse->MalformedArgsResponse();
    }

    my $credential = GeniCredential->CreateFromSigned($cred);
    if (!defined($credential)) {
	return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
				    "Could not create GeniCredential object");
    }
    
    #
    # Make sure the credential was issued to the caller, but no special
    # permission required to resolve component resources.
    #
    if ($credential->owner_uuid() ne $ENV{'GENIUUID'}) {
	return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
				    "This is not your credential!");
    }
    if ($type eq "Node") {
	my $node;
	
	if (defined($uuid)) {
	    $node= Node->Lookup($uuid);
	}
	else {
	    #
	    # We only want the last token for node lookup.
	    #
	    if ($hrn =~ /\./) {
		($hrn) = ($hrn =~ /\.(\w*)$/);
	    }
	    $node= Node->Lookup($hrn);
	}
	if (!defined($node)) {
	    return GeniResponse->Create(GENIRESPONSE_SEARCHFAILED,
					undef, "Nothing here by that name");
	}
	
	# Return a blob.
	my $blob = { "hrn"          => "${OURDOMAIN}." . $node->node_id(),
		     "uuid"         => $node->uuid(),
124
		     "role"	    => $node->role(),
Leigh B. Stoller's avatar
Leigh B. Stoller committed
125
126
127
128
129
130
131
132
133
134
135
136
137
		   };

	#
	# Get the list of interfaces for the node.
	#
	my @interfaces;
	if ($node->AllInterfaces(\@interfaces) != 0) {
	    return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
					"Could not get interfaces for $uuid");
	}

	my @iblobs = ();
	foreach my $interface (@interfaces) {
138
139
140
	    next
		if (!defined($interface->switch_id()));
		
Leigh B. Stoller's avatar
Leigh B. Stoller committed
141
142
143
144
145
146
	    my $iblob = { "uuid"	=> $interface->uuid(),
			  "iface"	=> $interface->iface(),
			  "type"	=> $interface->type(),
			  "card"	=> $interface->card(),
			  "port"	=> $interface->port(),
			  "role"	=> $interface->role(),
147
148
			  "IP"		=> $interface->IP() || "",
			  "mask"	=> $interface->mask() || "",
Leigh B. Stoller's avatar
Leigh B. Stoller committed
149
			  "MAC"		=> $interface->mac(),
150
151
152
153
154
			  "switch_id"   => "${OURDOMAIN}." . 
			      $interface->switch_id(),
			  "switch_card"	=> $interface->switch_card(),
			  "switch_port"	=> $interface->switch_port(),
			  "wire_type"	=> $interface->wire_type(),
Leigh B. Stoller's avatar
Leigh B. Stoller committed
155
156
157
158
159
160
161
162
163
164
165
166
		      };

	    push(@iblobs, $iblob);
	}
	$blob->{'interfaces'} = \@iblobs
	    if (@iblobs);
	
	return GeniResponse->Create(GENIRESPONSE_SUCCESS, $blob);
    }
    return GeniResponse->Create(GENIRESPONSE_UNSUPPORTED);
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
167
168
169
170
171
172
#
# Discover resources on this component, returning a resource availablity spec
#
sub DiscoverResources($)
{
    my ($argref) = @_;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
173
    my $slice      = $argref->{'slice'};
Leigh B. Stoller's avatar
Leigh B. Stoller committed
174
175
    my $credential = $argref->{'credential'};
    my $user_uuid  = $ENV{'GENIUSER'};
Leigh B. Stoller's avatar
Leigh B. Stoller committed
176
    my $slice_uuid;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
177

Leigh B. Stoller's avatar
Leigh B. Stoller committed
178
    if (! defined($slice)) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
179
180
181
182
183
184
185
186
	return GeniResponse->MalformedArgsResponse();
    }

    $credential = GeniCredential->CreateFromSigned($credential);
    if (!defined($credential)) {
	return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
				    "Could not create GeniCredential object");
    }
187
    GeniCertificate->CertificateInfo($slice, \$slice_uuid) == 0 or
Leigh B. Stoller's avatar
Leigh B. Stoller committed
188
189
190
	return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
				    "Could not get uuid from Certificate");
	
Leigh B. Stoller's avatar
Leigh B. Stoller committed
191
192
193
194
195
196
197
198
199
200
201
    # The credential owner/slice has to match what was provided.
    if (! ($user_uuid eq $credential->owner_uuid() &&
	   $slice_uuid eq $credential->this_uuid())) {
	return GeniResponse->Create(GENIRESPONSE_FORBIDDEN, undef,
				    "Invalid credentials for operation");
    }

    #
    # Eventually we will take an optional rspec, but for now just return
    # a list of free nodes using avail. 
    #
202
203
    my @nodelist = ();
    if (! open(AVAIL, "$AVAIL type=pc  aslist |")) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
204
205
206
207
208
209
210
211
212
213
214
215
	return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
				    "Could not start avail");
    }
    while (<AVAIL>) {
	my $nodeid = $_;
	chomp($nodeid);
	my $node = Node->Lookup($nodeid);
	push(@nodelist, $node)
	    if (defined($node));
    }
    close(AVAIL);

Leigh B. Stoller's avatar
Leigh B. Stoller committed
216
    my $xml = "<rspec xmlns=\"http://protogeni.net/resources/rspec/0.1\">\n";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
217
218
219
220
221
222
    foreach my $node (@nodelist) {
	my $uuid = $node->uuid();
	my $nodeid = $node->node_id();
	
	$xml .= "<node uuid=\"$uuid\" name=\"$nodeid\">".
	    "<available>true</available></node>\n";
223

Leigh B. Stoller's avatar
Leigh B. Stoller committed
224
225
226
227
228
	my @interfaces;
	if ($node->AllInterfaces(\@interfaces) != 0) {
	    return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
					"Could not get interfaces for $uuid");
	}
229
230
231
232
233
234
235
236
237
238
	foreach my $interface (@interfaces) {
	    my $iface_uuid = $interface->uuid();
	    my $iface      = $interface->iface();

	    next 
		if (! $interface->IsExperimental());
	    
	    $xml .= "<interface uuid=\"$iface_uuid\" node_name=\"$nodeid\">".
		"<iface>$iface</iface></interface>\n";
	}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
239
240
241
242
243
    }
    $xml .= "</rspec>";

    return GeniResponse->Create(GENIRESPONSE_SUCCESS, $xml);
}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
244

Leigh B. Stoller's avatar
Leigh B. Stoller committed
245
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
246
# Respond to a GetTicket request. 
Leigh B. Stoller's avatar
Leigh B. Stoller committed
247
248
249
250
251
#
sub GetTicket($)
{
    my ($argref) = @_;
    my $rspec      = $argref->{'rspec'};
Leigh B. Stoller's avatar
Leigh B. Stoller committed
252
    my $impotent   = $argref->{'impotent'};
253
    my $cred       = $argref->{'credential'};
254
    my $vtopo      = $argref->{'virtual_topology'};
Leigh B. Stoller's avatar
Leigh B. Stoller committed
255
    my $owner_uuid = $ENV{'GENIUSER'};
Leigh B. Stoller's avatar
Leigh B. Stoller committed
256

257
258
    if (! defined($cred)) {
	return GeniResponse->MalformedArgsResponse();
Leigh B. Stoller's avatar
Leigh B. Stoller committed
259
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
260
261
262
    if (! (defined($rspec) && ($rspec =~ /^[-\w]+$/))) {
	GeniResponse->Create(GENIRESPONSE_BADARGS, undef,
			     "Improper rspec");	
Leigh B. Stoller's avatar
Leigh B. Stoller committed
263
    }
264
    $rspec = XMLin($rspec, ForceArray => ["node", "link"]);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
265

Leigh B. Stoller's avatar
Leigh B. Stoller committed
266
267
268
    $impotent = 0
	if (!defined($impotent));

269
    my $credential = GeniCredential->CreateFromSigned($cred);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
270
271
272
273
    if (!defined($credential)) {
	return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
				    "Could not create GeniCredential object");
    }
274
275
276
277
278
279
280
    my $slice_uuid = $credential->this_uuid();
    my $user_uuid  = $credential->owner_uuid();
    
    #
    # Make sure the credential was issued to the caller.
    #
    if ($credential->owner_uuid() ne $ENV{'GENIUUID'}) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
281
	return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
282
				    "This is not your credential!");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
283
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
284

Leigh B. Stoller's avatar
Leigh B. Stoller committed
285
286
287
288
289
290
291
    #
    # See if we have a record of this slice in the DB. If not, then we have
    # to go to the ClearingHouse to find its record, so that we can find out
    # who the SA for it is.
    #
    my $slice = GeniSlice->Lookup($slice_uuid);
    if (!defined($slice)) {
292
	$slice = CreateSliceFromRegistry($slice_uuid);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
293
294
295
296
297
	if (!defined($slice)) {
	    print STDERR "No slice $slice_uuid in the ClearingHouse\n";
	    return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
			    "Could not get slice info from ClearingHouse");
	}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
298
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
299
    else {
300
	UpdateSliceFromRegistry($slice) == 0 or
Leigh B. Stoller's avatar
Leigh B. Stoller committed
301
302
303
	    return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
			"Could not update slice info from ClearingHouse");
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
304

Leigh B. Stoller's avatar
Leigh B. Stoller committed
305
    #
Leigh B. Stoller's avatar
Leigh B. Stoller committed
306
307
    # Ditto the user.
    #
308
    my $user = GeniUser->Lookup($user_uuid);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
309
    if (!defined($user)) {
310
	$user = CreateUserFromRegistry($user_uuid);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
311
	if (!defined($user)) {
312
	    print STDERR "No user $user_uuid in the ClearingHouse\n";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
313
314
315
316
317
	    return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
			    "Could not get user info from ClearingHouse");
	}
    }

Leigh B. Stoller's avatar
Leigh B. Stoller committed
318
    #
Leigh B. Stoller's avatar
Leigh B. Stoller committed
319
320
321
322
    # If the underlying experiment does not exist, need to create
    # a holding experiment. All these are going to go into the same
    # project for now. Generally, users for non-local slices do not
    # have local accounts or directories.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
323
    #
Leigh B. Stoller's avatar
Leigh B. Stoller committed
324
325
326
327
328
329
330
331
332
    my $experiment = Experiment->Lookup($slice_uuid);
    if (!defined($experiment)) {
	#
	# Form an eid for the experiment. 
	#
	my $eid = "slice" . TBGetUniqueIndex('next_sliceid', 1);

	# Note the -h option; allows experiment with no NS file.
	system("$CREATEEXPT -q -i -w -E 'Geni Slice Experiment' ".
Leigh B. Stoller's avatar
Leigh B. Stoller committed
333
	       "-h '$slice_uuid' -p GeniSlices -e $eid");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
334
335
336
337
338
339
340
	if ($?) {
	    return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
					"Internal Error");
	}
	$experiment = Experiment->Lookup($slice_uuid);
    }

Leigh B. Stoller's avatar
Leigh B. Stoller committed
341
    #
Leigh B. Stoller's avatar
Leigh B. Stoller committed
342
343
    # An rspec is a structure that requests specific nodes. If those
    # nodes are available, then reserve it. Otherwise the ticket
Leigh B. Stoller's avatar
Leigh B. Stoller committed
344
345
346
347
    # cannot be granted.
    #
    # XXX Simpleminded ... assumes only physical nodes for now. Need to deal
    # with virtual nodes (vservers on shared nodes, planetlab nodes, etc).
Leigh B. Stoller's avatar
Leigh B. Stoller committed
348
    #
Leigh B. Stoller's avatar
Leigh B. Stoller committed
349
    my @nodeids = ();
Leigh B. Stoller's avatar
Leigh B. Stoller committed
350
351
352
    my $pid     = $experiment->pid();
    my $eid     = $experiment->eid();

353
354
    foreach my $ref (@{$rspec->{'node'}}) {
	my $resource_uuid = $ref->{'uuid'};
355
356
	my $node = Node->Lookup($resource_uuid);
	if (!defined($node)) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
357
	    return GeniResponse->Create(GENIRESPONSE_BADARGS, undef,
358
					"Bad resource_uuid $resource_uuid");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
359
	}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
360
361
362
363
364
365
366
367
368

	#
	# See if the node is already reserved.
	#
	my $reservation = $node->Reservation();
	if (defined($reservation)) {
	    return GeniResponse->Create(GENIRESPONSE_UNAVAILABLE, undef,
				"$resource_uuid ($node) is not available");
	}
369
	push(@nodeids, $node->node_id());
Leigh B. Stoller's avatar
Leigh B. Stoller committed
370
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
371
372

    #
Leigh B. Stoller's avatar
Leigh B. Stoller committed
373
    # Create the ticket first, before allocating the node.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
374
    #
Leigh B. Stoller's avatar
Leigh B. Stoller committed
375
    my $ticket = GeniTicket->Create($slice, $user, $rspec);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
376
377
378
379
    if (!defined($ticket)) {
	return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
				    "Could not create GeniTicket object");
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
380
    # Nalloc might fail if the node gets picked up by someone else.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
381
    if (!$impotent) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
382
	system("$NALLOC $pid $eid @nodeids");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
383
384
385
386
387
388
389
390
391
392
	if (($? >> 8) < 0) {
	    $ticket->Delete();
	    return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
					"Allocation failure");
	}
	elsif (($? >> 8) > 0) {
	    $ticket->Delete();
	    return GeniResponse->Create(GENIRESPONSE_UNAVAILABLE, undef,
					"Could not allocate node\n");
	}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
393
    }
394
395
396
397
398
399
400
    if (defined($vtopo) && $experiment->InsertVirtTopo($vtopo) != 0) {
	# Release will free the nodes.
	$ticket->Release();
	return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
				    "Could not insert virt topology");
    }
    
Leigh B. Stoller's avatar
Leigh B. Stoller committed
401
    if ($ticket->Sign() != 0) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
402
	# Release will free the nodes.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
403
	$ticket->Release();
Leigh B. Stoller's avatar
Leigh B. Stoller committed
404
405
406
	return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
				    "Could not sign Ticket");
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
407
408
409
410
411
412
413
    return GeniResponse->Create(GENIRESPONSE_SUCCESS,
				$ticket->asString());
}

#
# Create a sliver.
#
414
sub RedeemTicket($)
Leigh B. Stoller's avatar
Leigh B. Stoller committed
415
416
{
    my ($argref) = @_;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
417
    my $ticket     = $argref->{'ticket'};
418
    my $impotent   = $argref->{'impotent'};
Leigh B. Stoller's avatar
Leigh B. Stoller committed
419
    my $extraargs  = $argref->{'extraargs'};
Leigh B. Stoller's avatar
Leigh B. Stoller committed
420
    my $message    = "Error creating sliver/aggregate";
421
422
423

    $impotent = 0
	if (!defined($impotent));
Leigh B. Stoller's avatar
Leigh B. Stoller committed
424
425
426
427
428
429
430
431
432
433
434
435

    if (! (defined($ticket) &&
	   !TBcheck_dbslot($ticket, "default", "text",
			   TBDB_CHECKDBSLOT_ERROR))) {
	return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
				    "ticket: ". TBFieldErrorString());
    }
    $ticket = GeniTicket->CreateFromSignedTicket($ticket);
    if (!defined($ticket)) {
	return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
				    "Could not create GeniTicket object");
    }
436
437
438
439
440
441
442
    
    #
    # Make sure the credential was issued to the caller.
    #
    if ($ticket->owner_uuid() ne $ENV{'GENIUUID'}) {
	return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
				    "This is not your credential!");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
443
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
444

Leigh B. Stoller's avatar
Leigh B. Stoller committed
445
446
447
448
449
    my $experiment = Experiment->Lookup($ticket->slice_uuid());
    if (!defined($experiment)) {
	return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
				    "No local experiment for slice");
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
450
451
    my $pid = $experiment->pid();
    my $eid = $experiment->eid();
Leigh B. Stoller's avatar
Leigh B. Stoller committed
452

453
454
455
456
457
458
459
460
461
462
463
464
465
    #
    # See if we have a record of this slice in the DB. If not, throw an
    # error; might change later.
    #
    my $slice = GeniSlice->Lookup($ticket->slice_uuid());
    if (!defined($slice)) {
	return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
				    "No slice record for slice");
    }

    #
    # Ditto the user.
    #
466
    my $owner_uuid = $ticket->owner_uuid();
467
468
    my $owner = GeniUser->Lookup($owner_uuid);
    if (!defined($owner)) {
469
470
471
472
473
474
	$owner = CreateUserFromRegistry($owner_uuid);
	if (!defined($owner)) {
	    print STDERR "No user $owner_uuid in the ClearingHouse\n";
	    return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
					"No user record for $owner_uuid");
	}
475
476
    }

Leigh B. Stoller's avatar
Leigh B. Stoller committed
477
478
479
480
481
482
    #
    # Create an emulab nonlocal user for tmcd.
    #
    $owner->BindToSlice($slice) == 0
	or return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
				       "Error binding user to slice");
483

Leigh B. Stoller's avatar
Leigh B. Stoller committed
484
485
486
487
488
489
490
491
    # Bind the other users too.
    my @userbindings;
    if ($slice->UserBindings(\@userbindings) != 0) {
	return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
				    "Error binding users to slice");
    }
    foreach my $otheruuid (@userbindings) {
	my $otheruser = GeniUser->Lookup($otheruuid);
492
493
494
495
496
497
498
	if (!defined($otheruser)) {
	    $otheruser = CreateUserFromRegistry($otheruuid);
	    if (!defined($otheruser)) {
		print STDERR "No user $otheruser, cannot bind to slice\n";
		next;
	    }
	}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
499
	if ($otheruser->BindToSlice($slice) != 0) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
500
501
502
503
	    print STDERR "Could not bind $otheruser to $slice\n";
	}
    }

Leigh B. Stoller's avatar
Leigh B. Stoller committed
504
    #
Leigh B. Stoller's avatar
Leigh B. Stoller committed
505
    # We are actually an Aggregate, so return an aggregate of slivers,
506
    # unless there is just one node.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
507
    #
508
    my $aggregate;
509
    if (scalar(@{$ticket->rspec()->{'node'}}) > 1) {
510
511
	$aggregate = GeniAggregate->Create($ticket, "Aggregate",
					   $slice->hrn());
512
513
	if (!defined($aggregate)) {
	    return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
Leigh B. Stoller's avatar
Leigh B. Stoller committed
514
				    "Could not create GeniAggregate object");
515
	}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
516
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
517

518
519
    print Dumper($ticket->rspec());

Leigh B. Stoller's avatar
Leigh B. Stoller committed
520
    #
Leigh B. Stoller's avatar
Leigh B. Stoller committed
521
522
    # Now for each resource (okay, node) in the ticket create a sliver and
    # add it to the aggregate.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
523
    #
524
    my %slivers = ();
525
526
    foreach my $ref (@{$ticket->rspec()->{'node'}}) {
	my $resource_uuid = $ref->{'uuid'};
527
528
529
	my $node = Node->Lookup($resource_uuid);
	if (!defined($node)) {
	    $message = "Unknown resource_uuid in ticket: $resource_uuid";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
530
531
	    goto bad;
	}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
532
533
534
535
	my $sliver = GeniSliver::Node->Create($slice,
					      $owner->uuid(),
					      $resource_uuid,
					      $ref);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
536
537
538
539
	if (!defined($sliver)) {
	    $message = "Could not create GeniSliver object for $resource_uuid";
	    goto bad;
	}
540
	$slivers{$resource_uuid} = $sliver;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
541
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
542

543
544
545
546
547
548
549
550
551
552
553
554
    #
    # Now do the links. For each link, we have to add a sliver for the
    # interfaces, and then combine those two interfaces into an aggregate,
    # and then that aggregate goes into the aggregate for toplevel sliver.
    #
    foreach my $linkname (keys(%{$ticket->rspec()->{'link'}})) {
	my @linkslivers  = ();
	if (! ($linkname =~ /^[-\w]*$/)) {
	    $message = "Bad name for link: $linkname";
	    goto bad;
	}

555
	my $linkaggregate = GeniAggregate::Link->Create($ticket, $linkname);
556
557
558
559
560
561
562
563
564
	if (!defined($linkaggregate)) {
	    $message = "Could not create link aggregate for $linkname";
	    goto bad;
	}
	$slivers{$linkaggregate->uuid()} = $linkaggregate;

	my $linkendpoints =
	    $ticket->rspec()->{'link'}->{$linkname}->{'LinkEndPoints'};
	
565
566
567
568
569
	foreach my $ifacename (keys(%{ $linkendpoints })) {
	    my $iface      = $linkendpoints->{$ifacename};
	    my $node_uuid  = $iface->{'node_uuid'};
	    my $iface_name = $iface->{'iface_name'};
	    my $nodesliver = $slivers{$node_uuid};
570
571
572
573
574
575
576
577
578
	    if (!defined($nodesliver)) {
		$message = "Link $linkname specifies a non-existent node";
		goto bad;
	    }
	    my $nodeobject= Node->Lookup($node_uuid);
	    if (!defined($nodeobject)) {
		$message = "Could not find node object for $node_uuid";
		goto bad;
	    }
579
	    my $interface = Interface->LookupByIface($nodeobject, $iface_name);
580
	    if (!defined($interface)) {
581
		$message = "No such interface $iface_name on node $nodeobject";
582
583
		goto bad;
	    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
584
585
586
	    my $sliver = GeniSliver::Interface->Create($slice,
						       $owner->uuid(),
						       $interface->uuid(),
587
						       $node_uuid,
Leigh B. Stoller's avatar
Leigh B. Stoller committed
588
			       $ticket->rspec()->{'link'}->{$linkname});
589
590
591
592
593
594
595
596
597
598
599
600
	    if (!defined($sliver)) {
		$message = "Could not create GeniSliver ".
		    "$interface in $linkname";
		goto bad;
	    }
	    if ($sliver->SetAggregate($linkaggregate) != 0) {
		$message = "Could not add link sliver $sliver to $aggregate";
		goto bad;
	    }
	}
    }

Leigh B. Stoller's avatar
Leigh B. Stoller committed
601
    #
602
603
604
    # Now do the provisioning (note that we actually allocated the
    # node above when the ticket was granted). Then add the sliver to
    # the aggregate.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
605
    #
606
    foreach my $sliver (values(%slivers)) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
607
	if (!$impotent && $sliver->Provision($extraargs) != 0) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
608
609
	    $message = "Could not provision $sliver";
	    goto bad;
610

Leigh B. Stoller's avatar
Leigh B. Stoller committed
611
	}
612
613
	if (defined($aggregate) &&
	    $sliver->SetAggregate($aggregate) != 0) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
614
615
616
617
618
	    $message = "Could not aggregate for $sliver to $aggregate";
	    goto bad;
	}
    }

Leigh B. Stoller's avatar
Leigh B. Stoller committed
619
    #
Leigh B. Stoller's avatar
Leigh B. Stoller committed
620
    # The API states we return a credential to control the sliver/aggregate.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
621
    #
622
623
624
625
626
627
628
    my $credential;
    if (defined($aggregate)) {
	$credential = $aggregate->NewCredential($owner);
    }
    else {
	$credential = ((values(%slivers))[0])->NewCredential($owner);
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
629
    if (!defined($credential)) {
630
	$message = "Could not create credential";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
631
	goto bad;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
632
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
633
634
635
636
637
638
639
640
    #
    # The last step is to delete the ticket, since it is no longer needed.
    # and will cause less confusion if it is not in the DB.
    #
    if ($ticket->Delete() != 0) {
	print STDERR "Error deleting $ticket for $slice\n";
    }
    
Leigh B. Stoller's avatar
Leigh B. Stoller committed
641
    return GeniResponse->Create(GENIRESPONSE_SUCCESS, $credential->asString());
Leigh B. Stoller's avatar
Leigh B. Stoller committed
642
643

  bad:
644
    foreach my $sliver (values(%slivers)) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
645
646
647
648
649
650
651
652
	$sliver->UnProvision()
	    if (! $impotent);
	$sliver->Delete();
    }
    $aggregate->Delete()
	if (defined($aggregate));
    
    return GeniResponse->Create(GENIRESPONSE_ERROR, undef, $message);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
653
654
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
#
# Release a ticket.
#
sub ReleaseTicket($)
{
    my ($argref) = @_;
    my $ticket     = $argref->{'ticket'};

    if (! (defined($ticket) &&
	   !TBcheck_dbslot($ticket, "default", "text",
			   TBDB_CHECKDBSLOT_ERROR))) {
	return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
				    "ticket: ". TBFieldErrorString());
    }
    $ticket = GeniTicket->CreateFromSignedTicket($ticket);
    if (!defined($ticket)) {
	return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
				    "Could not create GeniTicket object");
    }
    
    #
    # Make sure the credential was issued to the caller.
    #
    if ($ticket->owner_uuid() ne $ENV{'GENIUUID'}) {
	return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
				    "This is not your credential!");
    }
    if ($ticket->Release() != 0) {
	print STDERR "Error releasing $ticket\n";
	return GeniResponse->Create(GENIRESPONSE_ERROR);
    }
    return GeniResponse->Create(GENIRESPONSE_SUCCESS);
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
689
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
690
# Start a sliver (not sure what this means yet, so reboot for now).
Leigh B. Stoller's avatar
Leigh B. Stoller committed
691
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
692
sub StartSliver($)
Leigh B. Stoller's avatar
Leigh B. Stoller committed
693
694
{
    my ($argref) = @_;
695
696
    my $cred        = $argref->{'credential'};
    my $impotent    = $argref->{'impotent'};
Leigh B. Stoller's avatar
Leigh B. Stoller committed
697
698
699

    $impotent = 0
	if (!defined($impotent));
Leigh B. Stoller's avatar
Leigh B. Stoller committed
700

701
    if (!defined($cred)) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
702
703
	return GeniResponse->Create(GENIRESPONSE_BADARGS);
    }
704
    my $credential = GeniCredential->CreateFromSigned($cred);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
705
706
707
708
    if (!defined($credential)) {
	return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
				    "Could not create GeniCredential object");
    }
709
710
711
712
713
714
715
716
717
718
    my $sliver_uuid = $credential->this_uuid();
    my $user_uuid   = $credential->owner_uuid();
    
    #
    # Make sure the credential was issued to the caller.
    #
    if ($credential->owner_uuid() ne $ENV{'GENIUUID'}) {
	return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
				    "This is not your credential!");
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
719
720
    my $sliver = GeniSliver->Lookup($sliver_uuid);
    if (!defined($sliver)) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
721
722
723
724
725
726
	# Might be an aggregate instead.
	$sliver = GeniAggregate->Lookup($sliver_uuid);
	if (!defined($sliver)) {
	    return GeniResponse->Create(GENIRESPONSE_BADARGS, undef,
				    "No such sliver/aggregate $sliver_uuid");
	}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
727
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
728
    if (!$impotent) {
729
	$sliver->Start() == 0 or
Leigh B. Stoller's avatar
Leigh B. Stoller committed
730
731
732
733
734
735
736
737
738
	    return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
				"Could not start sliver/aggregate");
    }
    return GeniResponse->Create(GENIRESPONSE_SUCCESS);
}

#
# Destroy a sliver/aggregate.
#
739
sub DeleteSliver($)
Leigh B. Stoller's avatar
Leigh B. Stoller committed
740
741
{
    my ($argref) = @_;
742
743
    my $cred        = $argref->{'credential'};
    my $impotent    = $argref->{'impotent'};
Leigh B. Stoller's avatar
Leigh B. Stoller committed
744
745
746
747

    $impotent = 0
	if (!defined($impotent));

748
    if (!defined($cred)) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
749
750
	return GeniResponse->Create(GENIRESPONSE_BADARGS);
    }
751
    my $credential = GeniCredential->CreateFromSigned($cred);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
752
    if (!defined($credential)) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
753
	return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
Leigh B. Stoller's avatar
Leigh B. Stoller committed
754
				    "Could not create GeniCredential object");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
755
    }
756
757
    my $sliver_uuid = $credential->this_uuid();
    my $user_uuid   = $credential->owner_uuid();
758
    my @slivers     = ();
759
760
761
762
763
764
765
766
    
    #
    # Make sure the credential was issued to the caller.
    #
    if ($credential->owner_uuid() ne $ENV{'GENIUUID'}) {
	return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
				    "This is not your credential!");
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
767
768
769
    my $sliver = GeniSliver->Lookup($sliver_uuid);
    if (!defined($sliver)) {
	# Might be an aggregate instead.
770
771
	my $aggregate = GeniAggregate->Lookup($sliver_uuid);
	if (!defined($aggregate)) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
772
773
774
	    return GeniResponse->Create(GENIRESPONSE_BADARGS, undef,
				    "No such sliver/aggregate $sliver_uuid");
	}
775
	push(@slivers, $aggregate);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
776
    }
777
778
    else {
	# 
779
780
781
782
	# Find dependent slivers first (say, links on a node). For now,
	# do not allow this sliver to be torn down until the dependent
	# sliver(s) are torn down first. Eventually we want to tear them
	# down here in the proper order, hence the code below.
783
784
	#
	if ($sliver->DependentSlivers(\@slivers) != 0) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
785
	    return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
786
787
					"Could not get DependentSlivers");
	}
788
789
790
791
792
	if (@slivers) {
	    return GeniResponse->Create(GENIRESPONSE_REFUSED, undef,
					"Must tear dow dependent slivers");
	}
	@slivers = (@slivers, $sliver);	
793
794
795
796
797
798
799
800
801
802
803
    }

    foreach $sliver (@slivers) {
	if (!$impotent) {
	    $sliver->UnProvision() == 0 or
		return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
					    "Could not unprovision sliver");
	}
	$sliver->Delete() == 0 or
	    return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
					"Could not delete sliver");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
804
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
805
    return GeniResponse->Create(GENIRESPONSE_SUCCESS);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
806
}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
807

808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
#
# Utility Routines.
#
# Create a slice from the ClearingHouse, by looking up the info.
#
sub CreateSliceFromRegistry($)
{
    my ($slice_uuid) = @_;

    my $blob;
    return undef
	if (GeniCHClient::LookupSlice($slice_uuid, \$blob) != 0);

    my $authority = GeniAuthority->Lookup($blob->{'sa_uuid'});
    if (!defined($authority)) {
	$authority = CreateAuthorityFromRegistry($blob->{'sa_uuid'});
	if (!defined($authority)) {
	    print STDERR "Could not create new authority record\n";
	    return undef;
	}
    }
829

Leigh B. Stoller's avatar
Leigh B. Stoller committed
830
831
    print Dumper($blob);

832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
    #
    # The problem with HRNs is that people will tend to reuse them!
    # So check to see if we have a slice with the hrn, and if so
    # we need to check with the registry to see if its still active.
    # Destroy it locally if not active.
    #
    my $slice = GeniSlice->Lookup($blob->{'hrn'});
    if (defined($slice)) {
	return $slice
	    if ($slice->uuid() eq $slice_uuid);
	
	my $blob2;
	if (GeniCHClient::LookupSlice($slice->uuid(), \$blob2) == 0) {
	    print STDERR "hrn already in use for $slice!\n";
	    return undef;
	}
	return undef
	    if (CleanupDeadSlice($slice) != 0);
    }
    $slice = GeniSlice->Create($blob->{'hrn'},
			       $blob->{'uuid'},
			       $blob->{'creator_uuid'},
			       $blob->{'cert'}, $authority);
855
856
857
858
859
860
861
862
863
    return undef
	if (!defined($slice));

    # Add the user bindings.
    foreach my $uuid (@{ $blob->{'userbindings'} }) {
	my $user = GeniUser->Lookup($uuid);
	if (!defined($user)) {
	    $user = CreateUserFromRegistry($uuid);
	    if (!defined($user)) {
864
		print STDERR "No user $uuid locally or in the ClearingHouse\n";
865
866
867
		next;
	    }
	}
868
	$slice->BindUser($user) == 0
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
	    or print STDERR
	    "Could not insert user binding for $uuid to slice $slice_uuid\n";
    }
    return $slice;
}

#
# Update slice from the ClearingHouse, by looking up the info.
#
sub UpdateSliceFromRegistry($)
{
    my ($slice) = @_;
    my $slice_uuid = $slice->uuid();

    my $blob;
    return -1
	if (GeniCHClient::LookupSlice($slice_uuid, \$blob) != 0);

    DBQueryWarn("delete from geni_bindings ".
		"where slice_uuid='$slice_uuid'")
	or print STDERR
	"Could not delete user bindings from slice $slice_uuid\n";

    # Add the user bindings.
    foreach my $uuid (@{ $blob->{'userbindings'} }) {
	my $user = GeniUser->Lookup($uuid);
	if (!defined($user)) {
	    $user = CreateUserFromRegistry($uuid);
	    if (!defined($user)) {
		print STDERR "No user $uuid in the ClearingHouse\n";
		next;
	    }
	}
902
	$slice->BindUser($user) == 0
903
904
905
906
907
908
909
910
911
912
913
914
915
	    or print STDERR
	    "Could not insert user binding for $uuid to slice $slice_uuid\n";
    }
    return 0;
}

#
# Create a user from the ClearingHouse, by looking up the info.
#
sub CreateUserFromRegistry($)
{
    my ($uuid) = @_;

916
917
918
919
920
921
922
923
924
925
926
927
928
    #
    # Check Emulab users table first.
    #
    my $user = User->LookupByUUID($uuid);
    if (defined($user)) {
	my $geniuser = GeniUser->CreateFromLocal($user);
	if (!defined($geniuser)) {
	    print STDERR "Could not create geniuser from $user\n";
	    return undef;
	}
	return $geniuser;
    }

929
930
931
932
933
934
935
936
937
938
939
940
    my $blob;
    return undef
	if (GeniCHClient::LookupUser($uuid, \$blob) != 0);

    my $authority = GeniAuthority->Lookup($blob->{'sa_uuid'});
    if (!defined($authority)) {
	$authority = CreateAuthorityFromRegistry($blob->{'sa_uuid'});
	if (!defined($authority)) {
	    print STDERR "Could not create new authority record\n";
	    return undef;
	}
    }
941
942
    print STDERR Dumper($blob);
    
943
944
945
946
947
948
    return GeniUser->Create($blob->{'hrn'},
			    $blob->{'uid'},
			    $blob->{'uuid'},
			    $blob->{'name'},
			    $blob->{'email'},
			    $blob->{'cert'},
949
			    $authority, $blob->{'sliverkeys'});
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
}

#
# Create authority from the ClearingHouse, by looking up the info.
#
sub CreateAuthorityFromRegistry($)
{
    my ($uuid) = @_;

    my $blob;
    return undef
	if (GeniCHClient::Resolve($uuid, "SA", \$blob) != 0);

    return GeniAuthority->Create($uuid,
				 $blob->{'hrn'},
				 $blob->{'url'},
				 $blob->{'cert'},
				 $blob->{'uuid_prefix'}, "sa");
}
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986

#
# Cleanup a dead slice, releasing all the stuff associated with it.
#
sub CleanupDeadSlice($)
{
    my ($slice) = @_;

    print STDERR "Cleaning up dead slice $slice\n";

    #
    # Find any aggregates and tear them down.
    #
    my @aggregates;
    if (GeniAggregate->SliceAggregates($slice, \@aggregates) != 0) {
	print STDERR "Could not get dead aggregates for $slice.\n";
	return -1;
    }
987
988
989
990
991
    #
    # Link aggregates first.
    #
    my @nonlinks = ();
    
992
    foreach my $aggregate (@aggregates) {
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
	if ($aggregate->type() ne "Link") {
	    push(@nonlinks, $aggregate);
	    next;
	}
	if ($aggregate->UnProvision() != 0) {
	    print STDERR "Could not UnProvision $aggregate\n";
	    return -1;
	}
	if ($aggregate->Delete() != 0) {
	    print STDERR "Could not delete $aggregate\n";
	    return -1;
	}
    }
    foreach my $aggregate (@nonlinks) {
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
	if ($aggregate->UnProvision() != 0) {
	    print STDERR "Could not UnProvision $aggregate\n";
	    return -1;
	}
	if ($aggregate->Delete() != 0) {
	    print STDERR "Could not delete $aggregate\n";
	    return -1;
	}
    }

    #
    # Are there any slivers left after killing the aggregates? 
    #
    my @slivers;
    if (GeniSliver->SliceSlivers($slice, \@slivers) != 0) {
	print STDERR "Could not get dead slivers for $slice.\n";
	return -1;
    }
    foreach my $sliver (@slivers) {
	if ($sliver->UnProvision() != 0) {
	    print STDERR "Could not UnProvision $sliver\n";
	    return -1;
	}
	if ($sliver->Delete() != 0) {
	    print STDERR "Could not delete $sliver\n";
	    return -1;
	}
    }

    #
    # And lastly, any tickets that were not instantiated. 
    #
    my @tickets;
    if (GeniTicket->SliceTickets($slice, \@tickets) != 0) {
	print STDERR "Could not get dead tickets for $slice.\n";
	return -1;
    }
    foreach my $ticket (@tickets) {
	print STDERR "Releasing $ticket\n";
	
	if ($ticket->Release() != 0) {
	    print STDERR "Could not delete $ticket\n";
	    return -1;
	}
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062

    my $experiment = $slice->GetExperiment();
    if (!defined($experiment)) {
	print STDERR "Could not get experiment for $slice\n";
	return -1;
    }
    # Note the -h option; allows experiment with no NS file.
    system("$ENDEXPT -q -w " . $experiment->idx());
    return -1
	if ($?);
    
1063
1064
1065
1066
1067
1068
    if ($slice->Delete() != 0) {
	print STDERR "Could not delete $slice\n";
	return -1;
    }
    return 0;
}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113

#
# Remove a record, specifically a slice on this component.
#
sub DeleteSlice($)
{
    my ($argref) = @_;
    my $credential = $argref->{'credential'};

    if (! defined($credential)) {
	return GeniResponse->MalformedArgsResponse();
    }

    $credential = GeniCredential->CreateFromSigned($credential);
    if (!defined($credential)) {
	return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
				    "Could not create GeniCredential object");
    }

    my $slice_uuid = $credential->this_uuid();
    
    #
    # Make sure the credential was issued to the caller.
    #
    if ($credential->owner_uuid() ne $ENV{'GENIUUID'}) {
	return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
				    "This is not your credential!");
    }

    #
    # See if we have a record of this slice in the DB. If not, then we have
    # to go to the ClearingHouse to find its record, so that we can find out
    # who the SA for it is.
    #
    my $slice = GeniSlice->Lookup($slice_uuid);
    if (!defined($slice)) {
	return GeniResponse->Create(GENIRESPONSE_BADARGS, undef,
			    "No such slice on this component: $slice_uuid");
    }
    if (CleanupDeadSlice($slice) != 0) {
	return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
				    "Could not cleanup slice");
    }
    return GeniResponse->Create(GENIRESPONSE_SUCCESS);
}
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176

#
# Split an aggregated sliver into its separate parts and return a list.
#
sub SplitSliver($)
{
    my ($argref) = @_;
    my $cred        = $argref->{'credential'};
    my $impotent    = $argref->{'impotent'};

    $impotent = 0
	if (!defined($impotent));

    if (!defined($cred)) {
	return GeniResponse->Create(GENIRESPONSE_BADARGS);
    }
    my $credential = GeniCredential->CreateFromSigned($cred);
    if (!defined($credential)) {
	return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
				    "Could not create GeniCredential object");
    }
    my $sliver_uuid = $credential->this_uuid();
    my $user_uuid   = $credential->owner_uuid();
    
    #
    # Make sure the credential was issued to the caller.
    #
    if ($credential->owner_uuid() ne $ENV{'GENIUUID'}) {
	return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
				    "This is not your credential!");
    }
    my $user = GeniUser->Lookup($user_uuid);
    if (!defined($user)) {
	$user = CreateUserFromRegistry($user_uuid);
	if (!defined($user)) {
	    print STDERR "No user $user_uuid in the ClearingHouse\n";
	    return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
					"No user record for $user_uuid");
	}
    }

    my $aggregate = GeniAggregate->Lookup($sliver_uuid);
    if (!defined($aggregate)) {
	return GeniResponse->Create(GENIRESPONSE_BADARGS, undef,
				    "No such aggregate $sliver_uuid");
    }
    my @sliver_list = ();
    if ($aggregate->SliverList(\@sliver_list) != 0) {
	return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
				    "Could not get slivers for $aggregate");
    }
    my @credentials = ();

    foreach my $sliver (@sliver_list) {
	my $credential = $sliver->NewCredential($user);
	if (!defined($credential)) {
	    return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
				"Could not create credential for $sliver");
	}
	push(@credentials, $credential->asString());
    }
    return GeniResponse->Create(GENIRESPONSE_SUCCESS, \@credentials);
}