File Coverage

lib/NetAddr/BridgeID.pm
Criterion Covered Total %
statement 16 18 88.8
branch n/a
condition n/a
subroutine 6 6 100.0
pod n/a
total 22 24 91.6


line stmt bran cond sub pod time code
1             package NetAddr::BridgeID;
2              
3             our $VERSION = '0.97'; # VERSION
4             # ABSTRACT: Object for BridgeIDs (priority/MAC combos)
5              
6 1     1   51693 use sanity;
  1         517853  
  1         16  
7 1     1   405957 use NetAddr::MAC;
  1         6296  
  1         69  
8 1     1   11 use Scalar::Util qw/blessed/;
  1         2  
  1         87  
9              
10 1     1   912 use Moo;
  1         13648  
  1         7  
11 1     1   13724 use MooX::Types::MooseLike::Base qw/InstanceOf Str/;
  1         12896  
  1         167  
12 1     1   750 use MooX::Types::CLike qw/UShort/;
  0            
  0            
13              
14             has original => (
15             is => 'ro',
16             isa => Str,
17             required => 1,
18             );
19             has priority => (
20             is => 'ro',
21             isa => UShort,
22             required => 1,
23             );
24             has mac_obj => (
25             is => 'ro',
26             isa => InstanceOf['NetAddr::MAC'],
27             required => 1,
28             handles => {
29             (map { $_ => $_ } qw(
30             is_eui48
31             is_eui64
32             is_multicast
33             is_unicast
34             is_local
35             is_universal
36             as_basic
37             as_bpr
38             as_cisco
39             as_ieee
40             as_ipv6_suffix
41             as_microsoft
42             as_singledash
43             as_sun
44             as_tokenring
45             to_eui48
46             to_eui64
47             )),
48             qw(
49             mac original
50             ),
51             },
52             );
53              
54             sub bridge_id { $_[0]->priority.'#'.$_[0]->as_cisco; }
55              
56              
57             around BUILDARGS => sub {
58             my ($orig, $self) = (shift, shift);
59             my %opts;
60              
61             if (@_ == 1) {
62             my $arg = pop;
63             if (blessed $arg) {
64             if ($arg->isa('NetAddr::BridgeID')) { $opts{bridge_id} = $arg->original; }
65             elsif ($arg->isa('NetAddr::MAC')) { $opts{mac_obj} = $arg; }
66             else { $opts{bridge_id} = $arg; }
67             }
68             elsif (ref $arg eq 'ARRAY') { %opts = @$arg; }
69             elsif (ref $arg eq 'HASH') { %opts = %$arg; }
70             else { $opts{bridge_id} = $arg; }
71             }
72             else { %opts = @_; }
73              
74             # parse vars from bridge_id
75             if (defined $opts{bridge_id}) {
76             $opts{bridge_id} =~ /^(\d+)\#(.+)$/;
77             $opts{priority} //= $1;
78             $opts{mac} //= $2;
79             }
80              
81             # parse mac from mac_obj
82             $opts{mac} //= $opts{mac_obj}->original if (defined $opts{mac_obj});
83              
84             # defaults
85             $opts{priority} //= 0;
86             $opts{bridge_id} //= $opts{priority}.'#'.$opts{mac};
87             #$opts{mac_obj} //= NetAddr::MAC->new( mac => $opts{mac} );
88            
89             # NetAddr::MAC has some weird issues with MAC translation here
90             # (see https://rt.cpan.org/Ticket/Display.html?id=79915)
91             unless ($opts{mac_obj}) {
92             my $new_mac = $opts{mac};
93             $new_mac =~ s/[^\da-f]//gi;
94             $new_mac =~ s/(.{4})(?=.)/$1./g;
95             $opts{mac_obj} = NetAddr::MAC->new( mac => $new_mac );
96             $opts{mac_obj}->{original} = $opts{mac}; # Ugly and hacky; remove when bug is fixed
97             }
98            
99             # bridge_id is actually 'original'
100             $opts{original} = delete $opts{bridge_id};
101             delete $opts{mac};
102              
103             $orig->($self, \%opts);
104             };
105              
106             1;
107              
108              
109              
110             =pod
111              
112             =encoding utf-8
113              
114             =head1 NAME
115              
116             NetAddr::BridgeID - Object for BridgeIDs (priority/MAC combos)
117              
118             =head1 SYNOPSIS
119              
120             use NetAddr::BridgeID;
121            
122             my $bid;
123             $bid = NetAddr::BridgeID->new( '2#00:11:22:aa:bb:cc' );
124             $bid = NetAddr::BridgeID->new( bridge_id => '60#0011.22AA.BBCC' );
125            
126             use NetAddr::MAC;
127             my $mac = NetAddr::MAC->new( mac => '0011.22AA.BBCC' );
128            
129             $bid = NetAddr::BridgeID->new( priority => '60', mac_obj => $mac );
130             $bid = NetAddr::BridgeID->new( priority => '60', mac => '0011.22AA.BBCC' );
131              
132             =head1 DESCRIPTION
133              
134             This object class simply creates a L<NetAddr::MAC>-like object, with a priority for
135             STP Bridge IDs.
136              
137             =for Pod::Coverage BUILDARGS
138              
139             =head1 METHODS
140              
141             =head2 original
142              
143             The original bridge ID string, as it was passed to the constructor. If it was passed
144             in pieces (or if parts were left out), it will fill in the gaps to provide a full
145             bridge ID
146              
147             =head2 bridge_id
148              
149             The bridge ID, with a Cisco-style MAC address (like C<0000.1111.2222>).
150              
151             =head2 priority
152              
153             The priority number.
154              
155             =head2 mac_obj
156              
157             The L<NetAddr::MAC> object tied to this one.
158              
159             =head2 NetAddr::MAC Methods
160              
161             All of the C<is_* / as_* / to_*> methods are "handled" to the main BridgeID object, so
162             you can access them directly.
163              
164             =head1 AVAILABILITY
165              
166             The project homepage is L<https://github.com/SineSwiper/NetAddr-BridgeID/wiki>.
167              
168             The latest version of this module is available from the Comprehensive Perl
169             Archive Network (CPAN). Visit L<http://www.perl.com/CPAN/> to find a CPAN
170             site near you, or see L<https://metacpan.org/module/NetAddr::BridgeID/>.
171              
172             =for :stopwords cpan testmatrix url annocpan anno bugtracker rt cpants kwalitee diff irc mailto metadata placeholders metacpan
173              
174             =head1 SUPPORT
175              
176             =head2 Internet Relay Chat
177              
178             You can get live help by using IRC ( Internet Relay Chat ). If you don't know what IRC is,
179             please read this excellent guide: L<http://en.wikipedia.org/wiki/Internet_Relay_Chat>. Please
180             be courteous and patient when talking to us, as we might be busy or sleeping! You can join
181             those networks/channels and get help:
182              
183             =over 4
184              
185             =item *
186              
187             irc.perl.org
188              
189             You can connect to the server at 'irc.perl.org' and join this channel: #distzilla then talk to this person for help: SineSwiper.
190              
191             =back
192              
193             =head2 Bugs / Feature Requests
194              
195             Please report any bugs or feature requests via L<L<https://github.com/SineSwiper/NetAddr-BridgeID/issues>|GitHub>.
196              
197             =head1 AUTHOR
198              
199             Brendan Byrd <BBYRD@CPAN.org>
200              
201             =head1 COPYRIGHT AND LICENSE
202              
203             This software is Copyright (c) 2013 by Brendan Byrd.
204              
205             This is free software, licensed under:
206              
207             The Artistic License 2.0 (GPL Compatible)
208              
209             =cut
210              
211              
212             __END__
213