blib/lib/App/Netdisco/DB/Result/Node.pm untested

filter: Statementstmt 0.0% 0 / 46 Branchbran n/a Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 60 CC 1 SCAR 6.9
CC
1
Cov
0%
time_first_stamp
2.0
time_last_stamp
2.0
net_mac
2.0
CRAP 2.0
1 use utf8;
2 package App::Netdisco::DB::Result::Node;
3  
4  
5 use strict;
6 use warnings;
7  
8 use NetAddr::MAC;
9  
10 use base 'App::Netdisco::DB::Result';
11 __PACKAGE__->table("node");
12 __PACKAGE__->add_columns(
13 "mac",
14 { data_type => "macaddr", is_nullable => 0 },
15 "switch",
16 { data_type => "inet", is_nullable => 0 },
17 "port",
18 { data_type => "text", is_nullable => 0 },
19 "active",
20 { data_type => "boolean", is_nullable => 1 },
21 "oui",
22 { data_type => "varchar", is_nullable => 1, is_serializable => 0, size => 9 },
23 "time_first",
24 {
25 data_type => "timestamp",
26 default_value => \"LOCALTIMESTAMP",
27 is_nullable => 1,
28 original => { default_value => \"LOCALTIMESTAMP" },
29 },
30 "time_recent",
31 {
32 data_type => "timestamp",
33 default_value => \"LOCALTIMESTAMP",
34 is_nullable => 1,
35 original => { default_value => \"LOCALTIMESTAMP" },
36 },
37 "time_last",
38 {
39 data_type => "timestamp",
40 default_value => \"LOCALTIMESTAMP",
41 is_nullable => 1,
42 original => { default_value => \"LOCALTIMESTAMP" },
43 },
44 "vlan",
45 { data_type => "text", is_nullable => 0, default_value => '0' },
46 );
47 __PACKAGE__->set_primary_key("mac", "switch", "port", "vlan");
48  
49  
50  
51 =head1 RELATIONSHIPS
52  
53 =head2 device
54  
55 Returns the single C<device> to which this Node entry was associated at the
56 time of discovery.
57  
58 The JOIN is of type LEFT, in case the C<device> is no longer present in the
59 database but the relation is being used in C<search()>.
60  
61 =cut
62  
63 __PACKAGE__->belongs_to( device => 'App::Netdisco::DB::Result::Device',
64 { 'foreign.ip' => 'self.switch' }, { join_type => 'LEFT' } );
65  
66 =head2 device_port
67  
68 Returns the single C<device_port> to which this Node entry was associated at
69 the time of discovery.
70  
71 The JOIN is of type LEFT, in case the C<device> is no longer present in the
72 database but the relation is being used in C<search()>.
73  
74 =cut
75  
76 # device port may have been deleted (reconfigured modules?) but node remains
77 __PACKAGE__->belongs_to( device_port => 'App::Netdisco::DB::Result::DevicePort',
78 { 'foreign.ip' => 'self.switch', 'foreign.port' => 'self.port' },
79 { join_type => 'LEFT' }
80 );
81  
82 =head2 wireless_port
83  
84 Returns the single C<wireless_port> to which this Node entry was associated at
85 the time of discovery.
86  
87 The JOIN is of type LEFT, in case the C<device> is no longer present in the
88 database but the relation is being used in C<search()>.
89  
90 =cut
91  
92 __PACKAGE__->belongs_to(
93 wireless_port => 'App::Netdisco::DB::Result::DevicePortWireless',
94 { 'foreign.ip' => 'self.switch', 'foreign.port' => 'self.port' },
95 { join_type => 'LEFT' }
96 );
97  
98 =head2 ips
99  
100 Returns the set of C<node_ip> entries associated with this Node. That is, the
101 IP addresses which this MAC address was hosting at the time of discovery.
102  
103 Note that the Active status of the returned IP entries will all be the same as
104 the current Node's.
105  
106 =cut
107  
108 __PACKAGE__->has_many( ips => 'App::Netdisco::DB::Result::NodeIp',
109 { 'foreign.mac' => 'self.mac', 'foreign.active' => 'self.active' } );
110  
111 =head2 ip4s
112  
113 Same as C<ips> but for IPv4 only.
114  
115 =cut
116  
117 __PACKAGE__->has_many( ip4s => 'App::Netdisco::DB::Result::Virtual::NodeIp4',
118 { 'foreign.mac' => 'self.mac', 'foreign.active' => 'self.active' } );
119  
120 =head2 ip6s
121  
122 Same as C<ips> but for IPv6 only.
123  
124 =cut
125  
126 __PACKAGE__->has_many( ip6s => 'App::Netdisco::DB::Result::Virtual::NodeIp6',
127 { 'foreign.mac' => 'self.mac', 'foreign.active' => 'self.active' } );
128  
129 =head2 netbios
130  
131 Returns the C<node_nbt> entry associated with this Node if one exists. That
132 is, the NetBIOS information of this MAC address at the time of discovery.
133  
134 =cut
135  
136 __PACKAGE__->might_have( netbios => 'App::Netdisco::DB::Result::NodeNbt',
137 { 'foreign.mac' => 'self.mac' } );
138  
139 =head2 wireless
140  
141 Returns the set of C<node_wireless> entries associated with this Node. That
142 is, the SSIDs and wireless statistics associated with this MAC address
143 at the time of discovery.
144  
145 =cut
146  
147 __PACKAGE__->has_many( wireless => 'App::Netdisco::DB::Result::NodeWireless',
148 { 'foreign.mac' => 'self.mac' } );
149  
150 =head2 wireless_current
151  
152 Returns wireless client data observed during the associated device's most
153 recent macsuck. Older SSID rows are excluded in the JOIN so nodes without a
154 current wireless row remain available to reports.
155  
156 =cut
157  
158 __PACKAGE__->has_many( wireless_current => 'App::Netdisco::DB::Result::NodeWireless',
159 sub {
160 my $args = shift;
161 return {
162 "$args->{foreign_alias}.mac" => { -ident => "$args->{self_alias}.mac" },
163 "$args->{foreign_alias}.time_last" => { '>=' => \ "(SELECT last_macsuck FROM device WHERE device.ip = $args->{self_alias}.switch)" },
164 };
165 },
166 { join_type => 'LEFT' }
167 );
168  
169 =head2 oui
170  
171 DEPRECATED: USE MANUFACTURER INSTEAD
172  
173 Returns the C<oui> table entry matching this Node. You can then join on this
174 relation and retrieve the Company name from the related table.
175  
176 The JOIN is of type LEFT, in case the OUI table has not been populated.
177  
178 =cut
179  
180 __PACKAGE__->belongs_to( oui => 'App::Netdisco::DB::Result::Oui', 'oui',
181 { join_type => 'LEFT' } );
182  
183 =head2 manufacturer
184  
185 Returns the C<manufacturer> table entry matching this Node. You can then join on this
186 relation and retrieve the Company name from the related table.
187  
188 The JOIN is of type LEFT, in case the Manufacturer table has not been populated.
189  
190 =cut
191  
192 __PACKAGE__->belongs_to( manufacturer => 'App::Netdisco::DB::Result::Manufacturer', {
193 'foreign.base' => 'self.oui',
194 },
195 { join_type => 'LEFT' }
196 );
197  
198 =head1 ADDITIONAL COLUMNS
199  
200 =head2 time_first_stamp
201  
202 Formatted version of the C<time_first> field, accurate to the minute.
203  
204 The format is somewhat like ISO 8601 or RFC3339 but without the middle C<T>
205 between the date stamp and time stamp. That is:
206  
207 2012-02-06 12:49
208  
209 =cut
210  
211 sub time_first_stamp { return (shift)->get_column('time_first_stamp') }
212  
213 =head2 time_last_stamp
214  
215 Formatted version of the C<time_last> field, accurate to the minute.
216  
217 The format is somewhat like ISO 8601 or RFC3339 but without the middle C<T>
218 between the date stamp and time stamp. That is:
219  
220 2012-02-06 12:49
221  
222 =cut
223  
224 sub time_last_stamp { return (shift)->get_column('time_last_stamp') }
225  
226 =head2 net_mac
227  
228 Returns the C<mac> column instantiated into a L<NetAddr::MAC> object.
229  
230 =cut
231  
232 sub net_mac { return NetAddr::MAC->new(mac => ((shift)->mac || '')) }
233  
234 1;