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

filter: Statementstmt 0.0% 0 / 146 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 13 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 0.0% 0 / 24 Podpod 0.0% 0 / 16 totaltotal 0.0% 0 / 212 CC 4 SCAR 12.3
CC
4
Cov
0%
renumber
12.0
has_layer
6.0
serializable_columns
2.0
CRAP 3.4
1 use utf8;
2 package App::Netdisco::DB::Result::Device;
3  
4 use strict;
5 use warnings;
6  
7 use NetAddr::IP::Lite ':lower';
8 use App::Netdisco::Util::DNS 'hostname_from_ip';
9 use App::Netdisco::Util::L3Domain 'with_l3_domain_lock';
10  
11 use overload '""' => sub { shift->ip }, fallback => 1;
12  
13 use base 'App::Netdisco::DB::Result';
14 __PACKAGE__->table("device");
15 __PACKAGE__->add_columns(
16 "ip",
17 { data_type => "inet", is_nullable => 0 },
18 "creation",
19 {
20 data_type => "timestamp",
21 default_value => \"LOCALTIMESTAMP",
22 is_nullable => 1,
23 original => { default_value => \"LOCALTIMESTAMP" },
24 },
25 "dns",
26 { data_type => "text", is_nullable => 1 },
27 "description",
28 { data_type => "text", is_nullable => 1 },
29 "uptime",
30 { data_type => "bigint", is_nullable => 1 },
31 "contact",
32 { data_type => "text", is_nullable => 1 },
33 "name",
34 { data_type => "text", is_nullable => 1 },
35 "location",
36 { data_type => "text", is_nullable => 1 },
37 "layers",
38 { data_type => "varchar", is_nullable => 1, size => 8 },
39 "num_ports",
40 { data_type => "integer", is_serializable => 0, is_nullable => 1 },
41 "mac",
42 { data_type => "macaddr", is_nullable => 1 },
43 "serial",
44 { data_type => "text", is_nullable => 1 },
45 "chassis_id",
46 { data_type => "text", is_nullable => 1 },
47 "model",
48 { data_type => "text", is_nullable => 1 },
49 "ps1_type",
50 { data_type => "text", is_nullable => 1 },
51 "ps2_type",
52 { data_type => "text", is_nullable => 1 },
53 "ps1_status",
54 { data_type => "text", is_nullable => 1 },
55 "ps2_status",
56 { data_type => "text", is_nullable => 1 },
57 "fan",
58 { data_type => "text", is_nullable => 1 },
59 "slots",
60 { data_type => "integer", is_nullable => 1 },
61 "vendor",
62 { data_type => "text", is_nullable => 1 },
63 "os",
64 { data_type => "text", is_nullable => 1 },
65 "os_ver",
66 { data_type => "text", is_nullable => 1 },
67 "log",
68 { data_type => "text", is_nullable => 1 },
69 "snmp_ver",
70 { data_type => "integer", is_nullable => 1 },
71 "snmp_comm",
72 { data_type => "text", is_nullable => 1 },
73 "snmp_class",
74 { data_type => "text", is_nullable => 1 },
75 "snmp_engineid",
76 { data_type => "text", is_nullable => 1 },
77 "vtp_domain",
78 { data_type => "text", is_nullable => 1 },
79 "vtp_mode",
80 { data_type => "text", is_nullable => 1 },
81 "last_discover",
82 { data_type => "timestamp", is_nullable => 1 },
83 "last_macsuck",
84 { data_type => "timestamp", is_nullable => 1 },
85 "last_arpnip",
86 { data_type => "timestamp", is_nullable => 1 },
87 "is_pseudo",
88 { data_type => "boolean", is_nullable => 0, default_value => \"false" },
89 "pae_is_enabled",
90 { data_type => "boolean", is_nullable => 1 },
91 "custom_fields",
92 { data_type => "jsonb", is_nullable => 0, default_value => \"{}" },
93 "tags",
94 { data_type => "text[]", is_nullable => 0, default_value => \"'{}'::text[]" },
95 );
96 __PACKAGE__->set_primary_key("ip");
97  
98 # the stored SNMP read community is a credential, so it is kept out of the
99 # field list every API route's TO_JSON is built from
100 sub serializable_columns {
101 my $self = shift;
102 return [ grep { $_ ne 'snmp_comm' } @{ $self->next::method(@_) } ];
103 }
104  
105  
106 =head1 RELATIONSHIPS
107  
108 =head2 device_ips
109  
110 Returns rows from the C<device_ip> table which relate to this Device. That is,
111 all the interface IP aliases configured on the Device.
112  
113 =cut
114  
115 __PACKAGE__->has_many( device_ips => 'App::Netdisco::DB::Result::DeviceIp', 'ip' );
116  
117 =head2 vrfs
118  
119 Returns the C<device_vrf> rows: the VRF instances this Device reports.
120  
121 =cut
122  
123 __PACKAGE__->has_many( vrfs => 'App::Netdisco::DB::Result::DeviceVrf', 'ip' );
124  
125 =head2 l3_domain_members
126  
127 Returns the C<l3_domain_member> rows placing this Device's VRF instances in L3 domains.
128  
129 =cut
130  
131 __PACKAGE__->has_many( l3_domain_members => 'App::Netdisco::DB::Result::L3DomainMember', 'ip' );
132  
133 =head2 device_ips_by_address_or_name
134  
135 Returns rows from the C<device_ip> table which relate to this Device. That is,
136 all the interface IP aliases configured on the Device. However you probably
137 want to use the C<device_ips_with_address_or_name> ResultSet method instead,
138 so you can pass the MAC address part.
139  
140 =cut
141  
142 __PACKAGE__->has_many( device_ips_by_address_or_name => 'App::Netdisco::DB::Result::DeviceIp',
143 sub {
144 my $args = shift;
145 return {
146 "$args->{foreign_alias}.ip" => { -ident => "$args->{self_alias}.ip" },
147 -or => [
148 "$args->{foreign_alias}.dns" => { 'ilike', \'?' },
149 "$args->{foreign_alias}.alias" => { '<<=', \'?' },
150 "$args->{foreign_alias}.alias::text" => { 'ilike', \'?' },
151 ],
152 };
153 },
154 { cascade_copy => 0, cascade_update => 0, cascade_delete => 0 }
155 );
156  
157 =head2 vlans
158  
159 Returns the C<device_vlan> entries for this Device. That is, the list of VLANs
160 configured on or known by this Device.
161  
162 =cut
163  
164 __PACKAGE__->has_many( vlans => 'App::Netdisco::DB::Result::DeviceVlan', 'ip' );
165  
166 =head2 ports
167  
168 Returns the set of ports on this Device.
169  
170 =cut
171  
172 __PACKAGE__->has_many( ports => 'App::Netdisco::DB::Result::DevicePort', 'ip' );
173  
174 =head2 ports_by_mac
175  
176 Returns the set of ports on this Device, filtered by MAC. However you probably
177 want to use the C<ports_with_mac> ResultSet method instead, so you can pass the
178 MAC address part.
179  
180 =cut
181  
182 __PACKAGE__->has_many( ports_by_mac => 'App::Netdisco::DB::Result::DevicePort',
183 sub {
184 my $args = shift;
185 return {
186 "$args->{foreign_alias}.ip" => { -ident => "$args->{self_alias}.ip" },
187 "$args->{foreign_alias}.mac::text" => { 'ilike', \'?' },
188 };
189 },
190 { cascade_copy => 0, cascade_update => 0, cascade_delete => 0 }
191 );
192  
193 =head2 ports_by_exact_mac
194  
195 Like C<ports_by_mac>, but matches one complete MAC address by equality, which
196 the port MAC index can serve. Use C<ports_with_exact_mac> to pass the address.
197  
198 =cut
199  
200 __PACKAGE__->has_many( ports_by_exact_mac => 'App::Netdisco::DB::Result::DevicePort',
201 sub {
202 my $args = shift;
203 return {
204 "$args->{foreign_alias}.ip" => { -ident => "$args->{self_alias}.ip" },
205 "$args->{foreign_alias}.mac" => { '=', \'?' },
206 };
207 },
208 { cascade_copy => 0, cascade_update => 0, cascade_delete => 0 }
209 );
210  
211 =head2 module_serials
212  
213 Returns the set chassis modules on this Device.
214  
215 =cut
216  
217 __PACKAGE__->has_many( module_serials => 'App::Netdisco::DB::Result::DeviceModule',
218 sub {
219 my $args = shift;
220 return {
221 "$args->{foreign_alias}.ip" => { -ident => "$args->{self_alias}.ip" },
222 "$args->{foreign_alias}.class" => 'chassis',
223 -and => [
224 "$args->{foreign_alias}.serial" => { '!=' => undef },
225 "$args->{foreign_alias}.serial" => { '!=' => '' },
226 ],
227 };
228 },
229 { cascade_copy => 0, cascade_update => 0, cascade_delete => 0 }
230 );
231  
232 =head2 modules
233  
234 Returns the set chassis modules on this Device.
235  
236 =cut
237  
238 __PACKAGE__->has_many( modules => 'App::Netdisco::DB::Result::DeviceModule', 'ip' );
239  
240 =head2 power_modules
241  
242 Returns the set of power modules on this Device.
243  
244 =cut
245  
246 __PACKAGE__->has_many( power_modules => 'App::Netdisco::DB::Result::DevicePower', 'ip' );
247  
248 =head2 oids
249  
250 Returns the oids walked on this Device.
251  
252 =cut
253  
254 __PACKAGE__->has_many( oids => 'App::Netdisco::DB::Result::DeviceBrowser', 'ip' );
255  
256 =head2 port_vlans
257  
258 Returns the set of VLANs known to be configured on Ports on this Device,
259 either tagged or untagged.
260  
261 The JOIN is of type "RIGHT" meaning that the results are constrained to VLANs
262 only on Ports on this Device.
263  
264 =cut
265  
266 __PACKAGE__->has_many(
267 port_vlans => 'App::Netdisco::DB::Result::DevicePortVlan',
268 'ip', { join_type => 'RIGHT' }
269 );
270  
271 =head2 port_vlans_filter
272  
273 A JOIN condition which can be used to filter a set of Devices to those known
274 carrying a given VLAN on its ports. Uses an INNER JOIN to achieve this.
275  
276 =cut
277  
278 __PACKAGE__->has_many(
279 port_vlans_filter => 'App::Netdisco::DB::Result::DevicePortVlan',
280 'ip', { join_type => 'INNER' }
281 );
282  
283 # helper which assumes we've just RIGHT JOINed to Vlans table
284 sub vlan { return (shift)->vlans->first }
285  
286 =head2 wireless_ports
287  
288 Returns the set of wireless IDs known to be configured on Ports on this
289 Device.
290  
291 =cut
292  
293 __PACKAGE__->has_many(
294 wireless_ports => 'App::Netdisco::DB::Result::DevicePortWireless',
295 'ip', { join_type => 'RIGHT' }
296 );
297  
298 =head2 ssids
299  
300 Returns the set of SSIDs known to be configured on Ports on this Device.
301  
302 =cut
303  
304 __PACKAGE__->has_many(
305 ssids => 'App::Netdisco::DB::Result::DevicePortSsid',
306 'ip', { join_type => 'RIGHT' }
307 );
308  
309 =head2 properties_ports
310  
311 Returns the set of ports known to have recorded properties
312  
313 =cut
314  
315 __PACKAGE__->has_many(
316 properties_ports => 'App::Netdisco::DB::Result::DevicePortProperties',
317 'ip', { join_type => 'RIGHT' }
318 );
319  
320 =head2 powered_ports
321  
322 Returns the set of ports known to have PoE capability
323  
324 =cut
325  
326 __PACKAGE__->has_many(
327 powered_ports => 'App::Netdisco::DB::Result::DevicePortPower',
328 'ip', { join_type => 'RIGHT' }
329 );
330  
331 =head2 community
332  
333 Returns the row from the community string table, if one exists.
334  
335 =cut
336  
337 __PACKAGE__->might_have(
338 community => 'App::Netdisco::DB::Result::Community', 'ip');
339  
340 =head2 throughput
341  
342 Returns a sum of speeds on all ports on the device.
343  
344 =cut
345  
346 __PACKAGE__->has_one(
347 throughput => 'App::Netdisco::DB::Result::Virtual::DevicePortSpeed', 'ip');
348  
349 =head1 ADDITIONAL METHODS
350  
351 =head2 has_layer( $number )
352  
353 Returns true if the device provided sysServices and supports the given layer.
354  
355 =cut
356  
357 sub has_layer {
358 my ($device, $layer) = @_;
359 return unless $layer and $layer =~ m/^[1-7]$/;
360 return ($device->layers and (substr($device->layers, (8-$layer), 1) == 1));
361 }
362  
363 =head2 renumber( $new_ip )
364  
365 Will update this device and all related database records to use the new IP
366 C<$new_ip>. Returns C<undef> if $new_ip seems invalid, otherwise returns the
367 Device row object.
368  
369 =cut
370  
371 sub renumber {
372 my ($device, $ip) = @_;
373 my $schema = $device->result_source->schema;
374  
375 my $new_addr = NetAddr::IP::Lite->new($ip)
376 or return;
377  
378 my $old_ip = $device->ip;
379 my $new_ip = $new_addr->addr;
380  
381 return if $new_ip eq '0.0.0.0';
382  
383 # the special record in device_ip which is always there
384 $schema->resultset('DeviceIp')
385 ->search({ip => $old_ip, alias => $old_ip})
386 ->update({ip => $new_ip, alias => $new_ip});
387 $schema->resultset('DeviceIp')
388 ->search({ip => $old_ip, alias => $new_ip})->delete();
389  
390 # Community is not included as SNMP::test_connection will take care of it
391 with_l3_domain_lock($schema, sub {
392 foreach my $set (qw/
393 DeviceBrowser
394 DeviceIp
395 DeviceModule
396 DevicePower
397 DeviceVlan
398 DeviceVrf
399 L3DomainMember
400 DevicePort
401 DevicePortLog
402 DevicePortPower
403 DevicePortProperties
404 DevicePortSsid
405 DevicePortVlan
406 DevicePortWireless
407 /) {
408 $schema->resultset($set)
409 ->search({ip => $old_ip})
410 ->update({ip => $new_ip});
411 }
412 });
413  
414 $schema->resultset('DeviceSkip')
415 ->search({device => $new_ip})->delete;
416 $schema->resultset('DeviceSkip')
417 ->search({device => $old_ip})
418 ->update({device => $new_ip});
419  
420 $schema->resultset('DevicePort')
421 ->search({remote_ip => $old_ip})
422 ->update({remote_ip => $new_ip});
423  
424 $schema->resultset('Node')
425 ->search({switch => $old_ip})
426 ->update({switch => $new_ip});
427  
428 # this whole shenanigans exists because I cannot work out how to
429 # pass an escaped SQL placeholder into DBIx::Class/SQL::Abstract
430 # see https://www.mail-archive.com/dbix-class@lists.scsys.co.uk/msg07079.html
431 $schema->storage->dbh_do(sub {
432 my ($storage, $dbh, @extra) = @_;
433 local $dbh->{TraceLevel} = ($ENV{DBIC_TRACE} ? '1|SQL' : $dbh->{TraceLevel});
434  
435 my $router_first_sql = q{
436 UPDATE node_ip
437 SET seen_on_router_first = seen_on_router_first - ?::text || jsonb_build_object(?::text, seen_on_router_first -> ?::text)
438 WHERE seen_on_router_first \? ?::text
439 };
440 $dbh->do($router_first_sql, undef, $old_ip, $new_ip, $old_ip, $old_ip);
441  
442 my $router_last_sql = q{
443 UPDATE node_ip
444 SET seen_on_router_last = seen_on_router_last - ?::text || jsonb_build_object(?::text, seen_on_router_last -> ?::text)
445 WHERE seen_on_router_last \? ?::text
446 };
447 $dbh->do($router_last_sql, undef, $old_ip, $new_ip, $old_ip, $old_ip);
448  
449 $dbh->do($_, undef, $new_ip, $old_ip) for (
450 'UPDATE node_ip SET router_last = ? WHERE router_last = ?',
451 'UPDATE subnets SET router_last = ? WHERE router_last = ?',
452 );
453 });
454  
455 $schema->resultset('Topology')
456 ->search({dev1 => $old_ip})
457 ->update({dev1 => $new_ip});
458  
459 $schema->resultset('Topology')
460 ->search({dev2 => $old_ip})
461 ->update({dev2 => $new_ip});
462  
463 $schema->resultset('Admin')->search({
464 device => $old_ip,
465 })->delete;
466  
467 $schema->resultset('AccessControlList')->search({
468 rules => { '&&' => \[ 'ARRAY[?]', $old_ip ] }
469 })->update({
470 rules => \[ 'ARRAY_REPLACE(rules, ?, ?)', $old_ip, $new_ip ]
471 });
472  
473 $device->update({
474 ip => $new_ip,
475 dns => (hostname_from_ip($new_ip)
476 || eval { $schema->resultset('DeviceIp')->search({ ip => $new_ip, alias => $new_ip })->first->dns } || undef),
477 });
478  
479 return $device;
480 }
481  
482 =head1 ADDITIONAL COLUMNS
483  
484 =head2 port_count
485  
486 Returns the number of ports on this device. Enable this
487 column by applying the C<with_port_count()> modifier to C<search()>.
488  
489 =cut
490  
491 sub port_count { return (shift)->get_column('port_count') }
492  
493 =head2 is_discoverable
494  
495 Returns the number of backends able to discover the device. Enable this
496 column by applying the C<with_layer_features()> modifier to C<search()>.
497  
498 =cut
499  
500 sub is_discoverable { return (shift)->get_column('is_discoverable') }
501  
502 =head2 is_macsuckable
503  
504 Returns the number of backends able to macsuck the device. Enable this
505 column by applying the C<with_layer_features()> modifier to C<search()>.
506  
507 =cut
508  
509 sub is_macsuckable { return (shift)->get_column('is_macsuckable') }
510  
511 =head2 is_arpnipable
512  
513 Returns the number of backends able to arpnip the device. Enable this
514 column by applying the C<with_layer_features()> modifier to C<search()>.
515  
516 =cut
517  
518 sub is_arpnipable { return (shift)->get_column('is_arpnipable') }
519  
520 =head2 uptime_age
521  
522 Formatted version of the C<uptime> field.
523  
524 The format is in "X days/months/years" style, similar to:
525  
526 1 year 4 months 05:46:00
527  
528 =cut
529  
530 sub uptime_age { return (shift)->get_column('uptime_age') }
531  
532 =head2 first_seen_stamp
533  
534 Formatted version of the C<creation> field, accurate to the minute.
535  
536 The format is somewhat like ISO 8601 or RFC3339 but without the middle C<T>
537 between the date stamp and time stamp. That is:
538  
539 2012-02-06 12:49
540  
541 =cut
542  
543 sub first_seen_stamp { return (shift)->get_column('first_seen_stamp') }
544  
545 =head2 last_discover_stamp
546  
547 Formatted version of the C<last_discover> field, accurate to the minute.
548  
549 The format is somewhat like ISO 8601 or RFC3339 but without the middle C<T>
550 between the date stamp and time stamp. That is:
551  
552 2012-02-06 12:49
553  
554 =cut
555  
556 sub last_discover_stamp { return (shift)->get_column('last_discover_stamp') }
557  
558 =head2 last_macsuck_stamp
559  
560 Formatted version of the C<last_macsuck> field, accurate to the minute.
561  
562 The format is somewhat like ISO 8601 or RFC3339 but without the middle C<T>
563 between the date stamp and time stamp. That is:
564  
565 2012-02-06 12:49
566  
567 =cut
568  
569 sub last_macsuck_stamp { return (shift)->get_column('last_macsuck_stamp') }
570  
571 =head2 last_arpnip_stamp
572  
573 Formatted version of the C<last_arpnip> field, accurate to the minute.
574  
575 The format is somewhat like ISO 8601 or RFC3339 but without the middle C<T>
576 between the date stamp and time stamp. That is:
577  
578 2012-02-06 12:49
579  
580 =cut
581  
582 sub last_arpnip_stamp { return (shift)->get_column('last_arpnip_stamp') }
583  
584 =head2 since_last_discover
585  
586 Number of seconds which have elapsed since the value of C<last_discover>.
587  
588 =cut
589  
590 sub since_last_discover { return (shift)->get_column('since_last_discover') }
591  
592 =head2 since_last_macsuck
593  
594 Number of seconds which have elapsed since the value of C<last_macsuck>.
595  
596 =cut
597  
598 sub since_last_macsuck { return (shift)->get_column('since_last_macsuck') }
599  
600 =head2 since_last_arpnip
601  
602 Number of seconds which have elapsed since the value of C<last_arpnip>.
603  
604 =cut
605  
606 sub since_last_arpnip { return (shift)->get_column('since_last_arpnip') }
607  
608 1;