-
Notifications
You must be signed in to change notification settings - Fork 1
/
ref_manager.go
232 lines (205 loc) · 6.7 KB
/
ref_manager.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package component
import (
"fmt"
"log"
"github.com/ekara-platform/model"
)
type (
//ReferenceManager manages all the components declared and used into a descriptor
ReferenceManager struct {
l *log.Logger
cm *manager
//usedReferences stores the references of all components used
// into the parsed descriptors and all its parents
UsedReferences *model.UsedReferences
Orphans *model.Orphans
//referencedComponents stores the references of all components declared
// into the parsed descriptors and all its parents
ReferencedComponents *model.ReferencedComponents
Parents []ParentRef
rootComponent model.Component
rootComponents *model.ReferencedComponents
SortedFetchedComponents []string
}
//ParentRef defines the reference to the descriptor's parent
ParentRef struct {
Comp model.Component
ReferencedComponents *model.ReferencedComponents
}
)
//CreateReferenceManager creates a new references manager
func CreateReferenceManager(cm *manager) *ReferenceManager {
return &ReferenceManager{
cm: cm,
UsedReferences: model.CreateUsedReferences(),
Orphans: model.CreateOrphans(),
ReferencedComponents: model.CreateReferencedComponents(),
Parents: make([]ParentRef, 0, 0),
SortedFetchedComponents: make([]string, 0, 0),
rootComponents: model.CreateReferencedComponents(),
}
}
func (rm *ReferenceManager) init(c model.Component) error {
rm.cm.lC.Log().Println("Parsing the main descriptor")
rm.rootComponent = c
fComp, err := fetch(rm.cm, c)
if err != nil {
return err
}
rm.cm.Paths[c.Id] = fComp
refs, err := model.ParseYamlDescriptorReferences(fComp.DescriptorUrl, rm.cm.TemplateContext())
if err != nil {
return err
}
// Keep all the references used into the main descriptor
u, o := refs.Uses(rm.Orphans)
for id := range u.Refs {
rm.cm.lC.Log().Printf("Component used by the main descriptor %s", id)
rm.UsedReferences.AddReference(id)
}
for id := range o.Refs {
rm.cm.lC.Log().Printf("Orphan used by the main descriptor %s", id)
rm.Orphans.AddReference(id)
}
referenced, err := refs.References(c.Id)
if err != nil {
return err
}
for _, v := range referenced.Refs {
// Keep all the component declared into the main descriptor
rm.ReferencedComponents.AddReference(v)
rm.cm.lC.Log().Printf("Adding %s as component into the main descriptor", v.Component.Id)
rm.rootComponents.AddReference(v)
}
// if exists we parse the parent of the descriptor
parent, b, err := refs.Parent()
if err != nil {
return err
}
if b {
parentC, _ := parent.Component()
err = rm.parseParent(parentC, rm.cm.TemplateContext())
if err != nil {
return err
}
}
return nil
}
func (rm *ReferenceManager) parseParent(p model.Component, data *model.TemplateContext) error {
rm.cm.lC.Log().Printf("Parsing parent %s", p.Repository)
p.Id = fmt.Sprintf("%s%d", p.Id, len(rm.Parents)+1)
rm.cm.lC.Log().Printf("Parent Renamed %s", p.Id)
fComp, err := fetch(rm.cm, p)
if err != nil {
rm.cm.lC.Log().Printf("error fetching the parent %s", err.Error())
return err
}
refs, err := model.ParseYamlDescriptorReferences(fComp.DescriptorUrl, data)
if err != nil {
rm.cm.lC.Log().Printf("error parsing the parent references %s", err.Error())
return err
}
// Keep all the references used into the parent
u, o := refs.Uses(rm.Orphans)
for id := range u.Refs {
rm.cm.lC.Log().Printf("Component used by the parent %s", id)
rm.UsedReferences.AddReference(id)
}
for id := range o.Refs {
rm.cm.lC.Log().Printf("Orphan used by the parent %s", id)
rm.Orphans.AddReference(id)
}
pRef := ParentRef{
Comp: p,
ReferencedComponents: model.CreateReferencedComponents(),
}
// Keep only the references of the new declaration of compoments
referenced, err := refs.References(p.Id)
if err != nil {
return err
}
for _, v := range referenced.Refs {
rm.cm.lC.Log().Printf("Component referenced by the parent %s", v.Component.Repository)
added := rm.ReferencedComponents.AddReference(v)
if added {
rm.cm.lC.Log().Println("Referenced added")
pRef.ReferencedComponents.AddReference(v)
} else {
rm.cm.lC.Log().Println("Referenced ignored")
}
}
rm.Parents = append(rm.Parents, pRef)
// If the descriptor has a parent then we start fetching all the parents
parent, b, err := refs.Parent()
if err != nil {
return err
}
if b {
parentC, _ := parent.Component()
return rm.parseParent(parentC, data)
}
return nil
}
func (rm *ReferenceManager) ensure() error {
// The parents content must be processed fist
for i := len(rm.Parents) - 1; i >= 0; i-- {
p := rm.Parents[i]
// we must process the parent's declared components before the parent itself
// this will be done based on the alphabetical order of the component names
for _, c := range p.ReferencedComponents.Sorted() {
rm.cm.lC.Log().Printf("Reference manager, ensuring parent component %s", c.Id)
if rm.UsedReferences.IdUsed(c.Id) {
err := rm.callEnsure(c, rm.cm.TemplateContext())
if err != nil {
return err
}
} else {
rm.cm.lC.Log().Printf("Reference manager: unused parent component %s", c.Id)
}
}
//Once the declared components have been processed we can process the parent
rm.cm.lC.Log().Printf("Reference manager, ensuring parent %s", p.Comp.Id)
err := rm.callEnsure(p.Comp, rm.cm.TemplateContext())
if err != nil {
return err
}
}
// Once all the parents have been processed we must process
// all the components declared into the main descriptor, this
// is alo done in alphabetical order
for _, c := range rm.rootComponents.Sorted() {
rm.cm.lC.Log().Printf("Reference manager, ensuring descriptor component %s", c.Id)
if rm.UsedReferences.IdUsed(c.Id) {
err := rm.callEnsure(c, rm.cm.TemplateContext())
if err != nil {
return err
}
} else {
rm.cm.lC.Log().Printf("Reference manager: unused descriptor component %s", c.Id)
}
}
rm.cm.lC.Log().Printf("Reference manager ensuring, main descriptor %s", rm.rootComponent.Id)
// Root component is the main descriptor
err := rm.callEnsure(rm.rootComponent, rm.cm.TemplateContext())
if err != nil {
return err
}
return nil
}
func (rm *ReferenceManager) callEnsure(c model.Component, data *model.TemplateContext) error {
rm.cm.Environment().Platform().AddComponent(c)
err := rm.cm.ensureOneComponent(c, data)
if err != nil {
return err
}
rm.SortedFetchedComponents = append(rm.SortedFetchedComponents, c.Id)
return nil
}
//Component returns the referenced component
func (p ParentRef) Component() (model.Component, error) {
return p.Comp, nil
}
//ComponentName returns the referenced component name
func (p ParentRef) ComponentName() string {
return p.Comp.Id
}