APT_Rspec.pm.in 56.7 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 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
#!/usr/bin/perl -wT
#
# Copyright (c) 2007-2017 University of Utah and the Flux Group.
# 
# {{{EMULAB-LICENSE
# 
# This file is part of the Emulab network testbed software.
# 
# This file is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
# 
# This file is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
# License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this file.  If not, see <http://www.gnu.org/licenses/>.
# 
# }}}
#
package APT_Rspec;

use strict;
use Data::Dumper;
use Scalar::Util qw(blessed);
use HTML::Entities;
use Carp;
use Exporter;
use vars qw(@ISA @EXPORT);

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

# Must come after package declaration!
use emdb;
use GeniXML;
use GeniHRN;

# Configure variables
my $TB		  = "@prefix@";
my $OURDOMAIN     = "@OURDOMAIN@";

46 47 48
# This is a global instead of class.
my $verbose       = 0;

Leigh B Stoller's avatar
Leigh B Stoller committed
49 50 51 52 53 54 55
# Protos;
sub CompareHashes($$$);
sub CompareLists($$$);

#
# Parse an rspec into nice perl things.
#
56
sub new($$;$$)
Leigh B Stoller's avatar
Leigh B Stoller committed
57
{
58
    my ($class, $rspecfile, $permissive, $verbose_mode) = @_;
Leigh B Stoller's avatar
Leigh B Stoller committed
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
    my %namespaces = ();
    my $rspec;

    if ($rspecfile =~ m{<.*?>}s) {
	$rspec = GeniXML::Parse($rspecfile);
    }
    else {
	$rspec = GeniXML::ParseFile($rspecfile);
    }
    if (! defined($rspec)) {
	fatal("Could not parse rspec");
    }
    #
    # Find out all the name spaces being used.
    #
    foreach my $node ($rspec->findnodes('//namespace::*')) {
	my $name = $node->name();
	my $url  = $node->getData();
	$namespaces{$name} = $url;
	$namespaces{$url}  = $name;
    }
    my $self = {
	"nodes"	            => {},
82
	"nodelist"          => [],
Leigh B Stoller's avatar
Leigh B Stoller committed
83 84
	"ifaces"            => {},
	"links"             => {},
85 86
	"linklist"          => [],
	"toplevel_elements" => [],  # Use attributes for toplevel elements
Leigh B Stoller's avatar
Leigh B Stoller committed
87 88 89
	"tour"              => undef,
	"namespaces"        => \%namespaces,
	"bscounter"         => 0,
Leigh B Stoller's avatar
Leigh B Stoller committed
90
	"permissive"        => (defined($permissive) ? $permissive : 0),
Leigh B Stoller's avatar
Leigh B Stoller committed
91 92 93
    };
    bless($self, $class);

94 95 96 97
    if (defined($verbose_mode) && $verbose_mode) {
	$verbose = 1;
    }

Leigh B Stoller's avatar
Leigh B Stoller committed
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
    #
    # Process the Tour.
    #
    my $processTour = sub {
	my ($self, $ref) = @_;
	my $tour = {};

	foreach my $child ($ref->nonBlankChildNodes()) {
	    my ($ns,$name) = split(":", $child->nodeName);
	    $name = $ns if (!defined($name));

	    SWITCH: for (lc($name)) {
		/^description$/i && do {
		    my $type = GeniXML::FindAttr("type", $child);
		    my $text = $child->textContent();
Leigh B Stoller's avatar
Leigh B Stoller committed
113
		    # Trim for rtecheck.
Leigh B Stoller's avatar
Leigh B Stoller committed
114
		    $text =~ s/^\s+//;
115
		    $text =~ s/\s+$//s;
Leigh B Stoller's avatar
Leigh B Stoller committed
116
		    $text =~ s/\"/\\"/g;
117
		    $text =~ s/\t/\\t/g;
Leigh B Stoller's avatar
Leigh B Stoller committed
118 119 120 121 122 123 124 125 126
		    $tour->{'description'} = {
			"text" => $text,
			"type" => $type,
		    };
		    last SWITCH;
		};
		/^instructions$/i && do {
		    my $type = GeniXML::FindAttr("type", $child);
		    my $text = $child->textContent();
Leigh B Stoller's avatar
Leigh B Stoller committed
127
		    # Trim for rtecheck.
Leigh B Stoller's avatar
Leigh B Stoller committed
128
		    $text =~ s/^\s+//;
129
		    $text =~ s/\s+$//s;
130 131 132
		    # This happens a lot.
		    if ($text ne "") {
			$text =~ s/\"/\\"/g;
133
			$text =~ s/\t/\\t/g;
134 135 136 137 138
			$tour->{'instructions'} = {
			    "text" => $text,
			    "type" => $type,
			};
		    }
Leigh B Stoller's avatar
Leigh B Stoller committed
139 140
		    last SWITCH;
		};
Leigh B Stoller's avatar
Leigh B Stoller committed
141 142 143 144 145 146 147 148
		/^steps$/i && do {
		    my @steps = ();
		    foreach my $stepchild ($child->nonBlankChildNodes()) {
			my ($ns,$name) = split(":", $stepchild->nodeName);
			$name = $ns if (!defined($name));
			if ($name ne "step") {
			    fatal("Unknown element in steps: $name");
			}
149 150 151 152 153
			my $type  = GetTextOrFail("point_type", $stepchild);
			my $id    = GetTextOrFail("point_id", $stepchild);
			my $desc  = FindFirst("n:description", $stepchild);
			my $dtype = GetText("type", $desc);
			my $text  = $desc->textContent();
Leigh B Stoller's avatar
Leigh B Stoller committed
154 155 156 157 158
			# Kill terminating newline for rtecheck
			chomp($text);
			push(@steps, {
			    "type" => $type,
			    "id"   => $id,
159 160
			    "description" => $text,
			    "description_type" => $dtype});
Leigh B Stoller's avatar
Leigh B Stoller committed
161 162 163 164 165 166
		    }
		    # We see a lot of empty steps
		    $tour->{'steps'} = \@steps
			if (scalar(@steps));
		    last SWITCH;
		};
167 168 169 170
		/^#(comment|text)$/i && do {
		    # Maybe we should do something comment lines.
		    last SWITCH;
		};
Leigh B Stoller's avatar
Leigh B Stoller committed
171
		fatal("Unknown element in rspec_tour: $name");
Leigh B Stoller's avatar
Leigh B Stoller committed
172 173 174 175
	    };
	}
	return
	    if (! (exists($tour->{"description"}) ||
Leigh B Stoller's avatar
Leigh B Stoller committed
176 177
		   exists($tour->{"instructions"}) ||
		   exists($tour->{"steps"})));
Leigh B Stoller's avatar
Leigh B Stoller committed
178 179 180 181 182 183 184 185 186 187 188 189 190 191
	
	$self->{'tour'} = $tour;
    };
    
    #
    # Process top level elements.
    #
    my $processTopLevelElement = sub {
	my ($self, $ref) = @_;
	my ($ns,$name) = split(":", $ref->nodeName);
	$name = $ns if (!defined($name));

        SWITCH: for (lc($name)) {
	    /^password$/i && do {
Leigh B Stoller's avatar
Leigh B Stoller committed
192 193
		my $passname = GetTextOrFail("name", $ref);
		APT_Rspec::Attribute->new($self, $name, $passname);
Leigh B Stoller's avatar
Leigh B Stoller committed
194 195 196
		last SWITCH;
	    };
	    /^routable_pool$/i && do {
197 198
		APT_Rspec::Attribute->new($self, $name,
					  APT_Rspec::Pool->new($ref));
Leigh B Stoller's avatar
Leigh B Stoller committed
199 200 201 202
		last SWITCH;
	    };
	    /^collocate_factor$/i && do {
		my $count = GetTextOrFail("count", $ref);
203
		APT_Rspec::Attribute->new($self, $name, $count);
Leigh B Stoller's avatar
Leigh B Stoller committed
204 205 206 207 208 209 210
		last SWITCH;
	    };
	    /^packing_strategy$/i && do {
		my $strategy = GetTextOrFail("strategy", $ref);
		if ($strategy !~ /^(pack|balance)$/) {
		    fatal("packing_strategy: bad strategy: $strategy");
		}
211
		APT_Rspec::Attribute->new($self, $name, $strategy);
Leigh B Stoller's avatar
Leigh B Stoller committed
212 213 214 215
		last SWITCH;
	    };
	    /^routing_style$/i && do {
		my $style = GetTextOrFail("style", $ref);
216
		APT_Rspec::Attribute->new($self, $name, $style);
Leigh B Stoller's avatar
Leigh B Stoller committed
217 218 219 220
		last SWITCH;
	    };
	    /^delay_image$/i && do {
		my $urn = GetTextOrFail("urn", $ref);
221
		APT_Rspec::Attribute->new($self, $name, $urn);
Leigh B Stoller's avatar
Leigh B Stoller committed
222 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 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
		last SWITCH;
	    };
	    fatal("Toplevel XML element $name is not supported");
	}
    };

    #
    # Main processing. We want to do everything but the links on the
    # first pass, and then another pass do do the links.
    #
    foreach my $child ($rspec->nonBlankChildNodes()) {
	my ($ns,$name) = split(":", $child->nodeName);
	$name = $ns if (!defined($name));

	# Ignore old flack elements.
	next
	    if (defined($ns) && $ns =~/^(flack|history)$/);

        SWITCH: for (lc($name)) {
	    /^(site_info)$/i && do {
		# Manifest crap.
		last SWITCH;
	    };
	    /^node$/i && do {
		APT_Rspec::Node->addNode($self, $child);
		last SWITCH;
	    };
	    /^client_info$/i && do {
		# Old flack stuff?
		last SWITCH;
	    };
	    /^link$/i && do {
		# See below.
		last SWITCH;
	    };
	    /^rspec_tour$/i && do {
		&$processTour($self, $child);
		last SWITCH;
	    };
	    (/^(\w+:)?password$/i ||
	     /^(\w+:)?routable_pool$/i ||
Leigh B Stoller's avatar
Leigh B Stoller committed
263
	     /^(\w+:)?packing_strategy$/i ||
Leigh B Stoller's avatar
Leigh B Stoller committed
264 265 266 267 268 269 270
	     /^(\w+:)?collocate_factor$/i ||
	     /^(\w+:)?routing_style$/i ||
	     /^(\w+:)?delay_image$/i) && do {
		 &$processTopLevelElement($self, $child);
		 last SWITCH;
	    };
	    /^(\w+:)?data_set$/i && do {
Leigh B Stoller's avatar
Leigh B Stoller committed
271 272
		fatal("data_set element is not supported")
		    if (!$self->permissive());
Leigh B Stoller's avatar
Leigh B Stoller committed
273 274 275
		last SWITCH;
	    };
	    /^(\w+:)?profile_parameters$/i && do {
Leigh B Stoller's avatar
Leigh B Stoller committed
276 277
		fatal("profile_parameters element is not supported")
		    if (!$self->permissive());
Leigh B Stoller's avatar
Leigh B Stoller committed
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
		last SWITCH;
	    };
	    /^#(comment|text)$/i && do {
		# Maybe we should do something comment lines.
		last SWITCH;
	    };
	    fatal("Document XML element $name is not supported");
	}
    }
    # Now the links, since we have all the nodes processed.
    foreach my $child ($rspec->nonBlankChildNodes()) {
	my $name = $child->nodeName;
	my $ns   = $child->namespaceURI();
	
        SWITCH: for (lc($name)) {
	    /^link$/i && do {
		APT_Rspec::Link->addLink($self, $child);
		last SWITCH;
	    };
	}
    }
    return $self;
}
sub nodes($)			{ return $_[0]->{'nodes'}; }
sub links($)			{ return $_[0]->{'links'}; }
303 304
sub nodelist($)			{ return $_[0]->{'nodelist'}; }
sub linklist($)			{ return $_[0]->{'linklist'}; }
Leigh B Stoller's avatar
Leigh B Stoller committed
305 306
sub ifaces($)			{ return $_[0]->{'ifaces'}; }
sub tour($)			{ return $_[0]->{'tour'}; }
307 308
sub attributes($)		{ return $_[0]->{'toplevel_elements'}; }
sub toplevel_elements($)	{ return $_[0]->attributes(); }
Leigh B Stoller's avatar
Leigh B Stoller committed
309
sub permissive($)		{ return $_[0]->{'permissive'}; }
Leigh B Stoller's avatar
Leigh B Stoller committed
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329

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

    return undef
	if (!defined($self->tour()) ||
	    !exists($self->tour()->{'description'}));
    return $self->tour()->{'description'};
}
sub instructions($)
{
    my ($self) = @_;

    return undef
	if (!defined($self->tour()) ||
	    !exists($self->tour()->{'instructions'}));
    return $self->tour()->{'instructions'};
}

Leigh B Stoller's avatar
Leigh B Stoller committed
330 331 332 333 334 335 336 337 338 339
sub steps($)
{
    my ($self) = @_;

    return undef
	if (!defined($self->tour()) ||
	    !exists($self->tour()->{'steps'}));
    return $self->tour()->{'steps'};
}

Leigh B Stoller's avatar
Leigh B Stoller committed
340 341 342
sub addNode($$)
{
    my ($self, $node) = @_;
343 344
    # Maintain rspec ordering for RTE checks.
    push(@{$self->nodelist()}, $node);
Leigh B Stoller's avatar
Leigh B Stoller committed
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
    $self->nodes()->{$node->client_id()} = $node;
}
sub getNode($$)
{
    my ($self, $client_id) = @_;
    return undef
	if (!exists($self->{'nodes'}->{$client_id}));

    return $self->{'nodes'}->{$client_id};
}
sub addIface($$)
{
    my ($self, $iface) = @_;
    $self->ifaces()->{$iface->client_id()} = $iface;
}
sub getIface($$)
{
    my ($self, $client_id) = @_;
    return undef
	if (!exists($self->{'ifaces'}->{$client_id}));

    return $self->{'ifaces'}->{$client_id};
}
sub addLink($$)
{
    my ($self, $link) = @_;
371 372
    # Maintain rspec ordering for RTE checks.
    push(@{$self->linklist()}, $link);
Leigh B Stoller's avatar
Leigh B Stoller committed
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 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 417 418 419 420 421 422 423 424 425
    $self->links()->{$link->client_id()} = $link;
}
sub getLink($$)
{
    my ($self, $client_id) = @_;
    return undef
	if (!exists($self->{'links'}->{$client_id}));

    return $self->{'links'}->{$client_id};
}

#
# Not sure what I want to do about errors.
#
sub fatal($) {
    my ($mesg) = $_[0];

    die("*** $mesg\n");
}

sub GetTextOrFail(@)
{
    my ($id, $ref) = @_;
    my $name = $ref->nodeName;
    my $val  = GetText($id, $ref);

    return $val
	if (defined($val));
    
    fatal("Missing $id in $name element");
}

#
# Compare two values as strings, allowing both to be undefined.
# Return zero if equal.
#
sub CompareValues($$$)
{
    my ($text, $val1, $val2) = @_;

    #print "CompareValues: $text\n";

    # Both undefined is equal
    return 0
	if (! (defined($val1) && defined($val2)));

    return 0
	if (defined($val1) && defined($val2) && "$val1" eq "$val2");

    $val1 = "undef"
	if (!defined($val1));
    $val2 = "undef"
	if (!defined($val2));
426
    print STDERR "*** $text: '$val1' != '$val2'\n";
Leigh B Stoller's avatar
Leigh B Stoller committed
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
    return 1;
}

#
# Compare two hashes. Return zero if equal.
#
sub CompareHashes($$$)
{
    my ($text, $h1, $h2) = @_;

    #print "CompareHashes: $text\n";

    if (scalar(values(%{$h1})) != scalar(values(%{$h2}))) {
	my $msg;
	
	if (!defined($h1)) {
	    $msg = "defined in rspec two but not in rspec one";
	}
	elsif (!defined($h2)) {
	    $msg = "defined in rspec one but not in rspec two";
	}
	else {
	    $msg = "rspec one has a different count then rspec two";
	}
451 452 453 454 455
	if ($verbose) {
	    print STDERR Dumper($h1);
	    print STDERR Dumper($h2);
	}
	print STDERR "*** ${text}: $msg\n";
Leigh B Stoller's avatar
Leigh B Stoller committed
456 457 458 459
	return 1;
    }
    foreach my $key (sort(keys(%$h1))) {
	if (!exists($h2->{$key})) {
460 461 462 463 464
	    if ($verbose) {
		print STDERR Dumper($h1);
		print STDERR Dumper($h2);
	    }
	    print STDERR "*** $text: $key in rspec one but not rspec two\n";
Leigh B Stoller's avatar
Leigh B Stoller committed
465 466 467 468 469
	    return 1;
	}
	my $v1 = $h1->{$key};
	my $v2 = $h2->{$key};
	if (ref($v1) ne ref($v2)) {
470
	    print STDERR "*** $text: key $key typeof mismatch\n";
Leigh B Stoller's avatar
Leigh B Stoller committed
471 472 473 474 475 476 477 478 479 480
	    return 1;
	}
	if (ref($v1) eq "") {
	    return 1
		if (CompareValues("$text: $key", $v1, $v2));
	}
	elsif (ref($v1) eq "HASH") {
	    return 1
		if (CompareHashes("$text: $key", $v1, $v2));
	}
Leigh B Stoller's avatar
Leigh B Stoller committed
481 482 483 484
	elsif (ref($v1) eq "ARRAY") {
	    return 1
		if (CompareLists("$text: $key", $v1, $v2));
	}
Leigh B Stoller's avatar
Leigh B Stoller committed
485 486 487 488 489 490
	# Hmm, we assume all blessed refs are our own and have compare methods.
	elsif (blessed($v1)) {
	    return 1
		if ($v1->Compare($v2));
	}
	else {
491
	    print STDERR "*** $text: do not know how to compare $key\n";
Leigh B Stoller's avatar
Leigh B Stoller committed
492 493 494 495 496 497 498 499 500 501 502 503 504 505 506
	    return 1;
	}
    }
    return 0;
}

#
# Compare two lists. Return zero if equal.
#
sub CompareLists($$$)
{
    my ($text, $l1, $l2) = @_;
    my @l1 = @{$l1};
    my @l2 = @{$l2};

Leigh B Stoller's avatar
Leigh B Stoller committed
507
    #print STDERR "CompareLists: $text\n";
Leigh B Stoller's avatar
Leigh B Stoller committed
508 509

    if (scalar(@l1) != scalar(@l2)) {
510 511 512 513 514 515
	if ($verbose) {
	    print STDERR Dumper($l1);
	    print STDERR Dumper($l2);
	}
	print STDERR "*** $text: ".
	    "rspec one has a different count then rspec two\n";
Leigh B Stoller's avatar
Leigh B Stoller committed
516 517 518 519 520 521 522
	return 1;
    }
    while (@l1) {
	my $v1 = pop(@l1);
	my $v2 = pop(@l2);

	if (ref($v1) ne ref($v2)) {
523
	    print STDERR "*** $text: typeof mismatch\n";
Leigh B Stoller's avatar
Leigh B Stoller committed
524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539
	    return 1;
	}
	if (ref($v1) eq "") {
	    return 1
		if (CompareValues("$text", $v1, $v2));
	}
	elsif (ref($v1) eq "HASH") {
	    return 1
		if (CompareHashes("$text", $v1, $v2));
	}
	# Hmm, we assume all blessed refs are our own and have compare methods.
	elsif (blessed($v1)) {
	    return 1
		if ($v1->Compare($v2));
	}
	else {
540
	    print STDERR "*** $text: do not know how to compare.\n";
Leigh B Stoller's avatar
Leigh B Stoller committed
541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584
	    return 1;
	}
    }
    return 0;
}

#
# Compare two rspecs, returning zero if the are equivalent.
#
sub Compare($$)
{
    my ($this, $that) = @_;
    
    foreach my $key (keys(%{$this})) {
	my $val1 = $this->{$key};
	my $val2 = $that->{$key};

	#
	# Enumerate all fields to be certain this is not out of sync
	# with the definition above.
	#
        SWITCH: for (lc($key)) {
	    /^nodes$/i && do {
		return 1
		    if (CompareNodes($val1, $val2));
		last SWITCH;
	    };
	    /^ifaces$/i && do {
		# Handled in CompareNodes() and CompareLinks()
		last SWITCH;
	    };
	    /^links$/i && do {
		return 1
		    if (CompareLinks($val1, $val2));
		last SWITCH;
	    };
	    /^toplevel_elements$/i && do {
		return 1
		    if (CompareToplevelElements($val1, $val2));
		last SWITCH;
	    };
	    /^tour$/i && do {
		# Need to deal with whitespace changes.
		return 1
Leigh B Stoller's avatar
Leigh B Stoller committed
585
		    if (APT_Rspec::CompareHashes($key, $val1, $val2));
Leigh B Stoller's avatar
Leigh B Stoller committed
586 587
		last SWITCH;
	    };
588 589 590 591
	    /^(bscounter|namespaces|nodelist|linklist)$/i && do {
		last SWITCH;
	    };
	    /^(permissive|verbose)$/i && do {
Leigh B Stoller's avatar
Leigh B Stoller committed
592 593
		last SWITCH;
	    };
594
	    print STDERR "*** Rspec:Compare: Unknown attribute: $key\n";
Leigh B Stoller's avatar
Leigh B Stoller committed
595 596 597 598 599 600 601
	    return 1;
	}
    }
    return 0;
}

#
602
# Compare top level elements. These are lists.
Leigh B Stoller's avatar
Leigh B Stoller committed
603 604 605 606 607
#
sub CompareToplevelElements($$)
{
    my ($elements1, $elements2) = @_;

608
    if (scalar(@{$elements2}) != scalar(@{$elements2})) {
609
	print STDERR "*** Rspec:CompareToplevelElements: mismatching length\n";
610 611 612 613 614 615 616 617
	return -1;
    }
    for (my $index = 0; $index < scalar(@{$elements1}); $index++) {
	my $attr1 = @{$elements1}[$index];
	my $attr2 = @{$elements2}[$index];
	my $key   = $attr1->name();
	my $val1  = $attr1->value();
	my $val2  = $attr2->value();
Leigh B Stoller's avatar
Leigh B Stoller committed
618 619 620 621 622 623 624 625 626 627 628 629 630 631 632

	#
	# Enumerate all fields to be certain this is not out of sync
	# with the definition above.
	#
        SWITCH: for (lc($key)) {
	    /^routable_pool$/i && do {
		return 1
		    if (APT_Rspec::CompareHashes("routable_pool",
						 $val1, $val2));
		last SWITCH;
	    };
	    (/^(password|collocate_factor|packing_strategy)$/i ||
	     /^(routing_style|delay_image)$/i) && do {
		return 1
633
		    if (APT_Rspec::CompareValues($key, $val1, $val2));
Leigh B Stoller's avatar
Leigh B Stoller committed
634 635
		last SWITCH;
	    };
636
	    print STDERR "*** Rspec:CompareToplevelElements: unknown $key\n";
Leigh B Stoller's avatar
Leigh B Stoller committed
637 638 639 640 641 642 643 644 645 646 647 648 649 650
	    return -1;
	}
    }
    return 0;
}

#
# Compare two lists of nodes
#
sub CompareNodes($$)
{
    my ($l1, $l2) = @_;

    if (scalar(values(%{$l1})) != scalar(values(%{$l2}))) {
651
	print STDERR "*** different number of nodes\n";
Leigh B Stoller's avatar
Leigh B Stoller committed
652 653 654 655 656
	return 1;
    }

    foreach my $client_id (sort(keys(%$l1))) {
	if (!exists($l2->{$client_id})) {
657
	    print STDERR "*** Node $client_id in rspec one but not rspec two\n";
Leigh B Stoller's avatar
Leigh B Stoller committed
658 659 660 661 662 663 664 665 666 667 668 669
	    return 1;
	}
	my $node1 = $l1->{$client_id};
	my $node2 = $l2->{$client_id};

	#
	# Do the ifaces here for better error reporting.
	#
	my $ifs1  = $node1->ifaces();
	my $ifs2  = $node2->ifaces();
	
	if (scalar(values(%{$ifs1})) != scalar(values(%{$ifs2}))) {
670
	    print STDERR "*** different number of ifaces on Node $client_id\n";
Leigh B Stoller's avatar
Leigh B Stoller committed
671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691
	    return 1;
	}
	foreach my $iface_id (sort(keys(%$ifs1))) {
	    my $iface1 = $ifs1->{$iface_id};
	    
	    if (!exists($ifs2->{$iface_id})) {
		my $altid;
		#
		# We force all interfaces to node_id:iface_id, so this
		# might be a name mismatch.
		#
		if ($iface_id =~ /^([-\w]*):([-\w]*)$/) {
		    $altid = $2;
		}
		else {
		    $altid = "${client_id}:${iface_id}";
		}
		if (exists($ifs2->{$altid})) {
		    $iface_id = $altid;
		}
		else {
692
		    print STDERR "*** iface $iface_id on node $client_id ".
Leigh B Stoller's avatar
Leigh B Stoller committed
693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714
			"in rspec one but not rspec two\n";
		    return 1;
		}
	    }
	    my $iface2 = $ifs2->{$iface_id};
	    return 1
		if ($iface1->Compare($iface2));
	}
	return 1
	    if ($node1->Compare($node2));
    }
    return 0;
}

#
# Compare two lists of links.
#
sub CompareLinks($$)
{
    my ($l1, $l2) = @_;

    if (scalar(values(%{$l1})) != scalar(values(%{$l2}))) {
715
	print STDERR "*** different number of links\n";
Leigh B Stoller's avatar
Leigh B Stoller committed
716 717 718 719 720
	return 1;
    }

    foreach my $client_id (sort(keys(%$l1))) {
	if (!exists($l2->{$client_id})) {
721
	    print STDERR "*** Link $client_id in rspec one but not rspec two\n";
Leigh B Stoller's avatar
Leigh B Stoller committed
722 723 724 725 726 727 728 729 730 731 732 733 734
	    return 1;
	}
	my $link1 = $l1->{$client_id};
	my $link2 = $l2->{$client_id};

	#
	# Do the ifaces here for better error reporting and cause of the
	# geni-lib naming issue.
	#
	my $ifs1  = $link1->ifaces();
	my $ifs2  = $link2->ifaces();
	
	if (scalar(values(%{$ifs1})) != scalar(values(%{$ifs2}))) {
735
	    print STDERR "*** different number of ifaces on link $client_id\n";
Leigh B Stoller's avatar
Leigh B Stoller committed
736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757
	    return 1;
	}
	foreach my $iface_id (sort(keys(%$ifs1))) {
	    my $iface1 = $ifs1->{$iface_id};

	    if (!exists($ifs2->{$iface_id})) {
		my $altid;
		#
		# We force all interfaces to node_id:iface_id, so this
		# might be a name mismatch.
		#
		if ($iface_id =~ /^([-\w]*):([-\w]*)$/) {
		    $altid = $2;
		}
		else {
		    my $node_id = $iface1->node_id();
		    $altid = "${node_id}:${iface_id}";
		}
		if (exists($ifs2->{$altid})) {
		    $iface_id = $altid;
		}
		else {
758
		    #print STDERR "$altid\n";
759
		    print STDERR "*** iface $iface_id in link $client_id ".
Leigh B Stoller's avatar
Leigh B Stoller committed
760 761 762 763 764 765 766 767 768 769 770 771 772 773
			"in rspec one but not rspec two\n";
		    return 1;
		}
	    }
	    my $iface2 = $ifs2->{$iface_id};
	    return 1
		if ($iface1->Compare($iface2));
	}
	return 1
	    if ($link1->Compare($link2));
    }
    return 0;
}

774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799
############################################################################
# The point of this class is provide a consistent way to describe all
# the little attributes in elements, and maintain their order so that
# we can generate the exact same rspec/genilib script each time.
#
package APT_Rspec::Attribute;
use Carp;
use English;
use GeniXML;
use GeniHRN;

sub new($$$$)
{
    my ($class, $parent, $name, $value) = @_;

    my $self = {
	"name"       => $name,
	"value"      => $value,
    };
    bless($self, $class);
    push(@{$parent->attributes()}, $self);
    return $self;
}
sub name($)		{ return $_[0]->{'name'}; }
sub value($)		{ return $_[0]->{'value'}; }

Leigh B Stoller's avatar
Leigh B Stoller committed
800 801 802 803 804 805 806 807 808 809 810
############################################################################
package APT_Rspec::Node;
use Carp;
use English;
use GeniXML;
use GeniHRN;

sub new($$$$$)
{
    my ($class, $rspec, $client_id, $type, $exclusive) = @_;

Leigh B Stoller's avatar
Leigh B Stoller committed
811 812 813 814
    #
    # Make up a tag (variable for the script) that makes some kind of sense.
    #
    my $tag = $client_id;
815
    # Replace dashes/spaces/dots with underscores.
Leigh B Stoller's avatar
Leigh B Stoller committed
816
    $tag =~ s/\-/\_/g;
817 818
    $tag =~ s/\ /\_/g;
    $tag =~ s/\./\_/g;
Leigh B Stoller's avatar
Leigh B Stoller committed
819 820 821 822
    if ($tag !~ /^node/i) {
	$tag = "node_${tag}";
    }

Leigh B Stoller's avatar
Leigh B Stoller committed
823 824 825 826 827 828 829 830 831 832 833 834 835 836 837
    my $self = {
	"client_id"		=> $client_id,
	"type"			=> $type,
	"exclusive"		=> $exclusive,
	"component_id"		=> undef,
	"component_manager_id"  => undef,
	"disk_image"		=> undef,
	"hardware_type"		=> undef,
	"jacks_site"		=> undef,
	"xen_settings"		=> undef,
	"xen_ptype"		=> undef,
	"instantiate_on"	=> undef,
	"services"		=> [],
	"statements"		=> [],
	"desires"		=> {},
Leigh B Stoller's avatar
Leigh B Stoller committed
838
	"attributes"		=> {},
Leigh B Stoller's avatar
Leigh B Stoller committed
839
	"ifaces"		=> {},
840
	"ifacelist"             => [],
Leigh B Stoller's avatar
Leigh B Stoller committed
841 842 843 844
	"blockstores"		=> {},
	"pipes"			=> {},
	"use_type_default_image"=> undef,
	"routable_control_ip"   => undef,
845
	"failure_action"        => undef,
Leigh B Stoller's avatar
Leigh B Stoller committed
846
	"tag"			=> $tag,
Leigh B Stoller's avatar
Leigh B Stoller committed
847 848 849
	"isexptfirewall"        => 0,
	"firewall_style"        => undef,
	"firewall_rules"        => [],
Leigh B Stoller's avatar
Leigh B Stoller committed
850 851 852 853 854 855 856 857 858 859
    };
    bless($self, $class);
    return $self;
}
sub client_id($)		{ return $_[0]->{'client_id'}; }
sub type($)			{ return $_[0]->{'type'}; }
sub exclusive($)		{ return $_[0]->{'exclusive'}; }
sub statements($)		{ return $_[0]->{'statements'}; }
sub blockstores($)		{ return $_[0]->{'blockstores'}; }
sub ifaces($)			{ return $_[0]->{'ifaces'}; }
860
sub ifacelist($)		{ return $_[0]->{'ifacelist'}; }
Leigh B Stoller's avatar
Leigh B Stoller committed
861
sub services($)			{ return $_[0]->{'services'}; }
Leigh B Stoller's avatar
Leigh B Stoller committed
862 863
sub desires($)			{ return $_[0]->{'services'}; }
sub attributes($)		{ return $_[0]->{'attributes'}; }
Leigh B Stoller's avatar
Leigh B Stoller committed
864
sub pipes($)			{ return $_[0]->{'pipes'}; }
Leigh B Stoller's avatar
Leigh B Stoller committed
865 866
sub firewall_style($)		{ return $_[0]->{'firewall_style'}; }
sub firewall_rules($)		{ return $_[0]->{'firewall_rules'}; }
867
sub slivertype($)		{ return $_[0]->{'type'}; }
Leigh B Stoller's avatar
Leigh B Stoller committed
868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891
sub tag($)			{ return $_[0]->{'tag'}; }
sub fatal($)			{ return APT_Rspec::fatal($_[0]); }
sub GetTextOrFail($$)		{ return APT_Rspec::GetTextOrFail(@_); }

sub addStatement($$)
{
    my ($self, $statement) = @_;
    push(@{$self->{'statements'}}, $statement);
}
sub addTagStatement($$)
{
    my ($self, $statement) = @_;
    my $tag = $self->tag();
    addStatement($self, "${tag}.${statement}");
}
sub addBridgePipe($$) {
    my ($self, $pipe) = @_;
    my $iface_id = $pipe->iface_id();
    $self->pipes()->{$iface_id} = $pipe;
}

sub addIface($$)
{
    my ($self, $iface) = @_;
892
    push(@{$self->ifacelist()}, $iface);
Leigh B Stoller's avatar
Leigh B Stoller committed
893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921
    $self->ifaces()->{$iface->client_id()} = $iface;
}
sub addService($$)
{
    my ($self, $service) = @_;

    push(@{ $self->services() }, $service);
    return $service;
}

#
# Process the XML element for a node.
#
sub addNode($$$)
{
    my ($class, $rspec, $noderef) = @_;

    my $client_id   = GeniXML::GetVirtualId($noderef);
    my $sliver_type = GeniXML::GetVirtualizationSubtype($noderef);
    my $exclusive   = GeniXML::GetExclusive($noderef);

    fatal("Node: missing client_id or sliver_type")
	if (! defined($client_id));

    # Yep, users do this more then you think.
    fatal("Node: duplicate node: $client_id")
	if ($rspec->getNode($client_id));
    
    $sliver_type = "raw" if (!defined($sliver_type));
922 923 924 925 926 927 928

    if (defined($exclusive)) {
	$exclusive = ($exclusive ? 1 : 0);
	if ($sliver_type =~ /^(raw-pc|raw)$/) {
	    $exclusive = undef;
	}
    }
Leigh B Stoller's avatar
Leigh B Stoller committed
929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950
    
    my $node = APT_Rspec::Node->new($rspec,
				    $client_id, $sliver_type, $exclusive);
    $rspec->addNode($node);

    #
    # Handle sliver_type special cases.
    #
    if ($sliver_type eq "delay") {
	#
	# We need the pipes. 
	#
	my @pipes = GeniXML::GetDelayPipes($noderef);
	if (!@pipes) {
	    fatal("No pipes for bridge/delay node");
	}
	foreach my $pipe (@pipes) {
	    my $delaypipe = APT_Rspec::DelayPipe->new($node, $pipe);
	    $node->addBridgePipe($delaypipe);
	}
    }

Leigh B Stoller's avatar
Leigh B Stoller committed
951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969
    if ($sliver_type eq "firewall") {
	#
	# We need the firewall section from sliver_type
	#
	my $config = GeniXML::GetExperimentFirewallSettings($noderef);
	if (!$config) {
	    fatal("No configuration for firewall node");
	}
	$node->{'isexptfirewall'} = 1;
	$node->{'firewall_style'} = $config->{'style'};

	foreach my $rule (@{$config->{'rules'}}) {
	    # Trim for rtecheck.
	    $rule =~ s/^\s+//;
	    $rule =~ s/\s+$//s;
	    push(@{ $node->{'firewall_rules'}}, $rule);
	}
    }

Leigh B Stoller's avatar
Leigh B Stoller committed
970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992
    #
    # Scan all the attributes. 
    #
    foreach my $attr ($noderef->attributes()) {
	my $key = $attr->nodeName;
	my $val = $attr->getValue();

        SWITCH: for (lc($key)) {
	    /^sliver_id$/i && do {
		# Manifest crap.
		last SWITCH;
	    };
	    /^(client_id|exclusive|xmlns(:.*)?)$/i && do {
		last SWITCH;
	    };
	    /^component_id$/i && do {
		$node->{'component_id'} = $val;
		last SWITCH;
	    };
	    /^component_manager_id$/i && do {
		$node->{'component_manager_id'} = $val;
		last SWITCH;
	    };
Leigh B Stoller's avatar
Leigh B Stoller committed
993 994 995 996
	    /^(\w+:)?routable_control_ip$/i && $rspec->permissive() && do {
		# Ignore, it would not work anyway.
		last SWITCH;
	    };
Leigh B Stoller's avatar
Leigh B Stoller committed
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 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 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
	    fatal("Unknown attribute for node $client_id: $key,$val");
	}
    }

    #
    # Handle services.
    #
    my $process_Services = sub {
	my ($ref) = @_;

	foreach my $child ($ref->nonBlankChildNodes()) {
	    my ($ns,$name) = split(":", $child->nodeName);
	    $name = $ns if (!defined($name));
	
	    SWITCH: for (lc($name)) {
		/^#(comment|text)$/i && do {
		    # Maybe we should do something comment lines.
		    last SWITCH;
		};
		/^install$/i && do {
		    my $path = GetText("install_path", $child);
		    my $url  = GetText("url", $child);
		    if (!defined($path) && !defined($url)) {
			# We see a lot of empty install services. Skip.
			last SWITCH;
		    }
		    $node->addService(APT_Rspec::Service->new($node, $child));
		    last SWITCH;
		};
		/^execute$/i && do {
		    my $cmd = GetText("command", $child);
		    my $sh  = GetText("shell", $child);
		    if (!defined($cmd) && !defined($sh)) {
			# We see a lot of empty execute services. Skip.
			last SWITCH;
		    }
		    $node->addService(APT_Rspec::Service->new($node, $child));
		    last SWITCH;
		};
		/^program-agent$/i && do {
		    $node->addService(APT_Rspec::Service->new($node, $child));
		    last SWITCH;
		};
		/^(login|console)$/i && do {
		    # Manifest crap.
		    last SWITCH;
		};
		fatal("Unknown service for $client_id: $name");
	    }
	}
    };

    #
    # Processing functions for various elements.
    #
    my $process_SliverType = sub {
	my ($ref) = @_;

	foreach my $child ($ref->nonBlankChildNodes()) {
	    my ($ns,$name) = split(":", $child->nodeName);
	    $name = $ns if (!defined($name));

	    # Ignore old flack elements.
	    next
		if (defined($ns) && $ns =~/^(flack|history)$/);

	    SWITCH: for (lc($name)) {
		/^#(comment|text)$/i && do {
		    # Maybe we should do something comment lines.
		    last SWITCH;
		};
		/^disk_image$/i && do {
		    my $name  = GetText("name", $child);
		    my $url   = GetText("url", $child);
		    my $image = $name || $url;
		    if (!defined($image)) {
			fatal("No name or url for disk_image on $client_id");
		    }
		    if (GeniHRN::IsValid($image)) {
			my ($a,$b,$c,$d) = GeniHRN::ParseImage($image);
			if (!defined($a)) {
			    fatal("disk image for $client_id is not ".
				  "a valid image URN: $image");
			}
		    }
		    elsif ($image !~ /^http/) {
			fatal("disk image for $client_id is not ".
			      "a URN or URL: $image");
		    }
		    $node->{'disk_image'} = $image;
		    last SWITCH;
		};
		/^xen_ptype$/i && do {
		    $node->{"xen_ptype"} = GetTextOrFail("name", $child);
		    last SWITCH;
		};
		# XEN settings
		/^xen$/i && do {
		    my $settings = GeniXML::GetXenSettings($ref->parentNode);
		    fatal("Failed to get xen settings")
1097 1098 1099 1100
			if (!defined($settings));
		    if ($node->slivertype() ne "emulab-xen" && $verbose) {
			print STDERR "*** $client_id is not a XEN node\n";
		    }
Leigh B Stoller's avatar
Leigh B Stoller committed
1101 1102 1103
		    $node->{"xen_settings"} = $settings;
		    last SWITCH;
		};
Leigh B Stoller's avatar
Leigh B Stoller committed
1104
		/^(sliver_type_shaping|firewall_config)$/i && do {
Leigh B Stoller's avatar
Leigh B Stoller committed
1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117
		    # We handled this above. 
		    last SWITCH;
		};
		fatal("Unknown sliver_type element for $client_id: $name");
	    }
	}
    };

    my $process_Interface = sub {
	my ($ref) = @_;
	#
	# Interfaces are simple.
	#
1118 1119 1120 1121
	my $client_id    = GetTextOrFail("client_id", $ref);
	my $ip           = GeniXML::GetIp($ref, $ref->parentNode);
	my $mask         = GeniXML::GetMask($ref, $ref->parentNode);
	my $component_id = GeniXML::GetNodeId($ref);
Leigh B Stoller's avatar
Leigh B Stoller committed
1122

1123 1124 1125
	if ($rspec->getIface($client_id)) {
	    fatal("Duplicate iface $client_id on node " . $node->client_id());
	}
1126 1127
	APT_Rspec::Iface->new($rspec, $node,
			      $client_id, $ip, $mask, $component_id);
Leigh B Stoller's avatar
Leigh B Stoller committed
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
    };

    #
    # Blockstores
    #
    my $process_Blockstore = sub {
	my ($ref) = @_;
	my $bsname;
	
	my $bs = APT_Rspec::Blockstore->new($rspec, $node, $ref);
	
	# XXX This is a remote blockstore node, we ignore the bsname.
	if ($sliver_type eq "emulab-blockstore") {
	    $bsname = $client_id;
	}
	elsif (!defined($bs->name())) {
	    fatal("Missing name for blockstore on node $client_id");
	}
	else {
	    $bsname = $bs->name();
	}
	$node->{'blockstores'}->{$bsname} = $bs;
    };
    
    #
    # Scan all the elements.
    #
    foreach my $child ($noderef->nonBlankChildNodes()) {
	my ($ns,$name) = split(":", $child->nodeName);
	$name = $ns if (!defined($name));

	# Ignore old flack elements.
	next
	    if (defined($ns) && lc($ns) eq "flack");

        SWITCH: for (lc($name)) {
	    /^#(comment|text)$/i && do {
		# Maybe we should do something comment lines.
		last SWITCH;
	    };
Leigh B Stoller's avatar
Leigh B Stoller committed
1168
	    /^(vnode|host|location)$/i && do {
Leigh B Stoller's avatar
Leigh B Stoller committed
1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187
		# Manifest crap.
		last SWITCH;
	    };
	    /^icon$/i && do {
		last SWITCH;
	    };
	    /^sliver_type$/i && do {
		&$process_SliverType($child);
		last SWITCH;
	    };
	    /^interface$/i && do {
		&$process_Interface($child);
		last SWITCH;
	    };
	    /^hardware_type$/i && do {
		my $hardware = GetTextOrFail("name", $child);
		$node->{"hardware_type"} = $hardware;
		last SWITCH;
	    };
Leigh B Stoller's avatar
Leigh B Stoller committed
1188 1189 1190 1191 1192
	    /^adb_target$/i && do {
		my $hardware = GetTextOrFail("target_id", $child);
		$node->{"adb_target"} = $hardware;
		last SWITCH;
	    };
Leigh B Stoller's avatar
Leigh B Stoller committed
1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206
	    /^site$/i && do {
		my $site = GetTextOrFail("id", $child);
		$node->{"jacks_site"} = $site;
		last SWITCH;
	    };
	    /^use_type_default_image$/i && do {
		my $enabled = GetTextOrFail("enabled", $child);
		$node->{"use_type_default_image"} = $enabled;
		last SWITCH;
	    };
	    /^routable_control_ip$/i && do {
		$node->{"routable_control_ip"} = 1;
		last SWITCH;
	    };
1207 1208 1209 1210 1211
	    /^failure_action$/i && do {
		my $action = GetTextOrFail("action", $child);
		$node->{"failure_action"} = $action;
		last SWITCH;
	    };
Leigh B Stoller's avatar
Leigh B Stoller committed
1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225
	    /^services$/i && do {
		&$process_Services($child);
		last SWITCH;
	    };
	    /^blockstore$/i && do {
		&$process_Blockstore($child);
		last SWITCH;
	    };
	    /^fd$/i && do {
		my $desire = GetTextOrFail("name", $child);
		my $weight = GetTextOrFail("weight", $child);
		$node->{"desires"}->{$desire} = $weight;
		last SWITCH;
	    };
Leigh B Stoller's avatar
Leigh B Stoller committed
1226 1227 1228 1229 1230 1231
	    /^node_attribute$/i && do {
		my $key   = GetTextOrFail("key", $child);
		my $value = GetTextOrFail("value", $child);
		$node->{"attributes"}->{$key} = $value;
		last SWITCH;
	    };
Leigh B Stoller's avatar
Leigh B Stoller committed
1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277
	    /^moonshot_chassis$/i && do {
		# Convert to a desire
		my $chassis = GetTextOrFail("chassis", $child);
		$node->{"desires"}->{"ms-chassis${chassis}"} = "1.0";
		last SWITCH;
	    };
	    /^relation$/i && do {
		my $type = GetTextOrFail("type", $child);
		my $id   = GetTextOrFail("client_id", $child);
		if ($type ne "instantiate_on") {
		    fatal("Unsupported relation on node $client_id");
		}
		$node->{"instantiate_on"} = $id;
		last SWITCH;
	    };
	    fatal("Unable to process XML element for node $client_id: $name");
	}
    }
    return $node;
}

#
# Compare two nodes, Return zero if they are equivalent.
#
sub Compare($$)
{
    my ($this, $that) = @_;
    my $client_id = $this->client_id();

    foreach my $key (keys(%{$this})) {
	my $val1 = $this->{$key};
	my $val2 = $that->{$key};

	#
	# Enumerate all fields to be certain this is not out of sync
	# with the definition above.
	#
        SWITCH: for (lc($key)) {
	    /^(client_id|exclusive)$/i && do {
		return 1
		    if (APT_Rspec::CompareValues("Node: $client_id: $key",
						 $val1, $val2));
		last SWITCH;
	    };
	    /^type$/i && do {
		# Fix up; raw == raw-pc.
1278 1279 1280 1281 1282 1283 1284 1285 1286
		$val1 = "raw-pc"
		    if (defined($val1) && "$val1" eq "raw");
		$val2 = "raw-pc"
		    if (defined($val2) && "$val2" eq "raw");
		# Fix up; default-vm == emulab-xen.
		$val1 = "emulab-xen"
		    if (defined($val1) && "$val1" eq "default-vm");
		$val2 = "emulab-xen"
		    if (defined($val2) && "$val2" eq "default-vm");
Leigh B Stoller's avatar
Leigh B Stoller committed
1287 1288 1289 1290 1291 1292 1293 1294
		
		return 1
		    if (APT_Rspec::CompareValues("Node: $client_id: $key",
						 $val1, $val2));
		last SWITCH;
	    };
	    (/^(component_id|component_manager_id|disk_image)$/i ||
	     /^(hardware_type|jacks_site|xen_ptype|instantiate_on)$/i ||
1295
	     /^(adb_target|failure_action)$/i ||
Leigh B Stoller's avatar
Leigh B Stoller committed
1296
	     /^(isexptfirewall|firewall_style)$/i ||
Leigh B Stoller's avatar
Leigh B Stoller committed
1297 1298 1299 1300 1301 1302
	     /^(use_type_default_image|routable_control_ip)$/i) && do {
		 return 1
		     if (APT_Rspec::CompareValues("Node: $client_id: $key",
						  $val1,$val2));
		 last SWITCH;
	    };
1303
	    /^(ifaces|ifacelist)$/i && do {
Leigh B Stoller's avatar
Leigh B Stoller committed
1304 1305 1306
		# Handled up above in CompareNodes.
		last SWITCH;
	    };
Leigh B Stoller's avatar
Leigh B Stoller committed
1307
	    /^(xen_settings|desires|pipes|blockstores|attributes)$/i && do {
Leigh B Stoller's avatar
Leigh B Stoller committed
1308 1309 1310 1311 1312 1313 1314 1315 1316 1317
		return 1
		    if (APT_Rspec::CompareHashes("Node: $client_id: $key",
						 $val1, $val2));
		last SWITCH;
	    };
	    /^services$/i && do {
		return 1
		    if (APT_Rspec::CompareLists("service", $val1, $val2));
		last SWITCH;
	    };
Leigh B Stoller's avatar
Leigh B Stoller committed
1318
	    /^(tag|statements|firewall_rules)$/i && do {
Leigh B Stoller's avatar
Leigh B Stoller committed
1319 1320
		last SWITCH;
	    };
1321
	    print STDERR "*** Node:Compare: Unknown attribute: $key\n";
Leigh B Stoller's avatar
Leigh B Stoller committed
1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334
	    return 1;
	}
    }
    return 0;
}

############################################################################
package APT_Rspec::Iface;
use Carp;
use English;
use GeniXML;
use GeniHRN;

1335
sub new($$$$$$$)
Leigh B Stoller's avatar
Leigh B Stoller committed
1336
{
1337
    my ($class, $rspec, $node, $client_id, $ip, $mask, $component_id) = @_;
Leigh B Stoller's avatar
Leigh B Stoller committed
1338 1339

    my $self = {
1340 1341 1342 1343 1344 1345 1346
	"client_id"    => $client_id,
	"node_id"      => $node->client_id(),
	"ip"           => $ip,
	"mask"         => $mask,
	"component_id" => $component_id,
	"statements"   => [],
	"tag"          => "iface" . scalar(keys(%{$rspec->ifaces()})),
Leigh B Stoller's avatar
Leigh B Stoller committed
1347 1348 1349 1350 1351 1352 1353 1354 1355 1356
    };
    bless($self, $class);
    $rspec->addIface($self);
    $node->addIface($self);
    return $self;
}
sub client_id($)		{ return $_[0]->{'client_id'}; }
sub node_id($)			{ return $_[0]->{'node_id'}; }
sub ip($)			{ return $_[0]->{'ip'}; }
sub mask($)			{ return $_[0]->{'mask'}; }
1357
sub component_id($)		{ return $_[0]->{'component_id'}; }
Leigh B Stoller's avatar
Leigh B Stoller committed
1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394
sub statements($)		{ return $_[0]->{'statements'}; }
sub tag($)			{ return $_[0]->{'tag'}; }

sub addStatement($$)
{
    my ($self, $statement) = @_;
    push(@{$self->{'statements'}}, $statement);
}
sub addTagStatement($$)
{
    my ($self, $statement) = @_;
    my $tag = $self->tag();
    addStatement($self, "${tag}.${statement}");
}

#
# Compare two ifaces. Return zero if they are equivalent.
#
sub Compare($$)
{
    my ($this, $that) = @_;
    my $client_id = $this->client_id();

    foreach my $key (keys(%{$this})) {
	my $val1 = $this->{$key};
	my $val2 = $that->{$key};

	#
	# Enumerate all fields to be certain this is not out of sync
	# with the definition above.
	#
        SWITCH: for (lc($key)) {
	    /^client_id$/i && do {
		# This was checked up above in CompareNodes/CompareLinks
		# cause of the alternate naming issue (node:id).
		last SWITCH;
	    };
1395
	    /^(node_id|ip|mask|component_id)$/i && do {
Leigh B Stoller's avatar
Leigh B Stoller committed
1396 1397 1398 1399 1400 1401 1402 1403
		return 1
		    if (APT_Rspec::CompareValues("Iface: $client_id: $key",
						 $val1,$val2));
		last SWITCH;
	    };
	    /^(tag|statements)$/i && do {
		last SWITCH;
	    };
1404
	    print STDERR "*** Iface:Compare: Unknown attribute: $key\n";
Leigh B Stoller's avatar
Leigh B Stoller committed
1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421
	    return 1;
	}
    }
    return 0;
}

############################################################################
package APT_Rspec::Link;
use Carp;
use English;
use GeniXML;
use GeniHRN;

sub new($$$$)
{
    my ($class, $rspec, $client_id, $type) = @_;

Leigh B Stoller's avatar
Leigh B Stoller committed
1422 1423 1424 1425
    #
    # Make up a tag (variable for the script) that makes some kind of sense.
    #
    my $tag = $client_id;
1426
    # Replace dashes/spaces/dots with underscores.
Leigh B Stoller's avatar
Leigh B Stoller committed
1427
    $tag =~ s/\-/\_/g;
1428 1429
    $tag =~ s/\ /\_/g;
    $tag =~ s/\./\_/g;
Leigh B Stoller's avatar
Leigh B Stoller committed
1430 1431 1432 1433
    if ($tag !~ /^link/i) {
	$tag = "link_${tag}";
    }
    
Leigh B Stoller's avatar
Leigh B Stoller committed
1434 1435 1436 1437 1438 1439 1440 1441 1442
    my $self = {
	"client_id"		=> $client_id,
	"type"			=> $type,
	"component_id"		=> undef,
	"component_manager_id"  => undef,
	"protocol"		=> undef,
	"best_effort"		=> undef,
	"shared_vlan"		=> undef,
	"force_shaping"		=> undef,
1443
	"force_nobwshaping"	=> undef,
Leigh B Stoller's avatar
Leigh B Stoller committed
1444 1445 1446 1447 1448 1449 1450 1451 1452
	"trivial_ok"		=> undef,
	"vlan_tagging"		=> undef,
	"link_multiplexing"	=> undef,
	"interswitch"		=> undef,
	"nomac_learning"        => undef,
	"openflow_controller"   => undef,
	"isbslink"		=> 0,
	"bsnode"		=> undef,
	"ifaces"		=> {},
1453
	"ifacelist"             => [],
Leigh B Stoller's avatar
Leigh B Stoller committed
1454 1455 1456 1457
	"properties"            => {},
	"component_managers"    => [],
	"jacks_site"		=> undef,
	"statements"		=> [],
Leigh B Stoller's avatar
Leigh B Stoller committed
1458
	"tag"			=> $tag,
Leigh B Stoller's avatar
Leigh B Stoller committed
1459 1460 1461 1462 1463 1464 1465 1466
    };
    bless($self, $class);
    return $self;
}
sub client_id($)		{ return $_[0]->{'client_id'}; }
sub type($)			{ return $_[0]->{'type'}; }
sub statements($)		{ return $_[0]->{'statements'}; }
sub ifaces($)			{ return $_[0]->{'ifaces'}; }
1467
sub ifacelist($)		{ return $_[0]->{'ifacelist'}; }
Leigh B Stoller's avatar
Leigh B Stoller committed
1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487
sub properties($)		{ return $_[0]->{'properties'}; }
sub tag($)			{ return $_[0]->{'tag'}; }
sub fatal($)			{ return APT_Rspec::fatal($_[0]); }
sub GetTextOrFail($$)		{ return APT_Rspec::GetTextOrFail(@_); }

sub addStatement($$)
{
    my ($self, $statement) = @_;
    push(@{$self->{'statements'}}, $statement);
}
sub addTagStatement($$)
{
    my ($self, $statement) = @_;
    my $tag = $self->tag();
    addStatement($self, "${tag}.${statement}");
}
sub addIface($$)
{
    my ($self, $iface) = @_;
    my $iface_id = $iface->client_id();
1488
    push(@{$self->ifacelist()}, $iface);
Leigh B Stoller's avatar
Leigh B Stoller committed
1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527
    $self->ifaces()->{$iface_id} = $iface;
}

sub addLink($$$)
{
    my ($class, $rspec, $linkref)   = @_;
    my $client_id   = GeniXML::GetVirtualId($linkref);
    my $linktype    = GeniXML::LinkType($linkref);
    
    fatal("Link: missing client_id")
	if (! defined($client_id));
    
    my $link = APT_Rspec::Link->new($rspec, $client_id, $linktype);
    $rspec->addLink($link);

    #
    # Scan all the attributes. 
    #
    foreach my $attr ($linkref->attributes()) {
	my $key = $attr->nodeName;
	my $val = $attr->getValue();

        SWITCH: for (lc($key)) {
	    (/^(client_id|link_type|icon)$/i ||
	     /^xmlns(:.*)?$/i) && do {
		 last SWITCH;
	    };
	    /^component_id$/i && do {
		$link->{'component_id'} = $val;
		last SWITCH;
	    };
	    /^component_manager_id$/i && do {
		$link->{'component_manager_id'} = $val;
		last SWITCH;
	    };
	    /^protocol$/i && do {
		$link->{'protocol'} = $val;
		last SWITCH;
	    };
1528 1529 1530 1531
	    /^vlantag$/i && do {
		$link->{'vlantag'} = $val;
		last SWITCH;
	    };
Leigh B Stoller's avatar
Leigh B Stoller committed
1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627