dynamic_routing_handlers.go 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136
  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) handleSaveBabelInterface(w http.ResponseWriter, r *http.Request) {
  887. var iface dynamicrouting.BabelInterface
  888. if err := json.NewDecoder(r.Body).Decode(&iface); err != nil {
  889. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON: " + err.Error()})
  890. return
  891. }
  892. res, err := s.dynRoutingMgr.SaveBabelInterface(iface)
  893. if err != nil {
  894. writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
  895. return
  896. }
  897. writeJSON(w, http.StatusOK, res)
  898. }
  899. func (s *Server) handleDeleteBabelInterface(w http.ResponseWriter, r *http.Request) {
  900. id := r.PathValue("id")
  901. if id == "" {
  902. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Missing interface ID"})
  903. return
  904. }
  905. if err := s.dynRoutingMgr.DeleteBabelInterface(id); err != nil {
  906. writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
  907. return
  908. }
  909. writeJSON(w, http.StatusOK, map[string]string{"status": "deleted"})
  910. }
  911. func (s *Server) handleSetBabelInterfaceState(w http.ResponseWriter, r *http.Request) {
  912. id := r.PathValue("id")
  913. var req struct {
  914. Disabled bool `json:"disabled"`
  915. }
  916. if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
  917. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON"})
  918. return
  919. }
  920. if err := s.dynRoutingMgr.SetBabelInterfaceState(id, req.Disabled); err != nil {
  921. writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
  922. return
  923. }
  924. writeJSON(w, http.StatusOK, map[string]interface{}{"status": "success", "disabled": req.Disabled})
  925. }
  926. func (s *Server) handleGetBabelNeighbors(w http.ResponseWriter, r *http.Request) {
  927. nbrs := s.dynRoutingMgr.GetBabelNeighbors()
  928. if nbrs == nil {
  929. nbrs = []dynamicrouting.BabelNeighbor{}
  930. }
  931. writeJSON(w, http.StatusOK, nbrs)
  932. }
  933. func (s *Server) handleGetBabelRoutes(w http.ResponseWriter, r *http.Request) {
  934. rts := s.dynRoutingMgr.GetBabelRoutes()
  935. if rts == nil {
  936. rts = []dynamicrouting.BabelRoute{}
  937. }
  938. writeJSON(w, http.StatusOK, rts)
  939. }
  940. // -------------------------------------------------------------
  941. // RAdv Handlers (/api/routing/radv/*)
  942. // -------------------------------------------------------------
  943. func (s *Server) handleListRAdvInstances(w http.ResponseWriter, r *http.Request) {
  944. insts := s.dynRoutingMgr.ListRAdvInstances()
  945. if insts == nil {
  946. insts = []dynamicrouting.RAdvInstance{}
  947. }
  948. writeJSON(w, http.StatusOK, insts)
  949. }
  950. func (s *Server) handleSaveRAdvInstance(w http.ResponseWriter, r *http.Request) {
  951. var inst dynamicrouting.RAdvInstance
  952. if err := json.NewDecoder(r.Body).Decode(&inst); err != nil {
  953. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON: " + err.Error()})
  954. return
  955. }
  956. res, err := s.dynRoutingMgr.SaveRAdvInstance(inst)
  957. if err != nil {
  958. writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
  959. return
  960. }
  961. writeJSON(w, http.StatusOK, res)
  962. }
  963. func (s *Server) handleDeleteRAdvInstance(w http.ResponseWriter, r *http.Request) {
  964. id := r.PathValue("id")
  965. if id == "" {
  966. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Missing instance ID"})
  967. return
  968. }
  969. if err := s.dynRoutingMgr.DeleteRAdvInstance(id); err != nil {
  970. writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
  971. return
  972. }
  973. writeJSON(w, http.StatusOK, map[string]string{"status": "deleted"})
  974. }
  975. func (s *Server) handleSetRAdvInstanceState(w http.ResponseWriter, r *http.Request) {
  976. id := r.PathValue("id")
  977. var req struct {
  978. Disabled bool `json:"disabled"`
  979. }
  980. if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
  981. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON"})
  982. return
  983. }
  984. if err := s.dynRoutingMgr.SetRAdvInstanceState(id, req.Disabled); err != nil {
  985. writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
  986. return
  987. }
  988. writeJSON(w, http.StatusOK, map[string]interface{}{"status": "success", "disabled": req.Disabled})
  989. }
  990. func (s *Server) handleListRAdvInterfaces(w http.ResponseWriter, r *http.Request) {
  991. ifaces := s.dynRoutingMgr.ListRAdvInterfaces()
  992. if ifaces == nil {
  993. ifaces = []dynamicrouting.RAdvInterface{}
  994. }
  995. writeJSON(w, http.StatusOK, ifaces)
  996. }
  997. func (s *Server) handleSaveRAdvInterface(w http.ResponseWriter, r *http.Request) {
  998. var iface dynamicrouting.RAdvInterface
  999. if err := json.NewDecoder(r.Body).Decode(&iface); err != nil {
  1000. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON: " + err.Error()})
  1001. return
  1002. }
  1003. res, err := s.dynRoutingMgr.SaveRAdvInterface(iface)
  1004. if err != nil {
  1005. writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
  1006. return
  1007. }
  1008. writeJSON(w, http.StatusOK, res)
  1009. }
  1010. func (s *Server) handleDeleteRAdvInterface(w http.ResponseWriter, r *http.Request) {
  1011. id := r.PathValue("id")
  1012. if id == "" {
  1013. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Missing interface ID"})
  1014. return
  1015. }
  1016. if err := s.dynRoutingMgr.DeleteRAdvInterface(id); err != nil {
  1017. writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
  1018. return
  1019. }
  1020. writeJSON(w, http.StatusOK, map[string]string{"status": "deleted"})
  1021. }
  1022. func (s *Server) handleSetRAdvInterfaceState(w http.ResponseWriter, r *http.Request) {
  1023. id := r.PathValue("id")
  1024. var req struct {
  1025. Disabled bool `json:"disabled"`
  1026. }
  1027. if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
  1028. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON"})
  1029. return
  1030. }
  1031. if err := s.dynRoutingMgr.SetRAdvInterfaceState(id, req.Disabled); err != nil {
  1032. writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
  1033. return
  1034. }
  1035. writeJSON(w, http.StatusOK, map[string]interface{}{"status": "success", "disabled": req.Disabled})
  1036. }