« blib/lib/App/Netdisco/DB/Result/Virtual/UserRole.pm
↑ Summary
blib/lib/App/Netdisco/DB/Result/Virtual/WalkJobs.pm »
| 1 | package App::Netdisco::DB::Result::Virtual::VrfInconsistency; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use utf8; | ||
| 7 | use base 'DBIx::Class::Core'; | ||
| 8 | |||
| 9 | __PACKAGE__->table_class('DBIx::Class::ResultSource::View'); | ||
| 10 | |||
| 11 | __PACKAGE__->table('vrf_inconsistency'); | ||
| 12 | __PACKAGE__->result_source_instance->is_virtual(1); | ||
| 13 | __PACKAGE__->result_source_instance->view_definition(<<'ENDSQL'); | ||
| 14 | SELECT 'different names' AS finding, | ||
| 15 | d.name AS domain, | ||
| 16 | array_agg(DISTINCT m.vrf ORDER BY m.vrf) AS vrfs, | ||
| 17 | count(DISTINCT m.ip)::integer AS devices, | ||
| 18 | 'domain holds ' || array_to_string(array_agg(DISTINCT m.vrf ORDER BY m.vrf), ', ') AS detail, | ||
| 19 | ARRAY[d.id] AS domain_ids, | ||
| 20 | (SELECT string_agg(x.name || ' (#' || x.id || ')', ', ' ORDER BY x.id) FROM l3_domain x WHERE x.id = ANY(ARRAY[d.id])) AS domains | ||
| 21 | FROM l3_domain_member m | ||
| 22 | JOIN l3_domain d ON d.id = m.domain_id | ||
| 23 | WHERE m.vrf <> '' | ||
| 24 | -- vrf_domains groups different names on purpose | ||
| 25 | AND (d.inferred_name IS NULL OR d.inferred_name NOT LIKE 'manual:%') | ||
| 26 | GROUP BY d.id, d.name | ||
| 27 | HAVING count(DISTINCT m.vrf) > 1 | ||
| 28 | UNION ALL | ||
| 29 | -- same name once case and punctuation are ignored, but kept in separate domains | ||
| 30 | SELECT 'similar names' AS finding, | ||
| 31 | (array_agg(d.name ORDER BY m.vrf, m.domain_id))[1] AS domain, | ||
| 32 | array_agg(DISTINCT m.vrf ORDER BY m.vrf) AS vrfs, | ||
| 33 | count(DISTINCT m.ip)::integer AS devices, | ||
| 34 | 'case or punctuation' AS detail, | ||
| 35 | array_agg(DISTINCT m.domain_id ORDER BY m.domain_id) AS domain_ids, | ||
| 36 | (SELECT string_agg(x.name || ' (#' || x.id || ')', ', ' ORDER BY x.id) FROM l3_domain x WHERE x.id = ANY(array_agg(DISTINCT m.domain_id ORDER BY m.domain_id))) AS domains | ||
| 37 | FROM l3_domain_member m | ||
| 38 | JOIN l3_domain d ON d.id = m.domain_id | ||
| 39 | WHERE m.vrf <> '' | ||
| 40 | AND lower(regexp_replace(m.vrf, '[^A-Za-z0-9]', '', 'g')) <> '' | ||
| 41 | GROUP BY lower(regexp_replace(m.vrf, '[^A-Za-z0-9]', '', 'g')) | ||
| 42 | HAVING count(DISTINCT m.domain_id) > 1 | ||
| 43 | AND count(DISTINCT m.vrf) > 1 | ||
| 44 | UNION ALL | ||
| 45 | -- an RD is a hint only: nothing joins on it | ||
| 46 | SELECT 'shared RD' AS finding, | ||
| 47 | (array_agg(d.name ORDER BY v.vrf, m.domain_id))[1] AS domain, | ||
| 48 | array_agg(DISTINCT v.vrf ORDER BY v.vrf) AS vrfs, | ||
| 49 | count(DISTINCT v.ip)::integer AS devices, | ||
| 50 | 'RD ' || v.rd AS detail, | ||
| 51 | array_agg(DISTINCT m.domain_id ORDER BY m.domain_id) AS domain_ids, | ||
| 52 | (SELECT string_agg(x.name || ' (#' || x.id || ')', ', ' ORDER BY x.id) FROM l3_domain x WHERE x.id = ANY(array_agg(DISTINCT m.domain_id ORDER BY m.domain_id))) AS domains | ||
| 53 | FROM device_vrf v | ||
| 54 | JOIN l3_domain_member m ON m.ip = v.ip AND m.vrf = v.vrf | ||
| 55 | JOIN l3_domain d ON d.id = m.domain_id | ||
| 56 | WHERE v.vrf <> '' AND v.rd IS NOT NULL | ||
| 57 | GROUP BY v.rd | ||
| 58 | HAVING count(DISTINCT m.domain_id) > 1 | ||
| 59 | UNION ALL | ||
| 60 | -- a missing member row on one side only counts as a different domain | ||
| 61 | SELECT 'linked, one subnet' AS finding, | ||
| 62 | COALESCE(d.name, '') AS domain, | ||
| 63 | ARRAY[p.lo_vrf, p.hi_vrf] AS vrfs, | ||
| 64 | 2 AS devices, | ||
| 65 | 'subnet ' || p.subnet || ' between ' || host(p.lo_ip) || ' and ' || host(p.hi_ip) AS detail, | ||
| 66 | NULLIF(array_remove(ARRAY[p.lo_domain, p.hi_domain], NULL), '{}') AS domain_ids, | ||
| 67 | (SELECT string_agg(x.name || ' (#' || x.id || ')', ', ' ORDER BY x.id) FROM l3_domain x WHERE x.id = ANY(NULLIF(array_remove(ARRAY[p.lo_domain, p.hi_domain], NULL), '{}'))) AS domains | ||
| 68 | FROM ( | ||
| 69 | SELECT DISTINCT | ||
| 70 | CASE WHEN l.a_ip < l.b_ip THEN l.a_ip ELSE l.b_ip END AS lo_ip, | ||
| 71 | CASE WHEN l.a_ip < l.b_ip THEN l.a_vrf ELSE l.b_vrf END AS lo_vrf, | ||
| 72 | CASE WHEN l.a_ip < l.b_ip THEN l.a_domain ELSE l.b_domain END AS lo_domain, | ||
| 73 | CASE WHEN l.a_ip < l.b_ip THEN l.b_ip ELSE l.a_ip END AS hi_ip, | ||
| 74 | CASE WHEN l.a_ip < l.b_ip THEN l.b_vrf ELSE l.a_vrf END AS hi_vrf, | ||
| 75 | CASE WHEN l.a_ip < l.b_ip THEN l.b_domain ELSE l.a_domain END AS hi_domain, | ||
| 76 | l.subnet | ||
| 77 | FROM ( | ||
| 78 | SELECT pr.a_ip, ia.vrf AS a_vrf, ma.domain_id AS a_domain, | ||
| 79 | pr.b_ip, ib.vrf AS b_vrf, mb.domain_id AS b_domain, | ||
| 80 | ia.subnet AS subnet | ||
| 81 | FROM ( | ||
| 82 | SELECT DISTINCT dp.ip AS a_ip, dp.port AS a_port, r.ip AS b_ip | ||
| 83 | FROM device_port dp | ||
| 84 | JOIN device_ip_resolved r ON r.alias = dp.remote_ip | ||
| 85 | WHERE dp.ip <> r.ip | ||
| 86 | ) pr | ||
| 87 | -- the subnet must be on the neighbor's port (or a subinterface of | ||
| 88 | -- it): equal private space elsewhere on two neighbors is reuse | ||
| 89 | JOIN device_ip ia ON ia.ip = pr.a_ip AND ia.vrf <> '' AND ia.subnet IS NOT NULL | ||
| 90 | AND (ia.port = pr.a_port OR left(ia.port, length(pr.a_port) + 1) = pr.a_port || '.') | ||
| 91 | JOIN device_ip ib ON ib.ip = pr.b_ip AND ib.vrf <> '' AND ib.subnet = ia.subnet AND ia.alias <> ib.alias | ||
| 92 | LEFT JOIN l3_domain_member ma ON ma.ip = ia.ip AND ma.vrf = ia.vrf | ||
| 93 | LEFT JOIN l3_domain_member mb ON mb.ip = ib.ip AND mb.vrf = ib.vrf | ||
| 94 | WHERE ma.domain_id IS DISTINCT FROM mb.domain_id | ||
| 95 | -- a subnet a device holds in two named VRFs is reused space, so | ||
| 96 | -- which VRF faces the neighbor is unknown: pair only single holders | ||
| 97 | AND NOT EXISTS (SELECT 1 FROM device_ip xa WHERE xa.ip = ia.ip AND xa.subnet = ia.subnet | ||
| 98 | AND xa.vrf <> '' AND xa.vrf <> ia.vrf) | ||
| 99 | AND NOT EXISTS (SELECT 1 FROM device_ip xb WHERE xb.ip = ib.ip AND xb.subnet = ib.subnet | ||
| 100 | AND xb.vrf <> '' AND xb.vrf <> ib.vrf) | ||
| 101 | ) l | ||
| 102 | ) p | ||
| 103 | LEFT JOIN l3_domain d ON d.id = p.lo_domain | ||
| 104 | ENDSQL | ||
| 105 | |||
| 106 | __PACKAGE__->add_columns( | ||
| 107 | "finding", | ||
| 108 | { data_type => "text", is_nullable => 0 }, | ||
| 109 | "domain", | ||
| 110 | { data_type => "text", is_nullable => 1 }, | ||
| 111 | "vrfs", | ||
| 112 | { data_type => "text[]", is_nullable => 0 }, | ||
| 113 | "devices", | ||
| 114 | { data_type => "integer", is_nullable => 0 }, | ||
| 115 | "detail", | ||
| 116 | { data_type => "text", is_nullable => 0 }, | ||
| 117 | "domain_ids", | ||
| 118 | { data_type => "integer[]", is_nullable => 1 }, | ||
| 119 | "domains", | ||
| 120 | { data_type => "text", is_nullable => 1 }, | ||
| 121 | ); | ||
| 122 | |||
| 123 | 1; |