-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.go
More file actions
136 lines (113 loc) · 2.94 KB
/
Copy pathutils.go
File metadata and controls
136 lines (113 loc) · 2.94 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package xPlane
import (
"golang.org/x/exp/slices"
)
type ConstraintType int
const (
SENDER ConstraintType = iota
RECEIVER
SENDER_RECEIVER
)
type PolicyFunction struct {
functionName string
constraint ConstraintType
mutability bool
dataplanes []int
}
type Policy struct {
context []string
placement string
functions []PolicyFunction
}
// Accessor methods for PolicyFunction struct.
func (pf *PolicyFunction) GetFunctionName() string {
return pf.functionName
}
func (pf *PolicyFunction) GetConstraint() ConstraintType {
return pf.constraint
}
func (pf *PolicyFunction) GetMutability() bool {
return pf.mutability
}
func (pf *PolicyFunction) GetDataplanes() []int {
return pf.dataplanes
}
// Create a new PolicyFunction struct.
func CreatePolicyFunction(functionName string, constraint ConstraintType, mutability bool) PolicyFunction {
return PolicyFunction{
functionName: functionName,
constraint: constraint,
mutability: mutability,
}
}
// Create a new PolicyFunction struct.
func CreateNewPolicyFunction(functionName string, constraint ConstraintType, supportedDataplanes []int, mutability bool) PolicyFunction {
return PolicyFunction{
functionName: functionName,
constraint: constraint,
mutability: mutability,
dataplanes: supportedDataplanes,
}
}
// Accessor methods for Policy struct.
func (p *Policy) GetContext() []string {
return p.context
}
func (p *Policy) GetPlacement() string {
return p.placement
}
func (p *Policy) SetPlacement(placement string) {
p.placement = placement
}
func (p *Policy) GetFunctions() []PolicyFunction {
return p.functions
}
func (p *Policy) GetDataplanes() []int {
dataplanes := map[int]bool{}
// Add dataplanes of the first function.
for _, d := range p.functions[0].GetDataplanes() {
dataplanes[d] = true
}
// Take intersection of dataplanes of all functions.
for _, pf := range p.functions {
// For every d in dataplanes, check if it exists in pf.dataplanes.
for d := range dataplanes {
if !slices.Contains(pf.GetDataplanes(), d) {
delete(dataplanes, d)
}
}
}
// Whatever left, supports all functions.
var dataplanesArray []int
for d := range dataplanes {
dataplanesArray = append(dataplanesArray, d)
}
return dataplanesArray
}
func (p *Policy) ExistsMutableFunction() bool {
// Iterate over the functions and check if any of them mutate the CNO.
for _, pf := range p.functions {
if pf.GetMutability() {
return true
}
}
return false
}
// Create a new Policy struct.
func CreatePolicy(context []string, functions []PolicyFunction) Policy {
return Policy{
context: context,
placement: "",
functions: functions,
}
}
// Gets the constraint of the policy. No two functions in the policy can have SENDER and RECEIVER constraints.
func (p *Policy) GetConstraint() ConstraintType {
constraint := SENDER_RECEIVER
for _, pf := range p.functions {
if pf.GetConstraint() != SENDER_RECEIVER {
constraint = pf.GetConstraint()
}
}
return constraint
}