diff options
author | ISHIDA Wataru <ishida.wataru@lab.ntt.co.jp> | 2016-01-23 17:18:56 +0900 |
---|---|---|
committer | FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp> | 2016-01-25 04:57:11 -0800 |
commit | d94f5d2c03b8eb265cfa1af5dce43da7194538b8 (patch) | |
tree | 94872abec34c97a19c9abe457b10217d8c44964a | |
parent | 3817b82f9d57ce02adb025f5acff6f06b54c6641 (diff) |
config: add openconfig identity structs in bgp_configs.go
Signed-off-by: ISHIDA Wataru <ishida.wataru@lab.ntt.co.jp>
-rw-r--r-- | config/bgp_configs.go | 695 | ||||
-rw-r--r-- | config/default.go | 1 | ||||
-rw-r--r-- | config/util.go | 5 | ||||
-rw-r--r-- | gobgp/cmd/policy.go | 3 | ||||
-rw-r--r-- | packet/constant.go | 3 | ||||
-rw-r--r-- | packet/fsmstate_string.go | 7 | ||||
-rw-r--r-- | server/fsm.go | 4 | ||||
-rw-r--r-- | server/peer.go | 2 | ||||
-rw-r--r-- | server/server.go | 8 | ||||
-rw-r--r-- | table/policy.go | 23 | ||||
-rw-r--r-- | tools/pyang_plugins/bgpyang2golang.py | 151 | ||||
-rw-r--r-- | tools/pyang_plugins/gobgp.yang | 69 |
12 files changed, 663 insertions, 308 deletions
diff --git a/config/bgp_configs.go b/config/bgp_configs.go index 78bfdbe2..c6a3e53e 100644 --- a/config/bgp_configs.go +++ b/config/bgp_configs.go @@ -29,7 +29,7 @@ type Percentage uint8 // typedef for typedef bgp-types:rr-cluster-id-type type RrClusterIdType string -// typedef for typedef bgp-types:remove-private-as-option +// typedef for identity bgp-types:remove-private-as-option type RemovePrivateAsOption string const ( @@ -37,26 +37,26 @@ const ( REMOVE_PRIVATE_AS_OPTION_REPLACE RemovePrivateAsOption = "replace" ) +var RemovePrivateAsOptionToIntMap = map[RemovePrivateAsOption]int{ + REMOVE_PRIVATE_AS_OPTION_ALL: 0, + REMOVE_PRIVATE_AS_OPTION_REPLACE: 1, +} + func (v RemovePrivateAsOption) ToInt() int { - for i, vv := range []string{"all", "replace"} { - if string(v) == vv { - return i - } + i, ok := RemovePrivateAsOptionToIntMap[v] + if !ok { + return -1 } - return -1 + return i } -func (v RemovePrivateAsOption) FromInt(i int) RemovePrivateAsOption { - for j, vv := range []string{"all", "replace"} { - if i == j { - return RemovePrivateAsOption(vv) - } - } - return RemovePrivateAsOption("") +var IntToRemovePrivateAsOptionMap = map[int]RemovePrivateAsOption{ + 0: REMOVE_PRIVATE_AS_OPTION_ALL, + 1: REMOVE_PRIVATE_AS_OPTION_REPLACE, } func (v RemovePrivateAsOption) Validate() error { - if v.ToInt() < 0 { + if _, ok := RemovePrivateAsOptionToIntMap[v]; !ok { return fmt.Errorf("invalid RemovePrivateAsOption: %s", v) } return nil @@ -65,7 +65,7 @@ func (v RemovePrivateAsOption) Validate() error { // typedef for typedef bgp-types:bgp-community-regexp-type type BgpCommunityRegexpType StdRegexp -// typedef for typedef bgp-types:community-type +// typedef for identity bgp-types:community-type type CommunityType string const ( @@ -75,26 +75,30 @@ const ( COMMUNITY_TYPE_NONE CommunityType = "none" ) +var CommunityTypeToIntMap = map[CommunityType]int{ + COMMUNITY_TYPE_STANDARD: 0, + COMMUNITY_TYPE_EXTENDED: 1, + COMMUNITY_TYPE_BOTH: 2, + COMMUNITY_TYPE_NONE: 3, +} + func (v CommunityType) ToInt() int { - for i, vv := range []string{"standard", "extended", "both", "none"} { - if string(v) == vv { - return i - } + i, ok := CommunityTypeToIntMap[v] + if !ok { + return -1 } - return -1 + return i } -func (v CommunityType) FromInt(i int) CommunityType { - for j, vv := range []string{"standard", "extended", "both", "none"} { - if i == j { - return CommunityType(vv) - } - } - return CommunityType("") +var IntToCommunityTypeMap = map[int]CommunityType{ + 0: COMMUNITY_TYPE_STANDARD, + 1: COMMUNITY_TYPE_EXTENDED, + 2: COMMUNITY_TYPE_BOTH, + 3: COMMUNITY_TYPE_NONE, } func (v CommunityType) Validate() error { - if v.ToInt() < 0 { + if _, ok := CommunityTypeToIntMap[v]; !ok { return fmt.Errorf("invalid CommunityType: %s", v) } return nil @@ -106,7 +110,7 @@ type BgpExtCommunityType string // typedef for typedef bgp-types:bgp-std-community-type type BgpStdCommunityType string -// typedef for typedef bgp-types:peer-type +// typedef for identity bgp-types:peer-type type PeerType string const ( @@ -114,32 +118,32 @@ const ( PEER_TYPE_EXTERNAL PeerType = "external" ) +var PeerTypeToIntMap = map[PeerType]int{ + PEER_TYPE_INTERNAL: 0, + PEER_TYPE_EXTERNAL: 1, +} + func (v PeerType) ToInt() int { - for i, vv := range []string{"internal", "external"} { - if string(v) == vv { - return i - } + i, ok := PeerTypeToIntMap[v] + if !ok { + return -1 } - return -1 + return i } -func (v PeerType) FromInt(i int) PeerType { - for j, vv := range []string{"internal", "external"} { - if i == j { - return PeerType(vv) - } - } - return PeerType("") +var IntToPeerTypeMap = map[int]PeerType{ + 0: PEER_TYPE_INTERNAL, + 1: PEER_TYPE_EXTERNAL, } func (v PeerType) Validate() error { - if v.ToInt() < 0 { + if _, ok := PeerTypeToIntMap[v]; !ok { return fmt.Errorf("invalid PeerType: %s", v) } return nil } -// typedef for typedef bgp-types:bgp-session-direction +// typedef for identity bgp-types:bgp-session-direction type BgpSessionDirection string const ( @@ -147,32 +151,32 @@ const ( BGP_SESSION_DIRECTION_OUTBOUND BgpSessionDirection = "outbound" ) +var BgpSessionDirectionToIntMap = map[BgpSessionDirection]int{ + BGP_SESSION_DIRECTION_INBOUND: 0, + BGP_SESSION_DIRECTION_OUTBOUND: 1, +} + func (v BgpSessionDirection) ToInt() int { - for i, vv := range []string{"inbound", "outbound"} { - if string(v) == vv { - return i - } + i, ok := BgpSessionDirectionToIntMap[v] + if !ok { + return -1 } - return -1 + return i } -func (v BgpSessionDirection) FromInt(i int) BgpSessionDirection { - for j, vv := range []string{"inbound", "outbound"} { - if i == j { - return BgpSessionDirection(vv) - } - } - return BgpSessionDirection("") +var IntToBgpSessionDirectionMap = map[int]BgpSessionDirection{ + 0: BGP_SESSION_DIRECTION_INBOUND, + 1: BGP_SESSION_DIRECTION_OUTBOUND, } func (v BgpSessionDirection) Validate() error { - if v.ToInt() < 0 { + if _, ok := BgpSessionDirectionToIntMap[v]; !ok { return fmt.Errorf("invalid BgpSessionDirection: %s", v) } return nil } -// typedef for typedef bgp-types:bgp-origin-attr-type +// typedef for identity bgp-types:bgp-origin-attr-type type BgpOriginAttrType string const ( @@ -181,32 +185,196 @@ const ( BGP_ORIGIN_ATTR_TYPE_INCOMPLETE BgpOriginAttrType = "incomplete" ) +var BgpOriginAttrTypeToIntMap = map[BgpOriginAttrType]int{ + BGP_ORIGIN_ATTR_TYPE_IGP: 0, + BGP_ORIGIN_ATTR_TYPE_EGP: 1, + BGP_ORIGIN_ATTR_TYPE_INCOMPLETE: 2, +} + func (v BgpOriginAttrType) ToInt() int { - for i, vv := range []string{"igp", "egp", "incomplete"} { - if string(v) == vv { - return i - } + i, ok := BgpOriginAttrTypeToIntMap[v] + if !ok { + return -1 } - return -1 + return i } -func (v BgpOriginAttrType) FromInt(i int) BgpOriginAttrType { - for j, vv := range []string{"igp", "egp", "incomplete"} { - if i == j { - return BgpOriginAttrType(vv) - } - } - return BgpOriginAttrType("") +var IntToBgpOriginAttrTypeMap = map[int]BgpOriginAttrType{ + 0: BGP_ORIGIN_ATTR_TYPE_IGP, + 1: BGP_ORIGIN_ATTR_TYPE_EGP, + 2: BGP_ORIGIN_ATTR_TYPE_INCOMPLETE, } func (v BgpOriginAttrType) Validate() error { - if v.ToInt() < 0 { + if _, ok := BgpOriginAttrTypeToIntMap[v]; !ok { return fmt.Errorf("invalid BgpOriginAttrType: %s", v) } return nil } -// typedef for typedef ptypes:match-set-options-restricted-type +// typedef for identity bgp-types:afi-safi-type +type AfiSafiType string + +const ( + AFI_SAFI_TYPE_IPV4_UNICAST AfiSafiType = "ipv4-unicast" + AFI_SAFI_TYPE_IPV6_UNICAST AfiSafiType = "ipv6-unicast" + AFI_SAFI_TYPE_IPV4_LABELLED_UNICAST AfiSafiType = "ipv4-labelled-unicast" + AFI_SAFI_TYPE_IPV6_LABELLED_UNICAST AfiSafiType = "ipv6-labelled-unicast" + AFI_SAFI_TYPE_L3VPN_IPV4_UNICAST AfiSafiType = "l3vpn-ipv4-unicast" + AFI_SAFI_TYPE_L3VPN_IPV6_UNICAST AfiSafiType = "l3vpn-ipv6-unicast" + AFI_SAFI_TYPE_L3VPN_IPV4_MULTICAST AfiSafiType = "l3vpn-ipv4-multicast" + AFI_SAFI_TYPE_L3VPN_IPV6_MULTICAST AfiSafiType = "l3vpn-ipv6-multicast" + AFI_SAFI_TYPE_L2VPN_VPLS AfiSafiType = "l2vpn-vpls" + AFI_SAFI_TYPE_L2VPN_EVPN AfiSafiType = "l2vpn-evpn" + AFI_SAFI_TYPE_IPV4_MULTICAST AfiSafiType = "ipv4-multicast" + AFI_SAFI_TYPE_IPV6_MULTICAST AfiSafiType = "ipv6-multicast" + AFI_SAFI_TYPE_RTC AfiSafiType = "rtc" + AFI_SAFI_TYPE_ENCAP AfiSafiType = "encap" + AFI_SAFI_TYPE_IPV4_FLOWSPEC AfiSafiType = "ipv4-flowspec" + AFI_SAFI_TYPE_L3VPN_IPV4_FLOWSPEC AfiSafiType = "l3vpn-ipv4-flowspec" + AFI_SAFI_TYPE_IPV6_FLOWSPEC AfiSafiType = "ipv6-flowspec" + AFI_SAFI_TYPE_L3VPN_IPV6_FLOWSPEC AfiSafiType = "l3vpn-ipv6-flowspec" +) + +var AfiSafiTypeToIntMap = map[AfiSafiType]int{ + AFI_SAFI_TYPE_IPV4_UNICAST: 0, + AFI_SAFI_TYPE_IPV6_UNICAST: 1, + AFI_SAFI_TYPE_IPV4_LABELLED_UNICAST: 2, + AFI_SAFI_TYPE_IPV6_LABELLED_UNICAST: 3, + AFI_SAFI_TYPE_L3VPN_IPV4_UNICAST: 4, + AFI_SAFI_TYPE_L3VPN_IPV6_UNICAST: 5, + AFI_SAFI_TYPE_L3VPN_IPV4_MULTICAST: 6, + AFI_SAFI_TYPE_L3VPN_IPV6_MULTICAST: 7, + AFI_SAFI_TYPE_L2VPN_VPLS: 8, + AFI_SAFI_TYPE_L2VPN_EVPN: 9, + AFI_SAFI_TYPE_IPV4_MULTICAST: 10, + AFI_SAFI_TYPE_IPV6_MULTICAST: 11, + AFI_SAFI_TYPE_RTC: 12, + AFI_SAFI_TYPE_ENCAP: 13, + AFI_SAFI_TYPE_IPV4_FLOWSPEC: 14, + AFI_SAFI_TYPE_L3VPN_IPV4_FLOWSPEC: 15, + AFI_SAFI_TYPE_IPV6_FLOWSPEC: 16, + AFI_SAFI_TYPE_L3VPN_IPV6_FLOWSPEC: 17, +} + +func (v AfiSafiType) ToInt() int { + i, ok := AfiSafiTypeToIntMap[v] + if !ok { + return -1 + } + return i +} + +var IntToAfiSafiTypeMap = map[int]AfiSafiType{ + 0: AFI_SAFI_TYPE_IPV4_UNICAST, + 1: AFI_SAFI_TYPE_IPV6_UNICAST, + 2: AFI_SAFI_TYPE_IPV4_LABELLED_UNICAST, + 3: AFI_SAFI_TYPE_IPV6_LABELLED_UNICAST, + 4: AFI_SAFI_TYPE_L3VPN_IPV4_UNICAST, + 5: AFI_SAFI_TYPE_L3VPN_IPV6_UNICAST, + 6: AFI_SAFI_TYPE_L3VPN_IPV4_MULTICAST, + 7: AFI_SAFI_TYPE_L3VPN_IPV6_MULTICAST, + 8: AFI_SAFI_TYPE_L2VPN_VPLS, + 9: AFI_SAFI_TYPE_L2VPN_EVPN, + 10: AFI_SAFI_TYPE_IPV4_MULTICAST, + 11: AFI_SAFI_TYPE_IPV6_MULTICAST, + 12: AFI_SAFI_TYPE_RTC, + 13: AFI_SAFI_TYPE_ENCAP, + 14: AFI_SAFI_TYPE_IPV4_FLOWSPEC, + 15: AFI_SAFI_TYPE_L3VPN_IPV4_FLOWSPEC, + 16: AFI_SAFI_TYPE_IPV6_FLOWSPEC, + 17: AFI_SAFI_TYPE_L3VPN_IPV6_FLOWSPEC, +} + +func (v AfiSafiType) Validate() error { + if _, ok := AfiSafiTypeToIntMap[v]; !ok { + return fmt.Errorf("invalid AfiSafiType: %s", v) + } + return nil +} + +// typedef for identity bgp-types:bgp-capability +type BgpCapability string + +const ( + BGP_CAPABILITY_MPBGP BgpCapability = "mpbgp" + BGP_CAPABILITY_ROUTE_REFRESH BgpCapability = "route-refresh" + BGP_CAPABILITY_ASN32 BgpCapability = "asn32" + BGP_CAPABILITY_GRACEFUL_RESTART BgpCapability = "graceful-restart" + BGP_CAPABILITY_ADD_PATHS BgpCapability = "add-paths" +) + +var BgpCapabilityToIntMap = map[BgpCapability]int{ + BGP_CAPABILITY_MPBGP: 0, + BGP_CAPABILITY_ROUTE_REFRESH: 1, + BGP_CAPABILITY_ASN32: 2, + BGP_CAPABILITY_GRACEFUL_RESTART: 3, + BGP_CAPABILITY_ADD_PATHS: 4, +} + +func (v BgpCapability) ToInt() int { + i, ok := BgpCapabilityToIntMap[v] + if !ok { + return -1 + } + return i +} + +var IntToBgpCapabilityMap = map[int]BgpCapability{ + 0: BGP_CAPABILITY_MPBGP, + 1: BGP_CAPABILITY_ROUTE_REFRESH, + 2: BGP_CAPABILITY_ASN32, + 3: BGP_CAPABILITY_GRACEFUL_RESTART, + 4: BGP_CAPABILITY_ADD_PATHS, +} + +func (v BgpCapability) Validate() error { + if _, ok := BgpCapabilityToIntMap[v]; !ok { + return fmt.Errorf("invalid BgpCapability: %s", v) + } + return nil +} + +// typedef for identity bgp-types:bgp-well-known-std-community +type BgpWellKnownStdCommunity string + +const ( + BGP_WELL_KNOWN_STD_COMMUNITY_NO_EXPORT BgpWellKnownStdCommunity = "no_export" + BGP_WELL_KNOWN_STD_COMMUNITY_NO_ADVERTISE BgpWellKnownStdCommunity = "no_advertise" + BGP_WELL_KNOWN_STD_COMMUNITY_NO_EXPORT_SUBCONFED BgpWellKnownStdCommunity = "no_export_subconfed" + BGP_WELL_KNOWN_STD_COMMUNITY_NOPEER BgpWellKnownStdCommunity = "nopeer" +) + +var BgpWellKnownStdCommunityToIntMap = map[BgpWellKnownStdCommunity]int{ + BGP_WELL_KNOWN_STD_COMMUNITY_NO_EXPORT: 0, + BGP_WELL_KNOWN_STD_COMMUNITY_NO_ADVERTISE: 1, + BGP_WELL_KNOWN_STD_COMMUNITY_NO_EXPORT_SUBCONFED: 2, + BGP_WELL_KNOWN_STD_COMMUNITY_NOPEER: 3, +} + +func (v BgpWellKnownStdCommunity) ToInt() int { + i, ok := BgpWellKnownStdCommunityToIntMap[v] + if !ok { + return -1 + } + return i +} + +var IntToBgpWellKnownStdCommunityMap = map[int]BgpWellKnownStdCommunity{ + 0: BGP_WELL_KNOWN_STD_COMMUNITY_NO_EXPORT, + 1: BGP_WELL_KNOWN_STD_COMMUNITY_NO_ADVERTISE, + 2: BGP_WELL_KNOWN_STD_COMMUNITY_NO_EXPORT_SUBCONFED, + 3: BGP_WELL_KNOWN_STD_COMMUNITY_NOPEER, +} + +func (v BgpWellKnownStdCommunity) Validate() error { + if _, ok := BgpWellKnownStdCommunityToIntMap[v]; !ok { + return fmt.Errorf("invalid BgpWellKnownStdCommunity: %s", v) + } + return nil +} + +// typedef for identity ptypes:match-set-options-restricted-type type MatchSetOptionsRestrictedType string const ( @@ -214,26 +382,26 @@ const ( MATCH_SET_OPTIONS_RESTRICTED_TYPE_INVERT MatchSetOptionsRestrictedType = "invert" ) +var MatchSetOptionsRestrictedTypeToIntMap = map[MatchSetOptionsRestrictedType]int{ + MATCH_SET_OPTIONS_RESTRICTED_TYPE_ANY: 0, + MATCH_SET_OPTIONS_RESTRICTED_TYPE_INVERT: 1, +} + func (v MatchSetOptionsRestrictedType) ToInt() int { - for i, vv := range []string{"any", "invert"} { - if string(v) == vv { - return i - } + i, ok := MatchSetOptionsRestrictedTypeToIntMap[v] + if !ok { + return -1 } - return -1 + return i } -func (v MatchSetOptionsRestrictedType) FromInt(i int) MatchSetOptionsRestrictedType { - for j, vv := range []string{"any", "invert"} { - if i == j { - return MatchSetOptionsRestrictedType(vv) - } - } - return MatchSetOptionsRestrictedType("") +var IntToMatchSetOptionsRestrictedTypeMap = map[int]MatchSetOptionsRestrictedType{ + 0: MATCH_SET_OPTIONS_RESTRICTED_TYPE_ANY, + 1: MATCH_SET_OPTIONS_RESTRICTED_TYPE_INVERT, } func (v MatchSetOptionsRestrictedType) Validate() error { - if v.ToInt() < 0 { + if _, ok := MatchSetOptionsRestrictedTypeToIntMap[v]; !ok { return fmt.Errorf("invalid MatchSetOptionsRestrictedType: %s", v) } return nil @@ -250,7 +418,7 @@ func (v MatchSetOptionsRestrictedType) DefaultAsNeeded() MatchSetOptionsRestrict return v } -// typedef for typedef ptypes:match-set-options-type +// typedef for identity ptypes:match-set-options-type type MatchSetOptionsType string const ( @@ -259,26 +427,28 @@ const ( MATCH_SET_OPTIONS_TYPE_INVERT MatchSetOptionsType = "invert" ) +var MatchSetOptionsTypeToIntMap = map[MatchSetOptionsType]int{ + MATCH_SET_OPTIONS_TYPE_ANY: 0, + MATCH_SET_OPTIONS_TYPE_ALL: 1, + MATCH_SET_OPTIONS_TYPE_INVERT: 2, +} + func (v MatchSetOptionsType) ToInt() int { - for i, vv := range []string{"any", "all", "invert"} { - if string(v) == vv { - return i - } + i, ok := MatchSetOptionsTypeToIntMap[v] + if !ok { + return -1 } - return -1 + return i } -func (v MatchSetOptionsType) FromInt(i int) MatchSetOptionsType { - for j, vv := range []string{"any", "all", "invert"} { - if i == j { - return MatchSetOptionsType(vv) - } - } - return MatchSetOptionsType("") +var IntToMatchSetOptionsTypeMap = map[int]MatchSetOptionsType{ + 0: MATCH_SET_OPTIONS_TYPE_ANY, + 1: MATCH_SET_OPTIONS_TYPE_ALL, + 2: MATCH_SET_OPTIONS_TYPE_INVERT, } func (v MatchSetOptionsType) Validate() error { - if v.ToInt() < 0 { + if _, ok := MatchSetOptionsTypeToIntMap[v]; !ok { return fmt.Errorf("invalid MatchSetOptionsType: %s", v) } return nil @@ -298,7 +468,100 @@ func (v MatchSetOptionsType) DefaultAsNeeded() MatchSetOptionsType { // typedef for typedef ptypes:tag-type type TagType string -// typedef for typedef rpol:route-type +// typedef for identity ptypes:install-protocol-type +type InstallProtocolType string + +const ( + INSTALL_PROTOCOL_TYPE_BGP InstallProtocolType = "bgp" + INSTALL_PROTOCOL_TYPE_ISIS InstallProtocolType = "isis" + INSTALL_PROTOCOL_TYPE_OSPF InstallProtocolType = "ospf" + INSTALL_PROTOCOL_TYPE_OSPF3 InstallProtocolType = "ospf3" + INSTALL_PROTOCOL_TYPE_STATIC InstallProtocolType = "static" + INSTALL_PROTOCOL_TYPE_DIRECTLY_CONNECTED InstallProtocolType = "directly-connected" + INSTALL_PROTOCOL_TYPE_LOCAL_AGGREGATE InstallProtocolType = "local-aggregate" +) + +var InstallProtocolTypeToIntMap = map[InstallProtocolType]int{ + INSTALL_PROTOCOL_TYPE_BGP: 0, + INSTALL_PROTOCOL_TYPE_ISIS: 1, + INSTALL_PROTOCOL_TYPE_OSPF: 2, + INSTALL_PROTOCOL_TYPE_OSPF3: 3, + INSTALL_PROTOCOL_TYPE_STATIC: 4, + INSTALL_PROTOCOL_TYPE_DIRECTLY_CONNECTED: 5, + INSTALL_PROTOCOL_TYPE_LOCAL_AGGREGATE: 6, +} + +func (v InstallProtocolType) ToInt() int { + i, ok := InstallProtocolTypeToIntMap[v] + if !ok { + return -1 + } + return i +} + +var IntToInstallProtocolTypeMap = map[int]InstallProtocolType{ + 0: INSTALL_PROTOCOL_TYPE_BGP, + 1: INSTALL_PROTOCOL_TYPE_ISIS, + 2: INSTALL_PROTOCOL_TYPE_OSPF, + 3: INSTALL_PROTOCOL_TYPE_OSPF3, + 4: INSTALL_PROTOCOL_TYPE_STATIC, + 5: INSTALL_PROTOCOL_TYPE_DIRECTLY_CONNECTED, + 6: INSTALL_PROTOCOL_TYPE_LOCAL_AGGREGATE, +} + +func (v InstallProtocolType) Validate() error { + if _, ok := InstallProtocolTypeToIntMap[v]; !ok { + return fmt.Errorf("invalid InstallProtocolType: %s", v) + } + return nil +} + +// typedef for identity ptypes:attribute-comparison +type AttributeComparison string + +const ( + ATTRIBUTE_COMPARISON_ATTRIBUTE_EQ AttributeComparison = "attribute-eq" + ATTRIBUTE_COMPARISON_ATTRIBUTE_GE AttributeComparison = "attribute-ge" + ATTRIBUTE_COMPARISON_ATTRIBUTE_LE AttributeComparison = "attribute-le" + ATTRIBUTE_COMPARISON_EQ AttributeComparison = "eq" + ATTRIBUTE_COMPARISON_GE AttributeComparison = "ge" + ATTRIBUTE_COMPARISON_LE AttributeComparison = "le" +) + +var AttributeComparisonToIntMap = map[AttributeComparison]int{ + ATTRIBUTE_COMPARISON_ATTRIBUTE_EQ: 0, + ATTRIBUTE_COMPARISON_ATTRIBUTE_GE: 1, + ATTRIBUTE_COMPARISON_ATTRIBUTE_LE: 2, + ATTRIBUTE_COMPARISON_EQ: 3, + ATTRIBUTE_COMPARISON_GE: 4, + ATTRIBUTE_COMPARISON_LE: 5, +} + +func (v AttributeComparison) ToInt() int { + i, ok := AttributeComparisonToIntMap[v] + if !ok { + return -1 + } + return i +} + +var IntToAttributeComparisonMap = map[int]AttributeComparison{ + 0: ATTRIBUTE_COMPARISON_ATTRIBUTE_EQ, + 1: ATTRIBUTE_COMPARISON_ATTRIBUTE_GE, + 2: ATTRIBUTE_COMPARISON_ATTRIBUTE_LE, + 3: ATTRIBUTE_COMPARISON_EQ, + 4: ATTRIBUTE_COMPARISON_GE, + 5: ATTRIBUTE_COMPARISON_LE, +} + +func (v AttributeComparison) Validate() error { + if _, ok := AttributeComparisonToIntMap[v]; !ok { + return fmt.Errorf("invalid AttributeComparison: %s", v) + } + return nil +} + +// typedef for identity rpol:route-type type RouteType string const ( @@ -306,32 +569,32 @@ const ( ROUTE_TYPE_EXTERNAL RouteType = "external" ) +var RouteTypeToIntMap = map[RouteType]int{ + ROUTE_TYPE_INTERNAL: 0, + ROUTE_TYPE_EXTERNAL: 1, +} + func (v RouteType) ToInt() int { - for i, vv := range []string{"internal", "external"} { - if string(v) == vv { - return i - } + i, ok := RouteTypeToIntMap[v] + if !ok { + return -1 } - return -1 + return i } -func (v RouteType) FromInt(i int) RouteType { - for j, vv := range []string{"internal", "external"} { - if i == j { - return RouteType(vv) - } - } - return RouteType("") +var IntToRouteTypeMap = map[int]RouteType{ + 0: ROUTE_TYPE_INTERNAL, + 1: ROUTE_TYPE_EXTERNAL, } func (v RouteType) Validate() error { - if v.ToInt() < 0 { + if _, ok := RouteTypeToIntMap[v]; !ok { return fmt.Errorf("invalid RouteType: %s", v) } return nil } -// typedef for typedef rpol:default-policy-type +// typedef for identity rpol:default-policy-type type DefaultPolicyType string const ( @@ -339,32 +602,32 @@ const ( DEFAULT_POLICY_TYPE_REJECT_ROUTE DefaultPolicyType = "reject-route" ) +var DefaultPolicyTypeToIntMap = map[DefaultPolicyType]int{ + DEFAULT_POLICY_TYPE_ACCEPT_ROUTE: 0, + DEFAULT_POLICY_TYPE_REJECT_ROUTE: 1, +} + func (v DefaultPolicyType) ToInt() int { - for i, vv := range []string{"accept-route", "reject-route"} { - if string(v) == vv { - return i - } + i, ok := DefaultPolicyTypeToIntMap[v] + if !ok { + return -1 } - return -1 + return i } -func (v DefaultPolicyType) FromInt(i int) DefaultPolicyType { - for j, vv := range []string{"accept-route", "reject-route"} { - if i == j { - return DefaultPolicyType(vv) - } - } - return DefaultPolicyType("") +var IntToDefaultPolicyTypeMap = map[int]DefaultPolicyType{ + 0: DEFAULT_POLICY_TYPE_ACCEPT_ROUTE, + 1: DEFAULT_POLICY_TYPE_REJECT_ROUTE, } func (v DefaultPolicyType) Validate() error { - if v.ToInt() < 0 { + if _, ok := DefaultPolicyTypeToIntMap[v]; !ok { return fmt.Errorf("invalid DefaultPolicyType: %s", v) } return nil } -// typedef for typedef bgp:session-state +// typedef for identity bgp:session-state type SessionState string const ( @@ -376,32 +639,40 @@ const ( SESSION_STATE_ESTABLISHED SessionState = "established" ) +var SessionStateToIntMap = map[SessionState]int{ + SESSION_STATE_IDLE: 0, + SESSION_STATE_CONNECT: 1, + SESSION_STATE_ACTIVE: 2, + SESSION_STATE_OPENSENT: 3, + SESSION_STATE_OPENCONFIRM: 4, + SESSION_STATE_ESTABLISHED: 5, +} + func (v SessionState) ToInt() int { - for i, vv := range []string{"idle", "connect", "active", "opensent", "openconfirm", "established"} { - if string(v) == vv { - return i - } + i, ok := SessionStateToIntMap[v] + if !ok { + return -1 } - return -1 + return i } -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("") +var IntToSessionStateMap = map[int]SessionState{ + 0: SESSION_STATE_IDLE, + 1: SESSION_STATE_CONNECT, + 2: SESSION_STATE_ACTIVE, + 3: SESSION_STATE_OPENSENT, + 4: SESSION_STATE_OPENCONFIRM, + 5: SESSION_STATE_ESTABLISHED, } func (v SessionState) Validate() error { - if v.ToInt() < 0 { + if _, ok := SessionStateToIntMap[v]; !ok { return fmt.Errorf("invalid SessionState: %s", v) } return nil } -// typedef for typedef bgp:mode +// typedef for identity bgp:mode type Mode string const ( @@ -410,26 +681,28 @@ const ( MODE_REMOTE_HELPER Mode = "remote-helper" ) +var ModeToIntMap = map[Mode]int{ + MODE_HELPER_ONLY: 0, + MODE_BILATERAL: 1, + MODE_REMOTE_HELPER: 2, +} + func (v Mode) ToInt() int { - for i, vv := range []string{"helper-only", "bilateral", "remote-helper"} { - if string(v) == vv { - return i - } + i, ok := ModeToIntMap[v] + if !ok { + return -1 } - return -1 + return i } -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("") +var IntToModeMap = map[int]Mode{ + 0: MODE_HELPER_ONLY, + 1: MODE_BILATERAL, + 2: MODE_REMOTE_HELPER, } func (v Mode) Validate() error { - if v.ToInt() < 0 { + if _, ok := ModeToIntMap[v]; !ok { return fmt.Errorf("invalid Mode: %s", v) } return nil @@ -444,7 +717,7 @@ type BgpAsPathPrependRepeat uint8 // typedef for typedef bgp-pol:bgp-set-med-type type BgpSetMedType string -// typedef for typedef bgp-pol:bgp-set-community-option-type +// typedef for identity bgp-pol:bgp-set-community-option-type type BgpSetCommunityOptionType string const ( @@ -453,32 +726,34 @@ const ( BGP_SET_COMMUNITY_OPTION_TYPE_REPLACE BgpSetCommunityOptionType = "replace" ) +var BgpSetCommunityOptionTypeToIntMap = map[BgpSetCommunityOptionType]int{ + BGP_SET_COMMUNITY_OPTION_TYPE_ADD: 0, + BGP_SET_COMMUNITY_OPTION_TYPE_REMOVE: 1, + BGP_SET_COMMUNITY_OPTION_TYPE_REPLACE: 2, +} + func (v BgpSetCommunityOptionType) ToInt() int { - for i, vv := range []string{"add", "remove", "replace"} { - if string(v) == vv { - return i - } + i, ok := BgpSetCommunityOptionTypeToIntMap[v] + if !ok { + return -1 } - return -1 + return i } -func (v BgpSetCommunityOptionType) FromInt(i int) BgpSetCommunityOptionType { - for j, vv := range []string{"add", "remove", "replace"} { - if i == j { - return BgpSetCommunityOptionType(vv) - } - } - return BgpSetCommunityOptionType("") +var IntToBgpSetCommunityOptionTypeMap = map[int]BgpSetCommunityOptionType{ + 0: BGP_SET_COMMUNITY_OPTION_TYPE_ADD, + 1: BGP_SET_COMMUNITY_OPTION_TYPE_REMOVE, + 2: BGP_SET_COMMUNITY_OPTION_TYPE_REPLACE, } func (v BgpSetCommunityOptionType) Validate() error { - if v.ToInt() < 0 { + if _, ok := BgpSetCommunityOptionTypeToIntMap[v]; !ok { return fmt.Errorf("invalid BgpSetCommunityOptionType: %s", v) } return nil } -// typedef for typedef gobgp:bmp-route-monitoring-policy-type +// typedef for identity gobgp:bmp-route-monitoring-policy-type type BmpRouteMonitoringPolicyType string const ( @@ -487,32 +762,34 @@ const ( BMP_ROUTE_MONITORING_POLICY_TYPE_BOTH BmpRouteMonitoringPolicyType = "both" ) +var BmpRouteMonitoringPolicyTypeToIntMap = map[BmpRouteMonitoringPolicyType]int{ + BMP_ROUTE_MONITORING_POLICY_TYPE_PRE_POLICY: 0, + BMP_ROUTE_MONITORING_POLICY_TYPE_POST_POLICY: 1, + BMP_ROUTE_MONITORING_POLICY_TYPE_BOTH: 2, +} + func (v BmpRouteMonitoringPolicyType) ToInt() int { - for i, vv := range []string{"pre-policy", "post-policy", "both"} { - if string(v) == vv { - return i - } + i, ok := BmpRouteMonitoringPolicyTypeToIntMap[v] + if !ok { + return -1 } - return -1 + return i } -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("") +var IntToBmpRouteMonitoringPolicyTypeMap = map[int]BmpRouteMonitoringPolicyType{ + 0: BMP_ROUTE_MONITORING_POLICY_TYPE_PRE_POLICY, + 1: BMP_ROUTE_MONITORING_POLICY_TYPE_POST_POLICY, + 2: BMP_ROUTE_MONITORING_POLICY_TYPE_BOTH, } func (v BmpRouteMonitoringPolicyType) Validate() error { - if v.ToInt() < 0 { + if _, ok := BmpRouteMonitoringPolicyTypeToIntMap[v]; !ok { return fmt.Errorf("invalid BmpRouteMonitoringPolicyType: %s", v) } return nil } -// typedef for typedef gobgp:rpki-validation-result-type +// typedef for identity gobgp:rpki-validation-result-type type RpkiValidationResultType string const ( @@ -522,26 +799,30 @@ const ( RPKI_VALIDATION_RESULT_TYPE_INVALID RpkiValidationResultType = "invalid" ) +var RpkiValidationResultTypeToIntMap = map[RpkiValidationResultType]int{ + RPKI_VALIDATION_RESULT_TYPE_NONE: 0, + RPKI_VALIDATION_RESULT_TYPE_NOT_FOUND: 1, + RPKI_VALIDATION_RESULT_TYPE_VALID: 2, + RPKI_VALIDATION_RESULT_TYPE_INVALID: 3, +} + func (v RpkiValidationResultType) ToInt() int { - for i, vv := range []string{"none", "not-found", "valid", "invalid"} { - if string(v) == vv { - return i - } + i, ok := RpkiValidationResultTypeToIntMap[v] + if !ok { + return -1 } - return -1 + return i } -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("") +var IntToRpkiValidationResultTypeMap = map[int]RpkiValidationResultType{ + 0: RPKI_VALIDATION_RESULT_TYPE_NONE, + 1: RPKI_VALIDATION_RESULT_TYPE_NOT_FOUND, + 2: RPKI_VALIDATION_RESULT_TYPE_VALID, + 3: RPKI_VALIDATION_RESULT_TYPE_INVALID, } func (v RpkiValidationResultType) Validate() error { - if v.ToInt() < 0 { + if _, ok := RpkiValidationResultTypeToIntMap[v]; !ok { return fmt.Errorf("invalid RpkiValidationResultType: %s", v) } return nil @@ -1084,8 +1365,7 @@ type NeighborState struct { // original -> bgp-op:session-state SessionState SessionState `mapstructure:"session-state"` // original -> bgp-op:supported-capabilities - // original type is list of identityref - SupportedCapabilitiesList []string `mapstructure:"supported-capabilities-list"` + SupportedCapabilitiesList []BgpCapability `mapstructure:"supported-capabilities-list"` // original -> bgp:messages Messages Messages `mapstructure:"messages"` // original -> bgp:queues @@ -1186,8 +1466,7 @@ type Zebra struct { // original -> gobgp:url Url string `mapstructure:"url"` // original -> gobgp:redistribute-route-type - // original type is list of identityref - RedistributeRouteTypeList []string `mapstructure:"redistribute-route-type-list"` + RedistributeRouteTypeList []InstallProtocolType `mapstructure:"redistribute-route-type-list"` } //struct for container gobgp:mrt @@ -1391,7 +1670,7 @@ type ApplyPolicy struct { //struct for container bgp-mp:state type AfiSafiState struct { // original -> bgp-mp:afi-safi-name - AfiSafiName string `mapstructure:"afi-safi-name"` + AfiSafiName AfiSafiType `mapstructure:"afi-safi-name"` // original -> bgp-mp:enabled //bgp-mp:enabled's original type is boolean Enabled bool `mapstructure:"enabled"` @@ -1404,7 +1683,7 @@ type AfiSafiState struct { //struct for container bgp-mp:config type AfiSafiConfig struct { // original -> bgp-mp:afi-safi-name - AfiSafiName string `mapstructure:"afi-safi-name"` + AfiSafiName AfiSafiType `mapstructure:"afi-safi-name"` // original -> bgp-mp:enabled //bgp-mp:enabled's original type is boolean Enabled bool `mapstructure:"enabled"` @@ -1441,8 +1720,7 @@ type MpGracefulRestart struct { //struct for container bgp-mp:afi-safi type AfiSafi struct { // original -> bgp-mp:afi-safi-name - //bgp-mp:afi-safi-name's original type is identityref - AfiSafiName string `mapstructure:"afi-safi-name"` + AfiSafiName AfiSafiType `mapstructure:"afi-safi-name"` // original -> bgp-mp:mp-graceful-restart MpGracefulRestart MpGracefulRestart `mapstructure:"mp-graceful-restart"` // original -> bgp-mp:afi-safi-config @@ -1867,7 +2145,7 @@ type Actions struct { //struct for container bgp-pol:as-path-length type AsPathLength struct { // original -> ptypes:operator - Operator string `mapstructure:"operator"` + Operator AttributeComparison `mapstructure:"operator"` // original -> ptypes:value Value uint32 `mapstructure:"value"` } @@ -1875,7 +2153,7 @@ type AsPathLength struct { //struct for container bgp-pol:community-count type CommunityCount struct { // original -> ptypes:operator - Operator string `mapstructure:"operator"` + Operator AttributeComparison `mapstructure:"operator"` // original -> ptypes:value Value uint32 `mapstructure:"value"` } @@ -1920,8 +2198,7 @@ type BgpConditions struct { // original type is list of inet:ip-address NextHopInList []string `mapstructure:"next-hop-in-list"` // original -> bgp-pol:afi-safi-in - // original type is list of identityref - AfiSafiInList []string `mapstructure:"afi-safi-in-list"` + AfiSafiInList []AfiSafiType `mapstructure:"afi-safi-in-list"` // original -> bgp-pol:local-pref-eq LocalPrefEq uint32 `mapstructure:"local-pref-eq"` // original -> bgp-pol:community-count @@ -1973,7 +2250,7 @@ type Conditions struct { // original -> rpol:match-tag-set MatchTagSet MatchTagSet `mapstructure:"match-tag-set"` // original -> rpol:install-protocol-eq - InstallProtocolEq string `mapstructure:"install-protocol-eq"` + InstallProtocolEq InstallProtocolType `mapstructure:"install-protocol-eq"` // original -> rpol:igp-conditions IgpConditions IgpConditions `mapstructure:"igp-conditions"` // original -> bgp-pol:bgp-conditions diff --git a/config/default.go b/config/default.go index c0764c44..787a160b 100644 --- a/config/default.go +++ b/config/default.go @@ -19,6 +19,7 @@ func SetDefaultConfigValues(v *viper.Viper, b *Bgp) error { if v == nil { v = viper.New() } + if !v.IsSet("global.afi-safis") { b.Global.AfiSafis = []AfiSafi{ AfiSafi{AfiSafiName: "ipv4-unicast"}, diff --git a/config/util.go b/config/util.go index ca2ebe4c..5d543b9c 100644 --- a/config/util.go +++ b/config/util.go @@ -16,6 +16,7 @@ package config import ( + "fmt" "github.com/osrg/gobgp/packet" ) @@ -39,9 +40,9 @@ type AfiSafis []AfiSafi func (c AfiSafis) ToRfList() ([]bgp.RouteFamily, error) { rfs := make([]bgp.RouteFamily, 0, len(c)) for _, rf := range c { - k, err := bgp.GetRouteFamily(rf.AfiSafiName) + k, err := bgp.GetRouteFamily(string(rf.AfiSafiName)) if err != nil { - return nil, err + return nil, fmt.Errorf("invalid address family: %s", rf.AfiSafiName) } rfs = append(rfs, k) } diff --git a/gobgp/cmd/policy.go b/gobgp/cmd/policy.go index dfc9487c..c2c1ec70 100644 --- a/gobgp/cmd/policy.go +++ b/gobgp/cmd/policy.go @@ -438,9 +438,8 @@ func printStatement(indent int, s *api.Statement) { } rpki := s.Conditions.RpkiResult - var r config.RpkiValidationResultType if rpki > -1 { - fmt.Printf("%sRPKI result: %s\n", sIndent(indent+4), r.FromInt(int(rpki))) + fmt.Printf("%sRPKI result: %s\n", sIndent(indent+4), config.IntToRpkiValidationResultTypeMap[int(rpki)]) } fmt.Printf("%sActions:\n", sIndent(indent+2)) diff --git a/packet/constant.go b/packet/constant.go index 65101dc1..ac270cad 100644 --- a/packet/constant.go +++ b/packet/constant.go @@ -27,8 +27,7 @@ const BGP_PORT = 179 type FSMState int const ( - _ FSMState = iota - BGP_FSM_IDLE + BGP_FSM_IDLE FSMState = iota BGP_FSM_CONNECT BGP_FSM_ACTIVE BGP_FSM_OPENSENT diff --git a/packet/fsmstate_string.go b/packet/fsmstate_string.go index 26cafe25..4416afc1 100644 --- a/packet/fsmstate_string.go +++ b/packet/fsmstate_string.go @@ -1,4 +1,4 @@ -// generated by stringer -type FSMState constatnt.go; DO NOT EDIT +// generated by stringer -type=FSMState -output=fsmstate_string.go bgp.go validate.go mrt.go rtr.go constant.go bmp.go esitype_string.go bgpattrtype_string.go; DO NOT EDIT package bgp @@ -9,9 +9,8 @@ const _FSMState_name = "BGP_FSM_IDLEBGP_FSM_CONNECTBGP_FSM_ACTIVEBGP_FSM_OPENSEN var _FSMState_index = [...]uint8{0, 12, 27, 41, 57, 76, 95} func (i FSMState) String() string { - i -= 1 - if i < 0 || i+1 >= FSMState(len(_FSMState_index)) { - return fmt.Sprintf("FSMState(%d)", i+1) + if i < 0 || i >= FSMState(len(_FSMState_index)-1) { + return fmt.Sprintf("FSMState(%d)", i) } return _FSMState_name[_FSMState_index[i]:_FSMState_index[i+1]] } diff --git a/server/fsm.go b/server/fsm.go index 3e275ca7..0511f0af 100644 --- a/server/fsm.go +++ b/server/fsm.go @@ -468,8 +468,8 @@ func capabilitiesFromConfig(gConf *config.Global, pConf *config.Neighbor) []bgp. caps := make([]bgp.ParameterCapabilityInterface, 0, 4) caps = append(caps, bgp.NewCapRouteRefresh()) for _, rf := range pConf.AfiSafis { - k, _ := bgp.GetRouteFamily(rf.AfiSafiName) - caps = append(caps, bgp.NewCapMultiProtocol(k)) + family, _ := bgp.GetRouteFamily(string(rf.AfiSafiName)) + caps = append(caps, bgp.NewCapMultiProtocol(family)) } caps = append(caps, bgp.NewCapFourOctetASNumber(gConf.Config.As)) return caps diff --git a/server/peer.go b/server/peer.go index 77e07252..02db27c0 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 = conf.State.SessionState.FromInt(int(bgp.BGP_FSM_IDLE)) + conf.State.SessionState = config.IntToSessionStateMap[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 3a3b70fe..beee6560 100644 --- a/server/server.go +++ b/server/server.go @@ -911,7 +911,7 @@ func (server *BgpServer) handleFSMMessage(peer *Peer, e *FsmMsg) []*SenderMsg { case FSM_MSG_STATE_CHANGE: nextState := e.MsgData.(bgp.FSMState) oldState := bgp.FSMState(peer.conf.State.SessionState.ToInt()) - peer.conf.State.SessionState = peer.conf.State.SessionState.FromInt(int(nextState)) + peer.conf.State.SessionState = config.IntToSessionStateMap[int(nextState)] peer.fsm.StateChange(nextState) if oldState == bgp.BGP_FSM_ESTABLISHED { @@ -2177,7 +2177,7 @@ func (server *BgpServer) handleGrpcModNeighbor(grpcReq *GrpcRequest) (sMsgs []*S if !ok { return pconf, fmt.Errorf("invalid address family: %d", family) } - cAfiSafi := config.AfiSafi{AfiSafiName: name} + cAfiSafi := config.AfiSafi{AfiSafiName: config.AfiSafiType(name)} pconf.AfiSafis = append(pconf.AfiSafis, cAfiSafi) } } else { @@ -2827,7 +2827,7 @@ func (server *BgpServer) mkMrtRibMsgs(tbl *table.Table, t uint32) ([]*bgp.MRTMes return msgs, nil } -func (server *BgpServer) NewZclient(url string, redistRouteTypes []string) error { +func (server *BgpServer) NewZclient(url string, redistRouteTypes []config.InstallProtocolType) error { l := strings.SplitN(url, ":", 2) if len(l) != 2 { return fmt.Errorf("unsupported url: %s", url) @@ -2840,7 +2840,7 @@ func (server *BgpServer) NewZclient(url string, redistRouteTypes []string) error cli.SendRouterIDAdd() cli.SendInterfaceAdd() for _, typ := range redistRouteTypes { - t, err := zebra.RouteTypeFromString(typ) + t, err := zebra.RouteTypeFromString(string(typ)) if err != nil { return err } diff --git a/table/policy.go b/table/policy.go index 81010a9d..68792a6c 100644 --- a/table/policy.go +++ b/table/policy.go @@ -1546,15 +1546,12 @@ func NewAsPathLengthCondition(c config.AsPathLength) (*AsPathLengthCondition, er return nil, nil } var op AttributeComparison - switch strings.ToLower(c.Operator) { - case "eq": - op = ATTRIBUTE_EQ - case "ge": - op = ATTRIBUTE_GE - case "le": - op = ATTRIBUTE_LE - default: + if i := c.Operator.ToInt(); i < 0 { return nil, fmt.Errorf("invalid as path length operator: %s", c.Operator) + } else { + // take mod 3 because we have extended openconfig attribute-comparison + // for simple configuration. see config.AttributeComparison definition + op = AttributeComparison(i % 3) } return &AsPathLengthCondition{ length: c.Value, @@ -1582,9 +1579,7 @@ func NewRpkiValidationConditionFromApiStruct(a int32) (*RpkiValidationCondition, if a < 1 { return nil, nil } - var typ config.RpkiValidationResultType - typ = typ.FromInt(int(a)) - return NewRpkiValidationCondition(typ) + return NewRpkiValidationCondition(config.IntToRpkiValidationResultTypeMap[int(a)]) } func NewRpkiValidationCondition(c config.RpkiValidationResultType) (*RpkiValidationCondition, error) { @@ -1735,8 +1730,7 @@ func NewCommunityActionFromApiStruct(a *api.CommunityAction) (*CommunityAction, } var list []uint32 var removeList []*regexp.Regexp - var op config.BgpSetCommunityOptionType - op = op.FromInt(int(a.Type)) + op := config.IntToBgpSetCommunityOptionTypeMap[int(a.Type)] if op == config.BGP_SET_COMMUNITY_OPTION_TYPE_REMOVE { removeList = make([]*regexp.Regexp, 0, len(a.Communities)) } else { @@ -1857,8 +1851,7 @@ func NewExtCommunityActionFromApiStruct(a *api.CommunityAction) (*ExtCommunityAc var list []bgp.ExtendedCommunityInterface var removeList []*regexp.Regexp subtypeList := make([]bgp.ExtendedCommunityAttrSubType, 0, len(a.Communities)) - var op config.BgpSetCommunityOptionType - op = op.FromInt(int(a.Type)) + op := config.IntToBgpSetCommunityOptionTypeMap[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 64d4d97c..45a25078 100644 --- a/tools/pyang_plugins/bgpyang2golang.py +++ b/tools/pyang_plugins/bgpyang2golang.py @@ -94,6 +94,7 @@ def emit_go(ctx): for mod in ctx.module_deps: if mod not in _module_excluded: emit_typedef(ctx, mod) + emit_identity(ctx, mod) for struct in ctx.golang_struct_def: struct_name = struct.uniq_name @@ -154,7 +155,7 @@ def emit_class_def(ctx, yang_statement, struct_name, prefix): # case identityref if type_name == 'identityref': - emit_type_name = 'string' + emit_type_name = convert_to_golang(type_obj.search_one('base').arg.split(':')[-1]) # case leafref elif type_name == 'leafref': @@ -163,6 +164,8 @@ def emit_class_def(ctx, yang_statement, struct_name, prefix): print >> o, ' //%s:%s\'s original type is %s' \ % (child_prefix, container_or_list_name, t.arg) emit_type_name = translate_type(t.arg) + elif is_identityref(t): + emit_type_name = convert_to_golang(t.search_one('base').arg.split(':')[-1]) else: emit_type_name = t.arg @@ -203,6 +206,9 @@ def emit_class_def(ctx, yang_statement, struct_name, prefix): t = type_obj.i_type_spec.i_target_node.search_one('type') emit_type_name = '[]'+t.arg + elif type_name == 'identityref': + emit_type_name = '[]'+convert_to_golang(type_obj.search_one('base').arg.split(':')[-1]) + # case translation required elif is_translation_required(type_obj): print >> o, ' // original type is list of %s' % (type_obj.arg) @@ -372,6 +378,15 @@ def visit_identity(ctx, module): name = stmts.arg stmts.golang_name = convert_to_golang(name) child_map[name] = stmts + + base = stmts.search_one('base') + if base: + elems = base.arg.split(':') + if len(elems) > 1: + ctx.golang_identity_map[elems[0]][elems[1]].substmts.append(stmts) + else: + child_map[base.arg].substmts.append(stmts) + ctx.golang_identity_map[prefix] = child_map @@ -398,6 +413,68 @@ def lookup(basemap, default_prefix, key): return key +def emit_enum(prefix, name, stmt, substmts): + type_name_org = name + type_name = stmt.golang_name + o = StringIO.StringIO() + + print >> o, '// typedef for identity %s:%s' % (prefix, type_name_org) + print >> o, 'type %s string' % (type_name) + + const_prefix = convert_const_prefix(type_name_org) + print >> o, 'const (' + m = {} + for sub in substmts: + 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, 'var %sToIntMap = map[%s]int {' % (type_name, type_name) + for i, sub in enumerate(substmts): + enum_name = '%s_%s' % (const_prefix, convert_const_prefix(sub.arg)) + print >> o, ' %s: %d,' % (enum_name, i) + print >> o, '}\n' + + print >> o, 'func (v %s) ToInt() int {' % (type_name) + print >> o, 'i, ok := %sToIntMap[v]' % (type_name) + print >> o, 'if !ok {' + print >> o, 'return -1' + print >> o, '}' + print >> o, 'return i' + print >> o, '}' + + print >> o, 'var IntTo%sMap = map[int]%s {' % (type_name, type_name) + for i, sub in enumerate(substmts): + enum_name = '%s_%s' % (const_prefix, convert_const_prefix(sub.arg)) + print >> o, ' %d: %s,' % (i, enum_name) + print >> o, '}\n' + + print >> o, 'func (v %s) Validate() error {' % (type_name) + print >> o, 'if _, ok := %sToIntMap[v]; !ok {' % (type_name) + 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, '}' + + + + print o.getvalue() + + def emit_typedef(ctx, module): prefix = module.i_prefix t_map = ctx.golang_typedef_map[prefix] @@ -424,55 +501,7 @@ def emit_typedef(ctx, module): o = StringIO.StringIO() if t.arg == 'enumeration': - print >> o, '// typedef for typedef %s:%s'\ - % (prefix, type_name_org) - print >> o, 'type %s string' % (type_name) - - const_prefix = convert_const_prefix(type_name_org) - print >> o, 'const (' - m = {} - for sub in t.substmts: - 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, '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) - 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, '}' - - - + emit_enum(prefix, type_name_org, stmt, t.substmts) elif t.arg == 'union': print >> o, '// typedef for typedef %s:%s'\ % (prefix, type_name_org) @@ -497,29 +526,18 @@ def emit_identity(ctx, module): prefix = module.i_prefix i_map = ctx.golang_identity_map[prefix] for name, stmt in i_map.items(): - type_name_org = name - type_name = stmt.golang_name - base = stmt.search_one('base') - o = StringIO.StringIO() - - print >> o, '// typedef for identity %s:%s' % (prefix, type_name_org) - print >> o, 'type %s struct {' % (type_name) - if base is not None: - base_obj = lookup_identity(ctx, prefix, base.arg) - print >> o, ' // base_type -> %s' % (base.arg) - print >> o, ' %s' % (base_obj.golang_name) - - print >> o, '}' - print o.getvalue() - + enums = stmt.search('identity') + if len(enums) > 0: + emit_enum(prefix, name, stmt, enums) def is_reference(s): return s.arg in ['leafref', 'identityref'] - def is_leafref(s): return s.arg in ['leafref'] +def is_identityref(s): + return s.arg in ['identityref'] def is_leaf(s): return s.keyword in ['leaf'] @@ -563,7 +581,6 @@ _type_translation_map = { 'inet:ipv4-address': 'string', 'inet:as-number': 'uint32', 'bgp-set-community-option-type': 'string', - 'identityref' : 'string', 'inet:port-number': 'uint16', 'yang:timeticks': 'int64', 'ptypes:install-protocol-type': 'string', diff --git a/tools/pyang_plugins/gobgp.yang b/tools/pyang_plugins/gobgp.yang index 1cf34800..29e3342d 100644 --- a/tools/pyang_plugins/gobgp.yang +++ b/tools/pyang_plugins/gobgp.yang @@ -9,6 +9,7 @@ module gobgp { // import some basic types import openconfig-bgp { prefix bgp; } + import openconfig-bgp-types { prefix bgp-types; } import openconfig-routing-policy {prefix rpol; } import openconfig-policy-types {prefix ptypes; } import openconfig-bgp-policy {prefix bgp-pol; } @@ -49,6 +50,74 @@ module gobgp { } } + identity eq { + base ptypes:attribute-comparison; + } + + identity ge { + base ptypes:attribute-comparison; + } + + identity le { + base ptypes:attribute-comparison; + } + + identity IPV4-MULTICAST { + base bgp-types:afi-safi-type; + description + "IPv4 multicast (AFI,SAFI = 1,2)"; + reference "RFC4760"; + } + + identity IPV6-MULTICAST { + base bgp-types:afi-safi-type; + description + "IPv4 multicast (AFI,SAFI = 1,2)"; + reference "RFC4760"; + } + + identity RTC { + base bgp-types:afi-safi-type; + description + "Route target membership (AFI,SAFI = 1,132)"; + reference "RFC4684"; + } + + identity ENCAP { + base bgp-types:afi-safi-type; + description + "Encapsulation (AFI,SAFI = 1,7)"; + reference "RFC5512"; + } + + identity IPV4-FLOWSPEC { + base bgp-types:afi-safi-type; + description + "IPv4 flowspec (AFI,SAFI = 1,133)"; + reference "RFC5575"; + } + + identity L3VPN-IPV4-FLOWSPEC { + base bgp-types:afi-safi-type; + description + "L3VPN IPv4 flowspec (AFI,SAFI = 1,134)"; + reference "RFC5575"; + } + + identity IPV6-FLOWSPEC { + base bgp-types:afi-safi-type; + description + "IPv6 flowspec (AFI,SAFI = 1,133)"; + reference "RFC5575"; + } + + identity L3VPN-IPV6-FLOWSPEC { + base bgp-types:afi-safi-type; + description + "L3VPN IPv6 flowspec (AFI,SAFI = 1,134)"; + reference "RFC5575"; + } + grouping gobgp-message-counter { description "Counters for all BGPMessage types"; |