summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorFUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>2016-07-09 16:33:40 +0900
committerFUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>2016-07-20 04:25:25 +0900
commite88a82ed507df6fb2c9411de52427c3b466203ca (patch)
tree561a5e8e8d74a6a8687e63d7bc47b07b2889e65e
parent804973bff5110b1c925167d6e8da6b188b63eb0a (diff)
table: remove ToApiStruct methods from policy.go
Preparation for removing gRPC dependency. Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
-rw-r--r--server/grpc_server.go234
-rw-r--r--server/server.go94
-rw-r--r--table/policy.go289
3 files changed, 406 insertions, 211 deletions
diff --git a/server/grpc_server.go b/server/grpc_server.go
index e4de06f5..64a7f839 100644
--- a/server/grpc_server.go
+++ b/server/grpc_server.go
@@ -21,10 +21,13 @@ import (
api "github.com/osrg/gobgp/api"
"github.com/osrg/gobgp/config"
"github.com/osrg/gobgp/packet/bgp"
+ "github.com/osrg/gobgp/table"
"golang.org/x/net/context"
"google.golang.org/grpc"
"io"
"net"
+ "regexp"
+ "strconv"
"strings"
"sync"
)
@@ -667,7 +670,62 @@ func (s *Server) GetDefinedSet(ctx context.Context, arg *api.GetDefinedSetReques
if err != nil {
return nil, err
}
- return d.(*api.GetDefinedSetResponse), err
+ sets := make([]*api.DefinedSet, 0)
+ cd := d.(*config.DefinedSets)
+ for _, cs := range cd.PrefixSets {
+ ad := &api.DefinedSet{
+ Type: api.DefinedType_PREFIX,
+ Name: cs.PrefixSetName,
+ Prefixes: func() []*api.Prefix {
+ l := make([]*api.Prefix, 0, len(cs.PrefixList))
+ for _, p := range cs.PrefixList {
+ exp := regexp.MustCompile("(\\d+)\\.\\.(\\d+)")
+ elems := exp.FindStringSubmatch(p.MasklengthRange)
+ min, _ := strconv.Atoi(elems[1])
+ max, _ := strconv.Atoi(elems[2])
+
+ l = append(l, &api.Prefix{IpPrefix: p.IpPrefix, MaskLengthMin: uint32(min), MaskLengthMax: uint32(max)})
+ }
+ return l
+ }(),
+ }
+ sets = append(sets, ad)
+
+ }
+ for _, cs := range cd.NeighborSets {
+ ad := &api.DefinedSet{
+ Type: api.DefinedType_NEIGHBOR,
+ Name: cs.NeighborSetName,
+ List: cs.NeighborInfoList,
+ }
+ sets = append(sets, ad)
+ }
+ for _, cs := range cd.BgpDefinedSets.CommunitySets {
+ ad := &api.DefinedSet{
+ Type: api.DefinedType_COMMUNITY,
+ Name: cs.CommunitySetName,
+ List: cs.CommunityList,
+ }
+ sets = append(sets, ad)
+ }
+ for _, cs := range cd.BgpDefinedSets.ExtCommunitySets {
+ ad := &api.DefinedSet{
+ Type: api.DefinedType_EXT_COMMUNITY,
+ Name: cs.ExtCommunitySetName,
+ List: cs.ExtCommunityList,
+ }
+ sets = append(sets, ad)
+ }
+ for _, cs := range cd.BgpDefinedSets.AsPathSets {
+ ad := &api.DefinedSet{
+ Type: api.DefinedType_AS_PATH,
+ Name: cs.AsPathSetName,
+ List: cs.AsPathList,
+ }
+ sets = append(sets, ad)
+ }
+
+ return &api.GetDefinedSetResponse{Sets: sets}, nil
}
func (s *Server) AddDefinedSet(ctx context.Context, arg *api.AddDefinedSetRequest) (*api.AddDefinedSetResponse, error) {
@@ -694,12 +752,142 @@ func (s *Server) ReplaceDefinedSet(ctx context.Context, arg *api.ReplaceDefinedS
return d.(*api.ReplaceDefinedSetResponse), err
}
+func toStatementApi(s *config.Statement) *api.Statement {
+ cs := &api.Conditions{}
+ if s.Conditions.MatchPrefixSet.PrefixSet != "" {
+ cs.PrefixSet = &api.MatchSet{
+ Type: api.MatchType(s.Conditions.MatchPrefixSet.MatchSetOptions.ToInt()),
+ Name: s.Conditions.MatchPrefixSet.PrefixSet,
+ }
+ }
+ if s.Conditions.MatchNeighborSet.NeighborSet != "" {
+ cs.NeighborSet = &api.MatchSet{
+ Type: api.MatchType(s.Conditions.MatchNeighborSet.MatchSetOptions.ToInt()),
+ Name: s.Conditions.MatchNeighborSet.NeighborSet,
+ }
+ }
+ if s.Conditions.BgpConditions.AsPathLength.Operator != "" {
+ cs.AsPathLength = &api.AsPathLength{
+ Length: s.Conditions.BgpConditions.AsPathLength.Value,
+ Type: api.AsPathLengthType(s.Conditions.BgpConditions.AsPathLength.Operator.ToInt()),
+ }
+ }
+ if s.Conditions.BgpConditions.MatchAsPathSet.AsPathSet != "" {
+ cs.AsPathSet = &api.MatchSet{
+ Type: api.MatchType(s.Conditions.BgpConditions.MatchAsPathSet.MatchSetOptions.ToInt()),
+ Name: s.Conditions.BgpConditions.MatchAsPathSet.AsPathSet,
+ }
+ }
+ if s.Conditions.BgpConditions.MatchCommunitySet.CommunitySet != "" {
+ cs.CommunitySet = &api.MatchSet{
+ Type: api.MatchType(s.Conditions.BgpConditions.MatchCommunitySet.MatchSetOptions.ToInt()),
+ Name: s.Conditions.BgpConditions.MatchCommunitySet.CommunitySet,
+ }
+ }
+ if s.Conditions.BgpConditions.MatchExtCommunitySet.ExtCommunitySet != "" {
+ cs.CommunitySet = &api.MatchSet{
+ Type: api.MatchType(s.Conditions.BgpConditions.MatchExtCommunitySet.MatchSetOptions.ToInt()),
+ Name: s.Conditions.BgpConditions.MatchExtCommunitySet.ExtCommunitySet,
+ }
+ }
+ cs.RpkiResult = int32(s.Conditions.BgpConditions.RpkiValidationResult.ToInt())
+ as := &api.Actions{
+ RouteAction: func() api.RouteAction {
+ if s.Actions.RouteDisposition.AcceptRoute {
+ return api.RouteAction_ACCEPT
+ }
+ return api.RouteAction_REJECT
+ }(),
+ Community: func() *api.CommunityAction {
+ if len(s.Actions.BgpActions.SetCommunity.SetCommunityMethod.CommunitiesList) == 0 {
+ return nil
+ }
+ return &api.CommunityAction{
+ Type: api.CommunityActionType(config.BgpSetCommunityOptionTypeToIntMap[config.BgpSetCommunityOptionType(s.Actions.BgpActions.SetCommunity.Options)]),
+ Communities: s.Actions.BgpActions.SetCommunity.SetCommunityMethod.CommunitiesList}
+ }(),
+ Med: func() *api.MedAction {
+ if len(string(s.Actions.BgpActions.SetMed)) == 0 {
+ return nil
+ }
+ exp := regexp.MustCompile("^(\\+|\\-)?(\\d+)$")
+ elems := exp.FindStringSubmatch(string(s.Actions.BgpActions.SetMed))
+ action := api.MedActionType_MED_REPLACE
+ switch elems[1] {
+ case "+", "-":
+ action = api.MedActionType_MED_MOD
+ }
+ value, _ := strconv.Atoi(string(s.Actions.BgpActions.SetMed))
+ return &api.MedAction{
+ Value: int64(value),
+ Type: action,
+ }
+ }(),
+ AsPrepend: func() *api.AsPrependAction {
+ if len(s.Actions.BgpActions.SetAsPathPrepend.As) == 0 {
+ return nil
+ }
+ asn := 0
+ useleft := false
+ if s.Actions.BgpActions.SetAsPathPrepend.As != "last-as" {
+ asn, _ = strconv.Atoi(s.Actions.BgpActions.SetAsPathPrepend.As)
+ } else {
+ useleft = true
+ }
+ return &api.AsPrependAction{
+ Asn: uint32(asn),
+ Repeat: uint32(s.Actions.BgpActions.SetAsPathPrepend.RepeatN),
+ UseLeftMost: useleft,
+ }
+ }(),
+ ExtCommunity: func() *api.CommunityAction {
+ if len(s.Actions.BgpActions.SetExtCommunity.SetExtCommunityMethod.CommunitiesList) == 0 {
+ return nil
+ }
+ return &api.CommunityAction{
+ Type: api.CommunityActionType(config.BgpSetCommunityOptionTypeToIntMap[config.BgpSetCommunityOptionType(s.Actions.BgpActions.SetExtCommunity.Options)]),
+ Communities: s.Actions.BgpActions.SetExtCommunity.SetExtCommunityMethod.CommunitiesList,
+ }
+ }(),
+ Nexthop: func() *api.NexthopAction {
+ if len(string(s.Actions.BgpActions.SetNextHop)) == 0 {
+ return nil
+ }
+
+ if string(s.Actions.BgpActions.SetNextHop) == "self" {
+ return &api.NexthopAction{
+ Self: true,
+ }
+ }
+ return &api.NexthopAction{
+ Address: string(s.Actions.BgpActions.SetNextHop),
+ }
+ }(),
+ LocalPref: func() *api.LocalPrefAction {
+ if s.Actions.BgpActions.SetLocalPref == 0 {
+ return nil
+ }
+ return &api.LocalPrefAction{Value: s.Actions.BgpActions.SetLocalPref}
+ }(),
+ }
+ return &api.Statement{
+ Name: s.Name,
+ Conditions: cs,
+ Actions: as,
+ }
+}
+
func (s *Server) GetStatement(ctx context.Context, arg *api.GetStatementRequest) (*api.GetStatementResponse, error) {
d, err := s.get(REQ_GET_STATEMENT, arg)
if err != nil {
return nil, err
}
- return d.(*api.GetStatementResponse), err
+
+ l := make([]*api.Statement, 0)
+ for _, s := range d.([]*config.Statement) {
+ l = append(l, toStatementApi(s))
+ }
+ return &api.GetStatementResponse{Statements: l}, err
}
func (s *Server) AddStatement(ctx context.Context, arg *api.AddStatementRequest) (*api.AddStatementResponse, error) {
@@ -726,12 +914,29 @@ func (s *Server) ReplaceStatement(ctx context.Context, arg *api.ReplaceStatement
return d.(*api.ReplaceStatementResponse), err
}
+func toPolicyApi(p *config.PolicyDefinition) *api.Policy {
+ return &api.Policy{
+ Name: p.Name,
+ Statements: func() []*api.Statement {
+ l := make([]*api.Statement, 0)
+ for _, s := range p.Statements {
+ l = append(l, toStatementApi(&s))
+ }
+ return l
+ }(),
+ }
+}
+
func (s *Server) GetPolicy(ctx context.Context, arg *api.GetPolicyRequest) (*api.GetPolicyResponse, error) {
d, err := s.get(REQ_GET_POLICY, arg)
if err != nil {
return nil, err
}
- return d.(*api.GetPolicyResponse), err
+ l := make([]*api.Policy, 0)
+ for _, p := range d.([]*config.PolicyDefinition) {
+ l = append(l, toPolicyApi(p))
+ }
+ return &api.GetPolicyResponse{Policies: l}, err
}
func (s *Server) AddPolicy(ctx context.Context, arg *api.AddPolicyRequest) (*api.AddPolicyResponse, error) {
@@ -763,7 +968,28 @@ func (s *Server) GetPolicyAssignment(ctx context.Context, arg *api.GetPolicyAssi
if err != nil {
return nil, err
}
- return d.(*api.GetPolicyAssignmentResponse), err
+ a := d.(*PolicyAssignment)
+ return &api.GetPolicyAssignmentResponse{
+ Assignment: &api.PolicyAssignment{
+ Default: func() api.RouteAction {
+ switch a.Default {
+ case table.ROUTE_TYPE_ACCEPT:
+ return api.RouteAction_ACCEPT
+ case table.ROUTE_TYPE_REJECT:
+ return api.RouteAction_REJECT
+ }
+ return api.RouteAction_NONE
+
+ }(),
+ Policies: func() []*api.Policy {
+ l := make([]*api.Policy, 0)
+ for _, p := range a.PolicyDefinitions {
+ l = append(l, toPolicyApi(p))
+ }
+ return l
+ }(),
+ },
+ }, err
}
func (s *Server) AddPolicyAssignment(ctx context.Context, arg *api.AddPolicyAssignmentRequest) (*api.AddPolicyAssignmentResponse, error) {
diff --git a/server/server.go b/server/server.go
index 8552810c..5d1e2892 100644
--- a/server/server.go
+++ b/server/server.go
@@ -2156,20 +2156,6 @@ ERROR:
return
}
-func (server *BgpServer) handleGrpcGetDefinedSet(grpcReq *GrpcRequest) (*api.GetDefinedSetResponse, error) {
- arg := grpcReq.Data.(*api.GetDefinedSetRequest)
- typ := table.DefinedType(arg.Type)
- set, ok := server.policy.DefinedSetMap[typ]
- if !ok {
- return &api.GetDefinedSetResponse{}, fmt.Errorf("invalid defined-set type: %d", typ)
- }
- sets := make([]*api.DefinedSet, 0)
- for _, s := range set {
- sets = append(sets, s.ToApiStruct())
- }
- return &api.GetDefinedSetResponse{Sets: sets}, nil
-}
-
func (server *BgpServer) handleAddNeighbor(c *config.Neighbor) error {
addr := c.Config.NeighborAddress
if _, y := server.neighborMap[addr]; y {
@@ -2310,6 +2296,39 @@ func (server *BgpServer) handleUpdateNeighbor(c *config.Neighbor) (bool, error)
return policyUpdated, err
}
+func (server *BgpServer) handleGrpcGetDefinedSet(grpcReq *GrpcRequest) (*config.DefinedSets, error) {
+ arg := grpcReq.Data.(*api.GetDefinedSetRequest)
+ typ := table.DefinedType(arg.Type)
+ set, ok := server.policy.DefinedSetMap[typ]
+ if !ok {
+ return nil, fmt.Errorf("invalid defined-set type: %d", typ)
+ }
+ sets := config.DefinedSets{
+ PrefixSets: make([]config.PrefixSet, 0),
+ NeighborSets: make([]config.NeighborSet, 0),
+ BgpDefinedSets: config.BgpDefinedSets{
+ CommunitySets: make([]config.CommunitySet, 0),
+ ExtCommunitySets: make([]config.ExtCommunitySet, 0),
+ AsPathSets: make([]config.AsPathSet, 0),
+ },
+ }
+ for _, s := range set {
+ switch s.(type) {
+ case *table.PrefixSet:
+ sets.PrefixSets = append(sets.PrefixSets, *s.(*table.PrefixSet).ToConfig())
+ case *table.NeighborSet:
+ sets.NeighborSets = append(sets.NeighborSets, *s.(*table.NeighborSet).ToConfig())
+ case *table.CommunitySet:
+ sets.BgpDefinedSets.CommunitySets = append(sets.BgpDefinedSets.CommunitySets, *s.(*table.CommunitySet).ToConfig())
+ case *table.ExtCommunitySet:
+ sets.BgpDefinedSets.ExtCommunitySets = append(sets.BgpDefinedSets.ExtCommunitySets, *s.(*table.ExtCommunitySet).ToConfig())
+ case *table.AsPathSet:
+ sets.BgpDefinedSets.AsPathSets = append(sets.BgpDefinedSets.AsPathSets, *s.(*table.AsPathSet).ToConfig())
+ }
+ }
+ return &sets, nil
+}
+
func (server *BgpServer) handleGrpcAddDefinedSet(grpcReq *GrpcRequest) (*api.AddDefinedSetResponse, error) {
arg := grpcReq.Data.(*api.AddDefinedSetRequest)
set := arg.Set
@@ -2383,12 +2402,12 @@ func (server *BgpServer) handleGrpcReplaceDefinedSet(grpcReq *GrpcRequest) (*api
return &api.ReplaceDefinedSetResponse{}, d.Replace(s)
}
-func (server *BgpServer) handleGrpcGetStatement(grpcReq *GrpcRequest) (*api.GetStatementResponse, error) {
- l := make([]*api.Statement, 0)
+func (server *BgpServer) handleGrpcGetStatement(grpcReq *GrpcRequest) ([]*config.Statement, error) {
+ l := make([]*config.Statement, 0)
for _, s := range server.policy.StatementMap {
- l = append(l, s.ToApiStruct())
+ l = append(l, s.ToConfig())
}
- return &api.GetStatementResponse{Statements: l}, nil
+ return l, nil
}
func (server *BgpServer) handleGrpcAddStatement(grpcReq *GrpcRequest) (*api.AddStatementResponse, error) {
@@ -2450,12 +2469,12 @@ func (server *BgpServer) handleGrpcReplaceStatement(grpcReq *GrpcRequest) (*api.
return &api.ReplaceStatementResponse{}, err
}
-func (server *BgpServer) handleGrpcGetPolicy(grpcReq *GrpcRequest) (*api.GetPolicyResponse, error) {
- policies := make([]*api.Policy, 0, len(server.policy.PolicyMap))
+func (server *BgpServer) handleGrpcGetPolicy(grpcReq *GrpcRequest) ([]*config.PolicyDefinition, error) {
+ policies := make([]*config.PolicyDefinition, 0, len(server.policy.PolicyMap))
for _, s := range server.policy.PolicyMap {
- policies = append(policies, s.ToApiStruct())
+ policies = append(policies, s.ToConfig())
}
- return &api.GetPolicyResponse{Policies: policies}, nil
+ return policies, nil
}
func (server *BgpServer) policyInUse(x *table.Policy) bool {
@@ -2630,21 +2649,28 @@ func (server *BgpServer) getPolicyInfo(a *api.PolicyAssignment) (string, table.P
}
-func (server *BgpServer) handleGrpcGetPolicyAssignment(grpcReq *GrpcRequest) (*api.GetPolicyAssignmentResponse, error) {
- rsp := &api.GetPolicyAssignmentResponse{}
+// temporarily
+type PolicyAssignment struct {
+ Default table.RouteType
+ PolicyDefinitions []*config.PolicyDefinition
+}
+
+func (server *BgpServer) handleGrpcGetPolicyAssignment(grpcReq *GrpcRequest) (*PolicyAssignment, error) {
id, dir, err := server.getPolicyInfo(grpcReq.Data.(*api.GetPolicyAssignmentRequest).Assignment)
if err != nil {
- return rsp, err
- }
- rsp.Assignment = &api.PolicyAssignment{
- Default: server.policy.GetDefaultPolicy(id, dir).ToApiStruct(),
- }
- ps := server.policy.GetPolicy(id, dir)
- rsp.Assignment.Policies = make([]*api.Policy, 0, len(ps))
- for _, x := range ps {
- rsp.Assignment.Policies = append(rsp.Assignment.Policies, x.ToApiStruct())
+ return nil, err
}
- return rsp, nil
+ return &PolicyAssignment{
+ Default: server.policy.GetDefaultPolicy(id, dir),
+ PolicyDefinitions: func() []*config.PolicyDefinition {
+ ps := server.policy.GetPolicy(id, dir)
+ l := make([]*config.PolicyDefinition, 0, len(ps))
+ for _, p := range ps {
+ l = append(l, p.ToConfig())
+ }
+ return l
+ }(),
+ }, nil
}
func (server *BgpServer) handleGrpcAddPolicyAssignment(grpcReq *GrpcRequest) (*api.AddPolicyAssignmentResponse, error) {
diff --git a/table/policy.go b/table/policy.go
index f94c8665..09c4ea19 100644
--- a/table/policy.go
+++ b/table/policy.go
@@ -54,16 +54,6 @@ const (
ROUTE_TYPE_REJECT
)
-func (t RouteType) ToApiStruct() api.RouteAction {
- switch t {
- case ROUTE_TYPE_ACCEPT:
- return api.RouteAction_ACCEPT
- case ROUTE_TYPE_REJECT:
- return api.RouteAction_REJECT
- }
- return api.RouteAction_NONE
-}
-
type PolicyDirection int
const (
@@ -219,7 +209,6 @@ const (
type DefinedSet interface {
Type() DefinedType
Name() string
- ToApiStruct() *api.DefinedSet
Append(DefinedSet) error
Remove(DefinedSet) error
Replace(DefinedSet) error
@@ -266,14 +255,6 @@ func (lhs *Prefix) Equal(rhs *Prefix) bool {
return lhs.Prefix.String() == rhs.Prefix.String() && lhs.MasklengthRangeMin == rhs.MasklengthRangeMin && lhs.MasklengthRangeMax == rhs.MasklengthRangeMax
}
-func (p *Prefix) ToApiStruct() *api.Prefix {
- return &api.Prefix{
- IpPrefix: p.Prefix.String(),
- MaskLengthMin: uint32(p.MasklengthRangeMin),
- MaskLengthMax: uint32(p.MasklengthRangeMax),
- }
-}
-
func NewPrefixFromApiStruct(a *api.Prefix) (*Prefix, error) {
addr, prefix, err := net.ParseCIDR(a.IpPrefix)
if err != nil {
@@ -377,16 +358,16 @@ func (lhs *PrefixSet) Replace(arg DefinedSet) error {
return nil
}
-func (s *PrefixSet) ToApiStruct() *api.DefinedSet {
- list := make([]*api.Prefix, 0, s.tree.Len())
+func (s *PrefixSet) ToConfig() *config.PrefixSet {
+ list := make([]config.Prefix, 0, s.tree.Len())
s.tree.Walk(func(s string, v interface{}) bool {
- list = append(list, v.(*Prefix).ToApiStruct())
+ p := v.(*Prefix)
+ list = append(list, config.Prefix{IpPrefix: p.Prefix.String(), MasklengthRange: fmt.Sprintf("%d..%d", p.MasklengthRangeMin, p.MasklengthRangeMax)})
return false
})
- return &api.DefinedSet{
- Type: api.DefinedType(s.Type()),
- Name: s.name,
- Prefixes: list,
+ return &config.PrefixSet{
+ PrefixSetName: s.name,
+ PrefixList: list,
}
}
@@ -483,15 +464,14 @@ func (lhs *NeighborSet) Replace(arg DefinedSet) error {
return nil
}
-func (s *NeighborSet) ToApiStruct() *api.DefinedSet {
+func (s *NeighborSet) ToConfig() *config.NeighborSet {
list := make([]string, 0, len(s.list))
for _, n := range s.list {
list = append(list, n.String())
}
- return &api.DefinedSet{
- Type: api.DefinedType(s.Type()),
- Name: s.name,
- List: list,
+ return &config.NeighborSet{
+ NeighborSetName: s.name,
+ NeighborInfoList: list,
}
}
@@ -697,7 +677,7 @@ func (lhs *AsPathSet) Replace(arg DefinedSet) error {
return nil
}
-func (s *AsPathSet) ToApiStruct() *api.DefinedSet {
+func (s *AsPathSet) ToConfig() *config.AsPathSet {
list := make([]string, 0, len(s.list)+len(s.singleList))
for _, exp := range s.singleList {
list = append(list, exp.String())
@@ -705,10 +685,9 @@ func (s *AsPathSet) ToApiStruct() *api.DefinedSet {
for _, exp := range s.list {
list = append(list, exp.String())
}
- return &api.DefinedSet{
- Type: api.DefinedType(s.typ),
- Name: s.name,
- List: list,
+ return &config.AsPathSet{
+ AsPathSetName: s.name,
+ AsPathList: list,
}
}
@@ -823,22 +802,21 @@ func (lhs *regExpSet) Replace(arg DefinedSet) error {
return nil
}
-func (s *regExpSet) ToApiStruct() *api.DefinedSet {
+type CommunitySet struct {
+ regExpSet
+}
+
+func (s *CommunitySet) ToConfig() *config.CommunitySet {
list := make([]string, 0, len(s.list))
for _, exp := range s.list {
list = append(list, exp.String())
}
- return &api.DefinedSet{
- Type: api.DefinedType(s.typ),
- Name: s.name,
- List: list,
+ return &config.CommunitySet{
+ CommunitySetName: s.name,
+ CommunityList: list,
}
}
-type CommunitySet struct {
- regExpSet
-}
-
func ParseCommunity(arg string) (uint32, error) {
i, err := strconv.Atoi(arg)
if err == nil {
@@ -966,7 +944,7 @@ type ExtCommunitySet struct {
subtypeList []bgp.ExtendedCommunityAttrSubType
}
-func (s *ExtCommunitySet) ToApiStruct() *api.DefinedSet {
+func (s *ExtCommunitySet) ToConfig() *config.ExtCommunitySet {
list := make([]string, 0, len(s.list))
f := func(idx int, arg string) string {
switch s.subtypeList[idx] {
@@ -983,10 +961,9 @@ func (s *ExtCommunitySet) ToApiStruct() *api.DefinedSet {
for idx, exp := range s.list {
list = append(list, f(idx, exp.String()))
}
- return &api.DefinedSet{
- Type: api.DefinedType(s.typ),
- Name: s.name,
- List: list,
+ return &config.ExtCommunitySet{
+ ExtCommunitySetName: s.name,
+ ExtCommunityList: list,
}
}
@@ -1103,13 +1080,6 @@ func (c *PrefixCondition) Evaluate(path *Path, _ *PolicyOptions) bool {
return result
}
-func (c *PrefixCondition) ToApiStruct() *api.MatchSet {
- return &api.MatchSet{
- Type: api.MatchType(c.option),
- Name: c.set.Name(),
- }
-}
-
func NewPrefixConditionFromApiStruct(a *api.MatchSet, m map[string]DefinedSet) (*PrefixCondition, error) {
if a == nil {
return nil, nil
@@ -1197,13 +1167,6 @@ func (c *NeighborCondition) Evaluate(path *Path, options *PolicyOptions) bool {
return result
}
-func (c *NeighborCondition) ToApiStruct() *api.MatchSet {
- return &api.MatchSet{
- Type: api.MatchType(c.option),
- Name: c.set.Name(),
- }
-}
-
func NewNeighborConditionFromApiStruct(a *api.MatchSet, m map[string]DefinedSet) (*NeighborCondition, error) {
if a == nil {
return nil, nil
@@ -1258,13 +1221,6 @@ func (c *AsPathCondition) Option() MatchOption {
return c.option
}
-func (c *AsPathCondition) ToApiStruct() *api.MatchSet {
- return &api.MatchSet{
- Type: api.MatchType(c.option),
- Name: c.set.Name(),
- }
-}
-
func (c *AsPathCondition) Evaluate(path *Path, _ *PolicyOptions) bool {
if len(c.set.singleList) > 0 {
aspath := path.GetAsSeqList()
@@ -1356,13 +1312,6 @@ func (c *CommunityCondition) Option() MatchOption {
return c.option
}
-func (c *CommunityCondition) ToApiStruct() *api.MatchSet {
- return &api.MatchSet{
- Type: api.MatchType(c.option),
- Name: c.set.Name(),
- }
-}
-
func (c *CommunityCondition) Evaluate(path *Path, _ *PolicyOptions) bool {
cs := path.GetCommunities()
result := false
@@ -1441,13 +1390,6 @@ func (c *ExtCommunityCondition) Option() MatchOption {
return c.option
}
-func (c *ExtCommunityCondition) ToApiStruct() *api.MatchSet {
- return &api.MatchSet{
- Type: api.MatchType(c.option),
- Name: c.set.Name(),
- }
-}
-
func (c *ExtCommunityCondition) Evaluate(path *Path, _ *PolicyOptions) bool {
es := path.GetExtCommunities()
result := false
@@ -1545,13 +1487,6 @@ func (c *AsPathLengthCondition) Set() DefinedSet {
return nil
}
-func (c *AsPathLengthCondition) ToApiStruct() *api.AsPathLength {
- return &api.AsPathLength{
- Length: c.length,
- Type: api.AsPathLengthType(c.operator),
- }
-}
-
func NewAsPathLengthConditionFromApiStruct(a *api.AsPathLength) (*AsPathLengthCondition, error) {
if a == nil {
return nil, nil
@@ -1632,14 +1567,6 @@ func (a *RoutingAction) Apply(path *Path, _ *PolicyOptions) *Path {
return nil
}
-func (a *RoutingAction) ToApiStruct() api.RouteAction {
- if a.AcceptRoute {
- return api.RouteAction_ACCEPT
- } else {
- return api.RouteAction_REJECT
- }
-}
-
func NewRoutingActionFromApiStruct(a api.RouteAction) (*RoutingAction, error) {
if a == api.RouteAction_NONE {
return nil, nil
@@ -1730,7 +1657,7 @@ func (a *CommunityAction) Apply(path *Path, _ *PolicyOptions) *Path {
return path
}
-func (a *CommunityAction) ToApiStruct() *api.CommunityAction {
+func (a *CommunityAction) ToConfig() *config.SetCommunity {
cs := make([]string, 0, len(a.list)+len(a.removeList))
for _, comm := range a.list {
c := fmt.Sprintf("%d:%d", comm>>16, comm&0x0000ffff)
@@ -1739,9 +1666,9 @@ func (a *CommunityAction) ToApiStruct() *api.CommunityAction {
for _, exp := range a.removeList {
cs = append(cs, exp.String())
}
- return &api.CommunityAction{
- Type: api.CommunityActionType(a.action.ToInt()),
- Communities: cs,
+ return &config.SetCommunity{
+ Options: string(a.action),
+ SetCommunityMethod: config.SetCommunityMethod{CommunitiesList: cs},
}
}
@@ -1839,7 +1766,7 @@ func (a *ExtCommunityAction) Apply(path *Path, _ *PolicyOptions) *Path {
return path
}
-func (a *ExtCommunityAction) ToApiStruct() *api.CommunityAction {
+func (a *ExtCommunityAction) ToConfig() *config.SetExtCommunity {
cs := make([]string, 0, len(a.list)+len(a.removeList))
f := func(idx int, arg string) string {
switch a.subtypeList[idx] {
@@ -1859,9 +1786,11 @@ func (a *ExtCommunityAction) ToApiStruct() *api.CommunityAction {
for idx, exp := range a.removeList {
cs = append(cs, f(idx, exp.String()))
}
- return &api.CommunityAction{
- Type: api.CommunityActionType(a.action.ToInt()),
- Communities: cs,
+ return &config.SetExtCommunity{
+ Options: string(a.action),
+ SetExtCommunityMethod: config.SetExtCommunityMethod{
+ CommunitiesList: cs,
+ },
}
}
@@ -1973,11 +1902,11 @@ func (a *MedAction) Apply(path *Path, _ *PolicyOptions) *Path {
return path
}
-func (a *MedAction) ToApiStruct() *api.MedAction {
- return &api.MedAction{
- Type: api.MedActionType(a.action),
- Value: int64(a.value),
+func (a *MedAction) ToConfig() config.BgpSetMedType {
+ if a.action == MED_ACTION_MOD && a.value > 0 {
+ return config.BgpSetMedType(fmt.Sprintf("+%d", a.value))
}
+ return config.BgpSetMedType(fmt.Sprintf("%d", a.value))
}
func NewMedActionFromApiStruct(a *api.MedAction) (*MedAction, error) {
@@ -2024,10 +1953,8 @@ func (a *LocalPrefAction) Apply(path *Path, _ *PolicyOptions) *Path {
return path
}
-func (a *LocalPrefAction) ToApiStruct() *api.LocalPrefAction {
- return &api.LocalPrefAction{
- Value: a.value,
- }
+func (a *LocalPrefAction) ToConfig() uint32 {
+ return a.value
}
func NewLocalPrefActionFromApiStruct(a *api.LocalPrefAction) (*LocalPrefAction, error) {
@@ -2086,11 +2013,15 @@ func (a *AsPathPrependAction) Apply(path *Path, _ *PolicyOptions) *Path {
return path
}
-func (a *AsPathPrependAction) ToApiStruct() *api.AsPrependAction {
- return &api.AsPrependAction{
- Asn: a.asn,
- Repeat: uint32(a.repeat),
- UseLeftMost: a.useLeftMost,
+func (a *AsPathPrependAction) ToConfig() *config.SetAsPathPrepend {
+ return &config.SetAsPathPrepend{
+ RepeatN: uint8(a.repeat),
+ As: func() string {
+ if a.useLeftMost {
+ return "last-as"
+ }
+ return fmt.Sprintf("%d", a.asn)
+ }(),
}
}
@@ -2149,11 +2080,11 @@ func (a *NexthopAction) Apply(path *Path, options *PolicyOptions) *Path {
return path
}
-func (a *NexthopAction) ToApiStruct() *api.NexthopAction {
- return &api.NexthopAction{
- Address: a.value.String(),
- Self: a.self,
+func (a *NexthopAction) ToConfig() config.BgpNextHopType {
+ if a.self {
+ return config.BgpNextHopType("self")
}
+ return config.BgpNextHopType(a.value.String())
}
func NewNexthopActionFromApiStruct(a *api.NexthopAction) (*NexthopAction, error) {
@@ -2229,50 +2160,62 @@ func (s *Statement) Apply(path *Path, options *PolicyOptions) (RouteType, *Path)
return ROUTE_TYPE_NONE, path
}
-func (s *Statement) ToApiStruct() *api.Statement {
- cs := &api.Conditions{}
- for _, c := range s.Conditions {
- switch c.(type) {
- case *PrefixCondition:
- cs.PrefixSet = c.(*PrefixCondition).ToApiStruct()
- case *NeighborCondition:
- cs.NeighborSet = c.(*NeighborCondition).ToApiStruct()
- case *AsPathLengthCondition:
- cs.AsPathLength = c.(*AsPathLengthCondition).ToApiStruct()
- case *AsPathCondition:
- cs.AsPathSet = c.(*AsPathCondition).ToApiStruct()
- case *CommunityCondition:
- cs.CommunitySet = c.(*CommunityCondition).ToApiStruct()
- case *ExtCommunityCondition:
- cs.ExtCommunitySet = c.(*ExtCommunityCondition).ToApiStruct()
- case *RpkiValidationCondition:
- cs.RpkiResult = int32(c.(*RpkiValidationCondition).result.ToInt())
- }
- }
- as := &api.Actions{}
- if s.RouteAction != nil && !reflect.ValueOf(s.RouteAction).IsNil() {
- as.RouteAction = s.RouteAction.(*RoutingAction).ToApiStruct()
- }
- for _, a := range s.ModActions {
- switch a.(type) {
- case *CommunityAction:
- as.Community = a.(*CommunityAction).ToApiStruct()
- case *MedAction:
- as.Med = a.(*MedAction).ToApiStruct()
- case *LocalPrefAction:
- as.LocalPref = a.(*LocalPrefAction).ToApiStruct()
- case *AsPathPrependAction:
- as.AsPrepend = a.(*AsPathPrependAction).ToApiStruct()
- case *ExtCommunityAction:
- as.ExtCommunity = a.(*ExtCommunityAction).ToApiStruct()
- case *NexthopAction:
- as.Nexthop = a.(*NexthopAction).ToApiStruct()
- }
- }
- return &api.Statement{
- Name: s.Name,
- Conditions: cs,
- Actions: as,
+func (s *Statement) ToConfig() *config.Statement {
+ return &config.Statement{
+ Name: s.Name,
+ Conditions: func() config.Conditions {
+ cond := config.Conditions{}
+ for _, c := range s.Conditions {
+ switch c.(type) {
+ case *PrefixCondition:
+ v := c.(*PrefixCondition)
+ cond.MatchPrefixSet = config.MatchPrefixSet{PrefixSet: v.set.Name(), MatchSetOptions: config.IntToMatchSetOptionsRestrictedTypeMap[int(v.option)]}
+ case *NeighborCondition:
+ v := c.(*NeighborCondition)
+ cond.MatchNeighborSet = config.MatchNeighborSet{NeighborSet: v.set.Name(), MatchSetOptions: config.IntToMatchSetOptionsRestrictedTypeMap[int(v.option)]}
+ case *AsPathLengthCondition:
+ v := c.(*AsPathLengthCondition)
+ cond.BgpConditions.AsPathLength = config.AsPathLength{Operator: config.IntToAttributeComparisonMap[int(v.operator)], Value: v.length}
+ case *AsPathCondition:
+ v := c.(*AsPathCondition)
+ cond.BgpConditions.MatchAsPathSet = config.MatchAsPathSet{AsPathSet: v.set.Name(), MatchSetOptions: config.IntToMatchSetOptionsTypeMap[int(v.option)]}
+ case *CommunityCondition:
+ v := c.(*CommunityCondition)
+ cond.BgpConditions.MatchCommunitySet = config.MatchCommunitySet{CommunitySet: v.set.Name(), MatchSetOptions: config.IntToMatchSetOptionsTypeMap[int(v.option)]}
+ case *ExtCommunityCondition:
+ v := c.(*ExtCommunityCondition)
+ cond.BgpConditions.MatchExtCommunitySet = config.MatchExtCommunitySet{ExtCommunitySet: v.set.Name(), MatchSetOptions: config.IntToMatchSetOptionsTypeMap[int(v.option)]}
+ case *RpkiValidationCondition:
+ v := c.(*RpkiValidationCondition)
+ cond.BgpConditions.RpkiValidationResult = v.result
+ }
+ }
+ return cond
+ }(),
+ Actions: func() config.Actions {
+ act := config.Actions{}
+ if s.RouteAction != nil && !reflect.ValueOf(s.RouteAction).IsNil() {
+ a := s.RouteAction.(*RoutingAction)
+ act.RouteDisposition = config.RouteDisposition{AcceptRoute: a.AcceptRoute, RejectRoute: false}
+ }
+ for _, a := range s.ModActions {
+ switch a.(type) {
+ case *AsPathPrependAction:
+ act.BgpActions.SetAsPathPrepend = *a.(*AsPathPrependAction).ToConfig()
+ case *CommunityAction:
+ act.BgpActions.SetCommunity = *a.(*CommunityAction).ToConfig()
+ case *ExtCommunityAction:
+ act.BgpActions.SetExtCommunity = *a.(*ExtCommunityAction).ToConfig()
+ case *MedAction:
+ act.BgpActions.SetMed = a.(*MedAction).ToConfig()
+ case *LocalPrefAction:
+ act.BgpActions.SetLocalPref = a.(*LocalPrefAction).ToConfig()
+ case *NexthopAction:
+ act.BgpActions.SetNextHop = a.(*NexthopAction).ToConfig()
+ }
+ }
+ return act
+ }(),
}
}
@@ -2588,12 +2531,12 @@ func (p *Policy) Apply(path *Path, options *PolicyOptions) (RouteType, *Path) {
return ROUTE_TYPE_NONE, path
}
-func (p *Policy) ToApiStruct() *api.Policy {
- ss := make([]*api.Statement, 0, len(p.Statements))
+func (p *Policy) ToConfig() *config.PolicyDefinition {
+ ss := make([]config.Statement, 0, len(p.Statements))
for _, s := range p.Statements {
- ss = append(ss, s.ToApiStruct())
+ ss = append(ss, *s.ToConfig())
}
- return &api.Policy{
+ return &config.PolicyDefinition{
Name: p.name,
Statements: ss,
}