forked from brocaar/chirpstack-application-server
-
Notifications
You must be signed in to change notification settings - Fork 1
/
gateway_profile.go
254 lines (213 loc) · 7.37 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
package api
import (
"github.com/golang/protobuf/ptypes"
"github.com/gofrs/uuid"
"github.com/brocaar/loraserver/api/ns"
pb "github.com/brocaar/lora-app-server/api"
"github.com/brocaar/lora-app-server/internal/api/auth"
"github.com/brocaar/lora-app-server/internal/config"
"github.com/brocaar/lora-app-server/internal/storage"
"github.com/golang/protobuf/ptypes/empty"
"github.com/jmoiron/sqlx"
"golang.org/x/net/context"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
)
// GatewayProfileAPI exports the GatewayProfile related functions.
type GatewayProfileAPI struct {
validator auth.Validator
}
// NewGatewayProfileAPI creates a new GatewayProfileAPI.
func NewGatewayProfileAPI(validator auth.Validator) *GatewayProfileAPI {
return &GatewayProfileAPI{
validator: validator,
}
}
// Create creates the given gateway-profile.
func (a *GatewayProfileAPI) Create(ctx context.Context, req *pb.CreateGatewayProfileRequest) (*pb.CreateGatewayProfileResponse, error) {
if req.GatewayProfile == nil {
return nil, grpc.Errorf(codes.InvalidArgument, "gateway_profile must not be nil")
}
if err := a.validator.Validate(ctx,
auth.ValidateGatewayProfileAccess(auth.Create),
); err != nil {
return nil, grpc.Errorf(codes.Unauthenticated, "authentication failed: %s", err)
}
gp := storage.GatewayProfile{
NetworkServerID: req.GatewayProfile.NetworkServerId,
Name: req.GatewayProfile.Name,
GatewayProfile: ns.GatewayProfile{
Channels: req.GatewayProfile.Channels,
},
}
for _, ec := range req.GatewayProfile.ExtraChannels {
gp.GatewayProfile.ExtraChannels = append(gp.GatewayProfile.ExtraChannels, &ns.GatewayProfileExtraChannel{
Frequency: ec.Frequency,
Bandwidth: ec.Bandwidth,
Bitrate: ec.Bitrate,
SpreadingFactors: ec.SpreadingFactors,
Modulation: ec.Modulation,
})
}
err := storage.Transaction(config.C.PostgreSQL.DB, func(tx sqlx.Ext) error {
return storage.CreateGatewayProfile(tx, &gp)
})
if err != nil {
return nil, errToRPCError(err)
}
gpID, err := uuid.FromBytes(gp.GatewayProfile.Id)
if err != nil {
return nil, errToRPCError(err)
}
return &pb.CreateGatewayProfileResponse{
Id: gpID.String(),
}, nil
}
// Get returns the gateway-profile matching the given id.
func (a *GatewayProfileAPI) Get(ctx context.Context, req *pb.GetGatewayProfileRequest) (*pb.GetGatewayProfileResponse, error) {
if err := a.validator.Validate(ctx,
auth.ValidateGatewayProfileAccess(auth.Read),
); err != nil {
return nil, grpc.Errorf(codes.Unauthenticated, "authentication failed: %s", err)
}
gpID, err := uuid.FromString(req.Id)
if err != nil {
return nil, grpc.Errorf(codes.InvalidArgument, "uuid error: %s", err)
}
gp, err := storage.GetGatewayProfile(config.C.PostgreSQL.DB, gpID)
if err != nil {
return nil, errToRPCError(err)
}
out := pb.GetGatewayProfileResponse{
GatewayProfile: &pb.GatewayProfile{
Id: gpID.String(),
Name: gp.Name,
NetworkServerId: gp.NetworkServerID,
Channels: gp.GatewayProfile.Channels,
},
}
out.CreatedAt, err = ptypes.TimestampProto(gp.CreatedAt)
if err != nil {
return nil, errToRPCError(err)
}
out.UpdatedAt, err = ptypes.TimestampProto(gp.UpdatedAt)
if err != nil {
return nil, errToRPCError(err)
}
for _, ec := range gp.GatewayProfile.ExtraChannels {
out.GatewayProfile.ExtraChannels = append(out.GatewayProfile.ExtraChannels, &pb.GatewayProfileExtraChannel{
Frequency: ec.Frequency,
Bandwidth: ec.Bandwidth,
Bitrate: ec.Bitrate,
SpreadingFactors: ec.SpreadingFactors,
Modulation: ec.Modulation,
})
}
return &out, nil
}
// Update updates the given gateway-profile.
func (a *GatewayProfileAPI) Update(ctx context.Context, req *pb.UpdateGatewayProfileRequest) (*empty.Empty, error) {
if req.GatewayProfile == nil {
return nil, grpc.Errorf(codes.InvalidArgument, "gateway_profile must not be nil")
}
if err := a.validator.Validate(ctx,
auth.ValidateGatewayProfileAccess(auth.Update),
); err != nil {
return nil, grpc.Errorf(codes.Unauthenticated, "authentication failed: %s", err)
}
gpID, err := uuid.FromString(req.GatewayProfile.Id)
if err != nil {
return nil, grpc.Errorf(codes.InvalidArgument, "uuid error: %s", err)
}
gp, err := storage.GetGatewayProfile(config.C.PostgreSQL.DB, gpID)
if err != nil {
return nil, errToRPCError(err)
}
gp.Name = req.GatewayProfile.Name
gp.GatewayProfile.Channels = req.GatewayProfile.Channels
gp.GatewayProfile.ExtraChannels = []*ns.GatewayProfileExtraChannel{}
for _, ec := range req.GatewayProfile.ExtraChannels {
gp.GatewayProfile.ExtraChannels = append(gp.GatewayProfile.ExtraChannels, &ns.GatewayProfileExtraChannel{
Frequency: ec.Frequency,
Bandwidth: ec.Bandwidth,
Bitrate: ec.Bitrate,
SpreadingFactors: ec.SpreadingFactors,
Modulation: ec.Modulation,
})
}
err = storage.Transaction(config.C.PostgreSQL.DB, func(tx sqlx.Ext) error {
return storage.UpdateGatewayProfile(tx, &gp)
})
if err != nil {
return nil, errToRPCError(err)
}
return &empty.Empty{}, nil
}
// Delete deletes the gateway-profile matching the given id.
func (a *GatewayProfileAPI) Delete(ctx context.Context, req *pb.DeleteGatewayProfileRequest) (*empty.Empty, error) {
if err := a.validator.Validate(ctx,
auth.ValidateGatewayProfileAccess(auth.Delete),
); err != nil {
return nil, grpc.Errorf(codes.Unauthenticated, "authentication failed: %s", err)
}
gpID, err := uuid.FromString(req.Id)
if err != nil {
return nil, grpc.Errorf(codes.InvalidArgument, "uuid error: %s", err)
}
err = storage.DeleteGatewayProfile(config.C.PostgreSQL.DB, gpID)
if err != nil {
return nil, errToRPCError(err)
}
return &empty.Empty{}, nil
}
// List returns the existing gateway-profiles.
func (a *GatewayProfileAPI) List(ctx context.Context, req *pb.ListGatewayProfilesRequest) (*pb.ListGatewayProfilesResponse, error) {
if err := a.validator.Validate(ctx,
auth.ValidateGatewayProfileAccess(auth.List),
); err != nil {
return nil, grpc.Errorf(codes.Unauthenticated, "authentication failed: %s", err)
}
var err error
var count int
var gps []storage.GatewayProfileMeta
if req.NetworkServerId == 0 {
count, err = storage.GetGatewayProfileCount(config.C.PostgreSQL.DB)
if err != nil {
return nil, errToRPCError(err)
}
gps, err = storage.GetGatewayProfiles(config.C.PostgreSQL.DB, int(req.Limit), int(req.Offset))
if err != nil {
return nil, errToRPCError(err)
}
} else {
count, err = storage.GetGatewayProfileCountForNetworkServerID(config.C.PostgreSQL.DB, req.NetworkServerId)
if err != nil {
return nil, errToRPCError(err)
}
gps, err = storage.GetGatewayProfilesForNetworkServerID(config.C.PostgreSQL.DB, req.NetworkServerId, int(req.Limit), int(req.Offset))
if err != nil {
return nil, errToRPCError(err)
}
}
out := pb.ListGatewayProfilesResponse{
TotalCount: int64(count),
}
for _, gp := range gps {
row := pb.GatewayProfileListItem{
Id: gp.GatewayProfileID.String(),
Name: gp.Name,
NetworkServerName: gp.NetworkServerName,
NetworkServerId: gp.NetworkServerID,
}
row.CreatedAt, err = ptypes.TimestampProto(gp.CreatedAt)
if err != nil {
return nil, errToRPCError(err)
}
row.UpdatedAt, err = ptypes.TimestampProto(gp.UpdatedAt)
if err != nil {
return nil, errToRPCError(err)
}
out.Result = append(out.Result, &row)
}
return &out, nil
}