diff options
author | ISHIDA Wataru <ishida.wataru@lab.ntt.co.jp> | 2016-01-08 14:49:27 +0900 |
---|---|---|
committer | FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp> | 2016-01-09 05:17:57 -0800 |
commit | 3bed393b014210f6764778138798c31725839079 (patch) | |
tree | 469607542c5e8291b18de97cbd98737f6d2f66ab | |
parent | a0b87f1ddf849cc44a39645fb296bfe2d3a5b287 (diff) |
config: add FromInt() method to config typedef
use for the translation between api structs
Signed-off-by: ISHIDA Wataru <ishida.wataru@lab.ntt.co.jp>
-rw-r--r-- | config/bgp_configs.go | 99 | ||||
-rw-r--r-- | table/policy.go | 6 | ||||
-rw-r--r-- | tools/pyang_plugins/bgpyang2golang.py | 9 |
3 files changed, 111 insertions, 3 deletions
diff --git a/config/bgp_configs.go b/config/bgp_configs.go index 42c3e750..6baa8bdd 100644 --- a/config/bgp_configs.go +++ b/config/bgp_configs.go @@ -43,6 +43,15 @@ func (v RemovePrivateAsOption) ToInt() int { return -1 } +func (v RemovePrivateAsOption) FromInt(i int) RemovePrivateAsOption { + for j, vv := range []string{"all", "replace"} { + if i == j { + return RemovePrivateAsOption(vv) + } + } + return RemovePrivateAsOption("") +} + func (v RemovePrivateAsOption) Validate() error { if v.ToInt() < 0 { return fmt.Errorf("invalid RemovePrivateAsOption: %s", v) @@ -72,6 +81,15 @@ func (v CommunityType) ToInt() int { return -1 } +func (v CommunityType) FromInt(i int) CommunityType { + for j, vv := range []string{"standard", "extended", "both", "none"} { + if i == j { + return CommunityType(vv) + } + } + return CommunityType("") +} + func (v CommunityType) Validate() error { if v.ToInt() < 0 { return fmt.Errorf("invalid CommunityType: %s", v) @@ -102,6 +120,15 @@ func (v PeerTypeDef) ToInt() int { return -1 } +func (v PeerTypeDef) FromInt(i int) PeerTypeDef { + for j, vv := range []string{"internal", "external"} { + if i == j { + return PeerTypeDef(vv) + } + } + return PeerTypeDef("") +} + func (v PeerTypeDef) Validate() error { if v.ToInt() < 0 { return fmt.Errorf("invalid PeerTypeDef: %s", v) @@ -126,6 +153,15 @@ func (v BgpSessionDirection) ToInt() int { return -1 } +func (v BgpSessionDirection) FromInt(i int) BgpSessionDirection { + for j, vv := range []string{"inbound", "outbound"} { + if i == j { + return BgpSessionDirection(vv) + } + } + return BgpSessionDirection("") +} + func (v BgpSessionDirection) Validate() error { if v.ToInt() < 0 { return fmt.Errorf("invalid BgpSessionDirection: %s", v) @@ -150,6 +186,15 @@ func (v MatchSetOptionsRestrictedType) ToInt() int { return -1 } +func (v MatchSetOptionsRestrictedType) FromInt(i int) MatchSetOptionsRestrictedType { + for j, vv := range []string{"any", "invert"} { + if i == j { + return MatchSetOptionsRestrictedType(vv) + } + } + return MatchSetOptionsRestrictedType("") +} + func (v MatchSetOptionsRestrictedType) Validate() error { if v.ToInt() < 0 { return fmt.Errorf("invalid MatchSetOptionsRestrictedType: %s", v) @@ -186,6 +231,15 @@ func (v MatchSetOptionsType) ToInt() int { return -1 } +func (v MatchSetOptionsType) FromInt(i int) MatchSetOptionsType { + for j, vv := range []string{"any", "all", "invert"} { + if i == j { + return MatchSetOptionsType(vv) + } + } + return MatchSetOptionsType("") +} + func (v MatchSetOptionsType) Validate() error { if v.ToInt() < 0 { return fmt.Errorf("invalid MatchSetOptionsType: %s", v) @@ -224,6 +278,15 @@ func (v DefaultPolicyType) ToInt() int { return -1 } +func (v DefaultPolicyType) FromInt(i int) DefaultPolicyType { + for j, vv := range []string{"accept-route", "reject-route"} { + if i == j { + return DefaultPolicyType(vv) + } + } + return DefaultPolicyType("") +} + func (v DefaultPolicyType) Validate() error { if v.ToInt() < 0 { return fmt.Errorf("invalid DefaultPolicyType: %s", v) @@ -258,6 +321,15 @@ func (v BgpSetCommunityOptionType) ToInt() int { return -1 } +func (v BgpSetCommunityOptionType) FromInt(i int) BgpSetCommunityOptionType { + for j, vv := range []string{"add", "remove", "replace"} { + if i == j { + return BgpSetCommunityOptionType(vv) + } + } + return BgpSetCommunityOptionType("") +} + func (v BgpSetCommunityOptionType) Validate() error { if v.ToInt() < 0 { return fmt.Errorf("invalid BgpSetCommunityOptionType: %s", v) @@ -283,6 +355,15 @@ func (v BmpRouteMonitoringPolicyType) ToInt() int { return -1 } +func (v BmpRouteMonitoringPolicyType) FromInt(i int) BmpRouteMonitoringPolicyType { + for j, vv := range []string{"pre-policy", "post-policy", "both"} { + if i == j { + return BmpRouteMonitoringPolicyType(vv) + } + } + return BmpRouteMonitoringPolicyType("") +} + func (v BmpRouteMonitoringPolicyType) Validate() error { if v.ToInt() < 0 { return fmt.Errorf("invalid BmpRouteMonitoringPolicyType: %s", v) @@ -309,6 +390,15 @@ func (v RpkiValidationResultType) ToInt() int { return -1 } +func (v RpkiValidationResultType) FromInt(i int) RpkiValidationResultType { + for j, vv := range []string{"none", "not-found", "valid", "invalid"} { + if i == j { + return RpkiValidationResultType(vv) + } + } + return RpkiValidationResultType("") +} + func (v RpkiValidationResultType) Validate() error { if v.ToInt() < 0 { return fmt.Errorf("invalid RpkiValidationResultType: %s", v) @@ -334,6 +424,15 @@ func (v BgpOriginAttrType) ToInt() int { return -1 } +func (v BgpOriginAttrType) FromInt(i int) BgpOriginAttrType { + for j, vv := range []string{"igp", "egp", "incomplete"} { + if i == j { + return BgpOriginAttrType(vv) + } + } + return BgpOriginAttrType("") +} + func (v BgpOriginAttrType) Validate() error { if v.ToInt() < 0 { return fmt.Errorf("invalid BgpOriginAttrType: %s", v) diff --git a/table/policy.go b/table/policy.go index e556ec1d..263002c3 100644 --- a/table/policy.go +++ b/table/policy.go @@ -1709,7 +1709,8 @@ func NewCommunityActionFromApiStruct(a *api.CommunityAction) (*CommunityAction, } var list []uint32 var removeList []*regexp.Regexp - op := config.BgpSetCommunityOptionType(a.Type) + var op config.BgpSetCommunityOptionType + op = op.FromInt(int(a.Type)) if op == config.BGP_SET_COMMUNITY_OPTION_TYPE_REMOVE { removeList = make([]*regexp.Regexp, 0, len(a.Communities)) } else { @@ -1828,7 +1829,8 @@ func NewExtCommunityActionFromApiStruct(a *api.CommunityAction) (*ExtCommunityAc var list []bgp.ExtendedCommunityInterface var removeList []*regexp.Regexp subtypeList := make([]bgp.ExtendedCommunityAttrSubType, 0, len(a.Communities)) - op := config.BgpSetCommunityOptionType(a.Type) + var op config.BgpSetCommunityOptionType + op = op.FromInt(int(a.Type)) if op == config.BGP_SET_COMMUNITY_OPTION_TYPE_REMOVE { removeList = make([]*regexp.Regexp, 0, len(a.Communities)) } else { diff --git a/tools/pyang_plugins/bgpyang2golang.py b/tools/pyang_plugins/bgpyang2golang.py index 8acbfa02..5cb09e6a 100644 --- a/tools/pyang_plugins/bgpyang2golang.py +++ b/tools/pyang_plugins/bgpyang2golang.py @@ -412,13 +412,20 @@ def emit_typedef(ctx, module): 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, 'func (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) FromInt(i int) %s {' % (type_name, type_name) + print >> o, 'for j, vv := range []string{%s} {' % (",".join('"%s"' % s.arg.lower() for s in t.substmts)) + print >> o, 'if i == j {return %s(vv)}' % (type_name) + print >> o, '}' + print >> o, 'return %s("")' % (type_name) + 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) |