dynamic_routing_handlers.go 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144
  1. package api
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "strings"
  6. "debtik/internal/dynamicrouting"
  7. )
  8. // Summary
  9. func (s *Server) handleDynamicRoutingSummary(w http.ResponseWriter, r *http.Request) {
  10. summary := s.dynRoutingMgr.GetSummary()
  11. writeJSON(w, http.StatusOK, summary)
  12. }
  13. // -------------------------------------------------------------
  14. // BGP Handlers
  15. // -------------------------------------------------------------
  16. func (s *Server) handleGetBGPInstance(w http.ResponseWriter, r *http.Request) {
  17. inst := s.dynRoutingMgr.GetBGPInstance()
  18. writeJSON(w, http.StatusOK, inst)
  19. }
  20. func (s *Server) handleUpdateBGPInstance(w http.ResponseWriter, r *http.Request) {
  21. var inst dynamicrouting.BGPInstance
  22. if err := json.NewDecoder(r.Body).Decode(&inst); err != nil {
  23. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON: " + err.Error()})
  24. return
  25. }
  26. if err := s.dynRoutingMgr.UpdateBGPInstance(inst); err != nil {
  27. writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
  28. return
  29. }
  30. writeJSON(w, http.StatusOK, s.dynRoutingMgr.GetBGPInstance())
  31. }
  32. func (s *Server) handleListBGPPeers(w http.ResponseWriter, r *http.Request) {
  33. peers := s.dynRoutingMgr.ListBGPPeers()
  34. if peers == nil {
  35. peers = []dynamicrouting.BGPPeer{}
  36. }
  37. writeJSON(w, http.StatusOK, peers)
  38. }
  39. func (s *Server) handleSaveBGPPeer(w http.ResponseWriter, r *http.Request) {
  40. var peer dynamicrouting.BGPPeer
  41. if err := json.NewDecoder(r.Body).Decode(&peer); err != nil {
  42. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON: " + err.Error()})
  43. return
  44. }
  45. if peer.RemoteAddress == "" || peer.RemoteAS == 0 {
  46. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "remote_address and remote_as are required"})
  47. return
  48. }
  49. if err := s.dynRoutingMgr.SaveBGPPeer(&peer); err != nil {
  50. writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
  51. return
  52. }
  53. writeJSON(w, http.StatusOK, map[string]interface{}{"status": "success", "peer": peer})
  54. }
  55. func (s *Server) handleDeleteBGPPeer(w http.ResponseWriter, r *http.Request) {
  56. id := r.PathValue("id")
  57. if id == "" {
  58. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Missing peer id"})
  59. return
  60. }
  61. if err := s.dynRoutingMgr.DeleteBGPPeer(id); err != nil {
  62. writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
  63. return
  64. }
  65. writeJSON(w, http.StatusOK, map[string]string{"status": "deleted"})
  66. }
  67. func (s *Server) handleSetBGPPeerState(w http.ResponseWriter, r *http.Request) {
  68. id := r.PathValue("id")
  69. var req struct {
  70. Disabled bool `json:"disabled"`
  71. }
  72. if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
  73. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON"})
  74. return
  75. }
  76. if err := s.dynRoutingMgr.SetBGPPeerState(id, req.Disabled); err != nil {
  77. writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
  78. return
  79. }
  80. writeJSON(w, http.StatusOK, map[string]interface{}{"status": "success", "disabled": req.Disabled})
  81. }
  82. func (s *Server) handleListBGPNetworks(w http.ResponseWriter, r *http.Request) {
  83. nets := s.dynRoutingMgr.ListBGPNetworks()
  84. if nets == nil {
  85. nets = []dynamicrouting.BGPNetwork{}
  86. }
  87. writeJSON(w, http.StatusOK, nets)
  88. }
  89. func (s *Server) handleSaveBGPNetwork(w http.ResponseWriter, r *http.Request) {
  90. var net dynamicrouting.BGPNetwork
  91. if err := json.NewDecoder(r.Body).Decode(&net); err != nil {
  92. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON: " + err.Error()})
  93. return
  94. }
  95. if net.Network == "" {
  96. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "network (CIDR) is required"})
  97. return
  98. }
  99. if err := s.dynRoutingMgr.SaveBGPNetwork(net); err != nil {
  100. writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
  101. return
  102. }
  103. writeJSON(w, http.StatusOK, map[string]interface{}{"status": "success", "network": net})
  104. }
  105. func (s *Server) handleDeleteBGPNetwork(w http.ResponseWriter, r *http.Request) {
  106. id := r.PathValue("id")
  107. if id == "" {
  108. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Missing network id"})
  109. return
  110. }
  111. if err := s.dynRoutingMgr.DeleteBGPNetwork(id); err != nil {
  112. writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
  113. return
  114. }
  115. writeJSON(w, http.StatusOK, map[string]string{"status": "deleted"})
  116. }
  117. func (s *Server) handleGetBGPRoutes(w http.ResponseWriter, r *http.Request) {
  118. routes := s.dynRoutingMgr.GetBGPRoutes()
  119. if routes == nil {
  120. routes = []dynamicrouting.BGPRoute{}
  121. }
  122. writeJSON(w, http.StatusOK, routes)
  123. }
  124. // -------------------------------------------------------------
  125. // OSPF Handlers (RouterOS v7 Style OSPF)
  126. // -------------------------------------------------------------
  127. func (s *Server) handleListOSPFInstances(w http.ResponseWriter, r *http.Request) {
  128. insts := s.dynRoutingMgr.ListOSPFInstances()
  129. if insts == nil {
  130. insts = []dynamicrouting.OSPFInstance{}
  131. }
  132. writeJSON(w, http.StatusOK, insts)
  133. }
  134. func (s *Server) handleSaveOSPFInstance(w http.ResponseWriter, r *http.Request) {
  135. var inst dynamicrouting.OSPFInstance
  136. if err := json.NewDecoder(r.Body).Decode(&inst); err != nil {
  137. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON: " + err.Error()})
  138. return
  139. }
  140. if err := s.dynRoutingMgr.SaveOSPFInstance(&inst); err != nil {
  141. writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
  142. return
  143. }
  144. writeJSON(w, http.StatusOK, map[string]interface{}{"status": "success", "instance": inst})
  145. }
  146. func (s *Server) handleDeleteOSPFInstance(w http.ResponseWriter, r *http.Request) {
  147. id := r.PathValue("id")
  148. if id == "" {
  149. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Missing instance id"})
  150. return
  151. }
  152. if err := s.dynRoutingMgr.DeleteOSPFInstance(id); err != nil {
  153. writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
  154. return
  155. }
  156. writeJSON(w, http.StatusOK, map[string]string{"status": "deleted"})
  157. }
  158. func (s *Server) handleSetOSPFInstanceState(w http.ResponseWriter, r *http.Request) {
  159. id := r.PathValue("id")
  160. var req struct {
  161. Disabled bool `json:"disabled"`
  162. }
  163. if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
  164. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON"})
  165. return
  166. }
  167. if err := s.dynRoutingMgr.SetOSPFInstanceState(id, req.Disabled); err != nil {
  168. writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
  169. return
  170. }
  171. writeJSON(w, http.StatusOK, map[string]interface{}{"status": "success", "disabled": req.Disabled})
  172. }
  173. func (s *Server) handleGetOSPFInstance(w http.ResponseWriter, r *http.Request) {
  174. inst := s.dynRoutingMgr.GetOSPFInstance()
  175. writeJSON(w, http.StatusOK, inst)
  176. }
  177. func (s *Server) handleUpdateOSPFInstance(w http.ResponseWriter, r *http.Request) {
  178. var inst dynamicrouting.OSPFInstance
  179. if err := json.NewDecoder(r.Body).Decode(&inst); err != nil {
  180. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON: " + err.Error()})
  181. return
  182. }
  183. if err := s.dynRoutingMgr.UpdateOSPFInstance(inst); err != nil {
  184. writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
  185. return
  186. }
  187. writeJSON(w, http.StatusOK, s.dynRoutingMgr.GetOSPFInstance())
  188. }
  189. func (s *Server) handleListOSPFAreas(w http.ResponseWriter, r *http.Request) {
  190. areas := s.dynRoutingMgr.ListOSPFAreas()
  191. if areas == nil {
  192. areas = []dynamicrouting.OSPFArea{}
  193. }
  194. writeJSON(w, http.StatusOK, areas)
  195. }
  196. func (s *Server) handleSaveOSPFArea(w http.ResponseWriter, r *http.Request) {
  197. var area dynamicrouting.OSPFArea
  198. if err := json.NewDecoder(r.Body).Decode(&area); err != nil {
  199. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON: " + err.Error()})
  200. return
  201. }
  202. if area.AreaID == "" {
  203. area.AreaID = "0.0.0.0"
  204. }
  205. if err := s.dynRoutingMgr.SaveOSPFArea(area); err != nil {
  206. writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
  207. return
  208. }
  209. writeJSON(w, http.StatusOK, map[string]interface{}{"status": "success", "area": area})
  210. }
  211. func (s *Server) handleDeleteOSPFArea(w http.ResponseWriter, r *http.Request) {
  212. id := r.PathValue("id")
  213. if id == "" {
  214. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Missing area id"})
  215. return
  216. }
  217. if err := s.dynRoutingMgr.DeleteOSPFArea(id); err != nil {
  218. writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
  219. return
  220. }
  221. writeJSON(w, http.StatusOK, map[string]string{"status": "deleted"})
  222. }
  223. func (s *Server) handleSetOSPFAreaState(w http.ResponseWriter, r *http.Request) {
  224. id := r.PathValue("id")
  225. var req struct {
  226. Disabled bool `json:"disabled"`
  227. }
  228. if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
  229. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON"})
  230. return
  231. }
  232. if err := s.dynRoutingMgr.SetOSPFAreaState(id, req.Disabled); err != nil {
  233. writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
  234. return
  235. }
  236. writeJSON(w, http.StatusOK, map[string]interface{}{"status": "success", "disabled": req.Disabled})
  237. }
  238. // Area Ranges
  239. func (s *Server) handleListOSPFAreaRanges(w http.ResponseWriter, r *http.Request) {
  240. ranges := s.dynRoutingMgr.ListOSPFAreaRanges()
  241. if ranges == nil {
  242. ranges = []dynamicrouting.OSPFAreaRange{}
  243. }
  244. writeJSON(w, http.StatusOK, ranges)
  245. }
  246. func (s *Server) handleSaveOSPFAreaRange(w http.ResponseWriter, r *http.Request) {
  247. var ar dynamicrouting.OSPFAreaRange
  248. if err := json.NewDecoder(r.Body).Decode(&ar); err != nil {
  249. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON: " + err.Error()})
  250. return
  251. }
  252. if ar.Prefix == "" {
  253. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Prefix is required"})
  254. return
  255. }
  256. if err := s.dynRoutingMgr.SaveOSPFAreaRange(ar); err != nil {
  257. writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
  258. return
  259. }
  260. writeJSON(w, http.StatusOK, map[string]interface{}{"status": "success", "area_range": ar})
  261. }
  262. func (s *Server) handleDeleteOSPFAreaRange(w http.ResponseWriter, r *http.Request) {
  263. id := r.PathValue("id")
  264. if id == "" {
  265. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Missing range id"})
  266. return
  267. }
  268. if err := s.dynRoutingMgr.DeleteOSPFAreaRange(id); err != nil {
  269. writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
  270. return
  271. }
  272. writeJSON(w, http.StatusOK, map[string]string{"status": "deleted"})
  273. }
  274. // Interface Templates
  275. func (s *Server) handleListOSPFInterfaces(w http.ResponseWriter, r *http.Request) {
  276. ifaces := s.dynRoutingMgr.ListOSPFInterfaces()
  277. if ifaces == nil {
  278. ifaces = []dynamicrouting.OSPFInterface{}
  279. }
  280. writeJSON(w, http.StatusOK, ifaces)
  281. }
  282. func (s *Server) handleSaveOSPFInterface(w http.ResponseWriter, r *http.Request) {
  283. var iface dynamicrouting.OSPFInterface
  284. if err := json.NewDecoder(r.Body).Decode(&iface); err != nil {
  285. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON: " + err.Error()})
  286. return
  287. }
  288. if err := s.dynRoutingMgr.SaveOSPFInterface(iface); err != nil {
  289. writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
  290. return
  291. }
  292. writeJSON(w, http.StatusOK, map[string]interface{}{"status": "success", "interface": iface})
  293. }
  294. func (s *Server) handleDeleteOSPFInterface(w http.ResponseWriter, r *http.Request) {
  295. id := r.PathValue("id")
  296. if id == "" {
  297. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Missing interface id"})
  298. return
  299. }
  300. if err := s.dynRoutingMgr.DeleteOSPFInterface(id); err != nil {
  301. writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
  302. return
  303. }
  304. writeJSON(w, http.StatusOK, map[string]string{"status": "deleted"})
  305. }
  306. func (s *Server) handleSetOSPFInterfaceState(w http.ResponseWriter, r *http.Request) {
  307. id := r.PathValue("id")
  308. var req struct {
  309. Disabled bool `json:"disabled"`
  310. }
  311. if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
  312. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON"})
  313. return
  314. }
  315. if err := s.dynRoutingMgr.SetOSPFInterfaceState(id, req.Disabled); err != nil {
  316. writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
  317. return
  318. }
  319. writeJSON(w, http.StatusOK, map[string]interface{}{"status": "success", "disabled": req.Disabled})
  320. }
  321. // Live Dynamic Interfaces
  322. func (s *Server) handleGetOSPFInterfacesLive(w http.ResponseWriter, r *http.Request) {
  323. live := s.dynRoutingMgr.GetOSPFInterfacesLive()
  324. if live == nil {
  325. live = []dynamicrouting.OSPFInterfaceLive{}
  326. }
  327. writeJSON(w, http.StatusOK, live)
  328. }
  329. // Static Neighbors
  330. func (s *Server) handleListOSPFStaticNeighbors(w http.ResponseWriter, r *http.Request) {
  331. nbrs := s.dynRoutingMgr.ListOSPFStaticNeighbors()
  332. if nbrs == nil {
  333. nbrs = []dynamicrouting.OSPFStaticNeighbor{}
  334. }
  335. writeJSON(w, http.StatusOK, nbrs)
  336. }
  337. func (s *Server) handleSaveOSPFStaticNeighbor(w http.ResponseWriter, r *http.Request) {
  338. var nbr dynamicrouting.OSPFStaticNeighbor
  339. if err := json.NewDecoder(r.Body).Decode(&nbr); err != nil {
  340. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON: " + err.Error()})
  341. return
  342. }
  343. if nbr.Address == "" {
  344. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Address is required"})
  345. return
  346. }
  347. if err := s.dynRoutingMgr.SaveOSPFStaticNeighbor(nbr); err != nil {
  348. writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
  349. return
  350. }
  351. writeJSON(w, http.StatusOK, map[string]interface{}{"status": "success", "neighbor": nbr})
  352. }
  353. func (s *Server) handleDeleteOSPFStaticNeighbor(w http.ResponseWriter, r *http.Request) {
  354. id := r.PathValue("id")
  355. if id == "" {
  356. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Missing static neighbor id"})
  357. return
  358. }
  359. if err := s.dynRoutingMgr.DeleteOSPFStaticNeighbor(id); err != nil {
  360. writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
  361. return
  362. }
  363. writeJSON(w, http.StatusOK, map[string]string{"status": "deleted"})
  364. }
  365. func (s *Server) handleSetOSPFStaticNeighborState(w http.ResponseWriter, r *http.Request) {
  366. id := r.PathValue("id")
  367. if id == "" {
  368. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Missing static neighbor id"})
  369. return
  370. }
  371. var req struct {
  372. Disabled bool `json:"disabled"`
  373. }
  374. if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
  375. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON: " + err.Error()})
  376. return
  377. }
  378. if err := s.dynRoutingMgr.SetOSPFStaticNeighborState(id, req.Disabled); err != nil {
  379. writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
  380. return
  381. }
  382. writeJSON(w, http.StatusOK, map[string]interface{}{"status": "success", "disabled": req.Disabled})
  383. }
  384. // Live Neighbors
  385. func (s *Server) handleGetOSPFNeighbors(w http.ResponseWriter, r *http.Request) {
  386. nbrs := s.dynRoutingMgr.GetOSPFNeighbors()
  387. if nbrs == nil {
  388. nbrs = []dynamicrouting.OSPFNeighbor{}
  389. }
  390. writeJSON(w, http.StatusOK, nbrs)
  391. }
  392. // Live LSA Database
  393. func (s *Server) handleGetOSPFLSA(w http.ResponseWriter, r *http.Request) {
  394. lsa := s.dynRoutingMgr.GetOSPFLSA()
  395. if lsa == nil {
  396. lsa = []dynamicrouting.OSPFLSA{}
  397. }
  398. writeJSON(w, http.StatusOK, lsa)
  399. }
  400. func (s *Server) handleGetOSPFRoutes(w http.ResponseWriter, r *http.Request) {
  401. routes := s.dynRoutingMgr.GetOSPFRoutes()
  402. if routes == nil {
  403. routes = []dynamicrouting.OSPFRoute{}
  404. }
  405. writeJSON(w, http.StatusOK, routes)
  406. }
  407. // -------------------------------------------------------------
  408. // Routing Filters Handlers (/routing filter)
  409. // -------------------------------------------------------------
  410. func (s *Server) handleListRoutingFilters(w http.ResponseWriter, r *http.Request) {
  411. filters := s.dynRoutingMgr.ListRoutingFilters()
  412. if filters == nil {
  413. filters = []dynamicrouting.RoutingFilterRule{}
  414. }
  415. writeJSON(w, http.StatusOK, filters)
  416. }
  417. func (s *Server) handleSaveRoutingFilter(w http.ResponseWriter, r *http.Request) {
  418. var rule dynamicrouting.RoutingFilterRule
  419. if err := json.NewDecoder(r.Body).Decode(&rule); err != nil {
  420. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON: " + err.Error()})
  421. return
  422. }
  423. if rule.Chain == "" {
  424. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Chain name is required"})
  425. return
  426. }
  427. if rule.Action == "" && rule.Rule == "" {
  428. rule.Action = "accept"
  429. }
  430. if err := s.dynRoutingMgr.SaveRoutingFilter(&rule); err != nil {
  431. writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
  432. return
  433. }
  434. writeJSON(w, http.StatusOK, map[string]string{"status": "success", "id": rule.ID})
  435. }
  436. func (s *Server) handleDeleteRoutingFilter(w http.ResponseWriter, r *http.Request) {
  437. id := r.PathValue("id")
  438. if id == "" {
  439. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Missing filter rule ID"})
  440. return
  441. }
  442. if err := s.dynRoutingMgr.DeleteRoutingFilter(id); err != nil {
  443. writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
  444. return
  445. }
  446. writeJSON(w, http.StatusOK, map[string]string{"status": "deleted"})
  447. }
  448. func (s *Server) handleSetRoutingFilterState(w http.ResponseWriter, r *http.Request) {
  449. id := r.PathValue("id")
  450. if id == "" {
  451. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Missing filter rule ID"})
  452. return
  453. }
  454. var req struct {
  455. Disabled bool `json:"disabled"`
  456. }
  457. if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
  458. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON"})
  459. return
  460. }
  461. if err := s.dynRoutingMgr.SetRoutingFilterState(id, req.Disabled); err != nil {
  462. writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
  463. return
  464. }
  465. writeJSON(w, http.StatusOK, map[string]interface{}{"status": "success", "disabled": req.Disabled})
  466. }
  467. // -------------------------------------------------------------
  468. // Community Lists Handlers (/routing filter community-list)
  469. // -------------------------------------------------------------
  470. func (s *Server) handleListCommunityLists(w http.ResponseWriter, r *http.Request) {
  471. lists := s.dynRoutingMgr.ListCommunityLists()
  472. if lists == nil {
  473. lists = []dynamicrouting.RoutingFilterCommunityList{}
  474. }
  475. writeJSON(w, http.StatusOK, lists)
  476. }
  477. func (s *Server) handleSaveCommunityList(w http.ResponseWriter, r *http.Request) {
  478. var cl dynamicrouting.RoutingFilterCommunityList
  479. if err := json.NewDecoder(r.Body).Decode(&cl); err != nil {
  480. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON: " + err.Error()})
  481. return
  482. }
  483. if strings.TrimSpace(cl.Name) == "" {
  484. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "List name is required"})
  485. return
  486. }
  487. if err := s.dynRoutingMgr.SaveCommunityList(&cl); err != nil {
  488. writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
  489. return
  490. }
  491. writeJSON(w, http.StatusOK, map[string]string{"status": "success", "id": cl.ID})
  492. }
  493. func (s *Server) handleDeleteCommunityList(w http.ResponseWriter, r *http.Request) {
  494. id := r.PathValue("id")
  495. if id == "" {
  496. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Missing community list ID"})
  497. return
  498. }
  499. if err := s.dynRoutingMgr.DeleteCommunityList(id); err != nil {
  500. writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
  501. return
  502. }
  503. writeJSON(w, http.StatusOK, map[string]string{"status": "deleted"})
  504. }
  505. func (s *Server) handleSetCommunityListState(w http.ResponseWriter, r *http.Request) {
  506. id := r.PathValue("id")
  507. if id == "" {
  508. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Missing community list ID"})
  509. return
  510. }
  511. var req struct {
  512. Disabled bool `json:"disabled"`
  513. }
  514. if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
  515. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON"})
  516. return
  517. }
  518. if err := s.dynRoutingMgr.SetCommunityListState(id, req.Disabled); err != nil {
  519. writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
  520. return
  521. }
  522. writeJSON(w, http.StatusOK, map[string]interface{}{"status": "success", "disabled": req.Disabled})
  523. }
  524. // -------------------------------------------------------------
  525. // AS Lists Handlers (/routing filter num-list)
  526. // -------------------------------------------------------------
  527. func (s *Server) handleListASLists(w http.ResponseWriter, r *http.Request) {
  528. lists := s.dynRoutingMgr.ListASLists()
  529. if lists == nil {
  530. lists = []dynamicrouting.RoutingFilterASList{}
  531. }
  532. writeJSON(w, http.StatusOK, lists)
  533. }
  534. func (s *Server) handleSaveASList(w http.ResponseWriter, r *http.Request) {
  535. var al dynamicrouting.RoutingFilterASList
  536. if err := json.NewDecoder(r.Body).Decode(&al); err != nil {
  537. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON: " + err.Error()})
  538. return
  539. }
  540. if strings.TrimSpace(al.Name) == "" {
  541. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "List name is required"})
  542. return
  543. }
  544. if err := s.dynRoutingMgr.SaveASList(&al); err != nil {
  545. writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
  546. return
  547. }
  548. writeJSON(w, http.StatusOK, map[string]string{"status": "success", "id": al.ID})
  549. }
  550. func (s *Server) handleDeleteASList(w http.ResponseWriter, r *http.Request) {
  551. id := r.PathValue("id")
  552. if id == "" {
  553. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Missing AS list ID"})
  554. return
  555. }
  556. if err := s.dynRoutingMgr.DeleteASList(id); err != nil {
  557. writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
  558. return
  559. }
  560. writeJSON(w, http.StatusOK, map[string]string{"status": "deleted"})
  561. }
  562. func (s *Server) handleSetASListState(w http.ResponseWriter, r *http.Request) {
  563. id := r.PathValue("id")
  564. if id == "" {
  565. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Missing AS list ID"})
  566. return
  567. }
  568. var req struct {
  569. Disabled bool `json:"disabled"`
  570. }
  571. if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
  572. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON"})
  573. return
  574. }
  575. if err := s.dynRoutingMgr.SetASListState(id, req.Disabled); err != nil {
  576. writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
  577. return
  578. }
  579. writeJSON(w, http.StatusOK, map[string]interface{}{"status": "success", "disabled": req.Disabled})
  580. }
  581. // -------------------------------------------------------------
  582. // RIP Handlers (/api/routing/rip/*)
  583. // -------------------------------------------------------------
  584. func (s *Server) handleListRIPInstances(w http.ResponseWriter, r *http.Request) {
  585. insts := s.dynRoutingMgr.ListRIPInstances()
  586. if insts == nil {
  587. insts = []dynamicrouting.RIPInstance{}
  588. }
  589. writeJSON(w, http.StatusOK, insts)
  590. }
  591. func (s *Server) handleSaveRIPInstance(w http.ResponseWriter, r *http.Request) {
  592. var inst dynamicrouting.RIPInstance
  593. if err := json.NewDecoder(r.Body).Decode(&inst); err != nil {
  594. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON: " + err.Error()})
  595. return
  596. }
  597. res, err := s.dynRoutingMgr.SaveRIPInstance(inst)
  598. if err != nil {
  599. writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
  600. return
  601. }
  602. writeJSON(w, http.StatusOK, res)
  603. }
  604. func (s *Server) handleDeleteRIPInstance(w http.ResponseWriter, r *http.Request) {
  605. id := r.PathValue("id")
  606. if id == "" {
  607. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Missing instance ID"})
  608. return
  609. }
  610. if err := s.dynRoutingMgr.DeleteRIPInstance(id); err != nil {
  611. writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
  612. return
  613. }
  614. writeJSON(w, http.StatusOK, map[string]string{"status": "deleted"})
  615. }
  616. func (s *Server) handleSetRIPInstanceState(w http.ResponseWriter, r *http.Request) {
  617. id := r.PathValue("id")
  618. var req struct {
  619. Disabled bool `json:"disabled"`
  620. }
  621. if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
  622. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON"})
  623. return
  624. }
  625. if err := s.dynRoutingMgr.SetRIPInstanceState(id, req.Disabled); err != nil {
  626. writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
  627. return
  628. }
  629. writeJSON(w, http.StatusOK, map[string]interface{}{"status": "success", "disabled": req.Disabled})
  630. }
  631. func (s *Server) handleListRIPInterfaces(w http.ResponseWriter, r *http.Request) {
  632. ifaces := s.dynRoutingMgr.ListRIPInterfaces()
  633. if ifaces == nil {
  634. ifaces = []dynamicrouting.RIPInterface{}
  635. }
  636. writeJSON(w, http.StatusOK, ifaces)
  637. }
  638. func (s *Server) handleSaveRIPInterface(w http.ResponseWriter, r *http.Request) {
  639. var iface dynamicrouting.RIPInterface
  640. if err := json.NewDecoder(r.Body).Decode(&iface); err != nil {
  641. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON: " + err.Error()})
  642. return
  643. }
  644. res, err := s.dynRoutingMgr.SaveRIPInterface(iface)
  645. if err != nil {
  646. writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
  647. return
  648. }
  649. writeJSON(w, http.StatusOK, res)
  650. }
  651. func (s *Server) handleDeleteRIPInterface(w http.ResponseWriter, r *http.Request) {
  652. id := r.PathValue("id")
  653. if id == "" {
  654. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Missing interface ID"})
  655. return
  656. }
  657. if err := s.dynRoutingMgr.DeleteRIPInterface(id); err != nil {
  658. writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
  659. return
  660. }
  661. writeJSON(w, http.StatusOK, map[string]string{"status": "deleted"})
  662. }
  663. func (s *Server) handleSetRIPInterfaceState(w http.ResponseWriter, r *http.Request) {
  664. id := r.PathValue("id")
  665. var req struct {
  666. Disabled bool `json:"disabled"`
  667. }
  668. if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
  669. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON"})
  670. return
  671. }
  672. if err := s.dynRoutingMgr.SetRIPInterfaceState(id, req.Disabled); err != nil {
  673. writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
  674. return
  675. }
  676. writeJSON(w, http.StatusOK, map[string]interface{}{"status": "success", "disabled": req.Disabled})
  677. }
  678. func (s *Server) handleListRIPNeighbors(w http.ResponseWriter, r *http.Request) {
  679. nbrs := s.dynRoutingMgr.ListRIPNeighbors()
  680. if nbrs == nil {
  681. nbrs = []dynamicrouting.RIPNeighbor{}
  682. }
  683. writeJSON(w, http.StatusOK, nbrs)
  684. }
  685. func (s *Server) handleSaveRIPNeighbor(w http.ResponseWriter, r *http.Request) {
  686. var nbr dynamicrouting.RIPNeighbor
  687. if err := json.NewDecoder(r.Body).Decode(&nbr); err != nil {
  688. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON: " + err.Error()})
  689. return
  690. }
  691. res, err := s.dynRoutingMgr.SaveRIPNeighbor(nbr)
  692. if err != nil {
  693. writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
  694. return
  695. }
  696. writeJSON(w, http.StatusOK, res)
  697. }
  698. func (s *Server) handleDeleteRIPNeighbor(w http.ResponseWriter, r *http.Request) {
  699. id := r.PathValue("id")
  700. if id == "" {
  701. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Missing neighbor ID"})
  702. return
  703. }
  704. if err := s.dynRoutingMgr.DeleteRIPNeighbor(id); err != nil {
  705. writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
  706. return
  707. }
  708. writeJSON(w, http.StatusOK, map[string]string{"status": "deleted"})
  709. }
  710. func (s *Server) handleGetRIPNeighborsLive(w http.ResponseWriter, r *http.Request) {
  711. nbrs := s.dynRoutingMgr.GetRIPNeighbors()
  712. if nbrs == nil {
  713. nbrs = []dynamicrouting.RIPNeighborLive{}
  714. }
  715. writeJSON(w, http.StatusOK, nbrs)
  716. }
  717. func (s *Server) handleGetRIPRoutes(w http.ResponseWriter, r *http.Request) {
  718. rts := s.dynRoutingMgr.GetRIPRoutes()
  719. if rts == nil {
  720. rts = []dynamicrouting.RIPRoute{}
  721. }
  722. writeJSON(w, http.StatusOK, rts)
  723. }
  724. // -------------------------------------------------------------
  725. // BFD Handlers (/api/routing/bfd/*)
  726. // -------------------------------------------------------------
  727. func (s *Server) handleGetBFDInstance(w http.ResponseWriter, r *http.Request) {
  728. inst := s.dynRoutingMgr.GetBFDInstance()
  729. writeJSON(w, http.StatusOK, inst)
  730. }
  731. func (s *Server) handleSaveBFDInstance(w http.ResponseWriter, r *http.Request) {
  732. var inst dynamicrouting.BFDInstance
  733. if err := json.NewDecoder(r.Body).Decode(&inst); err != nil {
  734. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON: " + err.Error()})
  735. return
  736. }
  737. if err := s.dynRoutingMgr.SaveBFDInstance(inst); err != nil {
  738. writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
  739. return
  740. }
  741. writeJSON(w, http.StatusOK, s.dynRoutingMgr.GetBFDInstance())
  742. }
  743. func (s *Server) handleListBFDInterfaces(w http.ResponseWriter, r *http.Request) {
  744. ifaces := s.dynRoutingMgr.ListBFDInterfaces()
  745. if ifaces == nil {
  746. ifaces = []dynamicrouting.BFDInterface{}
  747. }
  748. writeJSON(w, http.StatusOK, ifaces)
  749. }
  750. func (s *Server) handleSaveBFDInterface(w http.ResponseWriter, r *http.Request) {
  751. var iface dynamicrouting.BFDInterface
  752. if err := json.NewDecoder(r.Body).Decode(&iface); err != nil {
  753. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON: " + err.Error()})
  754. return
  755. }
  756. res, err := s.dynRoutingMgr.SaveBFDInterface(iface)
  757. if err != nil {
  758. writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
  759. return
  760. }
  761. writeJSON(w, http.StatusOK, res)
  762. }
  763. func (s *Server) handleDeleteBFDInterface(w http.ResponseWriter, r *http.Request) {
  764. id := r.PathValue("id")
  765. if id == "" {
  766. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Missing interface ID"})
  767. return
  768. }
  769. if err := s.dynRoutingMgr.DeleteBFDInterface(id); err != nil {
  770. writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
  771. return
  772. }
  773. writeJSON(w, http.StatusOK, map[string]string{"status": "deleted"})
  774. }
  775. func (s *Server) handleSetBFDInterfaceState(w http.ResponseWriter, r *http.Request) {
  776. id := r.PathValue("id")
  777. var req struct {
  778. Disabled bool `json:"disabled"`
  779. }
  780. if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
  781. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON"})
  782. return
  783. }
  784. if err := s.dynRoutingMgr.SetBFDInterfaceState(id, req.Disabled); err != nil {
  785. writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
  786. return
  787. }
  788. writeJSON(w, http.StatusOK, map[string]interface{}{"status": "success", "disabled": req.Disabled})
  789. }
  790. func (s *Server) handleListBFDNeighbors(w http.ResponseWriter, r *http.Request) {
  791. nbrs := s.dynRoutingMgr.ListBFDNeighbors()
  792. if nbrs == nil {
  793. nbrs = []dynamicrouting.BFDNeighbor{}
  794. }
  795. writeJSON(w, http.StatusOK, nbrs)
  796. }
  797. func (s *Server) handleSaveBFDNeighbor(w http.ResponseWriter, r *http.Request) {
  798. var nbr dynamicrouting.BFDNeighbor
  799. if err := json.NewDecoder(r.Body).Decode(&nbr); err != nil {
  800. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON: " + err.Error()})
  801. return
  802. }
  803. res, err := s.dynRoutingMgr.SaveBFDNeighbor(nbr)
  804. if err != nil {
  805. writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
  806. return
  807. }
  808. writeJSON(w, http.StatusOK, res)
  809. }
  810. func (s *Server) handleDeleteBFDNeighbor(w http.ResponseWriter, r *http.Request) {
  811. id := r.PathValue("id")
  812. if id == "" {
  813. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Missing neighbor ID"})
  814. return
  815. }
  816. if err := s.dynRoutingMgr.DeleteBFDNeighbor(id); err != nil {
  817. writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
  818. return
  819. }
  820. writeJSON(w, http.StatusOK, map[string]string{"status": "deleted"})
  821. }
  822. func (s *Server) handleGetBFDSessions(w http.ResponseWriter, r *http.Request) {
  823. sessions := s.dynRoutingMgr.GetBFDSessions()
  824. if sessions == nil {
  825. sessions = []dynamicrouting.BFDSession{}
  826. }
  827. writeJSON(w, http.StatusOK, sessions)
  828. }
  829. // -------------------------------------------------------------
  830. // Babel Handlers (/api/routing/babel/*)
  831. // -------------------------------------------------------------
  832. func (s *Server) handleListBabelInstances(w http.ResponseWriter, r *http.Request) {
  833. insts := s.dynRoutingMgr.ListBabelInstances()
  834. if insts == nil {
  835. insts = []dynamicrouting.BabelInstance{}
  836. }
  837. writeJSON(w, http.StatusOK, insts)
  838. }
  839. func (s *Server) handleSaveBabelInstance(w http.ResponseWriter, r *http.Request) {
  840. var inst dynamicrouting.BabelInstance
  841. if err := json.NewDecoder(r.Body).Decode(&inst); err != nil {
  842. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON: " + err.Error()})
  843. return
  844. }
  845. res, err := s.dynRoutingMgr.SaveBabelInstance(inst)
  846. if err != nil {
  847. writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
  848. return
  849. }
  850. writeJSON(w, http.StatusOK, res)
  851. }
  852. func (s *Server) handleDeleteBabelInstance(w http.ResponseWriter, r *http.Request) {
  853. id := r.PathValue("id")
  854. if id == "" {
  855. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Missing instance ID"})
  856. return
  857. }
  858. if err := s.dynRoutingMgr.DeleteBabelInstance(id); err != nil {
  859. writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
  860. return
  861. }
  862. writeJSON(w, http.StatusOK, map[string]string{"status": "deleted"})
  863. }
  864. func (s *Server) handleSetBabelInstanceState(w http.ResponseWriter, r *http.Request) {
  865. id := r.PathValue("id")
  866. var req struct {
  867. Disabled bool `json:"disabled"`
  868. }
  869. if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
  870. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON"})
  871. return
  872. }
  873. if err := s.dynRoutingMgr.SetBabelInstanceState(id, req.Disabled); err != nil {
  874. writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
  875. return
  876. }
  877. writeJSON(w, http.StatusOK, map[string]interface{}{"status": "success", "disabled": req.Disabled})
  878. }
  879. func (s *Server) handleListBabelInterfaces(w http.ResponseWriter, r *http.Request) {
  880. ifaces := s.dynRoutingMgr.ListBabelInterfaces()
  881. if ifaces == nil {
  882. ifaces = []dynamicrouting.BabelInterface{}
  883. }
  884. writeJSON(w, http.StatusOK, ifaces)
  885. }
  886. func (s *Server) handleGetBabelInterfacesLive(w http.ResponseWriter, r *http.Request) {
  887. ifaces := s.dynRoutingMgr.GetBabelInterfacesLive()
  888. if ifaces == nil {
  889. ifaces = []dynamicrouting.BabelInterfaceLive{}
  890. }
  891. writeJSON(w, http.StatusOK, ifaces)
  892. }
  893. func (s *Server) handleSaveBabelInterface(w http.ResponseWriter, r *http.Request) {
  894. var iface dynamicrouting.BabelInterface
  895. if err := json.NewDecoder(r.Body).Decode(&iface); err != nil {
  896. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON: " + err.Error()})
  897. return
  898. }
  899. res, err := s.dynRoutingMgr.SaveBabelInterface(iface)
  900. if err != nil {
  901. writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
  902. return
  903. }
  904. writeJSON(w, http.StatusOK, res)
  905. }
  906. func (s *Server) handleDeleteBabelInterface(w http.ResponseWriter, r *http.Request) {
  907. id := r.PathValue("id")
  908. if id == "" {
  909. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Missing interface ID"})
  910. return
  911. }
  912. if err := s.dynRoutingMgr.DeleteBabelInterface(id); err != nil {
  913. writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
  914. return
  915. }
  916. writeJSON(w, http.StatusOK, map[string]string{"status": "deleted"})
  917. }
  918. func (s *Server) handleSetBabelInterfaceState(w http.ResponseWriter, r *http.Request) {
  919. id := r.PathValue("id")
  920. var req struct {
  921. Disabled bool `json:"disabled"`
  922. }
  923. if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
  924. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON"})
  925. return
  926. }
  927. if err := s.dynRoutingMgr.SetBabelInterfaceState(id, req.Disabled); err != nil {
  928. writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
  929. return
  930. }
  931. writeJSON(w, http.StatusOK, map[string]interface{}{"status": "success", "disabled": req.Disabled})
  932. }
  933. func (s *Server) handleGetBabelNeighbors(w http.ResponseWriter, r *http.Request) {
  934. nbrs := s.dynRoutingMgr.GetBabelNeighbors()
  935. if nbrs == nil {
  936. nbrs = []dynamicrouting.BabelNeighbor{}
  937. }
  938. writeJSON(w, http.StatusOK, nbrs)
  939. }
  940. func (s *Server) handleGetBabelRoutes(w http.ResponseWriter, r *http.Request) {
  941. rts := s.dynRoutingMgr.GetBabelRoutes()
  942. if rts == nil {
  943. rts = []dynamicrouting.BabelRoute{}
  944. }
  945. writeJSON(w, http.StatusOK, rts)
  946. }
  947. // -------------------------------------------------------------
  948. // RAdv Handlers (/api/routing/radv/*)
  949. // -------------------------------------------------------------
  950. func (s *Server) handleListRAdvInstances(w http.ResponseWriter, r *http.Request) {
  951. insts := s.dynRoutingMgr.ListRAdvInstances()
  952. if insts == nil {
  953. insts = []dynamicrouting.RAdvInstance{}
  954. }
  955. writeJSON(w, http.StatusOK, insts)
  956. }
  957. func (s *Server) handleSaveRAdvInstance(w http.ResponseWriter, r *http.Request) {
  958. var inst dynamicrouting.RAdvInstance
  959. if err := json.NewDecoder(r.Body).Decode(&inst); err != nil {
  960. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON: " + err.Error()})
  961. return
  962. }
  963. res, err := s.dynRoutingMgr.SaveRAdvInstance(inst)
  964. if err != nil {
  965. writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
  966. return
  967. }
  968. writeJSON(w, http.StatusOK, res)
  969. }
  970. func (s *Server) handleDeleteRAdvInstance(w http.ResponseWriter, r *http.Request) {
  971. id := r.PathValue("id")
  972. if id == "" {
  973. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Missing instance ID"})
  974. return
  975. }
  976. if err := s.dynRoutingMgr.DeleteRAdvInstance(id); err != nil {
  977. writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
  978. return
  979. }
  980. writeJSON(w, http.StatusOK, map[string]string{"status": "deleted"})
  981. }
  982. func (s *Server) handleSetRAdvInstanceState(w http.ResponseWriter, r *http.Request) {
  983. id := r.PathValue("id")
  984. var req struct {
  985. Disabled bool `json:"disabled"`
  986. }
  987. if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
  988. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON"})
  989. return
  990. }
  991. if err := s.dynRoutingMgr.SetRAdvInstanceState(id, req.Disabled); err != nil {
  992. writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
  993. return
  994. }
  995. writeJSON(w, http.StatusOK, map[string]interface{}{"status": "success", "disabled": req.Disabled})
  996. }
  997. func (s *Server) handleListRAdvInterfaces(w http.ResponseWriter, r *http.Request) {
  998. ifaces := s.dynRoutingMgr.ListRAdvInterfaces()
  999. if ifaces == nil {
  1000. ifaces = []dynamicrouting.RAdvInterface{}
  1001. }
  1002. writeJSON(w, http.StatusOK, ifaces)
  1003. }
  1004. func (s *Server) handleSaveRAdvInterface(w http.ResponseWriter, r *http.Request) {
  1005. var iface dynamicrouting.RAdvInterface
  1006. if err := json.NewDecoder(r.Body).Decode(&iface); err != nil {
  1007. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON: " + err.Error()})
  1008. return
  1009. }
  1010. res, err := s.dynRoutingMgr.SaveRAdvInterface(iface)
  1011. if err != nil {
  1012. writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
  1013. return
  1014. }
  1015. writeJSON(w, http.StatusOK, res)
  1016. }
  1017. func (s *Server) handleDeleteRAdvInterface(w http.ResponseWriter, r *http.Request) {
  1018. id := r.PathValue("id")
  1019. if id == "" {
  1020. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Missing interface ID"})
  1021. return
  1022. }
  1023. if err := s.dynRoutingMgr.DeleteRAdvInterface(id); err != nil {
  1024. writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
  1025. return
  1026. }
  1027. writeJSON(w, http.StatusOK, map[string]string{"status": "deleted"})
  1028. }
  1029. func (s *Server) handleSetRAdvInterfaceState(w http.ResponseWriter, r *http.Request) {
  1030. id := r.PathValue("id")
  1031. var req struct {
  1032. Disabled bool `json:"disabled"`
  1033. }
  1034. if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
  1035. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON"})
  1036. return
  1037. }
  1038. if err := s.dynRoutingMgr.SetRAdvInterfaceState(id, req.Disabled); err != nil {
  1039. writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
  1040. return
  1041. }
  1042. writeJSON(w, http.StatusOK, map[string]interface{}{"status": "success", "disabled": req.Disabled})
  1043. }