/
cassandra.go
497 lines (442 loc) · 11.6 KB
/
cassandra.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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
package cassandra
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"strings"
"time"
"github.com/Juniper/asf/pkg/logutil"
"github.com/Juniper/contrail/pkg/services"
"github.com/gocql/gocql"
"github.com/sirupsen/logrus"
"github.com/spf13/viper"
"github.com/streadway/amqp"
asfservices "github.com/Juniper/asf/pkg/services"
uuid "github.com/satori/go.uuid"
)
const (
defaultCassandraVersion = "3.4.4"
defaultCassandraKeyspace = "config_db_uuid"
exchangeName = "vnc_config.object-update"
)
// Config fields for cassandra
type Config struct {
Host string
Port int
Timeout time.Duration
ConnectTimeout time.Duration
}
//GetConfig returns cassandra Config filled with data from config file.
func GetConfig() Config {
return Config{
Host: viper.GetString("cassandra.host"),
Port: viper.GetInt("cassandra.port"),
Timeout: viper.GetDuration("cassandra.timeout"),
ConnectTimeout: viper.GetDuration("cassandra.connect_timeout"),
}
}
type contrailDBData struct {
Cassandra *cassandra `json:"cassandra"`
}
type object map[string][]interface{}
type uuidTable map[string][]interface{}
type cassandra struct {
ObjFQNameTable map[string]uuidTable `json:"obj_fq_name_table"`
ObjUUIDTable map[string]object `json:"obj_uuid_table"`
}
type table map[string]map[string]interface{}
func (t table) get(uuid string) map[string]interface{} {
r, ok := t[uuid]
if !ok {
r = map[string]interface{}{}
t[uuid] = r
}
return r
}
func (t table) makeEventList() *services.EventList {
events := &services.EventList{
Events: []*services.Event{},
}
for uuid, data := range t {
kind := data["type"].(string) //nolint: errcheck
data["uuid"] = uuid
event, err := services.NewEvent(services.EventOption{
Kind: kind,
Data: data,
UUID: uuid,
})
if err != nil {
logrus.Warnf("failed to create event - skipping: %v", err)
continue
}
events.Events = append(events.Events, event)
}
return events
}
func (o object) Get(key string) interface{} {
data, ok := o[key]
if !ok {
return nil
}
if data == nil {
return nil
}
var response interface{}
err := json.Unmarshal([]byte(data[0].(string)), &response) //nolint: errcheck
if err != nil {
logutil.FatalWithStackTrace(err)
}
return response
}
func (o object) GetString(key string) string {
data := o.Get(key)
response, _ := data.(string) //nolint: errcheck
return response
}
func parseProperty(data map[string]interface{}, property string, value interface{}) {
if strings.Contains(property, ":") {
propertyList := strings.Split(property, ":")
switch propertyList[0] {
case "prop":
data[propertyList[1]] = value
case "propl", "propm":
// TODO: preserve order in case of propl
l, _ := data[propertyList[1]].([]interface{}) //nolint: errcheck
data[propertyList[1]] = append(l, value)
case "parent":
data["parent_uuid"] = propertyList[2]
case "ref":
refProperty := propertyList[1] + "_refs"
list, _ := data[refProperty].([]interface{}) //nolint: errcheck
m, _ := value.(map[string]interface{}) //nolint: errcheck
data[refProperty] = append(list, map[string]interface{}{
"uuid": propertyList[2],
"attr": m["attr"],
})
}
} else {
data[property] = value
}
}
func (o object) convert(uuid string) map[string]interface{} {
data := map[string]interface{}{
"uuid": uuid,
}
for property := range o {
value := o.Get(property)
parseProperty(data, property, value)
}
return data
}
//DumpCassandra load all existing cassandra data.
func DumpCassandra(cfg Config) (*services.EventList, error) {
logrus.Debug("Dumping data from cassandra")
// connect to the cluster
cluster := getCluster(cfg)
session, err := cluster.CreateSession()
if err != nil {
return nil, err
}
defer session.Close()
t := table{}
// list all tweets
iter := session.Query(`SELECT key, column1, value FROM obj_uuid_table`).Iter()
var uuid, column, valueJSON string
for iter.Scan(&uuid, &column, &valueJSON) {
r := t.get(uuid)
var value interface{}
err = json.Unmarshal([]byte(valueJSON), &value)
if err != nil {
return nil, err
}
parseProperty(r, column, value)
}
if err = iter.Close(); err != nil {
return nil, err
}
return t.makeEventList(), nil
}
//ReadCassandraDump reads json dump of cassandra.
func ReadCassandraDump(inFile string) (*services.EventList, error) {
raw, err := ioutil.ReadFile(inFile)
if err != nil {
return nil, err
}
t := table{}
var data contrailDBData
err = json.Unmarshal(raw, &data)
if err != nil {
return nil, err
}
for uuid, object := range data.Cassandra.ObjUUIDTable {
objectType := object.GetString("type")
if objectType != "" {
t[uuid] = object.convert(uuid)
}
}
return t.makeEventList(), nil
}
// AmqpConfig groups config fields for AMQP
type AmqpConfig struct {
host string
queueName string
}
func getQueueName() string {
name, _ := os.Hostname() // nolint: errcheck
return "contrail_process_" + name
}
//EventProducer send db update for processor.
//Event will be harvest from cassandra db and from amqp later on.
type EventProducer struct {
Processor services.EventProcessor
cassandraConfig Config
amqpConfig AmqpConfig
log *logrus.Entry
}
//NewEventProducer makes event producer and couple it with processor given.
func NewEventProducer(
processor services.EventProcessor,
) *EventProducer {
cfg := GetConfig()
return &EventProducer{
cassandraConfig: cfg,
amqpConfig: AmqpConfig{
host: viper.GetString("amqp.url"),
queueName: getQueueName(),
},
Processor: processor,
log: logutil.NewLogger("cassandra-event-producer"),
}
}
//WatchAMQP watches AMQP.
//nolint: gocyclo
func (p *EventProducer) WatchAMQP(ctx context.Context) error {
p.log.Debug("Starting watch on AMQP")
conn, err := amqp.Dial(p.amqpConfig.host)
if err != nil {
return err
}
defer conn.Close() // nolint: errcheck
ch, err := conn.Channel()
if err != nil {
return err
}
defer ch.Close() // nolint: errcheck
q, err := ch.QueueDeclare(
p.amqpConfig.queueName,
false, // durable
false, // delete when usused
false, // exclusive
false, // no-wait
nil, // arguments
)
if err != nil {
return err
}
err = ch.QueueBind(
q.Name, // queue name
"", // routing key
exchangeName, // exchange
false,
nil,
)
if err != nil {
return err
}
msgs, err := ch.Consume(
q.Name, // queue
"", // consumer
true, // auto-ack
false, // exclusive
false, // no-local
false, // no-wait
nil, // args
)
if err != nil {
return err
}
for {
select {
case d := <-msgs:
var data map[string]interface{}
if err := json.Unmarshal(d.Body, &data); err != nil {
p.log.WithError(err).WithField("data", string(d.Body)).Warn("Decoding failed - ignoring")
continue
}
operation, _ := data["oper"].(string) //nolint: errcheck
kind, _ := data["type"].(string) //nolint: errcheck
uuid, _ := data["uuid"].(string) //nolint: errcheck
obj, _ := data["obj_dict"].(map[string]interface{}) //nolint: errcheck
event, err := services.NewEvent(services.EventOption{
Operation: operation,
Data: obj,
Kind: kind,
UUID: uuid,
})
if err != nil {
p.log.WithField("data", data).Warnf("Failed to create event - ignoring: %v", err)
continue
}
if _, err = p.Processor.Process(ctx, event); err != nil {
p.log.WithError(err).WithFields(logrus.Fields{
"context": ctx,
"event": event,
}).Warn("Processing event failed - ignoring")
}
case <-ctx.Done():
p.log.Debug("AQMP watcher cancelled by context")
return nil
}
}
}
//Start starts event processor for cassandra.
func (p *EventProducer) Start(ctx context.Context) error {
events, err := DumpCassandra(p.cassandraConfig)
if err != nil {
return err
}
if err = events.Sort(); err != nil {
p.log.WithError(err).Warn("Sorting events failed - ignoring")
}
for _, e := range events.Events {
if _, err = p.Processor.Process(ctx, e); err != nil {
p.log.WithError(err).WithFields(logrus.Fields{
"context": ctx,
"event": e,
}).Warn("Processing event failed - ignoring")
}
}
return p.WatchAMQP(ctx)
}
// EventProcessor writes events to cassandra and implements service.EventProcessor interface
type EventProcessor struct {
config Config
}
// NewEventProcessor returns new cassandra.EventProcessor
func NewEventProcessor() *EventProcessor {
cfg := GetConfig()
return &EventProcessor{
config: cfg}
}
func getCluster(cfg Config) *gocql.ClusterConfig {
cluster := gocql.NewCluster(cfg.Host)
if cfg.Port != 0 {
cluster.Port = cfg.Port
}
cluster.Timeout = cfg.Timeout
cluster.ConnectTimeout = cfg.ConnectTimeout
cluster.Keyspace = defaultCassandraKeyspace
cluster.CQLVersion = defaultCassandraVersion
return cluster
}
// Process is a method needed to implement service.EventProcessor interface
func (p *EventProcessor) Process(ctx context.Context, event *services.Event) (*services.Event, error) {
logrus.Debugf("Processing event %+v for cassandra", event)
// connect to the cluster
cluster := getCluster(p.config)
session, err := cluster.CreateSession()
if err != nil {
return nil, err
}
defer session.Close()
qry, err := getQuery(session, event)
if err != nil {
return nil, err
}
if err := qry.Exec(); err != nil {
return nil, err
}
return event, nil
}
func getQuery(session *gocql.Session, event *services.Event) (qry *gocql.Query, err error) { // nolint: interfacer
rsrc := event.GetResource()
switch event.Operation() {
case asfservices.OperationCreate, asfservices.OperationUpdate:
rsrcJSON, err := json.Marshal(rsrc.ToMap())
if err != nil {
return nil, err
}
qry = session.Query(
"INSERT INTO obj_uuid_table (key, column1, value) VALUES (?, ?, ?)",
rsrc.GetUUID(),
rsrc.Kind(),
rsrcJSON,
)
case asfservices.OperationDelete:
qry = session.Query(
"DELETE FROM obj_uuid_table WHERE key=? and column1=?",
rsrc.GetUUID(),
rsrc.Kind(),
)
}
return qry, nil
}
//AmqpEventProcessor implements EventProcessor
type AmqpEventProcessor struct {
config AmqpConfig
}
//NewAmqpEventProcessor return Amqp event processor
func NewAmqpEventProcessor() *AmqpEventProcessor {
return &AmqpEventProcessor{
config: AmqpConfig{
host: viper.GetString("amqp.url"),
queueName: getQueueName(),
},
}
}
//AmqpMessage type
type AmqpMessage struct {
RequestID string `json:"request_id"`
Oper string `json:"oper"`
Type string `json:"type"`
UUID string `json:"uuid"`
FqName []string `json:"fq_name"`
Data json.RawMessage `json:"obj_dict"`
}
//Process sends msg to amqp exchange
func (p *AmqpEventProcessor) Process(ctx context.Context, event *services.Event) (*services.Event, error) {
logrus.Debugf("Processing event %+v for amqp", event)
conn, err := amqp.Dial(p.config.host)
if err != nil {
return nil, err
}
defer conn.Close() // nolint: errcheck
ch, err := conn.Channel()
if err != nil {
return nil, err
}
defer ch.Close() // nolint: errcheck
rsrc := event.GetResource()
data, err := json.Marshal(rsrc.ToMap())
if err != nil {
return nil, err
}
msg := AmqpMessage{
RequestID: fmt.Sprintf("req-%s", uuid.NewV4().String()),
Oper: event.Operation(),
Type: rsrc.Kind(),
UUID: rsrc.GetUUID(),
FqName: rsrc.GetFQName(),
Data: json.RawMessage(data),
}
msgJSON, err := json.Marshal(msg)
if err != nil {
return nil, err
}
err = ch.Publish(
exchangeName,
"",
false,
false,
amqp.Publishing{
ContentType: "application/json",
Body: msgJSON,
},
)
if err != nil {
return nil, err
}
return event, nil
}