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

filter: Statementstmt 0.0% 0 / 105 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 10 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 0.0% 0 / 28 Podpod 0.0% 0 / 22 totaltotal 0.0% 0 / 178 CC 5 SCAR 15.0
CC
5
Cov
0%
remote_inventory
20.0
last_comment
6.0
neighbor
2.0
CRAP 4.5
1 use utf8;
2 package App::Netdisco::DB::Result::DevicePort;
3  
4  
5 use strict;
6 use warnings;
7  
8 use NetAddr::MAC;
9  
10 use MIME::Base64 'encode_base64url';
11  
12 use base 'App::Netdisco::DB::Result';
13 __PACKAGE__->table("device_port");
14 __PACKAGE__->add_columns(
15 "ip",
16 { data_type => "inet", is_nullable => 0 },
17 "port",
18 { data_type => "text", is_nullable => 0 },
19 "creation",
20 {
21 data_type => "timestamp",
22 default_value => \"LOCALTIMESTAMP",
23 is_nullable => 1,
24 original => { default_value => \"LOCALTIMESTAMP" },
25 },
26 "first_seen",
27 {
28 data_type => "timestamp",
29 default_value => \"LOCALTIMESTAMP",
30 is_nullable => 1,
31 original => { default_value => \"LOCALTIMESTAMP" },
32 },
33 "descr",
34 { data_type => "text", is_nullable => 1 },
35 "up",
36 { data_type => "text", is_nullable => 1 },
37 "up_admin",
38 { data_type => "text", is_nullable => 1 },
39 "type",
40 { data_type => "text", is_nullable => 1 },
41 "duplex",
42 { data_type => "text", is_nullable => 1 },
43 "duplex_admin",
44 { data_type => "text", is_nullable => 1 },
45 "speed",
46 { data_type => "text", is_nullable => 1 },
47 "speed_admin",
48 { data_type => "text", is_nullable => 1 },
49 "name",
50 { data_type => "text", is_nullable => 1 },
51 "mac",
52 { data_type => "macaddr", is_nullable => 1 },
53 "mtu",
54 { data_type => "integer", is_nullable => 1 },
55 "stp",
56 { data_type => "text", is_nullable => 1 },
57 "remote_ip",
58 { data_type => "inet", is_nullable => 1 },
59 "remote_port",
60 { data_type => "text", is_nullable => 1 },
61 "remote_type",
62 { data_type => "text", is_nullable => 1 },
63 "remote_id",
64 { data_type => "text", is_nullable => 1 },
65 "has_subinterfaces",
66 { data_type => "boolean", is_nullable => 0, default_value => \"false" },
67 "is_master",
68 { data_type => "boolean", is_nullable => 0, default_value => \"false" },
69 "slave_of",
70 { data_type => "text", is_nullable => 1 },
71 "manual_topo",
72 { data_type => "boolean", is_nullable => 0, default_value => \"false" },
73 "is_uplink",
74 { data_type => "boolean", is_nullable => 1 },
75 "vlan",
76 { data_type => "text", is_nullable => 1 },
77 "pvid",
78 { data_type => "integer", is_nullable => 1 },
79 "lastchange",
80 { data_type => "bigint", is_nullable => 1 },
81 "custom_fields",
82 { data_type => "jsonb", is_nullable => 0, default_value => \"{}" },
83 "tags",
84 { data_type => "text[]", is_nullable => 0, default_value => \"'{}'::text[]" },
85 );
86 __PACKAGE__->set_primary_key("port", "ip");
87  
88  
89  
90 =head1 RELATIONSHIPS
91  
92 =head2 device
93  
94 Returns the Device table entry to which the given Port is related.
95  
96 =cut
97  
98 __PACKAGE__->belongs_to( device => 'App::Netdisco::DB::Result::Device', 'ip' );
99  
100 =head2 port_vlans
101  
102 Returns the set of C<device_port_vlan> entries associated with this Port.
103 These will be both tagged and untagged. Use this relation in search conditions.
104  
105 =cut
106  
107 __PACKAGE__->has_many( port_vlans => 'App::Netdisco::DB::Result::DevicePortVlan',
108 { 'foreign.ip' => 'self.ip', 'foreign.port' => 'self.port' } );
109  
110 =head2 nodes / active_nodes / nodes_with_age / active_nodes_with_age
111  
112 Returns the set of Nodes whose MAC addresses are associated with this Device
113 Port.
114  
115 The C<active> variants return only the subset of nodes currently in the switch
116 MAC address table, that is the active ones.
117  
118 The C<with_age> variants add an additional column C<time_last_age>, a
119 preformatted value for the Node's C<time_last> field, which reads as "X
120 days/weeks/months/years".
121  
122 =cut
123  
124 __PACKAGE__->has_many( nodes => 'App::Netdisco::DB::Result::Node',
125 {
126 'foreign.switch' => 'self.ip',
127 'foreign.port' => 'self.port',
128 },
129 { join_type => 'LEFT' },
130 );
131  
132 __PACKAGE__->has_many( nodes_with_age => 'App::Netdisco::DB::Result::Virtual::NodeWithAge',
133 {
134 'foreign.switch' => 'self.ip',
135 'foreign.port' => 'self.port',
136 },
137 { join_type => 'LEFT',
138 cascade_copy => 0, cascade_update => 0, cascade_delete => 0 },
139 );
140  
141 __PACKAGE__->has_many( active_nodes => 'App::Netdisco::DB::Result::Virtual::ActiveNode',
142 {
143 'foreign.switch' => 'self.ip',
144 'foreign.port' => 'self.port',
145 },
146 { join_type => 'LEFT',
147 cascade_copy => 0, cascade_update => 0, cascade_delete => 0 },
148 );
149  
150 __PACKAGE__->has_many( active_nodes_with_age => 'App::Netdisco::DB::Result::Virtual::ActiveNodeWithAge',
151 {
152 'foreign.switch' => 'self.ip',
153 'foreign.port' => 'self.port',
154 },
155 { join_type => 'LEFT',
156 cascade_copy => 0, cascade_update => 0, cascade_delete => 0 },
157 );
158  
159 =head2 logs
160  
161 Returns the set of C<device_port_log> entries associated with this Port.
162  
163 =cut
164  
165 __PACKAGE__->has_many( logs => 'App::Netdisco::DB::Result::DevicePortLog',
166 { 'foreign.ip' => 'self.ip', 'foreign.port' => 'self.port' },
167 );
168  
169 =head2 power
170  
171 Returns a row from the C<device_port_power> table if one refers to this
172 device port.
173  
174 =cut
175  
176 __PACKAGE__->might_have( power => 'App::Netdisco::DB::Result::DevicePortPower', {
177 'foreign.ip' => 'self.ip', 'foreign.port' => 'self.port',
178 });
179  
180 =head2 properties
181  
182 Returns a row from the C<device_port_properties> table if one refers to this
183 device port.
184  
185 =cut
186  
187 __PACKAGE__->might_have( properties => 'App::Netdisco::DB::Result::DevicePortProperties', {
188 'foreign.ip' => 'self.ip', 'foreign.port' => 'self.port',
189 });
190  
191 =head2 ssid
192  
193 Returns every C<device_port_ssid> row for this port.
194  
195 =cut
196  
197 __PACKAGE__->has_many(
198 ssid => 'App::Netdisco::DB::Result::DevicePortSsid',
199 { 'foreign.ip' => 'self.ip',
200 'foreign.port' => 'self.port',
201 }
202 );
203  
204 =head2 wireless
205  
206 Returns a row from the C<device_port_wireless> table if one refers to this
207 device port.
208  
209 =cut
210  
211 __PACKAGE__->might_have(
212 wireless => 'App::Netdisco::DB::Result::DevicePortWireless',
213 { 'foreign.ip' => 'self.ip',
214 'foreign.port' => 'self.port',
215 }
216 );
217  
218 =head2 native_vlan
219  
220 Returns the row from the C<device_vlan> table corresponding to the native
221 vlan of this port.
222  
223 =cut
224  
225 __PACKAGE__->belongs_to(
226 native_vlan => 'App::Netdisco::DB::Result::DeviceVlan',
227 sub {
228 my $args = shift;
229 return {
230 "$args->{foreign_alias}.ip" =>
231 { '-ident' => "$args->{self_alias}.ip" },
232 "$args->{self_alias}.vlan" =>
233 { '=' => \"cast($args->{foreign_alias}.vlan as text)" }
234 };
235 }, { join_type => 'LEFT' }
236 );
237  
238 =head2 agg_master
239  
240 Returns another row from the C<device_port> table if this port is slave
241 to another in a link aggregate.
242  
243 =cut
244  
245 __PACKAGE__->belongs_to(
246 agg_master => 'App::Netdisco::DB::Result::DevicePort', {
247 'foreign.ip' => 'self.ip',
248 'foreign.port' => 'self.slave_of',
249 }, {
250 join_type => 'LEFT',
251 }
252 );
253  
254 =head2 neighbor_alias
255  
256 When a device port has an attached neighbor device, this relationship will
257 return the IP address of the neighbor. See the C<neighbor> helper method if
258 what you really want is to retrieve the Device entry for that neighbor.
259  
260 The JOIN is of type "LEFT" in case the neighbor device is known but has not
261 been fully discovered by Netdisco and so does not exist itself in the
262 database.
263  
264 =cut
265  
266 __PACKAGE__->belongs_to( neighbor_alias => 'App::Netdisco::DB::Result::DeviceIp',
267 sub {
268 my $args = shift;
269 my $foreign = $args->{foreign_alias};
270 my $owner = $args->{self_resultsource}->schema->resultset('DeviceIpResolved')
271 ->search({alias => { -ident => "$args->{self_alias}.remote_ip"}},
272 {rows => 1, columns => 'ip', alias => 'devipsub', order_by => 'ip'})->as_query;
273 # Joins the owner's own-address row, in its lowest VRF, so a duplicated
274 # address still gives one deterministic row per port. The owner is
275 # matched on alias, not ip, so the planner probes the alias index.
276 return {
277 "$foreign.alias" => { '=' => $owner },
278 "$foreign.ip" => { -ident => "$foreign.alias" },
279 "$foreign.vrf" => { '=' => \"(SELECT min(devipvrf.vrf) FROM device_ip devipvrf WHERE devipvrf.ip = $foreign.ip AND devipvrf.alias = $foreign.alias)" },
280 };
281 },
282 { join_type => 'LEFT' },
283 );
284  
285 =head2 last_node
286  
287 This relationship will return the last node that was seen on the port.
288  
289 The JOIN is of type "LEFT" in case there isn't any such node.
290  
291 =cut
292  
293 __PACKAGE__->belongs_to(
294 last_node => 'App::Netdisco::DB::Result::Virtual::LastNode', {
295 'foreign.switch' => 'self.ip',
296 'foreign.port' => 'self.port',
297 }, {
298 join_type => 'LEFT',
299 }
300 );
301  
302 =head2 vlans
303  
304 As compared to C<port_vlans>, this relationship returns a set of Device VLAN
305 row objects for the VLANs on the given port, which might be more useful if you
306 want to find out details such as the VLAN name.
307  
308 See also C<vlan_count>.
309  
310 =cut
311  
312 __PACKAGE__->many_to_many( vlans => 'port_vlans', 'vlan_entry' );
313  
314  
315 =head2 oui
316  
317 DEPRECATED: USE MANUFACTURER INSTEAD
318  
319 Returns the C<oui> table entry matching this Port. You can then join on this
320 relation and retrieve the Company name from the related table.
321  
322 The JOIN is of type LEFT, in case the OUI table has not been populated.
323  
324 =cut
325  
326 __PACKAGE__->belongs_to( oui => 'App::Netdisco::DB::Result::Oui',
327 sub {
328 my $args = shift;
329 return {
330 "$args->{foreign_alias}.oui" =>
331 { '=' => \"substring(cast($args->{self_alias}.mac as varchar) for 8)" }
332 };
333 },
334 { join_type => 'LEFT' }
335 );
336  
337 =head2 manufacturer
338  
339 Returns the C<manufacturer> table entry matching this Port. You can then join on this
340 relation and retrieve the Company name from the related table.
341  
342 The JOIN is of type LEFT, in case the Manufacturer table has not been populated.
343  
344 =cut
345  
346 __PACKAGE__->belongs_to( manufacturer => 'App::Netdisco::DB::Result::Manufacturer',
347 sub {
348 my $args = shift;
349 return {
350 "$args->{foreign_alias}.range" => { '@>' =>
351 \qq{('x' || lpad( translate( $args->{self_alias}.mac ::text, ':', ''), 16, '0')) ::bit(64) ::bigint} },
352 };
353 },
354 { join_type => 'LEFT' }
355 );
356  
357 =head1 ADDITIONAL METHODS
358  
359 =head2 neighbor
360  
361 Returns the Device entry for the neighbour Device on the given port.
362  
363 Might return an undefined value if there is no neighbor on the port, or if the
364 neighbor has not been fully discovered by Netdisco and so does not exist in
365 the database.
366  
367 =cut
368  
369 sub neighbor {
370 my $row = shift;
371 return eval { $row->neighbor_alias->device || undef };
372 }
373  
374 =head1 ADDITIONAL COLUMNS
375  
376 =head2 native
377  
378 An alias for the C<vlan> column, which stores the PVID (that is, the VLAN
379 ID assigned to untagged frames received on the port).
380  
381 =cut
382  
383 sub native { return (shift)->vlan }
384  
385 =head2 error_disable_cause
386  
387 Returns the textual reason given by the device if the port is in an error
388 state, or else `undef` if the port is not in an error state.
389  
390 =cut
391  
392 sub error_disable_cause { return (shift)->get_column('error_disable_cause') }
393  
394 =head2 remote_is_discoverable
395  
396 Returns true if Netdisco is permitted to discover the remote device with
397 the configuration of the local poller doing the local device discovery.
398  
399 =cut
400  
401 sub remote_is_discoverable { return (shift)->get_column('remote_is_discoverable') }
402  
403 =head2 remote_is_wap
404  
405 Returns true if the remote LLDP neighbor has reported Wireless Access Point
406 capability.
407  
408 =cut
409  
410 sub remote_is_wap { return (shift)->get_column('remote_is_wap') }
411  
412 =head2 remote_is_phone
413  
414 Returns true if the remote LLDP neighbor has reported Telephone capability.
415  
416 =cut
417  
418 sub remote_is_phone { return (shift)->get_column('remote_is_phone') }
419  
420 =head2 ifindex
421  
422 Returns the interface index (C<ifIndex>) of the port.
423  
424 =cut
425  
426 sub ifindex { return (shift)->get_column('ifindex') }
427  
428 =head2 pae_authsess_user
429  
430 Returns the pae_authsess_user of the port.
431  
432 =cut
433  
434 sub pae_authsess_user { return (shift)->get_column('pae_authsess_user') }
435  
436 =head2 pae_authsess_user_net_mac
437  
438 Returns the pae_authsess_user of the port as a NetAddr::MAC instance.
439  
440 =cut
441  
442 sub pae_authsess_user_net_mac { return NetAddr::MAC->new(mac => ((shift)->pae_authsess_user || '')) }
443  
444 =head2 pae_authconfig_port_control
445  
446 Returns the pae_authconfig_port_control of the port.
447  
448 =cut
449  
450 sub pae_authconfig_port_control { return (shift)->get_column('pae_authconfig_port_control') }
451  
452 =head2 pae_authconfig_state
453  
454 Returns the pae_authconfig_state of the port.
455  
456 =cut
457  
458 sub pae_authconfig_state { return (shift)->get_column('pae_authconfig_state') }
459  
460 =head2 pae_authconfig_port_status
461  
462 Returns the pae_authconfig_port_status of the port.
463  
464 =cut
465  
466 sub pae_authconfig_port_status { return (shift)->get_column('pae_authconfig_port_status') }
467  
468 =head2 pae_authsess_mab
469  
470 Returns the pae_authsess_mab of the port.
471  
472 =cut
473  
474 sub pae_authsess_mab { return (shift)->get_column('pae_authsess_mab') }
475  
476 =head2 pae_last_eapol_frame_source
477  
478 Returns the pae_last_eapol_frame_source of the port.
479  
480 =cut
481  
482 sub pae_last_eapol_frame_source { return (shift)->get_column('pae_last_eapol_frame_source') }
483  
484 =head2 remote_dns
485  
486 Returns a hostname resolved from C<remote_ip> column.
487  
488 =cut
489  
490 sub remote_dns { return (shift)->get_column('remote_dns') }
491  
492 =head2 remote_inventory
493  
494 Returns a synthesized description of the remote LLDP device if inventory
495 information was given, including vendor, model, OS version, and serial number.
496  
497 =cut
498  
499 sub remote_inventory {
500 my $port = shift;
501 my $os_ver = ($port->get_column('remote_os_ver')
502 ? ('running '. $port->get_column('remote_os_ver')) : '');
503 my $serial = ($port->get_column('remote_serial')
504 ? ('('. $port->get_column('remote_serial') .')') : '');
505  
506 my $retval = join ' ', ($port->get_column('remote_vendor') || ''),
507 ($port->get_column('remote_model') || ''), $serial, $os_ver;
508  
509 return (($retval =~ m/[[:alnum:]]/) ? $retval : '');
510 }
511  
512 =head2 vlan_count
513  
514 Returns the number of VLANs active on this device port. Enable this column by
515 applying the C<with_vlan_count()> modifier to C<search()>.
516  
517 =cut
518  
519 sub vlan_count { return (shift)->get_column('vlan_count') }
520  
521 =head2 lastchange_stamp
522  
523 Formatted version of the C<lastchange> field, accurate to the minute. Enable
524 this column by applying the C<with_times()> modifier to C<search()>.
525  
526 The format is somewhat like ISO 8601 or RFC3339 but without the middle C<T>
527 between the date stamp and time stamp. That is:
528  
529 2012-02-06 12:49
530  
531 =cut
532  
533 sub lastchange_stamp { return (shift)->get_column('lastchange_stamp') }
534  
535 =head2 is_free
536  
537 This method can be used to evaluate whether a device port could be considered
538 unused, based on the last time it changed from the "up" state to a "down"
539 state.
540  
541 See the C<with_is_free> and C<only_free_ports> modifiers to C<search()>.
542  
543 =cut
544  
545 sub is_free { return (shift)->get_column('is_free') }
546  
547 =head2 base64url_port
548  
549 Returns a Base64 encoded version of the C<port> column value suitable for use
550 in a URL.
551  
552 =cut
553  
554 sub base64url_port { return encode_base64url((shift)->port) }
555  
556 =head2 net_mac
557  
558 Returns the C<mac> column instantiated into a L<NetAddr::MAC> object.
559  
560 =cut
561  
562 sub net_mac { return NetAddr::MAC->new(mac => ((shift)->mac || '')) }
563  
564 =head2 last_comment
565  
566 Returns the most recent comment from the logs for this device port.
567  
568 =cut
569  
570 sub last_comment {
571 my $row = (shift)->logs->search(undef,
572 { order_by => { -desc => 'creation' }, rows => 1 })->first;
573 return ($row ? $row->log : '');
574 }
575  
576 1;