Port.pm 21 KB
Newer Older
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
#
# This is the Port class that represent a port in VLAN.
# It aims at providing all avaialble information of a
# port rather than just a string representation consisting
# of node and card or port only, which is used by most snmpit
# code.
#
# Previous string representation only has node and card for
# a PC port, which can not be unique when representing a port
# on a test node switch, such as our hp5406 nodes.
# A Port class instance uniquely identifies a port with
# all of its properties, such as node_id, card, port and iface.
#
# Some important terms used:
# - Tokens: seperated fields of a port, e.g. "node1", "card2", "iface3", etc.
# - Iface:  interface on a node, or a full representation of
#   an interface, say "iface2", "node1:iface4".
# - Triple: a port representation that uses three fields:
#   node, card and port, like "node1:card2.port1".
# - String: a string representation of a port, ususally a
#   combination of tokens, e.g. "node1:iface3", "node1:card3.port4".
#
# All code that needs to convert between different representations
# or merely parse tokens from string and vice-verse must
# use the converters provided in this class.
Weibin Sun's avatar
Weibin Sun committed
26
#
27
# Copyright (c) 2011-2013 University of Utah and the Flux Group.
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
# 
# {{{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/>.
# 
# }}}
Weibin Sun's avatar
Weibin Sun committed
47 48 49 50 51 52 53 54 55 56 57 58 59
#
package Port;

use strict;
use Exporter;
use vars qw(@ISA @EXPORT);

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

use libdb;
use English;
use Data::Dumper;
60
use overload ('""' => 'Stringify');
Weibin Sun's avatar
Weibin Sun committed
61

62 63
# Cache of port instances
# node:iface OR node:card.port => Port Instance
Weibin Sun's avatar
Weibin Sun committed
64 65
my %allports = ();

Weibin Sun's avatar
Weibin Sun committed
66
# Ends of wires, node:iface OR node:card.port => Port Instance
Weibin Sun's avatar
Weibin Sun committed
67 68
my %wiredports = ();

69 70 71 72 73 74 75 76 77 78 79 80
#
# check if a var is a Port instance
#
sub isPort($$)
{
	my ($c, $p) = @_;
	if (ref($p) eq "Port") {
		return 1;
	}
	return 0;
}

Weibin Sun's avatar
Weibin Sun committed
81 82 83 84 85 86
#
# Get the other end port of a wire by triple representation of this end port
# the classname can be ignored
# the representation can be a triple-port string or triple tokens
#
sub GetOtherEndByTriple($;$$$)
Weibin Sun's avatar
Weibin Sun committed
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
{
    my ($c, $node, $card, $port) = @_;
    my $str;

    if (!defined($node)) {
	$str = $c;
    } elsif (!defined($card)) {
	$str = $node;
    } elsif (!defined($port)) {
	$str = Port->Tokens2TripleString($c, $node, $card);
    } else {
	$str = Port->Tokens2TripleString($node, $card, $port);
    }

    if (exists($wiredports{$str})) {
	return $wiredports{$str};
    }

    my $p = Port->LookupByTriple($str);
Weibin Sun's avatar
Weibin Sun committed
106
    if (defined($p)) {
Weibin Sun's avatar
Weibin Sun committed
107

Weibin Sun's avatar
Weibin Sun committed
108 109
	$wiredports{$p->toTripleString()} = $p->getOtherEndPort();
	$wiredports{$p->toIfaceString()} = $p->getOtherEndPort();
Weibin Sun's avatar
Weibin Sun committed
110

Weibin Sun's avatar
Weibin Sun committed
111
	return $p->getOtherEndPort();
Weibin Sun's avatar
Weibin Sun committed
112 113 114 115 116
    } else {
	return undef;
    }
}

Weibin Sun's avatar
Weibin Sun committed
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 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 157
#
# Get the other end port of a wire by iface representation of this end port
# the classname can be ignored
# the representation can be a iface-port string or iface tokens
#
sub GetOtherEndByIface($;$$)
{
    my ($c, $node, $iface) = @_;
    my $str;
    my $p;
    
    if (defined($iface)) {
	$str = Port->Tokens2IfaceString($node, $iface);
    } elsif (!defined($node)) {
        $str = $c;
    } else {
        $str = $node;
        $p = Port->LookupByIface($str);
        if (!defined($p)) {
            $str = Port->Tokens2IfaceString($c, $node);
            $p = Port->LookupByIface($str);
            if (!defined($p)) {
                return undef;
            }
        }
    }
    
    if (!defined($p)) {
    	$p = Port->LookupByIface($str);
    }
    if (defined($p)) {
    
    	$wiredports{$p->toTripleString()} = $p->getOtherEndPort();
	$wiredports{$p->toIfaceString()} = $p->getOtherEndPort();

	return $p->getOtherEndPort();
    } else {
	return undef;
    }
}    

Weibin Sun's avatar
Weibin Sun committed
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174

#
# Parse node:iface string into tokens
# classname can be ignored
#
sub ParseIfaceString($;$)
{
    my ($c, $striface) = @_;

    if (!defined($striface)) {
	$striface = $c;
    }

    if ($striface =~ /^(.+):(.+)$/) {
	return ($1, $2);
    }

Weibin Sun's avatar
Weibin Sun committed
175
    return (undef, undef);
Weibin Sun's avatar
Weibin Sun committed
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
}

#
# Parse node:card.port string into tokens
# can be called without the classname
#
sub ParseTripleString($;$)
{
    my ($c, $triplestring) = @_;

    if (!defined($triplestring)) {
	$triplestring = $c;
    }

    if ($triplestring =~ /^(.+):(.+)\.(.+)$/) {
	return ($1, $2, $3);
    }

Weibin Sun's avatar
Weibin Sun committed
194
    return (undef, undef, undef);
Weibin Sun's avatar
Weibin Sun committed
195 196
}

Weibin Sun's avatar
Weibin Sun committed
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
sub ParseCardPortString($;$)
{
    my ($c, $cp) = @_;

    if (!defiend($cp)) {
	$cp = $c;
    }

    # Should not include all fields
    if ($cp =~ /^(.+):(.+)[\/\.](.+)$/) {
	return (undef, undef);
    }

    if ($cp =~ /^(.+)\.(.+)$/ || $cp =~ /^(.+)\/(.+)$/) {
	return ($1, $2);
    }

    return (undef, undef);
}

Weibin Sun's avatar
Weibin Sun committed
217 218 219 220 221 222 223 224 225 226 227 228
sub Iface2Triple($;$)
{
    my ($c, $striface) = @_;

    if (!defined($striface)) {
	$striface = $c;
    }

    if (exists($allports{$striface})) {
	return $allports{$striface}->toTripleString();
    } else {
	my ($nodeid, $iface) = ParseIfaceString($striface);
229 230 231
	if (!defined($iface)) {
	    return undef;
	}
Weibin Sun's avatar
Weibin Sun committed
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253

	my $port = Port->LookupByIface($nodeid, $iface);
	if (defined($port) && $port != 0 && $port != -1) {
	    return $port->toTripleString();
	} else {
	    return undef;
	}
    }
}

sub Triple2Iface($;$)
{
    my ($c, $strtriple) = @_;

    if (!defined($strtriple)) {
	$strtriple = $c;
    }

    if (exists($allports{$strtriple})) {
	return $allports{$strtriple}->toIfaceString();
    } else {
	my ($nodeid, $card, $port) = ParseTripleString($strtriple);
254 255 256
	if (!defined($card) || !defined($port) || !defined($nodeid)) {
	    return undef;
	}
Weibin Sun's avatar
Weibin Sun committed
257 258 259 260 261 262 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 288 289 290 291

	my $portInst = Port->LookupByTriple($nodeid, $card, $port);
	if (defined($portInst && $port != 0 && $port != -1)) {
	    return $portInst->toIfaceString();
	} else {
	    return undef;
	}
    }
}

sub Tokens2TripleString($$$;$)
{
    my ($c, $nodeid, $card, $port) = @_;

    if (!defined($port)) {
	$port = $card;
	$card = $nodeid;
	$nodeid = $c;
    }

    return "$nodeid:$card.$port";
}

sub Tokens2IfaceString($$;$)
{
    my ($c, $nodeid, $iface) = @_;

    if (!defined($iface)) {
	$iface = $nodeid;
	$nodeid = $c;
    }

    return "$nodeid:$iface";
}

Weibin Sun's avatar
Weibin Sun committed
292 293 294 295

# functions started with fake_ are used to handle
# a special 'node:card/port' representation.

Weibin Sun's avatar
Weibin Sun committed
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
sub fake_CardPort2Iface($$;$)
{
    my ($cn, $c, $p) = @_;

    if (!defined($p)) {
	$p = $c;
	$c = $cn;
    }
    return "$c/$p";
}

sub fake_TripleString2IfaceString($;$)
{
    my ($cn, $t) = @_;

    if (!defined($t)) {
	$t = $cn;
    }

    my ($n, $c, $p) = ParseTripleString($t);
316 317 318
    if (!defined($n) || !defined($c) || !defined($p)) {
        return undef;
    }
Weibin Sun's avatar
Weibin Sun committed
319 320 321 322 323 324 325 326 327 328 329 330 331

    return "$n:".fake_CardPort2Iface($c, $p);
}

sub fake_IfaceString2TripleTokens($;$)
{
    my ($cn, $i) = @_;

    if (!defined($i)) {
	$i = $cn;
    }

    my ($n, $iface) = ParseIfaceString($i);
332 333
    
    if (defined($iface) && $iface =~ /^(.+)\/(.+)$/) {
Weibin Sun's avatar
Weibin Sun committed
334 335 336 337 338 339
	return ($n, $1, $2);
    }    

    return (undef, undef, undef);
}

Weibin Sun's avatar
Weibin Sun committed
340 341 342 343 344 345 346 347
#
# Class method
# Find the port instance by a given string, if no port found from DB, 
# make a fake one.
#
# This is useful when a port string is derived from the switch, but
# it is not stored in DB. (eg. listing all ports on a switch)
#
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
sub LookupByStringForced($$)
{
    my ($class, $str) = @_;
    my $inst = {};
    
    my $p = Port->LookupByIface($str);
    if (defined($p)) {
	return $p;
    }
    else {
	$p = Port->LookupByTriple($str);
	if (defined($p)) {
	    return $p;
	}
    }
    
    my $iface;
    my ($nodeid, $card, $port) = Port->ParseTripleString($str);
    if (!defined($port)) {
	($nodeid, $card, $port) = Port->fake_IfaceString2TripleTokens($str);
	if (!defined($port)) {
	    ($nodeid, $iface) = Port->ParseIfaceString($str);
	    if (!defined($iface)) {
		$port = undef;
	    } else {
		$port = 1;
		$card = $iface;
	    }
	} else {
377
	    $iface = Port->fake_CardPort2Iface($card, $port);
378 379
	}
    } else {
380
	$iface = Port->fake_CardPort2Iface($card, $port);
381 382 383
    }
    
    if (defined($port)) {
384
	my $rowref = {};
385 386 387 388 389
	
	$inst->{"RAW_STRING"} = $str;
	$inst->{"FORCED"} = 1;
	$inst->{"HAS_FIELDS"} = 1;
	
390 391 392 393 394 395 396 397 398 399 400 401 402
	$rowref->{'iface'} = $iface;
	$rowref->{'node_id'} = $nodeid;
	$rowref->{'card'} = $card;
	$rowref->{'port'} = $port;
	$rowref->{'mac'} = "";
	$rowref->{'IP'} = "";
	$rowref->{'role'} = "";
	$rowref->{'interface_type'} = "";
	$rowref->{'mask'} = "";
	$rowref->{'uuid'} = "";
	$rowref->{'trunk'} = 0;
	$rowref->{'trunk_mode'} = "equal";
	$inst->{"INTERFACES_ROW"} = $rowref;
403 404 405 406 407 408 409 410 411 412 413
    }
    else {
	$inst->{"RAW_STRING"} = $str;
	$inst->{"FORCED"} = 1;
	$inst->{"HAS_FIELDS"} = 0;
    }
    
    #
    # We should determine this according to the query result
    # in nodes table by nodeid.
    #
414
    $inst->{"WIRE_END"} = "switch";
415 416 417 418 419
    
    bless($inst, $class);
    return $inst;
}

Weibin Sun's avatar
Weibin Sun committed
420 421 422 423
#
# Class method
# get the port instance according to its iface-string representation
#
Weibin Sun's avatar
Weibin Sun committed
424 425
sub LookupByIface($$;$)
{
426
    my ($class, $nodeid, $iface, $force) = @_;
427
    my $striface="";
Weibin Sun's avatar
Weibin Sun committed
428 429 430

    if (!defined($iface)) {
	$striface = $nodeid;
431
	($nodeid, $iface) = Port->ParseIfaceString($striface);	
Weibin Sun's avatar
Weibin Sun committed
432
    } else {
433 434 435
	$striface = Tokens2IfaceString($class, $nodeid, $iface);	
    }
    
436
    if (!defined($striface) || !defined($nodeid)) {
437
        return undef;
Weibin Sun's avatar
Weibin Sun committed
438 439 440 441 442 443 444 445 446
    }

    if (exists($allports{$striface})) {
	return $allports{$striface};
    }

    # all fields
    my $query_result = DBQueryWarn("select * from interfaces ".
		    "where node_id='$nodeid' AND iface='$iface'");
Weibin Sun's avatar
Weibin Sun committed
447
    return undef
Weibin Sun's avatar
Weibin Sun committed
448
	if (!$query_result);
Weibin Sun's avatar
Weibin Sun committed
449 450 451
    
    if (!$query_result->numrows) {
	my ($n, $c, $p) = fake_IfaceString2TripleTokens($class, $striface);
452
	if (defined($p)) {
Weibin Sun's avatar
Weibin Sun committed
453 454 455 456 457
	    return LookupByTriple($class, $n, $c, $p);
	} else {
	    return undef;
	}
    }
Weibin Sun's avatar
Weibin Sun committed
458 459 460 461 462 463 464 465 466 467 468 469

    my $rowref = $query_result->fetchrow_hashref();

    my $card  = $rowref->{'card'};
    my $port  = $rowref->{'port'};

    my $inst = {};
    $inst->{"INTERFACES_ROW"} = $rowref;

    # wire mapping
    $query_result =
	DBQueryWarn("select * from wires ".
470
		    "where node_id1='$nodeid' AND card1='$card' AND port1='$port'");
Weibin Sun's avatar
Weibin Sun committed
471
    return undef
472
	if (!$query_result);
Weibin Sun's avatar
Weibin Sun committed
473

474 475 476 477 478 479 480 481 482 483 484
    $inst->{"WIRE_END"} = "pc";
    
    if (!$query_result->numrows) {
	$query_result =
	    DBQueryWarn("select * from wires ".
		    "where node_id2='$nodeid' AND card2='$card' AND port2='$port'");
	return undef
	    if (!$query_result);
	return undef
	    if (!$query_result->numrows);
	$inst->{"WIRE_END"} = "switch";
Weibin Sun's avatar
Weibin Sun committed
485 486
    }

487
    $rowref = $query_result->fetchrow_hashref();
Weibin Sun's avatar
Weibin Sun committed
488
    $inst->{"WIRES_ROW"} = $rowref;
489 490
    $inst->{"FORCED"} = 0;
    $inst->{"HAS_FIELDS"} = 1;
Weibin Sun's avatar
Weibin Sun committed
491 492 493 494 495 496 497 498 499

    bless($inst, $class);

    $allports{$striface} = $inst;
    $allports{Tokens2TripleString($class, $nodeid, $card, $port)} = $inst;

    return $inst;
}

Weibin Sun's avatar
Weibin Sun committed
500 501 502 503
#
# Class method
# get the port instance according to its triple-string representation
#
Weibin Sun's avatar
Weibin Sun committed
504 505 506 507 508 509 510 511 512 513 514 515
sub LookupByTriple($$;$$)
{
    my ($class, $nodeid, $card, $port) = @_;

    my $strtriple;

    if (!defined($card)) {
	$strtriple = $nodeid;
	($nodeid, $card, $port) = Port->ParseTripleString($strtriple);
    } else {
	$strtriple = Tokens2TripleString($class, $nodeid, $card, $port);
    }
516
    
517
    if (!defined($strtriple) || !defined($nodeid) || !defined($card)) {
518 519
        return undef;
    }
Weibin Sun's avatar
Weibin Sun committed
520 521 522 523 524

    if (exists($allports{$strtriple})) {
	return $allports{$strtriple};
    }

Weibin Sun's avatar
Weibin Sun committed
525
    # wire mapping:
Weibin Sun's avatar
Weibin Sun committed
526 527
    my $query_result =
	DBQueryWarn("select * from wires ".
528
		    "where node_id1='$nodeid' AND card1='$card' AND port1='$port'");
Weibin Sun's avatar
Weibin Sun committed
529
    return undef
530
	if (!$query_result);
Weibin Sun's avatar
Weibin Sun committed
531

532 533
    my $inst = {};
    $inst->{"WIRE_END"} = "pc";
534
    
535 536 537 538 539 540 541 542 543 544 545 546
    if (!$query_result->numrows) {
	$query_result =
	    DBQueryWarn("select * from wires ".
		    "where node_id2='$nodeid' AND card2='$card' AND port2='$port'");
	return undef
	    if (!$query_result);
	return undef
	    if (!$query_result->numrows);
	$inst->{"WIRE_END"} = "switch";
    }

    my $rowref = $query_result->fetchrow_hashref();
Weibin Sun's avatar
Weibin Sun committed
547 548
    $inst->{"WIRES_ROW"} = $rowref;

Weibin Sun's avatar
Weibin Sun committed
549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566
    $query_result =
	DBQueryWarn("select * from interfaces ".
		    "where node_id='$nodeid' AND card='$card' AND port='$port'");
    return undef
	if (!$query_result);
    if (!$query_result->numrows) {
	$rowref = {};
	my $iface = fake_CardPort2Iface($card, $port);
	$rowref->{'iface'} = $iface;
	$rowref->{'node_id'} = $nodeid;
	$rowref->{'card'} = $card;
	$rowref->{'port'} = $port;
	$rowref->{'mac'} = "";
	$rowref->{'IP'} = "";
	$rowref->{'role'} = "";
	$rowref->{'interface_type'} = "";
	$rowref->{'mask'} = "";
	$rowref->{'uuid'} = "";
567
	$rowref->{'trunk'} = 0;
568
	$rowref->{'trunk_mode'} = "equal";
Weibin Sun's avatar
Weibin Sun committed
569 570 571 572 573 574 575
    } else {
	$rowref = $query_result->fetchrow_hashref();
    }

    my $iface = $rowref->{'iface'};

    $inst->{"INTERFACES_ROW"} = $rowref;
576 577
    $inst->{"FORCED"} = 0;
    $inst->{"HAS_FIELDS"} = 1;
Weibin Sun's avatar
Weibin Sun committed
578 579 580

    # wire mapping
    
Weibin Sun's avatar
Weibin Sun committed
581 582 583 584 585 586 587 588
    bless($inst, $class);

    $allports{$strtriple} = $inst;
    $allports{Tokens2IfaceString($class, $nodeid, $iface)} = $inst;

    return $inst;
}

Weibin Sun's avatar
Weibin Sun committed
589 590 591 592 593 594 595 596 597 598 599 600 601 602
sub LookupByIfaces($@)
{
    my ($c, @ifs) = @_;

    return map(Port->LookupByIface($_), @ifs);
}

sub LookupByTriples($@)
{
    my ($c, @ifs) = @_;

    return map(Port->LookupByTriple($_), @ifs);
}

Weibin Sun's avatar
Weibin Sun committed
603 604 605 606
#
# Class method
# Get all ports by their wire type
#
Weibin Sun's avatar
Weibin Sun committed
607 608 609 610 611 612
sub LookupByWireType($$)
{
	my ($c, $wt) = @_;
	my @ports = ();
	
	my $result = DBQueryFatal("SELECT node_id1, card1, port1, " .
613 614
		"node_id2, card2, port2 FROM wires ".
                "WHERE type='$wt' and logical=0");
Weibin Sun's avatar
Weibin Sun committed
615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632

	if ($result) {
		while (my @row = $result->fetchrow()) {
			my ($node_id1, $card1, $port1, $node_id2, $card2, $port2)  = @row;
			my $p1 = Port->LookupByTriple($node_id1, $card1, $port1);
			if (defined($p1)) {
				push @ports, $p1;
			}
			my $p2 = Port->LookupByTriple($node_id2, $card2, $port2);
			if (defined($p2)) {
				push @ports, $p2;
			}
		}
	}
	
	return @ports;
}

633
sub field($$)  { return (((! ref($_[0])) || ($_[0]->{'HAS_FIELDS'} == 0)) ? 
634
    -1 : $_[0]->{'INTERFACES_ROW'}->{$_[1]}); }
Weibin Sun's avatar
Weibin Sun committed
635 636 637 638 639 640 641 642 643 644
sub node_id($) { return field($_[0], 'node_id'); }
sub card($)    { return field($_[0], 'card'); }
sub port($)    { return field($_[0], 'port'); }
sub iface($)   { return field($_[0], 'iface'); }
sub mac($)     { return field($_[0], 'mac'); }
sub IP($)      { return field($_[0], 'IP'); }
sub role($)    { return field($_[0], 'role'); }
sub interface_type($)    { return field($_[0], 'interface_type'); }
sub mask($)    { return field($_[0], 'mask'); }
sub uuid($)    { return field($_[0], 'uuid'); }
645
sub trunk($)   { return field($_[0], 'trunk'); }
646
sub trunk_mode($) { return field($_[0], 'trunk_mode'); }
Weibin Sun's avatar
Weibin Sun committed
647 648

sub wire_end($) { return $_[0]->{'WIRE_END'}; }
649
sub is_switch_side($) { return $_[0]->wire_end() eq "switch"; }
Weibin Sun's avatar
Weibin Sun committed
650 651

sub wire_type($)   { return $_[0]->{'WIRES_ROW'}->{'type'}; }
652
sub is_trunk_port($)  { return $_[0]->wire_type() eq "Trunk"; }
Weibin Sun's avatar
Weibin Sun committed
653

654 655 656 657
sub is_forced($) { return $_[0]->{"FORCED"};}
sub has_fields($) { return $_[0]->{"HAS_FIELDS"};}
sub raw_string($) { return $_[0]->{"RAW_STRING"};}

Weibin Sun's avatar
Weibin Sun committed
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
sub switch_node_id($)
{
    my $self = shift;
    if ($self->is_switch_side()) {
	return $self->node_id();
    } else {
	return $self->other_end_node_id();
    }
}

sub switch_card($)
{
    my $self = shift;
    if ($self->is_switch_side()) {
	return $self->card();
    } else {
	return $self->other_end_card();
    }
}

sub switch_port($)
{
    my $self = shift;
    if ($self->is_switch_side()) {
	return $self->port();
    } else {
	return $self->other_end_port();
    }
}

sub switch_iface($)
{
    my $self = shift;
    if ($self->is_switch_side()) {
	return $self->iface();
    } else {
	return $self->other_end_iface();
    }
}

sub pc_node_id($)
{
    my $self = shift;
701
    if (!$self->is_switch_side()) {
Weibin Sun's avatar
Weibin Sun committed
702 703 704 705 706 707 708 709 710
	return $self->node_id();
    } else {
	return $self->other_end_node_id();
    }
}

sub pc_card($)
{
    my $self = shift;
711
    if (!$self->is_switch_side()) {
Weibin Sun's avatar
Weibin Sun committed
712 713 714 715 716 717 718 719 720
	return $self->card();
    } else {
	return $self->other_end_card();
    }
}

sub pc_port($)
{
    my $self = shift;
721
    if (!$self->is_switch_side()) {
722
	return $self->port();
Weibin Sun's avatar
Weibin Sun committed
723 724 725 726 727 728 729 730
    } else {
	return $self->other_end_port();
    }
}

sub pc_iface($)
{
    my $self = shift;
731
    if (!$self->is_switch_side()) {
Weibin Sun's avatar
Weibin Sun committed
732 733 734 735 736 737
	return $self->iface();
    } else {
	return $self->other_end_iface();
    }
}

Weibin Sun's avatar
Weibin Sun committed
738 739 740
#
# get node_id field of the other end Port instance
#
Weibin Sun's avatar
Weibin Sun committed
741 742 743
sub other_end_node_id($)   
{ 
    my $self = $_[0];
744 745 746 747 748 749 750 751
    
    if ($self->is_forced) {
	if ($self->has_fields()) {
	    return $self->node_id();
	} else {
	    return $self->raw_string();
	}
    }
Weibin Sun's avatar
Weibin Sun committed
752

753
    if ($self->wire_end() eq "pc") {
Weibin Sun's avatar
Weibin Sun committed
754 755 756 757 758 759
	return $self->{'WIRES_ROW'}->{'node_id2'}; 
    } else {
	return $self->{'WIRES_ROW'}->{'node_id1'};
    }
}

Weibin Sun's avatar
Weibin Sun committed
760 761 762
#
# get card field of the other end Port instance
#
Weibin Sun's avatar
Weibin Sun committed
763 764 765
sub other_end_card($) 
{
    my $self = $_[0];
766 767 768 769 770 771 772 773
    
    if ($self->is_forced) {
	if ($self->has_fields()) {
	    return $self->card();
	} else {
	    return $self->raw_string();
	}
    }
Weibin Sun's avatar
Weibin Sun committed
774

775
    if ($self->wire_end() eq "pc") {
Weibin Sun's avatar
Weibin Sun committed
776 777 778 779 780 781
	return $self->{'WIRES_ROW'}->{'card2'}; 
    } else {
	return $self->{'WIRES_ROW'}->{'card1'};
    }
}

Weibin Sun's avatar
Weibin Sun committed
782 783 784
#
# get port field of the other end Port instance
#
Weibin Sun's avatar
Weibin Sun committed
785 786 787
sub other_end_port($) 
{
    my $self = $_[0];
788 789 790 791 792 793 794 795
    
    if ($self->is_forced) {
	if ($self->has_fields()) {
	    return $self->port();
	} else {
	    return $self->raw_string();
	}
    }
Weibin Sun's avatar
Weibin Sun committed
796

797
    if ($self->wire_end() eq "pc") {
Weibin Sun's avatar
Weibin Sun committed
798 799 800 801 802 803
	return $self->{'WIRES_ROW'}->{'port2'}; 
    } else {
	return $self->{'WIRES_ROW'}->{'port1'};
    }
}

Weibin Sun's avatar
Weibin Sun committed
804 805 806
#
# get iface field of the other end Port instance
#
Weibin Sun's avatar
Weibin Sun committed
807 808 809
sub other_end_iface($)
{
    my $self = $_[0];
810 811 812 813 814 815 816 817
    
    if ($self->is_forced) {
	if ($self->has_fields()) {
	    return $self->iface();
	} else {
	    return $self->raw_string();
	}
    }
Weibin Sun's avatar
Weibin Sun committed
818

819
    if ($self->wire_end() eq "pc") {
Weibin Sun's avatar
Weibin Sun committed
820 821 822 823 824 825 826 827 828 829 830 831
	return Port->LookupByTriple(
	    $self->{'WIRES_ROW'}->{'node_id2'},
	    $self->{'WIRES_ROW'}->{'card2'},
	    $self->{'WIRES_ROW'}->{'port2'})->iface(); 
    } else {
	return Port->LookupByTriple(
	    $self->{'WIRES_ROW'}->{'node_id1'},
	    $self->{'WIRES_ROW'}->{'card1'},
	    $self->{'WIRES_ROW'}->{'port1'})->iface(); 
    }
}

Weibin Sun's avatar
Weibin Sun committed
832 833 834
#
# to the 'iface' string representation
#
Weibin Sun's avatar
Weibin Sun committed
835
sub toIfaceString($) {
836 837 838 839 840
    my $self = shift;
    if (!$self->has_fields()) {
	return $self->raw_string();
    }
    return Tokens2IfaceString($self->node_id(), $self->iface());
Weibin Sun's avatar
Weibin Sun committed
841 842
}
    
Weibin Sun's avatar
Weibin Sun committed
843 844 845
#
# to the 'triple' string representation
# 
Weibin Sun's avatar
Weibin Sun committed
846
sub toTripleString($) {
847 848 849 850 851
    my $self = shift;
    if (!$self->has_fields()) {
	return $self->raw_string();
    }
    return Tokens2TripleString($self->node_id(), $self->card(), $self->port());
Weibin Sun's avatar
Weibin Sun committed
852 853
}

Weibin Sun's avatar
Weibin Sun committed
854 855 856
#
# to default string representation, which default is 'triple'
# 
Weibin Sun's avatar
Weibin Sun committed
857
sub toString($) {
858 859 860 861 862
    my $self = shift;
    if (!$self->has_fields()) {
	return $self->raw_string();
    }
    return $self->toTripleString();
Weibin Sun's avatar
Weibin Sun committed
863
}
Weibin Sun's avatar
Weibin Sun committed
864

Weibin Sun's avatar
Weibin Sun committed
865
#
Weibin Sun's avatar
Weibin Sun committed
866
# convert to the old "node:card" string
Weibin Sun's avatar
Weibin Sun committed
867 868 869 870 871
#
sub toNodeCardString($) {
    return Tokens2IfaceString($_[0]->node_id(), $_[0]->card());
}

Weibin Sun's avatar
Weibin Sun committed
872 873 874
#
# get port instance according to the given node_id
# 
875 876 877 878 879 880 881 882 883 884 885
sub getEndByNode($$) {
	my ($self, $n) = @_;
	if ($n eq $self->node_id()) {
		return $self;
	} elsif ($n eq $self->other_end_node_id()) { 
		return $self->getOtherEndPort();
	} else {
		return undef;
	}
}

Weibin Sun's avatar
Weibin Sun committed
886 887 888
#
# get the other side's triple string representation
#
Weibin Sun's avatar
Weibin Sun committed
889
sub getOtherEndTripleString($) {
Weibin Sun's avatar
Weibin Sun committed
890 891 892
    return Tokens2TripleString($_[0]->other_end_node_id(), $_[0]->other_end_card(), $_[0]->other_end_port());
}

Weibin Sun's avatar
Weibin Sun committed
893 894 895
#
# get the other side's iface string representation
#
Weibin Sun's avatar
Weibin Sun committed
896 897 898 899
sub getOtherEndIfaceString($) {
    return Tokens2IfaceString($_[0]->other_end_node_id(), $_[0]->other_end_iface());
}

Weibin Sun's avatar
Weibin Sun committed
900 901 902
#
# get the other side of a port instance, according to 'wires' DB table
#
Weibin Sun's avatar
Weibin Sun committed
903
sub getOtherEndPort($) {
Weibin Sun's avatar
Weibin Sun committed
904 905
    my $self = $_[0];
    my $pt = Port->LookupByTriple($self->getOtherEndTripleString());
Weibin Sun's avatar
Weibin Sun committed
906 907 908 909 910
    if (defined($pt)) {
	return $pt;
    } else {
	return $self;
    }
Weibin Sun's avatar
Weibin Sun committed
911 912
}

Weibin Sun's avatar
Weibin Sun committed
913
#
914
# get the PC side of a port instance
Weibin Sun's avatar
Weibin Sun committed
915
#
Weibin Sun's avatar
Weibin Sun committed
916 917
sub getPCPort($) {
    my $self = $_[0];
918 919 920 921
    
    if ($self->is_forced) {
	return $self;
    }
Weibin Sun's avatar
Weibin Sun committed
922

923
    if ($self->wire_end() eq "pc") {
Weibin Sun's avatar
Weibin Sun committed
924 925 926 927 928 929
	return $self;
    } else {
	return $self->getOtherEndPort();
    }
}

Weibin Sun's avatar
Weibin Sun committed
930
#
931
# get the switch side of a port instance
Weibin Sun's avatar
Weibin Sun committed
932
#
Weibin Sun's avatar
Weibin Sun committed
933 934
sub getSwitchPort($) {
    my $self = $_[0];
935 936 937 938
    
    if ($self->is_forced) {
	return $self;
    }
Weibin Sun's avatar
Weibin Sun committed
939

940
    if ($self->wire_end() ne "pc") {
Weibin Sun's avatar
Weibin Sun committed
941 942 943 944 945 946
	return $self;
    } else {
	return $self->getOtherEndPort();
    }
}

Weibin Sun's avatar
Weibin Sun committed
947 948 949
#
# convert an array of ports into string, in iface representation
#
950
sub toIfaceStrings($@)
Weibin Sun's avatar
Weibin Sun committed
951
{
952
	my ($c, @pts) = @_;
953 954 955 956 957 958
	if (@pts != 0) {
	    if (Port->isPort($pts[0])) {
		return join(" ", map($_->toIfaceString(), @pts)); 
	    }
	}
	return join(" ", @pts);
Weibin Sun's avatar
Weibin Sun committed
959 960
}

Weibin Sun's avatar
Weibin Sun committed
961 962 963
#
# convert an array of ports into string, in triple-filed representation
#
964
sub toTripleStrings($@)
Weibin Sun's avatar
Weibin Sun committed
965
{
966
	my ($c, @pts) = @_;
967 968 969 970 971 972
	if (@pts != 0) {
	    if (Port->isPort($pts[0])) {
		return join(" ", map($_->toTripleString(), @pts)); 
	    }
	}
	return join(" ", @pts);
Weibin Sun's avatar
Weibin Sun committed
973 974
}

Weibin Sun's avatar
Weibin Sun committed
975 976 977
#
# convert an array of ports into string, in default string representation
#
978
sub toStrings($@)
Weibin Sun's avatar
Weibin Sun committed
979
{
980
	my ($c, @pts) = @_;
981 982 983 984 985 986
	if (@pts != 0) {
	    if (Port->isPort($pts[0])) {
		return join(" ", map($_->toString(), @pts)); 
	    }
	}
	return join(" ", @pts);
Weibin Sun's avatar
Weibin Sun committed
987
}
988 989 990 991 992 993 994 995 996 997 998

#
# Stringify for output.
#
sub Stringify($)
{
    my ($self) = @_;
    
    return $self->toString();
}

Weibin Sun's avatar
Weibin Sun committed
999
return 1;