Port.pm 22.9 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, 2017 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
#
package Port;

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

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

use libdb;
58
use EmulabConstants;
59
use Interface;
Weibin Sun's avatar
Weibin Sun committed
60
61
use English;
use Data::Dumper;
62
use overload ('""' => 'Stringify');
Weibin Sun's avatar
Weibin Sun committed
63

64
65
66
67
# Local constants
my $WIRE_END_NODE   = "node";
my $WIRE_END_SWITCH = "switch";

68
69
# Cache of port instances
# node:iface OR node:card.port => Port Instance
Weibin Sun's avatar
Weibin Sun committed
70
71
my %allports = ();

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

75
76
77
78
79
80
81
82
83
84
85
86
#
# 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
87
88
89
90
91
92
#
# 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
{
    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
112
    if (defined($p)) {
Weibin Sun's avatar
Weibin Sun committed
113

Weibin Sun's avatar
Weibin Sun committed
114
115
	$wiredports{$p->toTripleString()} = $p->getOtherEndPort();
	$wiredports{$p->toIfaceString()} = $p->getOtherEndPort();
Weibin Sun's avatar
Weibin Sun committed
116

Weibin Sun's avatar
Weibin Sun committed
117
	return $p->getOtherEndPort();
Weibin Sun's avatar
Weibin Sun committed
118
119
120
121
122
    } else {
	return undef;
    }
}

Weibin Sun's avatar
Weibin Sun committed
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
158
159
160
161
162
163
#
# 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180

#
# 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
181
    return (undef, undef);
Weibin Sun's avatar
Weibin Sun committed
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
}

#
# 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
200
    return (undef, undef, undef);
Weibin Sun's avatar
Weibin Sun committed
201
202
}

Weibin Sun's avatar
Weibin Sun committed
203
204
205
206
sub ParseCardPortString($;$)
{
    my ($c, $cp) = @_;

Kirk Webb's avatar
Kirk Webb committed
207
    if (!defined($cp)) {
Weibin Sun's avatar
Weibin Sun committed
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
	$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
223
224
225
226
227
228
229
230
231
232
233
234
sub Iface2Triple($;$)
{
    my ($c, $striface) = @_;

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

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

	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);
260
261
262
	if (!defined($card) || !defined($port) || !defined($nodeid)) {
	    return undef;
	}
Weibin Sun's avatar
Weibin Sun 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
288
289
290
291
292
293
294
295
296
297

	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
298
299
300
301

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

Weibin Sun's avatar
Weibin Sun committed
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
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);
322
323
324
    if (!defined($n) || !defined($c) || !defined($p)) {
        return undef;
    }
Weibin Sun's avatar
Weibin Sun committed
325
326
327
328
329
330
331
332
333
334
335
336
337

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

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

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

    my ($n, $iface) = ParseIfaceString($i);
338
339
    
    if (defined($iface) && $iface =~ /^(.+)\/(.+)$/) {
Weibin Sun's avatar
Weibin Sun committed
340
341
342
343
344
345
	return ($n, $1, $2);
    }    

    return (undef, undef, undef);
}

Weibin Sun's avatar
Weibin Sun committed
346
347
348
349
350
351
352
353
#
# 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)
#
354
355
356
357
358
# Note from RPR: We have never represented the switch side of
# [interfaces] (except trunks) in the database. Most 'wires' table
# entries have a "real" [interface] on the PC side, but are just
# dangling references on the switch side.
#
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
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 {
388
	    $iface = Port->fake_CardPort2Iface($card, $port);
389
390
	}
    } else {
391
	$iface = Port->fake_CardPort2Iface($card, $port);
392
393
394
    }
    
    if (defined($port)) {
395
396
	my $irowref = {};
	my $wrowref = {};
397
398
399
400
401
	
	$inst->{"RAW_STRING"} = $str;
	$inst->{"FORCED"} = 1;
	$inst->{"HAS_FIELDS"} = 1;
	
402
403
404
405
406
	$irowref->{'iface'} = $iface;
	$irowref->{'node_id'} = $nodeid;
	$irowref->{'card'} = $card;
	$irowref->{'port'} = $port;
	$irowref->{'trunk_mode'} = "equal";
407
	$inst->{"INTERFACES_ROW"} = Interface->MakeFake($nodeid, $irowref);
408
409
410
411
412
413
414

	# XXX: Incomplete, but if the port isn't in the wires table,
	# what are we to do?  We know nothing about the other end.
	$wrowref->{'type'} = TBDB_WIRETYPE_UNUSED();
	$wrowref->{'node_id1'} = $nodeid;
	$wrowref->{'card1'} = $card;
	$wrowref->{'port1'} = $port;
415
	$inst->{"WIRES_ROW"} = Interface::Wire->MakeFake($wrowref);
416
417
418
419
420
421
422
423
424
425
426
    }
    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.
    #
427
    $inst->{"WIRE_END"} = $WIRE_END_SWITCH;
428
429
430
431
432
    
    bless($inst, $class);
    return $inst;
}

Weibin Sun's avatar
Weibin Sun committed
433
434
435
436
#
# Class method
# get the port instance according to its iface-string representation
#
Weibin Sun's avatar
Weibin Sun committed
437
438
sub LookupByIface($$;$)
{
439
    my ($class, $nodeid, $iface, $force) = @_;
440
    my $striface="";
Weibin Sun's avatar
Weibin Sun committed
441
442
443

    if (!defined($iface)) {
	$striface = $nodeid;
444
	($nodeid, $iface) = Port->ParseIfaceString($striface);	
Weibin Sun's avatar
Weibin Sun committed
445
    } else {
446
447
448
	$striface = Tokens2IfaceString($class, $nodeid, $iface);	
    }
    
449
    if (!defined($striface) || !defined($nodeid)) {
450
        return undef;
Weibin Sun's avatar
Weibin Sun committed
451
452
453
454
455
456
    }

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

457
458
    my $interface = Interface->LookupByIface($nodeid, $iface);
    if (!defined($interface)) {
Weibin Sun's avatar
Weibin Sun committed
459
	my ($n, $c, $p) = fake_IfaceString2TripleTokens($class, $striface);
460
	if (defined($p)) {
Weibin Sun's avatar
Weibin Sun committed
461
462
463
464
465
	    return LookupByTriple($class, $n, $c, $p);
	} else {
	    return undef;
	}
    }
466
467
468
469
470
    my $card = $interface->card();
    my $port = $interface->port();
    my $wire = Interface::Wire->LookupAny($nodeid, $card, $port);
    return undef
	if (!defined($wire));
Weibin Sun's avatar
Weibin Sun committed
471
472

    my $inst = {};
473
474
    $inst->{"INTERFACES_ROW"} = $interface;
    $inst->{"WIRES_ROW"} = $wire;
Weibin Sun's avatar
Weibin Sun committed
475

476
477
478
    if ($wire->type() eq TBDB_WIRETYPE_NODE() ||
	$wire->type() eq TBDB_WIRETYPE_CONTROL()) {
	if ($wire->node_id1() eq $nodeid) {
479
480
481
482
	    $inst->{"WIRE_END"} = $WIRE_END_NODE;
	} else {
	    $inst->{"WIRE_END"} = $WIRE_END_SWITCH;
	}
483
    } elsif ($wire->type() eq TBDB_WIRETYPE_TRUNK()) {
484
	$inst->{"WIRE_END"} = $WIRE_END_SWITCH;
485
    } elsif ($wire->node_id2() eq $nodeid) {
486
487
488
489
	$inst->{"WIRE_END"} = $WIRE_END_SWITCH;	
    } else {
	# XXX: Other cases are unhandled for now...
	return undef;
Weibin Sun's avatar
Weibin Sun committed
490
491
    }

492
493
    $inst->{"FORCED"} = 0;
    $inst->{"HAS_FIELDS"} = 1;
Weibin Sun's avatar
Weibin Sun committed
494
495
496
497
498
499
500
501
502

    bless($inst, $class);

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

    return $inst;
}

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

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

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

528
529
    my $inst = {};

Weibin Sun's avatar
Weibin Sun committed
530
    # wire mapping:
531
    my $wire = Interface::Wire->LookupAny($nodeid, $card, $port);
Weibin Sun's avatar
Weibin Sun committed
532
    return undef
533
534
535
536
	if (!defined($wire));
    
    if ($wire->type() eq TBDB_WIRETYPE_NODE() ||
	$wire->type() eq TBDB_WIRETYPE_CONTROL()) {
537
538
539
	# Emulab is consistent about using the node_id1, etc. fields for the
	# endpoint for the above wire types.  If it were not, we would need
	# to consult the 'nodes' table to see what role the node has.
540
	if ($wire->node_id1() eq $nodeid) {
541
542
543
544
	    $inst->{"WIRE_END"} = $WIRE_END_NODE;
	} else {
	    $inst->{"WIRE_END"} = $WIRE_END_SWITCH;
	}
545
    } elsif ($wire->type() eq TBDB_WIRETYPE_TRUNK()) {
546
	$inst->{"WIRE_END"} = $WIRE_END_SWITCH;
547
    } elsif ($wire->node_id2() eq $nodeid) {
548
549
550
551
552
553
	# This is a failsafe case for wire types that are 'exotic'.
	$inst->{"WIRE_END"} = $WIRE_END_SWITCH;	
    } else {
	# XXX: Other cases are unhandled for now...
	return undef;
    }
554
    $inst->{"WIRES_ROW"} = $wire;
Weibin Sun's avatar
Weibin Sun committed
555

556
557
558
559
560
561
562
563
564
    #
    # Lookup the interface for the correct side of the wire.
    #
    if ($wire->node_id2() eq $nodeid) {
	$interface = Interface->LookupByIface($nodeid, $wire->iface2());
    }
    else {
	$interface = Interface->LookupByIface($nodeid, $wire->iface1());
    }
565
566
567
568

    # Note: The code will almost always fall into this conditional
    # block for switch ports because we typically do not have entries
    # for them in the 'interfaces' table.
569
570
    if (!defined($interface)) {
	my $rowref = {};
Weibin Sun's avatar
Weibin Sun committed
571
572
573
574
575
	my $iface = fake_CardPort2Iface($card, $port);
	$rowref->{'iface'} = $iface;
	$rowref->{'node_id'} = $nodeid;
	$rowref->{'card'} = $card;
	$rowref->{'port'} = $port;
576
	$rowref->{'trunk_mode'} = "equal";
577
578
579
	$interface = Interface->MakeFake($nodeid, $rowref);
    } 
    my $iface = $interface->iface();
Weibin Sun's avatar
Weibin Sun committed
580

581
    $inst->{"INTERFACES_ROW"} = $interface;
582
583
    $inst->{"FORCED"} = 0;
    $inst->{"HAS_FIELDS"} = 1;
Weibin Sun's avatar
Weibin Sun committed
584
585
586

    # wire mapping
    
Weibin Sun's avatar
Weibin Sun committed
587
588
589
590
591
592
593
594
    bless($inst, $class);

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

    return $inst;
}

Weibin Sun's avatar
Weibin Sun committed
595
596
597
598
599
600
601
602
603
604
605
606
607
608
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
609
610
611
612
#
# Class method
# Get all ports by their wire type
#
Weibin Sun's avatar
Weibin Sun committed
613
614
615
616
617
618
sub LookupByWireType($$)
{
	my ($c, $wt) = @_;
	my @ports = ();
	
	my $result = DBQueryFatal("SELECT node_id1, card1, port1, " .
619
		"node_id2, card2, port2 FROM wires ".
620
                "WHERE type='$wt'");
Weibin Sun's avatar
Weibin Sun committed
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638

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

639
640
641
642
643
644
645
646
sub field($$)  {
    my ($self, $slot) = @_;

    return -1
	if ((! ref($_[0])) || ($_[0]->{'HAS_FIELDS'} == 0));

    return $self->{'INTERFACES_ROW'}->$slot();
}
Weibin Sun's avatar
Weibin Sun committed
647
648
649
650
651
652
sub node_id($) { return field($_[0], 'node_id'); }
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'); }
653
sub interface_type($)    { return field($_[0], 'type'); }
Weibin Sun's avatar
Weibin Sun committed
654
655
sub mask($)    { return field($_[0], 'mask'); }
sub uuid($)    { return field($_[0], 'uuid'); }
656
sub trunk($)   { return field($_[0], 'trunk'); }
657
sub trunk_mode($) { return field($_[0], 'trunk_mode'); }
658
sub logical($) { return field($_[0], 'logical'); }
659
660
661
662
# These two come from the "interface_state" table, which gives the
# current view vs. the "mandated" (mapped) state from the interfaces table.
sub tagged($)  { return field($_[0], 'tagged'); }
sub enabled($) { return field($_[0], 'enabled'); }
Weibin Sun's avatar
Weibin Sun committed
663
664

sub wire_end($) { return $_[0]->{'WIRE_END'}; }
665
sub is_switch_side($) { return $_[0]->wire_end() eq $WIRE_END_SWITCH; }
Weibin Sun's avatar
Weibin Sun committed
666

667
sub wire_type($)   { return $_[0]->{'WIRES_ROW'}->type(); }
668
sub is_trunk_port($)  { return $_[0]->wire_type() eq TBDB_WIRETYPE_TRUNK(); }
Weibin Sun's avatar
Weibin Sun committed
669

670
671
672
673
sub is_forced($) { return $_[0]->{"FORCED"};}
sub has_fields($) { return $_[0]->{"HAS_FIELDS"};}
sub raw_string($) { return $_[0]->{"RAW_STRING"};}

674
675
676
677
678
679
680
681
682
683
684
685
686
687
#
# When logical, convert the card back from logical number.
#
sub card($)
{
    my ($self) = shift;
    my $card   = field($self, 'card');

    if ($self->logical()) {
	$card = $card - 200;
    }
    return $card;
}

Weibin Sun's avatar
Weibin Sun committed
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
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;
731
732
    if (!$self->is_switch_side() ||
	$self->is_trunk_port()) {
Weibin Sun's avatar
Weibin Sun committed
733
734
735
736
737
738
739
740
741
	return $self->node_id();
    } else {
	return $self->other_end_node_id();
    }
}

sub pc_card($)
{
    my $self = shift;
742
743
    if (!$self->is_switch_side() ||
	$self->is_trunk_port()) {
Weibin Sun's avatar
Weibin Sun committed
744
745
746
747
748
749
750
751
752
	return $self->card();
    } else {
	return $self->other_end_card();
    }
}

sub pc_port($)
{
    my $self = shift;
753
754
    if (!$self->is_switch_side() ||
	$self->is_trunk_port()) {
755
	return $self->port();
Weibin Sun's avatar
Weibin Sun committed
756
757
758
759
760
761
762
763
    } else {
	return $self->other_end_port();
    }
}

sub pc_iface($)
{
    my $self = shift;
764
765
    if (!$self->is_switch_side() ||
	$self->is_trunk_port()) {
Weibin Sun's avatar
Weibin Sun committed
766
767
768
769
770
771
	return $self->iface();
    } else {
	return $self->other_end_iface();
    }
}

Weibin Sun's avatar
Weibin Sun committed
772
773
774
#
# get node_id field of the other end Port instance
#
Weibin Sun's avatar
Weibin Sun committed
775
776
777
sub other_end_node_id($)   
{ 
    my $self = $_[0];
778
779
780
781
782
783
784
785
    
    if ($self->is_forced) {
	if ($self->has_fields()) {
	    return $self->node_id();
	} else {
	    return $self->raw_string();
	}
    }
Weibin Sun's avatar
Weibin Sun committed
786

787
788
    if ($self->node_id() eq $self->{'WIRES_ROW'}->node_id1()) {
	return $self->{'WIRES_ROW'}->node_id2(); 
Weibin Sun's avatar
Weibin Sun committed
789
    } else {
790
	return $self->{'WIRES_ROW'}->node_id1();
Weibin Sun's avatar
Weibin Sun committed
791
792
793
    }
}

Weibin Sun's avatar
Weibin Sun committed
794
795
796
#
# get card field of the other end Port instance
#
Weibin Sun's avatar
Weibin Sun committed
797
798
799
sub other_end_card($) 
{
    my $self = $_[0];
800
801
802
803
804
805
806
807
    
    if ($self->is_forced) {
	if ($self->has_fields()) {
	    return $self->card();
	} else {
	    return $self->raw_string();
	}
    }
808
    my $card;
Weibin Sun's avatar
Weibin Sun committed
809

810
811
    if ($self->node_id() eq $self->{'WIRES_ROW'}->node_id1()) {
	$card = $self->{'WIRES_ROW'}->card2(); 
Weibin Sun's avatar
Weibin Sun committed
812
    } else {
813
	$card = $self->{'WIRES_ROW'}->card1();
814
815
816
    }
    if ($self->logical()) {
	$card = $card - 200;
Weibin Sun's avatar
Weibin Sun committed
817
    }
818
    return $card;
Weibin Sun's avatar
Weibin Sun committed
819
820
}

Weibin Sun's avatar
Weibin Sun committed
821
822
823
#
# get port field of the other end Port instance
#
Weibin Sun's avatar
Weibin Sun committed
824
825
826
sub other_end_port($) 
{
    my $self = $_[0];
827
828
829
830
831
832
833
834
    
    if ($self->is_forced) {
	if ($self->has_fields()) {
	    return $self->port();
	} else {
	    return $self->raw_string();
	}
    }
Weibin Sun's avatar
Weibin Sun committed
835

836
837
    if ($self->node_id() eq $self->{'WIRES_ROW'}->node_id1()) {
	return $self->{'WIRES_ROW'}->port2(); 
Weibin Sun's avatar
Weibin Sun committed
838
    } else {
839
	return $self->{'WIRES_ROW'}->port1();
Weibin Sun's avatar
Weibin Sun committed
840
841
842
    }
}

Weibin Sun's avatar
Weibin Sun committed
843
844
845
#
# get iface field of the other end Port instance
#
Weibin Sun's avatar
Weibin Sun committed
846
847
848
sub other_end_iface($)
{
    my $self = $_[0];
849
850
851
852
853
854
855
856
    
    if ($self->is_forced) {
	if ($self->has_fields()) {
	    return $self->iface();
	} else {
	    return $self->raw_string();
	}
    }
Weibin Sun's avatar
Weibin Sun committed
857

858
    if ($self->node_id() eq $self->{'WIRES_ROW'}->node_id1()) {
Weibin Sun's avatar
Weibin Sun committed
859
	return Port->LookupByTriple(
860
861
862
	    $self->{'WIRES_ROW'}->node_id2(),
	    $self->{'WIRES_ROW'}->card2(),
	    $self->{'WIRES_ROW'}->port2())->iface(); 
Weibin Sun's avatar
Weibin Sun committed
863
864
    } else {
	return Port->LookupByTriple(
865
866
867
	    $self->{'WIRES_ROW'}->node_id1(),
	    $self->{'WIRES_ROW'}->card1(),
	    $self->{'WIRES_ROW'}->port1())->iface(); 
Weibin Sun's avatar
Weibin Sun committed
868
869
870
    }
}

Weibin Sun's avatar
Weibin Sun committed
871
872
873
#
# to the 'iface' string representation
#
Weibin Sun's avatar
Weibin Sun committed
874
sub toIfaceString($) {
875
876
877
878
879
    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
880
881
}
    
Weibin Sun's avatar
Weibin Sun committed
882
883
884
#
# to the 'triple' string representation
# 
Weibin Sun's avatar
Weibin Sun committed
885
sub toTripleString($) {
886
887
888
889
890
    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
891
892
}

Weibin Sun's avatar
Weibin Sun committed
893
894
895
#
# to default string representation, which default is 'triple'
# 
Weibin Sun's avatar
Weibin Sun committed
896
sub toString($) {
897
898
899
900
901
    my $self = shift;
    if (!$self->has_fields()) {
	return $self->raw_string();
    }
    return $self->toTripleString();
Weibin Sun's avatar
Weibin Sun committed
902
}
Weibin Sun's avatar
Weibin Sun committed
903

Weibin Sun's avatar
Weibin Sun committed
904
#
Weibin Sun's avatar
Weibin Sun committed
905
# convert to the old "node:card" string
Weibin Sun's avatar
Weibin Sun committed
906
907
908
909
910
#
sub toNodeCardString($) {
    return Tokens2IfaceString($_[0]->node_id(), $_[0]->card());
}

Weibin Sun's avatar
Weibin Sun committed
911
912
913
#
# get port instance according to the given node_id
# 
914
915
916
917
918
919
920
921
922
923
924
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
925
926
927
#
# get the other side's triple string representation
#
Weibin Sun's avatar
Weibin Sun committed
928
sub getOtherEndTripleString($) {
Weibin Sun's avatar
Weibin Sun committed
929
930
931
    return Tokens2TripleString($_[0]->other_end_node_id(), $_[0]->other_end_card(), $_[0]->other_end_port());
}

Weibin Sun's avatar
Weibin Sun committed
932
933
934
#
# get the other side's iface string representation
#
Weibin Sun's avatar
Weibin Sun committed
935
936
937
938
sub getOtherEndIfaceString($) {
    return Tokens2IfaceString($_[0]->other_end_node_id(), $_[0]->other_end_iface());
}

Weibin Sun's avatar
Weibin Sun committed
939
940
941
#
# get the other side of a port instance, according to 'wires' DB table
#
Weibin Sun's avatar
Weibin Sun committed
942
sub getOtherEndPort($) {
Weibin Sun's avatar
Weibin Sun committed
943
944
    my $self = $_[0];
    my $pt = Port->LookupByTriple($self->getOtherEndTripleString());
Weibin Sun's avatar
Weibin Sun committed
945
946
947
948
949
    if (defined($pt)) {
	return $pt;
    } else {
	return $self;
    }
Weibin Sun's avatar
Weibin Sun committed
950
951
}

Weibin Sun's avatar
Weibin Sun committed
952
#
953
954
955
956
# get the PC side of a port instance.  It is bogus to call this on an
# inter-switch trunk port, but we return the local ("this") side
# anyway in this case since some snmpit code using this method doesn't
# check the link type.
Weibin Sun's avatar
Weibin Sun committed
957
#
Weibin Sun's avatar
Weibin Sun committed
958
959
sub getPCPort($) {
    my $self = $_[0];
960
961
962
963
    
    if ($self->is_forced) {
	return $self;
    }
Weibin Sun's avatar
Weibin Sun committed
964

965
966
    if (!$self->is_switch_side() ||
	$self->is_trunk_port()) {
Weibin Sun's avatar
Weibin Sun committed
967
968
969
970
971
972
	return $self;
    } else {
	return $self->getOtherEndPort();
    }
}

Weibin Sun's avatar
Weibin Sun committed
973
#
974
975
976
# get the switch side of a port instance.  This call is ambiguous in
# the case of an inter-switch trunk port, and will always return "this"
# port.
Weibin Sun's avatar
Weibin Sun committed
977
#
Weibin Sun's avatar
Weibin Sun committed
978
979
sub getSwitchPort($) {
    my $self = $_[0];
980
981
982
983
    
    if ($self->is_forced) {
	return $self;
    }
Weibin Sun's avatar
Weibin Sun committed
984

985
    if ($self->is_switch_side()) {
Weibin Sun's avatar
Weibin Sun committed
986
987
988
989
990
991
	return $self;
    } else {
	return $self->getOtherEndPort();
    }
}

Weibin Sun's avatar
Weibin Sun committed
992
993
994
#
# convert an array of ports into string, in iface representation
#
995
sub toIfaceStrings($@)
Weibin Sun's avatar
Weibin Sun committed
996
{
997
	my ($c, @pts) = @_;
998
999
1000
1001
1002
1003
	if (@pts != 0) {
	    if (Port->isPort($pts[0])) {
		return join(" ", map($_->toIfaceString(), @pts)); 
	    }
	}
	return join(" ", @pts);
Weibin Sun's avatar
Weibin Sun committed
1004
1005
}

Weibin Sun's avatar
Weibin Sun committed
1006
1007
1008
#
# convert an array of ports into string, in triple-filed representation
#
1009
sub toTripleStrings($@)
Weibin Sun's avatar
Weibin Sun committed
1010
{
1011
	my ($c, @pts) = @_;
1012
1013
1014
1015
1016
1017
	if (@pts != 0) {
	    if (Port->isPort($pts[0])) {
		return join(" ", map($_->toTripleString(), @pts)); 
	    }
	}
	return join(" ", @pts);
Weibin Sun's avatar
Weibin Sun committed
1018
1019
}

Weibin Sun's avatar
Weibin Sun committed
1020
1021
1022
#
# convert an array of ports into string, in default string representation
#
1023
sub toStrings($@)
Weibin Sun's avatar
Weibin Sun committed
1024
{
1025
	my ($c, @pts) = @_;
1026
1027
1028
1029
1030
1031
	if (@pts != 0) {
	    if (Port->isPort($pts[0])) {
		return join(" ", map($_->toString(), @pts)); 
	    }
	}
	return join(" ", @pts);
Weibin Sun's avatar
Weibin Sun committed
1032
}
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043

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

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