/
end_device_templates.go
461 lines (416 loc) · 15 KB
/
end_device_templates.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
// Copyright © 2019 The Things Network Foundation, The Things Industries B.V.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package commands
import (
"encoding/binary"
"fmt"
stdio "io"
"os"
"strings"
pbtypes "github.com/gogo/protobuf/types"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"go.thethings.network/lorawan-stack/cmd/ttn-lw-cli/internal/api"
"go.thethings.network/lorawan-stack/cmd/ttn-lw-cli/internal/io"
"go.thethings.network/lorawan-stack/cmd/ttn-lw-cli/internal/util"
"go.thethings.network/lorawan-stack/pkg/errors"
"go.thethings.network/lorawan-stack/pkg/ttnpb"
"go.thethings.network/lorawan-stack/pkg/types"
)
var (
endDeviceTemplateFlattenPaths = []string{"end_device.provisioning_data"}
)
func templateFormatIDFlags() *pflag.FlagSet {
flagSet := &pflag.FlagSet{}
flagSet.String("format-id", "", "")
return flagSet
}
var (
errNoTemplateFormatID = errors.DefineInvalidArgument("no_template_format_id", "no template format ID set")
errEndDeviceMappingNotFound = errors.DefineNotFound("mapped_end_device_not_found", "end device mapping not found")
errNoEndDeviceTemplateJoinEUI = errors.DefineInvalidArgument("no_end_device_template_join_eui", "no end device template JoinEUI set")
errNoEndDeviceTemplateStartDevEUI = errors.DefineInvalidArgument("no_end_device_template_start_dev_eui", "no end device template start DevEUI set")
)
func getTemplateFormatID(flagSet *pflag.FlagSet, args []string) string {
var formatID string
if len(args) > 0 {
if len(args) > 1 {
logger.Warn("Multiple IDs found in arguments, considering only the first")
}
formatID = args[0]
} else {
formatID, _ = flagSet.GetString("format-id")
}
if formatID == "" {
return ""
}
return formatID
}
var (
endDeviceTemplatesCommand = &cobra.Command{
Use: "templates",
Aliases: []string{"template", "tmpl"},
Short: "End Device template commands",
}
endDeviceTemplatesExtendCommand = &cobra.Command{
Use: "extend [flags]",
Short: "Extend an end device template (EXPERIMENTAL)",
PersistentPreRunE: preRun(),
RunE: asBulk(func(cmd *cobra.Command, args []string) error {
forwardDeprecatedDeviceFlags(cmd.Flags())
paths := util.UpdateFieldMask(cmd.Flags(), setEndDeviceFlags, attributesFlags())
var res ttnpb.EndDeviceTemplate
if inputDecoder != nil {
_, err := inputDecoder.Decode(&res)
if err != nil {
return err
}
paths = append(paths, res.FieldMask.Paths...)
}
if mappingKey, _ := cmd.Flags().GetString("mapping-key"); mappingKey != "" {
res.MappingKey = mappingKey
}
if err := util.SetFields(&res.EndDevice, setEndDeviceFlags); err != nil {
return err
}
res.EndDevice.Attributes = mergeAttributes(res.EndDevice.Attributes, cmd.Flags())
res.FieldMask.Paths = ttnpb.BottomLevelFields(paths)
return io.Write(os.Stdout, config.OutputFormat, &res)
}),
}
endDeviceTemplatesCreateCommand = &cobra.Command{
Use: "create [flags]",
Short: "Create an end device template from an existing device (EXPERIMENTAL)",
Long: `Create an end device template from an existing device (EXPERIMENTAL)
By default, this command strips the device's application ID, device ID, JoinEUI,
DevEUI and server addresses to create a generic template. You can include the
end device identifiers by passing the concerning flags: --application-id,
--device-id, --join-eui and --dev-eui.
This command takes end devices from stdin.`,
PersistentPreRunE: preRun(),
RunE: asBulk(func(cmd *cobra.Command, args []string) error {
if inputDecoder == nil {
return nil
}
forwardDeprecatedDeviceFlags(cmd.Flags())
paths := util.UpdateFieldMask(cmd.Flags(), selectEndDeviceFlags)
excludePaths := []string{
"ids.dev_addr",
"created_at",
"updated_at",
"network_server_address",
"application_server_address",
"join_server_address",
}
if appID, _ := cmd.Flags().GetBool("application-id"); !appID {
excludePaths = append(excludePaths, "ids.application_ids.application_id")
}
if devID, _ := cmd.Flags().GetBool("device-id"); !devID {
excludePaths = append(excludePaths, "ids.device_id")
}
if joinEUI, _ := cmd.Flags().GetBool("join-eui"); !joinEUI {
excludePaths = append(excludePaths, "ids.join_eui")
}
if devEUI, _ := cmd.Flags().GetBool("dev-eui"); !devEUI {
excludePaths = append(excludePaths, "ids.dev_eui")
}
var input ttnpb.EndDevice
decodedPaths, err := inputDecoder.Decode(&input)
if err != nil {
return err
}
decodedPaths = ttnpb.FlattenPaths(decodedPaths, endDeviceFlattenPaths)
decodedPaths = ttnpb.ExcludeFields(decodedPaths, excludePaths...)
paths = append(paths, decodedPaths...)
mappingKey, _ := cmd.Flags().GetString("mapping-key")
res := &ttnpb.EndDeviceTemplate{
FieldMask: pbtypes.FieldMask{
Paths: paths,
},
MappingKey: mappingKey,
}
res.EndDevice.SetFields(&input, paths...)
return io.Write(os.Stdout, config.OutputFormat, res)
}),
}
endDeviceTemplatesExecuteCommand = &cobra.Command{
Use: "execute [flags]",
Aliases: []string{"exec"},
Short: "Execute the template to an end device (EXPERIMENTAL)",
Long: `Execute the template to an end device (EXPERIMENTAL)
This command takes end device templates from stdin.`,
PersistentPreRunE: preRun(),
RunE: asBulk(func(cmd *cobra.Command, args []string) error {
if inputDecoder == nil {
return nil
}
forwardDeprecatedDeviceFlags(cmd.Flags())
var input ttnpb.EndDeviceTemplate
_, err := inputDecoder.Decode(&input)
if err != nil {
return err
}
var device ttnpb.EndDevice
device.SetFields(&input.EndDevice, input.FieldMask.Paths...)
if err := util.SetFields(&device, setEndDeviceFlags); err != nil {
return err
}
return io.Write(os.Stdout, config.OutputFormat, &device)
}),
}
endDeviceTemplatesAssignEUIsCommand = &cobra.Command{
Use: "assign-euis [join-eui] [start-dev-eui] [flags]",
Aliases: []string{"euis", "eui"},
Short: "Assign JoinEUI and DevEUIs to end device templates (EXPERIMENTAL)",
Long: `Assign JoinEUI and DevEUIs to end device templates (EXPERIMENTAL)
Pass --count=N to assign N number of DevEUIs to each input end device template.
This command takes end device templates from stdin.`,
PersistentPreRunE: preRun(),
RunE: func(cmd *cobra.Command, args []string) error {
if inputDecoder == nil {
return nil
}
forwardDeprecatedDeviceFlags(cmd.Flags())
joinEUIHex, _ := cmd.Flags().GetString("join-eui")
startDevEUIHex, _ := cmd.Flags().GetString("start-dev-eui")
switch len(args) {
case 0:
case 1:
logger.Warn("only single EUI found in arguments, not considering arguments")
case 2:
joinEUIHex = args[0]
startDevEUIHex = args[1]
default:
logger.Warn("Multiple EUIs found in arguments, considering the first")
joinEUIHex = args[0]
startDevEUIHex = args[1]
}
if joinEUIHex == "" {
return errNoEndDeviceTemplateJoinEUI
}
if startDevEUIHex == "" {
return errNoEndDeviceTemplateStartDevEUI
}
var joinEUI types.EUI64
if err := joinEUI.UnmarshalText([]byte(joinEUIHex)); err != nil {
return err
}
var startDevEUI types.EUI64
if err := startDevEUI.UnmarshalText([]byte(startDevEUIHex)); err != nil {
return err
}
devEUIInt := binary.BigEndian.Uint64(startDevEUI[:])
count, _ := cmd.Flags().GetInt("count")
for {
var template ttnpb.EndDeviceTemplate
if _, err := inputDecoder.Decode(&template); err != nil {
if err == stdio.EOF {
return nil
}
return err
}
for i := 0; i < count; i++ {
res := template
var devEUI types.EUI64
binary.BigEndian.PutUint64(devEUI[:], devEUIInt)
devEUIInt++
res.EndDevice.DeviceID = fmt.Sprintf("eui-%s", strings.ToLower(devEUI.String()))
res.EndDevice.JoinEUI = &joinEUI
res.EndDevice.DevEUI = &devEUI
res.FieldMask.Paths = ttnpb.BottomLevelFields(append(res.FieldMask.Paths,
"ids.device_id",
"ids.join_eui",
"ids.dev_eui",
))
if err := io.Write(os.Stdout, config.OutputFormat, &res); err != nil {
return err
}
}
}
},
}
endDeviceTemplatesListFormats = &cobra.Command{
Use: "list-formats",
Aliases: []string{"ls-formats", "listformats", "lsformats", "lsfmts", "lsfmt"},
Short: "List available end device template formats (EXPERIMENTAL)",
PersistentPreRunE: preRun(),
RunE: func(cmd *cobra.Command, args []string) error {
dtc, err := api.Dial(ctx, config.DeviceTemplateConverterGRPCAddress)
if err != nil {
return err
}
res, err := ttnpb.NewEndDeviceTemplateConverterClient(dtc).ListFormats(ctx, ttnpb.Empty)
if err != nil {
return err
}
return io.Write(os.Stdout, config.OutputFormat, res)
},
}
endDeviceTemplatesFromDataCommand = &cobra.Command{
Use: "from-data [format-id]",
Aliases: []string{"fromdata"},
Short: "Convert data to an end device template (EXPERIMENTAL)",
PersistentPreRunE: preRun(),
RunE: func(cmd *cobra.Command, args []string) error {
formatID := getTemplateFormatID(cmd.Flags(), args)
if formatID == "" {
return errNoTemplateFormatID
}
data, err := getDataBytes("", cmd.Flags())
if err != nil {
return err
}
dtc, err := api.Dial(ctx, config.DeviceTemplateConverterGRPCAddress)
if err != nil {
return err
}
stream, err := ttnpb.NewEndDeviceTemplateConverterClient(dtc).Convert(ctx, &ttnpb.ConvertEndDeviceTemplateRequest{
FormatID: formatID,
Data: data,
})
if err != nil {
return err
}
for {
dev, err := stream.Recv()
if err == stdio.EOF {
return nil
}
if err != nil {
return err
}
if err := io.Write(os.Stdout, config.OutputFormat, dev); err != nil {
return err
}
}
},
}
endDeviceTemplatesMapCommand = &cobra.Command{
Use: "map [flags]",
Short: "Map end device templates (EXPERIMENTAL)",
Long: `Map end device templates (EXPERIMENTAL)
This command matches the input templates with the mapping file to create new
templates. The mapping file contains end device templates in the same format
as input.
The matching from input to a mapping template is, in order, by mapping key, end
device identifiers and DevEUI. If you don't specify a mapping key, end device
identifiers nor DevEUI, the mapping entry always matches. This is useful for
mapping many end device templates with a generic template.
Typical use cases are:
1. Assigning identifiers from a mapping file to device templates matching on
mapping key.
2. Mapping a device profile (i.e. MAC and PHY versions, frequency plan and class
B/C support) from a mapping file to many end device templates.
Use the create command to create a mapping file and (optionally) the assign-euis
command to assign EUIs to map to end device templates.`,
PersistentPreRunE: preRun(),
RunE: func(cmd *cobra.Command, args []string) error {
inputDecoder := inputDecoder
if inputDecoder == nil {
reader, err := getDataReader("input", cmd.Flags())
if err != nil {
return err
}
inputDecoder, err = getInputDecoder(reader)
if err != nil {
return err
}
}
var input []ttnpb.EndDeviceTemplate
for {
var entry ttnpb.EndDeviceTemplate
_, err := inputDecoder.Decode(&entry)
if err != nil {
if err == stdio.EOF {
break
}
return err
}
input = append(input, entry)
}
var mapping []ttnpb.EndDeviceTemplate
reader, err := getDataReader("mapping", cmd.Flags())
if err != nil {
return err
}
mappingDecoder, err := getInputDecoder(reader)
if err != nil {
return err
}
for {
var entry ttnpb.EndDeviceTemplate
if _, err := mappingDecoder.Decode(&entry); err != nil {
if err == stdio.EOF {
break
}
return err
}
mapping = append(mapping, entry)
}
for _, inputEntry := range input {
var mappedEntry *ttnpb.EndDeviceTemplate
for _, e := range mapping {
switch {
case e.MappingKey != "" && e.MappingKey == inputEntry.MappingKey:
case e.EndDevice.ApplicationID != "" && e.EndDevice.ApplicationID == inputEntry.EndDevice.ApplicationID &&
e.EndDevice.DeviceID != "" && e.EndDevice.DeviceID == inputEntry.EndDevice.DeviceID:
case e.EndDevice.DevEUI != nil && inputEntry.EndDevice.DevEUI != nil && e.EndDevice.DevEUI.Equal(*inputEntry.EndDevice.DevEUI):
case e.EndDevice.EndDeviceIdentifiers.IsZero():
default:
continue
}
mappedEntry = &e
break
}
if mappedEntry == nil {
if fail, _ := cmd.Flags().GetBool("fail-not-found"); fail {
return errEndDeviceMappingNotFound
}
continue
}
var res ttnpb.EndDeviceTemplate
res.EndDevice.SetFields(&inputEntry.EndDevice, inputEntry.FieldMask.Paths...)
res.EndDevice.SetFields(&mappedEntry.EndDevice, mappedEntry.FieldMask.Paths...)
res.FieldMask.Paths = ttnpb.BottomLevelFields(append(inputEntry.FieldMask.Paths, mappedEntry.FieldMask.Paths...))
if err := io.Write(os.Stdout, config.OutputFormat, &res); err != nil {
return err
}
}
return nil
},
}
)
func init() {
endDeviceTemplatesExtendCommand.Flags().AddFlagSet(attributesFlags())
endDeviceTemplatesExtendCommand.Flags().String("mapping-key", "", "")
endDeviceTemplatesCommand.AddCommand(endDeviceTemplatesExtendCommand)
endDeviceTemplatesCreateCommand.Flags().AddFlagSet(selectEndDeviceIDFlags())
endDeviceTemplatesCreateCommand.Flags().String("mapping-key", "", "")
endDeviceTemplatesCommand.AddCommand(endDeviceTemplatesCreateCommand)
endDeviceTemplatesCommand.AddCommand(endDeviceTemplatesExecuteCommand)
endDeviceTemplatesAssignEUIsCommand.Flags().String("join-eui", "", "(hex)")
endDeviceTemplatesAssignEUIsCommand.Flags().String("start-dev-eui", "", "(hex)")
endDeviceTemplatesAssignEUIsCommand.Flags().Int("count", 1, "")
endDeviceTemplatesCommand.AddCommand(endDeviceTemplatesAssignEUIsCommand)
endDeviceTemplatesCommand.AddCommand(endDeviceTemplatesListFormats)
endDeviceTemplatesFromDataCommand.Flags().AddFlagSet(templateFormatIDFlags())
endDeviceTemplatesFromDataCommand.Flags().AddFlagSet(dataFlags("", ""))
endDeviceTemplatesCommand.AddCommand(endDeviceTemplatesFromDataCommand)
endDeviceTemplatesMapCommand.Flags().AddFlagSet(dataFlags("input", "input file"))
endDeviceTemplatesMapCommand.Flags().AddFlagSet(dataFlags("mapping", "mapping file"))
endDeviceTemplatesMapCommand.Flags().Bool("fail-not-found", false, "fail if no matching mapping is found")
endDeviceTemplatesCommand.AddCommand(endDeviceTemplatesMapCommand)
endDevicesCommand.AddCommand(endDeviceTemplatesCommand)
Root.AddCommand(endDeviceTemplatesCommand)
}