/
multicast_group.go
393 lines (348 loc) · 8.84 KB
/
multicast_group.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
package storage
import (
"time"
"github.com/gofrs/uuid"
"github.com/jmoiron/sqlx"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/brocaar/lorawan"
)
// MulticastGroupType type defines the multicast-group type.
type MulticastGroupType string
// Possible multicast-group types.
const (
MulticastGroupB MulticastGroupType = "B"
MulticastGroupC MulticastGroupType = "C"
)
// MulticastGroup defines a multicast-group.
type MulticastGroup struct {
ID uuid.UUID `db:"id"`
CreatedAt time.Time `db:"created_at"`
UpdatedAt time.Time `db:"updated_at"`
MCAddr lorawan.DevAddr `db:"mc_addr"`
MCNwkSKey lorawan.AES128Key `db:"mc_nwk_s_key"`
FCnt uint32 `db:"f_cnt"`
GroupType MulticastGroupType `db:"group_type"`
DR int `db:"dr"`
Frequency int `db:"frequency"`
PingSlotPeriod int `db:"ping_slot_period"`
RoutingProfileID uuid.UUID `db:"routing_profile_id"` // there is no downlink data, but it can be used for future error reporting
ServiceProfileID uuid.UUID `db:"service_profile_id"`
}
// MulticastQueueItem defines a multicast queue-item.
type MulticastQueueItem struct {
ID int64 `db:"id"`
CreatedAt time.Time `db:"created_at"`
ScheduleAt time.Time `db:"schedule_at"`
EmitAtTimeSinceGPSEpoch *time.Duration `db:"emit_at_time_since_gps_epoch"`
MulticastGroupID uuid.UUID `db:"multicast_group_id"`
GatewayID lorawan.EUI64 `db:"gateway_id"`
FCnt uint32 `db:"f_cnt"`
FPort uint8 `db:"f_port"`
FRMPayload []byte `db:"frm_payload"`
}
// Validate validates the MulticastQueueItem.
func (m MulticastQueueItem) Validate() error {
if m.FPort == 0 {
return ErrInvalidFPort
}
return nil
}
// CreateMulticastGroup creates the given multi-cast group.
func CreateMulticastGroup(db sqlx.Execer, mg *MulticastGroup) error {
now := time.Now()
mg.CreatedAt = now
mg.UpdatedAt = now
if mg.ID == uuid.Nil {
var err error
mg.ID, err = uuid.NewV4()
if err != nil {
return errors.Wrap(err, "new uuid v4 error")
}
}
_, err := db.Exec(`
insert into multicast_group (
id,
created_at,
updated_at,
mc_addr,
mc_nwk_s_key,
f_cnt,
group_type,
dr,
frequency,
ping_slot_period,
service_profile_id,
routing_profile_id
) values ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12)`,
mg.ID,
mg.CreatedAt,
mg.UpdatedAt,
mg.MCAddr[:],
mg.MCNwkSKey[:],
mg.FCnt,
mg.GroupType,
mg.DR,
mg.Frequency,
mg.PingSlotPeriod,
mg.ServiceProfileID,
mg.RoutingProfileID,
)
if err != nil {
return handlePSQLError(err, "insert error")
}
log.WithFields(log.Fields{
"id": mg.ID,
}).Info("multicast-group created")
return nil
}
// GetMulticastGroup returns the multicast-group for the given ID.
func GetMulticastGroup(db sqlx.Queryer, id uuid.UUID, forUpdate bool) (MulticastGroup, error) {
var mg MulticastGroup
var fu string
if forUpdate {
fu = " for update"
}
err := sqlx.Get(db, &mg, `
select
*
from
multicast_group
where
id = $1`+fu,
id,
)
if err != nil {
return mg, handlePSQLError(err, "select error")
}
return mg, nil
}
// UpdateMulticastGroup updates the given multicast-grup.
func UpdateMulticastGroup(db sqlx.Execer, mg *MulticastGroup) error {
mg.UpdatedAt = time.Now()
res, err := db.Exec(`
update
multicast_group
set
updated_at = $2,
mc_addr = $3,
mc_nwk_s_key = $4,
f_cnt = $5,
group_type = $6,
dr = $7,
frequency = $8,
ping_slot_period = $9,
service_profile_id = $10,
routing_profile_id = $11
where
id = $1`,
mg.ID,
mg.UpdatedAt,
mg.MCAddr[:],
mg.MCNwkSKey[:],
mg.FCnt,
mg.GroupType,
mg.DR,
mg.Frequency,
mg.PingSlotPeriod,
mg.ServiceProfileID,
mg.RoutingProfileID,
)
if err != nil {
return handlePSQLError(err, "update error")
}
ra, err := res.RowsAffected()
if err != nil {
return handlePSQLError(err, "get rows affected error")
}
if ra == 0 {
return ErrDoesNotExist
}
log.WithFields(log.Fields{
"id": mg.ID,
}).Info("multicast-group updated")
return nil
}
// DeleteMulticastGroup deletes the multicast-group matching the given ID.
func DeleteMulticastGroup(db sqlx.Execer, id uuid.UUID) error {
res, err := db.Exec(`
delete from
multicast_group
where
id = $1`,
id,
)
if err != nil {
return handlePSQLError(err, "delete error")
}
ra, err := res.RowsAffected()
if err != nil {
return handlePSQLError(err, "get rows affected error")
}
if ra == 0 {
return ErrDoesNotExist
}
log.WithFields(log.Fields{
"id": id,
}).Info("multicast-group deleted")
return nil
}
// CreateMulticastQueueItem adds the given item to the queue.
func CreateMulticastQueueItem(db sqlx.Queryer, qi *MulticastQueueItem) error {
if err := qi.Validate(); err != nil {
return err
}
qi.CreatedAt = time.Now()
err := sqlx.Get(db, &qi.ID, `
insert into multicast_queue (
created_at,
schedule_at,
emit_at_time_since_gps_epoch,
multicast_group_id,
gateway_id,
f_cnt,
f_port,
frm_payload
) values ($1, $2, $3, $4, $5, $6, $7, $8)
returning
id
`,
qi.CreatedAt,
qi.ScheduleAt,
qi.EmitAtTimeSinceGPSEpoch,
qi.MulticastGroupID,
qi.GatewayID,
qi.FCnt,
qi.FPort,
qi.FRMPayload,
)
if err != nil {
return handlePSQLError(err, "insert error")
}
log.WithFields(log.Fields{
"id": qi.ID,
"f_cnt": qi.FCnt,
"gateway_id": qi.GatewayID,
"multicast_group_id": qi.MulticastGroupID,
}).Info("multicast queue-item created")
return nil
}
// DeleteMulticastQueueItem deletes the queue-item given an id.
func DeleteMulticastQueueItem(db sqlx.Execer, id int64) error {
res, err := db.Exec(`
delete from
multicast_queue
where
id = $1
`, id)
if err != nil {
return handlePSQLError(err, "delete error")
}
ra, err := res.RowsAffected()
if err != nil {
return errors.Wrap(err, "get rows affected error")
}
if ra == 0 {
return ErrDoesNotExist
}
log.WithFields(log.Fields{
"id": id,
}).Info("multicast queue-item deleted")
return nil
}
// FlushMulticastQueueForMulticastGroup flushes the multicast-queue given
// a multicast-group id.
func FlushMulticastQueueForMulticastGroup(db sqlx.Execer, multicastGroupID uuid.UUID) error {
_, err := db.Exec(`
delete from
multicast_queue
where
multicast_group_id = $1
`, multicastGroupID)
if err != nil {
return handlePSQLError(err, "delete error")
}
log.WithFields(log.Fields{
"multicast_group_id": multicastGroupID,
}).Info("multicast-group queue flushed")
return nil
}
// GetMulticastQueueItemsForMulticastGroup returns all queue-items given
// a multicast-group id.
func GetMulticastQueueItemsForMulticastGroup(db sqlx.Queryer, multicastGroupID uuid.UUID) ([]MulticastQueueItem, error) {
var items []MulticastQueueItem
err := sqlx.Select(db, &items, `
select
*
from
multicast_queue
where
multicast_group_id = $1
order by
id
`, multicastGroupID)
if err != nil {
return nil, handlePSQLError(err, "select error")
}
return items, nil
}
// GetSchedulableMulticastQueueItems returns a slice of multicast-queue items
// for scheduling.
// The returned queue-items will be locked for update so that this query can
// be executed in parallel.
func GetSchedulableMulticastQueueItems(db sqlx.Ext, count int) ([]MulticastQueueItem, error) {
var items []MulticastQueueItem
err := sqlx.Select(db, &items, `
select
*
from
multicast_queue
where
schedule_at <= $2
order by
id
limit $1
for update skip locked
`, count, time.Now())
if err != nil {
return nil, handlePSQLError(err, "select error")
}
return items, nil
}
// GetMaxEmitAtTimeSinceGPSEpochForMulticastGroup returns the maximum / last GPS
// epoch scheduling timestamp for the given multicast-group.
func GetMaxEmitAtTimeSinceGPSEpochForMulticastGroup(db sqlx.Queryer, multicastGroupID uuid.UUID) (time.Duration, error) {
var timeSinceGPSEpoch time.Duration
err := sqlx.Get(db, &timeSinceGPSEpoch, `
select
coalesce(max(emit_at_time_since_gps_epoch), 0)
from
multicast_queue
where
multicast_group_id = $1
`, multicastGroupID)
if err != nil {
return 0, handlePSQLError(err, "select error")
}
return timeSinceGPSEpoch, nil
}
// GetMaxScheduleAtForMulticastGroup returns the maximum schedule at timestamp
// for the given multicast-group.
func GetMaxScheduleAtForMulticastGroup(db sqlx.Queryer, multicastGroupID uuid.UUID) (time.Time, error) {
ts := new(time.Time)
err := sqlx.Get(db, &ts, `
select
max(schedule_at)
from
multicast_queue
where
multicast_group_id = $1
`, multicastGroupID)
if err != nil {
return time.Time{}, handlePSQLError(err, "select error")
}
if ts != nil {
return *ts, nil
}
return time.Time{}, nil
}