func extractCommunity(args []string) ([]string, bgp.PathAttributeInterface, error) { for idx, arg := range args { if arg == "community" && len(args) > (idx+1) { elems := strings.Split(args[idx+1], ",") comms := make([]uint32, 0, 1) for _, elem := range elems { c, err := table.ParseCommunity(elem) if err != nil { return nil, nil, err } comms = append(comms, c) } args = append(args[:idx], args[idx+2:]...) return args, bgp.NewPathAttributeCommunities(comms), nil } } return args, nil, nil }
// RemoveCommunities removes specific communities. // If the length of communites is 0, it does nothing. // If all communities are removed, it removes Communities path attribute itself. func (path *Path) RemoveCommunities(communities []uint32) int { if len(communities) == 0 { // do nothing return 0 } find := func(val uint32) bool { for _, com := range communities { if com == val { return true } } return false } count := 0 attr := path.getPathAttr(bgp.BGP_ATTR_TYPE_COMMUNITIES) if attr != nil { newList := make([]uint32, 0) c := attr.(*bgp.PathAttributeCommunities) for _, value := range c.Value { if find(value) { count += 1 } else { newList = append(newList, value) } } if len(newList) != 0 { path.setPathAttr(bgp.NewPathAttributeCommunities(newList)) } else { path.delPathAttr(bgp.BGP_ATTR_TYPE_COMMUNITIES) } } return count }
// SetCommunities adds or replaces communities with new ones. // If the length of communities is 0 and doReplace is true, it clears communities. func (path *Path) SetCommunities(communities []uint32, doReplace bool) { if len(communities) == 0 && doReplace { // clear communities path.delPathAttr(bgp.BGP_ATTR_TYPE_COMMUNITIES) return } newList := make([]uint32, 0) attr := path.getPathAttr(bgp.BGP_ATTR_TYPE_COMMUNITIES) if attr != nil { c := attr.(*bgp.PathAttributeCommunities) if doReplace { newList = append(newList, communities...) } else { newList = append(newList, c.Value...) newList = append(newList, communities...) } } else { newList = append(newList, communities...) } path.setPathAttr(bgp.NewPathAttributeCommunities(newList)) }