blob: 30689027a425ae4e175dfa25edf492849add6a0c [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 Bainbridge835dd0e2020-04-01 10:30:09 -070055 DeviceType string `short:"t" long:"devicetype" default:"" description:"Device type"`
56 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}
Zack Williamse940c7a2019-08-21 14:25:39 -0700199type DeviceOpts struct {
200 List DeviceList `command:"list"`
201 Create DeviceCreate `command:"create"`
202 Delete DeviceDelete `command:"delete"`
203 Enable DeviceEnable `command:"enable"`
204 Disable DeviceDisable `command:"disable"`
205 Flows DeviceFlowList `command:"flows"`
kesavand12cd8eb2020-01-20 22:25:22 -0500206 Port struct {
207 List DevicePortList `command:"list"`
208 Enable DevicePortEnable `command:"enable"`
209 Disable DevicePortDisable `command:"disable"`
210 } `command:"port"`
211 Inspect DeviceInspect `command:"inspect"`
212 Reboot DeviceReboot `command:"reboot"`
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -0800213 Value struct {
214 Get DeviceGetExtValue `command:"get"`
215 } `command:"value"`
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000216 PmConfig struct {
217 Get DevicePmConfigsGet `command:"get"`
218 Frequency struct {
219 Set DevicePmConfigFrequencySet `command:"set"`
220 } `command:"frequency"`
221 Metric struct {
222 List DevicePmConfigMetricList `command:"list"`
223 Enable DevicePmConfigMetricEnable `command:"enable"`
224 Disable DevicePmConfigMetricDisable `command:"disable"`
225 } `command:"metric"`
226 Group struct {
227 List DevicePmConfigGroupList `command:"list"`
228 Enable DevicePmConfigGroupEnable `command:"enable"`
229 Disable DevicePmConfigGroupDisable `command:"disable"`
230 } `command:"group"`
231 GroupMetric struct {
232 List DevicePmConfigGroupMetricList `command:"list"`
233 } `command:"groupmetric"`
234 } `command:"pmconfig"`
Zack Williamse940c7a2019-08-21 14:25:39 -0700235}
236
237var deviceOpts = DeviceOpts{}
238
239func RegisterDeviceCommands(parser *flags.Parser) {
David Bainbridge12f036f2019-10-15 22:09:04 +0000240 if _, err := parser.AddCommand("device", "device commands", "Commands to query and manipulate VOLTHA devices", &deviceOpts); err != nil {
David Bainbridgea6722342019-10-24 23:55:53 +0000241 Error.Fatalf("Unexpected error while attempting to register device commands : %s", err)
David Bainbridge12f036f2019-10-15 22:09:04 +0000242 }
Zack Williamse940c7a2019-08-21 14:25:39 -0700243}
244
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000245func (i *MetricName) Complete(match string) []flags.Completion {
246 conn, err := NewConnection()
247 if err != nil {
248 return nil
249 }
250 defer conn.Close()
251
252 client := voltha.NewVolthaServiceClient(conn)
253
254 var deviceId string
255found:
256 for i := len(os.Args) - 1; i >= 0; i -= 1 {
257 switch os.Args[i] {
258 case "enable":
259 fallthrough
260 case "disable":
261 if len(os.Args) > i+1 {
262 deviceId = os.Args[i+1]
263 } else {
264 return nil
265 }
266 break found
267 default:
268 }
269 }
270
271 if len(deviceId) == 0 {
272 return nil
273 }
274
275 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
276 defer cancel()
277
278 id := voltha.ID{Id: string(deviceId)}
279
280 pmconfigs, err := client.ListDevicePmConfigs(ctx, &id)
281
282 if err != nil {
283 return nil
284 }
285
286 list := make([]flags.Completion, 0)
287 for _, metrics := range pmconfigs.Metrics {
288 if strings.HasPrefix(metrics.Name, match) {
289 list = append(list, flags.Completion{Item: metrics.Name})
290 }
291 }
292
293 return list
294}
295
296func (i *GroupName) Complete(match string) []flags.Completion {
297 conn, err := NewConnection()
298 if err != nil {
299 return nil
300 }
301 defer conn.Close()
302
303 client := voltha.NewVolthaServiceClient(conn)
304
305 var deviceId string
306found:
307 for i := len(os.Args) - 1; i >= 0; i -= 1 {
308 switch os.Args[i] {
309 case "list":
310 fallthrough
311 case "enable":
312 fallthrough
313 case "disable":
314 if len(os.Args) > i+1 {
315 deviceId = os.Args[i+1]
316 } else {
317 return nil
318 }
319 break found
320 default:
321 }
322 }
323
324 if len(deviceId) == 0 {
325 return nil
326 }
327
328 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
329 defer cancel()
330
331 id := voltha.ID{Id: string(deviceId)}
332
333 pmconfigs, err := client.ListDevicePmConfigs(ctx, &id)
334
335 if err != nil {
336 return nil
337 }
338
339 list := make([]flags.Completion, 0)
340 for _, group := range pmconfigs.Groups {
341 if strings.HasPrefix(group.GroupName, match) {
342 list = append(list, flags.Completion{Item: group.GroupName})
343 }
344 }
345 return list
346}
347
kesavand12cd8eb2020-01-20 22:25:22 -0500348func (i *PortNum) Complete(match string) []flags.Completion {
349 conn, err := NewConnection()
350 if err != nil {
351 return nil
352 }
353 defer conn.Close()
354
Scott Baker9173ed82020-05-19 08:30:12 -0700355 client := voltha.NewVolthaServiceClient(conn)
kesavand12cd8eb2020-01-20 22:25:22 -0500356
357 /*
358 * The command line args when completing for PortNum will be a DeviceId
359 * followed by one or more PortNums. So walk the argument list from the
360 * end and find the first argument that is enable/disable as those are
361 * the subcommands that come before the positional arguments. It would
362 * be nice if this package gave us the list of optional arguments
363 * already parsed.
364 */
365 var deviceId string
366found:
367 for i := len(os.Args) - 1; i >= 0; i -= 1 {
368 switch os.Args[i] {
369 case "enable":
370 fallthrough
371 case "disable":
372 if len(os.Args) > i+1 {
373 deviceId = os.Args[i+1]
374 } else {
375 return nil
376 }
377 break found
378 default:
379 }
380 }
381
382 if len(deviceId) == 0 {
383 return nil
384 }
385
kesavand12cd8eb2020-01-20 22:25:22 -0500386 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
387 defer cancel()
kesavand12cd8eb2020-01-20 22:25:22 -0500388
Scott Baker9173ed82020-05-19 08:30:12 -0700389 id := voltha.ID{Id: string(deviceId)}
kesavand12cd8eb2020-01-20 22:25:22 -0500390
Scott Baker9173ed82020-05-19 08:30:12 -0700391 ports, err := client.ListDevicePorts(ctx, &id)
kesavand12cd8eb2020-01-20 22:25:22 -0500392 if err != nil {
393 return nil
394 }
395
396 list := make([]flags.Completion, 0)
Scott Baker9173ed82020-05-19 08:30:12 -0700397 for _, item := range ports.Items {
398 pn := strconv.FormatUint(uint64(item.PortNo), 10)
kesavand12cd8eb2020-01-20 22:25:22 -0500399 if strings.HasPrefix(pn, match) {
400 list = append(list, flags.Completion{Item: pn})
401 }
402 }
403
404 return list
405}
406
Zack Williamse940c7a2019-08-21 14:25:39 -0700407func (i *DeviceId) Complete(match string) []flags.Completion {
408 conn, err := NewConnection()
409 if err != nil {
410 return nil
411 }
412 defer conn.Close()
413
Scott Baker9173ed82020-05-19 08:30:12 -0700414 client := voltha.NewVolthaServiceClient(conn)
Zack Williamse940c7a2019-08-21 14:25:39 -0700415
416 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
417 defer cancel()
418
Scott Baker9173ed82020-05-19 08:30:12 -0700419 devices, err := client.ListDevices(ctx, &empty.Empty{})
Zack Williamse940c7a2019-08-21 14:25:39 -0700420 if err != nil {
421 return nil
422 }
423
424 list := make([]flags.Completion, 0)
Scott Baker9173ed82020-05-19 08:30:12 -0700425 for _, item := range devices.Items {
426 if strings.HasPrefix(item.Id, match) {
427 list = append(list, flags.Completion{Item: item.Id})
Zack Williamse940c7a2019-08-21 14:25:39 -0700428 }
429 }
430
431 return list
432}
433
434func (options *DeviceList) Execute(args []string) error {
435
436 conn, err := NewConnection()
437 if err != nil {
438 return err
439 }
440 defer conn.Close()
441
Scott Baker9173ed82020-05-19 08:30:12 -0700442 client := voltha.NewVolthaServiceClient(conn)
Zack Williamse940c7a2019-08-21 14:25:39 -0700443
444 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
445 defer cancel()
446
Scott Baker9173ed82020-05-19 08:30:12 -0700447 devices, err := client.ListDevices(ctx, &empty.Empty{})
Zack Williamse940c7a2019-08-21 14:25:39 -0700448 if err != nil {
449 return err
450 }
451
452 outputFormat := CharReplacer.Replace(options.Format)
453 if outputFormat == "" {
David Bainbridgea6722342019-10-24 23:55:53 +0000454 outputFormat = GetCommandOptionWithDefault("device-list", "format", DEFAULT_DEVICE_FORMAT)
Zack Williamse940c7a2019-08-21 14:25:39 -0700455 }
456 if options.Quiet {
457 outputFormat = "{{.Id}}"
458 }
459
David Bainbridgea6722342019-10-24 23:55:53 +0000460 orderBy := options.OrderBy
461 if orderBy == "" {
462 orderBy = GetCommandOptionWithDefault("device-list", "order", "")
463 }
464
Scott Baker9173ed82020-05-19 08:30:12 -0700465 // Make sure json output prints an empty list, not "null"
466 if devices.Items == nil {
467 devices.Items = make([]*voltha.Device, 0)
Zack Williamse940c7a2019-08-21 14:25:39 -0700468 }
469
470 result := CommandResult{
471 Format: format.Format(outputFormat),
472 Filter: options.Filter,
David Bainbridgea6722342019-10-24 23:55:53 +0000473 OrderBy: orderBy,
Zack Williamse940c7a2019-08-21 14:25:39 -0700474 OutputAs: toOutputType(options.OutputAs),
475 NameLimit: options.NameLimit,
Scott Baker9173ed82020-05-19 08:30:12 -0700476 Data: devices.Items,
Zack Williamse940c7a2019-08-21 14:25:39 -0700477 }
478
479 GenerateOutput(&result)
480 return nil
481}
482
483func (options *DeviceCreate) Execute(args []string) error {
484
Scott Baker9173ed82020-05-19 08:30:12 -0700485 device := voltha.Device{}
Zack Williamse940c7a2019-08-21 14:25:39 -0700486 if options.HostAndPort != "" {
Scott Baker9173ed82020-05-19 08:30:12 -0700487 device.Address = &voltha.Device_HostAndPort{HostAndPort: options.HostAndPort}
Zack Williamse940c7a2019-08-21 14:25:39 -0700488 } else if options.IPAddress != "" {
Scott Baker9173ed82020-05-19 08:30:12 -0700489 device.Address = &voltha.Device_Ipv4Address{Ipv4Address: options.IPAddress}
Hardik Windlassce1de342020-02-04 21:58:07 +0000490 }
491 if options.MACAddress != "" {
Scott Baker9173ed82020-05-19 08:30:12 -0700492 device.MacAddress = strings.ToLower(options.MACAddress)
Zack Williamse940c7a2019-08-21 14:25:39 -0700493 }
494 if options.DeviceType != "" {
Scott Baker9173ed82020-05-19 08:30:12 -0700495 device.Type = options.DeviceType
Zack Williamse940c7a2019-08-21 14:25:39 -0700496 }
497
498 conn, err := NewConnection()
499 if err != nil {
500 return err
501 }
502 defer conn.Close()
503
Scott Baker9173ed82020-05-19 08:30:12 -0700504 client := voltha.NewVolthaServiceClient(conn)
Zack Williamse940c7a2019-08-21 14:25:39 -0700505
506 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
507 defer cancel()
508
Scott Baker9173ed82020-05-19 08:30:12 -0700509 createdDevice, err := client.CreateDevice(ctx, &device)
Zack Williamse940c7a2019-08-21 14:25:39 -0700510 if err != nil {
511 return err
Zack Williamse940c7a2019-08-21 14:25:39 -0700512 }
513
Scott Baker9173ed82020-05-19 08:30:12 -0700514 fmt.Printf("%s\n", createdDevice.Id)
Zack Williamse940c7a2019-08-21 14:25:39 -0700515
516 return nil
517}
518
519func (options *DeviceDelete) Execute(args []string) error {
520
521 conn, err := NewConnection()
522 if err != nil {
523 return err
524 }
525 defer conn.Close()
526
Scott Baker9173ed82020-05-19 08:30:12 -0700527 client := voltha.NewVolthaServiceClient(conn)
Zack Williamse940c7a2019-08-21 14:25:39 -0700528
David Bainbridge7052fe82020-03-25 10:37:00 -0700529 var lastErr error
Zack Williamse940c7a2019-08-21 14:25:39 -0700530 for _, i := range options.Args.Ids {
Zack Williamse940c7a2019-08-21 14:25:39 -0700531 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
532 defer cancel()
533
Scott Baker9173ed82020-05-19 08:30:12 -0700534 id := voltha.ID{Id: string(i)}
535
536 _, err := client.DeleteDevice(ctx, &id)
Zack Williamse940c7a2019-08-21 14:25:39 -0700537 if err != nil {
David Bainbridge0f758d42019-10-26 05:17:48 +0000538 Error.Printf("Error while deleting '%s': %s\n", i, err)
David Bainbridge7052fe82020-03-25 10:37:00 -0700539 lastErr = err
Zack Williamse940c7a2019-08-21 14:25:39 -0700540 continue
Zack Williamse940c7a2019-08-21 14:25:39 -0700541 }
542 fmt.Printf("%s\n", i)
543 }
544
David Bainbridge7052fe82020-03-25 10:37:00 -0700545 if lastErr != nil {
546 return NoReportErr
547 }
Zack Williamse940c7a2019-08-21 14:25:39 -0700548 return nil
549}
550
551func (options *DeviceEnable) Execute(args []string) error {
552 conn, err := NewConnection()
553 if err != nil {
554 return err
555 }
556 defer conn.Close()
557
Scott Baker9173ed82020-05-19 08:30:12 -0700558 client := voltha.NewVolthaServiceClient(conn)
Zack Williamse940c7a2019-08-21 14:25:39 -0700559
David Bainbridge7052fe82020-03-25 10:37:00 -0700560 var lastErr error
Zack Williamse940c7a2019-08-21 14:25:39 -0700561 for _, i := range options.Args.Ids {
Zack Williamse940c7a2019-08-21 14:25:39 -0700562 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
563 defer cancel()
564
Scott Baker9173ed82020-05-19 08:30:12 -0700565 id := voltha.ID{Id: string(i)}
566
567 _, err := client.EnableDevice(ctx, &id)
Zack Williamse940c7a2019-08-21 14:25:39 -0700568 if err != nil {
David Bainbridge0f758d42019-10-26 05:17:48 +0000569 Error.Printf("Error while enabling '%s': %s\n", i, err)
David Bainbridge7052fe82020-03-25 10:37:00 -0700570 lastErr = err
Zack Williamse940c7a2019-08-21 14:25:39 -0700571 continue
Zack Williamse940c7a2019-08-21 14:25:39 -0700572 }
573 fmt.Printf("%s\n", i)
574 }
575
David Bainbridge7052fe82020-03-25 10:37:00 -0700576 if lastErr != nil {
577 return NoReportErr
578 }
Zack Williamse940c7a2019-08-21 14:25:39 -0700579 return nil
580}
581
582func (options *DeviceDisable) Execute(args []string) error {
583 conn, err := NewConnection()
584 if err != nil {
585 return err
586 }
587 defer conn.Close()
588
Scott Baker9173ed82020-05-19 08:30:12 -0700589 client := voltha.NewVolthaServiceClient(conn)
Zack Williamse940c7a2019-08-21 14:25:39 -0700590
David Bainbridge7052fe82020-03-25 10:37:00 -0700591 var lastErr error
Zack Williamse940c7a2019-08-21 14:25:39 -0700592 for _, i := range options.Args.Ids {
Zack Williamse940c7a2019-08-21 14:25:39 -0700593 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
594 defer cancel()
595
Scott Baker9173ed82020-05-19 08:30:12 -0700596 id := voltha.ID{Id: string(i)}
597
598 _, err := client.DisableDevice(ctx, &id)
Zack Williamse940c7a2019-08-21 14:25:39 -0700599 if err != nil {
David Bainbridge0f758d42019-10-26 05:17:48 +0000600 Error.Printf("Error while disabling '%s': %s\n", i, err)
David Bainbridge7052fe82020-03-25 10:37:00 -0700601 lastErr = err
Zack Williamse940c7a2019-08-21 14:25:39 -0700602 continue
Zack Williamse940c7a2019-08-21 14:25:39 -0700603 }
604 fmt.Printf("%s\n", i)
605 }
606
David Bainbridge7052fe82020-03-25 10:37:00 -0700607 if lastErr != nil {
608 return NoReportErr
609 }
Zack Williamse940c7a2019-08-21 14:25:39 -0700610 return nil
611}
612
613func (options *DeviceReboot) Execute(args []string) error {
614 conn, err := NewConnection()
615 if err != nil {
616 return err
617 }
618 defer conn.Close()
619
Scott Baker9173ed82020-05-19 08:30:12 -0700620 client := voltha.NewVolthaServiceClient(conn)
Zack Williamse940c7a2019-08-21 14:25:39 -0700621
David Bainbridge7052fe82020-03-25 10:37:00 -0700622 var lastErr error
Zack Williamse940c7a2019-08-21 14:25:39 -0700623 for _, i := range options.Args.Ids {
Zack Williamse940c7a2019-08-21 14:25:39 -0700624 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
625 defer cancel()
626
Scott Baker9173ed82020-05-19 08:30:12 -0700627 id := voltha.ID{Id: string(i)}
628
629 _, err := client.RebootDevice(ctx, &id)
Zack Williamse940c7a2019-08-21 14:25:39 -0700630 if err != nil {
David Bainbridge0f758d42019-10-26 05:17:48 +0000631 Error.Printf("Error while rebooting '%s': %s\n", i, err)
David Bainbridge7052fe82020-03-25 10:37:00 -0700632 lastErr = err
Zack Williamse940c7a2019-08-21 14:25:39 -0700633 continue
Zack Williamse940c7a2019-08-21 14:25:39 -0700634 }
635 fmt.Printf("%s\n", i)
636 }
637
David Bainbridge7052fe82020-03-25 10:37:00 -0700638 if lastErr != nil {
639 return NoReportErr
640 }
Zack Williamse940c7a2019-08-21 14:25:39 -0700641 return nil
642}
643
644func (options *DevicePortList) Execute(args []string) error {
645
646 conn, err := NewConnection()
647 if err != nil {
648 return err
649 }
650 defer conn.Close()
651
Scott Baker9173ed82020-05-19 08:30:12 -0700652 client := voltha.NewVolthaServiceClient(conn)
Zack Williamse940c7a2019-08-21 14:25:39 -0700653
654 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
655 defer cancel()
656
Scott Baker9173ed82020-05-19 08:30:12 -0700657 id := voltha.ID{Id: string(options.Args.Id)}
Zack Williamse940c7a2019-08-21 14:25:39 -0700658
Scott Baker9173ed82020-05-19 08:30:12 -0700659 ports, err := client.ListDevicePorts(ctx, &id)
Zack Williamse940c7a2019-08-21 14:25:39 -0700660 if err != nil {
661 return err
662 }
663
664 outputFormat := CharReplacer.Replace(options.Format)
665 if outputFormat == "" {
David Bainbridgea6722342019-10-24 23:55:53 +0000666 outputFormat = GetCommandOptionWithDefault("device-ports", "format", DEFAULT_DEVICE_PORTS_FORMAT)
Zack Williamse940c7a2019-08-21 14:25:39 -0700667 }
Zack Williamse940c7a2019-08-21 14:25:39 -0700668
David Bainbridgea6722342019-10-24 23:55:53 +0000669 orderBy := options.OrderBy
670 if orderBy == "" {
671 orderBy = GetCommandOptionWithDefault("device-ports", "order", "")
672 }
673
Zack Williamse940c7a2019-08-21 14:25:39 -0700674 result := CommandResult{
675 Format: format.Format(outputFormat),
676 Filter: options.Filter,
David Bainbridgea6722342019-10-24 23:55:53 +0000677 OrderBy: orderBy,
Zack Williamse940c7a2019-08-21 14:25:39 -0700678 OutputAs: toOutputType(options.OutputAs),
679 NameLimit: options.NameLimit,
Scott Baker9173ed82020-05-19 08:30:12 -0700680 Data: ports.Items,
Zack Williamse940c7a2019-08-21 14:25:39 -0700681 }
682
683 GenerateOutput(&result)
684 return nil
685}
686
687func (options *DeviceFlowList) Execute(args []string) error {
688 fl := &FlowList{}
689 fl.ListOutputOptions = options.ListOutputOptions
690 fl.Args.Id = string(options.Args.Id)
David Bainbridgea6722342019-10-24 23:55:53 +0000691 fl.Method = "device-flows"
Zack Williamse940c7a2019-08-21 14:25:39 -0700692 return fl.Execute(args)
693}
694
695func (options *DeviceInspect) Execute(args []string) error {
696 if len(args) > 0 {
697 return fmt.Errorf("only a single argument 'DEVICE_ID' can be provided")
698 }
699
700 conn, err := NewConnection()
701 if err != nil {
702 return err
703 }
704 defer conn.Close()
705
Scott Baker9173ed82020-05-19 08:30:12 -0700706 client := voltha.NewVolthaServiceClient(conn)
Zack Williamse940c7a2019-08-21 14:25:39 -0700707
708 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
709 defer cancel()
710
Scott Baker9173ed82020-05-19 08:30:12 -0700711 id := voltha.ID{Id: string(options.Args.Id)}
Zack Williamse940c7a2019-08-21 14:25:39 -0700712
Scott Baker9173ed82020-05-19 08:30:12 -0700713 device, err := client.GetDevice(ctx, &id)
Zack Williamse940c7a2019-08-21 14:25:39 -0700714 if err != nil {
715 return err
716 }
717
Zack Williamse940c7a2019-08-21 14:25:39 -0700718 outputFormat := CharReplacer.Replace(options.Format)
719 if outputFormat == "" {
David Bainbridgea6722342019-10-24 23:55:53 +0000720 outputFormat = GetCommandOptionWithDefault("device-inspect", "format", DEFAULT_DEVICE_INSPECT_FORMAT)
Zack Williamse940c7a2019-08-21 14:25:39 -0700721 }
722 if options.Quiet {
723 outputFormat = "{{.Id}}"
724 }
725
726 result := CommandResult{
727 Format: format.Format(outputFormat),
728 OutputAs: toOutputType(options.OutputAs),
729 NameLimit: options.NameLimit,
730 Data: device,
731 }
732 GenerateOutput(&result)
733 return nil
734}
kesavand12cd8eb2020-01-20 22:25:22 -0500735
736/*Device Port Enable */
737func (options *DevicePortEnable) Execute(args []string) error {
738 conn, err := NewConnection()
739 if err != nil {
740 return err
741 }
742 defer conn.Close()
743
Scott Baker9173ed82020-05-19 08:30:12 -0700744 client := voltha.NewVolthaServiceClient(conn)
kesavand12cd8eb2020-01-20 22:25:22 -0500745
kesavand12cd8eb2020-01-20 22:25:22 -0500746 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
747 defer cancel()
748
Scott Baker9173ed82020-05-19 08:30:12 -0700749 port := voltha.Port{DeviceId: string(options.Args.Id), PortNo: uint32(options.Args.PortId)}
750
751 _, err = client.EnablePort(ctx, &port)
kesavand12cd8eb2020-01-20 22:25:22 -0500752 if err != nil {
753 Error.Printf("Error enabling port number %v on device Id %s,err=%s\n", options.Args.PortId, options.Args.Id, ErrorToString(err))
754 return err
kesavand12cd8eb2020-01-20 22:25:22 -0500755 }
756
757 return nil
758}
759
Scott Baker9173ed82020-05-19 08:30:12 -0700760/*Device Port Disable */
kesavand12cd8eb2020-01-20 22:25:22 -0500761func (options *DevicePortDisable) Execute(args []string) error {
762 conn, err := NewConnection()
763 if err != nil {
764 return err
765 }
766 defer conn.Close()
767
Scott Baker9173ed82020-05-19 08:30:12 -0700768 client := voltha.NewVolthaServiceClient(conn)
769
kesavand12cd8eb2020-01-20 22:25:22 -0500770 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
771 defer cancel()
772
Scott Baker9173ed82020-05-19 08:30:12 -0700773 port := voltha.Port{DeviceId: string(options.Args.Id), PortNo: uint32(options.Args.PortId)}
774
775 _, err = client.DisablePort(ctx, &port)
kesavand12cd8eb2020-01-20 22:25:22 -0500776 if err != nil {
Scott Baker9173ed82020-05-19 08:30:12 -0700777 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 -0500778 return err
kesavand12cd8eb2020-01-20 22:25:22 -0500779 }
Scott Baker9173ed82020-05-19 08:30:12 -0700780
kesavand12cd8eb2020-01-20 22:25:22 -0500781 return nil
782}
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -0800783
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000784func (options *DevicePmConfigsGet) Execute(args []string) error {
785
786 conn, err := NewConnection()
787 if err != nil {
788 return err
789 }
790 defer conn.Close()
791
792 client := voltha.NewVolthaServiceClient(conn)
793
794 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
795 defer cancel()
796
797 id := voltha.ID{Id: string(options.Args.Id)}
798
799 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
800 if err != nil {
801 return err
802 }
803
804 outputFormat := CharReplacer.Replace(options.Format)
805 if outputFormat == "" {
806 outputFormat = GetCommandOptionWithDefault("device-pm-configs", "format", DEFAULT_DEVICE_PM_CONFIG_GET_FORMAT)
807 }
808
809 orderBy := options.OrderBy
810 if orderBy == "" {
811 orderBy = GetCommandOptionWithDefault("device-pm-configs", "order", "")
812 }
813
814 result := CommandResult{
815 Format: format.Format(outputFormat),
816 Filter: options.Filter,
817 OrderBy: orderBy,
818 OutputAs: toOutputType(options.OutputAs),
819 NameLimit: options.NameLimit,
820 Data: pmConfigs,
821 }
822
823 GenerateOutput(&result)
824 return nil
825
826}
827
828func (options *DevicePmConfigMetricList) Execute(args []string) error {
829
830 conn, err := NewConnection()
831 if err != nil {
832 return err
833 }
834 defer conn.Close()
835
836 client := voltha.NewVolthaServiceClient(conn)
837
838 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
839 defer cancel()
840
841 id := voltha.ID{Id: string(options.Args.Id)}
842
843 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
844 if err != nil {
845 return err
846 }
847
848 if !pmConfigs.Grouped {
849 for _, metric := range pmConfigs.Metrics {
850 if metric.SampleFreq == 0 {
851 metric.SampleFreq = pmConfigs.DefaultFreq
852 }
853 }
854
855 }
856
857 outputFormat := CharReplacer.Replace(options.Format)
858 if outputFormat == "" {
859 outputFormat = GetCommandOptionWithDefault("device-pm-configs", "format", DEFAULT_DEVICE_PM_CONFIG_METRIC_LIST_FORMAT)
860 }
861
862 orderBy := options.OrderBy
863 if orderBy == "" {
864 orderBy = GetCommandOptionWithDefault("device-pm-configs", "order", "")
865 }
866
867 result := CommandResult{
868 Format: format.Format(outputFormat),
869 Filter: options.Filter,
870 OrderBy: orderBy,
871 OutputAs: toOutputType(options.OutputAs),
872 NameLimit: options.NameLimit,
873 Data: pmConfigs.Metrics,
874 }
875
876 GenerateOutput(&result)
877 return nil
878
879}
880
881func (options *DevicePmConfigMetricEnable) Execute(args []string) error {
882
883 conn, err := NewConnection()
884 if err != nil {
885 return err
886 }
887 defer conn.Close()
888
889 client := voltha.NewVolthaServiceClient(conn)
890
891 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
892 defer cancel()
893
894 id := voltha.ID{Id: string(options.Args.Id)}
895
896 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
897 if err != nil {
898 return err
899 }
900
901 if !pmConfigs.Grouped {
902 for _, metric := range pmConfigs.Metrics {
903 for _, mName := range options.Args.Metrics {
904 if string(mName) == metric.Name && !metric.Enabled {
905 metric.Enabled = true
906 _, err := client.UpdateDevicePmConfigs(ctx, pmConfigs)
907 if err != nil {
908 return err
909 }
910 }
911 }
912 }
913 }
914 return nil
915}
916
917func (options *DevicePmConfigMetricDisable) Execute(args []string) error {
918
919 conn, err := NewConnection()
920 if err != nil {
921 return err
922 }
923 defer conn.Close()
924
925 client := voltha.NewVolthaServiceClient(conn)
926
927 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
928 defer cancel()
929
930 id := voltha.ID{Id: string(options.Args.Id)}
931
932 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
933 if err != nil {
934 return err
935 }
936
937 if !pmConfigs.Grouped {
938 for _, metric := range pmConfigs.Metrics {
939 for _, mName := range options.Args.Metrics {
940 if string(mName) == metric.Name && metric.Enabled {
941 metric.Enabled = false
942 _, err := client.UpdateDevicePmConfigs(ctx, pmConfigs)
943 if err != nil {
944 return err
945 }
946 }
947 }
948 }
949 }
950 return nil
951}
952
953func (options *DevicePmConfigGroupEnable) Execute(args []string) error {
954
955 conn, err := NewConnection()
956 if err != nil {
957 return err
958 }
959 defer conn.Close()
960
961 client := voltha.NewVolthaServiceClient(conn)
962
963 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
964 defer cancel()
965
966 id := voltha.ID{Id: string(options.Args.Id)}
967
968 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
969 if err != nil {
970 return err
971 }
972
973 if pmConfigs.Grouped {
974 for _, group := range pmConfigs.Groups {
975 for _, gName := range options.Args.Groups {
976 if string(gName) == group.GroupName && !group.Enabled {
977 group.Enabled = true
978 _, err := client.UpdateDevicePmConfigs(ctx, pmConfigs)
979 if err != nil {
980 return err
981 }
982 }
983 }
984 }
985 }
986 return nil
987}
988
989func (options *DevicePmConfigGroupDisable) Execute(args []string) error {
990
991 conn, err := NewConnection()
992 if err != nil {
993 return err
994 }
995 defer conn.Close()
996
997 client := voltha.NewVolthaServiceClient(conn)
998
999 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
1000 defer cancel()
1001
1002 id := voltha.ID{Id: string(options.Args.Id)}
1003
1004 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
1005 if err != nil {
1006 return err
1007 }
1008
1009 if pmConfigs.Grouped {
1010 for _, group := range pmConfigs.Groups {
1011 for _, gName := range options.Args.Groups {
1012 if string(gName) == group.GroupName && group.Enabled {
1013 group.Enabled = false
1014 _, err := client.UpdateDevicePmConfigs(ctx, pmConfigs)
1015 if err != nil {
1016 return err
1017 }
1018 }
1019 }
1020 }
1021 }
1022 return nil
1023}
1024
1025func (options *DevicePmConfigGroupList) Execute(args []string) error {
1026
1027 conn, err := NewConnection()
1028 if err != nil {
1029 return err
1030 }
1031 defer conn.Close()
1032
1033 client := voltha.NewVolthaServiceClient(conn)
1034
1035 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
1036 defer cancel()
1037
1038 id := voltha.ID{Id: string(options.Args.Id)}
1039
1040 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
1041 if err != nil {
1042 return err
1043 }
1044
1045 if pmConfigs.Grouped {
1046 for _, group := range pmConfigs.Groups {
1047 if group.GroupFreq == 0 {
1048 group.GroupFreq = pmConfigs.DefaultFreq
1049 }
1050 }
1051
1052 }
1053
1054 outputFormat := CharReplacer.Replace(options.Format)
1055 if outputFormat == "" {
1056 outputFormat = GetCommandOptionWithDefault("device-pm-configs", "format", DEFAULT_DEVICE_PM_CONFIG_GROUP_LIST_FORMAT)
1057 }
1058
1059 orderBy := options.OrderBy
1060 if orderBy == "" {
1061 orderBy = GetCommandOptionWithDefault("device-pm-configs", "order", "")
1062 }
1063
1064 result := CommandResult{
1065 Format: format.Format(outputFormat),
1066 Filter: options.Filter,
1067 OrderBy: orderBy,
1068 OutputAs: toOutputType(options.OutputAs),
1069 NameLimit: options.NameLimit,
1070 Data: pmConfigs.Groups,
1071 }
1072
1073 GenerateOutput(&result)
1074 return nil
1075
1076}
1077
1078func (options *DevicePmConfigGroupMetricList) Execute(args []string) error {
1079
1080 var metrics []*voltha.PmConfig
1081 conn, err := NewConnection()
1082 if err != nil {
1083 return err
1084 }
1085 defer conn.Close()
1086
1087 client := voltha.NewVolthaServiceClient(conn)
1088
1089 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
1090 defer cancel()
1091
1092 id := voltha.ID{Id: string(options.Args.Id)}
1093
1094 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
1095 if err != nil {
1096 return err
1097 }
1098
1099 for _, groups := range pmConfigs.Groups {
1100
1101 if string(options.Args.Group) == groups.GroupName {
1102 for _, metric := range groups.Metrics {
1103 if metric.SampleFreq == 0 && groups.GroupFreq == 0 {
1104 metric.SampleFreq = pmConfigs.DefaultFreq
1105 } else {
1106 metric.SampleFreq = groups.GroupFreq
1107 }
1108 }
1109 metrics = groups.Metrics
1110 }
1111 }
1112
1113 outputFormat := CharReplacer.Replace(options.Format)
1114 if outputFormat == "" {
1115 outputFormat = GetCommandOptionWithDefault("device-pm-configs", "format", DEFAULT_DEVICE_PM_CONFIG_METRIC_LIST_FORMAT)
1116 }
1117
1118 orderBy := options.OrderBy
1119 if orderBy == "" {
1120 orderBy = GetCommandOptionWithDefault("device-pm-configs", "order", "")
1121 }
1122
1123 result := CommandResult{
1124 Format: format.Format(outputFormat),
1125 Filter: options.Filter,
1126 OrderBy: orderBy,
1127 OutputAs: toOutputType(options.OutputAs),
1128 NameLimit: options.NameLimit,
1129 Data: metrics,
1130 }
1131
1132 GenerateOutput(&result)
1133 return nil
1134
1135}
1136
1137func (options *DevicePmConfigFrequencySet) Execute(args []string) error {
1138
1139 conn, err := NewConnection()
1140 if err != nil {
1141 return err
1142 }
1143 defer conn.Close()
1144
1145 client := voltha.NewVolthaServiceClient(conn)
1146
1147 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
1148 defer cancel()
1149
1150 id := voltha.ID{Id: string(options.Args.Id)}
1151
1152 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
1153 if err != nil {
1154 return err
1155 }
1156
1157 pmConfigs.DefaultFreq = options.Args.Frequency
1158
1159 _, err = client.UpdateDevicePmConfigs(ctx, pmConfigs)
1160 if err != nil {
1161 return err
1162 }
1163
1164 outputFormat := CharReplacer.Replace(options.Format)
1165 if outputFormat == "" {
1166 outputFormat = GetCommandOptionWithDefault("device-pm-configs", "format", DEFAULT_DEVICE_PM_CONFIG_GET_FORMAT)
1167 }
1168 if options.Quiet {
1169 outputFormat = "{{.Id}}"
1170 }
1171
1172 result := CommandResult{
1173 Format: format.Format(outputFormat),
1174 OutputAs: toOutputType(options.OutputAs),
1175 NameLimit: options.NameLimit,
1176 Data: pmConfigs,
1177 }
1178
1179 GenerateOutput(&result)
1180 return nil
1181
1182}
1183
Scott Baker9173ed82020-05-19 08:30:12 -07001184type ReturnValueRow struct {
1185 Name string `json:"name"`
1186 Result interface{} `json:"result"`
1187}
1188
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -08001189/*Device get Onu Distance */
1190func (options *DeviceGetExtValue) Execute(args []string) error {
1191 conn, err := NewConnection()
1192 if err != nil {
1193 return err
1194 }
1195 defer conn.Close()
1196
Scott Baker9173ed82020-05-19 08:30:12 -07001197 client := voltha.NewVolthaServiceClient(conn)
1198
1199 valueflag, okay := common.ValueType_Type_value[string(options.Args.Valueflag)]
1200 if !okay {
1201 Error.Printf("Unknown valueflag %s\n", options.Args.Valueflag)
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -08001202 }
1203
Scott Baker9173ed82020-05-19 08:30:12 -07001204 val := voltha.ValueSpecifier{Id: string(options.Args.Id), Value: common.ValueType_Type(valueflag)}
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -08001205
1206 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
1207 defer cancel()
1208
Scott Baker9173ed82020-05-19 08:30:12 -07001209 rv, err := client.GetExtValue(ctx, &val)
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -08001210 if err != nil {
1211 Error.Printf("Error getting value on device Id %s,err=%s\n", options.Args.Id, ErrorToString(err))
1212 return err
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -08001213 }
1214
Scott Baker9173ed82020-05-19 08:30:12 -07001215 var rows []ReturnValueRow
1216 for name, num := range common.ValueType_Type_value {
1217 if num == 0 {
1218 // EMPTY is not a real value
1219 continue
1220 }
1221 if (rv.Error & uint32(num)) != 0 {
1222 row := ReturnValueRow{Name: name, Result: "Error"}
1223 rows = append(rows, row)
1224 }
1225 if (rv.Unsupported & uint32(num)) != 0 {
1226 row := ReturnValueRow{Name: name, Result: "Unsupported"}
1227 rows = append(rows, row)
1228 }
1229 if (rv.Set & uint32(num)) != 0 {
1230 switch name {
1231 case "DISTANCE":
1232 row := ReturnValueRow{Name: name, Result: rv.Distance}
1233 rows = append(rows, row)
1234 default:
1235 row := ReturnValueRow{Name: name, Result: "Unimplemented-in-voltctl"}
1236 rows = append(rows, row)
1237 }
1238 }
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -08001239 }
1240
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -08001241 outputFormat := CharReplacer.Replace(options.Format)
1242 if outputFormat == "" {
1243 outputFormat = GetCommandOptionWithDefault("device-value-get", "format", DEFAULT_DEVICE_VALUE_GET_FORMAT)
1244 }
1245
1246 result := CommandResult{
1247 Format: format.Format(outputFormat),
1248 OutputAs: toOutputType(options.OutputAs),
1249 NameLimit: options.NameLimit,
Scott Baker9173ed82020-05-19 08:30:12 -07001250 Data: rows,
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -08001251 }
1252 GenerateOutput(&result)
1253 return nil
1254}