From 9ae30060eb399b825f035160d5b77df74789ffb6 Mon Sep 17 00:00:00 2001 From: Satoshi Fujimoto Date: Thu, 21 Jun 2018 16:18:20 +0900 Subject: cmd: Implement PrettyString() Signed-off-by: Satoshi Fujimoto --- api/policy.go | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++-- gobgp/cmd/policy.go | 31 ++++++++++----------- 2 files changed, 92 insertions(+), 18 deletions(-) diff --git a/api/policy.go b/api/policy.go index 7296f012..102e2e02 100644 --- a/api/policy.go +++ b/api/policy.go @@ -25,8 +25,9 @@ import ( ) var ( - repexpCommunity = regexp.MustCompile("(\\d+.)*\\d+:\\d+") - regexpLargeCommunity = regexp.MustCompile("\\d+:\\d+:\\d+") + repexpCommunity = regexp.MustCompile("(\\d+.)*\\d+:\\d+") + regexpLargeCommunity = regexp.MustCompile("\\d+:\\d+:\\d+") + regexpCommunityString = regexp.MustCompile("[\\^\\$]") ) func ParseCommunityRegexp(arg string) (*regexp.Regexp, error) { @@ -77,3 +78,77 @@ func ParseLargeCommunityRegexp(arg string) (*regexp.Regexp, error) { } return exp, nil } + +func (s *MatchSet) PrettyString() string { + var typ string + switch s.Type { + case MatchType_ALL: + typ = "all" + case MatchType_ANY: + typ = "any" + case MatchType_INVERT: + typ = "invert" + } + return fmt.Sprintf("%s %s", typ, s.GetName()) +} + +func (s *AsPathLength) PrettyString() string { + var typ string + switch s.Type { + case AsPathLengthType_EQ: + typ = "=" + case AsPathLengthType_GE: + typ = ">=" + case AsPathLengthType_LE: + typ = "<=" + } + return fmt.Sprintf("%s%d", typ, s.Length) +} + +func (s Conditions_RouteType) PrettyString() string { + switch s { + case Conditions_ROUTE_TYPE_EXTERNAL: + return "external" + case Conditions_ROUTE_TYPE_INTERNAL: + return "internal" + case Conditions_ROUTE_TYPE_LOCAL: + return "local" + } + return "unknown" +} + +func (a *CommunityAction) PrettyString() string { + l := regexpCommunityString.ReplaceAllString(strings.Join(a.Communities, ", "), "") + var typ string + switch a.Type { + case CommunityActionType_COMMUNITY_ADD: + typ = "add" + case CommunityActionType_COMMUNITY_REMOVE: + typ = "remove" + case CommunityActionType_COMMUNITY_REPLACE: + typ = "replace" + } + return fmt.Sprintf("%s[%s]", typ, l) +} + +func (a *MedAction) PrettyString() string { + if a.Type == MedActionType_MED_MOD && a.Value > 0 { + return fmt.Sprintf("+%d", a.Value) + } + return fmt.Sprintf("%d", a.Value) +} + +func (a *LocalPrefAction) PrettyString() string { + return fmt.Sprintf("%d", a.Value) +} + +func (a *NexthopAction) PrettyString() string { + if a.Self { + return "self" + } + return a.Address +} + +func (a *AsPrependAction) PrettyString() string { + return fmt.Sprintf("prepend %d %d times", a.Asn, a.Repeat) +} diff --git a/gobgp/cmd/policy.go b/gobgp/cmd/policy.go index 84da805c..f86e4078 100644 --- a/gobgp/cmd/policy.go +++ b/gobgp/cmd/policy.go @@ -337,23 +337,22 @@ func printStatement(indent int, s *api.Statement) { c := s.Conditions if c.PrefixSet != nil { - fmt.Printf("%sPrefixSet: %s %s\n", ind, c.PrefixSet.GetType(), c.PrefixSet.GetName()) + fmt.Printf("%sPrefixSet: %s \n", ind, c.PrefixSet.PrettyString()) } else if c.NeighborSet != nil { - fmt.Printf("%sNeighborSet: %s %s\n", ind, c.NeighborSet.GetType(), c.NeighborSet.GetName()) + fmt.Printf("%sNeighborSet: %s\n", ind, c.NeighborSet.PrettyString()) } else if c.AsPathSet != nil { - fmt.Printf("%sAsPathSet: %s %s\n", ind, c.AsPathSet.GetType(), c.AsPathSet.GetName()) + fmt.Printf("%sAsPathSet: %s \n", ind, c.AsPathSet.PrettyString()) } else if c.CommunitySet != nil { - fmt.Printf("%sCommunitySet: %s %s\n", ind, c.CommunitySet.GetType(), c.CommunitySet.GetName()) + fmt.Printf("%sCommunitySet: %s\n", ind, c.CommunitySet.PrettyString()) } else if c.ExtCommunitySet != nil { - fmt.Printf("%sExtCommunitySet: %s %s\n", ind, c.ExtCommunitySet.GetType(), c.ExtCommunitySet.GetName()) + fmt.Printf("%sExtCommunitySet: %s\n", ind, c.ExtCommunitySet.PrettyString()) } else if c.LargeCommunitySet != nil { - fmt.Printf("%sLargeCommunitySet: %s %s\n", ind, c.LargeCommunitySet.GetType(), c.LargeCommunitySet.GetName()) + fmt.Printf("%sLargeCommunitySet: %s\n", ind, c.LargeCommunitySet.PrettyString()) } else if c.NextHopInList != nil { fmt.Printf("%sNextHopInList: %s\n", ind, "[ "+strings.Join(c.NextHopInList, ", ")+" ]") } else if c.AsPathLength != nil { - fmt.Printf("%sAsPathLength: %s\n", ind, fmt.Sprintf("%s%d", c.AsPathLength.Type, c.AsPathLength.Length)) + fmt.Printf("%sAsPathLength: %s\n", ind, c.AsPathLength.PrettyString()) } else if c.RpkiResult != -1 { - fmt.Println(c.RpkiResult) var result string switch c.RpkiResult { case 0: @@ -367,7 +366,7 @@ func printStatement(indent int, s *api.Statement) { } fmt.Printf("%sRPKI result: %s\n", ind, result) } else if c.RouteType != api.Conditions_ROUTE_TYPE_NONE { - fmt.Printf("%sRoute Type: %s\n", ind, c.RouteType.String()) + fmt.Printf("%sRoute Type: %s\n", ind, c.RouteType.PrettyString()) } else if c.AfiSafiIn != nil { fmt.Printf("%sAFI SAFI In: %s\n", ind, c.AfiSafiIn) } @@ -375,19 +374,19 @@ func printStatement(indent int, s *api.Statement) { fmt.Printf("%sActions:\n", sIndent(indent+2)) a := s.Actions if a.Community != nil { - fmt.Println(ind, "Community: ", a.Community.String()) + fmt.Println(ind, "Community: ", a.Community.PrettyString()) } else if a.ExtCommunity != nil { - fmt.Println(ind, "ExtCommunity: ", a.ExtCommunity.String()) + fmt.Println(ind, "ExtCommunity: ", a.ExtCommunity.PrettyString()) } else if a.LargeCommunity != nil { - fmt.Println(ind, "LargeCommunity: ", a.LargeCommunity.String()) + fmt.Println(ind, "LargeCommunity: ", a.LargeCommunity.PrettyString()) } else if a.Med != nil { - fmt.Println(ind, "MED: ", a.Med.String()) + fmt.Println(ind, "MED: ", a.Med.PrettyString()) } else if a.LocalPref != nil { - fmt.Println(ind, "LocalPref: ", a.LocalPref.String()) + fmt.Println(ind, "LocalPref: ", a.LocalPref.PrettyString()) } else if a.AsPrepend != nil { - fmt.Println(ind, "ASPathPrepend: ", a.AsPrepend.String()) + fmt.Println(ind, "ASPathPrepend: ", a.AsPrepend.PrettyString()) } else if a.Nexthop != nil { - fmt.Println(ind, "Nexthop: ", a.Nexthop.String()) + fmt.Println(ind, "Nexthop: ", a.Nexthop.PrettyString()) } if a.RouteAction != api.RouteAction_NONE { -- cgit v1.2.3