This repository has been archived by the owner on Jul 16, 2020. It is now read-only.
/
controller.go
353 lines (296 loc) · 9.57 KB
/
controller.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
// // Copyright (c) 2016 Intel Corporation
//
// 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 testutil
import (
"errors"
"fmt"
"os"
"sync"
"time"
"github.com/ciao-project/ciao/payloads"
"github.com/ciao-project/ciao/ssntp"
"gopkg.in/yaml.v2"
)
// SsntpTestController is global state for the testutil SSNTP controller
type SsntpTestController struct {
Ssntp ssntp.Client
Name string
UUID string
CmdChans map[ssntp.Command]chan Result
CmdChansLock *sync.Mutex
EventChans map[ssntp.Event]chan Result
EventChansLock *sync.Mutex
ErrorChans map[ssntp.Error]chan Result
ErrorChansLock *sync.Mutex
}
// Shutdown shuts down the testutil.SsntpTestClient and cleans up state
func (ctl *SsntpTestController) Shutdown() {
closeControllerChans(ctl)
ctl.Ssntp.Close()
}
// NewSsntpTestControllerConnection creates an SsntpTestController and dials the server.
// Calling with a unique name parameter string for inclusion in the
// SsntpTestClient.Name field aides in debugging. The uuid string
// parameter allows tests to specify a known uuid for simpler tests.
func NewSsntpTestControllerConnection(name string, uuid string) (*SsntpTestController, error) {
if uuid == "" {
return nil, errors.New("no uuid specified")
}
var role ssntp.Role = ssntp.Controller
ctl := &SsntpTestController{
Name: "Test " + role.String() + " " + name,
UUID: uuid,
}
ctl.CmdChansLock = &sync.Mutex{}
ctl.EventChansLock = &sync.Mutex{}
ctl.ErrorChansLock = &sync.Mutex{}
openControllerChans(ctl)
config := &ssntp.Config{
URI: "",
CAcert: ssntp.DefaultCACert,
Cert: ssntp.RoleToDefaultCertName(ssntp.Controller),
Log: ssntp.Log,
UUID: ctl.UUID,
}
if err := ctl.Ssntp.Dial(config, ctl); err != nil {
return nil, err
}
return ctl, nil
}
// AddCmdChan adds an ssntp.Command to the SsntpTestController command channel
func (ctl *SsntpTestController) AddCmdChan(cmd ssntp.Command) chan Result {
c := make(chan Result)
ctl.CmdChansLock.Lock()
ctl.CmdChans[cmd] = c
ctl.CmdChansLock.Unlock()
return c
}
// GetCmdChanResult gets a Result from the SsntpTestController command channel
func (ctl *SsntpTestController) GetCmdChanResult(c chan Result, cmd ssntp.Command) (result Result, err error) {
select {
case result = <-c:
if result.Err != nil {
err = fmt.Errorf("Controller error sending %s command: %s", cmd, result.Err)
}
case <-time.After(25 * time.Second):
err = fmt.Errorf("Timeout waiting for controller %s command result", cmd)
}
return result, err
}
// SendResultAndDelCmdChan deletes an ssntp.Command from the SsntpTestController command channel
func (ctl *SsntpTestController) SendResultAndDelCmdChan(cmd ssntp.Command, result Result) {
ctl.CmdChansLock.Lock()
c, ok := ctl.CmdChans[cmd]
if ok {
delete(ctl.CmdChans, cmd)
ctl.CmdChansLock.Unlock()
c <- result
close(c)
return
}
ctl.CmdChansLock.Unlock()
}
// AddEventChan adds an ssntp.Event to the SsntpTestController event channel
func (ctl *SsntpTestController) AddEventChan(evt ssntp.Event) chan Result {
c := make(chan Result)
ctl.EventChansLock.Lock()
ctl.EventChans[evt] = c
ctl.EventChansLock.Unlock()
return c
}
// GetEventChanResult gets a Result from the SsntpTestController event channel
func (ctl *SsntpTestController) GetEventChanResult(c chan Result, evt ssntp.Event) (result Result, err error) {
select {
case result = <-c:
if result.Err != nil {
err = fmt.Errorf("Controller error sending %s event: %s", evt, result.Err)
}
case <-time.After(25 * time.Second):
err = fmt.Errorf("Timeout waiting for controller %s event result", evt)
}
return result, err
}
// SendResultAndDelEventChan deletes an ssntpEvent from the SsntpTestController event channel
func (ctl *SsntpTestController) SendResultAndDelEventChan(evt ssntp.Event, result Result) {
ctl.EventChansLock.Lock()
c, ok := ctl.EventChans[evt]
if ok {
delete(ctl.EventChans, evt)
ctl.EventChansLock.Unlock()
c <- result
close(c)
return
}
ctl.EventChansLock.Unlock()
}
// AddErrorChan adds an ssntp.Error to the SsntpTestController error channel
func (ctl *SsntpTestController) AddErrorChan(error ssntp.Error) chan Result {
c := make(chan Result)
ctl.ErrorChansLock.Lock()
ctl.ErrorChans[error] = c
ctl.ErrorChansLock.Unlock()
return c
}
// GetErrorChanResult gets a Result from the SsntpTestController error channel
func (ctl *SsntpTestController) GetErrorChanResult(c chan Result, error ssntp.Error) (result Result, err error) {
select {
case result = <-c:
if result.Err != nil {
err = fmt.Errorf("Controller error sending %s error: %s", error, result.Err)
}
case <-time.After(25 * time.Second):
err = fmt.Errorf("Timeout waiting for controller %s error result", error)
}
return result, err
}
// SendResultAndDelErrorChan deletes an ssntp.Error from the SsntpTestController error channel
func (ctl *SsntpTestController) SendResultAndDelErrorChan(error ssntp.Error, result Result) {
ctl.ErrorChansLock.Lock()
c, ok := ctl.ErrorChans[error]
if ok {
delete(ctl.ErrorChans, error)
ctl.ErrorChansLock.Unlock()
c <- result
close(c)
return
}
ctl.ErrorChansLock.Unlock()
}
func openControllerChans(ctl *SsntpTestController) {
ctl.CmdChansLock.Lock()
ctl.CmdChans = make(map[ssntp.Command]chan Result)
ctl.CmdChansLock.Unlock()
ctl.EventChansLock.Lock()
ctl.EventChans = make(map[ssntp.Event]chan Result)
ctl.EventChansLock.Unlock()
ctl.ErrorChansLock.Lock()
ctl.ErrorChans = make(map[ssntp.Error]chan Result)
ctl.ErrorChansLock.Unlock()
}
func closeControllerChans(ctl *SsntpTestController) {
ctl.CmdChansLock.Lock()
for k := range ctl.CmdChans {
close(ctl.CmdChans[k])
delete(ctl.CmdChans, k)
}
ctl.CmdChansLock.Unlock()
ctl.EventChansLock.Lock()
for k := range ctl.EventChans {
close(ctl.EventChans[k])
delete(ctl.EventChans, k)
}
ctl.EventChansLock.Unlock()
ctl.ErrorChansLock.Lock()
for k := range ctl.ErrorChans {
close(ctl.ErrorChans[k])
delete(ctl.ErrorChans, k)
}
ctl.ErrorChansLock.Unlock()
}
// ConnectNotify implements the SSNTP client ConnectNotify callback for SsntpTestController
func (ctl *SsntpTestController) ConnectNotify() {
var result Result
go ctl.SendResultAndDelEventChan(ssntp.NodeConnected, result)
}
// DisconnectNotify implements the SSNTP client DisconnectNotify callback for SsntpTestController
func (ctl *SsntpTestController) DisconnectNotify() {
var result Result
go ctl.SendResultAndDelEventChan(ssntp.NodeDisconnected, result)
}
// StatusNotify implements the SSNTP client StatusNotify callback for SsntpTestController
func (ctl *SsntpTestController) StatusNotify(status ssntp.Status, frame *ssntp.Frame) {
}
// CommandNotify implements the SSNTP client CommandNotify callback for SsntpTestController
func (ctl *SsntpTestController) CommandNotify(command ssntp.Command, frame *ssntp.Frame) {
var result Result
//payload := frame.Payload
switch command {
/* FIXME: implement
case ssntp.START:
case ssntp.STOP:
case ssntp.RESTART:
case ssntp.DELETE:
*/
case ssntp.STATS:
var stats payloads.Stat
stats.Init()
err := yaml.Unmarshal(frame.Payload, &stats)
if err != nil {
result.Err = err
}
default:
fmt.Fprintf(os.Stderr, "controller unhandled command: %s\n", command.String())
}
go ctl.SendResultAndDelCmdChan(command, result)
}
// EventNotify implements the SSNTP client EventNotify callback for SsntpTestController
func (ctl *SsntpTestController) EventNotify(event ssntp.Event, frame *ssntp.Frame) {
var result Result
switch event {
// case ssntp.TenantAdded: does not reach controller
// case ssntp.TenantRemoved: does not reach controller
case ssntp.NodeConnected:
//handled by ConnectNotify()
return
case ssntp.NodeDisconnected:
//handled by DisconnectNotify()
return
case ssntp.PublicIPAssigned:
var publicIPAssignedEvent payloads.EventPublicIPAssigned
err := yaml.Unmarshal(frame.Payload, &publicIPAssignedEvent)
if err != nil {
result.Err = err
}
case ssntp.InstanceDeleted:
var deleteEvent payloads.EventInstanceDeleted
err := yaml.Unmarshal(frame.Payload, &deleteEvent)
if err != nil {
result.Err = err
}
case ssntp.TraceReport:
var traceEvent payloads.Trace
err := yaml.Unmarshal(frame.Payload, &traceEvent)
if err != nil {
result.Err = err
}
case ssntp.ConcentratorInstanceAdded:
var concentratorAddedEvent payloads.EventConcentratorInstanceAdded
err := yaml.Unmarshal(frame.Payload, &concentratorAddedEvent)
if err != nil {
result.Err = err
}
default:
fmt.Fprintf(os.Stderr, "controller unhandled event: %s\n", event.String())
}
go ctl.SendResultAndDelEventChan(event, result)
}
// ErrorNotify implements the SSNTP client ErrorNotify callback for SsntpTestController
func (ctl *SsntpTestController) ErrorNotify(error ssntp.Error, frame *ssntp.Frame) {
var result Result
//payload := frame.Payload
switch error {
/* FIXME: implement
case ssntp.InvalidFrameType:
case ssntp.StartFailure:
case ssntp.ConnectionFailure:
case ssntp.DeleteFailure:
case ssntp.ConnectionAborted:
case ssntp.InvalidConfiguration:
*/
default:
fmt.Fprintf(os.Stderr, "controller unhandled error %s\n", error.String())
}
go ctl.SendResultAndDelErrorChan(error, result)
}