-
Notifications
You must be signed in to change notification settings - Fork 9
/
registry.go
260 lines (235 loc) · 7.05 KB
/
registry.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/*
Copyright (c) 2021 Nordix Foundation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package registry
import (
nspAPI "github.com/nordix/meridio/api/nsp/v1"
)
// types of resource
const (
Trench = iota
Conduit
Stream
Flow
Vip
Attractor
Gateway
)
type ResourceType int
// ConfigurationEvent reprensent the struct which will be
// sent on any resource modification
type ConfigurationEvent struct {
ResourceType ResourceType
}
// ConfigurationRegistry is a memory registry for the meridio
// configuration. It can register and get all resources: trench
// conduits, streams, flows, vips, attractors and gateways.
// On any resource modification, a event is sent via a channel
type ConfigurationRegistry struct {
// Chan is the channel which will send the event on any
// resource modification
Chan chan<- *ConfigurationEvent
trench *nspAPI.Trench
conduits []*nspAPI.Conduit
streams []*nspAPI.Stream
flows []*nspAPI.Flow
vips []*nspAPI.Vip
attractors []*nspAPI.Attractor
gateways []*nspAPI.Gateway
}
// New is the constructor of ConfigurationRegistry.
func New(configurationEventChan chan<- *ConfigurationEvent) *ConfigurationRegistry {
configurationRegistry := &ConfigurationRegistry{
Chan: configurationEventChan,
trench: nil,
conduits: []*nspAPI.Conduit{},
streams: []*nspAPI.Stream{},
flows: []*nspAPI.Flow{},
vips: []*nspAPI.Vip{},
attractors: []*nspAPI.Attractor{},
gateways: []*nspAPI.Gateway{},
}
return configurationRegistry
}
// SetTrench sets the trench and notifies the change via the channel
// with the resource type as Trench
func (cr *ConfigurationRegistry) SetTrench(trench *nspAPI.Trench) {
cr.trench = trench
cr.notify(&ConfigurationEvent{
ResourceType: Trench,
})
}
// GetTrench returns a trench with the same name as the one in parameter
// if existing. If the trench in parameter is nil, the registered trench
// will be returned
func (cr *ConfigurationRegistry) GetTrench(trench *nspAPI.Trench) *nspAPI.Trench {
if cr.trench == nil {
return nil
}
if trench == nil {
return cr.trench
}
if trench.Name == cr.trench.Name {
return cr.trench
}
return nil
}
// SetConduits sets the conduits and notifies the change via the channel
// with the resource type as Conduit
func (cr *ConfigurationRegistry) SetConduits(conduits []*nspAPI.Conduit) {
cr.conduits = conduits
cr.notify(&ConfigurationEvent{
ResourceType: Conduit,
})
}
// GetConduits returns conduits with the same name (ignored if empty) and
// the same parent as the one in parameter. If the conduit in parameter is
// nil, all conduits will be returned
func (cr *ConfigurationRegistry) GetConduits(conduit *nspAPI.Conduit) []*nspAPI.Conduit {
conduits := []*nspAPI.Conduit{}
if conduit == nil {
return cr.conduits
}
for _, c := range cr.conduits {
if !c.Equals(conduit) {
continue
}
conduits = append(conduits, c)
}
return conduits
}
// SetStreams sets the streams and notifies the change via the channel
// with the resource type as Stream
func (cr *ConfigurationRegistry) SetStreams(streams []*nspAPI.Stream) {
cr.streams = streams
cr.notify(&ConfigurationEvent{
ResourceType: Stream,
})
}
// GetStreams returns streams with the same name (ignored if empty) and
// the same parent as the one in parameter. If the stream in parameter is
// nil, all streams will be returned
func (cr *ConfigurationRegistry) GetStreams(stream *nspAPI.Stream) []*nspAPI.Stream {
streams := []*nspAPI.Stream{}
if stream == nil {
return cr.streams
}
for _, s := range cr.streams {
if !s.Equals(stream) {
continue
}
streams = append(streams, s)
}
return streams
}
// SetFlows sets the flows and notifies the change via the channel
// with the resource type as Flow
func (cr *ConfigurationRegistry) SetFlows(flows []*nspAPI.Flow) {
cr.flows = flows
cr.notify(&ConfigurationEvent{
ResourceType: Flow,
})
}
// GetFlows returns flows with the same name (ignored if empty) and
// the same parent as the one in parameter. If the flow in parameter is
// nil, all flows will be returned
func (cr *ConfigurationRegistry) GetFlows(flow *nspAPI.Flow) []*nspAPI.Flow {
flows := []*nspAPI.Flow{}
if flow == nil {
return cr.flows
}
for _, f := range cr.flows {
if !f.Equals(flow) {
continue
}
flows = append(flows, f)
}
return flows
}
// SetVips sets the vips and notifies the change via the channel
// with the resource type as Vip
func (cr *ConfigurationRegistry) SetVips(vips []*nspAPI.Vip) {
cr.vips = vips
cr.notify(&ConfigurationEvent{
ResourceType: Vip,
})
}
// GetVips returns vips with the same name (ignored if empty) and
// the same parent as the one in parameter. If the vip in parameter is
// nil, all vips will be returned
func (cr *ConfigurationRegistry) GetVips(vip *nspAPI.Vip) []*nspAPI.Vip {
vips := []*nspAPI.Vip{}
if vip == nil {
return cr.vips
}
for _, v := range cr.vips {
if !v.Equals(vip) {
continue
}
vips = append(vips, v)
}
return vips
}
// SetAttractors sets the attractors and notifies the change via the channel
// with the resource type as Attractor
func (cr *ConfigurationRegistry) SetAttractors(attractors []*nspAPI.Attractor) {
cr.attractors = attractors
cr.notify(&ConfigurationEvent{
ResourceType: Attractor,
})
}
// GetAttractors returns attractors with the same name (ignored if empty) and
// the same parent as the one in parameter. If the attractor in parameter is
// nil, all attractors will be returned
func (cr *ConfigurationRegistry) GetAttractors(attractor *nspAPI.Attractor) []*nspAPI.Attractor {
attractors := []*nspAPI.Attractor{}
if attractor == nil {
return cr.attractors
}
for _, a := range cr.attractors {
if !a.Equals(attractor) {
continue
}
attractors = append(attractors, a)
}
return attractors
}
// SetGateways sets the gateways and notifies the change via the channel
// with the resource type as Gateway
func (cr *ConfigurationRegistry) SetGateways(gateways []*nspAPI.Gateway) {
cr.gateways = gateways
cr.notify(&ConfigurationEvent{
ResourceType: Gateway,
})
}
// GetGateways returns gateways with the same name (ignored if empty) and
// the same parent as the one in parameter. If the gateway in parameter is
// nil, all gateways will be returned
func (cr *ConfigurationRegistry) GetGateways(gateway *nspAPI.Gateway) []*nspAPI.Gateway {
gateways := []*nspAPI.Gateway{}
if gateway == nil {
return cr.gateways
}
for _, g := range cr.gateways {
if !g.Equals(gateway) {
continue
}
gateways = append(gateways, g)
}
return gateways
}
func (cr *ConfigurationRegistry) notify(configurationEvent *ConfigurationEvent) {
if cr.Chan == nil {
return
}
cr.Chan <- configurationEvent
}