| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326 |
- package api
- import (
- "encoding/json"
- "io"
- "net/http"
- "strings"
- "debtik/internal/dynamicrouting"
- )
- // Summary
- func (s *Server) handleDynamicRoutingSummary(w http.ResponseWriter, r *http.Request) {
- summary := s.dynRoutingMgr.GetSummary()
- writeJSON(w, http.StatusOK, summary)
- }
- // -------------------------------------------------------------
- // BGP Handlers
- // -------------------------------------------------------------
- func (s *Server) handleGetBGPInstance(w http.ResponseWriter, r *http.Request) {
- inst := s.dynRoutingMgr.GetBGPInstance()
- writeJSON(w, http.StatusOK, inst)
- }
- func (s *Server) handleUpdateBGPInstance(w http.ResponseWriter, r *http.Request) {
- var inst dynamicrouting.BGPInstance
- if err := json.NewDecoder(r.Body).Decode(&inst); err != nil {
- writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON: " + err.Error()})
- return
- }
- if err := s.dynRoutingMgr.UpdateBGPInstance(inst); err != nil {
- writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
- return
- }
- writeJSON(w, http.StatusOK, s.dynRoutingMgr.GetBGPInstance())
- }
- func (s *Server) handleListBGPPeers(w http.ResponseWriter, r *http.Request) {
- peers := s.dynRoutingMgr.ListBGPPeers()
- if peers == nil {
- peers = []dynamicrouting.BGPPeer{}
- }
- writeJSON(w, http.StatusOK, peers)
- }
- func (s *Server) handleSaveBGPPeer(w http.ResponseWriter, r *http.Request) {
- var peer dynamicrouting.BGPPeer
- if err := json.NewDecoder(r.Body).Decode(&peer); err != nil {
- writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON: " + err.Error()})
- return
- }
- if peer.RemoteAddress == "" || peer.RemoteAS == 0 {
- writeJSON(w, http.StatusBadRequest, map[string]string{"error": "remote_address and remote_as are required"})
- return
- }
- if err := s.dynRoutingMgr.SaveBGPPeer(&peer); err != nil {
- writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
- return
- }
- writeJSON(w, http.StatusOK, map[string]interface{}{"status": "success", "peer": peer})
- }
- func (s *Server) handleDeleteBGPPeer(w http.ResponseWriter, r *http.Request) {
- id := r.PathValue("id")
- if id == "" {
- writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Missing peer id"})
- return
- }
- if err := s.dynRoutingMgr.DeleteBGPPeer(id); err != nil {
- writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
- return
- }
- writeJSON(w, http.StatusOK, map[string]string{"status": "deleted"})
- }
- func (s *Server) handleSetBGPPeerState(w http.ResponseWriter, r *http.Request) {
- id := r.PathValue("id")
- var req struct {
- Disabled bool `json:"disabled"`
- }
- if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
- writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON"})
- return
- }
- if err := s.dynRoutingMgr.SetBGPPeerState(id, req.Disabled); err != nil {
- writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
- return
- }
- writeJSON(w, http.StatusOK, map[string]interface{}{"status": "success", "disabled": req.Disabled})
- }
- func (s *Server) handleListBGPNetworks(w http.ResponseWriter, r *http.Request) {
- nets := s.dynRoutingMgr.ListBGPNetworks()
- if nets == nil {
- nets = []dynamicrouting.BGPNetwork{}
- }
- writeJSON(w, http.StatusOK, nets)
- }
- func (s *Server) handleSaveBGPNetwork(w http.ResponseWriter, r *http.Request) {
- var net dynamicrouting.BGPNetwork
- if err := json.NewDecoder(r.Body).Decode(&net); err != nil {
- writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON: " + err.Error()})
- return
- }
- if net.Network == "" {
- writeJSON(w, http.StatusBadRequest, map[string]string{"error": "network (CIDR) is required"})
- return
- }
- if err := s.dynRoutingMgr.SaveBGPNetwork(net); err != nil {
- writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
- return
- }
- writeJSON(w, http.StatusOK, map[string]interface{}{"status": "success", "network": net})
- }
- func (s *Server) handleDeleteBGPNetwork(w http.ResponseWriter, r *http.Request) {
- id := r.PathValue("id")
- if id == "" {
- writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Missing network id"})
- return
- }
- if err := s.dynRoutingMgr.DeleteBGPNetwork(id); err != nil {
- writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
- return
- }
- writeJSON(w, http.StatusOK, map[string]string{"status": "deleted"})
- }
- func (s *Server) handleGetBGPRoutes(w http.ResponseWriter, r *http.Request) {
- routes := s.dynRoutingMgr.GetBGPRoutes()
- if routes == nil {
- routes = []dynamicrouting.BGPRoute{}
- }
- writeJSON(w, http.StatusOK, routes)
- }
- // -------------------------------------------------------------
- // OSPF Handlers (RouterOS v7 Style OSPF)
- // -------------------------------------------------------------
- func (s *Server) handleListOSPFInstances(w http.ResponseWriter, r *http.Request) {
- insts := s.dynRoutingMgr.ListOSPFInstances()
- if insts == nil {
- insts = []dynamicrouting.OSPFInstance{}
- }
- writeJSON(w, http.StatusOK, insts)
- }
- func (s *Server) handleSaveOSPFInstance(w http.ResponseWriter, r *http.Request) {
- var inst dynamicrouting.OSPFInstance
- if err := json.NewDecoder(r.Body).Decode(&inst); err != nil {
- writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON: " + err.Error()})
- return
- }
- if err := s.dynRoutingMgr.SaveOSPFInstance(&inst); err != nil {
- writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
- return
- }
- writeJSON(w, http.StatusOK, map[string]interface{}{"status": "success", "instance": inst})
- }
- func (s *Server) handleDeleteOSPFInstance(w http.ResponseWriter, r *http.Request) {
- id := r.PathValue("id")
- if id == "" {
- writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Missing instance id"})
- return
- }
- if err := s.dynRoutingMgr.DeleteOSPFInstance(id); err != nil {
- writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
- return
- }
- writeJSON(w, http.StatusOK, map[string]string{"status": "deleted"})
- }
- func (s *Server) handleSetOSPFInstanceState(w http.ResponseWriter, r *http.Request) {
- id := r.PathValue("id")
- var req struct {
- Disabled bool `json:"disabled"`
- }
- if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
- writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON"})
- return
- }
- if err := s.dynRoutingMgr.SetOSPFInstanceState(id, req.Disabled); err != nil {
- writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
- return
- }
- writeJSON(w, http.StatusOK, map[string]interface{}{"status": "success", "disabled": req.Disabled})
- }
- func (s *Server) handleGetOSPFInstance(w http.ResponseWriter, r *http.Request) {
- inst := s.dynRoutingMgr.GetOSPFInstance()
- writeJSON(w, http.StatusOK, inst)
- }
- func (s *Server) handleUpdateOSPFInstance(w http.ResponseWriter, r *http.Request) {
- var inst dynamicrouting.OSPFInstance
- if err := json.NewDecoder(r.Body).Decode(&inst); err != nil {
- writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON: " + err.Error()})
- return
- }
- if err := s.dynRoutingMgr.UpdateOSPFInstance(inst); err != nil {
- writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
- return
- }
- writeJSON(w, http.StatusOK, s.dynRoutingMgr.GetOSPFInstance())
- }
- func (s *Server) handleListOSPFAreas(w http.ResponseWriter, r *http.Request) {
- areas := s.dynRoutingMgr.ListOSPFAreas()
- if areas == nil {
- areas = []dynamicrouting.OSPFArea{}
- }
- writeJSON(w, http.StatusOK, areas)
- }
- func (s *Server) handleSaveOSPFArea(w http.ResponseWriter, r *http.Request) {
- var area dynamicrouting.OSPFArea
- if err := json.NewDecoder(r.Body).Decode(&area); err != nil {
- writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON: " + err.Error()})
- return
- }
- if area.AreaID == "" {
- area.AreaID = "0.0.0.0"
- }
- if err := s.dynRoutingMgr.SaveOSPFArea(area); err != nil {
- writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
- return
- }
- writeJSON(w, http.StatusOK, map[string]interface{}{"status": "success", "area": area})
- }
- func (s *Server) handleDeleteOSPFArea(w http.ResponseWriter, r *http.Request) {
- id := r.PathValue("id")
- if id == "" {
- writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Missing area id"})
- return
- }
- if err := s.dynRoutingMgr.DeleteOSPFArea(id); err != nil {
- writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
- return
- }
- writeJSON(w, http.StatusOK, map[string]string{"status": "deleted"})
- }
- func (s *Server) handleSetOSPFAreaState(w http.ResponseWriter, r *http.Request) {
- id := r.PathValue("id")
- var req struct {
- Disabled bool `json:"disabled"`
- }
- if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
- writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON"})
- return
- }
- if err := s.dynRoutingMgr.SetOSPFAreaState(id, req.Disabled); err != nil {
- writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
- return
- }
- writeJSON(w, http.StatusOK, map[string]interface{}{"status": "success", "disabled": req.Disabled})
- }
- // Area Ranges
- func (s *Server) handleListOSPFAreaRanges(w http.ResponseWriter, r *http.Request) {
- ranges := s.dynRoutingMgr.ListOSPFAreaRanges()
- if ranges == nil {
- ranges = []dynamicrouting.OSPFAreaRange{}
- }
- writeJSON(w, http.StatusOK, ranges)
- }
- func (s *Server) handleSaveOSPFAreaRange(w http.ResponseWriter, r *http.Request) {
- var ar dynamicrouting.OSPFAreaRange
- if err := json.NewDecoder(r.Body).Decode(&ar); err != nil {
- writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON: " + err.Error()})
- return
- }
- if ar.Prefix == "" {
- writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Prefix is required"})
- return
- }
- if err := s.dynRoutingMgr.SaveOSPFAreaRange(ar); err != nil {
- writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
- return
- }
- writeJSON(w, http.StatusOK, map[string]interface{}{"status": "success", "area_range": ar})
- }
- func (s *Server) handleDeleteOSPFAreaRange(w http.ResponseWriter, r *http.Request) {
- id := r.PathValue("id")
- if id == "" {
- writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Missing range id"})
- return
- }
- if err := s.dynRoutingMgr.DeleteOSPFAreaRange(id); err != nil {
- writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
- return
- }
- writeJSON(w, http.StatusOK, map[string]string{"status": "deleted"})
- }
- // Interface Templates
- func (s *Server) handleListOSPFInterfaces(w http.ResponseWriter, r *http.Request) {
- ifaces := s.dynRoutingMgr.ListOSPFInterfaces()
- if ifaces == nil {
- ifaces = []dynamicrouting.OSPFInterface{}
- }
- writeJSON(w, http.StatusOK, ifaces)
- }
- func (s *Server) handleSaveOSPFInterface(w http.ResponseWriter, r *http.Request) {
- var iface dynamicrouting.OSPFInterface
- if err := json.NewDecoder(r.Body).Decode(&iface); err != nil {
- writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON: " + err.Error()})
- return
- }
- if err := s.dynRoutingMgr.SaveOSPFInterface(iface); err != nil {
- writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
- return
- }
- writeJSON(w, http.StatusOK, map[string]interface{}{"status": "success", "interface": iface})
- }
- func (s *Server) handleDeleteOSPFInterface(w http.ResponseWriter, r *http.Request) {
- id := r.PathValue("id")
- if id == "" {
- writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Missing interface id"})
- return
- }
- if err := s.dynRoutingMgr.DeleteOSPFInterface(id); err != nil {
- writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
- return
- }
- writeJSON(w, http.StatusOK, map[string]string{"status": "deleted"})
- }
- func (s *Server) handleSetOSPFInterfaceState(w http.ResponseWriter, r *http.Request) {
- id := r.PathValue("id")
- var req struct {
- Disabled bool `json:"disabled"`
- }
- if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
- writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON"})
- return
- }
- if err := s.dynRoutingMgr.SetOSPFInterfaceState(id, req.Disabled); err != nil {
- writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
- return
- }
- writeJSON(w, http.StatusOK, map[string]interface{}{"status": "success", "disabled": req.Disabled})
- }
- // Live Dynamic Interfaces
- func (s *Server) handleGetOSPFInterfacesLive(w http.ResponseWriter, r *http.Request) {
- live := s.dynRoutingMgr.GetOSPFInterfacesLive()
- if live == nil {
- live = []dynamicrouting.OSPFInterfaceLive{}
- }
- writeJSON(w, http.StatusOK, live)
- }
- // Static Neighbors
- func (s *Server) handleListOSPFStaticNeighbors(w http.ResponseWriter, r *http.Request) {
- nbrs := s.dynRoutingMgr.ListOSPFStaticNeighbors()
- if nbrs == nil {
- nbrs = []dynamicrouting.OSPFStaticNeighbor{}
- }
- writeJSON(w, http.StatusOK, nbrs)
- }
- func (s *Server) handleSaveOSPFStaticNeighbor(w http.ResponseWriter, r *http.Request) {
- var nbr dynamicrouting.OSPFStaticNeighbor
- if err := json.NewDecoder(r.Body).Decode(&nbr); err != nil {
- writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON: " + err.Error()})
- return
- }
- if nbr.Address == "" {
- writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Address is required"})
- return
- }
- if err := s.dynRoutingMgr.SaveOSPFStaticNeighbor(nbr); err != nil {
- writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
- return
- }
- writeJSON(w, http.StatusOK, map[string]interface{}{"status": "success", "neighbor": nbr})
- }
- func (s *Server) handleDeleteOSPFStaticNeighbor(w http.ResponseWriter, r *http.Request) {
- id := r.PathValue("id")
- if id == "" {
- writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Missing static neighbor id"})
- return
- }
- if err := s.dynRoutingMgr.DeleteOSPFStaticNeighbor(id); err != nil {
- writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
- return
- }
- writeJSON(w, http.StatusOK, map[string]string{"status": "deleted"})
- }
- func (s *Server) handleSetOSPFStaticNeighborState(w http.ResponseWriter, r *http.Request) {
- id := r.PathValue("id")
- if id == "" {
- writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Missing static neighbor id"})
- return
- }
- var req struct {
- Disabled bool `json:"disabled"`
- }
- if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
- writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON: " + err.Error()})
- return
- }
- if err := s.dynRoutingMgr.SetOSPFStaticNeighborState(id, req.Disabled); err != nil {
- writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
- return
- }
- writeJSON(w, http.StatusOK, map[string]interface{}{"status": "success", "disabled": req.Disabled})
- }
- // Live Neighbors
- func (s *Server) handleGetOSPFNeighbors(w http.ResponseWriter, r *http.Request) {
- nbrs := s.dynRoutingMgr.GetOSPFNeighbors()
- if nbrs == nil {
- nbrs = []dynamicrouting.OSPFNeighbor{}
- }
- writeJSON(w, http.StatusOK, nbrs)
- }
- // Live LSA Database
- func (s *Server) handleGetOSPFLSA(w http.ResponseWriter, r *http.Request) {
- lsa := s.dynRoutingMgr.GetOSPFLSA()
- if lsa == nil {
- lsa = []dynamicrouting.OSPFLSA{}
- }
- writeJSON(w, http.StatusOK, lsa)
- }
- func (s *Server) handleGetOSPFRoutes(w http.ResponseWriter, r *http.Request) {
- routes := s.dynRoutingMgr.GetOSPFRoutes()
- if routes == nil {
- routes = []dynamicrouting.OSPFRoute{}
- }
- writeJSON(w, http.StatusOK, routes)
- }
- // -------------------------------------------------------------
- // Routing Filters Handlers (/routing filter)
- // -------------------------------------------------------------
- func (s *Server) handleListRoutingFilters(w http.ResponseWriter, r *http.Request) {
- filters := s.dynRoutingMgr.ListRoutingFilters()
- if filters == nil {
- filters = []dynamicrouting.RoutingFilterRule{}
- }
- writeJSON(w, http.StatusOK, filters)
- }
- func (s *Server) handleSaveRoutingFilter(w http.ResponseWriter, r *http.Request) {
- var rule dynamicrouting.RoutingFilterRule
- if err := json.NewDecoder(r.Body).Decode(&rule); err != nil {
- writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON: " + err.Error()})
- return
- }
- if rule.Chain == "" {
- writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Chain name is required"})
- return
- }
- if rule.Action == "" && rule.Rule == "" {
- rule.Action = "accept"
- }
- if err := s.dynRoutingMgr.SaveRoutingFilter(&rule); err != nil {
- writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
- return
- }
- writeJSON(w, http.StatusOK, map[string]string{"status": "success", "id": rule.ID})
- }
- func (s *Server) handleDeleteRoutingFilter(w http.ResponseWriter, r *http.Request) {
- id := r.PathValue("id")
- if id == "" {
- writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Missing filter rule ID"})
- return
- }
- if err := s.dynRoutingMgr.DeleteRoutingFilter(id); err != nil {
- writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
- return
- }
- writeJSON(w, http.StatusOK, map[string]string{"status": "deleted"})
- }
- func (s *Server) handleSetRoutingFilterState(w http.ResponseWriter, r *http.Request) {
- id := r.PathValue("id")
- if id == "" {
- writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Missing filter rule ID"})
- return
- }
- var req struct {
- Disabled bool `json:"disabled"`
- }
- if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
- writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON"})
- return
- }
- if err := s.dynRoutingMgr.SetRoutingFilterState(id, req.Disabled); err != nil {
- writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
- return
- }
- writeJSON(w, http.StatusOK, map[string]interface{}{"status": "success", "disabled": req.Disabled})
- }
- func (s *Server) handleMoveRoutingFilter(w http.ResponseWriter, r *http.Request) {
- id := r.PathValue("id")
- if id == "" {
- writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Missing filter rule ID"})
- return
- }
- var req struct {
- Direction string `json:"direction"`
- }
- if err := json.NewDecoder(r.Body).Decode(&req); err != nil && err != io.EOF {
- writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON: " + err.Error()})
- return
- }
- dir := strings.ToLower(strings.TrimSpace(req.Direction))
- if dir != "up" && dir != "down" {
- dir = "up"
- }
- if err := s.dynRoutingMgr.MoveRoutingFilter(id, dir); err != nil {
- writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
- return
- }
- writeJSON(w, http.StatusOK, map[string]any{"status": "success", "id": id, "direction": dir})
- }
- // -------------------------------------------------------------
- // Community Lists Handlers (/routing filter community-list)
- // -------------------------------------------------------------
- func (s *Server) handleListCommunityLists(w http.ResponseWriter, r *http.Request) {
- lists := s.dynRoutingMgr.ListCommunityLists()
- if lists == nil {
- lists = []dynamicrouting.RoutingFilterCommunityList{}
- }
- writeJSON(w, http.StatusOK, lists)
- }
- func (s *Server) handleSaveCommunityList(w http.ResponseWriter, r *http.Request) {
- var cl dynamicrouting.RoutingFilterCommunityList
- if err := json.NewDecoder(r.Body).Decode(&cl); err != nil {
- writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON: " + err.Error()})
- return
- }
- if strings.TrimSpace(cl.Name) == "" {
- writeJSON(w, http.StatusBadRequest, map[string]string{"error": "List name is required"})
- return
- }
- if err := s.dynRoutingMgr.SaveCommunityList(&cl); err != nil {
- writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
- return
- }
- writeJSON(w, http.StatusOK, map[string]string{"status": "success", "id": cl.ID})
- }
- func (s *Server) handleDeleteCommunityList(w http.ResponseWriter, r *http.Request) {
- id := r.PathValue("id")
- if id == "" {
- writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Missing community list ID"})
- return
- }
- if err := s.dynRoutingMgr.DeleteCommunityList(id); err != nil {
- writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
- return
- }
- writeJSON(w, http.StatusOK, map[string]string{"status": "deleted"})
- }
- func (s *Server) handleSetCommunityListState(w http.ResponseWriter, r *http.Request) {
- id := r.PathValue("id")
- if id == "" {
- writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Missing community list ID"})
- return
- }
- var req struct {
- Disabled bool `json:"disabled"`
- }
- if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
- writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON"})
- return
- }
- if err := s.dynRoutingMgr.SetCommunityListState(id, req.Disabled); err != nil {
- writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
- return
- }
- writeJSON(w, http.StatusOK, map[string]interface{}{"status": "success", "disabled": req.Disabled})
- }
- // -------------------------------------------------------------
- // AS Lists Handlers (/routing filter num-list)
- // -------------------------------------------------------------
- func (s *Server) handleListASLists(w http.ResponseWriter, r *http.Request) {
- lists := s.dynRoutingMgr.ListASLists()
- if lists == nil {
- lists = []dynamicrouting.RoutingFilterASList{}
- }
- writeJSON(w, http.StatusOK, lists)
- }
- func (s *Server) handleSaveASList(w http.ResponseWriter, r *http.Request) {
- var al dynamicrouting.RoutingFilterASList
- if err := json.NewDecoder(r.Body).Decode(&al); err != nil {
- writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON: " + err.Error()})
- return
- }
- if strings.TrimSpace(al.Name) == "" {
- writeJSON(w, http.StatusBadRequest, map[string]string{"error": "List name is required"})
- return
- }
- if err := s.dynRoutingMgr.SaveASList(&al); err != nil {
- writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
- return
- }
- writeJSON(w, http.StatusOK, map[string]string{"status": "success", "id": al.ID})
- }
- func (s *Server) handleDeleteASList(w http.ResponseWriter, r *http.Request) {
- id := r.PathValue("id")
- if id == "" {
- writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Missing AS list ID"})
- return
- }
- if err := s.dynRoutingMgr.DeleteASList(id); err != nil {
- writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
- return
- }
- writeJSON(w, http.StatusOK, map[string]string{"status": "deleted"})
- }
- func (s *Server) handleSetASListState(w http.ResponseWriter, r *http.Request) {
- id := r.PathValue("id")
- if id == "" {
- writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Missing AS list ID"})
- return
- }
- var req struct {
- Disabled bool `json:"disabled"`
- }
- if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
- writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON"})
- return
- }
- if err := s.dynRoutingMgr.SetASListState(id, req.Disabled); err != nil {
- writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
- return
- }
- writeJSON(w, http.StatusOK, map[string]interface{}{"status": "success", "disabled": req.Disabled})
- }
- // -------------------------------------------------------------
- // RIP Handlers (/api/routing/rip/*)
- // -------------------------------------------------------------
- func (s *Server) handleListRIPInstances(w http.ResponseWriter, r *http.Request) {
- insts := s.dynRoutingMgr.ListRIPInstances()
- if insts == nil {
- insts = []dynamicrouting.RIPInstance{}
- }
- writeJSON(w, http.StatusOK, insts)
- }
- func (s *Server) handleSaveRIPInstance(w http.ResponseWriter, r *http.Request) {
- var inst dynamicrouting.RIPInstance
- if err := json.NewDecoder(r.Body).Decode(&inst); err != nil {
- writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON: " + err.Error()})
- return
- }
- res, err := s.dynRoutingMgr.SaveRIPInstance(inst)
- if err != nil {
- writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
- return
- }
- writeJSON(w, http.StatusOK, res)
- }
- func (s *Server) handleDeleteRIPInstance(w http.ResponseWriter, r *http.Request) {
- id := r.PathValue("id")
- if id == "" {
- writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Missing instance ID"})
- return
- }
- if err := s.dynRoutingMgr.DeleteRIPInstance(id); err != nil {
- writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
- return
- }
- writeJSON(w, http.StatusOK, map[string]string{"status": "deleted"})
- }
- func (s *Server) handleSetRIPInstanceState(w http.ResponseWriter, r *http.Request) {
- id := r.PathValue("id")
- var req struct {
- Disabled bool `json:"disabled"`
- }
- if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
- writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON"})
- return
- }
- if err := s.dynRoutingMgr.SetRIPInstanceState(id, req.Disabled); err != nil {
- writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
- return
- }
- writeJSON(w, http.StatusOK, map[string]interface{}{"status": "success", "disabled": req.Disabled})
- }
- func (s *Server) handleListRIPInterfaces(w http.ResponseWriter, r *http.Request) {
- ifaces := s.dynRoutingMgr.ListRIPInterfaces()
- if ifaces == nil {
- ifaces = []dynamicrouting.RIPInterface{}
- }
- writeJSON(w, http.StatusOK, ifaces)
- }
- func (s *Server) handleSaveRIPInterface(w http.ResponseWriter, r *http.Request) {
- var iface dynamicrouting.RIPInterface
- if err := json.NewDecoder(r.Body).Decode(&iface); err != nil {
- writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON: " + err.Error()})
- return
- }
- res, err := s.dynRoutingMgr.SaveRIPInterface(iface)
- if err != nil {
- writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
- return
- }
- writeJSON(w, http.StatusOK, res)
- }
- func (s *Server) handleDeleteRIPInterface(w http.ResponseWriter, r *http.Request) {
- id := r.PathValue("id")
- if id == "" {
- writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Missing interface ID"})
- return
- }
- if err := s.dynRoutingMgr.DeleteRIPInterface(id); err != nil {
- writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
- return
- }
- writeJSON(w, http.StatusOK, map[string]string{"status": "deleted"})
- }
- func (s *Server) handleSetRIPInterfaceState(w http.ResponseWriter, r *http.Request) {
- id := r.PathValue("id")
- var req struct {
- Disabled bool `json:"disabled"`
- }
- if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
- writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON"})
- return
- }
- if err := s.dynRoutingMgr.SetRIPInterfaceState(id, req.Disabled); err != nil {
- writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
- return
- }
- writeJSON(w, http.StatusOK, map[string]interface{}{"status": "success", "disabled": req.Disabled})
- }
- func (s *Server) handleListRIPNeighbors(w http.ResponseWriter, r *http.Request) {
- nbrs := s.dynRoutingMgr.ListRIPNeighbors()
- if nbrs == nil {
- nbrs = []dynamicrouting.RIPNeighbor{}
- }
- writeJSON(w, http.StatusOK, nbrs)
- }
- func (s *Server) handleSaveRIPNeighbor(w http.ResponseWriter, r *http.Request) {
- var nbr dynamicrouting.RIPNeighbor
- if err := json.NewDecoder(r.Body).Decode(&nbr); err != nil {
- writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON: " + err.Error()})
- return
- }
- res, err := s.dynRoutingMgr.SaveRIPNeighbor(nbr)
- if err != nil {
- writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
- return
- }
- writeJSON(w, http.StatusOK, res)
- }
- func (s *Server) handleDeleteRIPNeighbor(w http.ResponseWriter, r *http.Request) {
- id := r.PathValue("id")
- if id == "" {
- writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Missing neighbor ID"})
- return
- }
- if err := s.dynRoutingMgr.DeleteRIPNeighbor(id); err != nil {
- writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
- return
- }
- writeJSON(w, http.StatusOK, map[string]string{"status": "deleted"})
- }
- func (s *Server) handleGetRIPNeighborsLive(w http.ResponseWriter, r *http.Request) {
- nbrs := s.dynRoutingMgr.GetRIPNeighbors()
- if nbrs == nil {
- nbrs = []dynamicrouting.RIPNeighborLive{}
- }
- writeJSON(w, http.StatusOK, nbrs)
- }
- func (s *Server) handleGetRIPRoutes(w http.ResponseWriter, r *http.Request) {
- rts := s.dynRoutingMgr.GetRIPRoutes()
- if rts == nil {
- rts = []dynamicrouting.RIPRoute{}
- }
- writeJSON(w, http.StatusOK, rts)
- }
- // -------------------------------------------------------------
- // BFD Handlers (/api/routing/bfd/*)
- // -------------------------------------------------------------
- func (s *Server) handleGetBFDInstance(w http.ResponseWriter, r *http.Request) {
- inst := s.dynRoutingMgr.GetBFDInstance()
- writeJSON(w, http.StatusOK, inst)
- }
- func (s *Server) handleSaveBFDInstance(w http.ResponseWriter, r *http.Request) {
- var inst dynamicrouting.BFDInstance
- if err := json.NewDecoder(r.Body).Decode(&inst); err != nil {
- writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON: " + err.Error()})
- return
- }
- if err := s.dynRoutingMgr.SaveBFDInstance(inst); err != nil {
- writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
- return
- }
- writeJSON(w, http.StatusOK, s.dynRoutingMgr.GetBFDInstance())
- }
- func (s *Server) handleListBFDInterfaces(w http.ResponseWriter, r *http.Request) {
- ifaces := s.dynRoutingMgr.ListBFDInterfaces()
- if ifaces == nil {
- ifaces = []dynamicrouting.BFDInterface{}
- }
- writeJSON(w, http.StatusOK, ifaces)
- }
- func (s *Server) handleSaveBFDInterface(w http.ResponseWriter, r *http.Request) {
- var iface dynamicrouting.BFDInterface
- if err := json.NewDecoder(r.Body).Decode(&iface); err != nil {
- writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON: " + err.Error()})
- return
- }
- res, err := s.dynRoutingMgr.SaveBFDInterface(iface)
- if err != nil {
- writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
- return
- }
- writeJSON(w, http.StatusOK, res)
- }
- func (s *Server) handleDeleteBFDInterface(w http.ResponseWriter, r *http.Request) {
- id := r.PathValue("id")
- if id == "" {
- writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Missing interface ID"})
- return
- }
- if err := s.dynRoutingMgr.DeleteBFDInterface(id); err != nil {
- writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
- return
- }
- writeJSON(w, http.StatusOK, map[string]string{"status": "deleted"})
- }
- func (s *Server) handleSetBFDInterfaceState(w http.ResponseWriter, r *http.Request) {
- id := r.PathValue("id")
- var req struct {
- Disabled bool `json:"disabled"`
- }
- if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
- writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON"})
- return
- }
- if err := s.dynRoutingMgr.SetBFDInterfaceState(id, req.Disabled); err != nil {
- writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
- return
- }
- writeJSON(w, http.StatusOK, map[string]interface{}{"status": "success", "disabled": req.Disabled})
- }
- func (s *Server) handleListBFDNeighbors(w http.ResponseWriter, r *http.Request) {
- nbrs := s.dynRoutingMgr.ListBFDNeighbors()
- if nbrs == nil {
- nbrs = []dynamicrouting.BFDNeighbor{}
- }
- writeJSON(w, http.StatusOK, nbrs)
- }
- func (s *Server) handleSaveBFDNeighbor(w http.ResponseWriter, r *http.Request) {
- var nbr dynamicrouting.BFDNeighbor
- if err := json.NewDecoder(r.Body).Decode(&nbr); err != nil {
- writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON: " + err.Error()})
- return
- }
- res, err := s.dynRoutingMgr.SaveBFDNeighbor(nbr)
- if err != nil {
- writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
- return
- }
- writeJSON(w, http.StatusOK, res)
- }
- func (s *Server) handleDeleteBFDNeighbor(w http.ResponseWriter, r *http.Request) {
- id := r.PathValue("id")
- if id == "" {
- writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Missing neighbor ID"})
- return
- }
- if err := s.dynRoutingMgr.DeleteBFDNeighbor(id); err != nil {
- writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
- return
- }
- writeJSON(w, http.StatusOK, map[string]string{"status": "deleted"})
- }
- func (s *Server) handleGetBFDSessions(w http.ResponseWriter, r *http.Request) {
- sessions := s.dynRoutingMgr.GetBFDSessions()
- if sessions == nil {
- sessions = []dynamicrouting.BFDSession{}
- }
- writeJSON(w, http.StatusOK, sessions)
- }
- // -------------------------------------------------------------
- // Babel Handlers (/api/routing/babel/*)
- // -------------------------------------------------------------
- func (s *Server) handleListBabelInstances(w http.ResponseWriter, r *http.Request) {
- insts := s.dynRoutingMgr.ListBabelInstances()
- if insts == nil {
- insts = []dynamicrouting.BabelInstance{}
- }
- writeJSON(w, http.StatusOK, insts)
- }
- func (s *Server) handleSaveBabelInstance(w http.ResponseWriter, r *http.Request) {
- var inst dynamicrouting.BabelInstance
- if err := json.NewDecoder(r.Body).Decode(&inst); err != nil {
- writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON: " + err.Error()})
- return
- }
- res, err := s.dynRoutingMgr.SaveBabelInstance(inst)
- if err != nil {
- writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
- return
- }
- writeJSON(w, http.StatusOK, res)
- }
- func (s *Server) handleDeleteBabelInstance(w http.ResponseWriter, r *http.Request) {
- id := r.PathValue("id")
- if id == "" {
- writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Missing instance ID"})
- return
- }
- if err := s.dynRoutingMgr.DeleteBabelInstance(id); err != nil {
- writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
- return
- }
- writeJSON(w, http.StatusOK, map[string]string{"status": "deleted"})
- }
- func (s *Server) handleSetBabelInstanceState(w http.ResponseWriter, r *http.Request) {
- id := r.PathValue("id")
- var req struct {
- Disabled bool `json:"disabled"`
- }
- if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
- writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON"})
- return
- }
- if err := s.dynRoutingMgr.SetBabelInstanceState(id, req.Disabled); err != nil {
- writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
- return
- }
- writeJSON(w, http.StatusOK, map[string]interface{}{"status": "success", "disabled": req.Disabled})
- }
- func (s *Server) handleListBabelInterfaces(w http.ResponseWriter, r *http.Request) {
- ifaces := s.dynRoutingMgr.ListBabelInterfaces()
- if ifaces == nil {
- ifaces = []dynamicrouting.BabelInterface{}
- }
- writeJSON(w, http.StatusOK, ifaces)
- }
- func (s *Server) handleGetBabelInterfacesLive(w http.ResponseWriter, r *http.Request) {
- ifaces := s.dynRoutingMgr.GetBabelInterfacesLive()
- if ifaces == nil {
- ifaces = []dynamicrouting.BabelInterfaceLive{}
- }
- writeJSON(w, http.StatusOK, ifaces)
- }
- func (s *Server) handleSaveBabelInterface(w http.ResponseWriter, r *http.Request) {
- var iface dynamicrouting.BabelInterface
- if err := json.NewDecoder(r.Body).Decode(&iface); err != nil {
- writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON: " + err.Error()})
- return
- }
- res, err := s.dynRoutingMgr.SaveBabelInterface(iface)
- if err != nil {
- writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
- return
- }
- writeJSON(w, http.StatusOK, res)
- }
- func (s *Server) handleDeleteBabelInterface(w http.ResponseWriter, r *http.Request) {
- id := r.PathValue("id")
- if id == "" {
- writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Missing interface ID"})
- return
- }
- if err := s.dynRoutingMgr.DeleteBabelInterface(id); err != nil {
- writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
- return
- }
- writeJSON(w, http.StatusOK, map[string]string{"status": "deleted"})
- }
- func (s *Server) handleSetBabelInterfaceState(w http.ResponseWriter, r *http.Request) {
- id := r.PathValue("id")
- var req struct {
- Disabled bool `json:"disabled"`
- }
- if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
- writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON"})
- return
- }
- if err := s.dynRoutingMgr.SetBabelInterfaceState(id, req.Disabled); err != nil {
- writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
- return
- }
- writeJSON(w, http.StatusOK, map[string]interface{}{"status": "success", "disabled": req.Disabled})
- }
- func (s *Server) handleGetBabelNeighbors(w http.ResponseWriter, r *http.Request) {
- nbrs := s.dynRoutingMgr.GetBabelNeighbors()
- if nbrs == nil {
- nbrs = []dynamicrouting.BabelNeighbor{}
- }
- writeJSON(w, http.StatusOK, nbrs)
- }
- func (s *Server) handleGetBabelRoutes(w http.ResponseWriter, r *http.Request) {
- rts := s.dynRoutingMgr.GetBabelRoutes()
- if rts == nil {
- rts = []dynamicrouting.BabelRoute{}
- }
- writeJSON(w, http.StatusOK, rts)
- }
- // -------------------------------------------------------------
- // RAdv Handlers (/api/routing/radv/*)
- // -------------------------------------------------------------
- func (s *Server) handleListRAdvInstances(w http.ResponseWriter, r *http.Request) {
- insts := s.dynRoutingMgr.ListRAdvInstances()
- if insts == nil {
- insts = []dynamicrouting.RAdvInstance{}
- }
- writeJSON(w, http.StatusOK, insts)
- }
- func (s *Server) handleSaveRAdvInstance(w http.ResponseWriter, r *http.Request) {
- var inst dynamicrouting.RAdvInstance
- if err := json.NewDecoder(r.Body).Decode(&inst); err != nil {
- writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON: " + err.Error()})
- return
- }
- res, err := s.dynRoutingMgr.SaveRAdvInstance(inst)
- if err != nil {
- writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
- return
- }
- writeJSON(w, http.StatusOK, res)
- }
- func (s *Server) handleDeleteRAdvInstance(w http.ResponseWriter, r *http.Request) {
- id := r.PathValue("id")
- if id == "" {
- writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Missing instance ID"})
- return
- }
- if err := s.dynRoutingMgr.DeleteRAdvInstance(id); err != nil {
- writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
- return
- }
- writeJSON(w, http.StatusOK, map[string]string{"status": "deleted"})
- }
- func (s *Server) handleSetRAdvInstanceState(w http.ResponseWriter, r *http.Request) {
- id := r.PathValue("id")
- var req struct {
- Disabled bool `json:"disabled"`
- }
- if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
- writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON"})
- return
- }
- if err := s.dynRoutingMgr.SetRAdvInstanceState(id, req.Disabled); err != nil {
- writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
- return
- }
- writeJSON(w, http.StatusOK, map[string]interface{}{"status": "success", "disabled": req.Disabled})
- }
- func (s *Server) handleListRAdvInterfaces(w http.ResponseWriter, r *http.Request) {
- ifaces := s.dynRoutingMgr.ListRAdvInterfaces()
- if ifaces == nil {
- ifaces = []dynamicrouting.RAdvInterface{}
- }
- writeJSON(w, http.StatusOK, ifaces)
- }
- func (s *Server) handleSaveRAdvInterface(w http.ResponseWriter, r *http.Request) {
- var iface dynamicrouting.RAdvInterface
- if err := json.NewDecoder(r.Body).Decode(&iface); err != nil {
- writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON: " + err.Error()})
- return
- }
- res, err := s.dynRoutingMgr.SaveRAdvInterface(iface)
- if err != nil {
- writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
- return
- }
- writeJSON(w, http.StatusOK, res)
- }
- func (s *Server) handleDeleteRAdvInterface(w http.ResponseWriter, r *http.Request) {
- id := r.PathValue("id")
- if id == "" {
- writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Missing interface ID"})
- return
- }
- if err := s.dynRoutingMgr.DeleteRAdvInterface(id); err != nil {
- writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
- return
- }
- writeJSON(w, http.StatusOK, map[string]string{"status": "deleted"})
- }
- func (s *Server) handleSetRAdvInterfaceState(w http.ResponseWriter, r *http.Request) {
- id := r.PathValue("id")
- var req struct {
- Disabled bool `json:"disabled"`
- }
- if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
- writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON"})
- return
- }
- if err := s.dynRoutingMgr.SetRAdvInterfaceState(id, req.Disabled); err != nil {
- writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
- return
- }
- writeJSON(w, http.StatusOK, map[string]interface{}{"status": "success", "disabled": req.Disabled})
- }
- // ==========================================
- // EIGRP Handlers (/api/routing/eigrp/...)
- // ==========================================
- func (s *Server) handleListEIGRPInstances(w http.ResponseWriter, r *http.Request) {
- insts := s.dynRoutingMgr.ListEIGRPInstances()
- if insts == nil {
- insts = []dynamicrouting.EIGRPInstance{}
- }
- writeJSON(w, http.StatusOK, insts)
- }
- func (s *Server) handleSaveEIGRPInstance(w http.ResponseWriter, r *http.Request) {
- var inst dynamicrouting.EIGRPInstance
- if err := json.NewDecoder(r.Body).Decode(&inst); err != nil {
- writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON: " + err.Error()})
- return
- }
- res, err := s.dynRoutingMgr.SaveEIGRPInstance(inst)
- if err != nil {
- writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
- return
- }
- writeJSON(w, http.StatusOK, res)
- }
- func (s *Server) handleDeleteEIGRPInstance(w http.ResponseWriter, r *http.Request) {
- id := r.PathValue("id")
- if id == "" {
- writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Missing instance ID"})
- return
- }
- if err := s.dynRoutingMgr.DeleteEIGRPInstance(id); err != nil {
- writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
- return
- }
- writeJSON(w, http.StatusOK, map[string]string{"status": "deleted"})
- }
- func (s *Server) handleSetEIGRPInstanceState(w http.ResponseWriter, r *http.Request) {
- id := r.PathValue("id")
- var req struct {
- Disabled bool `json:"disabled"`
- }
- if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
- writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON"})
- return
- }
- if err := s.dynRoutingMgr.SetEIGRPInstanceState(id, req.Disabled); err != nil {
- writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
- return
- }
- writeJSON(w, http.StatusOK, map[string]interface{}{"status": "success", "disabled": req.Disabled})
- }
- func (s *Server) handleListEIGRPInterfaces(w http.ResponseWriter, r *http.Request) {
- ifaces := s.dynRoutingMgr.ListEIGRPInterfaces()
- if ifaces == nil {
- ifaces = []dynamicrouting.EIGRPInterface{}
- }
- writeJSON(w, http.StatusOK, ifaces)
- }
- func (s *Server) handleSaveEIGRPInterface(w http.ResponseWriter, r *http.Request) {
- var iface dynamicrouting.EIGRPInterface
- if err := json.NewDecoder(r.Body).Decode(&iface); err != nil {
- writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON: " + err.Error()})
- return
- }
- res, err := s.dynRoutingMgr.SaveEIGRPInterface(iface)
- if err != nil {
- writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
- return
- }
- writeJSON(w, http.StatusOK, res)
- }
- func (s *Server) handleDeleteEIGRPInterface(w http.ResponseWriter, r *http.Request) {
- id := r.PathValue("id")
- if id == "" {
- writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Missing interface ID"})
- return
- }
- if err := s.dynRoutingMgr.DeleteEIGRPInterface(id); err != nil {
- writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
- return
- }
- writeJSON(w, http.StatusOK, map[string]string{"status": "deleted"})
- }
- func (s *Server) handleSetEIGRPInterfaceState(w http.ResponseWriter, r *http.Request) {
- id := r.PathValue("id")
- var req struct {
- Disabled bool `json:"disabled"`
- }
- if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
- writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON"})
- return
- }
- if err := s.dynRoutingMgr.SetEIGRPInterfaceState(id, req.Disabled); err != nil {
- writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
- return
- }
- writeJSON(w, http.StatusOK, map[string]interface{}{"status": "success", "disabled": req.Disabled})
- }
- func (s *Server) handleListEIGRPNetworks(w http.ResponseWriter, r *http.Request) {
- nets := s.dynRoutingMgr.ListEIGRPNetworks()
- if nets == nil {
- nets = []dynamicrouting.EIGRPNetwork{}
- }
- writeJSON(w, http.StatusOK, nets)
- }
- func (s *Server) handleSaveEIGRPNetwork(w http.ResponseWriter, r *http.Request) {
- var net dynamicrouting.EIGRPNetwork
- if err := json.NewDecoder(r.Body).Decode(&net); err != nil {
- writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON: " + err.Error()})
- return
- }
- res, err := s.dynRoutingMgr.SaveEIGRPNetwork(net)
- if err != nil {
- writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
- return
- }
- writeJSON(w, http.StatusOK, res)
- }
- func (s *Server) handleDeleteEIGRPNetwork(w http.ResponseWriter, r *http.Request) {
- id := r.PathValue("id")
- if id == "" {
- writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Missing network ID"})
- return
- }
- if err := s.dynRoutingMgr.DeleteEIGRPNetwork(id); err != nil {
- writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
- return
- }
- writeJSON(w, http.StatusOK, map[string]string{"status": "deleted"})
- }
- func (s *Server) handleListEIGRPNeighbors(w http.ResponseWriter, r *http.Request) {
- nbrs := s.dynRoutingMgr.ListEIGRPNeighborsLive()
- if nbrs == nil {
- nbrs = []dynamicrouting.EIGRPNeighborLive{}
- }
- writeJSON(w, http.StatusOK, nbrs)
- }
- func (s *Server) handleListEIGRPTopology(w http.ResponseWriter, r *http.Request) {
- routes := s.dynRoutingMgr.ListEIGRPTopologyLive()
- if routes == nil {
- routes = []dynamicrouting.EIGRPRouteLive{}
- }
- writeJSON(w, http.StatusOK, routes)
- }
|