blob: 4d93acff6369a4410c3fc0132867012e18b8c2c9 [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}
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 }
Rohan Agrawalbca69122020-06-17 14:59:03 +0000854 outputFormat := CharReplacer.Replace(options.Format)
855 if outputFormat == "" {
856 outputFormat = GetCommandOptionWithDefault("device-pm-configs", "format", DEFAULT_DEVICE_PM_CONFIG_METRIC_LIST_FORMAT)
857 }
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000858
Rohan Agrawalbca69122020-06-17 14:59:03 +0000859 orderBy := options.OrderBy
860 if orderBy == "" {
861 orderBy = GetCommandOptionWithDefault("device-pm-configs", "order", "")
862 }
863
864 result := CommandResult{
865 Format: format.Format(outputFormat),
866 Filter: options.Filter,
867 OrderBy: orderBy,
868 OutputAs: toOutputType(options.OutputAs),
869 NameLimit: options.NameLimit,
870 Data: pmConfigs.Metrics,
871 }
872
873 GenerateOutput(&result)
874 return nil
875 } else {
876 return fmt.Errorf("Device '%s' does not have Non Grouped Metrics", options.Args.Id)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000877 }
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000878}
879
880func (options *DevicePmConfigMetricEnable) Execute(args []string) error {
881
882 conn, err := NewConnection()
883 if err != nil {
884 return err
885 }
886 defer conn.Close()
887
888 client := voltha.NewVolthaServiceClient(conn)
889
890 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
891 defer cancel()
892
893 id := voltha.ID{Id: string(options.Args.Id)}
894
895 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
896 if err != nil {
897 return err
898 }
899
900 if !pmConfigs.Grouped {
Rohan Agrawalbca69122020-06-17 14:59:03 +0000901 metrics := make(map[string]struct{})
902 for _, metric := range pmConfigs.Metrics {
903 metrics[metric.Name] = struct{}{}
904 }
905
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000906 for _, metric := range pmConfigs.Metrics {
907 for _, mName := range options.Args.Metrics {
Rohan Agrawalbca69122020-06-17 14:59:03 +0000908 if _, exist := metrics[string(mName)]; !exist {
909 return fmt.Errorf("Metric Name '%s' does not exist", mName)
910 }
911
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000912 if string(mName) == metric.Name && !metric.Enabled {
913 metric.Enabled = true
914 _, err := client.UpdateDevicePmConfigs(ctx, pmConfigs)
915 if err != nil {
916 return err
917 }
918 }
919 }
920 }
Rohan Agrawalbca69122020-06-17 14:59:03 +0000921 } else {
922 return fmt.Errorf("Device '%s' does not have Non Grouped Metrics", options.Args.Id)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000923 }
924 return nil
925}
926
927func (options *DevicePmConfigMetricDisable) Execute(args []string) error {
928
929 conn, err := NewConnection()
930 if err != nil {
931 return err
932 }
933 defer conn.Close()
934
935 client := voltha.NewVolthaServiceClient(conn)
936
937 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
938 defer cancel()
939
940 id := voltha.ID{Id: string(options.Args.Id)}
941
942 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
943 if err != nil {
944 return err
945 }
946
947 if !pmConfigs.Grouped {
Rohan Agrawalbca69122020-06-17 14:59:03 +0000948 metrics := make(map[string]struct{})
949 for _, metric := range pmConfigs.Metrics {
950 metrics[metric.Name] = struct{}{}
951 }
952
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000953 for _, metric := range pmConfigs.Metrics {
954 for _, mName := range options.Args.Metrics {
Rohan Agrawalbca69122020-06-17 14:59:03 +0000955 if _, have := metrics[string(mName)]; !have {
956 return fmt.Errorf("Metric Name '%s' does not exist", mName)
957 }
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000958 if string(mName) == metric.Name && metric.Enabled {
959 metric.Enabled = false
960 _, err := client.UpdateDevicePmConfigs(ctx, pmConfigs)
961 if err != nil {
962 return err
963 }
Rohan Agrawalbca69122020-06-17 14:59:03 +0000964 } else {
965 return fmt.Errorf("Metric '%s' cannot be disabled", string(mName))
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000966 }
967 }
968 }
Rohan Agrawalbca69122020-06-17 14:59:03 +0000969 } else {
970 return fmt.Errorf("Device '%s' does not have Non Grouped Metrics", options.Args.Id)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000971 }
972 return nil
973}
974
975func (options *DevicePmConfigGroupEnable) Execute(args []string) error {
976
977 conn, err := NewConnection()
978 if err != nil {
979 return err
980 }
981 defer conn.Close()
982
983 client := voltha.NewVolthaServiceClient(conn)
984
985 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
986 defer cancel()
987
988 id := voltha.ID{Id: string(options.Args.Id)}
989
990 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
991 if err != nil {
992 return err
993 }
994
995 if pmConfigs.Grouped {
Rohan Agrawalbca69122020-06-17 14:59:03 +0000996 groups := make(map[string]struct{})
997 for _, group := range pmConfigs.Groups {
998 groups[group.GroupName] = struct{}{}
999 }
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001000 for _, group := range pmConfigs.Groups {
1001 for _, gName := range options.Args.Groups {
Rohan Agrawalbca69122020-06-17 14:59:03 +00001002 if _, have := groups[string(gName)]; !have {
1003 return fmt.Errorf("Group Name '%s' does not exist", gName)
1004 }
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001005 if string(gName) == group.GroupName && !group.Enabled {
1006 group.Enabled = true
1007 _, err := client.UpdateDevicePmConfigs(ctx, pmConfigs)
1008 if err != nil {
1009 return err
1010 }
1011 }
1012 }
1013 }
Rohan Agrawalbca69122020-06-17 14:59:03 +00001014 } else {
1015 return fmt.Errorf("Device '%s' does not have Group Metrics", options.Args.Id)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001016 }
1017 return nil
1018}
1019
1020func (options *DevicePmConfigGroupDisable) Execute(args []string) error {
1021
1022 conn, err := NewConnection()
1023 if err != nil {
1024 return err
1025 }
1026 defer conn.Close()
1027
1028 client := voltha.NewVolthaServiceClient(conn)
1029
1030 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
1031 defer cancel()
1032
1033 id := voltha.ID{Id: string(options.Args.Id)}
1034
1035 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
1036 if err != nil {
1037 return err
1038 }
1039
1040 if pmConfigs.Grouped {
Rohan Agrawalbca69122020-06-17 14:59:03 +00001041 groups := make(map[string]struct{})
1042 for _, group := range pmConfigs.Groups {
1043 groups[group.GroupName] = struct{}{}
1044 }
1045
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001046 for _, group := range pmConfigs.Groups {
1047 for _, gName := range options.Args.Groups {
Rohan Agrawalbca69122020-06-17 14:59:03 +00001048 if _, have := groups[string(gName)]; !have {
1049 return fmt.Errorf("Group Name '%s' does not exist", gName)
1050 }
1051
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001052 if string(gName) == group.GroupName && group.Enabled {
1053 group.Enabled = false
1054 _, err := client.UpdateDevicePmConfigs(ctx, pmConfigs)
1055 if err != nil {
1056 return err
1057 }
1058 }
1059 }
1060 }
Rohan Agrawalbca69122020-06-17 14:59:03 +00001061 } else {
1062 return fmt.Errorf("Device '%s' does not have Group Metrics", options.Args.Id)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001063 }
1064 return nil
1065}
1066
1067func (options *DevicePmConfigGroupList) Execute(args []string) error {
1068
1069 conn, err := NewConnection()
1070 if err != nil {
1071 return err
1072 }
1073 defer conn.Close()
1074
1075 client := voltha.NewVolthaServiceClient(conn)
1076
1077 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
1078 defer cancel()
1079
1080 id := voltha.ID{Id: string(options.Args.Id)}
1081
1082 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
1083 if err != nil {
1084 return err
1085 }
1086
1087 if pmConfigs.Grouped {
1088 for _, group := range pmConfigs.Groups {
1089 if group.GroupFreq == 0 {
1090 group.GroupFreq = pmConfigs.DefaultFreq
1091 }
1092 }
Rohan Agrawalbca69122020-06-17 14:59:03 +00001093 outputFormat := CharReplacer.Replace(options.Format)
1094 if outputFormat == "" {
1095 outputFormat = GetCommandOptionWithDefault("device-pm-configs", "format", DEFAULT_DEVICE_PM_CONFIG_GROUP_LIST_FORMAT)
1096 }
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001097
Rohan Agrawalbca69122020-06-17 14:59:03 +00001098 orderBy := options.OrderBy
1099 if orderBy == "" {
1100 orderBy = GetCommandOptionWithDefault("device-pm-configs", "order", "")
1101 }
1102
1103 result := CommandResult{
1104 Format: format.Format(outputFormat),
1105 Filter: options.Filter,
1106 OrderBy: orderBy,
1107 OutputAs: toOutputType(options.OutputAs),
1108 NameLimit: options.NameLimit,
1109 Data: pmConfigs.Groups,
1110 }
1111
1112 GenerateOutput(&result)
1113 } else {
1114 return fmt.Errorf("Device '%s' does not have Group Metrics", string(options.Args.Id))
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001115 }
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001116 return nil
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001117}
1118
1119func (options *DevicePmConfigGroupMetricList) Execute(args []string) error {
1120
1121 var metrics []*voltha.PmConfig
1122 conn, err := NewConnection()
1123 if err != nil {
1124 return err
1125 }
1126 defer conn.Close()
1127
1128 client := voltha.NewVolthaServiceClient(conn)
1129
1130 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
1131 defer cancel()
1132
1133 id := voltha.ID{Id: string(options.Args.Id)}
1134
1135 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
1136 if err != nil {
1137 return err
1138 }
1139
1140 for _, groups := range pmConfigs.Groups {
1141
1142 if string(options.Args.Group) == groups.GroupName {
1143 for _, metric := range groups.Metrics {
1144 if metric.SampleFreq == 0 && groups.GroupFreq == 0 {
1145 metric.SampleFreq = pmConfigs.DefaultFreq
1146 } else {
1147 metric.SampleFreq = groups.GroupFreq
1148 }
1149 }
1150 metrics = groups.Metrics
1151 }
1152 }
1153
1154 outputFormat := CharReplacer.Replace(options.Format)
1155 if outputFormat == "" {
1156 outputFormat = GetCommandOptionWithDefault("device-pm-configs", "format", DEFAULT_DEVICE_PM_CONFIG_METRIC_LIST_FORMAT)
1157 }
1158
1159 orderBy := options.OrderBy
1160 if orderBy == "" {
1161 orderBy = GetCommandOptionWithDefault("device-pm-configs", "order", "")
1162 }
1163
1164 result := CommandResult{
1165 Format: format.Format(outputFormat),
1166 Filter: options.Filter,
1167 OrderBy: orderBy,
1168 OutputAs: toOutputType(options.OutputAs),
1169 NameLimit: options.NameLimit,
1170 Data: metrics,
1171 }
1172
1173 GenerateOutput(&result)
1174 return nil
1175
1176}
1177
1178func (options *DevicePmConfigFrequencySet) Execute(args []string) error {
1179
1180 conn, err := NewConnection()
1181 if err != nil {
1182 return err
1183 }
1184 defer conn.Close()
1185
1186 client := voltha.NewVolthaServiceClient(conn)
1187
1188 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
1189 defer cancel()
1190
1191 id := voltha.ID{Id: string(options.Args.Id)}
1192
1193 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
1194 if err != nil {
1195 return err
1196 }
1197
1198 pmConfigs.DefaultFreq = options.Args.Frequency
1199
1200 _, err = client.UpdateDevicePmConfigs(ctx, pmConfigs)
1201 if err != nil {
1202 return err
1203 }
1204
1205 outputFormat := CharReplacer.Replace(options.Format)
1206 if outputFormat == "" {
1207 outputFormat = GetCommandOptionWithDefault("device-pm-configs", "format", DEFAULT_DEVICE_PM_CONFIG_GET_FORMAT)
1208 }
1209 if options.Quiet {
1210 outputFormat = "{{.Id}}"
1211 }
1212
1213 result := CommandResult{
1214 Format: format.Format(outputFormat),
1215 OutputAs: toOutputType(options.OutputAs),
1216 NameLimit: options.NameLimit,
1217 Data: pmConfigs,
1218 }
1219
1220 GenerateOutput(&result)
1221 return nil
1222
1223}
1224
Scott Baker9173ed82020-05-19 08:30:12 -07001225type ReturnValueRow struct {
1226 Name string `json:"name"`
1227 Result interface{} `json:"result"`
1228}
1229
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -08001230/*Device get Onu Distance */
1231func (options *DeviceGetExtValue) Execute(args []string) error {
1232 conn, err := NewConnection()
1233 if err != nil {
1234 return err
1235 }
1236 defer conn.Close()
1237
Scott Baker9173ed82020-05-19 08:30:12 -07001238 client := voltha.NewVolthaServiceClient(conn)
1239
1240 valueflag, okay := common.ValueType_Type_value[string(options.Args.Valueflag)]
1241 if !okay {
1242 Error.Printf("Unknown valueflag %s\n", options.Args.Valueflag)
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -08001243 }
1244
Scott Baker9173ed82020-05-19 08:30:12 -07001245 val := voltha.ValueSpecifier{Id: string(options.Args.Id), Value: common.ValueType_Type(valueflag)}
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -08001246
1247 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
1248 defer cancel()
1249
Scott Baker9173ed82020-05-19 08:30:12 -07001250 rv, err := client.GetExtValue(ctx, &val)
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -08001251 if err != nil {
1252 Error.Printf("Error getting value on device Id %s,err=%s\n", options.Args.Id, ErrorToString(err))
1253 return err
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -08001254 }
1255
Scott Baker9173ed82020-05-19 08:30:12 -07001256 var rows []ReturnValueRow
1257 for name, num := range common.ValueType_Type_value {
1258 if num == 0 {
1259 // EMPTY is not a real value
1260 continue
1261 }
1262 if (rv.Error & uint32(num)) != 0 {
1263 row := ReturnValueRow{Name: name, Result: "Error"}
1264 rows = append(rows, row)
1265 }
1266 if (rv.Unsupported & uint32(num)) != 0 {
1267 row := ReturnValueRow{Name: name, Result: "Unsupported"}
1268 rows = append(rows, row)
1269 }
1270 if (rv.Set & uint32(num)) != 0 {
1271 switch name {
1272 case "DISTANCE":
1273 row := ReturnValueRow{Name: name, Result: rv.Distance}
1274 rows = append(rows, row)
1275 default:
1276 row := ReturnValueRow{Name: name, Result: "Unimplemented-in-voltctl"}
1277 rows = append(rows, row)
1278 }
1279 }
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -08001280 }
1281
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -08001282 outputFormat := CharReplacer.Replace(options.Format)
1283 if outputFormat == "" {
1284 outputFormat = GetCommandOptionWithDefault("device-value-get", "format", DEFAULT_DEVICE_VALUE_GET_FORMAT)
1285 }
1286
1287 result := CommandResult{
1288 Format: format.Format(outputFormat),
1289 OutputAs: toOutputType(options.OutputAs),
1290 NameLimit: options.NameLimit,
Scott Baker9173ed82020-05-19 08:30:12 -07001291 Data: rows,
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -08001292 }
1293 GenerateOutput(&result)
1294 return nil
1295}