diff options
author | ISHIDA Wataru <ishida.wataru@lab.ntt.co.jp> | 2016-01-22 23:58:31 +0900 |
---|---|---|
committer | FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp> | 2016-01-25 04:57:11 -0800 |
commit | cca91da0dfe70c2c32d5152b3a2cf76bfdb0f048 (patch) | |
tree | c651f896af8ae8a85a1e928bbc919b9eda4dd6b5 | |
parent | 4ad751b4f01a23af523ba6ea6b183bebb74be041 (diff) |
config: add go type of embeded enums defined in openconfig
these types are embeded enums of openconfig and were left uint32
Signed-off-by: ISHIDA Wataru <ishida.wataru@lab.ntt.co.jp>
-rw-r--r-- | config/bgp_configs.go | 113 | ||||
-rw-r--r-- | server/peer.go | 2 | ||||
-rw-r--r-- | server/server.go | 4 | ||||
-rw-r--r-- | tools/pyang_plugins/bgpyang2golang.py | 14 |
4 files changed, 123 insertions, 10 deletions
diff --git a/config/bgp_configs.go b/config/bgp_configs.go index 24fc2535..78bfdbe2 100644 --- a/config/bgp_configs.go +++ b/config/bgp_configs.go @@ -298,6 +298,39 @@ func (v MatchSetOptionsType) DefaultAsNeeded() MatchSetOptionsType { // typedef for typedef ptypes:tag-type type TagType string +// typedef for typedef rpol:route-type +type RouteType string + +const ( + ROUTE_TYPE_INTERNAL RouteType = "internal" + ROUTE_TYPE_EXTERNAL RouteType = "external" +) + +func (v RouteType) ToInt() int { + for i, vv := range []string{"internal", "external"} { + if string(v) == vv { + return i + } + } + return -1 +} + +func (v RouteType) FromInt(i int) RouteType { + for j, vv := range []string{"internal", "external"} { + if i == j { + return RouteType(vv) + } + } + return RouteType("") +} + +func (v RouteType) Validate() error { + if v.ToInt() < 0 { + return fmt.Errorf("invalid RouteType: %s", v) + } + return nil +} + // typedef for typedef rpol:default-policy-type type DefaultPolicyType string @@ -331,6 +364,77 @@ func (v DefaultPolicyType) Validate() error { return nil } +// typedef for typedef bgp:session-state +type SessionState string + +const ( + SESSION_STATE_IDLE SessionState = "idle" + SESSION_STATE_CONNECT SessionState = "connect" + SESSION_STATE_ACTIVE SessionState = "active" + SESSION_STATE_OPENSENT SessionState = "opensent" + SESSION_STATE_OPENCONFIRM SessionState = "openconfirm" + SESSION_STATE_ESTABLISHED SessionState = "established" +) + +func (v SessionState) ToInt() int { + for i, vv := range []string{"idle", "connect", "active", "opensent", "openconfirm", "established"} { + if string(v) == vv { + return i + } + } + return -1 +} + +func (v SessionState) FromInt(i int) SessionState { + for j, vv := range []string{"idle", "connect", "active", "opensent", "openconfirm", "established"} { + if i == j { + return SessionState(vv) + } + } + return SessionState("") +} + +func (v SessionState) Validate() error { + if v.ToInt() < 0 { + return fmt.Errorf("invalid SessionState: %s", v) + } + return nil +} + +// typedef for typedef bgp:mode +type Mode string + +const ( + MODE_HELPER_ONLY Mode = "helper-only" + MODE_BILATERAL Mode = "bilateral" + MODE_REMOTE_HELPER Mode = "remote-helper" +) + +func (v Mode) ToInt() int { + for i, vv := range []string{"helper-only", "bilateral", "remote-helper"} { + if string(v) == vv { + return i + } + } + return -1 +} + +func (v Mode) FromInt(i int) Mode { + for j, vv := range []string{"helper-only", "bilateral", "remote-helper"} { + if i == j { + return Mode(vv) + } + } + return Mode("") +} + +func (v Mode) Validate() error { + if v.ToInt() < 0 { + return fmt.Errorf("invalid Mode: %s", v) + } + return nil +} + // typedef for typedef bgp-pol:bgp-next-hop-type type BgpNextHopType string @@ -978,8 +1082,7 @@ type NeighborState struct { //bgp:neighbor-address's original type is inet:ip-address NeighborAddress string `mapstructure:"neighbor-address"` // original -> bgp-op:session-state - //bgp-op:session-state's original type is enumeration - SessionState uint32 `mapstructure:"session-state"` + SessionState SessionState `mapstructure:"session-state"` // original -> bgp-op:supported-capabilities // original type is list of identityref SupportedCapabilitiesList []string `mapstructure:"supported-capabilities-list"` @@ -1396,8 +1499,7 @@ type GracefulRestartState struct { //bgp-op:local-restarting's original type is boolean LocalRestarting bool `mapstructure:"local-restarting"` // original -> bgp-op:mode - //bgp-op:mode's original type is enumeration - Mode uint32 `mapstructure:"mode"` + Mode Mode `mapstructure:"mode"` } //struct for container bgp:config @@ -1827,8 +1929,7 @@ type BgpConditions struct { // original -> bgp-pol:as-path-length AsPathLength AsPathLength `mapstructure:"as-path-length"` // original -> bgp-pol:route-type - //bgp-pol:route-type's original type is enumeration - RouteType uint32 `mapstructure:"route-type"` + RouteType RouteType `mapstructure:"route-type"` // original -> gobgp:rpki-validation-result RpkiValidationResult RpkiValidationResultType `mapstructure:"rpki-validation-result"` } diff --git a/server/peer.go b/server/peer.go index ef08a4d8..b8bc9bcb 100644 --- a/server/peer.go +++ b/server/peer.go @@ -56,7 +56,7 @@ func NewPeer(g config.Global, conf config.Neighbor, loc *table.TableManager, pol tableId = conf.Config.NeighborAddress } peer.tableId = tableId - conf.State.SessionState = uint32(bgp.BGP_FSM_IDLE) + conf.State.SessionState = conf.State.SessionState.FromInt(int(bgp.BGP_FSM_IDLE)) conf.Timers.State.Downtime = time.Now().Unix() rfs, _ := config.AfiSafis(conf.AfiSafis).ToRfList() peer.adjRibIn = table.NewAdjRib(peer.ID(), rfs) diff --git a/server/server.go b/server/server.go index 3471ee7e..3a3b70fe 100644 --- a/server/server.go +++ b/server/server.go @@ -910,8 +910,8 @@ func (server *BgpServer) handleFSMMessage(peer *Peer, e *FsmMsg) []*SenderMsg { switch e.MsgType { case FSM_MSG_STATE_CHANGE: nextState := e.MsgData.(bgp.FSMState) - oldState := bgp.FSMState(peer.conf.State.SessionState) - peer.conf.State.SessionState = uint32(nextState) + oldState := bgp.FSMState(peer.conf.State.SessionState.ToInt()) + peer.conf.State.SessionState = peer.conf.State.SessionState.FromInt(int(nextState)) peer.fsm.StateChange(nextState) if oldState == bgp.BGP_FSM_ESTABLISHED { diff --git a/tools/pyang_plugins/bgpyang2golang.py b/tools/pyang_plugins/bgpyang2golang.py index cc2b81f2..64d4d97c 100644 --- a/tools/pyang_plugins/bgpyang2golang.py +++ b/tools/pyang_plugins/bgpyang2golang.py @@ -166,6 +166,9 @@ def emit_class_def(ctx, yang_statement, struct_name, prefix): else: emit_type_name = t.arg + # case embeded enumeration + elif type_name == 'enumeration': + emit_type_name = val_name_go # case translation required elif is_translation_required(type_obj): @@ -290,6 +293,16 @@ def visit_children(ctx, module, children): t = c.search_one('type') + # define container embeded enums + if is_leaf(c) and c.search_one('type').arg == 'enumeration': + prefix = module.i_prefix + c.path = get_path(c) + c.golang_name = convert_to_golang(c.arg) + if prefix in ctx.golang_typedef_map: + ctx.golang_typedef_map[prefix][c.arg] = c + else: + ctx.golang_typedef_map[prefix] = {c.arg: c} + if is_list(c) or is_container(c) or is_choice(c): c.golang_name = convert_to_golang(c.uniq_name) @@ -542,7 +555,6 @@ def is_translation_required(t): _type_translation_map = { 'union': 'string', - 'enumeration': 'uint32', 'decimal64': 'float64', 'boolean': 'bool', 'empty': 'bool', |