func NewDefinedSetFromApiStruct(a *DefinedSet) (table.DefinedSet, error) { if a.Name == "" { return nil, fmt.Errorf("empty neighbor set name") } switch table.DefinedType(a.Type) { case table.DEFINED_TYPE_PREFIX: prefixes := make([]*table.Prefix, 0, len(a.Prefixes)) for _, p := range a.Prefixes { prefix, err := NewPrefixFromApiStruct(p) if err != nil { return nil, err } prefixes = append(prefixes, prefix) } return table.NewPrefixSetFromApiStruct(a.Name, prefixes) case table.DEFINED_TYPE_NEIGHBOR: list := make([]net.IP, 0, len(a.List)) for _, x := range a.List { addr := net.ParseIP(x) if addr == nil { return nil, fmt.Errorf("invalid ip address format: %s", x) } list = append(list, addr) } return table.NewNeighborSetFromApiStruct(a.Name, list) case table.DEFINED_TYPE_AS_PATH: return table.NewAsPathSet(config.AsPathSet{ AsPathSetName: a.Name, AsPathList: a.List, }) case table.DEFINED_TYPE_COMMUNITY: return table.NewCommunitySet(config.CommunitySet{ CommunitySetName: a.Name, CommunityList: a.List, }) case table.DEFINED_TYPE_EXT_COMMUNITY: return table.NewExtCommunitySet(config.ExtCommunitySet{ ExtCommunitySetName: a.Name, ExtCommunityList: a.List, }) default: return nil, fmt.Errorf("invalid defined type") } }
func (s *Server) GetDefinedSet(ctx context.Context, arg *GetDefinedSetRequest) (*GetDefinedSetResponse, error) { cd, err := s.bgpServer.GetDefinedSet(table.DefinedType(arg.Type)) if err != nil { return nil, err } sets := make([]*DefinedSet, 0) for _, cs := range cd.PrefixSets { ad := &DefinedSet{ Type: DefinedType_PREFIX, Name: cs.PrefixSetName, Prefixes: func() []*Prefix { l := make([]*Prefix, 0, len(cs.PrefixList)) for _, p := range cs.PrefixList { exp := regexp.MustCompile("(\\d+)\\.\\.(\\d+)") elems := exp.FindStringSubmatch(p.MasklengthRange) min, _ := strconv.Atoi(elems[1]) max, _ := strconv.Atoi(elems[2]) l = append(l, &Prefix{IpPrefix: p.IpPrefix, MaskLengthMin: uint32(min), MaskLengthMax: uint32(max)}) } return l }(), } sets = append(sets, ad) } for _, cs := range cd.NeighborSets { ad := &DefinedSet{ Type: DefinedType_NEIGHBOR, Name: cs.NeighborSetName, List: cs.NeighborInfoList, } sets = append(sets, ad) } for _, cs := range cd.BgpDefinedSets.CommunitySets { ad := &DefinedSet{ Type: DefinedType_COMMUNITY, Name: cs.CommunitySetName, List: cs.CommunityList, } sets = append(sets, ad) } for _, cs := range cd.BgpDefinedSets.ExtCommunitySets { ad := &DefinedSet{ Type: DefinedType_EXT_COMMUNITY, Name: cs.ExtCommunitySetName, List: cs.ExtCommunityList, } sets = append(sets, ad) } for _, cs := range cd.BgpDefinedSets.AsPathSets { ad := &DefinedSet{ Type: DefinedType_AS_PATH, Name: cs.AsPathSetName, List: cs.AsPathList, } sets = append(sets, ad) } return &GetDefinedSetResponse{Sets: sets}, nil }