blob: dd0bfc8af4ef79190f422e9f19748ddd7a7c2b8a [file] [log] [blame]
Zack Williamse940c7a2019-08-21 14:25:39 -07001/*
2 * Copyright 2019-present Ciena Corporation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package commands
17
18import (
19 "context"
20 "fmt"
David Bainbridge7052fe82020-03-25 10:37:00 -070021 "os"
22 "strconv"
23 "strings"
24
Scott Baker9173ed82020-05-19 08:30:12 -070025 "github.com/golang/protobuf/ptypes/empty"
Zack Williamse940c7a2019-08-21 14:25:39 -070026 flags "github.com/jessevdk/go-flags"
Scott Baker2b0ad652019-08-21 14:57:07 -070027 "github.com/opencord/voltctl/pkg/format"
Scott Baker9173ed82020-05-19 08:30:12 -070028 "github.com/opencord/voltha-protos/v3/go/common"
29 "github.com/opencord/voltha-protos/v3/go/voltha"
Zack Williamse940c7a2019-08-21 14:25:39 -070030)
31
32const (
David K. Bainbridge89003c42020-02-27 17:22:49 -080033 DEFAULT_DEVICE_FORMAT = "table{{ .Id }}\t{{.Type}}\t{{.Root}}\t{{.ParentId}}\t{{.SerialNumber}}\t{{.AdminState}}\t{{.OperStatus}}\t{{.ConnectStatus}}\t{{.Reason}}"
Zack Williamse940c7a2019-08-21 14:25:39 -070034 DEFAULT_DEVICE_PORTS_FORMAT = "table{{.PortNo}}\t{{.Label}}\t{{.Type}}\t{{.AdminState}}\t{{.OperStatus}}\t{{.DeviceId}}\t{{.Peers}}"
35 DEFAULT_DEVICE_INSPECT_FORMAT = `ID: {{.Id}}
36 TYPE: {{.Type}}
37 ROOT: {{.Root}}
38 PARENTID: {{.ParentId}}
39 SERIALNUMBER: {{.SerialNumber}}
40 VLAN: {{.Vlan}}
41 ADMINSTATE: {{.AdminState}}
42 OPERSTATUS: {{.OperStatus}}
43 CONNECTSTATUS: {{.ConnectStatus}}`
Rohan Agrawal9228d2f2020-06-03 07:48:50 +000044 DEFAULT_DEVICE_PM_CONFIG_GET_FORMAT = "table{{.DefaultFreq}}\t{{.Grouped}}\t{{.FreqOverride}}"
45 DEFAULT_DEVICE_PM_CONFIG_METRIC_LIST_FORMAT = "table{{.Name}}\t{{.Type}}\t{{.Enabled}}\t{{.SampleFreq}}"
46 DEFAULT_DEVICE_PM_CONFIG_GROUP_LIST_FORMAT = "table{{.GroupName}}\t{{.Enabled}}\t{{.GroupFreq}}"
47 DEFAULT_DEVICE_VALUE_GET_FORMAT = "table{{.Name}}\t{{.Result}}"
Zack Williamse940c7a2019-08-21 14:25:39 -070048)
49
50type DeviceList struct {
51 ListOutputOptions
52}
53
54type DeviceCreate struct {
David Bainbridge1a514392020-06-23 11:12:51 -070055 DeviceType string `short:"t" required:"true" long:"devicetype" description:"Device type"`
David Bainbridge835dd0e2020-04-01 10:30:09 -070056 MACAddress string `short:"m" long:"macaddress" default:"" description:"MAC Address"`
Zack Williamse940c7a2019-08-21 14:25:39 -070057 IPAddress string `short:"i" long:"ipaddress" default:"" description:"IP Address"`
58 HostAndPort string `short:"H" long:"hostandport" default:"" description:"Host and port"`
59}
60
61type DeviceId string
62
Rohan Agrawal9228d2f2020-06-03 07:48:50 +000063type MetricName string
64type GroupName string
kesavand12cd8eb2020-01-20 22:25:22 -050065type PortNum uint32
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -080066type ValueFlag string
kesavand12cd8eb2020-01-20 22:25:22 -050067
Zack Williamse940c7a2019-08-21 14:25:39 -070068type DeviceDelete struct {
69 Args struct {
70 Ids []DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
71 } `positional-args:"yes"`
72}
73
74type DeviceEnable struct {
75 Args struct {
76 Ids []DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
77 } `positional-args:"yes"`
78}
79
80type DeviceDisable struct {
81 Args struct {
82 Ids []DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
83 } `positional-args:"yes"`
84}
85
86type DeviceReboot struct {
87 Args struct {
88 Ids []DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
89 } `positional-args:"yes"`
90}
91
92type DeviceFlowList struct {
93 ListOutputOptions
94 Args struct {
95 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
96 } `positional-args:"yes"`
97}
98
99type DevicePortList struct {
100 ListOutputOptions
101 Args struct {
102 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
103 } `positional-args:"yes"`
104}
105
106type DeviceInspect struct {
107 OutputOptionsJson
108 Args struct {
109 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
110 } `positional-args:"yes"`
111}
112
kesavand12cd8eb2020-01-20 22:25:22 -0500113type DevicePortEnable struct {
114 Args struct {
115 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
116 PortId PortNum `positional-arg-name:"PORT_NUMBER" required:"yes"`
117 } `positional-args:"yes"`
118}
119
120type DevicePortDisable struct {
121 Args struct {
122 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
123 PortId PortNum `positional-arg-name:"PORT_NUMBER" required:"yes"`
124 } `positional-args:"yes"`
125}
126
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000127type DevicePmConfigsGet struct {
128 ListOutputOptions
129 Args struct {
130 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
131 } `positional-args:"yes"`
132}
133
134type DevicePmConfigMetricList struct {
135 ListOutputOptions
136 Args struct {
137 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
138 } `positional-args:"yes"`
139}
140
141type DevicePmConfigGroupList struct {
142 ListOutputOptions
143 Args struct {
144 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
145 } `positional-args:"yes"`
146}
147
148type DevicePmConfigGroupMetricList struct {
149 ListOutputOptions
150 Args struct {
151 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
152 Group GroupName `positional-arg-name:"GROUP_NAME" required:"yes"`
153 } `positional-args:"yes"`
154}
155
156type DevicePmConfigFrequencySet struct {
157 OutputOptions
158 Args struct {
159 Frequency uint32 `positional-arg-name:"FREQUENCY" required:"yes"`
160 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
161 } `positional-args:"yes"`
162}
163
164type DevicePmConfigMetricEnable struct {
165 Args struct {
166 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
167 Metrics []MetricName `positional-arg-name:"METRIC_NAME" required:"yes"`
168 } `positional-args:"yes"`
169}
170
171type DevicePmConfigMetricDisable struct {
172 Args struct {
173 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
174 Metrics []MetricName `positional-arg-name:"METRIC_NAME" required:"yes"`
175 } `positional-args:"yes"`
176}
177
178type DevicePmConfigGroupEnable struct {
179 Args struct {
180 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
181 Groups []GroupName `positional-arg-name:"GROUP_NAME" required:"yes"`
182 } `positional-args:"yes"`
183}
184
185type DevicePmConfigGroupDisable struct {
186 Args struct {
187 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
188 Groups []GroupName `positional-arg-name:"GROUP_NAME" required:"yes"`
189 } `positional-args:"yes"`
190}
191
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -0800192type DeviceGetExtValue struct {
193 ListOutputOptions
194 Args struct {
195 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
196 Valueflag ValueFlag `positional-arg-name:"VALUE_FLAG" required:"yes"`
197 } `positional-args:"yes"`
198}
Rohan Agrawald7df3772020-06-29 11:23:36 +0000199
200type DevicePmConfigSetMaxSkew struct {
201 Args struct {
202 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
203 MaxSkew uint32 `positional-arg-name:"MAX_SKEW" required:"yes"`
204 } `positional-args:"yes"`
205}
206
Zack Williamse940c7a2019-08-21 14:25:39 -0700207type DeviceOpts struct {
208 List DeviceList `command:"list"`
209 Create DeviceCreate `command:"create"`
210 Delete DeviceDelete `command:"delete"`
211 Enable DeviceEnable `command:"enable"`
212 Disable DeviceDisable `command:"disable"`
213 Flows DeviceFlowList `command:"flows"`
kesavand12cd8eb2020-01-20 22:25:22 -0500214 Port struct {
215 List DevicePortList `command:"list"`
216 Enable DevicePortEnable `command:"enable"`
217 Disable DevicePortDisable `command:"disable"`
218 } `command:"port"`
219 Inspect DeviceInspect `command:"inspect"`
220 Reboot DeviceReboot `command:"reboot"`
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -0800221 Value struct {
222 Get DeviceGetExtValue `command:"get"`
223 } `command:"value"`
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000224 PmConfig struct {
Rohan Agrawald7df3772020-06-29 11:23:36 +0000225 Get DevicePmConfigsGet `command:"get"`
226 MaxSkew struct {
227 Set DevicePmConfigSetMaxSkew `command:"set"`
228 } `command:"maxskew"`
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000229 Frequency struct {
230 Set DevicePmConfigFrequencySet `command:"set"`
231 } `command:"frequency"`
232 Metric struct {
233 List DevicePmConfigMetricList `command:"list"`
234 Enable DevicePmConfigMetricEnable `command:"enable"`
235 Disable DevicePmConfigMetricDisable `command:"disable"`
236 } `command:"metric"`
237 Group struct {
238 List DevicePmConfigGroupList `command:"list"`
239 Enable DevicePmConfigGroupEnable `command:"enable"`
240 Disable DevicePmConfigGroupDisable `command:"disable"`
241 } `command:"group"`
242 GroupMetric struct {
243 List DevicePmConfigGroupMetricList `command:"list"`
244 } `command:"groupmetric"`
245 } `command:"pmconfig"`
Zack Williamse940c7a2019-08-21 14:25:39 -0700246}
247
248var deviceOpts = DeviceOpts{}
249
250func RegisterDeviceCommands(parser *flags.Parser) {
David Bainbridge12f036f2019-10-15 22:09:04 +0000251 if _, err := parser.AddCommand("device", "device commands", "Commands to query and manipulate VOLTHA devices", &deviceOpts); err != nil {
David Bainbridgea6722342019-10-24 23:55:53 +0000252 Error.Fatalf("Unexpected error while attempting to register device commands : %s", err)
David Bainbridge12f036f2019-10-15 22:09:04 +0000253 }
Zack Williamse940c7a2019-08-21 14:25:39 -0700254}
255
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000256func (i *MetricName) Complete(match string) []flags.Completion {
257 conn, err := NewConnection()
258 if err != nil {
259 return nil
260 }
261 defer conn.Close()
262
263 client := voltha.NewVolthaServiceClient(conn)
264
265 var deviceId string
266found:
267 for i := len(os.Args) - 1; i >= 0; i -= 1 {
268 switch os.Args[i] {
269 case "enable":
270 fallthrough
271 case "disable":
272 if len(os.Args) > i+1 {
273 deviceId = os.Args[i+1]
274 } else {
275 return nil
276 }
277 break found
278 default:
279 }
280 }
281
282 if len(deviceId) == 0 {
283 return nil
284 }
285
286 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
287 defer cancel()
288
289 id := voltha.ID{Id: string(deviceId)}
290
291 pmconfigs, err := client.ListDevicePmConfigs(ctx, &id)
292
293 if err != nil {
294 return nil
295 }
296
297 list := make([]flags.Completion, 0)
298 for _, metrics := range pmconfigs.Metrics {
299 if strings.HasPrefix(metrics.Name, match) {
300 list = append(list, flags.Completion{Item: metrics.Name})
301 }
302 }
303
304 return list
305}
306
307func (i *GroupName) Complete(match string) []flags.Completion {
308 conn, err := NewConnection()
309 if err != nil {
310 return nil
311 }
312 defer conn.Close()
313
314 client := voltha.NewVolthaServiceClient(conn)
315
316 var deviceId string
317found:
318 for i := len(os.Args) - 1; i >= 0; i -= 1 {
319 switch os.Args[i] {
320 case "list":
321 fallthrough
322 case "enable":
323 fallthrough
324 case "disable":
325 if len(os.Args) > i+1 {
326 deviceId = os.Args[i+1]
327 } else {
328 return nil
329 }
330 break found
331 default:
332 }
333 }
334
335 if len(deviceId) == 0 {
336 return nil
337 }
338
339 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
340 defer cancel()
341
342 id := voltha.ID{Id: string(deviceId)}
343
344 pmconfigs, err := client.ListDevicePmConfigs(ctx, &id)
345
346 if err != nil {
347 return nil
348 }
349
350 list := make([]flags.Completion, 0)
351 for _, group := range pmconfigs.Groups {
352 if strings.HasPrefix(group.GroupName, match) {
353 list = append(list, flags.Completion{Item: group.GroupName})
354 }
355 }
356 return list
357}
358
kesavand12cd8eb2020-01-20 22:25:22 -0500359func (i *PortNum) Complete(match string) []flags.Completion {
360 conn, err := NewConnection()
361 if err != nil {
362 return nil
363 }
364 defer conn.Close()
365
Scott Baker9173ed82020-05-19 08:30:12 -0700366 client := voltha.NewVolthaServiceClient(conn)
kesavand12cd8eb2020-01-20 22:25:22 -0500367
368 /*
369 * The command line args when completing for PortNum will be a DeviceId
370 * followed by one or more PortNums. So walk the argument list from the
371 * end and find the first argument that is enable/disable as those are
372 * the subcommands that come before the positional arguments. It would
373 * be nice if this package gave us the list of optional arguments
374 * already parsed.
375 */
376 var deviceId string
377found:
378 for i := len(os.Args) - 1; i >= 0; i -= 1 {
379 switch os.Args[i] {
380 case "enable":
381 fallthrough
382 case "disable":
383 if len(os.Args) > i+1 {
384 deviceId = os.Args[i+1]
385 } else {
386 return nil
387 }
388 break found
389 default:
390 }
391 }
392
393 if len(deviceId) == 0 {
394 return nil
395 }
396
kesavand12cd8eb2020-01-20 22:25:22 -0500397 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
398 defer cancel()
kesavand12cd8eb2020-01-20 22:25:22 -0500399
Scott Baker9173ed82020-05-19 08:30:12 -0700400 id := voltha.ID{Id: string(deviceId)}
kesavand12cd8eb2020-01-20 22:25:22 -0500401
Scott Baker9173ed82020-05-19 08:30:12 -0700402 ports, err := client.ListDevicePorts(ctx, &id)
kesavand12cd8eb2020-01-20 22:25:22 -0500403 if err != nil {
404 return nil
405 }
406
407 list := make([]flags.Completion, 0)
Scott Baker9173ed82020-05-19 08:30:12 -0700408 for _, item := range ports.Items {
409 pn := strconv.FormatUint(uint64(item.PortNo), 10)
kesavand12cd8eb2020-01-20 22:25:22 -0500410 if strings.HasPrefix(pn, match) {
411 list = append(list, flags.Completion{Item: pn})
412 }
413 }
414
415 return list
416}
417
Zack Williamse940c7a2019-08-21 14:25:39 -0700418func (i *DeviceId) Complete(match string) []flags.Completion {
419 conn, err := NewConnection()
420 if err != nil {
421 return nil
422 }
423 defer conn.Close()
424
Scott Baker9173ed82020-05-19 08:30:12 -0700425 client := voltha.NewVolthaServiceClient(conn)
Zack Williamse940c7a2019-08-21 14:25:39 -0700426
427 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
428 defer cancel()
429
Scott Baker9173ed82020-05-19 08:30:12 -0700430 devices, err := client.ListDevices(ctx, &empty.Empty{})
Zack Williamse940c7a2019-08-21 14:25:39 -0700431 if err != nil {
432 return nil
433 }
434
435 list := make([]flags.Completion, 0)
Scott Baker9173ed82020-05-19 08:30:12 -0700436 for _, item := range devices.Items {
437 if strings.HasPrefix(item.Id, match) {
438 list = append(list, flags.Completion{Item: item.Id})
Zack Williamse940c7a2019-08-21 14:25:39 -0700439 }
440 }
441
442 return list
443}
444
445func (options *DeviceList) Execute(args []string) error {
446
447 conn, err := NewConnection()
448 if err != nil {
449 return err
450 }
451 defer conn.Close()
452
Scott Baker9173ed82020-05-19 08:30:12 -0700453 client := voltha.NewVolthaServiceClient(conn)
Zack Williamse940c7a2019-08-21 14:25:39 -0700454
455 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
456 defer cancel()
457
Scott Baker9173ed82020-05-19 08:30:12 -0700458 devices, err := client.ListDevices(ctx, &empty.Empty{})
Zack Williamse940c7a2019-08-21 14:25:39 -0700459 if err != nil {
460 return err
461 }
462
463 outputFormat := CharReplacer.Replace(options.Format)
464 if outputFormat == "" {
David Bainbridgea6722342019-10-24 23:55:53 +0000465 outputFormat = GetCommandOptionWithDefault("device-list", "format", DEFAULT_DEVICE_FORMAT)
Zack Williamse940c7a2019-08-21 14:25:39 -0700466 }
467 if options.Quiet {
468 outputFormat = "{{.Id}}"
469 }
470
David Bainbridgea6722342019-10-24 23:55:53 +0000471 orderBy := options.OrderBy
472 if orderBy == "" {
473 orderBy = GetCommandOptionWithDefault("device-list", "order", "")
474 }
475
Scott Baker9173ed82020-05-19 08:30:12 -0700476 // Make sure json output prints an empty list, not "null"
477 if devices.Items == nil {
478 devices.Items = make([]*voltha.Device, 0)
Zack Williamse940c7a2019-08-21 14:25:39 -0700479 }
480
481 result := CommandResult{
482 Format: format.Format(outputFormat),
483 Filter: options.Filter,
David Bainbridgea6722342019-10-24 23:55:53 +0000484 OrderBy: orderBy,
Zack Williamse940c7a2019-08-21 14:25:39 -0700485 OutputAs: toOutputType(options.OutputAs),
486 NameLimit: options.NameLimit,
Scott Baker9173ed82020-05-19 08:30:12 -0700487 Data: devices.Items,
Zack Williamse940c7a2019-08-21 14:25:39 -0700488 }
489
490 GenerateOutput(&result)
491 return nil
492}
493
494func (options *DeviceCreate) Execute(args []string) error {
495
Scott Baker9173ed82020-05-19 08:30:12 -0700496 device := voltha.Device{}
Zack Williamse940c7a2019-08-21 14:25:39 -0700497 if options.HostAndPort != "" {
Scott Baker9173ed82020-05-19 08:30:12 -0700498 device.Address = &voltha.Device_HostAndPort{HostAndPort: options.HostAndPort}
Zack Williamse940c7a2019-08-21 14:25:39 -0700499 } else if options.IPAddress != "" {
Scott Baker9173ed82020-05-19 08:30:12 -0700500 device.Address = &voltha.Device_Ipv4Address{Ipv4Address: options.IPAddress}
Hardik Windlassce1de342020-02-04 21:58:07 +0000501 }
502 if options.MACAddress != "" {
Scott Baker9173ed82020-05-19 08:30:12 -0700503 device.MacAddress = strings.ToLower(options.MACAddress)
Zack Williamse940c7a2019-08-21 14:25:39 -0700504 }
505 if options.DeviceType != "" {
Scott Baker9173ed82020-05-19 08:30:12 -0700506 device.Type = options.DeviceType
Zack Williamse940c7a2019-08-21 14:25:39 -0700507 }
508
509 conn, err := NewConnection()
510 if err != nil {
511 return err
512 }
513 defer conn.Close()
514
Scott Baker9173ed82020-05-19 08:30:12 -0700515 client := voltha.NewVolthaServiceClient(conn)
Zack Williamse940c7a2019-08-21 14:25:39 -0700516
517 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
518 defer cancel()
519
Scott Baker9173ed82020-05-19 08:30:12 -0700520 createdDevice, err := client.CreateDevice(ctx, &device)
Zack Williamse940c7a2019-08-21 14:25:39 -0700521 if err != nil {
522 return err
Zack Williamse940c7a2019-08-21 14:25:39 -0700523 }
524
Scott Baker9173ed82020-05-19 08:30:12 -0700525 fmt.Printf("%s\n", createdDevice.Id)
Zack Williamse940c7a2019-08-21 14:25:39 -0700526
527 return nil
528}
529
530func (options *DeviceDelete) Execute(args []string) error {
531
532 conn, err := NewConnection()
533 if err != nil {
534 return err
535 }
536 defer conn.Close()
537
Scott Baker9173ed82020-05-19 08:30:12 -0700538 client := voltha.NewVolthaServiceClient(conn)
Zack Williamse940c7a2019-08-21 14:25:39 -0700539
David Bainbridge7052fe82020-03-25 10:37:00 -0700540 var lastErr error
Zack Williamse940c7a2019-08-21 14:25:39 -0700541 for _, i := range options.Args.Ids {
Zack Williamse940c7a2019-08-21 14:25:39 -0700542 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
543 defer cancel()
544
Scott Baker9173ed82020-05-19 08:30:12 -0700545 id := voltha.ID{Id: string(i)}
546
547 _, err := client.DeleteDevice(ctx, &id)
Zack Williamse940c7a2019-08-21 14:25:39 -0700548 if err != nil {
David Bainbridge0f758d42019-10-26 05:17:48 +0000549 Error.Printf("Error while deleting '%s': %s\n", i, err)
David Bainbridge7052fe82020-03-25 10:37:00 -0700550 lastErr = err
Zack Williamse940c7a2019-08-21 14:25:39 -0700551 continue
Zack Williamse940c7a2019-08-21 14:25:39 -0700552 }
553 fmt.Printf("%s\n", i)
554 }
555
David Bainbridge7052fe82020-03-25 10:37:00 -0700556 if lastErr != nil {
557 return NoReportErr
558 }
Zack Williamse940c7a2019-08-21 14:25:39 -0700559 return nil
560}
561
562func (options *DeviceEnable) Execute(args []string) error {
563 conn, err := NewConnection()
564 if err != nil {
565 return err
566 }
567 defer conn.Close()
568
Scott Baker9173ed82020-05-19 08:30:12 -0700569 client := voltha.NewVolthaServiceClient(conn)
Zack Williamse940c7a2019-08-21 14:25:39 -0700570
David Bainbridge7052fe82020-03-25 10:37:00 -0700571 var lastErr error
Zack Williamse940c7a2019-08-21 14:25:39 -0700572 for _, i := range options.Args.Ids {
Zack Williamse940c7a2019-08-21 14:25:39 -0700573 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
574 defer cancel()
575
Scott Baker9173ed82020-05-19 08:30:12 -0700576 id := voltha.ID{Id: string(i)}
577
578 _, err := client.EnableDevice(ctx, &id)
Zack Williamse940c7a2019-08-21 14:25:39 -0700579 if err != nil {
David Bainbridge0f758d42019-10-26 05:17:48 +0000580 Error.Printf("Error while enabling '%s': %s\n", i, err)
David Bainbridge7052fe82020-03-25 10:37:00 -0700581 lastErr = err
Zack Williamse940c7a2019-08-21 14:25:39 -0700582 continue
Zack Williamse940c7a2019-08-21 14:25:39 -0700583 }
584 fmt.Printf("%s\n", i)
585 }
586
David Bainbridge7052fe82020-03-25 10:37:00 -0700587 if lastErr != nil {
588 return NoReportErr
589 }
Zack Williamse940c7a2019-08-21 14:25:39 -0700590 return nil
591}
592
593func (options *DeviceDisable) Execute(args []string) error {
594 conn, err := NewConnection()
595 if err != nil {
596 return err
597 }
598 defer conn.Close()
599
Scott Baker9173ed82020-05-19 08:30:12 -0700600 client := voltha.NewVolthaServiceClient(conn)
Zack Williamse940c7a2019-08-21 14:25:39 -0700601
David Bainbridge7052fe82020-03-25 10:37:00 -0700602 var lastErr error
Zack Williamse940c7a2019-08-21 14:25:39 -0700603 for _, i := range options.Args.Ids {
Zack Williamse940c7a2019-08-21 14:25:39 -0700604 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
605 defer cancel()
606
Scott Baker9173ed82020-05-19 08:30:12 -0700607 id := voltha.ID{Id: string(i)}
608
609 _, err := client.DisableDevice(ctx, &id)
Zack Williamse940c7a2019-08-21 14:25:39 -0700610 if err != nil {
David Bainbridge0f758d42019-10-26 05:17:48 +0000611 Error.Printf("Error while disabling '%s': %s\n", i, err)
David Bainbridge7052fe82020-03-25 10:37:00 -0700612 lastErr = err
Zack Williamse940c7a2019-08-21 14:25:39 -0700613 continue
Zack Williamse940c7a2019-08-21 14:25:39 -0700614 }
615 fmt.Printf("%s\n", i)
616 }
617
David Bainbridge7052fe82020-03-25 10:37:00 -0700618 if lastErr != nil {
619 return NoReportErr
620 }
Zack Williamse940c7a2019-08-21 14:25:39 -0700621 return nil
622}
623
624func (options *DeviceReboot) Execute(args []string) error {
625 conn, err := NewConnection()
626 if err != nil {
627 return err
628 }
629 defer conn.Close()
630
Scott Baker9173ed82020-05-19 08:30:12 -0700631 client := voltha.NewVolthaServiceClient(conn)
Zack Williamse940c7a2019-08-21 14:25:39 -0700632
David Bainbridge7052fe82020-03-25 10:37:00 -0700633 var lastErr error
Zack Williamse940c7a2019-08-21 14:25:39 -0700634 for _, i := range options.Args.Ids {
Zack Williamse940c7a2019-08-21 14:25:39 -0700635 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
636 defer cancel()
637
Scott Baker9173ed82020-05-19 08:30:12 -0700638 id := voltha.ID{Id: string(i)}
639
640 _, err := client.RebootDevice(ctx, &id)
Zack Williamse940c7a2019-08-21 14:25:39 -0700641 if err != nil {
David Bainbridge0f758d42019-10-26 05:17:48 +0000642 Error.Printf("Error while rebooting '%s': %s\n", i, err)
David Bainbridge7052fe82020-03-25 10:37:00 -0700643 lastErr = err
Zack Williamse940c7a2019-08-21 14:25:39 -0700644 continue
Zack Williamse940c7a2019-08-21 14:25:39 -0700645 }
646 fmt.Printf("%s\n", i)
647 }
648
David Bainbridge7052fe82020-03-25 10:37:00 -0700649 if lastErr != nil {
650 return NoReportErr
651 }
Zack Williamse940c7a2019-08-21 14:25:39 -0700652 return nil
653}
654
655func (options *DevicePortList) Execute(args []string) error {
656
657 conn, err := NewConnection()
658 if err != nil {
659 return err
660 }
661 defer conn.Close()
662
Scott Baker9173ed82020-05-19 08:30:12 -0700663 client := voltha.NewVolthaServiceClient(conn)
Zack Williamse940c7a2019-08-21 14:25:39 -0700664
665 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
666 defer cancel()
667
Scott Baker9173ed82020-05-19 08:30:12 -0700668 id := voltha.ID{Id: string(options.Args.Id)}
Zack Williamse940c7a2019-08-21 14:25:39 -0700669
Scott Baker9173ed82020-05-19 08:30:12 -0700670 ports, err := client.ListDevicePorts(ctx, &id)
Zack Williamse940c7a2019-08-21 14:25:39 -0700671 if err != nil {
672 return err
673 }
674
675 outputFormat := CharReplacer.Replace(options.Format)
676 if outputFormat == "" {
David Bainbridgea6722342019-10-24 23:55:53 +0000677 outputFormat = GetCommandOptionWithDefault("device-ports", "format", DEFAULT_DEVICE_PORTS_FORMAT)
Zack Williamse940c7a2019-08-21 14:25:39 -0700678 }
Zack Williamse940c7a2019-08-21 14:25:39 -0700679
David Bainbridgea6722342019-10-24 23:55:53 +0000680 orderBy := options.OrderBy
681 if orderBy == "" {
682 orderBy = GetCommandOptionWithDefault("device-ports", "order", "")
683 }
684
Zack Williamse940c7a2019-08-21 14:25:39 -0700685 result := CommandResult{
686 Format: format.Format(outputFormat),
687 Filter: options.Filter,
David Bainbridgea6722342019-10-24 23:55:53 +0000688 OrderBy: orderBy,
Zack Williamse940c7a2019-08-21 14:25:39 -0700689 OutputAs: toOutputType(options.OutputAs),
690 NameLimit: options.NameLimit,
Scott Baker9173ed82020-05-19 08:30:12 -0700691 Data: ports.Items,
Zack Williamse940c7a2019-08-21 14:25:39 -0700692 }
693
694 GenerateOutput(&result)
695 return nil
696}
697
698func (options *DeviceFlowList) Execute(args []string) error {
699 fl := &FlowList{}
700 fl.ListOutputOptions = options.ListOutputOptions
701 fl.Args.Id = string(options.Args.Id)
David Bainbridgea6722342019-10-24 23:55:53 +0000702 fl.Method = "device-flows"
Zack Williamse940c7a2019-08-21 14:25:39 -0700703 return fl.Execute(args)
704}
705
706func (options *DeviceInspect) Execute(args []string) error {
707 if len(args) > 0 {
708 return fmt.Errorf("only a single argument 'DEVICE_ID' can be provided")
709 }
710
711 conn, err := NewConnection()
712 if err != nil {
713 return err
714 }
715 defer conn.Close()
716
Scott Baker9173ed82020-05-19 08:30:12 -0700717 client := voltha.NewVolthaServiceClient(conn)
Zack Williamse940c7a2019-08-21 14:25:39 -0700718
719 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
720 defer cancel()
721
Scott Baker9173ed82020-05-19 08:30:12 -0700722 id := voltha.ID{Id: string(options.Args.Id)}
Zack Williamse940c7a2019-08-21 14:25:39 -0700723
Scott Baker9173ed82020-05-19 08:30:12 -0700724 device, err := client.GetDevice(ctx, &id)
Zack Williamse940c7a2019-08-21 14:25:39 -0700725 if err != nil {
726 return err
727 }
728
Zack Williamse940c7a2019-08-21 14:25:39 -0700729 outputFormat := CharReplacer.Replace(options.Format)
730 if outputFormat == "" {
David Bainbridgea6722342019-10-24 23:55:53 +0000731 outputFormat = GetCommandOptionWithDefault("device-inspect", "format", DEFAULT_DEVICE_INSPECT_FORMAT)
Zack Williamse940c7a2019-08-21 14:25:39 -0700732 }
733 if options.Quiet {
734 outputFormat = "{{.Id}}"
735 }
736
737 result := CommandResult{
738 Format: format.Format(outputFormat),
739 OutputAs: toOutputType(options.OutputAs),
740 NameLimit: options.NameLimit,
741 Data: device,
742 }
743 GenerateOutput(&result)
744 return nil
745}
kesavand12cd8eb2020-01-20 22:25:22 -0500746
747/*Device Port Enable */
748func (options *DevicePortEnable) Execute(args []string) error {
749 conn, err := NewConnection()
750 if err != nil {
751 return err
752 }
753 defer conn.Close()
754
Scott Baker9173ed82020-05-19 08:30:12 -0700755 client := voltha.NewVolthaServiceClient(conn)
kesavand12cd8eb2020-01-20 22:25:22 -0500756
kesavand12cd8eb2020-01-20 22:25:22 -0500757 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
758 defer cancel()
759
Scott Baker9173ed82020-05-19 08:30:12 -0700760 port := voltha.Port{DeviceId: string(options.Args.Id), PortNo: uint32(options.Args.PortId)}
761
762 _, err = client.EnablePort(ctx, &port)
kesavand12cd8eb2020-01-20 22:25:22 -0500763 if err != nil {
764 Error.Printf("Error enabling port number %v on device Id %s,err=%s\n", options.Args.PortId, options.Args.Id, ErrorToString(err))
765 return err
kesavand12cd8eb2020-01-20 22:25:22 -0500766 }
767
768 return nil
769}
770
Scott Baker9173ed82020-05-19 08:30:12 -0700771/*Device Port Disable */
kesavand12cd8eb2020-01-20 22:25:22 -0500772func (options *DevicePortDisable) Execute(args []string) error {
773 conn, err := NewConnection()
774 if err != nil {
775 return err
776 }
777 defer conn.Close()
778
Scott Baker9173ed82020-05-19 08:30:12 -0700779 client := voltha.NewVolthaServiceClient(conn)
780
kesavand12cd8eb2020-01-20 22:25:22 -0500781 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
782 defer cancel()
783
Scott Baker9173ed82020-05-19 08:30:12 -0700784 port := voltha.Port{DeviceId: string(options.Args.Id), PortNo: uint32(options.Args.PortId)}
785
786 _, err = client.DisablePort(ctx, &port)
kesavand12cd8eb2020-01-20 22:25:22 -0500787 if err != nil {
Scott Baker9173ed82020-05-19 08:30:12 -0700788 Error.Printf("Error enabling port number %v on device Id %s,err=%s\n", options.Args.PortId, options.Args.Id, ErrorToString(err))
kesavand12cd8eb2020-01-20 22:25:22 -0500789 return err
kesavand12cd8eb2020-01-20 22:25:22 -0500790 }
Scott Baker9173ed82020-05-19 08:30:12 -0700791
kesavand12cd8eb2020-01-20 22:25:22 -0500792 return nil
793}
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -0800794
Rohan Agrawald7df3772020-06-29 11:23:36 +0000795func (options *DevicePmConfigSetMaxSkew) Execute(args []string) error {
796 conn, err := NewConnection()
797 if err != nil {
798 return err
799 }
800 defer conn.Close()
801
802 client := voltha.NewVolthaServiceClient(conn)
803
804 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
805 defer cancel()
806
807 id := voltha.ID{Id: string(options.Args.Id)}
808
809 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
810 if err != nil {
811 return err
812 }
813
814 pmConfigs.MaxSkew = options.Args.MaxSkew
815
816 _, err = client.UpdateDevicePmConfigs(ctx, pmConfigs)
817 if err != nil {
818 return err
819 }
820
821 return nil
822}
823
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000824func (options *DevicePmConfigsGet) Execute(args []string) error {
825
826 conn, err := NewConnection()
827 if err != nil {
828 return err
829 }
830 defer conn.Close()
831
832 client := voltha.NewVolthaServiceClient(conn)
833
834 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
835 defer cancel()
836
837 id := voltha.ID{Id: string(options.Args.Id)}
838
839 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
840 if err != nil {
841 return err
842 }
843
844 outputFormat := CharReplacer.Replace(options.Format)
845 if outputFormat == "" {
846 outputFormat = GetCommandOptionWithDefault("device-pm-configs", "format", DEFAULT_DEVICE_PM_CONFIG_GET_FORMAT)
847 }
848
849 orderBy := options.OrderBy
850 if orderBy == "" {
851 orderBy = GetCommandOptionWithDefault("device-pm-configs", "order", "")
852 }
853
854 result := CommandResult{
855 Format: format.Format(outputFormat),
856 Filter: options.Filter,
857 OrderBy: orderBy,
858 OutputAs: toOutputType(options.OutputAs),
859 NameLimit: options.NameLimit,
860 Data: pmConfigs,
861 }
862
863 GenerateOutput(&result)
864 return nil
865
866}
867
868func (options *DevicePmConfigMetricList) Execute(args []string) error {
869
870 conn, err := NewConnection()
871 if err != nil {
872 return err
873 }
874 defer conn.Close()
875
876 client := voltha.NewVolthaServiceClient(conn)
877
878 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
879 defer cancel()
880
881 id := voltha.ID{Id: string(options.Args.Id)}
882
883 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
884 if err != nil {
885 return err
886 }
887
888 if !pmConfigs.Grouped {
889 for _, metric := range pmConfigs.Metrics {
890 if metric.SampleFreq == 0 {
891 metric.SampleFreq = pmConfigs.DefaultFreq
892 }
893 }
Rohan Agrawalbca69122020-06-17 14:59:03 +0000894 outputFormat := CharReplacer.Replace(options.Format)
895 if outputFormat == "" {
896 outputFormat = GetCommandOptionWithDefault("device-pm-configs", "format", DEFAULT_DEVICE_PM_CONFIG_METRIC_LIST_FORMAT)
897 }
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000898
Rohan Agrawalbca69122020-06-17 14:59:03 +0000899 orderBy := options.OrderBy
900 if orderBy == "" {
901 orderBy = GetCommandOptionWithDefault("device-pm-configs", "order", "")
902 }
903
904 result := CommandResult{
905 Format: format.Format(outputFormat),
906 Filter: options.Filter,
907 OrderBy: orderBy,
908 OutputAs: toOutputType(options.OutputAs),
909 NameLimit: options.NameLimit,
910 Data: pmConfigs.Metrics,
911 }
912
913 GenerateOutput(&result)
914 return nil
915 } else {
916 return fmt.Errorf("Device '%s' does not have Non Grouped Metrics", options.Args.Id)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000917 }
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000918}
919
920func (options *DevicePmConfigMetricEnable) Execute(args []string) error {
921
922 conn, err := NewConnection()
923 if err != nil {
924 return err
925 }
926 defer conn.Close()
927
928 client := voltha.NewVolthaServiceClient(conn)
929
930 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
931 defer cancel()
932
933 id := voltha.ID{Id: string(options.Args.Id)}
934
935 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
936 if err != nil {
937 return err
938 }
939
940 if !pmConfigs.Grouped {
Rohan Agrawalbca69122020-06-17 14:59:03 +0000941 metrics := make(map[string]struct{})
942 for _, metric := range pmConfigs.Metrics {
943 metrics[metric.Name] = struct{}{}
944 }
945
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000946 for _, metric := range pmConfigs.Metrics {
947 for _, mName := range options.Args.Metrics {
Rohan Agrawalbca69122020-06-17 14:59:03 +0000948 if _, exist := metrics[string(mName)]; !exist {
949 return fmt.Errorf("Metric Name '%s' does not exist", mName)
950 }
951
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000952 if string(mName) == metric.Name && !metric.Enabled {
953 metric.Enabled = true
954 _, err := client.UpdateDevicePmConfigs(ctx, pmConfigs)
955 if err != nil {
956 return err
957 }
958 }
959 }
960 }
Rohan Agrawalbca69122020-06-17 14:59:03 +0000961 } else {
962 return fmt.Errorf("Device '%s' does not have Non Grouped Metrics", options.Args.Id)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000963 }
964 return nil
965}
966
967func (options *DevicePmConfigMetricDisable) Execute(args []string) error {
968
969 conn, err := NewConnection()
970 if err != nil {
971 return err
972 }
973 defer conn.Close()
974
975 client := voltha.NewVolthaServiceClient(conn)
976
977 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
978 defer cancel()
979
980 id := voltha.ID{Id: string(options.Args.Id)}
981
982 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
983 if err != nil {
984 return err
985 }
986
987 if !pmConfigs.Grouped {
Rohan Agrawalbca69122020-06-17 14:59:03 +0000988 metrics := make(map[string]struct{})
989 for _, metric := range pmConfigs.Metrics {
990 metrics[metric.Name] = struct{}{}
991 }
992
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000993 for _, metric := range pmConfigs.Metrics {
994 for _, mName := range options.Args.Metrics {
Rohan Agrawalbca69122020-06-17 14:59:03 +0000995 if _, have := metrics[string(mName)]; !have {
996 return fmt.Errorf("Metric Name '%s' does not exist", mName)
997 }
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000998 if string(mName) == metric.Name && metric.Enabled {
999 metric.Enabled = false
1000 _, err := client.UpdateDevicePmConfigs(ctx, pmConfigs)
1001 if err != nil {
1002 return err
1003 }
Rohan Agrawalbca69122020-06-17 14:59:03 +00001004 } else {
1005 return fmt.Errorf("Metric '%s' cannot be disabled", string(mName))
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001006 }
1007 }
1008 }
Rohan Agrawalbca69122020-06-17 14:59:03 +00001009 } else {
1010 return fmt.Errorf("Device '%s' does not have Non Grouped Metrics", options.Args.Id)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001011 }
1012 return nil
1013}
1014
1015func (options *DevicePmConfigGroupEnable) Execute(args []string) error {
1016
1017 conn, err := NewConnection()
1018 if err != nil {
1019 return err
1020 }
1021 defer conn.Close()
1022
1023 client := voltha.NewVolthaServiceClient(conn)
1024
1025 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
1026 defer cancel()
1027
1028 id := voltha.ID{Id: string(options.Args.Id)}
1029
1030 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
1031 if err != nil {
1032 return err
1033 }
1034
1035 if pmConfigs.Grouped {
Rohan Agrawalbca69122020-06-17 14:59:03 +00001036 groups := make(map[string]struct{})
1037 for _, group := range pmConfigs.Groups {
1038 groups[group.GroupName] = struct{}{}
1039 }
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001040 for _, group := range pmConfigs.Groups {
1041 for _, gName := range options.Args.Groups {
Rohan Agrawalbca69122020-06-17 14:59:03 +00001042 if _, have := groups[string(gName)]; !have {
1043 return fmt.Errorf("Group Name '%s' does not exist", gName)
1044 }
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001045 if string(gName) == group.GroupName && !group.Enabled {
1046 group.Enabled = true
1047 _, err := client.UpdateDevicePmConfigs(ctx, pmConfigs)
1048 if err != nil {
1049 return err
1050 }
1051 }
1052 }
1053 }
Rohan Agrawalbca69122020-06-17 14:59:03 +00001054 } else {
1055 return fmt.Errorf("Device '%s' does not have Group Metrics", options.Args.Id)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001056 }
1057 return nil
1058}
1059
1060func (options *DevicePmConfigGroupDisable) Execute(args []string) error {
1061
1062 conn, err := NewConnection()
1063 if err != nil {
1064 return err
1065 }
1066 defer conn.Close()
1067
1068 client := voltha.NewVolthaServiceClient(conn)
1069
1070 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
1071 defer cancel()
1072
1073 id := voltha.ID{Id: string(options.Args.Id)}
1074
1075 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
1076 if err != nil {
1077 return err
1078 }
1079
1080 if pmConfigs.Grouped {
Rohan Agrawalbca69122020-06-17 14:59:03 +00001081 groups := make(map[string]struct{})
1082 for _, group := range pmConfigs.Groups {
1083 groups[group.GroupName] = struct{}{}
1084 }
1085
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001086 for _, group := range pmConfigs.Groups {
1087 for _, gName := range options.Args.Groups {
Rohan Agrawalbca69122020-06-17 14:59:03 +00001088 if _, have := groups[string(gName)]; !have {
1089 return fmt.Errorf("Group Name '%s' does not exist", gName)
1090 }
1091
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001092 if string(gName) == group.GroupName && group.Enabled {
1093 group.Enabled = false
1094 _, err := client.UpdateDevicePmConfigs(ctx, pmConfigs)
1095 if err != nil {
1096 return err
1097 }
1098 }
1099 }
1100 }
Rohan Agrawalbca69122020-06-17 14:59:03 +00001101 } else {
1102 return fmt.Errorf("Device '%s' does not have Group Metrics", options.Args.Id)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001103 }
1104 return nil
1105}
1106
1107func (options *DevicePmConfigGroupList) Execute(args []string) error {
1108
1109 conn, err := NewConnection()
1110 if err != nil {
1111 return err
1112 }
1113 defer conn.Close()
1114
1115 client := voltha.NewVolthaServiceClient(conn)
1116
1117 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
1118 defer cancel()
1119
1120 id := voltha.ID{Id: string(options.Args.Id)}
1121
1122 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
1123 if err != nil {
1124 return err
1125 }
1126
1127 if pmConfigs.Grouped {
1128 for _, group := range pmConfigs.Groups {
1129 if group.GroupFreq == 0 {
1130 group.GroupFreq = pmConfigs.DefaultFreq
1131 }
1132 }
Rohan Agrawalbca69122020-06-17 14:59:03 +00001133 outputFormat := CharReplacer.Replace(options.Format)
1134 if outputFormat == "" {
1135 outputFormat = GetCommandOptionWithDefault("device-pm-configs", "format", DEFAULT_DEVICE_PM_CONFIG_GROUP_LIST_FORMAT)
1136 }
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001137
Rohan Agrawalbca69122020-06-17 14:59:03 +00001138 orderBy := options.OrderBy
1139 if orderBy == "" {
1140 orderBy = GetCommandOptionWithDefault("device-pm-configs", "order", "")
1141 }
1142
1143 result := CommandResult{
1144 Format: format.Format(outputFormat),
1145 Filter: options.Filter,
1146 OrderBy: orderBy,
1147 OutputAs: toOutputType(options.OutputAs),
1148 NameLimit: options.NameLimit,
1149 Data: pmConfigs.Groups,
1150 }
1151
1152 GenerateOutput(&result)
1153 } else {
1154 return fmt.Errorf("Device '%s' does not have Group Metrics", string(options.Args.Id))
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001155 }
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001156 return nil
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001157}
1158
1159func (options *DevicePmConfigGroupMetricList) Execute(args []string) error {
1160
1161 var metrics []*voltha.PmConfig
1162 conn, err := NewConnection()
1163 if err != nil {
1164 return err
1165 }
1166 defer conn.Close()
1167
1168 client := voltha.NewVolthaServiceClient(conn)
1169
1170 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
1171 defer cancel()
1172
1173 id := voltha.ID{Id: string(options.Args.Id)}
1174
1175 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
1176 if err != nil {
1177 return err
1178 }
1179
1180 for _, groups := range pmConfigs.Groups {
1181
1182 if string(options.Args.Group) == groups.GroupName {
1183 for _, metric := range groups.Metrics {
1184 if metric.SampleFreq == 0 && groups.GroupFreq == 0 {
1185 metric.SampleFreq = pmConfigs.DefaultFreq
1186 } else {
1187 metric.SampleFreq = groups.GroupFreq
1188 }
1189 }
1190 metrics = groups.Metrics
1191 }
1192 }
1193
1194 outputFormat := CharReplacer.Replace(options.Format)
1195 if outputFormat == "" {
1196 outputFormat = GetCommandOptionWithDefault("device-pm-configs", "format", DEFAULT_DEVICE_PM_CONFIG_METRIC_LIST_FORMAT)
1197 }
1198
1199 orderBy := options.OrderBy
1200 if orderBy == "" {
1201 orderBy = GetCommandOptionWithDefault("device-pm-configs", "order", "")
1202 }
1203
1204 result := CommandResult{
1205 Format: format.Format(outputFormat),
1206 Filter: options.Filter,
1207 OrderBy: orderBy,
1208 OutputAs: toOutputType(options.OutputAs),
1209 NameLimit: options.NameLimit,
1210 Data: metrics,
1211 }
1212
1213 GenerateOutput(&result)
1214 return nil
1215
1216}
1217
1218func (options *DevicePmConfigFrequencySet) Execute(args []string) error {
1219
1220 conn, err := NewConnection()
1221 if err != nil {
1222 return err
1223 }
1224 defer conn.Close()
1225
1226 client := voltha.NewVolthaServiceClient(conn)
1227
1228 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
1229 defer cancel()
1230
1231 id := voltha.ID{Id: string(options.Args.Id)}
1232
1233 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
1234 if err != nil {
1235 return err
1236 }
1237
1238 pmConfigs.DefaultFreq = options.Args.Frequency
1239
1240 _, err = client.UpdateDevicePmConfigs(ctx, pmConfigs)
1241 if err != nil {
1242 return err
1243 }
1244
1245 outputFormat := CharReplacer.Replace(options.Format)
1246 if outputFormat == "" {
1247 outputFormat = GetCommandOptionWithDefault("device-pm-configs", "format", DEFAULT_DEVICE_PM_CONFIG_GET_FORMAT)
1248 }
1249 if options.Quiet {
1250 outputFormat = "{{.Id}}"
1251 }
1252
1253 result := CommandResult{
1254 Format: format.Format(outputFormat),
1255 OutputAs: toOutputType(options.OutputAs),
1256 NameLimit: options.NameLimit,
1257 Data: pmConfigs,
1258 }
1259
1260 GenerateOutput(&result)
1261 return nil
1262
1263}
1264
Scott Baker9173ed82020-05-19 08:30:12 -07001265type ReturnValueRow struct {
1266 Name string `json:"name"`
1267 Result interface{} `json:"result"`
1268}
1269
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -08001270/*Device get Onu Distance */
1271func (options *DeviceGetExtValue) Execute(args []string) error {
1272 conn, err := NewConnection()
1273 if err != nil {
1274 return err
1275 }
1276 defer conn.Close()
1277
Scott Baker9173ed82020-05-19 08:30:12 -07001278 client := voltha.NewVolthaServiceClient(conn)
1279
1280 valueflag, okay := common.ValueType_Type_value[string(options.Args.Valueflag)]
1281 if !okay {
1282 Error.Printf("Unknown valueflag %s\n", options.Args.Valueflag)
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -08001283 }
1284
Scott Baker9173ed82020-05-19 08:30:12 -07001285 val := voltha.ValueSpecifier{Id: string(options.Args.Id), Value: common.ValueType_Type(valueflag)}
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -08001286
1287 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
1288 defer cancel()
1289
Scott Baker9173ed82020-05-19 08:30:12 -07001290 rv, err := client.GetExtValue(ctx, &val)
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -08001291 if err != nil {
1292 Error.Printf("Error getting value on device Id %s,err=%s\n", options.Args.Id, ErrorToString(err))
1293 return err
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -08001294 }
1295
Scott Baker9173ed82020-05-19 08:30:12 -07001296 var rows []ReturnValueRow
1297 for name, num := range common.ValueType_Type_value {
1298 if num == 0 {
1299 // EMPTY is not a real value
1300 continue
1301 }
1302 if (rv.Error & uint32(num)) != 0 {
1303 row := ReturnValueRow{Name: name, Result: "Error"}
1304 rows = append(rows, row)
1305 }
1306 if (rv.Unsupported & uint32(num)) != 0 {
1307 row := ReturnValueRow{Name: name, Result: "Unsupported"}
1308 rows = append(rows, row)
1309 }
1310 if (rv.Set & uint32(num)) != 0 {
1311 switch name {
1312 case "DISTANCE":
1313 row := ReturnValueRow{Name: name, Result: rv.Distance}
1314 rows = append(rows, row)
1315 default:
1316 row := ReturnValueRow{Name: name, Result: "Unimplemented-in-voltctl"}
1317 rows = append(rows, row)
1318 }
1319 }
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -08001320 }
1321
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -08001322 outputFormat := CharReplacer.Replace(options.Format)
1323 if outputFormat == "" {
1324 outputFormat = GetCommandOptionWithDefault("device-value-get", "format", DEFAULT_DEVICE_VALUE_GET_FORMAT)
1325 }
1326
1327 result := CommandResult{
1328 Format: format.Format(outputFormat),
1329 OutputAs: toOutputType(options.OutputAs),
1330 NameLimit: options.NameLimit,
Scott Baker9173ed82020-05-19 08:30:12 -07001331 Data: rows,
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -08001332 }
1333 GenerateOutput(&result)
1334 return nil
1335}