diff options
author | FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp> | 2018-08-18 22:02:06 +0900 |
---|---|---|
committer | FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp> | 2018-08-23 10:05:19 +0900 |
commit | 008c961ecd943739c5db63fcd931904804a45aa5 (patch) | |
tree | a56ebdc908ce1b387c030cf6223e7f5f5d76d5c8 /pkg/server/server.go | |
parent | 7e07240b292946c5fdd281fcc06d0cb8438c349a (diff) |
policy cleanup
- remove ReplaceDefinedSet and ReplaceStatement APIs; not intutive and
should create a new one instead of modifying the existing.
- Rename ReplacePolicyAssignment to SetPolicyAssignment API; we use
internally SetPolicy() name from the beginning.
- Rename UpdatePolicy() to SetPolicies() API; It doesn't update
anything so the name is confusing. It discards the all policies and
create policies from the argument.
- Changes some member names in structures for policy.
Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
Diffstat (limited to 'pkg/server/server.go')
-rw-r--r-- | pkg/server/server.go | 118 |
1 files changed, 56 insertions, 62 deletions
diff --git a/pkg/server/server.go b/pkg/server/server.go index c69ebbfb..c76ec45d 100644 --- a/pkg/server/server.go +++ b/pkg/server/server.go @@ -1613,22 +1613,63 @@ func (s *BgpServer) StopBgp(ctx context.Context, r *api.StopBgpRequest) error { return nil } -func (s *BgpServer) UpdatePolicy(ctx context.Context, r *api.UpdatePolicyRequest) error { +func (s *BgpServer) SetPolicies(ctx context.Context, r *api.SetPoliciesRequest) error { rp, err := NewRoutingPolicyFromApiStruct(r) if err != nil { return err } + getConfig := func(id string) (*config.ApplyPolicy, error) { + f := func(id string, dir table.PolicyDirection) (config.DefaultPolicyType, []string, error) { + rt, policies, err := s.policy.GetPolicyAssignment(id, dir) + if err != nil { + return config.DEFAULT_POLICY_TYPE_REJECT_ROUTE, nil, err + } + names := make([]string, 0, len(policies)) + for _, p := range policies { + names = append(names, p.Name) + } + t := config.DEFAULT_POLICY_TYPE_ACCEPT_ROUTE + if rt == table.ROUTE_TYPE_REJECT { + t = config.DEFAULT_POLICY_TYPE_REJECT_ROUTE + } + return t, names, nil + } + + c := &config.ApplyPolicy{} + rt, policies, err := f(id, table.POLICY_DIRECTION_IMPORT) + if err != nil { + return nil, err + } + c.Config.ImportPolicyList = policies + c.Config.DefaultImportPolicy = rt + rt, policies, err = f(id, table.POLICY_DIRECTION_EXPORT) + if err != nil { + return nil, err + } + c.Config.ExportPolicyList = policies + c.Config.DefaultExportPolicy = rt + return c, nil + } + return s.mgmtOperation(func() error { ap := make(map[string]config.ApplyPolicy, len(s.neighborMap)+1) - ap[table.GLOBAL_RIB_NAME] = s.bgpConfig.Global.ApplyPolicy + a, err := getConfig(table.GLOBAL_RIB_NAME) + if err != nil { + return err + } + ap[table.GLOBAL_RIB_NAME] = *a for _, peer := range s.neighborMap { peer.fsm.lock.RLock() log.WithFields(log.Fields{ "Topic": "Peer", "Key": peer.fsm.pConf.State.NeighborAddress, }).Info("call set policy") - ap[peer.ID()] = peer.fsm.pConf.ApplyPolicy + a, err := getConfig(peer.ID()) + if err != nil { + return err + } + ap[peer.ID()] = *a peer.fsm.lock.RUnlock() } return s.policy.Reset(rp, ap) @@ -2970,10 +3011,10 @@ func (s *BgpServer) ListDefinedSet(ctx context.Context, r *api.ListDefinedSetReq func (s *BgpServer) AddDefinedSet(ctx context.Context, r *api.AddDefinedSetRequest) error { return s.mgmtOperation(func() error { - if r == nil || r.Set == nil { + if r == nil || r.DefinedSet == nil { return fmt.Errorf("invalid request") } - set, err := NewDefinedSetFromApiStruct(r.Set) + set, err := NewDefinedSetFromApiStruct(r.DefinedSet) if err != nil { return err } @@ -2983,10 +3024,10 @@ func (s *BgpServer) AddDefinedSet(ctx context.Context, r *api.AddDefinedSetReque func (s *BgpServer) DeleteDefinedSet(ctx context.Context, r *api.DeleteDefinedSetRequest) error { return s.mgmtOperation(func() error { - if r == nil || r.Set == nil { + if r == nil || r.DefinedSet == nil { return fmt.Errorf("invalid request") } - set, err := NewDefinedSetFromApiStruct(r.Set) + set, err := NewDefinedSetFromApiStruct(r.DefinedSet) if err != nil { return err } @@ -2994,19 +3035,6 @@ func (s *BgpServer) DeleteDefinedSet(ctx context.Context, r *api.DeleteDefinedSe }, false) } -func (s *BgpServer) ReplaceDefinedSet(ctx context.Context, r *api.ReplaceDefinedSetRequest) error { - return s.mgmtOperation(func() error { - if r == nil || r.Set == nil { - return fmt.Errorf("invalid request") - } - set, err := NewDefinedSetFromApiStruct(r.Set) - if err != nil { - return err - } - return s.policy.ReplaceDefinedSet(set) - }, false) -} - func (s *BgpServer) ListStatement(ctx context.Context, r *api.ListStatementRequest) ([]*api.Statement, error) { l := make([]*api.Statement, 0) s.mgmtOperation(func() error { @@ -3044,19 +3072,6 @@ func (s *BgpServer) DeleteStatement(ctx context.Context, r *api.DeleteStatementR }, false) } -func (s *BgpServer) ReplaceStatement(ctx context.Context, r *api.ReplaceStatementRequest) error { - return s.mgmtOperation(func() error { - if r == nil || r.Statement == nil { - return fmt.Errorf("invalid request") - } - st, err := NewStatementFromApiStruct(r.Statement) - if err == nil { - err = s.policy.ReplaceStatement(st) - } - return err - }, false) -} - func (s *BgpServer) ListPolicy(ctx context.Context, r *api.ListPolicyRequest) ([]*api.Policy, error) { l := make([]*api.Policy, 0) s.mgmtOperation(func() error { @@ -3101,19 +3116,6 @@ func (s *BgpServer) DeletePolicy(ctx context.Context, r *api.DeletePolicyRequest }, false) } -func (s *BgpServer) ReplacePolicy(ctx context.Context, r *api.ReplacePolicyRequest) error { - return s.mgmtOperation(func() error { - if r == nil || r.Policy == nil { - return fmt.Errorf("invalid request") - } - p, err := NewPolicyFromApiStruct(r.Policy) - if err == nil { - err = s.policy.ReplacePolicy(p, r.ReferExistingStatements, r.PreserveStatements) - } - return err - }, false) -} - func (s *BgpServer) toPolicyInfo(name string, dir api.PolicyDirection) (string, table.PolicyDirection, error) { if name == "" { return "", table.POLICY_DIRECTION_NONE, fmt.Errorf("empty table name") @@ -3169,21 +3171,13 @@ func (s *BgpServer) ListPolicyAssignment(ctx context.Context, r *api.ListPolicyA if err != nil { return err } - rt, l, err := s.policy.GetPolicyAssignment(id, dir) + rt, policies, err := s.policy.GetPolicyAssignment(id, dir) if err != nil { return err } - if len(l) == 0 { + if len(policies) == 0 { continue } - policies := make([]*table.Policy, 0, len(l)) - for _, p := range l { - np, err := table.NewPolicy(*p) - if err != nil { - return err - } - policies = append(policies, np) - } t := &table.PolicyAssignment{ Name: name, Type: dir, @@ -3203,11 +3197,11 @@ func (s *BgpServer) AddPolicyAssignment(ctx context.Context, r *api.AddPolicyAss if r == nil || r.Assignment == nil { return fmt.Errorf("invalid request") } - id, dir, err := s.toPolicyInfo(r.Assignment.Name, r.Assignment.Type) + id, dir, err := s.toPolicyInfo(r.Assignment.Name, r.Assignment.Direction) if err != nil { return err } - return s.policy.AddPolicyAssignment(id, dir, toPolicyDefinition(r.Assignment.Policies), defaultRouteType(r.Assignment.Default)) + return s.policy.AddPolicyAssignment(id, dir, toPolicyDefinition(r.Assignment.Policies), defaultRouteType(r.Assignment.DefaultAction)) }, false) } @@ -3216,7 +3210,7 @@ func (s *BgpServer) DeletePolicyAssignment(ctx context.Context, r *api.DeletePol if r == nil || r.Assignment == nil { return fmt.Errorf("invalid request") } - id, dir, err := s.toPolicyInfo(r.Assignment.Name, r.Assignment.Type) + id, dir, err := s.toPolicyInfo(r.Assignment.Name, r.Assignment.Direction) if err != nil { return err } @@ -3224,16 +3218,16 @@ func (s *BgpServer) DeletePolicyAssignment(ctx context.Context, r *api.DeletePol }, false) } -func (s *BgpServer) ReplacePolicyAssignment(ctx context.Context, r *api.ReplacePolicyAssignmentRequest) error { +func (s *BgpServer) SetPolicyAssignment(ctx context.Context, r *api.SetPolicyAssignmentRequest) error { return s.mgmtOperation(func() error { if r == nil || r.Assignment == nil { return fmt.Errorf("invalid request") } - id, dir, err := s.toPolicyInfo(r.Assignment.Name, r.Assignment.Type) + id, dir, err := s.toPolicyInfo(r.Assignment.Name, r.Assignment.Direction) if err != nil { return err } - return s.policy.ReplacePolicyAssignment(id, dir, toPolicyDefinition(r.Assignment.Policies), defaultRouteType(r.Assignment.Default)) + return s.policy.SetPolicyAssignment(id, dir, toPolicyDefinition(r.Assignment.Policies), defaultRouteType(r.Assignment.DefaultAction)) }, false) } |