blib/lib/App/Netdisco/Worker/Plugin/Arpnip/Subnets.pm untested

filter: Statementstmt 0.0% 0 / 145 Branchbran 0.0% 0 / 40 Conditioncond 0.0% 0 / 53 MC/DCmcdc 0.0% 0 / 32 Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 290 CC 15 SCAR 35.1
CC
15
Cov
0%
vrf_subnet_cidr
90.0
store_cli_vrf_subnets
30.0
store_vrf_subnets
6.0
CRAP 33.6
1 package App::Netdisco::Worker::Plugin::Arpnip::Subnets;
2  
3 use Dancer ':syntax';
4 use App::Netdisco::Worker::Plugin;
5 use aliased 'App::Netdisco::Worker::Status';
6  
7 use App::Netdisco::Transport::SNMP ();
8 use App::Netdisco::Transport::SSH ();
9 use App::Netdisco::Util::Permission 'acl_matches';
10 use App::Netdisco::Util::VRF qw/
11 vrf_collection_enabled vrf_skip_test netdisco_vrf vrf_instances_for each_vrf_instance/;
12 use Dancer::Plugin::DBIC 'schema';
13 use NetAddr::IP::Lite ':lower';
14 use Time::HiRes 'gettimeofday';
15 use List::Util 'sum0';
16 use Try::Tiny;
17  
18 register_worker({ phase => 'early', title => 'prepare subnets' }, sub {
19 # netdisco-do runs every job in one process, so no job sees another's subnets
20 delete vars->{'subnets'};
21 });
22  
23 register_worker({ phase => 'main', driver => 'snmp' }, sub {
24 my ($job, $workerconf) = @_;
25 my $device = $job->device;
26  
27 my $snmp = App::Netdisco::Transport::SNMP->reader_for($device)
28 or return Status->defer("arpnip (snmp) failed: could not SNMP connect to $device");
29  
30 my $instances = vrf_instances_for($device, $snmp);
31 return store_vrf_subnets($device, $snmp, $instances) if $instances;
32  
33 # TODO: IPv6 subnets
34 my @subnets = ();
35  
36 my $ip_netmask = $snmp->ip_netmask;
37 foreach my $entry (keys %$ip_netmask) {
38 my $ip = NetAddr::IP::Lite->new($entry) or next;
39 my $addr = $ip->addr;
40  
41 next if $addr eq '0.0.0.0';
42 next if acl_matches($ip, 'group:__LOOPBACK_ADDRESSES__');
43 next if setting('ignore_private_nets') and $ip->is_rfc1918;
44  
45 my $netmask = $ip_netmask->{$addr} || $ip->bits();
46 next if $netmask eq '255.255.255.255' or $netmask eq '0.0.0.0';
47  
48 my $cidr = NetAddr::IP::Lite->new($addr, $netmask)->network->cidr;
49 push @subnets, $cidr;
50 }
51  
52 debug sprintf ' [%s] arpnip (snmp) - found subnet %s', $device->ip, $_ for @subnets;
53 store_subnet($_) for @subnets;
54  
55 return Status->info(sprintf ' [%s] arpnip (snmp) - processed %s Subnet entries',
56 $device->ip, scalar @subnets);
57 });
58  
59 # store_vrfs path: each VRF's subnets are read in its own context; a VRF whose
60 # read came back short stores none, so its stored rows are left alone
61 sub store_vrf_subnets {
62 my ($device, $snmp, $instances) = @_;
63  
64 my $reads = each_vrf_instance($device, $snmp, $instances,
65 sub { return $snmp->vrf_subnets($_[0]) }, log_as => 'arpnip');
66  
67 my $count = 0;
68 foreach my $vrf (sort keys %$reads) {
69 next unless $reads->{$vrf}->{complete};
70 my $found = $reads->{$vrf}->{data} || {};
71 foreach my $key (sort keys %$found) {
72 my $cidr = vrf_subnet_cidr($found->{$key}) or next;
73 debug sprintf ' [%s] arpnip (snmp) - found subnet %s in vrf "%s"', $device->ip, $cidr, $vrf;
74 store_subnet($cidr, $vrf);
75 ++$count;
76 }
77 }
78  
79 return Status->info(sprintf ' [%s] arpnip (snmp) - processed %s Subnet entries',
80 $device->ip, $count);
81 }
82  
83 # the network of a vrf_subnets entry in CIDR form, or undef for one that is
84 # not stored: unspecified, host, loopback, link-local and (when configured) private
85 sub vrf_subnet_cidr {
86 my ($entry) = @_;
87 my ($prefix, $len) = @$entry{qw/prefix len/};
88 return undef unless defined $prefix and defined $len;
89  
90 my $ip = NetAddr::IP::Lite->new($prefix) or return undef;
91 my $is_v6 = $ip->version == 6;
92 return undef if $len == 0 or $len == ($is_v6 ? 128 : 32);
93 return undef if $ip->addr eq '0.0.0.0' or acl_matches($ip, 'group:__LOOPBACK_ADDRESSES__');
94  
95 if ($is_v6) {
96 return undef if NetAddr::IP::Lite->new('fe80::/10')->contains($ip);
97 return undef if setting('ignore_private_nets') and NetAddr::IP::Lite->new('fc00::/7')->contains($ip);
98 }
99 else {
100 return undef if setting('ignore_private_nets') and $ip->is_rfc1918;
101 }
102  
103 my $network = NetAddr::IP::Lite->new($ip->addr, $len) or return undef;
104 return $network->network->cidr;
105 }
106  
107 register_worker({ phase => 'main', driver => 'cli' }, sub {
108 my ($job, $workerconf) = @_;
109 my $device = $job->device;
110  
111 my $cli = App::Netdisco::Transport::SSH->session_for($device)
112 or return Status->defer("arpnip (cli) failed: could not SSH connect to $device");
113  
114 return store_cli_vrf_subnets($device, $cli)
115 if vrf_collection_enabled($device) and $cli->platform->can('subnets_by_vrf');
116  
117 my @subnets = grep { defined and length } $cli->subnets;
118  
119 debug sprintf ' [%s] arpnip (cli) - found subnet %s', $device->ip, $_ for @subnets;
120 store_subnet($_) for @subnets;
121  
122 return Status->info(sprintf ' [%s] arpnip (cli) - processed %s Subnet entries',
123 $device->ip, scalar @subnets);
124 });
125  
126 # a platform with subnets_by_vrf returns { VRF => [ { prefix, len } ] } with the
127 # device's own VRF names, or undef when it read nothing. Nothing read stores
128 # nothing and is not a defer: subnets never retire, and the ARP worker defers.
129 # A platform subclass that overrides subnets must also override subnets_by_vrf,
130 # or with store_vrfs on the inherited subnets_by_vrf runs instead of its subnets.
131 sub store_cli_vrf_subnets {
132 my ($device, $cli) = @_;
133 my $error = 'returned no subnet data';
134 my $found = try { $cli->subnets_by_vrf } catch { ($error = "failed: $_") =~ s{\s+\z}{}; undef };
135 unless (ref {} eq ref $found) {
136 debug sprintf ' [%s] arpnip (cli) - subnets_by_vrf %s, so no subnets are stored', $device->ip, $error;
137 return Status->info(sprintf ' [%s] arpnip (cli) - processed 0 Subnet entries', $device->ip);
138 }
139  
140 my %seen;
141 my $skipped = vrf_skip_test($device);
142 foreach my $device_vrf (sort keys %$found) {
143 my $vrf = netdisco_vrf($device_vrf);
144 next if $skipped->($vrf);
145 unless (ref [] eq ref $found->{$device_vrf}) {
146 debug sprintf ' [%s] arpnip (cli) - subnets for vrf "%s" are not a list, skipped', $device->ip, $vrf;
147 next;
148 }
149 foreach my $entry (@{ $found->{$device_vrf} }) {
150 unless (ref {} eq ref $entry) {
151 debug sprintf ' [%s] arpnip (cli) - subnet entry in vrf "%s" is not a hash, skipped', $device->ip, $vrf;
152 next;
153 }
154 my $cidr = vrf_subnet_cidr($entry) or next;
155 debug sprintf ' [%s] arpnip (cli) - found subnet %s in vrf "%s"', $device->ip, $cidr, $vrf;
156 store_subnet($cidr, $vrf);
157 $seen{$vrf}{$cidr} = 1;
158 }
159 }
160  
161 my $count = sum0(map { scalar keys %$_ } values %seen);
162 return Status->info(sprintf ' [%s] arpnip (cli) - processed %s Subnet entries',
163 $device->ip, $count);
164 }
165  
166 register_worker({ phase => 'store', title => 'store subnets' }, sub {
167 my ($job, $workerconf) = @_;
168 my $device = $job->device;
169  
170 # my $subs = vars->{'subnets'};
171 # use DDP; p $subs;
172  
173 my $now = vars->{'timestamp'}
174 || (sprintf 'to_timestamp(%d.%06d)::timestamp', gettimeofday);
175  
176 # update subnets with new networks
177 my $stored = 0;
178 foreach my $vrf (sort keys %{ vars->{'subnets'} || {} }) {
179 foreach my $cidr (keys %{ vars->{'subnets'}->{$vrf} }) {
180 schema('netdisco')->resultset('Subnet')->upsert_subnet($cidr, $now, $vrf, $device->ip);
181 ++$stored;
182 }
183 }
184  
185 debug sprintf ' [%s] arpnip - processed %s subnets', $device->ip, $stored;
186  
187 return Status->info("Stored subnets for $device");
188 });
189  
190 sub store_subnet {
191 my ($subnet, $vrf) = @_;
192 return unless $subnet;
193 vars->{'subnets'}->{ $vrf // '' }->{$subnet} = 1;
194 }
195  
196 true;