-
Notifications
You must be signed in to change notification settings - Fork 98
/
business_policy.go
207 lines (172 loc) · 7.45 KB
/
business_policy.go
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package businesspolicy
import (
"fmt"
"github.com/golang/glog"
"github.com/open-horizon/anax/externalpolicy"
"github.com/open-horizon/anax/i18n"
"github.com/open-horizon/anax/policy"
)
const DEFAULT_MAX_AGREEMENT = 0
// the business policy
type BusinessPolicy struct {
Owner string `json:"owner,omitempty"`
Label string `json:"label"`
Description string `json:"description"`
Service ServiceRef `json:"service"`
Properties externalpolicy.PropertyList `json:"properties,omitempty"`
Constraints externalpolicy.ConstraintExpression `json:"constraints,omitempty"`
UserInput []policy.UserInput `json:"userInput,omitempty"`
}
func (w BusinessPolicy) String() string {
return fmt.Sprintf("Owner: %v, Label: %v, Description: %v, Service: %v, Properties: %v, Constraints: %v, UserInput: %v",
w.Owner,
w.Label,
w.Description,
w.Service,
w.Properties,
w.Constraints,
w.UserInput)
}
type ServiceRef struct {
Name string `json:"name"` // refers to a service definition in the exchange
Org string `json:"org,omitempty"` // the org holding the service definition
Arch string `json:"arch,omitempty"` // the hardware architecture of the service definition
ServiceVersions []WorkloadChoice `json:"serviceVersions,omitempty"` // a list of service version for rollback
NodeH NodeHealth `json:"nodeHealth"` // policy for determining when a node's health is violating its agreements
}
func (w ServiceRef) String() string {
return fmt.Sprintf("Name: %v, Org: %v, Arch: %v, ServiceVersions: %v, NodeH: %v",
w.Name,
w.Org,
w.Arch,
w.ServiceVersions,
w.NodeH)
}
type WorkloadPriority struct {
PriorityValue int `json:"priority_value,omitempty"` // The priority of the workload
Retries int `json:"retries,omitempty"` // The number of retries before giving up and moving to the next priority
RetryDurationS int `json:"retry_durations,omitempty"` // The number of seconds in which the specified number of retries must occur in order for the next priority workload to be attempted.
VerifiedDurationS int `json:"verified_durations,omitempty"` // The number of second in which verified data must exist before the rollback retry feature is turned off
}
func (w WorkloadPriority) String() string {
return fmt.Sprintf("PriorityValue: %v, Retries: %v, RetryDurationS: %v, VerifiedDurationS: %v",
w.PriorityValue,
w.Retries,
w.RetryDurationS,
w.VerifiedDurationS)
}
type UpgradePolicy struct {
Lifecycle string `json:"lifecycle,omitempty"` // immediate, never, agreement
Time string `json:"time,omitempty"` // the time of the upgrade
}
func (w UpgradePolicy) String() string {
return fmt.Sprintf("Lifecycle: %v, Time: %v",
w.Lifecycle,
w.Time)
}
type WorkloadChoice struct {
Version string `json:"version,omitempty"` // the version of the workload
Priority WorkloadPriority `json:"priority,omitempty"` // the highest priority workload is tried first for an agreement, if it fails, the next priority is tried. Priority 1 is the highest, priority 2 is next, etc.
Upgrade UpgradePolicy `json:"upgradePolicy,omitempty"`
}
func (w WorkloadChoice) String() string {
return fmt.Sprintf("Version: %v, Priority: %v, Upgrade: %v",
w.Version,
w.Priority,
w.Upgrade)
}
type NodeHealth struct {
MissingHBInterval int `json:"missing_heartbeat_interval,omitempty"` // How long a heartbeat can be missing until it is considered missing (in seconds)
CheckAgreementStatus int `json:"check_agreement_status,omitempty"` // How often to check that the node agreement entry still exists in the exchange (in seconds)
}
func (w NodeHealth) String() string {
return fmt.Sprintf("MissingHBInterval: %v, CheckAgreementStatus: %v",
w.MissingHBInterval,
w.CheckAgreementStatus)
}
// The validate function returns errors if the policy does not validate. It uses the constraint language
// plugins to handle the constraints field.
func (b *BusinessPolicy) Validate() error {
// get message printer
msgPrinter := i18n.GetMessagePrinter()
// make sure required fields are not empty
if b.Service.Name == "" || b.Service.Org == "" {
return fmt.Errorf(msgPrinter.Sprintf("Name, or Org is empty string."))
} else if b.Service.ServiceVersions == nil || len(b.Service.ServiceVersions) == 0 {
return fmt.Errorf(msgPrinter.Sprintf("The serviceVersions array is empty."))
}
// Validate the PropertyList.
if b != nil && len(b.Properties) != 0 {
if err := b.Properties.Validate(); err != nil {
return fmt.Errorf(msgPrinter.Sprintf("properties contains an invalid property: %v", err))
}
}
// Validate the Constraints expression by invoking the plugins.
if b != nil && len(b.Constraints) != 0 {
_, err := b.Constraints.Validate()
return err
}
// We only get here if the input object is nil OR all of the top level fields are empty.
return nil
}
// Convert business policy to a policy object.
func (b *BusinessPolicy) GenPolicyFromBusinessPolicy(policyName string) (*policy.Policy, error) {
// validate first
if err := b.Validate(); err != nil {
return nil, fmt.Errorf("Failed to validate the business policy: %v", err)
}
service := b.Service
pol := policy.Policy_Factory(fmt.Sprintf("%v", policyName))
// Copy service metadata into the policy
for _, wl := range service.ServiceVersions {
if wl.Version == "" {
return nil, fmt.Errorf("The version for service %v arch %v is empty in the business policy for %v", service.Name, service.Arch, policyName)
}
ConvertChoice(wl, service.Name, service.Org, service.Arch, pol)
}
// properties and constrains
if err := ConvertProperties(b.Properties, pol); err != nil {
return nil, err
}
if err := ConvertConstraints(b.Constraints, pol); err != nil {
return nil, err
}
// node health
ConvertNodeHealth(service.NodeH, pol)
pol.MaxAgreements = DEFAULT_MAX_AGREEMENT
// add default agreement protocol
newAGP := policy.AgreementProtocol_Factory(policy.BasicProtocol)
newAGP.Initialize()
pol.Add_Agreement_Protocol(newAGP)
// make a copy of the user input
pol.UserInput = make([]policy.UserInput, len(b.UserInput))
copy(pol.UserInput, b.UserInput)
glog.V(3).Infof("converted %v into %v", service, pol)
return pol, nil
}
func ConvertChoice(wl WorkloadChoice, url string, org string, arch string, pol *policy.Policy) {
newWL := policy.Workload_Factory(url, org, wl.Version, arch)
newWL.Priority = (*policy.Workload_Priority_Factory(wl.Priority.PriorityValue, wl.Priority.Retries, wl.Priority.RetryDurationS, wl.Priority.VerifiedDurationS))
pol.Add_Workload(newWL)
}
func ConvertNodeHealth(nodeh NodeHealth, pol *policy.Policy) {
// Copy over the node health policy
nh := policy.NodeHealth_Factory(nodeh.MissingHBInterval, nodeh.CheckAgreementStatus)
pol.Add_NodeHealth(nh)
}
func ConvertProperties(properties externalpolicy.PropertyList, pol *policy.Policy) error {
for _, p := range properties {
if err := pol.Add_Property(&p, false); err != nil {
return fmt.Errorf("error trying add external policy property %v to policy. %v", p, err)
}
}
return nil
}
func ConvertConstraints(constraints externalpolicy.ConstraintExpression, pol *policy.Policy) error {
newconstr := externalpolicy.Constraint_Factory()
for _, c := range constraints {
newconstr.Add_Constraint(c)
}
pol.Constraints = *newconstr
return nil
}