blob: d41b9aca2a2b2e4098bea3b0b77d3a759d748ded [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 {
Himani Chawla9933ddc2020-10-12 23:53:27 +053069 Force bool `long:"force" description:"Delete device forcefully"`
70 Args struct {
Zack Williamse940c7a2019-08-21 14:25:39 -070071 Ids []DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
72 } `positional-args:"yes"`
73}
74
75type DeviceEnable struct {
76 Args struct {
77 Ids []DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
78 } `positional-args:"yes"`
79}
80
81type DeviceDisable struct {
82 Args struct {
83 Ids []DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
84 } `positional-args:"yes"`
85}
86
87type DeviceReboot struct {
88 Args struct {
89 Ids []DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
90 } `positional-args:"yes"`
91}
92
93type DeviceFlowList struct {
94 ListOutputOptions
Maninder045921e2020-09-29 16:46:02 +053095 FlowIdOptions
Zack Williamse940c7a2019-08-21 14:25:39 -070096 Args struct {
97 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
98 } `positional-args:"yes"`
99}
100
101type DevicePortList struct {
102 ListOutputOptions
103 Args struct {
104 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
105 } `positional-args:"yes"`
106}
107
108type DeviceInspect struct {
109 OutputOptionsJson
110 Args struct {
111 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
112 } `positional-args:"yes"`
113}
114
kesavand12cd8eb2020-01-20 22:25:22 -0500115type DevicePortEnable struct {
116 Args struct {
117 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
118 PortId PortNum `positional-arg-name:"PORT_NUMBER" required:"yes"`
119 } `positional-args:"yes"`
120}
121
122type DevicePortDisable struct {
123 Args struct {
124 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
125 PortId PortNum `positional-arg-name:"PORT_NUMBER" required:"yes"`
126 } `positional-args:"yes"`
127}
128
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000129type DevicePmConfigsGet struct {
130 ListOutputOptions
131 Args struct {
132 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
133 } `positional-args:"yes"`
134}
135
136type DevicePmConfigMetricList struct {
137 ListOutputOptions
138 Args struct {
139 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
140 } `positional-args:"yes"`
141}
142
143type DevicePmConfigGroupList struct {
144 ListOutputOptions
145 Args struct {
146 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
147 } `positional-args:"yes"`
148}
149
150type DevicePmConfigGroupMetricList struct {
151 ListOutputOptions
152 Args struct {
153 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
154 Group GroupName `positional-arg-name:"GROUP_NAME" required:"yes"`
155 } `positional-args:"yes"`
156}
157
158type DevicePmConfigFrequencySet struct {
159 OutputOptions
160 Args struct {
161 Frequency uint32 `positional-arg-name:"FREQUENCY" required:"yes"`
162 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
163 } `positional-args:"yes"`
164}
165
166type DevicePmConfigMetricEnable struct {
167 Args struct {
168 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
169 Metrics []MetricName `positional-arg-name:"METRIC_NAME" required:"yes"`
170 } `positional-args:"yes"`
171}
172
173type DevicePmConfigMetricDisable struct {
174 Args struct {
175 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
176 Metrics []MetricName `positional-arg-name:"METRIC_NAME" required:"yes"`
177 } `positional-args:"yes"`
178}
179
180type DevicePmConfigGroupEnable struct {
181 Args struct {
182 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
183 Groups []GroupName `positional-arg-name:"GROUP_NAME" required:"yes"`
184 } `positional-args:"yes"`
185}
186
187type DevicePmConfigGroupDisable struct {
188 Args struct {
189 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
190 Groups []GroupName `positional-arg-name:"GROUP_NAME" required:"yes"`
191 } `positional-args:"yes"`
192}
193
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -0800194type DeviceGetExtValue struct {
195 ListOutputOptions
196 Args struct {
197 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
198 Valueflag ValueFlag `positional-arg-name:"VALUE_FLAG" required:"yes"`
199 } `positional-args:"yes"`
200}
Rohan Agrawald7df3772020-06-29 11:23:36 +0000201
202type DevicePmConfigSetMaxSkew struct {
203 Args struct {
204 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
205 MaxSkew uint32 `positional-arg-name:"MAX_SKEW" required:"yes"`
206 } `positional-args:"yes"`
207}
208
Zack Williamse940c7a2019-08-21 14:25:39 -0700209type DeviceOpts struct {
210 List DeviceList `command:"list"`
211 Create DeviceCreate `command:"create"`
212 Delete DeviceDelete `command:"delete"`
213 Enable DeviceEnable `command:"enable"`
214 Disable DeviceDisable `command:"disable"`
215 Flows DeviceFlowList `command:"flows"`
kesavand12cd8eb2020-01-20 22:25:22 -0500216 Port struct {
217 List DevicePortList `command:"list"`
218 Enable DevicePortEnable `command:"enable"`
219 Disable DevicePortDisable `command:"disable"`
220 } `command:"port"`
221 Inspect DeviceInspect `command:"inspect"`
222 Reboot DeviceReboot `command:"reboot"`
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -0800223 Value struct {
224 Get DeviceGetExtValue `command:"get"`
225 } `command:"value"`
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000226 PmConfig struct {
Rohan Agrawald7df3772020-06-29 11:23:36 +0000227 Get DevicePmConfigsGet `command:"get"`
228 MaxSkew struct {
229 Set DevicePmConfigSetMaxSkew `command:"set"`
230 } `command:"maxskew"`
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000231 Frequency struct {
232 Set DevicePmConfigFrequencySet `command:"set"`
233 } `command:"frequency"`
234 Metric struct {
235 List DevicePmConfigMetricList `command:"list"`
236 Enable DevicePmConfigMetricEnable `command:"enable"`
237 Disable DevicePmConfigMetricDisable `command:"disable"`
238 } `command:"metric"`
239 Group struct {
240 List DevicePmConfigGroupList `command:"list"`
241 Enable DevicePmConfigGroupEnable `command:"enable"`
242 Disable DevicePmConfigGroupDisable `command:"disable"`
243 } `command:"group"`
244 GroupMetric struct {
245 List DevicePmConfigGroupMetricList `command:"list"`
246 } `command:"groupmetric"`
247 } `command:"pmconfig"`
Zack Williamse940c7a2019-08-21 14:25:39 -0700248}
249
250var deviceOpts = DeviceOpts{}
251
252func RegisterDeviceCommands(parser *flags.Parser) {
David Bainbridge12f036f2019-10-15 22:09:04 +0000253 if _, err := parser.AddCommand("device", "device commands", "Commands to query and manipulate VOLTHA devices", &deviceOpts); err != nil {
David Bainbridgea6722342019-10-24 23:55:53 +0000254 Error.Fatalf("Unexpected error while attempting to register device commands : %s", err)
David Bainbridge12f036f2019-10-15 22:09:04 +0000255 }
Zack Williamse940c7a2019-08-21 14:25:39 -0700256}
257
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000258func (i *MetricName) Complete(match string) []flags.Completion {
259 conn, err := NewConnection()
260 if err != nil {
261 return nil
262 }
263 defer conn.Close()
264
265 client := voltha.NewVolthaServiceClient(conn)
266
267 var deviceId string
268found:
269 for i := len(os.Args) - 1; i >= 0; i -= 1 {
270 switch os.Args[i] {
271 case "enable":
272 fallthrough
273 case "disable":
274 if len(os.Args) > i+1 {
275 deviceId = os.Args[i+1]
276 } else {
277 return nil
278 }
279 break found
280 default:
281 }
282 }
283
284 if len(deviceId) == 0 {
285 return nil
286 }
287
288 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
289 defer cancel()
290
291 id := voltha.ID{Id: string(deviceId)}
292
293 pmconfigs, err := client.ListDevicePmConfigs(ctx, &id)
294
295 if err != nil {
296 return nil
297 }
298
299 list := make([]flags.Completion, 0)
300 for _, metrics := range pmconfigs.Metrics {
301 if strings.HasPrefix(metrics.Name, match) {
302 list = append(list, flags.Completion{Item: metrics.Name})
303 }
304 }
305
306 return list
307}
308
309func (i *GroupName) Complete(match string) []flags.Completion {
310 conn, err := NewConnection()
311 if err != nil {
312 return nil
313 }
314 defer conn.Close()
315
316 client := voltha.NewVolthaServiceClient(conn)
317
318 var deviceId string
319found:
320 for i := len(os.Args) - 1; i >= 0; i -= 1 {
321 switch os.Args[i] {
322 case "list":
323 fallthrough
324 case "enable":
325 fallthrough
326 case "disable":
327 if len(os.Args) > i+1 {
328 deviceId = os.Args[i+1]
329 } else {
330 return nil
331 }
332 break found
333 default:
334 }
335 }
336
337 if len(deviceId) == 0 {
338 return nil
339 }
340
341 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
342 defer cancel()
343
344 id := voltha.ID{Id: string(deviceId)}
345
346 pmconfigs, err := client.ListDevicePmConfigs(ctx, &id)
347
348 if err != nil {
349 return nil
350 }
351
352 list := make([]flags.Completion, 0)
353 for _, group := range pmconfigs.Groups {
354 if strings.HasPrefix(group.GroupName, match) {
355 list = append(list, flags.Completion{Item: group.GroupName})
356 }
357 }
358 return list
359}
360
kesavand12cd8eb2020-01-20 22:25:22 -0500361func (i *PortNum) Complete(match string) []flags.Completion {
362 conn, err := NewConnection()
363 if err != nil {
364 return nil
365 }
366 defer conn.Close()
367
Scott Baker9173ed82020-05-19 08:30:12 -0700368 client := voltha.NewVolthaServiceClient(conn)
kesavand12cd8eb2020-01-20 22:25:22 -0500369
370 /*
371 * The command line args when completing for PortNum will be a DeviceId
372 * followed by one or more PortNums. So walk the argument list from the
373 * end and find the first argument that is enable/disable as those are
374 * the subcommands that come before the positional arguments. It would
375 * be nice if this package gave us the list of optional arguments
376 * already parsed.
377 */
378 var deviceId string
379found:
380 for i := len(os.Args) - 1; i >= 0; i -= 1 {
381 switch os.Args[i] {
382 case "enable":
383 fallthrough
384 case "disable":
385 if len(os.Args) > i+1 {
386 deviceId = os.Args[i+1]
387 } else {
388 return nil
389 }
390 break found
391 default:
392 }
393 }
394
395 if len(deviceId) == 0 {
396 return nil
397 }
398
kesavand12cd8eb2020-01-20 22:25:22 -0500399 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
400 defer cancel()
kesavand12cd8eb2020-01-20 22:25:22 -0500401
Scott Baker9173ed82020-05-19 08:30:12 -0700402 id := voltha.ID{Id: string(deviceId)}
kesavand12cd8eb2020-01-20 22:25:22 -0500403
Scott Baker9173ed82020-05-19 08:30:12 -0700404 ports, err := client.ListDevicePorts(ctx, &id)
kesavand12cd8eb2020-01-20 22:25:22 -0500405 if err != nil {
406 return nil
407 }
408
409 list := make([]flags.Completion, 0)
Scott Baker9173ed82020-05-19 08:30:12 -0700410 for _, item := range ports.Items {
411 pn := strconv.FormatUint(uint64(item.PortNo), 10)
kesavand12cd8eb2020-01-20 22:25:22 -0500412 if strings.HasPrefix(pn, match) {
413 list = append(list, flags.Completion{Item: pn})
414 }
415 }
416
417 return list
418}
419
Zack Williamse940c7a2019-08-21 14:25:39 -0700420func (i *DeviceId) Complete(match string) []flags.Completion {
421 conn, err := NewConnection()
422 if err != nil {
423 return nil
424 }
425 defer conn.Close()
426
Scott Baker9173ed82020-05-19 08:30:12 -0700427 client := voltha.NewVolthaServiceClient(conn)
Zack Williamse940c7a2019-08-21 14:25:39 -0700428
429 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
430 defer cancel()
431
Scott Baker9173ed82020-05-19 08:30:12 -0700432 devices, err := client.ListDevices(ctx, &empty.Empty{})
Zack Williamse940c7a2019-08-21 14:25:39 -0700433 if err != nil {
434 return nil
435 }
436
437 list := make([]flags.Completion, 0)
Scott Baker9173ed82020-05-19 08:30:12 -0700438 for _, item := range devices.Items {
439 if strings.HasPrefix(item.Id, match) {
440 list = append(list, flags.Completion{Item: item.Id})
Zack Williamse940c7a2019-08-21 14:25:39 -0700441 }
442 }
443
444 return list
445}
446
447func (options *DeviceList) Execute(args []string) error {
448
449 conn, err := NewConnection()
450 if err != nil {
451 return err
452 }
453 defer conn.Close()
454
Scott Baker9173ed82020-05-19 08:30:12 -0700455 client := voltha.NewVolthaServiceClient(conn)
Zack Williamse940c7a2019-08-21 14:25:39 -0700456
457 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
458 defer cancel()
459
Scott Baker9173ed82020-05-19 08:30:12 -0700460 devices, err := client.ListDevices(ctx, &empty.Empty{})
Zack Williamse940c7a2019-08-21 14:25:39 -0700461 if err != nil {
462 return err
463 }
464
465 outputFormat := CharReplacer.Replace(options.Format)
466 if outputFormat == "" {
David Bainbridgea6722342019-10-24 23:55:53 +0000467 outputFormat = GetCommandOptionWithDefault("device-list", "format", DEFAULT_DEVICE_FORMAT)
Zack Williamse940c7a2019-08-21 14:25:39 -0700468 }
469 if options.Quiet {
470 outputFormat = "{{.Id}}"
471 }
472
David Bainbridgea6722342019-10-24 23:55:53 +0000473 orderBy := options.OrderBy
474 if orderBy == "" {
475 orderBy = GetCommandOptionWithDefault("device-list", "order", "")
476 }
477
Scott Baker9173ed82020-05-19 08:30:12 -0700478 // Make sure json output prints an empty list, not "null"
479 if devices.Items == nil {
480 devices.Items = make([]*voltha.Device, 0)
Zack Williamse940c7a2019-08-21 14:25:39 -0700481 }
482
483 result := CommandResult{
484 Format: format.Format(outputFormat),
485 Filter: options.Filter,
David Bainbridgea6722342019-10-24 23:55:53 +0000486 OrderBy: orderBy,
Zack Williamse940c7a2019-08-21 14:25:39 -0700487 OutputAs: toOutputType(options.OutputAs),
488 NameLimit: options.NameLimit,
Scott Baker9173ed82020-05-19 08:30:12 -0700489 Data: devices.Items,
Zack Williamse940c7a2019-08-21 14:25:39 -0700490 }
491
492 GenerateOutput(&result)
493 return nil
494}
495
496func (options *DeviceCreate) Execute(args []string) error {
497
Scott Baker9173ed82020-05-19 08:30:12 -0700498 device := voltha.Device{}
Zack Williamse940c7a2019-08-21 14:25:39 -0700499 if options.HostAndPort != "" {
Scott Baker9173ed82020-05-19 08:30:12 -0700500 device.Address = &voltha.Device_HostAndPort{HostAndPort: options.HostAndPort}
Zack Williamse940c7a2019-08-21 14:25:39 -0700501 } else if options.IPAddress != "" {
Scott Baker9173ed82020-05-19 08:30:12 -0700502 device.Address = &voltha.Device_Ipv4Address{Ipv4Address: options.IPAddress}
Hardik Windlassce1de342020-02-04 21:58:07 +0000503 }
504 if options.MACAddress != "" {
Scott Baker9173ed82020-05-19 08:30:12 -0700505 device.MacAddress = strings.ToLower(options.MACAddress)
Zack Williamse940c7a2019-08-21 14:25:39 -0700506 }
507 if options.DeviceType != "" {
Scott Baker9173ed82020-05-19 08:30:12 -0700508 device.Type = options.DeviceType
Zack Williamse940c7a2019-08-21 14:25:39 -0700509 }
510
511 conn, err := NewConnection()
512 if err != nil {
513 return err
514 }
515 defer conn.Close()
516
Scott Baker9173ed82020-05-19 08:30:12 -0700517 client := voltha.NewVolthaServiceClient(conn)
Zack Williamse940c7a2019-08-21 14:25:39 -0700518
519 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
520 defer cancel()
521
Scott Baker9173ed82020-05-19 08:30:12 -0700522 createdDevice, err := client.CreateDevice(ctx, &device)
Zack Williamse940c7a2019-08-21 14:25:39 -0700523 if err != nil {
524 return err
Zack Williamse940c7a2019-08-21 14:25:39 -0700525 }
526
Scott Baker9173ed82020-05-19 08:30:12 -0700527 fmt.Printf("%s\n", createdDevice.Id)
Zack Williamse940c7a2019-08-21 14:25:39 -0700528
529 return nil
530}
531
532func (options *DeviceDelete) Execute(args []string) error {
533
534 conn, err := NewConnection()
535 if err != nil {
536 return err
537 }
538 defer conn.Close()
539
Scott Baker9173ed82020-05-19 08:30:12 -0700540 client := voltha.NewVolthaServiceClient(conn)
David Bainbridge7052fe82020-03-25 10:37:00 -0700541 var lastErr error
Zack Williamse940c7a2019-08-21 14:25:39 -0700542 for _, i := range options.Args.Ids {
Zack Williamse940c7a2019-08-21 14:25:39 -0700543 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
544 defer cancel()
545
Scott Baker9173ed82020-05-19 08:30:12 -0700546 id := voltha.ID{Id: string(i)}
Himani Chawla9933ddc2020-10-12 23:53:27 +0530547 if options.Force {
548 _, err = client.ForceDeleteDevice(ctx, &id)
549 } else {
550 _, err = client.DeleteDevice(ctx, &id)
551 }
Scott Baker9173ed82020-05-19 08:30:12 -0700552
Zack Williamse940c7a2019-08-21 14:25:39 -0700553 if err != nil {
David Bainbridge0f758d42019-10-26 05:17:48 +0000554 Error.Printf("Error while deleting '%s': %s\n", i, err)
David Bainbridge7052fe82020-03-25 10:37:00 -0700555 lastErr = err
Zack Williamse940c7a2019-08-21 14:25:39 -0700556 continue
Zack Williamse940c7a2019-08-21 14:25:39 -0700557 }
558 fmt.Printf("%s\n", i)
559 }
560
David Bainbridge7052fe82020-03-25 10:37:00 -0700561 if lastErr != nil {
562 return NoReportErr
563 }
Zack Williamse940c7a2019-08-21 14:25:39 -0700564 return nil
565}
566
567func (options *DeviceEnable) Execute(args []string) error {
568 conn, err := NewConnection()
569 if err != nil {
570 return err
571 }
572 defer conn.Close()
573
Scott Baker9173ed82020-05-19 08:30:12 -0700574 client := voltha.NewVolthaServiceClient(conn)
Zack Williamse940c7a2019-08-21 14:25:39 -0700575
David Bainbridge7052fe82020-03-25 10:37:00 -0700576 var lastErr error
Zack Williamse940c7a2019-08-21 14:25:39 -0700577 for _, i := range options.Args.Ids {
Zack Williamse940c7a2019-08-21 14:25:39 -0700578 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
579 defer cancel()
580
Scott Baker9173ed82020-05-19 08:30:12 -0700581 id := voltha.ID{Id: string(i)}
582
583 _, err := client.EnableDevice(ctx, &id)
Zack Williamse940c7a2019-08-21 14:25:39 -0700584 if err != nil {
David Bainbridge0f758d42019-10-26 05:17:48 +0000585 Error.Printf("Error while enabling '%s': %s\n", i, err)
David Bainbridge7052fe82020-03-25 10:37:00 -0700586 lastErr = err
Zack Williamse940c7a2019-08-21 14:25:39 -0700587 continue
Zack Williamse940c7a2019-08-21 14:25:39 -0700588 }
589 fmt.Printf("%s\n", i)
590 }
591
David Bainbridge7052fe82020-03-25 10:37:00 -0700592 if lastErr != nil {
593 return NoReportErr
594 }
Zack Williamse940c7a2019-08-21 14:25:39 -0700595 return nil
596}
597
598func (options *DeviceDisable) Execute(args []string) error {
599 conn, err := NewConnection()
600 if err != nil {
601 return err
602 }
603 defer conn.Close()
604
Scott Baker9173ed82020-05-19 08:30:12 -0700605 client := voltha.NewVolthaServiceClient(conn)
Zack Williamse940c7a2019-08-21 14:25:39 -0700606
David Bainbridge7052fe82020-03-25 10:37:00 -0700607 var lastErr error
Zack Williamse940c7a2019-08-21 14:25:39 -0700608 for _, i := range options.Args.Ids {
Zack Williamse940c7a2019-08-21 14:25:39 -0700609 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
610 defer cancel()
611
Scott Baker9173ed82020-05-19 08:30:12 -0700612 id := voltha.ID{Id: string(i)}
613
614 _, err := client.DisableDevice(ctx, &id)
Zack Williamse940c7a2019-08-21 14:25:39 -0700615 if err != nil {
David Bainbridge0f758d42019-10-26 05:17:48 +0000616 Error.Printf("Error while disabling '%s': %s\n", i, err)
David Bainbridge7052fe82020-03-25 10:37:00 -0700617 lastErr = err
Zack Williamse940c7a2019-08-21 14:25:39 -0700618 continue
Zack Williamse940c7a2019-08-21 14:25:39 -0700619 }
620 fmt.Printf("%s\n", i)
621 }
622
David Bainbridge7052fe82020-03-25 10:37:00 -0700623 if lastErr != nil {
624 return NoReportErr
625 }
Zack Williamse940c7a2019-08-21 14:25:39 -0700626 return nil
627}
628
629func (options *DeviceReboot) Execute(args []string) error {
630 conn, err := NewConnection()
631 if err != nil {
632 return err
633 }
634 defer conn.Close()
635
Scott Baker9173ed82020-05-19 08:30:12 -0700636 client := voltha.NewVolthaServiceClient(conn)
Zack Williamse940c7a2019-08-21 14:25:39 -0700637
David Bainbridge7052fe82020-03-25 10:37:00 -0700638 var lastErr error
Zack Williamse940c7a2019-08-21 14:25:39 -0700639 for _, i := range options.Args.Ids {
Zack Williamse940c7a2019-08-21 14:25:39 -0700640 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
641 defer cancel()
642
Scott Baker9173ed82020-05-19 08:30:12 -0700643 id := voltha.ID{Id: string(i)}
644
645 _, err := client.RebootDevice(ctx, &id)
Zack Williamse940c7a2019-08-21 14:25:39 -0700646 if err != nil {
David Bainbridge0f758d42019-10-26 05:17:48 +0000647 Error.Printf("Error while rebooting '%s': %s\n", i, err)
David Bainbridge7052fe82020-03-25 10:37:00 -0700648 lastErr = err
Zack Williamse940c7a2019-08-21 14:25:39 -0700649 continue
Zack Williamse940c7a2019-08-21 14:25:39 -0700650 }
651 fmt.Printf("%s\n", i)
652 }
653
David Bainbridge7052fe82020-03-25 10:37:00 -0700654 if lastErr != nil {
655 return NoReportErr
656 }
Zack Williamse940c7a2019-08-21 14:25:39 -0700657 return nil
658}
659
660func (options *DevicePortList) Execute(args []string) error {
661
662 conn, err := NewConnection()
663 if err != nil {
664 return err
665 }
666 defer conn.Close()
667
Scott Baker9173ed82020-05-19 08:30:12 -0700668 client := voltha.NewVolthaServiceClient(conn)
Zack Williamse940c7a2019-08-21 14:25:39 -0700669
670 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
671 defer cancel()
672
Scott Baker9173ed82020-05-19 08:30:12 -0700673 id := voltha.ID{Id: string(options.Args.Id)}
Zack Williamse940c7a2019-08-21 14:25:39 -0700674
Scott Baker9173ed82020-05-19 08:30:12 -0700675 ports, err := client.ListDevicePorts(ctx, &id)
Zack Williamse940c7a2019-08-21 14:25:39 -0700676 if err != nil {
677 return err
678 }
679
680 outputFormat := CharReplacer.Replace(options.Format)
681 if outputFormat == "" {
David Bainbridgea6722342019-10-24 23:55:53 +0000682 outputFormat = GetCommandOptionWithDefault("device-ports", "format", DEFAULT_DEVICE_PORTS_FORMAT)
Zack Williamse940c7a2019-08-21 14:25:39 -0700683 }
Zack Williamse940c7a2019-08-21 14:25:39 -0700684
David Bainbridgea6722342019-10-24 23:55:53 +0000685 orderBy := options.OrderBy
686 if orderBy == "" {
687 orderBy = GetCommandOptionWithDefault("device-ports", "order", "")
688 }
689
Zack Williamse940c7a2019-08-21 14:25:39 -0700690 result := CommandResult{
691 Format: format.Format(outputFormat),
692 Filter: options.Filter,
David Bainbridgea6722342019-10-24 23:55:53 +0000693 OrderBy: orderBy,
Zack Williamse940c7a2019-08-21 14:25:39 -0700694 OutputAs: toOutputType(options.OutputAs),
695 NameLimit: options.NameLimit,
Scott Baker9173ed82020-05-19 08:30:12 -0700696 Data: ports.Items,
Zack Williamse940c7a2019-08-21 14:25:39 -0700697 }
698
699 GenerateOutput(&result)
700 return nil
701}
702
703func (options *DeviceFlowList) Execute(args []string) error {
704 fl := &FlowList{}
705 fl.ListOutputOptions = options.ListOutputOptions
Maninder045921e2020-09-29 16:46:02 +0530706 fl.FlowIdOptions = options.FlowIdOptions
Zack Williamse940c7a2019-08-21 14:25:39 -0700707 fl.Args.Id = string(options.Args.Id)
David Bainbridgea6722342019-10-24 23:55:53 +0000708 fl.Method = "device-flows"
Zack Williamse940c7a2019-08-21 14:25:39 -0700709 return fl.Execute(args)
710}
711
712func (options *DeviceInspect) Execute(args []string) error {
713 if len(args) > 0 {
714 return fmt.Errorf("only a single argument 'DEVICE_ID' can be provided")
715 }
716
717 conn, err := NewConnection()
718 if err != nil {
719 return err
720 }
721 defer conn.Close()
722
Scott Baker9173ed82020-05-19 08:30:12 -0700723 client := voltha.NewVolthaServiceClient(conn)
Zack Williamse940c7a2019-08-21 14:25:39 -0700724
725 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
726 defer cancel()
727
Scott Baker9173ed82020-05-19 08:30:12 -0700728 id := voltha.ID{Id: string(options.Args.Id)}
Zack Williamse940c7a2019-08-21 14:25:39 -0700729
Scott Baker9173ed82020-05-19 08:30:12 -0700730 device, err := client.GetDevice(ctx, &id)
Zack Williamse940c7a2019-08-21 14:25:39 -0700731 if err != nil {
732 return err
733 }
734
Zack Williamse940c7a2019-08-21 14:25:39 -0700735 outputFormat := CharReplacer.Replace(options.Format)
736 if outputFormat == "" {
David Bainbridgea6722342019-10-24 23:55:53 +0000737 outputFormat = GetCommandOptionWithDefault("device-inspect", "format", DEFAULT_DEVICE_INSPECT_FORMAT)
Zack Williamse940c7a2019-08-21 14:25:39 -0700738 }
739 if options.Quiet {
740 outputFormat = "{{.Id}}"
741 }
742
743 result := CommandResult{
744 Format: format.Format(outputFormat),
745 OutputAs: toOutputType(options.OutputAs),
746 NameLimit: options.NameLimit,
747 Data: device,
748 }
749 GenerateOutput(&result)
750 return nil
751}
kesavand12cd8eb2020-01-20 22:25:22 -0500752
753/*Device Port Enable */
754func (options *DevicePortEnable) Execute(args []string) error {
755 conn, err := NewConnection()
756 if err != nil {
757 return err
758 }
759 defer conn.Close()
760
Scott Baker9173ed82020-05-19 08:30:12 -0700761 client := voltha.NewVolthaServiceClient(conn)
kesavand12cd8eb2020-01-20 22:25:22 -0500762
kesavand12cd8eb2020-01-20 22:25:22 -0500763 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
764 defer cancel()
765
Scott Baker9173ed82020-05-19 08:30:12 -0700766 port := voltha.Port{DeviceId: string(options.Args.Id), PortNo: uint32(options.Args.PortId)}
767
768 _, err = client.EnablePort(ctx, &port)
kesavand12cd8eb2020-01-20 22:25:22 -0500769 if err != nil {
770 Error.Printf("Error enabling port number %v on device Id %s,err=%s\n", options.Args.PortId, options.Args.Id, ErrorToString(err))
771 return err
kesavand12cd8eb2020-01-20 22:25:22 -0500772 }
773
774 return nil
775}
776
Scott Baker9173ed82020-05-19 08:30:12 -0700777/*Device Port Disable */
kesavand12cd8eb2020-01-20 22:25:22 -0500778func (options *DevicePortDisable) Execute(args []string) error {
779 conn, err := NewConnection()
780 if err != nil {
781 return err
782 }
783 defer conn.Close()
784
Scott Baker9173ed82020-05-19 08:30:12 -0700785 client := voltha.NewVolthaServiceClient(conn)
786
kesavand12cd8eb2020-01-20 22:25:22 -0500787 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
788 defer cancel()
789
Scott Baker9173ed82020-05-19 08:30:12 -0700790 port := voltha.Port{DeviceId: string(options.Args.Id), PortNo: uint32(options.Args.PortId)}
791
792 _, err = client.DisablePort(ctx, &port)
kesavand12cd8eb2020-01-20 22:25:22 -0500793 if err != nil {
Scott Baker9173ed82020-05-19 08:30:12 -0700794 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 -0500795 return err
kesavand12cd8eb2020-01-20 22:25:22 -0500796 }
Scott Baker9173ed82020-05-19 08:30:12 -0700797
kesavand12cd8eb2020-01-20 22:25:22 -0500798 return nil
799}
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -0800800
Rohan Agrawald7df3772020-06-29 11:23:36 +0000801func (options *DevicePmConfigSetMaxSkew) Execute(args []string) error {
802 conn, err := NewConnection()
803 if err != nil {
804 return err
805 }
806 defer conn.Close()
807
808 client := voltha.NewVolthaServiceClient(conn)
809
810 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
811 defer cancel()
812
813 id := voltha.ID{Id: string(options.Args.Id)}
814
815 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
816 if err != nil {
817 return err
818 }
819
820 pmConfigs.MaxSkew = options.Args.MaxSkew
821
822 _, err = client.UpdateDevicePmConfigs(ctx, pmConfigs)
823 if err != nil {
824 return err
825 }
826
827 return nil
828}
829
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000830func (options *DevicePmConfigsGet) Execute(args []string) error {
831
832 conn, err := NewConnection()
833 if err != nil {
834 return err
835 }
836 defer conn.Close()
837
838 client := voltha.NewVolthaServiceClient(conn)
839
840 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
841 defer cancel()
842
843 id := voltha.ID{Id: string(options.Args.Id)}
844
845 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
846 if err != nil {
847 return err
848 }
849
850 outputFormat := CharReplacer.Replace(options.Format)
851 if outputFormat == "" {
852 outputFormat = GetCommandOptionWithDefault("device-pm-configs", "format", DEFAULT_DEVICE_PM_CONFIG_GET_FORMAT)
853 }
854
855 orderBy := options.OrderBy
856 if orderBy == "" {
857 orderBy = GetCommandOptionWithDefault("device-pm-configs", "order", "")
858 }
859
860 result := CommandResult{
861 Format: format.Format(outputFormat),
862 Filter: options.Filter,
863 OrderBy: orderBy,
864 OutputAs: toOutputType(options.OutputAs),
865 NameLimit: options.NameLimit,
866 Data: pmConfigs,
867 }
868
869 GenerateOutput(&result)
870 return nil
871
872}
873
874func (options *DevicePmConfigMetricList) Execute(args []string) error {
875
876 conn, err := NewConnection()
877 if err != nil {
878 return err
879 }
880 defer conn.Close()
881
882 client := voltha.NewVolthaServiceClient(conn)
883
884 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
885 defer cancel()
886
887 id := voltha.ID{Id: string(options.Args.Id)}
888
889 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
890 if err != nil {
891 return err
892 }
893
894 if !pmConfigs.Grouped {
895 for _, metric := range pmConfigs.Metrics {
896 if metric.SampleFreq == 0 {
897 metric.SampleFreq = pmConfigs.DefaultFreq
898 }
899 }
Rohan Agrawalbca69122020-06-17 14:59:03 +0000900 outputFormat := CharReplacer.Replace(options.Format)
901 if outputFormat == "" {
902 outputFormat = GetCommandOptionWithDefault("device-pm-configs", "format", DEFAULT_DEVICE_PM_CONFIG_METRIC_LIST_FORMAT)
903 }
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000904
Rohan Agrawalbca69122020-06-17 14:59:03 +0000905 orderBy := options.OrderBy
906 if orderBy == "" {
907 orderBy = GetCommandOptionWithDefault("device-pm-configs", "order", "")
908 }
909
910 result := CommandResult{
911 Format: format.Format(outputFormat),
912 Filter: options.Filter,
913 OrderBy: orderBy,
914 OutputAs: toOutputType(options.OutputAs),
915 NameLimit: options.NameLimit,
916 Data: pmConfigs.Metrics,
917 }
918
919 GenerateOutput(&result)
920 return nil
921 } else {
922 return fmt.Errorf("Device '%s' does not have Non Grouped Metrics", options.Args.Id)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000923 }
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000924}
925
926func (options *DevicePmConfigMetricEnable) Execute(args []string) error {
927
928 conn, err := NewConnection()
929 if err != nil {
930 return err
931 }
932 defer conn.Close()
933
934 client := voltha.NewVolthaServiceClient(conn)
935
936 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
937 defer cancel()
938
939 id := voltha.ID{Id: string(options.Args.Id)}
940
941 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
942 if err != nil {
943 return err
944 }
945
946 if !pmConfigs.Grouped {
Rohan Agrawalbca69122020-06-17 14:59:03 +0000947 metrics := make(map[string]struct{})
948 for _, metric := range pmConfigs.Metrics {
949 metrics[metric.Name] = struct{}{}
950 }
951
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000952 for _, metric := range pmConfigs.Metrics {
953 for _, mName := range options.Args.Metrics {
Rohan Agrawalbca69122020-06-17 14:59:03 +0000954 if _, exist := metrics[string(mName)]; !exist {
955 return fmt.Errorf("Metric Name '%s' does not exist", mName)
956 }
957
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000958 if string(mName) == metric.Name && !metric.Enabled {
959 metric.Enabled = true
960 _, err := client.UpdateDevicePmConfigs(ctx, pmConfigs)
961 if err != nil {
962 return err
963 }
964 }
965 }
966 }
Rohan Agrawalbca69122020-06-17 14:59:03 +0000967 } else {
968 return fmt.Errorf("Device '%s' does not have Non Grouped Metrics", options.Args.Id)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000969 }
970 return nil
971}
972
973func (options *DevicePmConfigMetricDisable) Execute(args []string) error {
974
975 conn, err := NewConnection()
976 if err != nil {
977 return err
978 }
979 defer conn.Close()
980
981 client := voltha.NewVolthaServiceClient(conn)
982
983 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
984 defer cancel()
985
986 id := voltha.ID{Id: string(options.Args.Id)}
987
988 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
989 if err != nil {
990 return err
991 }
992
993 if !pmConfigs.Grouped {
Rohan Agrawalbca69122020-06-17 14:59:03 +0000994 metrics := make(map[string]struct{})
995 for _, metric := range pmConfigs.Metrics {
996 metrics[metric.Name] = struct{}{}
997 }
998
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000999 for _, metric := range pmConfigs.Metrics {
1000 for _, mName := range options.Args.Metrics {
Rohan Agrawalbca69122020-06-17 14:59:03 +00001001 if _, have := metrics[string(mName)]; !have {
1002 return fmt.Errorf("Metric Name '%s' does not exist", mName)
1003 }
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001004 if string(mName) == metric.Name && metric.Enabled {
1005 metric.Enabled = false
1006 _, err := client.UpdateDevicePmConfigs(ctx, pmConfigs)
1007 if err != nil {
1008 return err
1009 }
Rohan Agrawalbca69122020-06-17 14:59:03 +00001010 } else {
1011 return fmt.Errorf("Metric '%s' cannot be disabled", string(mName))
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001012 }
1013 }
1014 }
Rohan Agrawalbca69122020-06-17 14:59:03 +00001015 } else {
1016 return fmt.Errorf("Device '%s' does not have Non Grouped Metrics", options.Args.Id)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001017 }
1018 return nil
1019}
1020
1021func (options *DevicePmConfigGroupEnable) Execute(args []string) error {
1022
1023 conn, err := NewConnection()
1024 if err != nil {
1025 return err
1026 }
1027 defer conn.Close()
1028
1029 client := voltha.NewVolthaServiceClient(conn)
1030
1031 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
1032 defer cancel()
1033
1034 id := voltha.ID{Id: string(options.Args.Id)}
1035
1036 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
1037 if err != nil {
1038 return err
1039 }
1040
1041 if pmConfigs.Grouped {
Rohan Agrawalbca69122020-06-17 14:59:03 +00001042 groups := make(map[string]struct{})
1043 for _, group := range pmConfigs.Groups {
1044 groups[group.GroupName] = struct{}{}
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 }
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001051 if string(gName) == group.GroupName && !group.Enabled {
1052 group.Enabled = true
1053 _, err := client.UpdateDevicePmConfigs(ctx, pmConfigs)
1054 if err != nil {
1055 return err
1056 }
1057 }
1058 }
1059 }
Rohan Agrawalbca69122020-06-17 14:59:03 +00001060 } else {
1061 return fmt.Errorf("Device '%s' does not have Group Metrics", options.Args.Id)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001062 }
1063 return nil
1064}
1065
1066func (options *DevicePmConfigGroupDisable) Execute(args []string) error {
1067
1068 conn, err := NewConnection()
1069 if err != nil {
1070 return err
1071 }
1072 defer conn.Close()
1073
1074 client := voltha.NewVolthaServiceClient(conn)
1075
1076 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
1077 defer cancel()
1078
1079 id := voltha.ID{Id: string(options.Args.Id)}
1080
1081 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
1082 if err != nil {
1083 return err
1084 }
1085
1086 if pmConfigs.Grouped {
Rohan Agrawalbca69122020-06-17 14:59:03 +00001087 groups := make(map[string]struct{})
1088 for _, group := range pmConfigs.Groups {
1089 groups[group.GroupName] = struct{}{}
1090 }
1091
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001092 for _, group := range pmConfigs.Groups {
1093 for _, gName := range options.Args.Groups {
Rohan Agrawalbca69122020-06-17 14:59:03 +00001094 if _, have := groups[string(gName)]; !have {
1095 return fmt.Errorf("Group Name '%s' does not exist", gName)
1096 }
1097
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001098 if string(gName) == group.GroupName && group.Enabled {
1099 group.Enabled = false
1100 _, err := client.UpdateDevicePmConfigs(ctx, pmConfigs)
1101 if err != nil {
1102 return err
1103 }
1104 }
1105 }
1106 }
Rohan Agrawalbca69122020-06-17 14:59:03 +00001107 } else {
1108 return fmt.Errorf("Device '%s' does not have Group Metrics", options.Args.Id)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001109 }
1110 return nil
1111}
1112
1113func (options *DevicePmConfigGroupList) Execute(args []string) error {
1114
1115 conn, err := NewConnection()
1116 if err != nil {
1117 return err
1118 }
1119 defer conn.Close()
1120
1121 client := voltha.NewVolthaServiceClient(conn)
1122
1123 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
1124 defer cancel()
1125
1126 id := voltha.ID{Id: string(options.Args.Id)}
1127
1128 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
1129 if err != nil {
1130 return err
1131 }
1132
1133 if pmConfigs.Grouped {
1134 for _, group := range pmConfigs.Groups {
1135 if group.GroupFreq == 0 {
1136 group.GroupFreq = pmConfigs.DefaultFreq
1137 }
1138 }
Rohan Agrawalbca69122020-06-17 14:59:03 +00001139 outputFormat := CharReplacer.Replace(options.Format)
1140 if outputFormat == "" {
1141 outputFormat = GetCommandOptionWithDefault("device-pm-configs", "format", DEFAULT_DEVICE_PM_CONFIG_GROUP_LIST_FORMAT)
1142 }
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001143
Rohan Agrawalbca69122020-06-17 14:59:03 +00001144 orderBy := options.OrderBy
1145 if orderBy == "" {
1146 orderBy = GetCommandOptionWithDefault("device-pm-configs", "order", "")
1147 }
1148
1149 result := CommandResult{
1150 Format: format.Format(outputFormat),
1151 Filter: options.Filter,
1152 OrderBy: orderBy,
1153 OutputAs: toOutputType(options.OutputAs),
1154 NameLimit: options.NameLimit,
1155 Data: pmConfigs.Groups,
1156 }
1157
1158 GenerateOutput(&result)
1159 } else {
1160 return fmt.Errorf("Device '%s' does not have Group Metrics", string(options.Args.Id))
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001161 }
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001162 return nil
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001163}
1164
1165func (options *DevicePmConfigGroupMetricList) Execute(args []string) error {
1166
1167 var metrics []*voltha.PmConfig
1168 conn, err := NewConnection()
1169 if err != nil {
1170 return err
1171 }
1172 defer conn.Close()
1173
1174 client := voltha.NewVolthaServiceClient(conn)
1175
1176 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
1177 defer cancel()
1178
1179 id := voltha.ID{Id: string(options.Args.Id)}
1180
1181 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
1182 if err != nil {
1183 return err
1184 }
1185
1186 for _, groups := range pmConfigs.Groups {
1187
1188 if string(options.Args.Group) == groups.GroupName {
1189 for _, metric := range groups.Metrics {
1190 if metric.SampleFreq == 0 && groups.GroupFreq == 0 {
1191 metric.SampleFreq = pmConfigs.DefaultFreq
1192 } else {
1193 metric.SampleFreq = groups.GroupFreq
1194 }
1195 }
1196 metrics = groups.Metrics
1197 }
1198 }
1199
1200 outputFormat := CharReplacer.Replace(options.Format)
1201 if outputFormat == "" {
1202 outputFormat = GetCommandOptionWithDefault("device-pm-configs", "format", DEFAULT_DEVICE_PM_CONFIG_METRIC_LIST_FORMAT)
1203 }
1204
1205 orderBy := options.OrderBy
1206 if orderBy == "" {
1207 orderBy = GetCommandOptionWithDefault("device-pm-configs", "order", "")
1208 }
1209
1210 result := CommandResult{
1211 Format: format.Format(outputFormat),
1212 Filter: options.Filter,
1213 OrderBy: orderBy,
1214 OutputAs: toOutputType(options.OutputAs),
1215 NameLimit: options.NameLimit,
1216 Data: metrics,
1217 }
1218
1219 GenerateOutput(&result)
1220 return nil
1221
1222}
1223
1224func (options *DevicePmConfigFrequencySet) Execute(args []string) error {
1225
1226 conn, err := NewConnection()
1227 if err != nil {
1228 return err
1229 }
1230 defer conn.Close()
1231
1232 client := voltha.NewVolthaServiceClient(conn)
1233
1234 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
1235 defer cancel()
1236
1237 id := voltha.ID{Id: string(options.Args.Id)}
1238
1239 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
1240 if err != nil {
1241 return err
1242 }
1243
1244 pmConfigs.DefaultFreq = options.Args.Frequency
1245
1246 _, err = client.UpdateDevicePmConfigs(ctx, pmConfigs)
1247 if err != nil {
1248 return err
1249 }
1250
1251 outputFormat := CharReplacer.Replace(options.Format)
1252 if outputFormat == "" {
1253 outputFormat = GetCommandOptionWithDefault("device-pm-configs", "format", DEFAULT_DEVICE_PM_CONFIG_GET_FORMAT)
1254 }
1255 if options.Quiet {
1256 outputFormat = "{{.Id}}"
1257 }
1258
1259 result := CommandResult{
1260 Format: format.Format(outputFormat),
1261 OutputAs: toOutputType(options.OutputAs),
1262 NameLimit: options.NameLimit,
1263 Data: pmConfigs,
1264 }
1265
1266 GenerateOutput(&result)
1267 return nil
1268
1269}
1270
Scott Baker9173ed82020-05-19 08:30:12 -07001271type ReturnValueRow struct {
1272 Name string `json:"name"`
1273 Result interface{} `json:"result"`
1274}
1275
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -08001276/*Device get Onu Distance */
1277func (options *DeviceGetExtValue) Execute(args []string) error {
1278 conn, err := NewConnection()
1279 if err != nil {
1280 return err
1281 }
1282 defer conn.Close()
1283
Scott Baker9173ed82020-05-19 08:30:12 -07001284 client := voltha.NewVolthaServiceClient(conn)
1285
1286 valueflag, okay := common.ValueType_Type_value[string(options.Args.Valueflag)]
1287 if !okay {
1288 Error.Printf("Unknown valueflag %s\n", options.Args.Valueflag)
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -08001289 }
1290
Scott Baker9173ed82020-05-19 08:30:12 -07001291 val := voltha.ValueSpecifier{Id: string(options.Args.Id), Value: common.ValueType_Type(valueflag)}
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -08001292
1293 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
1294 defer cancel()
1295
Scott Baker9173ed82020-05-19 08:30:12 -07001296 rv, err := client.GetExtValue(ctx, &val)
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -08001297 if err != nil {
1298 Error.Printf("Error getting value on device Id %s,err=%s\n", options.Args.Id, ErrorToString(err))
1299 return err
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -08001300 }
1301
Scott Baker9173ed82020-05-19 08:30:12 -07001302 var rows []ReturnValueRow
1303 for name, num := range common.ValueType_Type_value {
1304 if num == 0 {
1305 // EMPTY is not a real value
1306 continue
1307 }
1308 if (rv.Error & uint32(num)) != 0 {
1309 row := ReturnValueRow{Name: name, Result: "Error"}
1310 rows = append(rows, row)
1311 }
1312 if (rv.Unsupported & uint32(num)) != 0 {
1313 row := ReturnValueRow{Name: name, Result: "Unsupported"}
1314 rows = append(rows, row)
1315 }
1316 if (rv.Set & uint32(num)) != 0 {
1317 switch name {
1318 case "DISTANCE":
1319 row := ReturnValueRow{Name: name, Result: rv.Distance}
1320 rows = append(rows, row)
1321 default:
1322 row := ReturnValueRow{Name: name, Result: "Unimplemented-in-voltctl"}
1323 rows = append(rows, row)
1324 }
1325 }
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -08001326 }
1327
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -08001328 outputFormat := CharReplacer.Replace(options.Format)
1329 if outputFormat == "" {
1330 outputFormat = GetCommandOptionWithDefault("device-value-get", "format", DEFAULT_DEVICE_VALUE_GET_FORMAT)
1331 }
1332
1333 result := CommandResult{
1334 Format: format.Format(outputFormat),
1335 OutputAs: toOutputType(options.OutputAs),
1336 NameLimit: options.NameLimit,
Scott Baker9173ed82020-05-19 08:30:12 -07001337 Data: rows,
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -08001338 }
1339 GenerateOutput(&result)
1340 return nil
1341}