diff options
-rw-r--r-- | config/bgp_configs.go | 282 | ||||
-rw-r--r-- | gobgp/cmd/policy.go | 6 | ||||
-rw-r--r-- | server/peer.go | 6 | ||||
-rw-r--r-- | server/rpki.go | 4 | ||||
-rw-r--r-- | table/path.go | 2 | ||||
-rw-r--r-- | table/policy.go | 14 | ||||
-rw-r--r-- | test/lib/gobgp.py | 4 | ||||
-rw-r--r-- | tools/pyang_plugins/bgpyang2golang.py | 56 |
8 files changed, 296 insertions, 78 deletions
diff --git a/config/bgp_configs.go b/config/bgp_configs.go index c0013581..41d32d52 100644 --- a/config/bgp_configs.go +++ b/config/bgp_configs.go @@ -15,30 +15,64 @@ package config +import "fmt" + // typedef for typedef bgp-types:rr-cluster-id-type type RrClusterIdType string // typedef for typedef bgp-types:remove-private-as-option -type RemovePrivateAsOption int +type RemovePrivateAsOption string const ( - REMOVE_PRIVATE_AS_OPTION_ALL RemovePrivateAsOption = iota - REMOVE_PRIVATE_AS_OPTION_REPLACE + REMOVE_PRIVATE_AS_OPTION_ALL RemovePrivateAsOption = "all" + REMOVE_PRIVATE_AS_OPTION_REPLACE RemovePrivateAsOption = "replace" ) +func (v RemovePrivateAsOption) ToInt() int { + for i, vv := range []string{"all", "replace"} { + if string(v) == vv { + return i + } + } + return -1 +} + +func (v RemovePrivateAsOption) Validate() error { + if v.ToInt() < 0 { + return fmt.Errorf("invalid RemovePrivateAsOption: %s", v) + } + return nil +} + // typedef for typedef bgp-types:bgp-community-regexp-type type BgpCommunityRegexpType string // typedef for typedef bgp-types:community-type -type CommunityType int +type CommunityType string const ( - COMMUNITY_TYPE_STANDARD CommunityType = iota - COMMUNITY_TYPE_EXTENDED - COMMUNITY_TYPE_BOTH - COMMUNITY_TYPE_NONE + COMMUNITY_TYPE_STANDARD CommunityType = "standard" + COMMUNITY_TYPE_EXTENDED CommunityType = "extended" + COMMUNITY_TYPE_BOTH CommunityType = "both" + COMMUNITY_TYPE_NONE CommunityType = "none" ) +func (v CommunityType) ToInt() int { + for i, vv := range []string{"standard", "extended", "both", "none"} { + if string(v) == vv { + return i + } + } + return -1 +} + +func (v CommunityType) Validate() error { + if v.ToInt() < 0 { + return fmt.Errorf("invalid CommunityType: %s", v) + } + return nil +} + // typedef for typedef bgp-types:bgp-ext-community-type type BgpExtCommunityType string @@ -46,52 +80,154 @@ type BgpExtCommunityType string type BgpStdCommunityType string // typedef for typedef bgp-types:peer-type -type PeerTypeDef int +type PeerTypeDef string const ( - PEER_TYPE_INTERNAL PeerTypeDef = iota - PEER_TYPE_EXTERNAL + PEER_TYPE_INTERNAL PeerTypeDef = "internal" + PEER_TYPE_EXTERNAL PeerTypeDef = "external" ) +func (v PeerTypeDef) ToInt() int { + for i, vv := range []string{"internal", "external"} { + if string(v) == vv { + return i + } + } + return -1 +} + +func (v PeerTypeDef) Validate() error { + if v.ToInt() < 0 { + return fmt.Errorf("invalid PeerTypeDef: %s", v) + } + return nil +} + // typedef for typedef bgp-types:percentage type Percentage uint8 // typedef for typedef bgp-types:bgp-session-direction -type BgpSessionDirection int +type BgpSessionDirection string const ( - BGP_SESSION_DIRECTION_INBOUND BgpSessionDirection = iota - BGP_SESSION_DIRECTION_OUTBOUND + BGP_SESSION_DIRECTION_INBOUND BgpSessionDirection = "inbound" + BGP_SESSION_DIRECTION_OUTBOUND BgpSessionDirection = "outbound" ) +func (v BgpSessionDirection) ToInt() int { + for i, vv := range []string{"inbound", "outbound"} { + if string(v) == vv { + return i + } + } + return -1 +} + +func (v BgpSessionDirection) Validate() error { + if v.ToInt() < 0 { + return fmt.Errorf("invalid BgpSessionDirection: %s", v) + } + return nil +} + // typedef for typedef ptypes:match-set-options-restricted-type -type MatchSetOptionsRestrictedType int +type MatchSetOptionsRestrictedType string const ( - MATCH_SET_OPTIONS_RESTRICTED_TYPE_ANY MatchSetOptionsRestrictedType = iota - MATCH_SET_OPTIONS_RESTRICTED_TYPE_INVERT + MATCH_SET_OPTIONS_RESTRICTED_TYPE_ANY MatchSetOptionsRestrictedType = "any" + MATCH_SET_OPTIONS_RESTRICTED_TYPE_INVERT MatchSetOptionsRestrictedType = "invert" ) +func (v MatchSetOptionsRestrictedType) ToInt() int { + for i, vv := range []string{"any", "invert"} { + if string(v) == vv { + return i + } + } + return -1 +} + +func (v MatchSetOptionsRestrictedType) Validate() error { + if v.ToInt() < 0 { + return fmt.Errorf("invalid MatchSetOptionsRestrictedType: %s", v) + } + return nil +} + +func (v MatchSetOptionsRestrictedType) Default() MatchSetOptionsRestrictedType { + return MATCH_SET_OPTIONS_RESTRICTED_TYPE_ANY +} + +func (v MatchSetOptionsRestrictedType) DefaultAsNeeded() MatchSetOptionsRestrictedType { + if string(v) == "" { + return v.Default() + } + return v +} + // typedef for typedef ptypes:match-set-options-type -type MatchSetOptionsType int +type MatchSetOptionsType string const ( - MATCH_SET_OPTIONS_TYPE_ANY MatchSetOptionsType = iota - MATCH_SET_OPTIONS_TYPE_ALL - MATCH_SET_OPTIONS_TYPE_INVERT + MATCH_SET_OPTIONS_TYPE_ANY MatchSetOptionsType = "any" + MATCH_SET_OPTIONS_TYPE_ALL MatchSetOptionsType = "all" + MATCH_SET_OPTIONS_TYPE_INVERT MatchSetOptionsType = "invert" ) +func (v MatchSetOptionsType) ToInt() int { + for i, vv := range []string{"any", "all", "invert"} { + if string(v) == vv { + return i + } + } + return -1 +} + +func (v MatchSetOptionsType) Validate() error { + if v.ToInt() < 0 { + return fmt.Errorf("invalid MatchSetOptionsType: %s", v) + } + return nil +} + +func (v MatchSetOptionsType) Default() MatchSetOptionsType { + return MATCH_SET_OPTIONS_TYPE_ANY +} + +func (v MatchSetOptionsType) DefaultAsNeeded() MatchSetOptionsType { + if string(v) == "" { + return v.Default() + } + return v +} + // typedef for typedef ptypes:tag-type type TagType string // typedef for typedef rpol:default-policy-type -type DefaultPolicyType int +type DefaultPolicyType string const ( - DEFAULT_POLICY_TYPE_ACCEPT_ROUTE DefaultPolicyType = iota - DEFAULT_POLICY_TYPE_REJECT_ROUTE + DEFAULT_POLICY_TYPE_ACCEPT_ROUTE DefaultPolicyType = "accept-route" + DEFAULT_POLICY_TYPE_REJECT_ROUTE DefaultPolicyType = "reject-route" ) +func (v DefaultPolicyType) ToInt() int { + for i, vv := range []string{"accept-route", "reject-route"} { + if string(v) == vv { + return i + } + } + return -1 +} + +func (v DefaultPolicyType) Validate() error { + if v.ToInt() < 0 { + return fmt.Errorf("invalid DefaultPolicyType: %s", v) + } + return nil +} + // typedef for typedef bgp-pol:bgp-next-hop-type type BgpNextHopType string @@ -102,42 +238,106 @@ type BgpAsPathPrependRepeat uint8 type BgpSetMedType string // typedef for typedef bgp-pol:bgp-set-community-option-type -type BgpSetCommunityOptionType int +type BgpSetCommunityOptionType string const ( - BGP_SET_COMMUNITY_OPTION_TYPE_ADD BgpSetCommunityOptionType = iota - BGP_SET_COMMUNITY_OPTION_TYPE_REMOVE - BGP_SET_COMMUNITY_OPTION_TYPE_REPLACE + BGP_SET_COMMUNITY_OPTION_TYPE_ADD BgpSetCommunityOptionType = "add" + BGP_SET_COMMUNITY_OPTION_TYPE_REMOVE BgpSetCommunityOptionType = "remove" + BGP_SET_COMMUNITY_OPTION_TYPE_REPLACE BgpSetCommunityOptionType = "replace" ) +func (v BgpSetCommunityOptionType) ToInt() int { + for i, vv := range []string{"add", "remove", "replace"} { + if string(v) == vv { + return i + } + } + return -1 +} + +func (v BgpSetCommunityOptionType) Validate() error { + if v.ToInt() < 0 { + return fmt.Errorf("invalid BgpSetCommunityOptionType: %s", v) + } + return nil +} + // typedef for typedef gobgp:bmp-route-monitoring-policy-type -type BmpRouteMonitoringPolicyType int +type BmpRouteMonitoringPolicyType string const ( - BMP_ROUTE_MONITORING_POLICY_TYPE_PRE_POLICY BmpRouteMonitoringPolicyType = 0 - BMP_ROUTE_MONITORING_POLICY_TYPE_POST_POLICY = 1 - BMP_ROUTE_MONITORING_POLICY_TYPE_BOTH = 2 + BMP_ROUTE_MONITORING_POLICY_TYPE_PRE_POLICY BmpRouteMonitoringPolicyType = "pre-policy" + BMP_ROUTE_MONITORING_POLICY_TYPE_POST_POLICY BmpRouteMonitoringPolicyType = "post-policy" + BMP_ROUTE_MONITORING_POLICY_TYPE_BOTH BmpRouteMonitoringPolicyType = "both" ) +func (v BmpRouteMonitoringPolicyType) ToInt() int { + for i, vv := range []string{"pre-policy", "post-policy", "both"} { + if string(v) == vv { + return i + } + } + return -1 +} + +func (v BmpRouteMonitoringPolicyType) Validate() error { + if v.ToInt() < 0 { + return fmt.Errorf("invalid BmpRouteMonitoringPolicyType: %s", v) + } + return nil +} + // typedef for typedef gobgp:rpki-validation-result-type -type RpkiValidationResultType int +type RpkiValidationResultType string const ( - RPKI_VALIDATION_RESULT_TYPE_NONE RpkiValidationResultType = iota - RPKI_VALIDATION_RESULT_TYPE_NOT_FOUND - RPKI_VALIDATION_RESULT_TYPE_VALID - RPKI_VALIDATION_RESULT_TYPE_INVALID + RPKI_VALIDATION_RESULT_TYPE_NONE RpkiValidationResultType = "none" + RPKI_VALIDATION_RESULT_TYPE_NOT_FOUND RpkiValidationResultType = "not-found" + RPKI_VALIDATION_RESULT_TYPE_VALID RpkiValidationResultType = "valid" + RPKI_VALIDATION_RESULT_TYPE_INVALID RpkiValidationResultType = "invalid" ) +func (v RpkiValidationResultType) ToInt() int { + for i, vv := range []string{"none", "not-found", "valid", "invalid"} { + if string(v) == vv { + return i + } + } + return -1 +} + +func (v RpkiValidationResultType) Validate() error { + if v.ToInt() < 0 { + return fmt.Errorf("invalid RpkiValidationResultType: %s", v) + } + return nil +} + // typedef for typedef gobgp:bgp-origin-attr-type -type BgpOriginAttrType int +type BgpOriginAttrType string const ( - BGP_ORIGIN_ATTR_TYPE_IGP BgpOriginAttrType = 0 - BGP_ORIGIN_ATTR_TYPE_EGP = 1 - BGP_ORIGIN_ATTR_TYPE_INCOMPLETE = 2 + BGP_ORIGIN_ATTR_TYPE_IGP BgpOriginAttrType = "igp" + BGP_ORIGIN_ATTR_TYPE_EGP BgpOriginAttrType = "egp" + BGP_ORIGIN_ATTR_TYPE_INCOMPLETE BgpOriginAttrType = "incomplete" ) +func (v BgpOriginAttrType) ToInt() int { + for i, vv := range []string{"igp", "egp", "incomplete"} { + if string(v) == vv { + return i + } + } + return -1 +} + +func (v BgpOriginAttrType) Validate() error { + if v.ToInt() < 0 { + return fmt.Errorf("invalid BgpOriginAttrType: %s", v) + } + return nil +} + //struct for container gobgp:state type BmpServerState struct { } diff --git a/gobgp/cmd/policy.go b/gobgp/cmd/policy.go index 6b3fcb85..9a30f15b 100644 --- a/gobgp/cmd/policy.go +++ b/gobgp/cmd/policy.go @@ -734,11 +734,11 @@ func modCondition(name, op string, args []string) error { } switch strings.ToLower(args[0]) { case "valid": - stmt.Conditions.RpkiResult = int32(config.RPKI_VALIDATION_RESULT_TYPE_VALID) + stmt.Conditions.RpkiResult = int32(config.RPKI_VALIDATION_RESULT_TYPE_VALID.ToInt()) case "invalid": - stmt.Conditions.RpkiResult = int32(config.RPKI_VALIDATION_RESULT_TYPE_INVALID) + stmt.Conditions.RpkiResult = int32(config.RPKI_VALIDATION_RESULT_TYPE_INVALID.ToInt()) case "not-found": - stmt.Conditions.RpkiResult = int32(config.RPKI_VALIDATION_RESULT_TYPE_NOT_FOUND) + stmt.Conditions.RpkiResult = int32(config.RPKI_VALIDATION_RESULT_TYPE_NOT_FOUND.ToInt()) default: return fmt.Errorf("%s rpki { valid | invalid | not-found }") } diff --git a/server/peer.go b/server/peer.go index 5f42c73e..518edefe 100644 --- a/server/peer.go +++ b/server/peer.go @@ -226,11 +226,11 @@ func (peer *Peer) ToApiStruct() *api.Peer { Id: peer.fsm.peerInfo.ID.To4().String(), PeerAs: c.Config.PeerAs, LocalAs: c.Config.LocalAs, - PeerType: uint32(c.Config.PeerType), + PeerType: uint32(c.Config.PeerType.ToInt()), AuthPassword: c.Config.AuthPassword, - RemovePrivateAs: uint32(c.Config.RemovePrivateAs), + RemovePrivateAs: uint32(c.Config.RemovePrivateAs.ToInt()), RouteFlapDamping: c.Config.RouteFlapDamping, - SendCommunity: uint32(c.Config.SendCommunity), + SendCommunity: uint32(c.Config.SendCommunity.ToInt()), Description: c.Config.Description, PeerGroup: c.Config.PeerGroup, RemoteCap: remoteCap, diff --git a/server/rpki.go b/server/rpki.go index 293b0a97..aa50a475 100644 --- a/server/rpki.go +++ b/server/rpki.go @@ -460,8 +460,8 @@ func (c *roaManager) validate(pathList []*table.Path, isMonitor bool) []*api.ROA rr := &api.ROAResult{ OriginAs: path.GetSourceAs(), Prefix: path.GetNlri().String(), - OldResult: api.ROAResult_ValidationResult(path.Validation), - NewResult: api.ROAResult_ValidationResult(r), + OldResult: api.ROAResult_ValidationResult(path.Validation.ToInt()), + NewResult: api.ROAResult_ValidationResult(r.ToInt()), Roas: apiRoaList, } results = append(results, rr) diff --git a/table/path.go b/table/path.go index 05144812..94bc5fd0 100644 --- a/table/path.go +++ b/table/path.go @@ -208,7 +208,7 @@ func (path *Path) ToApiStruct(id string) *api.Path { Pattrs: pattrs, Age: int64(time.Now().Sub(path.timestamp).Seconds()), IsWithdraw: path.IsWithdraw, - Validation: int32(path.Validation), + Validation: int32(path.Validation.ToInt()), Filtered: path.Filtered(id) > POLICY_DIRECTION_NONE, Family: family, } diff --git a/table/policy.go b/table/policy.go index c9e65300..e556ec1d 100644 --- a/table/policy.go +++ b/table/policy.go @@ -172,9 +172,10 @@ const ( ) func NewMatchOption(c interface{}) (MatchOption, error) { - switch c.(type) { + switch t := c.(type) { case config.MatchSetOptionsType: - switch c.(config.MatchSetOptionsType) { + t = t.DefaultAsNeeded() + switch t { case config.MATCH_SET_OPTIONS_TYPE_ANY: return MATCH_OPTION_ANY, nil case config.MATCH_SET_OPTIONS_TYPE_ALL: @@ -183,7 +184,8 @@ func NewMatchOption(c interface{}) (MatchOption, error) { return MATCH_OPTION_INVERT, nil } case config.MatchSetOptionsRestrictedType: - switch c.(config.MatchSetOptionsRestrictedType) { + t = t.DefaultAsNeeded() + switch t { case config.MATCH_SET_OPTIONS_RESTRICTED_TYPE_ANY: return MATCH_OPTION_ANY, nil case config.MATCH_SET_OPTIONS_RESTRICTED_TYPE_INVERT: @@ -1696,7 +1698,7 @@ func (a *CommunityAction) ToApiStruct() *api.CommunityAction { cs = append(cs, exp.String()) } return &api.CommunityAction{ - Type: api.CommunityActionType(a.action), + Type: api.CommunityActionType(a.action.ToInt()), Communities: cs, } } @@ -1814,7 +1816,7 @@ func (a *ExtCommunityAction) ToApiStruct() *api.CommunityAction { cs = append(cs, f(idx, exp.String())) } return &api.CommunityAction{ - Type: api.CommunityActionType(a.action), + Type: api.CommunityActionType(a.action.ToInt()), Communities: cs, } } @@ -2108,7 +2110,7 @@ func (s *Statement) ToApiStruct() *api.Statement { case *ExtCommunityCondition: cs.ExtCommunitySet = c.(*ExtCommunityCondition).ToApiStruct() case *RpkiValidationCondition: - cs.RpkiResult = int32(c.(*RpkiValidationCondition).result) + cs.RpkiResult = int32(c.(*RpkiValidationCondition).result.ToInt()) } } as := &api.Actions{} diff --git a/test/lib/gobgp.py b/test/lib/gobgp.py index 22b68385..06924edd 100644 --- a/test/lib/gobgp.py +++ b/test/lib/gobgp.py @@ -244,9 +244,9 @@ class GoBGPContainer(BGPContainer): def f(v): if v == 'reject': - return 1 + return 'reject-route' elif v == 'accept': - return 0 + return 'accept-route' raise Exception('invalid default policy type {0}'.format(v)) if len(default_import_policy) > 0: diff --git a/tools/pyang_plugins/bgpyang2golang.py b/tools/pyang_plugins/bgpyang2golang.py index 3b7702e9..ce64fce0 100644 --- a/tools/pyang_plugins/bgpyang2golang.py +++ b/tools/pyang_plugins/bgpyang2golang.py @@ -34,6 +34,7 @@ _COPYRIGHT_NOTICE = """ // implied. // See the License for the specific language governing permissions and // limitations under the License. + """ emitted_type_names = {} @@ -268,7 +269,6 @@ def visit_children(ctx, module, children): c.uniq_name = c.parent.uniq_name + '-config' if c.arg == 'state': - c.uniq_name = c.parent.uniq_name + '-state' if c.arg == 'graceful-restart' and prefix == 'bgp-mp': @@ -377,7 +377,6 @@ def emit_typedef(ctx, module): prefix = module.i_prefix t_map = ctx.golang_typedef_map[prefix] for name, stmt in t_map.items(): - if stmt.path in _typedef_exclude: continue @@ -402,31 +401,46 @@ def emit_typedef(ctx, module): if t.arg == 'enumeration': print >> o, '// typedef for typedef %s:%s'\ % (prefix, type_name_org) - print >> o, 'type %s int' % (type_name) + print >> o, 'type %s string' % (type_name) const_prefix = convert_const_prefix(type_name_org) print >> o, 'const (' - - already_added_iota = False - already_added_type = False + m = {} for sub in t.substmts: - if sub.search_one('value'): - enum_value = " = "+sub.search_one('value').arg - else: - if already_added_iota: - enum_value = "" - else: - enum_value = " = iota" - already_added_iota = True + enum_name = '%s_%s' % (const_prefix, convert_const_prefix(sub.arg)) + m[sub.arg.lower()] = enum_name + print >> o, ' %s %s = "%s"' % (enum_name, type_name, sub.arg.lower()) + print >> o, ')\n' + + print >> o, '\nfunc (v %s) ToInt() int {' % (type_name) + print >> o, 'for i, vv := range []string{%s} {' % (",".join('"%s"' % s.arg.lower() for s in t.substmts)) + print >> o, 'if string(v) == vv {return i}' + print >> o, '}' + print >> o, 'return -1' + print >> o, '}\n' + + print >> o, 'func (v %s) Validate() error {' % (type_name) + print >> o, 'if v.ToInt() < 0 {' + print >> o, 'return fmt.Errorf("invalid %s: %%s", v)' % (type_name) + print >> o, '}' + print >> o, 'return nil' + print >> o, '}\n' + + if stmt.search_one('default'): + default = stmt.search_one('default') + print >> o, 'func (v %s) Default() %s {' % (type_name, type_name) + print >> o, 'return %s' % m[default.arg.lower()] + print >> o, '}\n' + + print >> o, 'func (v %s) DefaultAsNeeded() %s {' % (type_name, type_name) + print >> o, ' if string(v) == "" {' + print >> o, ' return v.Default()' + print >> o, '}' + print >> o, ' return v' + print >> o, '}' - enum_name = convert_const_prefix(sub.arg) - t = type_name if not already_added_type else "" - already_added_type = True - - print >> o, ' %s_%s %s%s' % (const_prefix, enum_name, t, enum_value) - print >> o, ')' elif t.arg == 'union': print >> o, '// typedef for typedef %s:%s'\ % (prefix, type_name_org) @@ -548,6 +562,8 @@ def generate_header(ctx): print _COPYRIGHT_NOTICE print 'package config' print '' + print 'import "fmt"' + print '' def translate_type(key): |