From f0b724878360618dd0a950364709b048486771b6 Mon Sep 17 00:00:00 2001 From: FUJITA Tomonori Date: Thu, 16 Aug 2018 22:58:22 +0900 Subject: pkt/server: ListPolicy() and ListStatement() support filtering like ListDefinedSet() and ListPolicyAassignment() Signed-off-by: FUJITA Tomonori --- internal/pkg/table/policy.go | 10 ++++++++-- internal/pkg/table/policy_test.go | 17 +++++++++++++++++ pkg/server/server.go | 12 ++---------- 3 files changed, 27 insertions(+), 12 deletions(-) diff --git a/internal/pkg/table/policy.go b/internal/pkg/table/policy.go index 23c2110c..1f5be006 100644 --- a/internal/pkg/table/policy.go +++ b/internal/pkg/table/policy.go @@ -3566,12 +3566,15 @@ func (r *RoutingPolicy) ReplaceDefinedSet(a DefinedSet) (err error) { return err } -func (r *RoutingPolicy) GetStatement() []*config.Statement { +func (r *RoutingPolicy) GetStatement(name string) []*config.Statement { r.mu.RLock() defer r.mu.RUnlock() l := make([]*config.Statement, 0, len(r.statementMap)) for _, st := range r.statementMap { + if name != "" && name != st.Name { + continue + } l = append(l, st.ToConfig()) } return l @@ -3638,11 +3641,14 @@ func (r *RoutingPolicy) ReplaceStatement(st *Statement) (err error) { return err } -func (r *RoutingPolicy) GetAllPolicy() []*config.PolicyDefinition { +func (r *RoutingPolicy) GetPolicy(name string) []*config.PolicyDefinition { r.mu.RLock() var ps Policies for _, p := range r.policyMap { + if name != "" && name != p.Name { + continue + } ps = append(ps, p) } r.mu.RUnlock() diff --git a/internal/pkg/table/policy_test.go b/internal/pkg/table/policy_test.go index 7f1a1dd9..31889980 100644 --- a/internal/pkg/table/policy_test.go +++ b/internal/pkg/table/policy_test.go @@ -32,6 +32,23 @@ import ( "github.com/stretchr/testify/require" ) +func TestGetStatement(t *testing.T) { + r := NewRoutingPolicy() + r.statementMap["statement1"] = &Statement{Name: "statement1"} + r.statementMap["statement2"] = &Statement{Name: "statement2"} + assert.Equal(t, len(r.GetStatement("")), 2) + assert.Equal(t, len(r.GetStatement("statement1")), 1) + assert.Equal(t, len(r.GetStatement("unknown")), 0) +} + +func TestGetPolicy(t *testing.T) { + r := NewRoutingPolicy() + r.policyMap["p1"] = &Policy{Name: "p1"} + r.policyMap["p2"] = &Policy{Name: "p2"} + assert.Equal(t, len(r.GetPolicy("")), 2) + assert.Equal(t, len(r.GetPolicy("p1")), 1) + assert.Equal(t, len(r.GetPolicy("unknown")), 0) +} func TestPrefixCalcurateNoRange(t *testing.T) { // create path peer := &PeerInfo{AS: 65001, Address: net.ParseIP("10.0.0.1")} diff --git a/pkg/server/server.go b/pkg/server/server.go index 52624b97..356ebf23 100644 --- a/pkg/server/server.go +++ b/pkg/server/server.go @@ -3011,18 +3011,10 @@ func (s *BgpServer) ReplaceDefinedSet(ctx context.Context, r *api.ReplaceDefined }, false) } -func (s *BgpServer) GetStatement() (l []*config.Statement) { - s.mgmtOperation(func() error { - l = s.policy.GetStatement() - return nil - }, false) - return l -} - func (s *BgpServer) ListStatement(ctx context.Context, r *api.ListStatementRequest) ([]*api.Statement, error) { l := make([]*api.Statement, 0) s.mgmtOperation(func() error { - for _, st := range s.policy.GetStatement() { + for _, st := range s.policy.GetStatement(r.Name) { l = append(l, toStatementApi(st)) } return nil @@ -3072,7 +3064,7 @@ func (s *BgpServer) ReplaceStatement(ctx context.Context, r *api.ReplaceStatemen func (s *BgpServer) ListPolicy(ctx context.Context, r *api.ListPolicyRequest) ([]*api.Policy, error) { l := make([]*api.Policy, 0) s.mgmtOperation(func() error { - for _, p := range s.policy.GetAllPolicy() { + for _, p := range s.policy.GetPolicy(r.Name) { l = append(l, toPolicyApi(p)) } return nil -- cgit v1.2.3