Example #1
0
File: vrf.go Project: mfeed/gobgp
func showVrfs() error {
	maxLens := []int{20, 20, 20, 20}
	vrfs, err := getVrfs()
	if err != nil {
		return err
	}
	if globalOpts.Json {
		j, _ := json.Marshal(vrfs)
		fmt.Println(string(j))
		return nil
	}
	if globalOpts.Quiet {
		for _, v := range vrfs {
			fmt.Println(v.Name)
		}
		return nil
	}
	lines := make([][]string, 0, len(vrfs))
	for _, v := range vrfs {
		name := v.Name
		rd := bgp.GetRouteDistinguisher(v.Rd).String()

		f := func(bufs [][]byte) (string, error) {
			ret := make([]string, 0, len(bufs))
			for _, rt := range bufs {
				r, err := bgp.ParseExtended(rt)
				if err != nil {
					return "", err
				}
				ret = append(ret, r.String())
			}
			return strings.Join(ret, ", "), nil
		}

		importRts, _ := f(v.ImportRt)
		exportRts, _ := f(v.ExportRt)
		lines = append(lines, []string{name, rd, importRts, exportRts})

		for i, v := range []int{len(name), len(rd), len(importRts), len(exportRts)} {
			if v > maxLens[i] {
				maxLens[i] = v + 4
			}
		}

	}
	format := fmt.Sprintf("  %%-%ds %%-%ds %%-%ds %%-%ds\n", maxLens[0], maxLens[1], maxLens[2], maxLens[3])
	fmt.Printf(format, "Name", "RD", "Import RT", "Export RT")
	for _, l := range lines {
		fmt.Printf(format, l[0], l[1], l[2], l[3])
	}
	return nil
}
Example #2
0
File: client.go Project: osrg/gobgp
func (cli *GoBGPClient) GetVRF() ([]*table.Vrf, error) {
	ret, err := cli.cli.GetVrf(context.Background(), &api.GetVrfRequest{})
	if err != nil {
		return nil, err
	}
	var vrfs []*table.Vrf

	f := func(bufs [][]byte) ([]bgp.ExtendedCommunityInterface, error) {
		ret := make([]bgp.ExtendedCommunityInterface, 0, len(bufs))
		for _, rt := range bufs {
			r, err := bgp.ParseExtended(rt)
			if err != nil {
				return nil, err
			}
			ret = append(ret, r)
		}
		return ret, nil
	}

	for _, vrf := range ret.Vrfs {
		importRT, err := f(vrf.ImportRt)
		if err != nil {
			return nil, err
		}
		exportRT, err := f(vrf.ExportRt)
		if err != nil {
			return nil, err
		}
		vrfs = append(vrfs, &table.Vrf{
			Name:     vrf.Name,
			Id:       vrf.Id,
			Rd:       bgp.GetRouteDistinguisher(vrf.Rd),
			ImportRt: importRT,
			ExportRt: exportRT,
		})
	}

	return vrfs, nil
}
Example #3
0
func (s *Server) AddVrf(ctx context.Context, arg *AddVrfRequest) (r *AddVrfResponse, err error) {
	rd := bgp.GetRouteDistinguisher(arg.Vrf.Rd)
	f := func(bufs [][]byte) ([]bgp.ExtendedCommunityInterface, error) {
		ret := make([]bgp.ExtendedCommunityInterface, 0, len(bufs))
		for _, rt := range bufs {
			r, err := bgp.ParseExtended(rt)
			if err != nil {
				return nil, err
			}
			ret = append(ret, r)
		}
		return ret, nil
	}
	im, err := f(arg.Vrf.ImportRt)
	if err != nil {
		return &AddVrfResponse{}, err
	}
	ex, err := f(arg.Vrf.ExportRt)
	if err != nil {
		return &AddVrfResponse{}, err
	}
	return &AddVrfResponse{}, s.bgpServer.AddVrf(arg.Vrf.Name, rd, im, ex)
}