-
Notifications
You must be signed in to change notification settings - Fork 0
/
service_profile.go
321 lines (280 loc) · 7.99 KB
/
service_profile.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
package storage
import (
"bytes"
"encoding/gob"
"fmt"
"time"
"github.com/gomodule/redigo/redis"
"github.com/pkg/errors"
"github.com/gofrs/uuid"
"github.com/jmoiron/sqlx"
log "github.com/sirupsen/logrus"
"github.com/brocaar/loraserver/internal/config"
)
// Templates used for generating Redis keys
const (
ServiceProfileKeyTempl = "lora:ns:sp:%s"
)
// RatePolicy defines the RatePolicy type.
type RatePolicy string
// Available rate policies.
const (
Drop RatePolicy = "Drop"
Mark RatePolicy = "Mark"
)
// ServiceProfile defines the backend.ServiceProfile with some extra meta-data.
type ServiceProfile struct {
CreatedAt time.Time `db:"created_at"`
UpdatedAt time.Time `db:"updated_at"`
ID uuid.UUID `db:"service_profile_id"`
ULRate int `db:"ul_rate"`
ULBucketSize int `db:"ul_bucket_size"`
ULRatePolicy RatePolicy `db:"ul_rate_policy"`
DLRate int `db:"dl_rate"`
DLBucketSize int `db:"dl_bucket_size"`
DLRatePolicy RatePolicy `db:"dl_rate_policy"`
AddGWMetadata bool `db:"add_gw_metadata"`
DevStatusReqFreq int `db:"dev_status_req_freq"` // Unit: requests-per-day
ReportDevStatusBattery bool `db:"report_dev_status_battery"`
ReportDevStatusMargin bool `db:"report_dev_status_margin"`
DRMin int `db:"dr_min"`
DRMax int `db:"dr_max"`
ChannelMask []byte `db:"channel_mask"`
PRAllowed bool `db:"pr_allowed"`
HRAllowed bool `db:"hr_allowed"`
RAAllowed bool `db:"ra_allowed"`
NwkGeoLoc bool `db:"nwk_geo_loc"`
TargetPER int `db:"target_per"` // Example: 10 indicates 10%
MinGWDiversity int `db:"min_gw_diversity"`
}
// CreateServiceProfile creates the given service-profile.
func CreateServiceProfile(db sqlx.Execer, sp *ServiceProfile) error {
now := time.Now()
if sp.ID == uuid.Nil {
var err error
sp.ID, err = uuid.NewV4()
if err != nil {
return errors.Wrap(err, "new uuid v4 error")
}
}
sp.CreatedAt = now
sp.UpdatedAt = now
_, err := db.Exec(`
insert into service_profile (
created_at,
updated_at,
service_profile_id,
ul_rate,
ul_bucket_size,
ul_rate_policy,
dl_rate,
dl_bucket_size,
dl_rate_policy,
add_gw_metadata,
dev_status_req_freq,
report_dev_status_battery,
report_dev_status_margin,
dr_min,
dr_max,
channel_mask,
pr_allowed,
hr_allowed,
ra_allowed,
nwk_geo_loc,
target_per,
min_gw_diversity
) values ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21, $22)`,
sp.CreatedAt,
sp.UpdatedAt,
sp.ID,
sp.ULRate,
sp.ULBucketSize,
sp.ULRatePolicy,
sp.DLRate,
sp.DLBucketSize,
sp.DLRatePolicy,
sp.AddGWMetadata,
sp.DevStatusReqFreq,
sp.ReportDevStatusBattery,
sp.ReportDevStatusMargin,
sp.DRMin,
sp.DRMax,
sp.ChannelMask,
sp.PRAllowed,
sp.HRAllowed,
sp.RAAllowed,
sp.NwkGeoLoc,
sp.TargetPER,
sp.MinGWDiversity,
)
if err != nil {
return handlePSQLError(err, "insert error")
}
log.WithFields(log.Fields{
"id": sp.ID,
}).Info("service-profile created")
return nil
}
// CreateServiceProfileCache caches the given service-profile into the Redis.
// This is used for faster lookups, but also in case of roaming where we
// only want to store the service-profile of a roaming device for a finite
// duration.
// The TTL of the service-profile is the same as that of the device-sessions.
func CreateServiceProfileCache(p *redis.Pool, sp ServiceProfile) error {
var buf bytes.Buffer
if err := gob.NewEncoder(&buf).Encode(sp); err != nil {
return errors.Wrap(err, "gob encode service-profile error")
}
c := p.Get()
defer c.Close()
key := fmt.Sprintf(ServiceProfileKeyTempl, sp.ID)
exp := int64(config.C.NetworkServer.DeviceSessionTTL) / int64(time.Millisecond)
_, err := c.Do("PSETEX", key, exp, buf.Bytes())
if err != nil {
return errors.Wrap(err, "set service-profile error")
}
return nil
}
// GetServiceProfileCache returns a cached service-profile.
func GetServiceProfileCache(p *redis.Pool, id uuid.UUID) (ServiceProfile, error) {
var sp ServiceProfile
key := fmt.Sprintf(ServiceProfileKeyTempl, id)
c := p.Get()
defer c.Close()
val, err := redis.Bytes(c.Do("GET", key))
if err != nil {
if err == redis.ErrNil {
return sp, ErrDoesNotExist
}
return sp, errors.Wrap(err, "get error")
}
err = gob.NewDecoder(bytes.NewReader(val)).Decode(&sp)
if err != nil {
return sp, errors.Wrap(err, "gob decode error")
}
return sp, nil
}
// FlushServiceProfileCache deletes a cached service-profile.
func FlushServiceProfileCache(p *redis.Pool, id uuid.UUID) error {
key := fmt.Sprintf(ServiceProfileKeyTempl, id)
c := p.Get()
defer c.Close()
_, err := c.Do("DEL", key)
if err != nil {
return errors.Wrap(err, "delete error")
}
return nil
}
// GetAndCacheServiceProfile returns the service-profile from cache in case
// available, else it will be retrieved from the database and then stored
// in cache.
func GetAndCacheServiceProfile(db sqlx.Queryer, p *redis.Pool, id uuid.UUID) (ServiceProfile, error) {
sp, err := GetServiceProfileCache(p, id)
if err == nil {
return sp, nil
}
if err != ErrDoesNotExist {
log.WithFields(log.Fields{
"id": id,
}).WithError(err).Error("get service-profile cache error")
// we don't return as we can fall-back onto db retrieval
}
sp, err = GetServiceProfile(db, id)
if err != nil {
return ServiceProfile{}, errors.Wrap(err, "get service-profile-error")
}
err = CreateServiceProfileCache(p, sp)
if err != nil {
log.WithFields(log.Fields{
"id": id,
}).WithError(err).Error("create service-profile cache error")
}
return sp, nil
}
// GetServiceProfile returns the service-profile matching the given id.
func GetServiceProfile(db sqlx.Queryer, id uuid.UUID) (ServiceProfile, error) {
var sp ServiceProfile
err := sqlx.Get(db, &sp, "select * from service_profile where service_profile_id = $1", id)
if err != nil {
return sp, handlePSQLError(err, "select error")
}
return sp, nil
}
// UpdateServiceProfile updates the given service-profile.
func UpdateServiceProfile(db sqlx.Execer, sp *ServiceProfile) error {
sp.UpdatedAt = time.Now()
res, err := db.Exec(`
update service_profile set
updated_at = $2,
ul_rate = $3,
ul_bucket_size = $4,
ul_rate_policy = $5,
dl_rate = $6,
dl_bucket_size = $7,
dl_rate_policy = $8,
add_gw_metadata = $9,
dev_status_req_freq = $10,
report_dev_status_battery = $11,
report_dev_status_margin = $12,
dr_min = $13,
dr_max = $14,
channel_mask = $15,
pr_allowed = $16,
hr_allowed = $17,
ra_allowed = $18,
nwk_geo_loc = $19,
target_per = $20,
min_gw_diversity = $21
where
service_profile_id = $1`,
sp.ID,
sp.UpdatedAt,
sp.ULRate,
sp.ULBucketSize,
sp.ULRatePolicy,
sp.DLRate,
sp.DLBucketSize,
sp.DLRatePolicy,
sp.AddGWMetadata,
sp.DevStatusReqFreq,
sp.ReportDevStatusBattery,
sp.ReportDevStatusMargin,
sp.DRMin,
sp.DRMax,
sp.ChannelMask,
sp.PRAllowed,
sp.HRAllowed,
sp.RAAllowed,
sp.NwkGeoLoc,
sp.TargetPER,
sp.MinGWDiversity,
)
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.WithField("id", sp.ID).Info("service-profile updated")
return nil
}
// DeleteServiceProfile deletes the service-profile matching the given id.
func DeleteServiceProfile(db sqlx.Execer, id uuid.UUID) error {
res, err := db.Exec("delete from service_profile where service_profile_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.WithField("id", id).Info("service-profile deleted")
return nil
}