-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathroleutils.go
More file actions
147 lines (136 loc) · 3.75 KB
/
roleutils.go
File metadata and controls
147 lines (136 loc) · 3.75 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
137
138
139
140
141
142
143
144
145
146
147
/*
* === This file is part of ALICE O² ===
*
* Copyright 2020 CERN and copyright holders of ALICE O².
* Author: Teo Mrnjavac <teo.mrnjavac@cern.ch>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* In applying this license CERN does not waive the privileges and
* immunities granted to it by virtue of its status as an
* Intergovernmental Organization or submit itself to any jurisdiction.
*/
package workflow
import (
"github.com/AliceO2Group/Control/configuration/template"
"github.com/AliceO2Group/Control/core/task"
"github.com/AliceO2Group/Control/core/task/constraint"
)
func WrapConstraints(items constraint.Constraints) template.Fields {
fields := make(template.Fields, 0)
for i := range items {
index := i // we need a local copy for the getter/setter closures
fields = append(fields, &template.GenericWrapper{
Getter: func() string {
return items[index].Value
},
Setter: func(value string) {
items[index].Value = value
},
})
}
return fields
}
func GetRoot(role Role) (root Role) {
if role == nil {
return nil
}
current := role
for {
if parent := current.GetParentRole(); parent != nil {
// if there's a parent, we go up
current = parent
continue
}
// it has no parent, so...
return current
}
}
func Walk(root Role, do func(role Role)) {
switch typed := root.(type) {
case *aggregatorRole:
do(typed)
for _, child := range typed.Roles {
Walk(child, do)
}
case *iteratorRole:
do(typed)
for _, child := range typed.Roles {
Walk(child, do)
}
case *includeRole:
do(typed)
for _, child := range typed.Roles {
Walk(child, do)
}
case *taskRole:
do(typed)
case *callRole:
do(typed)
}
}
func LeafWalk(root Role, do func(role Role)) {
switch typed := root.(type) {
case *aggregatorRole:
for _, child := range typed.Roles {
LeafWalk(child, do)
}
case *iteratorRole:
for _, child := range typed.Roles {
LeafWalk(child, do)
}
case *includeRole:
for _, child := range typed.Roles {
LeafWalk(child, do)
}
case *taskRole:
do(typed)
case *callRole:
do(typed)
}
}
// LinkChildrenToParents walks through the role tree and sets the correct parent for each child
func LinkChildrenToParents(root Role) {
var setParents func(role Role, parent Role)
setParents = func(role Role, parent Role) {
if typedParent, ok := parent.(Updatable); ok {
role.setParent(typedParent)
}
for _, child := range role.GetRoles() {
setParents(child, role)
}
}
setParents(root, nil)
}
func MakeDisabledRoleCallback(r Role) func(stage template.Stage, err error) error {
return func(stage template.Stage, err error) error {
if stage == template.STAGE0 { // only `enabled` has been processed so far
if !r.IsEnabled() {
rde := &template.RoleDisabledError{RolePath: r.GetPath()}
return rde
}
}
return err
}
}
func GetActiveTasks(r Role) task.Tasks {
return r.GetTasks().Filtered(func(t *task.Task) bool {
// the parent role of a task is taskRole, thus we are in fact accessing the status of the task.
// t.status is private to the task package, thus we cannot access it directly.
if pr, ok := t.GetParentRole().(Role); ok {
return pr.GetStatus() == task.ACTIVE
}
return false
})
}