APT_Rspec.pm.in 54.3 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 46 47 48 49 50 51 52
#!/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@";

# Protos;
sub CompareHashes($$$);
sub CompareLists($$$);

#
# Parse an rspec into nice perl things.
#
Leigh B Stoller's avatar
Leigh B Stoller committed
53
sub new($$;$)
Leigh B Stoller's avatar
Leigh B Stoller committed
54
{
Leigh B Stoller's avatar
Leigh B Stoller committed
55
    my ($class, $rspecfile, $permissive) = @_;
Leigh B Stoller's avatar
Leigh B Stoller committed
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
    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"	            => {},
79
	"nodelist"          => [],
Leigh B Stoller's avatar
Leigh B Stoller committed
80 81
	"ifaces"            => {},
	"links"             => {},
82 83
	"linklist"          => [],
	"toplevel_elements" => [],  # Use attributes for toplevel elements
Leigh B Stoller's avatar
Leigh B Stoller committed
84 85 86
	"tour"              => undef,
	"namespaces"        => \%namespaces,
	"bscounter"         => 0,
Leigh B Stoller's avatar
Leigh B Stoller committed
87
	"permissive"        => (defined($permissive) ? $permissive : 0),
Leigh B Stoller's avatar
Leigh B Stoller committed
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
    };
    bless($self, $class);

    #
    # 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
106
		    # Trim for rtecheck.
Leigh B Stoller's avatar
Leigh B Stoller committed
107
		    $text =~ s/^\s+//;
108
		    $text =~ s/\s+$//s;
Leigh B Stoller's avatar
Leigh B Stoller committed
109
		    $text =~ s/\"/\\"/g;
110
		    $text =~ s/\t/\\t/g;
Leigh B Stoller's avatar
Leigh B Stoller committed
111 112 113 114 115 116 117 118 119
		    $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
120
		    # Trim for rtecheck.
Leigh B Stoller's avatar
Leigh B Stoller committed
121
		    $text =~ s/^\s+//;
122
		    $text =~ s/\s+$//s;
123 124 125
		    # This happens a lot.
		    if ($text ne "") {
			$text =~ s/\"/\\"/g;
126
			$text =~ s/\t/\\t/g;
127 128 129 130 131
			$tour->{'instructions'} = {
			    "text" => $text,
			    "type" => $type,
			};
		    }
Leigh B Stoller's avatar
Leigh B Stoller committed
132 133
		    last SWITCH;
		};
Leigh B Stoller's avatar
Leigh B Stoller committed
134 135 136 137 138 139 140 141
		/^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");
			}
142 143 144 145 146
			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
147 148 149 150 151
			# Kill terminating newline for rtecheck
			chomp($text);
			push(@steps, {
			    "type" => $type,
			    "id"   => $id,
152 153
			    "description" => $text,
			    "description_type" => $dtype});
Leigh B Stoller's avatar
Leigh B Stoller committed
154 155 156 157 158 159 160
		    }
		    # We see a lot of empty steps
		    $tour->{'steps'} = \@steps
			if (scalar(@steps));
		    last SWITCH;
		};
		fatal("Unknown element in rspec_tour: $name");
Leigh B Stoller's avatar
Leigh B Stoller committed
161 162 163 164
	    };
	}
	return
	    if (! (exists($tour->{"description"}) ||
Leigh B Stoller's avatar
Leigh B Stoller committed
165 166
		   exists($tour->{"instructions"}) ||
		   exists($tour->{"steps"})));
Leigh B Stoller's avatar
Leigh B Stoller committed
167 168 169 170 171 172 173 174 175 176 177 178 179 180
	
	$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
181 182
		my $passname = GetTextOrFail("name", $ref);
		APT_Rspec::Attribute->new($self, $name, $passname);
Leigh B Stoller's avatar
Leigh B Stoller committed
183 184 185
		last SWITCH;
	    };
	    /^routable_pool$/i && do {
186 187
		APT_Rspec::Attribute->new($self, $name,
					  APT_Rspec::Pool->new($ref));
Leigh B Stoller's avatar
Leigh B Stoller committed
188 189 190 191
		last SWITCH;
	    };
	    /^collocate_factor$/i && do {
		my $count = GetTextOrFail("count", $ref);
192
		APT_Rspec::Attribute->new($self, $name, $count);
Leigh B Stoller's avatar
Leigh B Stoller committed
193 194 195 196 197 198 199
		last SWITCH;
	    };
	    /^packing_strategy$/i && do {
		my $strategy = GetTextOrFail("strategy", $ref);
		if ($strategy !~ /^(pack|balance)$/) {
		    fatal("packing_strategy: bad strategy: $strategy");
		}
200
		APT_Rspec::Attribute->new($self, $name, $strategy);
Leigh B Stoller's avatar
Leigh B Stoller committed
201 202 203 204
		last SWITCH;
	    };
	    /^routing_style$/i && do {
		my $style = GetTextOrFail("style", $ref);
205
		APT_Rspec::Attribute->new($self, $name, $style);
Leigh B Stoller's avatar
Leigh B Stoller committed
206 207 208 209
		last SWITCH;
	    };
	    /^delay_image$/i && do {
		my $urn = GetTextOrFail("urn", $ref);
210
		APT_Rspec::Attribute->new($self, $name, $urn);
Leigh B Stoller's avatar
Leigh B Stoller committed
211 212 213 214 215 216 217 218 219 220 221 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
		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
252
	     /^(\w+:)?packing_strategy$/i ||
Leigh B Stoller's avatar
Leigh B Stoller committed
253 254 255 256 257 258 259
	     /^(\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
260 261
		fatal("data_set element is not supported")
		    if (!$self->permissive());
Leigh B Stoller's avatar
Leigh B Stoller committed
262 263 264
		last SWITCH;
	    };
	    /^(\w+:)?profile_parameters$/i && do {
Leigh B Stoller's avatar
Leigh B Stoller committed
265 266
		fatal("profile_parameters element is not supported")
		    if (!$self->permissive());
Leigh B Stoller's avatar
Leigh B Stoller committed
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
		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'}; }
292 293
sub nodelist($)			{ return $_[0]->{'nodelist'}; }
sub linklist($)			{ return $_[0]->{'linklist'}; }
Leigh B Stoller's avatar
Leigh B Stoller committed
294 295
sub ifaces($)			{ return $_[0]->{'ifaces'}; }
sub tour($)			{ return $_[0]->{'tour'}; }
296 297
sub attributes($)		{ return $_[0]->{'toplevel_elements'}; }
sub toplevel_elements($)	{ return $_[0]->attributes(); }
Leigh B Stoller's avatar
Leigh B Stoller committed
298
sub permissive($)		{ return $_[0]->{'permissive'}; }
Leigh B Stoller's avatar
Leigh B Stoller committed
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318

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
319 320 321 322 323 324 325 326 327 328
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
329 330 331
sub addNode($$)
{
    my ($self, $node) = @_;
332 333
    # Maintain rspec ordering for RTE checks.
    push(@{$self->nodelist()}, $node);
Leigh B Stoller's avatar
Leigh B Stoller committed
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
    $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) = @_;
360 361
    # Maintain rspec ordering for RTE checks.
    push(@{$self->linklist()}, $link);
Leigh B Stoller's avatar
Leigh B Stoller committed
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 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
    $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));
Leigh B Stoller's avatar
Leigh B Stoller committed
415
    print STDERR "$text: '$val1' != '$val2'\n";
Leigh B Stoller's avatar
Leigh B Stoller committed
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461
    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";
	}
	print STDERR "${text}: $msg\n";
	return 1;
    }
    foreach my $key (sort(keys(%$h1))) {
	if (!exists($h2->{$key})) {
	    print STDERR "$text: $key in rspec one but not rspec two\n";
	    return 1;
	}
	my $v1 = $h1->{$key};
	my $v2 = $h2->{$key};
	if (ref($v1) ne ref($v2)) {
	    print STDERR "$text: key $key typeof mismatch\n";
	    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
462 463 464 465
	elsif (ref($v1) eq "ARRAY") {
	    return 1
		if (CompareLists("$text: $key", $v1, $v2));
	}
Leigh B Stoller's avatar
Leigh B Stoller committed
466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487
	# Hmm, we assume all blessed refs are our own and have compare methods.
	elsif (blessed($v1)) {
	    return 1
		if ($v1->Compare($v2));
	}
	else {
	    print STDERR "$text: do not know how to compare $key\n";
	    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
488
    #print STDERR "CompareLists: $text\n";
Leigh B Stoller's avatar
Leigh B Stoller committed
489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560

    if (scalar(@l1) != scalar(@l2)) {
	print STDERR "$text: rspec one has a different count then rspec two\n";
	return 1;
    }
    while (@l1) {
	my $v1 = pop(@l1);
	my $v2 = pop(@l2);

	if (ref($v1) ne ref($v2)) {
	    print STDERR "$text: typeof mismatch\n";
	    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 {
	    print STDERR "$text: do not know how to compare.\n";
	    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
561
		    if (APT_Rspec::CompareHashes($key, $val1, $val2));
Leigh B Stoller's avatar
Leigh B Stoller committed
562 563
		last SWITCH;
	    };
Leigh B Stoller's avatar
Leigh B Stoller committed
564
	    /^(bscounter|namespaces|nodelist|linklist|permissive)$/i && do {
Leigh B Stoller's avatar
Leigh B Stoller committed
565 566 567 568 569 570 571 572 573 574
		last SWITCH;
	    };
	    print STDERR "Rspec:Compare: Unknown attribute: $key\n";
	    return 1;
	}
    }
    return 0;
}

#
575
# Compare top level elements. These are lists.
Leigh B Stoller's avatar
Leigh B Stoller committed
576 577 578 579 580
#
sub CompareToplevelElements($$)
{
    my ($elements1, $elements2) = @_;

581 582 583 584 585 586 587 588 589 590
    if (scalar(@{$elements2}) != scalar(@{$elements2})) {
	print STDERR "Rspec:CompareToplevelElements: mismatching length\n";
	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
591 592 593 594 595 596 597 598 599 600 601 602 603 604 605

	#
	# 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
606
		    if (APT_Rspec::CompareValues($key, $val1, $val2));
Leigh B Stoller's avatar
Leigh B Stoller committed
607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 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 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730
		last SWITCH;
	    };
	    print STDERR "Rspec:CompareToplevelElements: unknown $key\n";
	    return -1;
	}
    }
    return 0;
}

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

    if (scalar(values(%{$l1})) != scalar(values(%{$l2}))) {
	print STDERR "different number of nodes\n";
	return 1;
    }

    foreach my $client_id (sort(keys(%$l1))) {
	if (!exists($l2->{$client_id})) {
	    print STDERR "Node $client_id in rspec one but not rspec two\n";
	    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}))) {
	    print STDERR "different number of ifaces on Node $client_id\n";
	    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 {
		    print STDERR "iface $iface_id on node $client_id ".
			"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}))) {
	print STDERR "different number of links\n";
	return 1;
    }

    foreach my $client_id (sort(keys(%$l1))) {
	if (!exists($l2->{$client_id})) {
	    print STDERR "Link $client_id in rspec one but not rspec two\n";
	    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}))) {
	    print STDERR "different number of ifaces on link $client_id\n";
	    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 {
731
		    #print STDERR "$altid\n";
Leigh B Stoller's avatar
Leigh B Stoller committed
732 733 734 735 736 737 738 739 740 741 742 743 744 745 746
		    print STDERR "iface $iface_id in link $client_id ".
			"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;
}

747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772
############################################################################
# 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
773 774 775 776 777 778 779 780 781 782 783
############################################################################
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
784 785 786 787
    #
    # Make up a tag (variable for the script) that makes some kind of sense.
    #
    my $tag = $client_id;
788
    # Replace dashes/spaces/dots with underscores.
Leigh B Stoller's avatar
Leigh B Stoller committed
789
    $tag =~ s/\-/\_/g;
790 791
    $tag =~ s/\ /\_/g;
    $tag =~ s/\./\_/g;
Leigh B Stoller's avatar
Leigh B Stoller committed
792 793 794 795
    if ($tag !~ /^node/i) {
	$tag = "node_${tag}";
    }

Leigh B Stoller's avatar
Leigh B Stoller committed
796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811
    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"		=> {},
	"ifaces"		=> {},
812
	"ifacelist"             => [],
Leigh B Stoller's avatar
Leigh B Stoller committed
813 814 815 816
	"blockstores"		=> {},
	"pipes"			=> {},
	"use_type_default_image"=> undef,
	"routable_control_ip"   => undef,
817
	"failure_action"        => undef,
Leigh B Stoller's avatar
Leigh B Stoller committed
818
	"tag"			=> $tag,
Leigh B Stoller's avatar
Leigh B Stoller committed
819 820 821 822 823 824 825 826 827 828
    };
    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'}; }
829
sub ifacelist($)		{ return $_[0]->{'ifacelist'}; }
Leigh B Stoller's avatar
Leigh B Stoller committed
830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855
sub services($)			{ return $_[0]->{'services'}; }
sub pipes($)			{ return $_[0]->{'pipes'}; }
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) = @_;
856
    push(@{$self->ifacelist()}, $iface);
Leigh B Stoller's avatar
Leigh B Stoller committed
857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876
    $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);
877 878 879
    if (defined($exclusive)) {
	$exclusive = ($exclusive ? 1 : 0);
    }
Leigh B Stoller's avatar
Leigh B Stoller committed
880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933

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

    #
    # 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
934 935 936 937
	    /^(\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
938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 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
	    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")
			if (!defined($settings));		    
		    $node->{"xen_settings"} = $settings;
		    last SWITCH;
		};
		/^sliver_type_shaping$/i && do {
		    # We handled this above. 
		    last SWITCH;
		};
		fatal("Unknown sliver_type element for $client_id: $name");
	    }
	}
    };

    my $process_Interface = sub {
	my ($ref) = @_;
	#
	# Interfaces are simple.
	#
1056 1057 1058 1059
	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
1060

1061 1062
	APT_Rspec::Iface->new($rspec, $node,
			      $client_id, $ip, $mask, $component_id);
Leigh B Stoller's avatar
Leigh B Stoller committed
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 1097 1098 1099 1100 1101 1102
    };

    #
    # 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
1103
	    /^(vnode|host|location)$/i && do {
Leigh B Stoller's avatar
Leigh B Stoller committed
1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122
		# 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
1123 1124 1125 1126 1127
	    /^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
1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141
	    /^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;
	    };
1142 1143 1144 1145 1146
	    /^failure_action$/i && do {
		my $action = GetTextOrFail("action", $child);
		$node->{"failure_action"} = $action;
		last SWITCH;
	    };
Leigh B Stoller's avatar
Leigh B Stoller committed
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 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216
	    /^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;
	    };
	    /^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.
		$val1 = "raw-pc" if (defined($val1) && "$val1" eq "raw");
		$val2 = "raw-pc" if (defined($val2) && "$val2" eq "raw");
		
		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 ||
1217
	     /^(adb_target|failure_action)$/i ||
Leigh B Stoller's avatar
Leigh B Stoller committed
1218 1219 1220 1221 1222 1223
	     /^(use_type_default_image|routable_control_ip)$/i) && do {
		 return 1
		     if (APT_Rspec::CompareValues("Node: $client_id: $key",
						  $val1,$val2));
		 last SWITCH;
	    };
1224
	    /^(ifaces|ifacelist)$/i && do {
Leigh B Stoller's avatar
Leigh B Stoller committed
1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255
		# Handled up above in CompareNodes.
		last SWITCH;
	    };
	    /^(xen_settings|desires|pipes|blockstores)$/i && do {
		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;
	    };
	    /^(tag|statements)$/i && do {
		last SWITCH;
	    };
	    print STDERR "Node:Compare: Unknown attribute: $key\n";
	    return 1;
	}
    }
    return 0;
}

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

1256
sub new($$$$$$$)
Leigh B Stoller's avatar
Leigh B Stoller committed
1257
{
1258
    my ($class, $rspec, $node, $client_id, $ip, $mask, $component_id) = @_;
Leigh B Stoller's avatar
Leigh B Stoller committed
1259 1260

    my $self = {
1261 1262 1263 1264 1265 1266 1267
	"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
1268 1269 1270 1271 1272 1273 1274 1275 1276 1277
    };
    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'}; }
1278
sub component_id($)		{ return $_[0]->{'component_id'}; }
Leigh B Stoller's avatar
Leigh B Stoller committed
1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315
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;
	    };
1316
	    /^(node_id|ip|mask|component_id)$/i && do {
Leigh B Stoller's avatar
Leigh B Stoller committed
1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342
		return 1
		    if (APT_Rspec::CompareValues("Iface: $client_id: $key",
						 $val1,$val2));
		last SWITCH;
	    };
	    /^(tag|statements)$/i && do {
		last SWITCH;
	    };
	    print STDERR "Iface:Compare: Unknown attribute: $key\n";
	    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
1343 1344 1345 1346
    #
    # Make up a tag (variable for the script) that makes some kind of sense.
    #
    my $tag = $client_id;
1347
    # Replace dashes/spaces/dots with underscores.
Leigh B Stoller's avatar
Leigh B Stoller committed
1348
    $tag =~ s/\-/\_/g;
1349 1350
    $tag =~ s/\ /\_/g;
    $tag =~ s/\./\_/g;
Leigh B Stoller's avatar
Leigh B Stoller committed
1351 1352 1353 1354
    if ($tag !~ /^link/i) {
	$tag = "link_${tag}";
    }
    
Leigh B Stoller's avatar
Leigh B Stoller committed
1355 1356 1357 1358 1359 1360 1361 1362 1363
    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,
1364
	"force_nobwshaping"	=> undef,
Leigh B Stoller's avatar
Leigh B Stoller committed
1365 1366 1367 1368 1369 1370 1371 1372 1373
	"trivial_ok"		=> undef,
	"vlan_tagging"		=> undef,
	"link_multiplexing"	=> undef,
	"interswitch"		=> undef,
	"nomac_learning"        => undef,
	"openflow_controller"   => undef,
	"isbslink"		=> 0,
	"bsnode"		=> undef,
	"ifaces"		=> {},
1374
	"ifacelist"             => [],
Leigh B Stoller's avatar
Leigh B Stoller committed
1375 1376 1377 1378
	"properties"            => {},
	"component_managers"    => [],
	"jacks_site"		=> undef,
	"statements"		=> [],
Leigh B Stoller's avatar
Leigh B Stoller committed
1379
	"tag"			=> $tag,
Leigh B Stoller's avatar
Leigh B Stoller committed
1380 1381 1382 1383 1384 1385 1386 1387
    };
    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'}; }
1388
sub ifacelist($)		{ return $_[0]->{'ifacelist'}; }
Leigh B Stoller's avatar
Leigh B Stoller committed
1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408
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();
1409
    push(@{$self->ifacelist()}, $iface);
Leigh B Stoller's avatar
Leigh B Stoller committed
1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 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 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 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
    $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;
	    };
	    /^sliver_id$/i && do {
		# Manifest crap.
		last SWITCH;
	    };
	    fatal("Unknown attribute for link $client_id: $key,$val");
	}
    }

    #
    # Interfaces
    #
    # Blockstore special case; Kirk would prefer not to have IP
    # addresses show through since they are ignored. So first go
    # through and see if any of the nodes is a blockstore, and if they
    # are, kill the IP address in all the interfaces.
    #
    my $isbslink = 0;
    
    foreach my $ifaceref (GeniXML::FindNodes("n:interface_ref",
					     $linkref)->get_nodelist()) {
	my $virtual_id = GeniXML::GetInterfaceId($ifaceref);
	fatal("No client_id for interface in link $client_id")
	    if (!defined($virtual_id));
	
	my $iface = $rspec->getIface($virtual_id);
	fatal("No node iface found for $virtual_id in $client_id")
	    if (!defined($iface));
	my $node  = $rspec->getNode($iface->{'node_id'});
	fatal("No node found for $virtual_id in $client_id")
	    if (!defined($node));

	#
	# geni-lib is going to name the interfaces as node_id:iface_id
	# so we have to careful to strip existing node_id from the id
	# in case the rspec came from a geni-lib script.
	#
	my ($tmp,$iface_id) = split(":", $virtual_id);
	$iface_id = $tmp if (!defined($iface_id));

	if ($node->{'type'} eq "emulab-blockstore") {
	    $link->{'isbslink'} = $isbslink = 1;
	    $link->{'bsnode'}   = $node;
	}
	$link->addIface($iface);
    }
    if ($isbslink) {
	#
	# Kirk would like the IPs to not show. 
	#
	foreach my $iface (values(%{$link->{'ifaces'}})) {
	    $iface->{'ip'}   = undef;
	    $iface->{'mask'} = undef;
	}
    }

    #
    # Shaping.
    #
    my @properties = GeniXML::GetLinkProperties($linkref);
    if (@properties) {
	foreach my $property (@properties) {
	    my $linkproperty =
		APT_Rspec::LinkProperty->new($rspec, $link, $property);

	    # Empty properties seems to happen a lot.
	    next
		if (!defined($linkproperty));
	    
	    #
	    # Note that geni-lib does not support asymmetric properties,
	    # while rspecs do. This will be a problem.
	    #
	    my $source = $linkproperty->source();
	    my $dest   = $linkproperty->dest();
	    #
	    # For the array key, convert source/dest to node_id:iface_id
	    # if not already in that format. Makes it easier to compare the
	    # two hashes.
	    #
	    if ($source !~ /^([^:]*):([^:]*)$/) {
		$source = $linkproperty->source_node_id() . ":" . $source;
	    }
	    # geni-lib lans set the dest to the link_id.
	    if ($dest ne $client_id && $dest !~ /^([^:]*):([^:]*)$/) {
		$dest = $linkproperty->dest_node_id() . ":" . $dest;
	    }
	    $link->{'properties'}->{"${source}##${dest}"} = $linkproperty;
	}
    }

    #
    # Scan all the elements.
    #
    foreach my $child ($linkref->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$/i && do {
		my $site = GetTextOrFail("id", $child);
		$link->{"jacks_site"} = $site;
		last SWITCH;
	    };
	    /^icon$/i && do {
		last SWITCH;
	    };
	    /^#(comment|text)$/i && do {
		# Maybe we should do something comment lines.
		last SWITCH;
	    };
	    /^component_manager$/i && do {
		my $urn = GetTextOrFail("name", $child);
		if (!GeniHRN::IsValid($urn)) {
		    fatal("component manager for $client_id ".
			  "is not a valid URN: $urn");
		}
		push(@{$link->{'component_managers'}}, $urn);
		last SWITCH;
	    };
	    /^(property|interface_ref|link_type)$/i && do {
		# Did these above.
		last SWITCH;
	    };
	    /^best_effort$/i && do {
		$link->{'best_effort'} = 1;
		last SWITCH;
	    };
	    /^link_shared_vlan$/i && do {
		my $name = GetTextOrFail("name", $child);
		$link->{'shared_vlan'} = $name;
		last SWITCH;
	    };
	    /^force_shaping$/i && do {
		$link->{'force_shaping'} = 1;
		last SWITCH;
	    };
1589 1590 1591 1592
	    /^force_nobwshaping$/i && do {
		$link->{'force_nobwshaping'} = 1;
		last SWITCH;
	    };
Leigh B Stoller's avatar
Leigh B Stoller committed
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 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665
	    /^trivial_ok$/i && do {
		$link->{'trivial_ok'} = 1;
		last SWITCH;
	    };
	    /^vlan_tagging$/i && do {
		my $enabled = GetTextOrFail("enabled", $child);
		$link->{'vlan_tagging'} = (lc($enabled) eq "true" ? 1 : 0);
		last SWITCH;
	    };
	    /^link_multiplexing$/i && do {
		my $enabled = GetTextOrFail("enabled", $child);
		$link->{'link_multiplexing'} = (lc($enabled) eq "true" ? 1 : 0);
		last SWITCH;
	    };
	    /^interswitch$/i && do {
		my $allow = GetTextOrFail("allow", $child);
		$link->{'interswitch'} = (lc($allow) eq "true" ? 1 : 0);
		last SWITCH;
	    };
	    /^link_attribute$/i && do {
		my $key = GetTextOrFail("key", $child);
		my $val = GetTextOrFail("value", $child);
		# This is only attribute we grok.
		if ($key ne "nomac_learning") {
		    fatal("Unknown link_attribute for $client_id: $key");
		}
		if (lc($val) ne "false") {
		    $link->{'nomac_learning'} = 1;
		}
		last SWITCH;
	    };
	    /^(controller|openflow_controller)$/i && do {
		my $url = GetTextOrFail("url", $child);
		if ($url =~ /^tcp:(\d+\.\d+\.\d+\.\d+):(\d+)$/) {
		    $link->{'openflow_controller'} = {
			"host" => $1, "port" => $2};
		}
		else {
		    fatal("Bad openflow controller for link $client_id: $url");
		}
		last SWITCH;
	    };
	    fatal("Unable to process XML element for link $client_id: $name");
	}
    }
    return $link;
}

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

    return 1
	if (CompareProperties($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)) {
	    (/^(client_id|protocol)$/i ||
	     /^(component_id|component_manager_id)$/i ||
	     /^(best_effort|shared_vlan|force_shaping|trivial_ok)$/i ||
	     /^(vlan_tagging|link_multiplexing|interswitch)$/i ||
	     /^(vlan_tagging|link_multiplexing|interswitch)$/i ||
1666
	     /^(nomac_learning|jacks_site|force_nobwshaping)$/i) && do {
Leigh B Stoller's avatar
Leigh B Stoller committed
1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681
		 return 1
		     if (APT_Rspec::CompareValues("Link: $client_id: $key",
						  $val1, $val2));
		 last SWITCH;
   	    };
	    /^openflow_controller$/i && do {
		 return 1
		     if (APT_Rspec::CompareHashes("Link: $client_id: $key",
						  $val1, $val2));
		last SWITCH;
	    };
	    /^type$/i && do {
		# See below.
		last SWITCH;
	    };
1682
	    /^(ifaces|ifacelist)$/i && do {
Leigh B Stoller's avatar
Leigh B Stoller committed
1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757