@takodotid/circuit 0.34.3 → 0.35.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (240) hide show
  1. package/README.md +13 -14
  2. package/dist/chunks/index-20kcjxjg.js +174 -0
  3. package/dist/chunks/index-2r4bbhac.js +977 -0
  4. package/dist/chunks/index-n7w3tg5y.js +149 -0
  5. package/dist/chunks/new-w3zys5y4.js +172 -0
  6. package/dist/chunks/server-gzsf5me5.js +259 -0
  7. package/dist/cli/index.js +11345 -9024
  8. package/dist/helpers/index.js +594 -0
  9. package/dist/index.js +9 -28
  10. package/dist/types/core/{addresses.d.ts → address-space.d.ts} +23 -7
  11. package/dist/types/core/address.d.ts +44 -0
  12. package/dist/types/core/cabling.d.ts +41 -0
  13. package/dist/types/core/cross-checks.d.ts +28 -0
  14. package/dist/types/core/define.d.ts +25 -3
  15. package/dist/types/core/project-files.d.ts +12 -0
  16. package/dist/types/core/schema-docs.d.ts +13 -4
  17. package/dist/types/core/secrets.d.ts +62 -21
  18. package/dist/types/core/{graph.d.ts → topology.d.ts} +32 -12
  19. package/dist/types/core/utils.d.ts +8 -0
  20. package/dist/types/core/validator.d.ts +19 -0
  21. package/dist/types/helpers/bgp.d.ts +97 -0
  22. package/dist/types/helpers/checks.d.ts +32 -0
  23. package/dist/types/helpers/exchange.d.ts +49 -0
  24. package/dist/types/helpers/filters.d.ts +51 -0
  25. package/dist/types/helpers/index.d.ts +10 -0
  26. package/dist/types/helpers/route-server.d.ts +52 -0
  27. package/dist/types/index.d.ts +5 -3
  28. package/dist/types/integrations/librenms.d.ts +60 -0
  29. package/dist/types/integrations/netbox.d.ts +89 -0
  30. package/dist/types/integrations/peeringdb.d.ts +24 -0
  31. package/dist/types/integrations/prometheus.d.ts +24 -0
  32. package/dist/types/integrations/registries.d.ts +8 -0
  33. package/dist/types/platforms/bgp-wait.d.ts +18 -0
  34. package/dist/types/platforms/catalog.d.ts +109 -0
  35. package/dist/types/platforms/derive.d.ts +17 -0
  36. package/dist/types/platforms/fortios/firewall.d.ts +45 -0
  37. package/dist/types/platforms/fortios/index.d.ts +134 -0
  38. package/dist/types/platforms/fortios/names.d.ts +52 -0
  39. package/dist/types/platforms/fortios/plan.d.ts +104 -0
  40. package/dist/types/platforms/fortios/render.d.ts +75 -0
  41. package/dist/types/platforms/fortios/routing.d.ts +56 -0
  42. package/dist/types/platforms/fortios/tree.d.ts +37 -0
  43. package/dist/types/platforms/index.d.ts +13 -0
  44. package/dist/types/platforms/junos/bgp.d.ts +44 -0
  45. package/dist/types/platforms/junos/index.d.ts +114 -0
  46. package/dist/types/platforms/junos/render.d.ts +154 -0
  47. package/dist/types/platforms/line-diff.d.ts +67 -0
  48. package/dist/types/platforms/platform.d.ts +120 -0
  49. package/dist/types/platforms/raisecom-ros/index.d.ts +71 -0
  50. package/dist/types/platforms/raisecom-ros/render.d.ts +41 -0
  51. package/dist/types/platforms/routeros/export.d.ts +59 -0
  52. package/dist/types/platforms/routeros/index.d.ts +96 -0
  53. package/dist/types/{adapters/devices → platforms}/routeros/models.d.ts +4 -0
  54. package/dist/types/platforms/routeros/plan.d.ts +54 -0
  55. package/dist/types/platforms/routeros/render.d.ts +240 -0
  56. package/dist/types/platforms/vrp/index.d.ts +83 -0
  57. package/dist/types/platforms/vrp/render.d.ts +96 -0
  58. package/dist/types/schema/common.d.ts +12 -6
  59. package/dist/types/schema/index.d.ts +1 -0
  60. package/dist/types/schema/interfaces.d.ts +14 -6
  61. package/dist/types/schema/policy.d.ts +18 -10
  62. package/dist/types/schema/routing.d.ts +14 -1
  63. package/dist/types/schema/system.d.ts +16 -6
  64. package/dist/types/schema/time-zones.d.ts +2 -0
  65. package/dist/types/transport/console.d.ts +79 -23
  66. package/dist/types/transport/path-check.d.ts +32 -0
  67. package/dist/types/transport/ssh.d.ts +17 -15
  68. package/package.json +17 -11
  69. package/src/cli/graph-format.ts +91 -0
  70. package/src/cli/index.ts +1074 -484
  71. package/src/cli/new.ts +208 -175
  72. package/src/cli/rename.ts +97 -91
  73. package/src/core/address-space.ts +320 -0
  74. package/src/core/address.ts +202 -0
  75. package/src/core/cabling.ts +154 -0
  76. package/src/core/cross-checks.ts +364 -0
  77. package/src/core/define.ts +26 -3
  78. package/src/core/project-files.ts +35 -0
  79. package/src/core/schema-docs.ts +138 -130
  80. package/src/core/secrets.ts +170 -76
  81. package/src/core/topology.ts +656 -0
  82. package/src/core/utils.ts +37 -0
  83. package/src/core/validator.ts +407 -0
  84. package/src/helpers/bgp.ts +301 -0
  85. package/src/helpers/checks.ts +178 -0
  86. package/src/helpers/exchange.ts +117 -0
  87. package/src/helpers/filters.ts +107 -0
  88. package/src/helpers/index.ts +12 -0
  89. package/src/helpers/route-server.ts +212 -0
  90. package/src/index.ts +6 -3
  91. package/src/integrations/librenms.ts +291 -0
  92. package/src/integrations/netbox.ts +620 -0
  93. package/src/integrations/peeringdb.ts +173 -0
  94. package/src/integrations/prometheus.ts +90 -0
  95. package/src/integrations/registries.ts +25 -0
  96. package/src/platforms/bgp-wait.ts +41 -0
  97. package/src/platforms/catalog.ts +69 -0
  98. package/src/platforms/derive.ts +117 -0
  99. package/src/{adapters/devices → platforms}/fortios/TRAPS.md +6 -0
  100. package/src/platforms/fortios/firewall.ts +377 -0
  101. package/src/platforms/fortios/index.ts +1060 -0
  102. package/src/platforms/fortios/names.ts +130 -0
  103. package/src/platforms/fortios/plan.ts +929 -0
  104. package/src/platforms/fortios/render.ts +779 -0
  105. package/src/platforms/fortios/routing.ts +476 -0
  106. package/src/platforms/fortios/tree.ts +129 -0
  107. package/src/platforms/index.ts +36 -0
  108. package/src/{adapters/devices → platforms}/junos/TRAPS.md +8 -0
  109. package/src/platforms/junos/bgp.ts +293 -0
  110. package/src/platforms/junos/index.ts +1006 -0
  111. package/src/platforms/junos/render.ts +967 -0
  112. package/src/platforms/line-diff.ts +244 -0
  113. package/src/platforms/platform.ts +122 -0
  114. package/src/platforms/raisecom-ros/TRAPS.md +40 -0
  115. package/src/platforms/raisecom-ros/index.ts +731 -0
  116. package/src/platforms/raisecom-ros/render.ts +328 -0
  117. package/src/{adapters/devices → platforms}/routeros/TRAPS.md +16 -2
  118. package/src/platforms/routeros/export.ts +342 -0
  119. package/src/platforms/routeros/index.ts +636 -0
  120. package/src/{adapters/devices → platforms}/routeros/models.ts +2 -0
  121. package/src/platforms/routeros/plan.ts +444 -0
  122. package/src/platforms/routeros/render.ts +1483 -0
  123. package/src/platforms/vrp/TRAPS.md +45 -0
  124. package/src/platforms/vrp/index.ts +652 -0
  125. package/src/platforms/vrp/render.ts +615 -0
  126. package/src/schema/common.ts +15 -6
  127. package/src/schema/index.ts +1 -0
  128. package/src/schema/interfaces.ts +15 -5
  129. package/src/schema/policy.ts +22 -10
  130. package/src/schema/routing.ts +16 -1
  131. package/src/schema/system.ts +16 -6
  132. package/src/schema/time-zones.ts +482 -0
  133. package/src/studio/data.ts +172 -168
  134. package/src/studio/git.ts +95 -90
  135. package/src/studio/public/app.js +161 -150
  136. package/src/studio/public/compare.js +219 -212
  137. package/src/studio/public/diff.js +77 -75
  138. package/src/studio/public/dom.js +118 -108
  139. package/src/studio/public/evaluate.js +584 -545
  140. package/src/studio/public/export.js +120 -118
  141. package/src/studio/public/graph.js +1343 -1266
  142. package/src/studio/public/net.js +115 -109
  143. package/src/studio/public/tables.js +1135 -1083
  144. package/src/studio/public/tools.js +561 -529
  145. package/src/studio/server.ts +202 -196
  146. package/src/transport/console.ts +363 -271
  147. package/src/transport/path-check.ts +91 -0
  148. package/src/transport/ssh.ts +98 -94
  149. package/templates/colocation/checks.ts +11 -6
  150. package/templates/colocation/edge-01.ts +9 -9
  151. package/templates/colocation/routing.ts +2 -2
  152. package/templates/colocation/tenancy.ts +3 -3
  153. package/templates/colocation/tor-01.ts +3 -3
  154. package/templates/edge-router/checks.ts +4 -4
  155. package/templates/edge-router/edge-01.ts +5 -5
  156. package/templates/edge-router/routing.ts +2 -2
  157. package/templates/project/AGENTS.md +5 -1
  158. package/templates/single-site/checks.ts +2 -2
  159. package/dist/chunks/index-47debt16.js +0 -956
  160. package/dist/chunks/index-v8zgr7wm.js +0 -128
  161. package/dist/chunks/new-ny89s7y7.js +0 -166
  162. package/dist/chunks/server-tjbb3rqc.js +0 -251
  163. package/dist/presets/index.js +0 -394
  164. package/dist/types/adapters/devices/catalog.d.ts +0 -86
  165. package/dist/types/adapters/devices/derive.d.ts +0 -7
  166. package/dist/types/adapters/devices/fortios/firewall.d.ts +0 -23
  167. package/dist/types/adapters/devices/fortios/index.d.ts +0 -58
  168. package/dist/types/adapters/devices/fortios/names.d.ts +0 -48
  169. package/dist/types/adapters/devices/fortios/plan.d.ts +0 -28
  170. package/dist/types/adapters/devices/fortios/render.d.ts +0 -38
  171. package/dist/types/adapters/devices/fortios/routing.d.ts +0 -32
  172. package/dist/types/adapters/devices/fortios/tree.d.ts +0 -33
  173. package/dist/types/adapters/devices/index.d.ts +0 -3
  174. package/dist/types/adapters/devices/junos/bgp.d.ts +0 -11
  175. package/dist/types/adapters/devices/junos/index.d.ts +0 -42
  176. package/dist/types/adapters/devices/junos/render.d.ts +0 -52
  177. package/dist/types/adapters/devices/lines.d.ts +0 -56
  178. package/dist/types/adapters/devices/raisecom-ros/index.d.ts +0 -28
  179. package/dist/types/adapters/devices/routeros/export.d.ts +0 -39
  180. package/dist/types/adapters/devices/routeros/index.d.ts +0 -30
  181. package/dist/types/adapters/devices/routeros/plan.d.ts +0 -4
  182. package/dist/types/adapters/devices/routeros/render.d.ts +0 -100
  183. package/dist/types/adapters/devices/types.d.ts +0 -40
  184. package/dist/types/adapters/devices/vrp/index.d.ts +0 -27
  185. package/dist/types/adapters/registries/index.d.ts +0 -3
  186. package/dist/types/adapters/registries/librenms.d.ts +0 -15
  187. package/dist/types/adapters/registries/peeringdb.d.ts +0 -16
  188. package/dist/types/adapters/registries/prometheus.d.ts +0 -16
  189. package/dist/types/core/across.d.ts +0 -4
  190. package/dist/types/core/addr.d.ts +0 -34
  191. package/dist/types/core/files.d.ts +0 -6
  192. package/dist/types/core/merge.d.ts +0 -4
  193. package/dist/types/core/time.d.ts +0 -2
  194. package/dist/types/core/validate.d.ts +0 -2
  195. package/dist/types/presets/index.d.ts +0 -183
  196. package/src/adapters/devices/catalog.ts +0 -41
  197. package/src/adapters/devices/derive.ts +0 -59
  198. package/src/adapters/devices/fortios/firewall.ts +0 -362
  199. package/src/adapters/devices/fortios/index.ts +0 -923
  200. package/src/adapters/devices/fortios/names.ts +0 -115
  201. package/src/adapters/devices/fortios/plan.ts +0 -888
  202. package/src/adapters/devices/fortios/render.ts +0 -768
  203. package/src/adapters/devices/fortios/routing.ts +0 -444
  204. package/src/adapters/devices/fortios/tree.ts +0 -121
  205. package/src/adapters/devices/index.ts +0 -9
  206. package/src/adapters/devices/junos/bgp.ts +0 -269
  207. package/src/adapters/devices/junos/index.ts +0 -862
  208. package/src/adapters/devices/junos/render.ts +0 -872
  209. package/src/adapters/devices/lines.ts +0 -212
  210. package/src/adapters/devices/raisecom-ros/TRAPS.md +0 -26
  211. package/src/adapters/devices/raisecom-ros/index.ts +0 -700
  212. package/src/adapters/devices/routeros/export.ts +0 -321
  213. package/src/adapters/devices/routeros/index.ts +0 -346
  214. package/src/adapters/devices/routeros/plan.ts +0 -398
  215. package/src/adapters/devices/routeros/render.ts +0 -1490
  216. package/src/adapters/devices/types.ts +0 -38
  217. package/src/adapters/devices/vrp/TRAPS.md +0 -26
  218. package/src/adapters/devices/vrp/index.ts +0 -865
  219. package/src/adapters/registries/index.ts +0 -19
  220. package/src/adapters/registries/librenms.ts +0 -92
  221. package/src/adapters/registries/peeringdb.ts +0 -169
  222. package/src/adapters/registries/prometheus.ts +0 -71
  223. package/src/cli/graph.ts +0 -89
  224. package/src/core/across.ts +0 -350
  225. package/src/core/addr.ts +0 -174
  226. package/src/core/addresses.ts +0 -316
  227. package/src/core/files.ts +0 -32
  228. package/src/core/graph.ts +0 -640
  229. package/src/core/merge.ts +0 -19
  230. package/src/core/time.ts +0 -15
  231. package/src/core/validate.ts +0 -348
  232. package/src/presets/index.ts +0 -579
  233. /package/dist/types/{adapters/devices → platforms}/fortios/models.d.ts +0 -0
  234. /package/dist/types/{adapters/devices → platforms}/junos/models.d.ts +0 -0
  235. /package/dist/types/{adapters/devices → platforms}/raisecom-ros/models.d.ts +0 -0
  236. /package/dist/types/{adapters/devices → platforms}/vrp/models.d.ts +0 -0
  237. /package/src/{adapters/devices → platforms}/fortios/models.ts +0 -0
  238. /package/src/{adapters/devices → platforms}/junos/models.ts +0 -0
  239. /package/src/{adapters/devices → platforms}/raisecom-ros/models.ts +0 -0
  240. /package/src/{adapters/devices → platforms}/vrp/models.ts +0 -0
@@ -0,0 +1,364 @@
1
+ // Rules that span devices: what one device declares about another has to agree with what the other declares.
2
+
3
+ import type { Device, Gre, Port, Prefix, Routed, Switched, Vxlan } from "../schema";
4
+ import { Address } from "./address";
5
+ import type { Finding } from "./define";
6
+ import { type Holder, Topology } from "./topology";
7
+
8
+ export class CrossChecks {
9
+ /** Every rule across devices. */
10
+ static run(devices: readonly Device[]): Finding[] {
11
+ const holders = Topology.holders(devices);
12
+
13
+ // Who holds each address. The first holder wins; a second is reported by checkAddresses.
14
+ const owners = new Map<string, Holder>();
15
+ for (const holder of holders) {
16
+ const key = Topology.keyOf(Address.host(holder.address))!;
17
+ if (!owners.has(key)) owners.set(key, holder);
18
+ }
19
+
20
+ return [
21
+ ...CrossChecks.checkLinks(devices),
22
+ ...CrossChecks.checkAddresses(holders),
23
+ ...CrossChecks.checkSessions(devices, owners),
24
+ ...CrossChecks.checkVrrp(holders),
25
+ ...CrossChecks.checkTunnels(devices, owners),
26
+ ];
27
+ }
28
+
29
+ /** The VLAN ids a port or LAG carries, by how it carries them, so two ends can be compared across devices. */
30
+ private static carried(device: Device, settings: Switched<string>) {
31
+ const id = (vlan: string | undefined) => (vlan ? device.vlans?.[vlan]?.id : undefined);
32
+ return {
33
+ access: id(settings.access_vlan),
34
+ native: id(settings.native_vlan),
35
+ trunk: (settings.trunk_vlans ?? []).map(id).sort((a, b) => (a ?? 0) - (b ?? 0)),
36
+ };
37
+ }
38
+
39
+ /** How a port carries its VLANs, in words: "VLANs 10, 20 tagged and VLAN 30 untagged as native". */
40
+ private static describeCarried({ access, native, trunk }: ReturnType<typeof CrossChecks.carried>): string {
41
+ const parts: string[] = [];
42
+ const tagged = trunk.filter((id) => id !== undefined);
43
+
44
+ if (access !== undefined) parts.push(`VLAN ${access} untagged`);
45
+ if (tagged.length) parts.push(`${tagged.length === 1 ? "VLAN" : "VLANs"} ${tagged.join(", ")} tagged`);
46
+ if (native !== undefined) parts.push(`VLAN ${native} untagged as native`);
47
+
48
+ return parts.length ? parts.join(" and ") : "no VLANs";
49
+ }
50
+
51
+ /** VLAN names by id, for the VLANs a port or LAG carries. */
52
+ private static vlanNames(device: Device, settings: Switched<string>): Map<number, string> {
53
+ const names = [settings.access_vlan, settings.native_vlan, ...(settings.trunk_vlans ?? [])].filter((name) => name !== undefined);
54
+
55
+ return new Map(names.flatMap((name) => (device.vlans?.[name] ? [[device.vlans[name].id, name] as const] : [])));
56
+ }
57
+
58
+ /** What a port's settings are kept on: the LAG when the port is a member of one, otherwise the port. */
59
+ private static carrierOf(device: Device, portName: string, port: Port<string, string, string, string>) {
60
+ const lag = port.lag ? device.interfaces?.[port.lag] : undefined;
61
+ if (port.lag && lag?.type === "lag") return { name: port.lag, settings: lag };
62
+
63
+ return { name: portName, settings: port };
64
+ }
65
+
66
+ /** The plain addresses of one family on an interface, leaving out point-to-point ones. */
67
+ private static plainAddresses(settings: Routed<string>, family: string): string[] {
68
+ return (settings.addresses ?? []).filter(
69
+ (entry): entry is Prefix => typeof entry === "string" && Address.isCidr(entry) && Address.family(entry) === family
70
+ );
71
+ }
72
+
73
+ /** Why the two ends of a cable cannot reach each other by address, or undefined when they can. */
74
+ private static addressDisagreement(here: Routed<string>, there: Routed<string>, far: Device, farName: string): string | undefined {
75
+ for (const family of ["ipv4", "ipv6"]) {
76
+ const ours = CrossChecks.plainAddresses(here, family);
77
+ const theirs = CrossChecks.plainAddresses(there, family);
78
+ if (!ours.length || !theirs.length) continue;
79
+
80
+ // A prefix includes its length, so 10.0.0.0/24 and 10.0.0.0/25 are not one.
81
+ const theirPrefixes = new Set(theirs.map(Address.prefixOf));
82
+ if (!ours.some((address) => theirPrefixes.has(Address.prefixOf(address)))) {
83
+ return `has ${ours.join(", ")}, and ${far.name} ${farName} has ${theirs.join(", ")}: not one prefix of one length`;
84
+ }
85
+ }
86
+
87
+ // A point-to-point address names the far end's own address as its peer.
88
+ const farHosts = new Set(
89
+ (there.addresses ?? []).map((entry) => Topology.keyOf(Address.host(typeof entry === "string" ? entry : entry.address)))
90
+ );
91
+
92
+ for (const entry of here.addresses ?? []) {
93
+ if (typeof entry === "string" || !Topology.keyOf(entry.peer) || farHosts.has(Topology.keyOf(entry.peer))) continue;
94
+ return `${entry.address} names peer ${entry.peer}, which ${far.name} ${farName} does not hold`;
95
+ }
96
+
97
+ return undefined;
98
+ }
99
+
100
+ /** A link names a device and port that exist, the far end links back, and both ends carry the same VLANs the same way, with the same MTU, speed and addresses. */
101
+ private static checkLinks(devices: readonly Device[]): Finding[] {
102
+ const findings: Finding[] = [];
103
+ const byName = new Map(devices.map((device) => [device.name, device]));
104
+
105
+ for (const device of devices) {
106
+ for (const [portName, port] of Object.entries(device.ports ?? {})) {
107
+ const link = port?.link;
108
+ if (!port || !link) continue;
109
+
110
+ const report = (message: string, level: Finding["level"] = "error") =>
111
+ findings.push({
112
+ level,
113
+ device: device.name,
114
+ message: `port ${portName} link: ${message}`,
115
+ });
116
+
117
+ const far = byName.get(link.device);
118
+ if (!far) {
119
+ report(`device ${link.device} is not in the network`);
120
+ continue;
121
+ }
122
+
123
+ const farPort = far.ports?.[link.port];
124
+ if (!farPort) {
125
+ report(`${link.device} does not declare port ${link.port}`);
126
+ continue;
127
+ }
128
+
129
+ const linksBack = farPort.link?.device === device.name && farPort.link.port === portName;
130
+ if (!linksBack) {
131
+ report(`${link.device} ${link.port} does not link back to it`);
132
+ continue;
133
+ }
134
+
135
+ const near = CrossChecks.carrierOf(device, portName, port);
136
+ const away = CrossChecks.carrierOf(far, link.port, farPort);
137
+
138
+ // A LAG's VLANs are on the LAG, so its member ports are compared by what their LAGs carry.
139
+ const inLag = near.name !== portName;
140
+ const farInLag = away.name !== link.port;
141
+ if (inLag !== farInLag) {
142
+ report(
143
+ inLag
144
+ ? `${portName} is in ${near.name}, and ${link.device} ${link.port} is in no LAG`
145
+ : `${portName} is in no LAG, and ${link.device} ${link.port} is in ${away.name}`
146
+ );
147
+ }
148
+
149
+ const here = CrossChecks.carried(device, near.settings);
150
+ const there = CrossChecks.carried(far, away.settings);
151
+ if (JSON.stringify(here) !== JSON.stringify(there)) {
152
+ report(
153
+ `${near.name} carries ${CrossChecks.describeCarried(here)}, and ${link.device} ${away.name} carries ${CrossChecks.describeCarried(there)}`
154
+ );
155
+ }
156
+
157
+ if (port.speed && farPort.speed && port.speed !== farPort.speed) {
158
+ report(`speed ${port.speed}, and ${link.device} ${link.port} is ${farPort.speed}`);
159
+ }
160
+
161
+ const mtu = near.settings.mtu ?? 1500;
162
+ const farMtu = away.settings.mtu ?? 1500;
163
+ if (mtu !== farMtu) report(`MTU ${mtu} on ${near.name}, and ${link.device} ${away.name} has ${farMtu}`);
164
+
165
+ const disagreement = CrossChecks.addressDisagreement(near.settings, away.settings, far, away.name);
166
+ if (disagreement) report(`${near.name} ${disagreement}`);
167
+
168
+ // One VLAN under two names works, but reads as two VLANs.
169
+ const farNames = CrossChecks.vlanNames(far, away.settings);
170
+ for (const [id, name] of CrossChecks.vlanNames(device, near.settings)) {
171
+ const farName = farNames.get(id);
172
+ if (farName && farName !== name) report(`VLAN ${id} is ${name} here and ${farName} on ${link.device}`, "warning");
173
+ }
174
+ }
175
+ }
176
+
177
+ return findings;
178
+ }
179
+
180
+ /** No address is held by two devices in one VRF. Two loopbacks may hold one on purpose, for anycast, so that is a warning. */
181
+ private static checkAddresses(holders: Holder[]): Finding[] {
182
+ const findings: Finding[] = [];
183
+ const first = new Map<string, Holder>();
184
+
185
+ const isLoopback = (holder: Holder) => holder.device.interfaces?.[holder.name]?.type === "loopback";
186
+
187
+ for (const holder of holders) {
188
+ const key = `${holder.vrf ?? ""} ${Topology.keyOf(Address.host(holder.address))!}`;
189
+ const earlier = first.get(key);
190
+
191
+ if (!earlier) {
192
+ first.set(key, holder);
193
+ continue;
194
+ }
195
+
196
+ // Two on one device are reported by the device's own checks.
197
+ if (earlier.device === holder.device) continue;
198
+
199
+ const anycast = isLoopback(earlier) && isLoopback(holder);
200
+
201
+ findings.push({
202
+ level: anycast ? "warning" : "error",
203
+ device: holder.device.name,
204
+ message: `${Address.host(holder.address)} on ${holder.name} is also on ${earlier.device.name} ${earlier.name}${anycast ? ", which is right only for anycast" : ""}`,
205
+ });
206
+ }
207
+
208
+ return findings;
209
+ }
210
+
211
+ /** A BGP session to an address another of our devices holds needs that device's session back, in the AS it expects, from the address it names. */
212
+ private static checkSessions(devices: readonly Device[], owners: Map<string, Holder>): Finding[] {
213
+ const findings: Finding[] = [];
214
+
215
+ const sessionsOf = (device: Device) => {
216
+ const bgp = device.routing?.bgp;
217
+
218
+ return Object.entries(bgp?.neighbors ?? {}).map(([name, neighbor]) => {
219
+ const group = neighbor.group ? bgp?.groups?.[neighbor.group] : undefined;
220
+
221
+ return {
222
+ name,
223
+ address: neighbor.address,
224
+ remoteAs: neighbor.remote_as ?? group?.remote_as,
225
+ localAddress: neighbor.local_address ?? group?.local_address,
226
+ };
227
+ });
228
+ };
229
+
230
+ for (const device of devices) {
231
+ for (const session of sessionsOf(device)) {
232
+ const far = owners.get(Topology.keyOf(session.address) ?? "")?.device;
233
+ if (!far || far === device) continue;
234
+
235
+ const report = (message: string) =>
236
+ findings.push({ level: "error", device: device.name, message: `neighbor ${session.name}: ${message}` });
237
+
238
+ const farBgp = far.routing?.bgp;
239
+ if (!farBgp) {
240
+ report(`${session.address} is ${far.name}'s, which runs no BGP`);
241
+ continue;
242
+ }
243
+
244
+ if (session.remoteAs !== undefined && session.remoteAs !== farBgp.asn) {
245
+ report(`expects AS${session.remoteAs}, and ${far.name} is AS${farBgp.asn}`);
246
+ }
247
+
248
+ // Two routers often hold one session per family, and a session of the other family does not answer this one.
249
+ const back = sessionsOf(far).find(
250
+ (other) =>
251
+ owners.get(Topology.keyOf(other.address) ?? "")?.device === device &&
252
+ Address.family(other.address) === Address.family(session.address)
253
+ );
254
+ if (!back) {
255
+ report(`${far.name} has no session back to ${device.name}`);
256
+ continue;
257
+ }
258
+
259
+ // The far end only accepts the session from the address it names.
260
+ if (session.localAddress && Topology.keyOf(session.localAddress) !== Topology.keyOf(back.address)) {
261
+ report(`starts from ${session.localAddress}, and ${far.name}'s session ${back.name} expects ${back.address}`);
262
+ }
263
+ }
264
+ }
265
+
266
+ return findings;
267
+ }
268
+
269
+ /** On one segment, a VRRP id has one address and an address has one id, on every device. */
270
+ private static checkVrrp(holders: Holder[]): Finding[] {
271
+ const findings: Finding[] = [];
272
+ const bySegment = new Map<string, { holder: Holder; id: number; address: string }[]>();
273
+
274
+ for (const holder of holders) {
275
+ const segment = Address.prefixOf(holder.address);
276
+
277
+ for (const group of holder.vrrp) {
278
+ if (!Topology.keyOf(group.address) || Address.family(group.address) !== Address.family(holder.address)) continue;
279
+ if (!Address.contains(segment, group.address)) continue;
280
+
281
+ bySegment.set(segment, [...(bySegment.get(segment) ?? []), { holder, id: group.id, address: group.address }]);
282
+ }
283
+ }
284
+
285
+ for (const members of bySegment.values()) {
286
+ for (const member of members) {
287
+ const where = `${member.holder.device.name} ${member.holder.name}`;
288
+ const others = members.filter((other) => other.holder.device !== member.holder.device);
289
+
290
+ const sameId = others.find(
291
+ (other) => other.id === member.id && Topology.keyOf(other.address) !== Topology.keyOf(member.address)
292
+ );
293
+ if (sameId) {
294
+ findings.push({
295
+ level: "error",
296
+ device: member.holder.device.name,
297
+ message: `VRRP ${member.id} on ${member.holder.name} is ${member.address}, and on ${sameId.holder.device.name} ${sameId.holder.name} it is ${sameId.address}`,
298
+ });
299
+ }
300
+
301
+ const sameAddress = others.find(
302
+ (other) => Topology.keyOf(other.address) === Topology.keyOf(member.address) && other.id !== member.id
303
+ );
304
+ if (sameAddress) {
305
+ findings.push({
306
+ level: "error",
307
+ device: member.holder.device.name,
308
+ message: `VRRP address ${member.address} is id ${member.id} on ${where}, and id ${sameAddress.id} on ${sameAddress.holder.device.name} ${sameAddress.holder.name}`,
309
+ });
310
+ }
311
+ }
312
+ }
313
+
314
+ return findings;
315
+ }
316
+
317
+ /** A GRE or VXLAN tunnel to another of our devices has a tunnel back there, with the endpoints mirrored and, for VXLAN, the same VNI and port. */
318
+ private static checkTunnels(devices: readonly Device[], owners: Map<string, Holder>): Finding[] {
319
+ const findings: Finding[] = [];
320
+
321
+ const ends = devices.flatMap((device) =>
322
+ Object.entries(device.interfaces ?? {}).flatMap(([name, iface]) =>
323
+ iface.type === "gre" || iface.type === "vxlan" ? [{ device, name, iface: iface as Gre<string> | Vxlan<string> }] : []
324
+ )
325
+ );
326
+
327
+ const same = (a: string, b: string) => Topology.keyOf(a) !== undefined && Topology.keyOf(a) === Topology.keyOf(b);
328
+
329
+ for (const end of ends) {
330
+ const far = owners.get(Topology.keyOf(end.iface.remote) ?? "")?.device;
331
+ if (!far || far === end.device) continue;
332
+
333
+ const report = (message: string) =>
334
+ findings.push({ level: "error", device: end.device.name, message: `interface ${end.name}: ${message}` });
335
+ const kind = end.iface.type === "gre" ? "GRE" : "VXLAN";
336
+
337
+ // Two routers may run several VXLAN tunnels between them, one for each VNI, so a tunnel pairs with the one of its own VNI first.
338
+ const back = ends.filter(
339
+ (other) =>
340
+ other.device === far &&
341
+ other.iface.type === end.iface.type &&
342
+ same(other.iface.local, end.iface.remote) &&
343
+ same(other.iface.remote, end.iface.local)
344
+ );
345
+ const vniOf = (iface: Gre<string> | Vxlan<string>) => (iface.type === "vxlan" ? iface.vni : undefined);
346
+ const mirror = back.find((other) => vniOf(other.iface) === vniOf(end.iface)) ?? back[0];
347
+
348
+ if (!mirror) {
349
+ report(`${end.iface.remote} is ${far.name}'s, which has no ${kind} tunnel from there back to ${end.iface.local}`);
350
+ continue;
351
+ }
352
+
353
+ if (end.iface.type !== "vxlan" || mirror.iface.type !== "vxlan") continue;
354
+
355
+ if (end.iface.vni !== mirror.iface.vni) report(`VNI ${end.iface.vni}, and ${far.name} ${mirror.name} has ${mirror.iface.vni}`);
356
+
357
+ const port = end.iface.port ?? 4789;
358
+ const farPort = mirror.iface.port ?? 4789;
359
+ if (port !== farPort) report(`UDP port ${port}, and ${far.name} ${mirror.name} has ${farPort}`);
360
+ }
361
+
362
+ return findings;
363
+ }
364
+ }
@@ -1,4 +1,4 @@
1
- import type { ModelName, PlatformName, PortName } from "../adapters/devices/catalog";
1
+ import type { ModelName, PlatformName, PortName } from "../platforms/catalog";
2
2
  import type { Device, Secret } from "../schema";
3
3
 
4
4
  /** A value resolved by name when a device is applied, from the environment or `.env` beside the config, and through 1Password when that value is an `op://` reference. Never the value itself. */
@@ -81,18 +81,41 @@ export type Network = {
81
81
  asn?: number;
82
82
  /** The BGP communities the network defines, printed by `circuit communities` for a looking glass or bgp.tools. */
83
83
  communities?: readonly Community[];
84
+ /**
85
+ * The site each device is at, such as a data centre or an office, by name. Devices ignore it.
86
+ *
87
+ * `circuit netbox` puts the device on the NetBox site of that name, and `circuit librenms` puts it in a LibreNMS device group of that name.
88
+ */
89
+ site?: (device: Device) => string;
84
90
  /** Keeps the network's PeeringDB record in step with the config: `circuit peeringdb`. Needs `asn`. */
85
91
  peeringdb?: {
86
92
  /** An API key with write access to the network's record. */
87
93
  api_key: Secret;
88
94
  };
89
- /** Keeps LibreNMS monitoring every device: `circuit librenms`. */
95
+ /** Keeps LibreNMS monitoring every device, under its name, with its SNMP credentials and location, and in a group for its `site`: `circuit librenms`. */
90
96
  librenms?: {
91
97
  /** Where LibreNMS is, such as `https://librenms.example.com`. */
92
98
  url: string;
93
- /** An API token that can add devices. */
99
+ /** An API token that can add and change devices and device groups. */
94
100
  api_token: Secret;
95
101
  };
102
+ /** Keeps NetBox in step with the config: `circuit netbox`. Needs `site`. */
103
+ netbox?: {
104
+ /** Where NetBox is, such as `https://netbox.example.com`. */
105
+ url: string;
106
+ /** An API token that can write. */
107
+ api_token: Secret;
108
+ /** What NetBox needs to know about a device that its config does not say. */
109
+ device: (device: Device) => NetBoxDevice;
110
+ };
111
+ };
112
+
113
+ /** What NetBox needs to know about a device that its config does not say. */
114
+ export type NetBoxDevice = {
115
+ /** The device role's name in NetBox, such as `Core switch`. Circuit makes the role when NetBox has none of that name. */
116
+ role: string;
117
+ /** The device type's model name in NetBox, made by the platform's vendor. The device's `model` when absent. */
118
+ device_type?: string;
96
119
  };
97
120
 
98
121
  /** Declare the network, in `circuit.config.ts`. */
@@ -0,0 +1,35 @@
1
+ // A project's own TypeScript files, and which of them declares a device: for renaming a device everywhere, and for a device's history.
2
+
3
+ import { readdirSync, readFileSync } from "node:fs";
4
+ import { join, relative } from "node:path";
5
+
6
+ export class ProjectFiles {
7
+ /** Directories that hold no config of the project's own. */
8
+ private static readonly SKIPPED = new Set(["node_modules", ".git", ".circuit", "dist"]);
9
+
10
+ /** The first name written out after a call to defineDevice: the device's own, not that of another device a link, a peer or a tunnel names further on. */
11
+ private static readonly FIRST_NAME = /^[\s\S]*?\bname\s*:\s*(["'`])(.*?)\1/;
12
+
13
+ /** Every TypeScript file of the project, relative to its root, in name order. */
14
+ static sources(root: string, directory = root): string[] {
15
+ const entries = readdirSync(directory, { withFileTypes: true }).sort((a, b) => (a.name < b.name ? -1 : a.name > b.name ? 1 : 0));
16
+
17
+ return entries.flatMap((entry) => {
18
+ if (entry.isDirectory())
19
+ return ProjectFiles.SKIPPED.has(entry.name) ? [] : ProjectFiles.sources(root, join(directory, entry.name));
20
+ return /\.(ts|mts|cts)$/.test(entry.name) ? [relative(root, join(directory, entry.name))] : [];
21
+ });
22
+ }
23
+
24
+ /** Whether a file's text declares the device: it calls defineDevice and gives it the name, written out whole. */
25
+ static declaresDevice(text: string, name: string): boolean {
26
+ return [...text.matchAll(/\bdefineDevice\s*\(/g)].some(
27
+ (call) => ProjectFiles.FIRST_NAME.exec(text.slice(call.index + call[0].length))?.[2] === name
28
+ );
29
+ }
30
+
31
+ /** The project files that declare the device, relative to the root. */
32
+ static deviceFiles(root: string, name: string, sources = ProjectFiles.sources(root)): string[] {
33
+ return sources.filter((file) => ProjectFiles.declaresDevice(readFileSync(join(root, file), "utf8"), name));
34
+ }
35
+ }