From 98a539a2e921151cf37d28ce0f2789ec3d003457 Mon Sep 17 00:00:00 2001 From: Carl Baldwin Date: Fri, 11 Oct 2019 17:32:47 -0600 Subject: Split Reset into three methods The Reset method was difficult to understand. The reason is that it was called in three different ways and did different things in each case. It is easier to read when the three different modes are each their own method. This came up as I was looking deeper into the threading model around policies. I think this change makes it easier to understand the code. --- internal/pkg/table/policy.go | 61 ++++++++++++++++++++++++++++++++------------ 1 file changed, 44 insertions(+), 17 deletions(-) (limited to 'internal') diff --git a/internal/pkg/table/policy.go b/internal/pkg/table/policy.go index 3395863d..35fa8c58 100644 --- a/internal/pkg/table/policy.go +++ b/internal/pkg/table/policy.go @@ -3832,32 +3832,59 @@ func (r *RoutingPolicy) SetPolicyAssignment(id string, dir PolicyDirection, poli return err } -func (r *RoutingPolicy) Reset(rp *config.RoutingPolicy, ap map[string]config.ApplyPolicy) error { +func (r *RoutingPolicy) Initialize() error { r.mu.Lock() defer r.mu.Unlock() - if rp != nil { - if err := r.reload(*rp); err != nil { + if err := r.reload(config.RoutingPolicy{}); err != nil { + log.WithFields(log.Fields{ + "Topic": "Policy", + }).Errorf("failed to create routing policy: %s", err) + return err + } + return nil +} + +func (r *RoutingPolicy) setPeerPolicy(id string, c config.ApplyPolicy) { + for _, dir := range []PolicyDirection{POLICY_DIRECTION_IMPORT, POLICY_DIRECTION_EXPORT} { + ps, def, err := r.getAssignmentFromConfig(dir, c) + if err != nil { log.WithFields(log.Fields{ "Topic": "Policy", - }).Errorf("failed to create routing policy: %s", err) - return err + "Dir": dir, + }).Errorf("failed to get policy info: %s", err) + continue } + r.setDefaultPolicy(id, dir, def) + r.setPolicy(id, dir, ps) + } +} + +func (r *RoutingPolicy) SetPeerPolicy(peerId string, c config.ApplyPolicy) error { + r.mu.Lock() + defer r.mu.Unlock() + + r.setPeerPolicy(peerId, c) + return nil +} + +func (r *RoutingPolicy) Reset(rp *config.RoutingPolicy, ap map[string]config.ApplyPolicy) error { + if rp == nil { + return fmt.Errorf("routing Policy is nil in call to Reset") + } + + r.mu.Lock() + defer r.mu.Unlock() + + if err := r.reload(*rp); err != nil { + log.WithFields(log.Fields{ + "Topic": "Policy", + }).Errorf("failed to create routing policy: %s", err) + return err } for id, c := range ap { - for _, dir := range []PolicyDirection{POLICY_DIRECTION_IMPORT, POLICY_DIRECTION_EXPORT} { - ps, def, err := r.getAssignmentFromConfig(dir, c) - if err != nil { - log.WithFields(log.Fields{ - "Topic": "Policy", - "Dir": dir, - }).Errorf("failed to get policy info: %s", err) - continue - } - r.setDefaultPolicy(id, dir, def) - r.setPolicy(id, dir, ps) - } + r.setPeerPolicy(id, c) } return nil } -- cgit v1.2.3