-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpolicy_manager.go
More file actions
75 lines (63 loc) · 2.9 KB
/
Copy pathpolicy_manager.go
File metadata and controls
75 lines (63 loc) · 2.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// SPDX-License-Identifier: AGPL-3.0-or-later
package runtime
import (
"github.com/pilot-protocol/common/coreapi"
"github.com/pilot-protocol/common/daemonapi"
)
// AsDaemonPolicyManager adapts a coreapi.PolicyManager (returned by
// plugins/policy.Service.Manager()) to daemonapi.PolicyManager. The two
// interfaces have identical method shapes; the adapter does the
// PolicyRunner element-type conversion on All() and Get().
func AsDaemonPolicyManager(pm coreapi.PolicyManager) daemonapi.PolicyManager {
if pm == nil {
return nil
}
return policyManagerAdapter{inner: pm}
}
type policyManagerAdapter struct{ inner coreapi.PolicyManager }
func (a policyManagerAdapter) Start(netID uint16, policyJSON []byte) (daemonapi.PolicyRunner, error) {
pr, err := a.inner.Start(netID, policyJSON)
if err != nil {
return nil, err
}
return wrapRunner(pr), nil
}
func (a policyManagerAdapter) Stop(netID uint16) { a.inner.Stop(netID) }
func (a policyManagerAdapter) Get(netID uint16) daemonapi.PolicyRunner {
return wrapRunner(a.inner.Get(netID))
}
func (a policyManagerAdapter) All() []daemonapi.PolicyRunner {
src := a.inner.All()
out := make([]daemonapi.PolicyRunner, 0, len(src))
for _, pr := range src {
out = append(out, wrapRunner(pr))
}
return out
}
func (a policyManagerAdapter) StopAll() { a.inner.StopAll() }
func (a policyManagerAdapter) LoadPersisted() error { return a.inner.LoadPersisted() }
// wrapRunner converts a coreapi.PolicyRunner to a daemonapi.PolicyRunner.
// The methods are structurally compatible (PolicyEventType is a type
// alias to string), but Go's named-interface typing requires an
// explicit shim.
func wrapRunner(pr coreapi.PolicyRunner) daemonapi.PolicyRunner {
if pr == nil {
return nil
}
return runnerAdapter{inner: pr}
}
type runnerAdapter struct{ inner coreapi.PolicyRunner }
func (r runnerAdapter) NetworkID() uint16 { return r.inner.NetworkID() }
func (r runnerAdapter) HasMember(peerNodeID uint32) bool { return r.inner.HasMember(peerNodeID) }
func (r runnerAdapter) EvaluatePortGate(eventType string, port uint16, peerNodeID uint32, payloadSize int, direction string, localTags, nodeInfoTags []string) bool {
return r.inner.EvaluatePortGate(eventType, port, peerNodeID, payloadSize, direction, localTags, nodeInfoTags)
}
func (r runnerAdapter) EvaluateActions(eventType string, ctx map[string]any) {
r.inner.EvaluateActions(eventType, ctx)
}
func (r runnerAdapter) Status() map[string]any { return r.inner.Status() }
func (r runnerAdapter) PeerList() []map[string]interface{} { return r.inner.PeerList() }
func (r runnerAdapter) ForceCycle() map[string]any { return r.inner.ForceCycle() }
func (r runnerAdapter) ReconcileNow() { r.inner.ReconcileNow() }
func (r runnerAdapter) PolicyJSON() ([]byte, error) { return r.inner.PolicyJSON() }
func (r runnerAdapter) Stop() { r.inner.Stop() }