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

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 Stoller's avatar
Leigh Stoller committed
315 316 317 318 319 320 321 322 323 324
sub steps($)
{
    my ($self) = @_;

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

Leigh Stoller's avatar
Leigh Stoller committed
325 326 327
sub addNode($$)
{
    my ($self, $node) = @_;
328 329
    # Maintain rspec ordering for RTE checks.
    push(@{$self->nodelist()}, $node);
Leigh Stoller's avatar
Leigh Stoller committed
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
    $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) = @_;
356 357
    # Maintain rspec ordering for RTE checks.
    push(@{$self->linklist()}, $link);
Leigh Stoller's avatar
Leigh Stoller committed
358 359 360 361 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
    $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 Stoller's avatar
Leigh Stoller committed
411
    print STDERR "$text: '$val1' != '$val2'\n";
Leigh Stoller's avatar
Leigh Stoller committed
412 413 414 415 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
    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 Stoller's avatar
Leigh Stoller committed
458 459 460 461
	elsif (ref($v1) eq "ARRAY") {
	    return 1
		if (CompareLists("$text: $key", $v1, $v2));
	}
Leigh Stoller's avatar
Leigh Stoller committed
462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
	# 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};

484
    #print STDERR "CompareLists: $text\n";
Leigh Stoller's avatar
Leigh Stoller committed
485 486 487 488 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

    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 Stoller's avatar
Leigh Stoller committed
557
		    if (APT_Rspec::CompareHashes($key, $val1, $val2));
Leigh Stoller's avatar
Leigh Stoller committed
558 559
		last SWITCH;
	    };
560
	    /^(bscounter|namespaces|nodelist|linklist|permissive)$/i && do {
Leigh Stoller's avatar
Leigh Stoller committed
561 562 563 564 565 566 567 568 569 570
		last SWITCH;
	    };
	    print STDERR "Rspec:Compare: Unknown attribute: $key\n";
	    return 1;
	}
    }
    return 0;
}

#
571
# Compare top level elements. These are lists.
Leigh Stoller's avatar
Leigh Stoller committed
572 573 574 575 576
#
sub CompareToplevelElements($$)
{
    my ($elements1, $elements2) = @_;

577 578 579 580 581 582 583 584 585 586
    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 Stoller's avatar
Leigh Stoller committed
587 588 589 590 591 592 593 594 595 596 597 598 599 600 601

	#
	# 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
602
		    if (APT_Rspec::CompareValues($key, $val1, $val2));
Leigh Stoller's avatar
Leigh Stoller committed
603 604 605 606 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 731 732 733 734 735 736 737 738 739 740 741 742
		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 {
		    print STDERR "$altid\n";
		    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;
}

743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768
############################################################################
# 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 Stoller's avatar
Leigh Stoller committed
769 770 771 772 773 774 775 776 777 778 779
############################################################################
package APT_Rspec::Node;
use Carp;
use English;
use GeniXML;
use GeniHRN;

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

780 781 782 783 784 785 786 787 788 789
    #
    # Make up a tag (variable for the script) that makes some kind of sense.
    #
    my $tag = $client_id;
    # Replace dashes with underscores.
    $tag =~ s/\-/\_/g;
    if ($tag !~ /^node/i) {
	$tag = "node_${tag}";
    }

Leigh Stoller's avatar
Leigh Stoller committed
790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805
    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"		=> {},
806
	"ifacelist"             => [],
Leigh Stoller's avatar
Leigh Stoller committed
807 808 809 810
	"blockstores"		=> {},
	"pipes"			=> {},
	"use_type_default_image"=> undef,
	"routable_control_ip"   => undef,
811
	"tag"			=> $tag,
Leigh Stoller's avatar
Leigh Stoller committed
812 813 814 815 816 817 818 819 820 821
    };
    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'}; }
822
sub ifacelist($)		{ return $_[0]->{'ifacelist'}; }
Leigh Stoller's avatar
Leigh Stoller committed
823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848
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) = @_;
849
    push(@{$self->ifacelist()}, $iface);
Leigh Stoller's avatar
Leigh Stoller committed
850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869
    $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);
870 871 872
    if (defined($exclusive)) {
	$exclusive = ($exclusive ? 1 : 0);
    }
Leigh Stoller's avatar
Leigh Stoller committed
873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 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

    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;
	    };
927 928 929 930
	    /^(\w+:)?routable_control_ip$/i && $rspec->permissive() && do {
		# Ignore, it would not work anyway.
		last SWITCH;
	    };
Leigh Stoller's avatar
Leigh Stoller committed
931 932 933 934 935 936 937 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 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
	    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.
	#
	my $client_id = GetTextOrFail("client_id", $ref);
	my $ip        = GeniXML::GetIp($ref, $ref->parentNode);
	my $mask      = GeniXML::GetMask($ref, $ref->parentNode);

	APT_Rspec::Iface->new($rspec, $node, $client_id, $ip, $mask);
    };

    #
    # 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 Stoller's avatar
Leigh Stoller committed
1094
	    /^(vnode|host|location)$/i && do {
Leigh Stoller's avatar
Leigh Stoller committed
1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113
		# 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;
	    };
1114 1115 1116 1117 1118
	    /^adb_target$/i && do {
		my $hardware = GetTextOrFail("target_id", $child);
		$node->{"adb_target"} = $hardware;
		last SWITCH;
	    };
Leigh Stoller's avatar
Leigh Stoller committed
1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 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
	    /^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;
	    };
	    /^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 ||
1203
	     /^(adb_target)$/i ||
Leigh Stoller's avatar
Leigh Stoller committed
1204 1205 1206 1207 1208 1209
	     /^(use_type_default_image|routable_control_ip)$/i) && do {
		 return 1
		     if (APT_Rspec::CompareValues("Node: $client_id: $key",
						  $val1,$val2));
		 last SWITCH;
	    };
1210
	    /^(ifaces|ifacelist)$/i && do {
Leigh Stoller's avatar
Leigh Stoller committed
1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 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 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 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 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326
		# 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;

sub new($$$$$$)
{
    my ($class, $rspec, $node, $client_id, $ip, $mask) = @_;

    my $self = {
	"client_id"  => $client_id,
	"node_id"    => $node->client_id(),
	"ip"         => $ip,
	"mask"       => $mask,
	"statements" => [],
	"tag"        => "iface" . scalar(keys(%{$rspec->ifaces()})),
    };
    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'}; }
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;
	    };
	    /^(node_id|ip|mask)$/i && do {
		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) = @_;

1327 1328 1329 1330 1331 1332 1333 1334 1335 1336
    #
    # Make up a tag (variable for the script) that makes some kind of sense.
    #
    my $tag = $client_id;
    # Replace dashes with underscores.
    $tag =~ s/\-/\_/g;
    if ($tag !~ /^link/i) {
	$tag = "link_${tag}";
    }
    
Leigh Stoller's avatar
Leigh Stoller committed
1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354
    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,
	"trivial_ok"		=> undef,
	"vlan_tagging"		=> undef,
	"link_multiplexing"	=> undef,
	"interswitch"		=> undef,
	"nomac_learning"        => undef,
	"openflow_controller"   => undef,
	"isbslink"		=> 0,
	"bsnode"		=> undef,
	"ifaces"		=> {},
1355
	"ifacelist"             => [],
Leigh Stoller's avatar
Leigh Stoller committed
1356 1357 1358 1359
	"properties"            => {},
	"component_managers"    => [],
	"jacks_site"		=> undef,
	"statements"		=> [],
1360
	"tag"			=> $tag,
Leigh Stoller's avatar
Leigh Stoller committed
1361 1362 1363 1364 1365 1366 1367 1368
    };
    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'}; }
1369
sub ifacelist($)		{ return $_[0]->{'ifacelist'}; }
Leigh Stoller's avatar
Leigh Stoller committed
1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389
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();
1390
    push(@{$self->ifacelist()}, $iface);
Leigh Stoller's avatar
Leigh Stoller committed
1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 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 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 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
    $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;
	    };
	    /^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 ||
	     /^(nomac_learning|jacks_site)$/i) && do {
		 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;
	    };
1659
	    /^(ifaces|ifacelist)$/i && do {
Leigh Stoller's avatar
Leigh Stoller committed
1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 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 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966
		# Handled up in CompareLinks()
		last SWITCH;
	    };
	    /^bsnode$/i && do {
		# Handled up in CompareLinks()
		last SWITCH;
	    };
	    /^properties$/i && do {
		# Handled up above.
		last SWITCH;
	    };
	    /^component_managers$/i && do {
		return 1
		    if (APT_Rspec::CompareLists("Link: $client_id: $key",
						$val1, $val2));
		last SWITCH;
	    };
	    /^(tag|statements|isbslink)$/i && do {
		last SWITCH;
	    };
	    print STDERR "Link:Compare: Unknown attribute: $key\n";
	    return 1;
	}
    }
    #
    # Kludge; if the links compare okay, but the type mismatches lan!=vlan,
    # then let it pass.
    #
    my $type1 = $this->type();
    my $type2 = $that->type();
    if (defined($type1) && defined($type2)) {
	return 0
	    if (($type1 eq "lan" && $type2 eq "vlan") ||
		($type1 eq "vlan" && $type2 eq "lan"));

	return APT_Rspec::CompareValues("Link: $client_id: type",
					$this->type(), $that->type());
    }
    return 0;
}

#
# We do link properties specially, since we have to deal with the naming
# mismatch caused by how geni-lib names interfaces.
#
sub CompareProperties($$)
{
    my ($link1, $link2) = @_;
    my $client_id = $link1->client_id();
    my $plist1    = $link1->properties();
    my $plist2    = $link2->properties();

    if (0) {
	foreach my $k (sort(keys(%{$plist1}))) {
	    my $p  = $plist1->{$k};
	    my $s  = $p->source();
	    my $d  = $p->dest();
	    my $ns = $p->source_node_id();
	    my $nd = $p->dest_node_id();
	    print "P1: $k, $s, $d, $ns, $nd\n";
	}
	foreach my $k (sort(keys(%{$plist2}))) {
	    my $p  = $plist2->{$k};
	    my $s  = $p->source();
	    my $d  = $p->dest();
	    my $ns = $p->source_node_id();
	    my $nd = $p->dest_node_id();
	    print "P2: $k, $s, $d, $ns, $nd\n";
	}
    }

    if (scalar(values(%{$plist1})) != scalar(values(%{$plist2}))) {
	my $c1 = scalar(values(%{$plist1}));
	my $c2 = scalar(values(%{$plist2}));
	print STDERR "Property count mismatch in link $client_id: $c1!=$c2\n";
	return 1;
    }
    foreach my $key (sort(keys(%$plist1))) {
	my $prop1 = $plist1->{$key};

	if (!exists($plist2->{$key})) {
	    print STDERR "property $key in link $client_id ".
		"in rspec one but not rspec two\n";
	    return 1;
	}
	my $prop2 = $plist2->{$key};
	return 1
	    if ($prop1->Compare($prop2));
    }
    return 0;
}

############################################################################
package APT_Rspec::Pool;
use Carp;
use English;
use GeniXML;

sub new($$)
{
    my ($class, $ref) = @_;
    my $client_id = GetTextOrFail("client_id", $ref);
    my $count     = GetTextOrFail("count", $ref);
    my $type      = GetTextOrFail("type", $ref);

    my $self = {
	"client_id" => $client_id,
	"count"     => $count,
	"type"      => $type,
    };
    bless($self, $class);
    return $self;
}
sub client_id($)		{ return $_[0]->{'client_id'}; }
sub count($)			{ return $_[0]->{'count'}; }
sub type($)			{ return $_[0]->{'type'}; }
sub fatal($)			{ return APT_Rspec::fatal($_[0]); }
sub GetTextOrFail($$)		{ return APT_Rspec::GetTextOrFail(@_); }

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

    return 1
	if (APT_Rspec::CompareHashes("routable_pool $id", $this, $that));
    return 0;
}

############################################################################
package APT_Rspec::DelayPipe;
use Carp;
use English;
use GeniXML;

sub new($$$)
{
    my ($class, $node, $ref) = @_;
    #
    # Not all params need to be defined.
    #
    my $source   = GetText("source", $ref);
    my $capacity = GetText("capacity", $ref);
    my $latency  = GetText("latency", $ref);
    my $lossrate = GetText("packet_loss", $ref);

    # Geni-lib will always add the node to the iface_id.
    my ($tmp,$iface_id) = split(":", $source);
    $iface_id = $tmp if (!defined($iface_id));

    my $self = {
	"iface_id"   => $iface_id,
	"node_id"    => $node->client_id(),
	"capacity"   => $capacity,
	"latency"    => $latency,
	"lossrate"   => $lossrate,
    };
    bless($self, $class);
    return $self;
}
sub iface_id($)			{ return $_[0]->{'iface_id'}; }
sub node_id($)			{ return $_[0]->{'node_id'}; }
sub capacity($)			{ return $_[0]->{'capacity'}; }
sub latency($)			{ return $_[0]->{'latency'}; }
sub lossrate($)			{ return $_[0]->{'lossrate'}; }
sub fatal($)			{ return APT_Rspec::fatal($_[0]); }
sub GetTextOrFail($$)		{ return APT_Rspec::GetTextOrFail(@_); }

#
# Compare two pipes Return zero if they are equivalent.
#
sub Compare($$)
{
    my ($this, $that) = @_;
    my $id = $this->node_id() . ":" . $this->iface_id();

    return 1
	if (APT_Rspec::CompareHashes("delay pipe $id", $this, $that));
    return 0;
}

############################################################################
package APT_Rspec::LinkProperty;
use Carp;
use English;
use GeniXML;

sub new($$$$)
{
    my ($class, $rspec, $link, $ref) = @_;
    my $link_id = $link->client_id();

    #
    # Not all params need to be defined.
    #
    my $source   = GetTextOrFail("source_id", $ref);
    my $dest     = GetTextOrFail("dest_id", $ref);
    my $bandwidth= GetText("capacity", $ref);
    my $plr      = GetText("packet_loss", $ref);
    my $latency  = GetText("latency", $ref);

    # Empty property seems to happen a lot, ignore.
    return undef
	if (! (defined($bandwidth) || defined($plr) || defined($latency)));

    my $self = {
	"source"         => $source,
	"dest"           => $dest,
	"link_id"        => $link_id,
	"source_node_id" => undef,
	"dest_node_id"   => undef,
	"bandwidth"      => $bandwidth,
	"latency"        => $latency,
	"plr"            => $plr,
    };

    #
    # Better be a real iface. We need it so we can assign the node_id
    # to the property.
    #
    my $source_iface = $rspec->getIface($source);
    if (!defined($source_iface)) {
	fatal("LinkProperty: No such source iface $source in link $link_id");
    }
    $self->{'source_node_id'} = $source_iface->node_id();

    # For geni-lib LAN, all properties are dest=link_id
    if ($dest ne $link_id) {
	my $dest_iface = $rspec->getIface($dest);
	if (!defined($dest_iface)) {
	    fatal("LinkProperty: No such dest iface $dest in link $link_id");
	}
	$self->{'dest_node_id'} = $dest_iface->node_id();
    }
    
    bless($self, $class);
    return $self;
}
sub link_id($)			{ return $_[0]->{'link_id'}; }
sub source_node_id($)		{ return $_[0]->{'source_node_id'}; }
sub source($)			{ return $_[0]->{'source'}; }
sub dest($)			{ return $_[0]->{'dest'}; }
sub dest_node_id($)		{ return $_[0]->{'dest_node_id'}; }
sub bandwidth($)		{ return $_[0]->{'bandwidth'}; }
sub latency($)			{ return $_[0]->{'latency'}; }
sub plr($)			{ return $_[0]->{'plr'}; }
sub fatal($)			{ return APT_Rspec::fatal($_[0]); }
sub GetTextOrFail($$)		{ return APT_Rspec::GetTextOrFail(@_); }

#
# Compare two properties. Return zero if they are equivalent.
#
sub Compare($$)
{
    my ($this, $that) = @_;
    my $link_id = $this->link_id();
    my $id = $this->link_id() . ":" . $this->source();

    #
    # We had to deal with the client_id naming issue (node:iface)
    # in CompareProperties, so do not compare the source. But still
    # need to apply same treatment to the dest.
    #
    foreach my $key (keys(%{$this})) {
	if ($key eq "dest" || $key eq "source") {
	    next
		if ($this->{$key} eq $that->{$key});

	    #
	    # We force all interfaces to node_id:iface_id in geni-lib, so 
	    # this might be a name mismatch.
	    #
	    my $id = $this->{$key};
	    my $altid;
	    
	    if ($id =~ /^([-\w]*):([-\w]*)$/) {
		$altid = $2;
	    }
	    else {
		my $node_id = ($key eq "dest" ?
			       $this->dest_node_id() : $this->source_node_id());
		$altid = "${node_id}:${id}";
	    }
	    if ($altid eq $that->{$key}) {
		next;
	    }
	    else {
		print STDERR "$altid\n";
		print STDERR "property $id in link $link_id ".
		    "in rspec one but not rspec two\n";
		return 1;
	    }
	}
	return 1
	    if (APT_Rspec::CompareValues("link property $id $key",
					 $this->{$key}, $that->{$key}));
    }
    return 0;
}

############################################################################
package APT_Rspec::Service;
use Carp;
use English;
1967
use Data::Dumper;
Leigh Stoller's avatar
Leigh Stoller committed
1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075
use HTML::Entities;
use GeniXML;

sub new($$$)
{
    my ($class, $node, $ref) = @_;
    my $node_id = $node->client_id();
    my $self;

    my ($ns,$name) = split(":", $ref->nodeName);
    $name = $ns if (!defined($name));
	
    SWITCH: for (lc($name)) {
	/^install$/i && do {
	    my $path = GetText("install_path", $ref);
	    my $url  = GetText("url", $ref);
	    fatal("Missing install_path in install service")
		if (!defined($path));
	    fatal("Missing url in install service")
		if (!defined($url));

	    $self = {
		"type"   => $name,
		"path"   => $path,
		"url"    => $url
	    };
	    last SWITCH;
	};
	/^execute$/i && do {
	    my $cmd = GetText("command", $ref);
	    my $sh  = GetText("shell", $ref);
	    fatal("Missing command in execute service")
		if (!defined($cmd));
	    fatal("Missing sh in execute service")
		if (!defined($sh));
		    
	    $self = {
		"type"   => $name,
		"cmd"    => decode_entities($cmd),
		"shell"  => $sh
	    };
	    last SWITCH;
	};
	/^program-agent$/i && do {
	    my $pname      = GetTextOrFail("name", $ref);
	    my $command    = GetTextOrFail("command", $ref);
	    my $directory  = GetText("directory", $ref);
	    my $start      = GetText("onexpstart", $ref);

	    $self = {
		"type"       => $name,
		"name"       => $pname,
		"command"    => $command,
		"dir"        => $directory,
		"onexpstart" => $start
	    };
	    last SWITCH;
	};
	fatal("Unknown execute service for $node_id: $name");
    }
    $self->{'node_id'} = $node_id;
    bless($self, $class);
    return $self;
}
sub type($)			{ return $_[0]->{'type'}; }
sub node_id($)			{ return $_[0]->{'node_id'}; }
sub fatal($)			{ return APT_Rspec::fatal($_[0]); }
sub GetTextOrFail($$)		{ return APT_Rspec::GetTextOrFail(@_); }

#
# Compare two services. Return zero if they are equivalent.
#
sub Compare($$)
{
    my ($this, $that) = @_;
    my $id = $this->node_id() . ":" . $this->type();

    return 1
	if (APT_Rspec::CompareHashes("service $id", $this, $that));
    return 0;
}

############################################################################
package APT_Rspec::Blockstore;
use Carp;
use English;
use GeniXML;

sub new($$$$)
{
    my ($class, $rspec, $node, $ref) = @_;

    #
    # The name is optional on a remote blockstore, but geni-lib always
    # spits it out, cause it has the same name as the node. This is going
    # to make comparison below painful.
    #
    my $bsname = GetText("name", $ref);
    my $bclass = GetTextOrFail("class", $ref);
    my $tag    = "bs" . $rspec->{'bscounter'}++;
    my $self = {
	"name"      => $bsname,
	"class"     => $bclass,
	"mount"     => undef,
	"size"      => undef,
	"readonly"  => undef,
	"dataset"   => undef,
	"placement" => undef,
2076
	"rwclone"   => undef,
Leigh Stoller's avatar
Leigh Stoller committed
2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111
	"tag"       => $tag,
    };

    # Everything is an attribute.
    foreach my $attr ($ref->attributes()) {
	my $key = $attr->nodeName;
	my $val = $attr->getValue();

        SWITCH: for (lc($key)) {
	    /^(name|class|xmlns(:.*)?)$/i && do {
		last SWITCH;
	    };
	    /^mountpoint$/i && do {
		$self->{'mount'} = $val;
		last SWITCH;
	    };
	    /^readonly$/i && do {
		$self->{'readonly'} = $val;
		last SWITCH;
	    };
	    /^dataset$/i && do {
		if (!GeniHRN::IsValid($val)) {
		    fatal("dataset for $bsname is not a valid URN: $val");
		}
		$self->{'dataset'} = $val;
		last SWITCH;
	    };
	    /^placement$/i && do {
		$self->{'placement'} = $val;
		last SWITCH;
	    };
	    /^size$/i && do {
		$self->{'size'} = $val;
		last SWITCH;
	    };
2112 2113 2114 2115
	    /^rwclone$/i && do {
		$self->{'rwclone'} = (lc($val) eq "true" ? 1 : 0);
		last SWITCH;
	    };
Leigh Stoller's avatar
Leigh Stoller committed
2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176
	    fatal("Unknown attribute for blockstore $bsname: $key,$val");
	}
    }
    $self->{'node_id'} = $node->client_id();
    bless($self, $class);
    return $self;
}
sub name($)			{ return $_[0]->{'name'}; }
sub class($)			{ return $_[0]->{'class'}; }
sub node_id($)			{ return $_[0]->{'node_id'}; }
sub fatal($)			{ return APT_Rspec::fatal($_[0]); }
sub GetTextOrFail($$)		{ return APT_Rspec::GetTextOrFail(@_); }

#
# Compare two blockstores. Return zero if they are equivalent.
#
sub Compare($$)
{
    my ($this, $that) = @_;
    # Remote block store might not have a name, its optional.
    my $id = $this->node_id() . ":" . ($this->name() // "undef");

    foreach my $key (keys(%{$this})) {
	next
	    if ($key eq "tag");

	#
	# Oddity created by a silly decision in geni-lib.  We used to add a
	# "-bs" to the name of remote blockstores, which of course makes
	# regression testing a problem. It was dumb to add this extension,
	# the CM fully ignores the name on remote blockstores.
	#
	# Yet another problem is that remote blockstores are not required
	# to have a name, even though geni-lib always spits it out with
	# it set to the name of the node. Makes comparison annoying. 
	#
	if ($key eq "name") {
	    my $bsname1 = $this->{$key};
	    my $bsname2 = $that->{$key};
	    
	    if (defined($bsname1) && defined($bsname2)) {
		next
		    if ($bsname1 eq $bsname2);
		next
		    if ("${bsname1}-bs" eq $bsname2 ||
			"${bsname2}-bs" eq $bsname1);
	    }
	    # Class is required.
	    next
		if ((!defined($bsname1) && $this->class() eq "remote") ||
		    (!defined($bsname2) && $that->class() eq "remote"));
	}
	return 1
	    if (APT_Rspec::CompareValues("blockstore $id $key",
					 $this->{$key}, $that->{$key}));
    }
    return 0;
}

1;