forked from brocaar/chirpstack-application-server
/
gateway_profile.go
332 lines (287 loc) · 7.87 KB
/
gateway_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
322
323
324
325
326
327
328
329
330
331
332
package storage
import (
"context"
"time"
"github.com/gofrs/uuid"
"github.com/jmoiron/sqlx"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/brocaar/lora-app-server/internal/config"
"github.com/brocaar/loraserver/api/ns"
)
// Modulations
const (
ModulationFSK = "FSK"
ModulationLoRa = "LORA"
)
// ExtraChannel defines an extra channel for the gateway-profile.
type ExtraChannel struct {
Modulation string
Frequency int
Bandwidth int
Bitrate int
SpreadingFactors []int
}
// GatewayProfile defines a gateway-profile.
type GatewayProfile struct {
NetworkServerID int64 `db:"network_server_id"`
CreatedAt time.Time `db:"created_at"`
UpdatedAt time.Time `db:"updated_at"`
Name string `db:"name"`
GatewayProfile ns.GatewayProfile `db:"-"`
}
// GatewayProfileMeta defines the gateway-profile meta record.
type GatewayProfileMeta struct {
GatewayProfileID uuid.UUID `db:"gateway_profile_id"`
NetworkServerID int64 `db:"network_server_id"`
NetworkServerName string `db:"network_server_name"`
CreatedAt time.Time `db:"created_at"`
UpdatedAt time.Time `db:"updated_at"`
Name string `db:"name"`
}
// CreateGatewayProfile creates the given gateway-profile.
// This will create the gateway-profile at the network-server side and will
// create a local reference record.
func CreateGatewayProfile(db sqlx.Ext, gp *GatewayProfile) error {
gpID, err := uuid.NewV4()
if err != nil {
return errors.Wrap(err, "new uuid v4 error")
}
now := time.Now()
gp.GatewayProfile.Id = gpID.Bytes()
gp.CreatedAt = now
gp.UpdatedAt = now
_, err = db.Exec(`
insert into gateway_profile (
gateway_profile_id,
network_server_id,
created_at,
updated_at,
name
) values ($1, $2, $3, $4, $5)`,
gpID,
gp.NetworkServerID,
gp.CreatedAt,
gp.UpdatedAt,
gp.Name,
)
if err != nil {
return handlePSQLError(Insert, err, "insert error")
}
n, err := GetNetworkServer(db, gp.NetworkServerID)
if err != nil {
return errors.Wrap(err, "get network-server error")
}
nsClient, err := config.C.NetworkServer.Pool.Get(n.Server, []byte(n.CACert), []byte(n.TLSCert), []byte(n.TLSKey))
if err != nil {
return errors.Wrap(err, "get network-server client error")
}
_, err = nsClient.CreateGatewayProfile(context.Background(), &ns.CreateGatewayProfileRequest{
GatewayProfile: &gp.GatewayProfile,
})
if err != nil {
return handleGrpcError(err, "create gateway-profile error")
}
log.WithFields(log.Fields{
"id": gpID,
}).Info("gateway-profile created")
return nil
}
// GetGatewayProfile returns the gateway-profile matching the given id.
func GetGatewayProfile(db sqlx.Queryer, id uuid.UUID) (GatewayProfile, error) {
var gp GatewayProfile
err := sqlx.Get(db, &gp, `
select
network_server_id,
name,
created_at,
updated_at
from gateway_profile
where
gateway_profile_id = $1`,
id,
)
if err != nil {
return gp, handlePSQLError(Select, err, "select error")
}
n, err := GetNetworkServer(db, gp.NetworkServerID)
if err != nil {
return gp, errors.Wrap(err, "get network-server error")
}
nsClient, err := config.C.NetworkServer.Pool.Get(n.Server, []byte(n.CACert), []byte(n.TLSCert), []byte(n.TLSKey))
if err != nil {
return gp, errors.Wrap(err, "get network-server client error")
}
resp, err := nsClient.GetGatewayProfile(context.Background(), &ns.GetGatewayProfileRequest{
Id: id.Bytes(),
})
if err != nil {
return gp, handleGrpcError(err, "get gateway-profile error")
}
if resp.GatewayProfile == nil {
return gp, errors.New("gateway_profile must not be nil")
}
gp.GatewayProfile = *resp.GatewayProfile
return gp, nil
}
// UpdateGatewayProfile updates the given gateway-profile.
func UpdateGatewayProfile(db sqlx.Ext, gp *GatewayProfile) error {
gp.UpdatedAt = time.Now()
gpID, err := uuid.FromBytes(gp.GatewayProfile.Id)
if err != nil {
return errors.Wrap(err, "uuid from bytes error")
}
res, err := db.Exec(`
update gateway_profile
set
updated_at = $2,
network_server_id = $3,
name = $4
where
gateway_profile_id = $1`,
gpID,
gp.UpdatedAt,
gp.NetworkServerID,
gp.Name,
)
if err != nil {
return handlePSQLError(Update, err, "update gateway-profile error")
}
ra, err := res.RowsAffected()
if err != nil {
return errors.Wrap(err, "get rows affected error")
}
if ra == 0 {
return ErrDoesNotExist
}
n, err := GetNetworkServer(db, gp.NetworkServerID)
if err != nil {
return errors.Wrap(err, "get network-server error")
}
nsClient, err := config.C.NetworkServer.Pool.Get(n.Server, []byte(n.CACert), []byte(n.TLSCert), []byte(n.TLSKey))
if err != nil {
return errors.Wrap(err, "get network-server client error")
}
_, err = nsClient.UpdateGatewayProfile(context.Background(), &ns.UpdateGatewayProfileRequest{
GatewayProfile: &gp.GatewayProfile,
})
if err != nil {
handleGrpcError(err, "update gateway-profile error")
}
return nil
}
// DeleteGatewayProfile deletes the gateway-profile matching the given id.
func DeleteGatewayProfile(db sqlx.Ext, id uuid.UUID) error {
n, err := GetNetworkServerForGatewayProfileID(db, id)
if err != nil {
return errors.Wrap(err, "get network-server error")
}
res, err := db.Exec(`
delete from gateway_profile
where
gateway_profile_id = $1`,
id,
)
if err != nil {
return handlePSQLError(Delete, err, "delete gateway-profile error")
}
ra, err := res.RowsAffected()
if err != nil {
return errors.Wrap(err, "get rows affected error")
}
if ra == 0 {
return ErrDoesNotExist
}
nsClient, err := config.C.NetworkServer.Pool.Get(n.Server, []byte(n.CACert), []byte(n.TLSCert), []byte(n.TLSKey))
if err != nil {
return errors.Wrap(err, "get network-server client error")
}
_, err = nsClient.DeleteGatewayProfile(context.Background(), &ns.DeleteGatewayProfileRequest{
Id: id.Bytes(),
})
if err != nil {
return handleGrpcError(err, "delete gateway-profile error")
}
return nil
}
// GetGatewayProfileCount returns the total number of gateway-profiles.
func GetGatewayProfileCount(db sqlx.Queryer) (int, error) {
var count int
err := sqlx.Get(db, &count, `
select
count(*)
from gateway_profile`)
if err != nil {
return 0, handlePSQLError(Select, err, "select error")
}
return count, nil
}
// GetGatewayProfileCountForNetworkServerID returns the total number of
// gateway-profiles given a network-server ID.
func GetGatewayProfileCountForNetworkServerID(db sqlx.Queryer, networkServerID int64) (int, error) {
var count int
err := sqlx.Get(db, &count, `
select
count(*)
from gateway_profile
where
network_server_id = $1`,
networkServerID,
)
if err != nil {
return 0, handlePSQLError(Select, err, "select error")
}
return count, nil
}
// GetGatewayProfiles returns a slice of gateway-profiles.
func GetGatewayProfiles(db sqlx.Queryer, limit, offset int) ([]GatewayProfileMeta, error) {
var gps []GatewayProfileMeta
err := sqlx.Select(db, &gps, `
select
gp.*,
n.name as network_server_name
from
gateway_profile gp
inner join
network_server n
on
n.id = gp.network_server_id
order by
name
limit $1 offset $2`,
limit,
offset,
)
if err != nil {
return nil, handlePSQLError(Select, err, "select error")
}
return gps, nil
}
// GetGatewayProfilesForNetworkServerID returns a slice of gateway-profiles
// for the given network-server ID.
func GetGatewayProfilesForNetworkServerID(db sqlx.Queryer, networkServerID int64, limit, offset int) ([]GatewayProfileMeta, error) {
var gps []GatewayProfileMeta
err := sqlx.Select(db, &gps, `
select
gp.*,
n.name as network_server_name
from
gateway_profile gp
inner join
network_server n
on
n.id = gp.network_server_id
where
network_server_id = $1
order by
name
limit $2 offset $3`,
networkServerID,
limit,
offset,
)
if err != nil {
return nil, handlePSQLError(Select, err, "select error")
}
return gps, nil
}