| 2465 | } |
| 2466 | |
| 2467 | func (s *BgpServer) AddPath(req apiutil.AddPathRequest) ([]apiutil.AddPathResponse, error) { |
| 2468 | if len(req.Paths) == 0 { |
| 2469 | return []apiutil.AddPathResponse{}, fmt.Errorf("no path(s) to add") |
| 2470 | } |
| 2471 | isVRF := false |
| 2472 | if req.VRFID != "" { |
| 2473 | if _, ok := s.globalRib.GetVrf(req.VRFID); !ok { |
| 2474 | return []apiutil.AddPathResponse{}, fmt.Errorf("vrf %s not found", req.VRFID) |
| 2475 | } |
| 2476 | isVRF = true |
| 2477 | } |
| 2478 | |
| 2479 | resps := make([]apiutil.AddPathResponse, len(req.Paths)) |
| 2480 | var lastErr error |
| 2481 | err := s.mgmtOperation(func() error { |
| 2482 | for i, p := range req.Paths { |
| 2483 | if p == nil { |
| 2484 | lastErr = errors.New("path is nil") |
| 2485 | resps[i].Error = lastErr |
| 2486 | continue |
| 2487 | } |
| 2488 | path, err := apiutil2Path(p, isVRF) |
| 2489 | if err != nil { |
| 2490 | lastErr = err |
| 2491 | resps[i].Error = err |
| 2492 | continue |
| 2493 | } |
| 2494 | |
| 2495 | err = s.addPathList(req.VRFID, []*table.Path{path}) |
| 2496 | if err != nil { |
| 2497 | lastErr = err |
| 2498 | resps[i].Error = err |
| 2499 | continue |
| 2500 | } |
| 2501 | |
| 2502 | id, err := uuid.NewRandom() |
| 2503 | if err != nil { |
| 2504 | lastErr = err |
| 2505 | resps[i].Error = err |
| 2506 | continue |
| 2507 | } |
| 2508 | s.uuidMap[pathTokey(path)] = id |
| 2509 | resps[i].UUID = id |
| 2510 | } |
| 2511 | return lastErr |
| 2512 | }, true) |
| 2513 | if err != nil { |
| 2514 | return []apiutil.AddPathResponse{}, err |
| 2515 | } |
| 2516 | return resps, err |
| 2517 | } |
| 2518 | |
| 2519 | // if deleteAll is true, it will delete all locally generated paths, if deleteFamily is set, then the whole family will be deleted |
| 2520 | // if uuids is not empty, it will delete paths with the given UUIDs otherwise it will delete specified paths |