2021-10-22 22:40:03 +02:00
|
|
|
package http
|
2018-09-22 15:51:20 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2018-10-23 19:06:24 +02:00
|
|
|
"time"
|
2021-03-22 16:50:08 +01:00
|
|
|
|
|
|
|
"github.com/julienschmidt/httprouter"
|
|
|
|
|
|
|
|
"github.com/alice-lg/alice-lg/pkg/api"
|
2018-09-22 15:51:20 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Handle routes
|
2021-10-26 23:11:42 +02:00
|
|
|
func (s *Server) apiRoutesList(
|
|
|
|
_req *http.Request,
|
|
|
|
params httprouter.Params,
|
|
|
|
) (api.Response, error) {
|
2021-07-02 14:30:43 +02:00
|
|
|
rsID, err := validateSourceID(params.ByName("id"))
|
2018-09-22 15:51:20 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-07-02 14:30:43 +02:00
|
|
|
neighborID := params.ByName("neighborId")
|
2019-01-11 20:13:31 +01:00
|
|
|
|
2021-10-26 23:11:42 +02:00
|
|
|
source := s.cfg.SourceInstanceByID(rsID)
|
2019-01-11 20:13:31 +01:00
|
|
|
if source == nil {
|
2021-10-27 14:48:52 +00:00
|
|
|
return nil, ErrSourceNotFound
|
2019-01-11 20:13:31 +01:00
|
|
|
}
|
|
|
|
|
2021-07-02 14:30:43 +02:00
|
|
|
result, err := source.Routes(neighborID)
|
2018-10-02 14:52:47 +02:00
|
|
|
if err != nil {
|
2021-07-02 14:30:43 +02:00
|
|
|
apiLogSourceError("routes", rsID, neighborID, err)
|
2018-10-02 14:52:47 +02:00
|
|
|
}
|
|
|
|
|
2018-09-22 15:51:20 +02:00
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Paginated Routes Respponse: Received routes
|
2021-10-26 23:11:42 +02:00
|
|
|
func (s *Server) apiRoutesListReceived(
|
2018-09-22 15:51:20 +02:00
|
|
|
req *http.Request,
|
|
|
|
params httprouter.Params,
|
|
|
|
) (api.Response, error) {
|
2018-10-23 19:06:24 +02:00
|
|
|
// Measure response time
|
|
|
|
t0 := time.Now()
|
|
|
|
|
2021-07-02 14:30:43 +02:00
|
|
|
rsID, err := validateSourceID(params.ByName("id"))
|
2018-09-22 15:51:20 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-07-02 14:30:43 +02:00
|
|
|
neighborID := params.ByName("neighborId")
|
2021-10-26 23:11:42 +02:00
|
|
|
source := s.cfg.SourceInstanceByID(rsID)
|
2019-01-11 20:13:31 +01:00
|
|
|
if source == nil {
|
2021-10-26 23:11:42 +02:00
|
|
|
return nil, ErrSourceNotFound
|
2019-01-11 20:13:31 +01:00
|
|
|
}
|
|
|
|
|
2021-07-02 14:30:43 +02:00
|
|
|
result, err := source.RoutesReceived(neighborID)
|
2018-09-22 15:51:20 +02:00
|
|
|
if err != nil {
|
2021-07-02 14:30:43 +02:00
|
|
|
apiLogSourceError("routes_received", rsID, neighborID, err)
|
2018-09-22 15:51:20 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Filter routes based on criteria if present
|
2018-10-23 19:06:24 +02:00
|
|
|
allRoutes := apiQueryFilterNextHopGateway(req, "q", result.Imported)
|
|
|
|
routes := api.Routes{}
|
|
|
|
|
|
|
|
// Apply other (commmunity) filters
|
2018-10-28 18:17:10 +01:00
|
|
|
filtersApplied, err := api.FiltersFromQuery(req.URL.Query())
|
2018-10-23 19:06:24 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
filtersAvailable := api.NewSearchFilters()
|
|
|
|
for _, r := range allRoutes {
|
2018-10-28 18:17:10 +01:00
|
|
|
if !filtersApplied.MatchRoute(r) {
|
2018-10-23 19:06:24 +02:00
|
|
|
continue // Exclude route from results set
|
|
|
|
}
|
|
|
|
routes = append(routes, r)
|
|
|
|
filtersAvailable.UpdateFromRoute(r)
|
|
|
|
}
|
2018-09-22 15:51:20 +02:00
|
|
|
|
2018-10-25 22:22:32 +02:00
|
|
|
// Remove applied filters from available
|
2018-10-28 18:17:10 +01:00
|
|
|
filtersApplied.MergeProperties(filtersAvailable)
|
|
|
|
filtersAvailable = filtersAvailable.Sub(filtersApplied)
|
2018-10-25 22:22:32 +02:00
|
|
|
|
2018-09-22 15:51:20 +02:00
|
|
|
// Paginate results
|
|
|
|
page := apiQueryMustInt(req, "page", 0)
|
2021-10-27 14:48:52 +00:00
|
|
|
pageSize := s.cfg.UI.Pagination.RoutesAcceptedPageSize
|
2018-09-22 15:51:20 +02:00
|
|
|
routes, pagination := apiPaginateRoutes(routes, page, pageSize)
|
|
|
|
|
2018-10-23 19:06:24 +02:00
|
|
|
// Calculate query duration
|
|
|
|
queryDuration := time.Since(t0)
|
|
|
|
|
2018-09-22 15:51:20 +02:00
|
|
|
// Make paginated response
|
|
|
|
response := api.PaginatedRoutesResponse{
|
|
|
|
RoutesResponse: &api.RoutesResponse{
|
|
|
|
Api: result.Api,
|
|
|
|
Imported: routes,
|
|
|
|
},
|
2018-10-23 19:06:24 +02:00
|
|
|
TimedResponse: api.TimedResponse{
|
|
|
|
RequestDuration: DurationMs(queryDuration),
|
|
|
|
},
|
|
|
|
FilterableResponse: api.FilterableResponse{
|
|
|
|
FiltersAvailable: filtersAvailable,
|
2018-10-28 18:17:10 +01:00
|
|
|
FiltersApplied: filtersApplied,
|
2018-10-23 19:06:24 +02:00
|
|
|
},
|
2018-09-22 15:51:20 +02:00
|
|
|
Pagination: pagination,
|
|
|
|
}
|
|
|
|
|
|
|
|
return response, nil
|
|
|
|
}
|
|
|
|
|
2021-10-26 23:11:42 +02:00
|
|
|
func (s *Server) apiRoutesListFiltered(
|
2018-09-22 15:51:20 +02:00
|
|
|
req *http.Request,
|
|
|
|
params httprouter.Params,
|
|
|
|
) (api.Response, error) {
|
2018-10-23 19:06:24 +02:00
|
|
|
t0 := time.Now()
|
|
|
|
|
2021-07-02 14:30:43 +02:00
|
|
|
rsID, err := validateSourceID(params.ByName("id"))
|
2018-09-22 15:51:20 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-07-02 14:30:43 +02:00
|
|
|
neighborID := params.ByName("neighborId")
|
2021-10-26 23:11:42 +02:00
|
|
|
source := s.cfg.SourceInstanceByID(rsID)
|
2019-01-11 20:13:31 +01:00
|
|
|
if source == nil {
|
2021-10-26 23:11:42 +02:00
|
|
|
return nil, ErrSourceNotFound
|
2019-01-11 20:13:31 +01:00
|
|
|
}
|
|
|
|
|
2021-07-02 14:30:43 +02:00
|
|
|
result, err := source.RoutesFiltered(neighborID)
|
2018-09-22 15:51:20 +02:00
|
|
|
if err != nil {
|
2021-07-02 14:30:43 +02:00
|
|
|
apiLogSourceError("routes_filtered", rsID, neighborID, err)
|
2018-09-22 15:51:20 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Filter routes based on criteria if present
|
2018-10-23 19:06:24 +02:00
|
|
|
allRoutes := apiQueryFilterNextHopGateway(req, "q", result.Filtered)
|
|
|
|
routes := api.Routes{}
|
|
|
|
|
|
|
|
// Apply other (commmunity) filters
|
2018-10-28 18:17:10 +01:00
|
|
|
filtersApplied, err := api.FiltersFromQuery(req.URL.Query())
|
2018-10-23 19:06:24 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
filtersAvailable := api.NewSearchFilters()
|
|
|
|
for _, r := range allRoutes {
|
2018-10-28 18:17:10 +01:00
|
|
|
if !filtersApplied.MatchRoute(r) {
|
2018-10-23 19:06:24 +02:00
|
|
|
continue // Exclude route from results set
|
|
|
|
}
|
|
|
|
routes = append(routes, r)
|
|
|
|
filtersAvailable.UpdateFromRoute(r)
|
|
|
|
}
|
2018-09-22 15:51:20 +02:00
|
|
|
|
2018-10-25 22:22:32 +02:00
|
|
|
// Remove applied filters from available
|
2018-10-28 18:17:10 +01:00
|
|
|
filtersApplied.MergeProperties(filtersAvailable)
|
|
|
|
filtersAvailable = filtersAvailable.Sub(filtersApplied)
|
2018-10-25 22:22:32 +02:00
|
|
|
|
2018-09-22 15:51:20 +02:00
|
|
|
// Paginate results
|
|
|
|
page := apiQueryMustInt(req, "page", 0)
|
2021-10-27 14:48:52 +00:00
|
|
|
pageSize := s.cfg.UI.Pagination.RoutesFilteredPageSize
|
2018-09-22 15:51:20 +02:00
|
|
|
routes, pagination := apiPaginateRoutes(routes, page, pageSize)
|
|
|
|
|
2018-10-23 19:06:24 +02:00
|
|
|
// Calculate query duration
|
|
|
|
queryDuration := time.Since(t0)
|
|
|
|
|
2018-09-22 15:51:20 +02:00
|
|
|
// Make response
|
|
|
|
response := api.PaginatedRoutesResponse{
|
|
|
|
RoutesResponse: &api.RoutesResponse{
|
|
|
|
Api: result.Api,
|
|
|
|
Filtered: routes,
|
|
|
|
},
|
2018-10-23 19:06:24 +02:00
|
|
|
TimedResponse: api.TimedResponse{
|
|
|
|
RequestDuration: DurationMs(queryDuration),
|
|
|
|
},
|
|
|
|
FilterableResponse: api.FilterableResponse{
|
|
|
|
FiltersAvailable: filtersAvailable,
|
2018-10-28 18:17:10 +01:00
|
|
|
FiltersApplied: filtersApplied,
|
2018-10-23 19:06:24 +02:00
|
|
|
},
|
2018-09-22 15:51:20 +02:00
|
|
|
Pagination: pagination,
|
|
|
|
}
|
|
|
|
|
|
|
|
return response, nil
|
|
|
|
}
|
|
|
|
|
2021-10-26 23:11:42 +02:00
|
|
|
func (s *Server) apiRoutesListNotExported(
|
2018-09-22 15:51:20 +02:00
|
|
|
req *http.Request,
|
|
|
|
params httprouter.Params,
|
|
|
|
) (api.Response, error) {
|
2018-10-23 19:06:24 +02:00
|
|
|
t0 := time.Now()
|
|
|
|
|
2021-07-02 14:30:43 +02:00
|
|
|
rsID, err := validateSourceID(params.ByName("id"))
|
2018-09-22 15:51:20 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-07-02 14:30:43 +02:00
|
|
|
neighborID := params.ByName("neighborId")
|
2021-10-26 23:11:42 +02:00
|
|
|
source := s.cfg.SourceInstanceByID(rsID)
|
2019-01-11 20:13:31 +01:00
|
|
|
if source == nil {
|
2021-10-26 23:11:42 +02:00
|
|
|
return nil, ErrSourceNotFound
|
2019-01-11 20:13:31 +01:00
|
|
|
}
|
|
|
|
|
2021-07-02 14:30:43 +02:00
|
|
|
result, err := source.RoutesNotExported(neighborID)
|
2018-09-22 15:51:20 +02:00
|
|
|
if err != nil {
|
2021-07-02 14:30:43 +02:00
|
|
|
apiLogSourceError("routes_not_exported", rsID, neighborID, err)
|
2018-09-22 15:51:20 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-10-23 19:06:24 +02:00
|
|
|
// Filter routes based on criteria if present
|
|
|
|
allRoutes := apiQueryFilterNextHopGateway(req, "q", result.NotExported)
|
|
|
|
routes := api.Routes{}
|
|
|
|
|
|
|
|
// Apply other (commmunity) filters
|
2018-10-28 18:17:10 +01:00
|
|
|
filtersApplied, err := api.FiltersFromQuery(req.URL.Query())
|
2018-10-23 19:06:24 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
filtersAvailable := api.NewSearchFilters()
|
|
|
|
for _, r := range allRoutes {
|
2018-10-28 18:17:10 +01:00
|
|
|
if !filtersApplied.MatchRoute(r) {
|
2018-10-23 19:06:24 +02:00
|
|
|
continue // Exclude route from results set
|
|
|
|
}
|
|
|
|
routes = append(routes, r)
|
|
|
|
filtersAvailable.UpdateFromRoute(r)
|
|
|
|
}
|
2018-09-22 15:51:20 +02:00
|
|
|
|
2018-10-25 22:22:32 +02:00
|
|
|
// Remove applied filters from available
|
2018-10-28 18:17:10 +01:00
|
|
|
filtersApplied.MergeProperties(filtersAvailable)
|
|
|
|
filtersAvailable = filtersAvailable.Sub(filtersApplied)
|
2018-10-25 22:22:32 +02:00
|
|
|
|
2018-09-22 15:51:20 +02:00
|
|
|
// Paginate results
|
|
|
|
page := apiQueryMustInt(req, "page", 0)
|
2021-10-26 23:11:42 +02:00
|
|
|
pageSize := s.cfg.UI.Pagination.RoutesNotExportedPageSize
|
2018-09-22 15:51:20 +02:00
|
|
|
routes, pagination := apiPaginateRoutes(routes, page, pageSize)
|
|
|
|
|
2018-10-23 19:06:24 +02:00
|
|
|
// Calculate query duration
|
|
|
|
queryDuration := time.Since(t0)
|
|
|
|
|
2018-09-22 15:51:20 +02:00
|
|
|
// Make response
|
|
|
|
response := api.PaginatedRoutesResponse{
|
|
|
|
RoutesResponse: &api.RoutesResponse{
|
|
|
|
Api: result.Api,
|
|
|
|
NotExported: routes,
|
|
|
|
},
|
2018-10-23 19:06:24 +02:00
|
|
|
TimedResponse: api.TimedResponse{
|
|
|
|
RequestDuration: DurationMs(queryDuration),
|
|
|
|
},
|
|
|
|
FilterableResponse: api.FilterableResponse{
|
|
|
|
FiltersAvailable: filtersAvailable,
|
2018-10-28 18:17:10 +01:00
|
|
|
FiltersApplied: filtersApplied,
|
2018-10-23 19:06:24 +02:00
|
|
|
},
|
2018-09-22 15:51:20 +02:00
|
|
|
Pagination: pagination,
|
|
|
|
}
|
|
|
|
|
|
|
|
return response, nil
|
|
|
|
}
|