blob: 2f0d8a870beb5e6e2e6bee7b45f8720060245999 [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
Maninder045921e2020-09-29 16:46:02 +053094 FlowIdOptions
Zack Williamse940c7a2019-08-21 14:25:39 -070095 Args struct {
96 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
97 } `positional-args:"yes"`
98}
99
100type DevicePortList struct {
101 ListOutputOptions
102 Args struct {
103 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
104 } `positional-args:"yes"`
105}
106
107type DeviceInspect struct {
108 OutputOptionsJson
109 Args struct {
110 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
111 } `positional-args:"yes"`
112}
113
kesavand12cd8eb2020-01-20 22:25:22 -0500114type DevicePortEnable struct {
115 Args struct {
116 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
117 PortId PortNum `positional-arg-name:"PORT_NUMBER" required:"yes"`
118 } `positional-args:"yes"`
119}
120
121type DevicePortDisable struct {
122 Args struct {
123 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
124 PortId PortNum `positional-arg-name:"PORT_NUMBER" required:"yes"`
125 } `positional-args:"yes"`
126}
127
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000128type DevicePmConfigsGet struct {
129 ListOutputOptions
130 Args struct {
131 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
132 } `positional-args:"yes"`
133}
134
135type DevicePmConfigMetricList struct {
136 ListOutputOptions
137 Args struct {
138 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
139 } `positional-args:"yes"`
140}
141
142type DevicePmConfigGroupList struct {
143 ListOutputOptions
144 Args struct {
145 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
146 } `positional-args:"yes"`
147}
148
149type DevicePmConfigGroupMetricList struct {
150 ListOutputOptions
151 Args struct {
152 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
153 Group GroupName `positional-arg-name:"GROUP_NAME" required:"yes"`
154 } `positional-args:"yes"`
155}
156
157type DevicePmConfigFrequencySet struct {
158 OutputOptions
159 Args struct {
160 Frequency uint32 `positional-arg-name:"FREQUENCY" required:"yes"`
161 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
162 } `positional-args:"yes"`
163}
164
165type DevicePmConfigMetricEnable struct {
166 Args struct {
167 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
168 Metrics []MetricName `positional-arg-name:"METRIC_NAME" required:"yes"`
169 } `positional-args:"yes"`
170}
171
172type DevicePmConfigMetricDisable struct {
173 Args struct {
174 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
175 Metrics []MetricName `positional-arg-name:"METRIC_NAME" required:"yes"`
176 } `positional-args:"yes"`
177}
178
179type DevicePmConfigGroupEnable struct {
180 Args struct {
181 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
182 Groups []GroupName `positional-arg-name:"GROUP_NAME" required:"yes"`
183 } `positional-args:"yes"`
184}
185
186type DevicePmConfigGroupDisable struct {
187 Args struct {
188 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
189 Groups []GroupName `positional-arg-name:"GROUP_NAME" required:"yes"`
190 } `positional-args:"yes"`
191}
192
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -0800193type DeviceGetExtValue struct {
194 ListOutputOptions
195 Args struct {
196 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
197 Valueflag ValueFlag `positional-arg-name:"VALUE_FLAG" required:"yes"`
198 } `positional-args:"yes"`
199}
Rohan Agrawald7df3772020-06-29 11:23:36 +0000200
201type DevicePmConfigSetMaxSkew struct {
202 Args struct {
203 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
204 MaxSkew uint32 `positional-arg-name:"MAX_SKEW" required:"yes"`
205 } `positional-args:"yes"`
206}
207
Zack Williamse940c7a2019-08-21 14:25:39 -0700208type DeviceOpts struct {
209 List DeviceList `command:"list"`
210 Create DeviceCreate `command:"create"`
211 Delete DeviceDelete `command:"delete"`
212 Enable DeviceEnable `command:"enable"`
213 Disable DeviceDisable `command:"disable"`
214 Flows DeviceFlowList `command:"flows"`
kesavand12cd8eb2020-01-20 22:25:22 -0500215 Port struct {
216 List DevicePortList `command:"list"`
217 Enable DevicePortEnable `command:"enable"`
218 Disable DevicePortDisable `command:"disable"`
219 } `command:"port"`
220 Inspect DeviceInspect `command:"inspect"`
221 Reboot DeviceReboot `command:"reboot"`
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -0800222 Value struct {
223 Get DeviceGetExtValue `command:"get"`
224 } `command:"value"`
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000225 PmConfig struct {
Rohan Agrawald7df3772020-06-29 11:23:36 +0000226 Get DevicePmConfigsGet `command:"get"`
227 MaxSkew struct {
228 Set DevicePmConfigSetMaxSkew `command:"set"`
229 } `command:"maxskew"`
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000230 Frequency struct {
231 Set DevicePmConfigFrequencySet `command:"set"`
232 } `command:"frequency"`
233 Metric struct {
234 List DevicePmConfigMetricList `command:"list"`
235 Enable DevicePmConfigMetricEnable `command:"enable"`
236 Disable DevicePmConfigMetricDisable `command:"disable"`
237 } `command:"metric"`
238 Group struct {
239 List DevicePmConfigGroupList `command:"list"`
240 Enable DevicePmConfigGroupEnable `command:"enable"`
241 Disable DevicePmConfigGroupDisable `command:"disable"`
242 } `command:"group"`
243 GroupMetric struct {
244 List DevicePmConfigGroupMetricList `command:"list"`
245 } `command:"groupmetric"`
246 } `command:"pmconfig"`
Zack Williamse940c7a2019-08-21 14:25:39 -0700247}
248
249var deviceOpts = DeviceOpts{}
250
251func RegisterDeviceCommands(parser *flags.Parser) {
David Bainbridge12f036f2019-10-15 22:09:04 +0000252 if _, err := parser.AddCommand("device", "device commands", "Commands to query and manipulate VOLTHA devices", &deviceOpts); err != nil {
David Bainbridgea6722342019-10-24 23:55:53 +0000253 Error.Fatalf("Unexpected error while attempting to register device commands : %s", err)
David Bainbridge12f036f2019-10-15 22:09:04 +0000254 }
Zack Williamse940c7a2019-08-21 14:25:39 -0700255}
256
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000257func (i *MetricName) Complete(match string) []flags.Completion {
258 conn, err := NewConnection()
259 if err != nil {
260 return nil
261 }
262 defer conn.Close()
263
264 client := voltha.NewVolthaServiceClient(conn)
265
266 var deviceId string
267found:
268 for i := len(os.Args) - 1; i >= 0; i -= 1 {
269 switch os.Args[i] {
270 case "enable":
271 fallthrough
272 case "disable":
273 if len(os.Args) > i+1 {
274 deviceId = os.Args[i+1]
275 } else {
276 return nil
277 }
278 break found
279 default:
280 }
281 }
282
283 if len(deviceId) == 0 {
284 return nil
285 }
286
287 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
288 defer cancel()
289
290 id := voltha.ID{Id: string(deviceId)}
291
292 pmconfigs, err := client.ListDevicePmConfigs(ctx, &id)
293
294 if err != nil {
295 return nil
296 }
297
298 list := make([]flags.Completion, 0)
299 for _, metrics := range pmconfigs.Metrics {
300 if strings.HasPrefix(metrics.Name, match) {
301 list = append(list, flags.Completion{Item: metrics.Name})
302 }
303 }
304
305 return list
306}
307
308func (i *GroupName) Complete(match string) []flags.Completion {
309 conn, err := NewConnection()
310 if err != nil {
311 return nil
312 }
313 defer conn.Close()
314
315 client := voltha.NewVolthaServiceClient(conn)
316
317 var deviceId string
318found:
319 for i := len(os.Args) - 1; i >= 0; i -= 1 {
320 switch os.Args[i] {
321 case "list":
322 fallthrough
323 case "enable":
324 fallthrough
325 case "disable":
326 if len(os.Args) > i+1 {
327 deviceId = os.Args[i+1]
328 } else {
329 return nil
330 }
331 break found
332 default:
333 }
334 }
335
336 if len(deviceId) == 0 {
337 return nil
338 }
339
340 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
341 defer cancel()
342
343 id := voltha.ID{Id: string(deviceId)}
344
345 pmconfigs, err := client.ListDevicePmConfigs(ctx, &id)
346
347 if err != nil {
348 return nil
349 }
350
351 list := make([]flags.Completion, 0)
352 for _, group := range pmconfigs.Groups {
353 if strings.HasPrefix(group.GroupName, match) {
354 list = append(list, flags.Completion{Item: group.GroupName})
355 }
356 }
357 return list
358}
359
kesavand12cd8eb2020-01-20 22:25:22 -0500360func (i *PortNum) Complete(match string) []flags.Completion {
361 conn, err := NewConnection()
362 if err != nil {
363 return nil
364 }
365 defer conn.Close()
366
Scott Baker9173ed82020-05-19 08:30:12 -0700367 client := voltha.NewVolthaServiceClient(conn)
kesavand12cd8eb2020-01-20 22:25:22 -0500368
369 /*
370 * The command line args when completing for PortNum will be a DeviceId
371 * followed by one or more PortNums. So walk the argument list from the
372 * end and find the first argument that is enable/disable as those are
373 * the subcommands that come before the positional arguments. It would
374 * be nice if this package gave us the list of optional arguments
375 * already parsed.
376 */
377 var deviceId string
378found:
379 for i := len(os.Args) - 1; i >= 0; i -= 1 {
380 switch os.Args[i] {
381 case "enable":
382 fallthrough
383 case "disable":
384 if len(os.Args) > i+1 {
385 deviceId = os.Args[i+1]
386 } else {
387 return nil
388 }
389 break found
390 default:
391 }
392 }
393
394 if len(deviceId) == 0 {
395 return nil
396 }
397
kesavand12cd8eb2020-01-20 22:25:22 -0500398 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
399 defer cancel()
kesavand12cd8eb2020-01-20 22:25:22 -0500400
Scott Baker9173ed82020-05-19 08:30:12 -0700401 id := voltha.ID{Id: string(deviceId)}
kesavand12cd8eb2020-01-20 22:25:22 -0500402
Scott Baker9173ed82020-05-19 08:30:12 -0700403 ports, err := client.ListDevicePorts(ctx, &id)
kesavand12cd8eb2020-01-20 22:25:22 -0500404 if err != nil {
405 return nil
406 }
407
408 list := make([]flags.Completion, 0)
Scott Baker9173ed82020-05-19 08:30:12 -0700409 for _, item := range ports.Items {
410 pn := strconv.FormatUint(uint64(item.PortNo), 10)
kesavand12cd8eb2020-01-20 22:25:22 -0500411 if strings.HasPrefix(pn, match) {
412 list = append(list, flags.Completion{Item: pn})
413 }
414 }
415
416 return list
417}
418
Zack Williamse940c7a2019-08-21 14:25:39 -0700419func (i *DeviceId) Complete(match string) []flags.Completion {
420 conn, err := NewConnection()
421 if err != nil {
422 return nil
423 }
424 defer conn.Close()
425
Scott Baker9173ed82020-05-19 08:30:12 -0700426 client := voltha.NewVolthaServiceClient(conn)
Zack Williamse940c7a2019-08-21 14:25:39 -0700427
428 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
429 defer cancel()
430
Scott Baker9173ed82020-05-19 08:30:12 -0700431 devices, err := client.ListDevices(ctx, &empty.Empty{})
Zack Williamse940c7a2019-08-21 14:25:39 -0700432 if err != nil {
433 return nil
434 }
435
436 list := make([]flags.Completion, 0)
Scott Baker9173ed82020-05-19 08:30:12 -0700437 for _, item := range devices.Items {
438 if strings.HasPrefix(item.Id, match) {
439 list = append(list, flags.Completion{Item: item.Id})
Zack Williamse940c7a2019-08-21 14:25:39 -0700440 }
441 }
442
443 return list
444}
445
446func (options *DeviceList) Execute(args []string) error {
447
448 conn, err := NewConnection()
449 if err != nil {
450 return err
451 }
452 defer conn.Close()
453
Scott Baker9173ed82020-05-19 08:30:12 -0700454 client := voltha.NewVolthaServiceClient(conn)
Zack Williamse940c7a2019-08-21 14:25:39 -0700455
456 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
457 defer cancel()
458
Scott Baker9173ed82020-05-19 08:30:12 -0700459 devices, err := client.ListDevices(ctx, &empty.Empty{})
Zack Williamse940c7a2019-08-21 14:25:39 -0700460 if err != nil {
461 return err
462 }
463
464 outputFormat := CharReplacer.Replace(options.Format)
465 if outputFormat == "" {
David Bainbridgea6722342019-10-24 23:55:53 +0000466 outputFormat = GetCommandOptionWithDefault("device-list", "format", DEFAULT_DEVICE_FORMAT)
Zack Williamse940c7a2019-08-21 14:25:39 -0700467 }
468 if options.Quiet {
469 outputFormat = "{{.Id}}"
470 }
471
David Bainbridgea6722342019-10-24 23:55:53 +0000472 orderBy := options.OrderBy
473 if orderBy == "" {
474 orderBy = GetCommandOptionWithDefault("device-list", "order", "")
475 }
476
Scott Baker9173ed82020-05-19 08:30:12 -0700477 // Make sure json output prints an empty list, not "null"
478 if devices.Items == nil {
479 devices.Items = make([]*voltha.Device, 0)
Zack Williamse940c7a2019-08-21 14:25:39 -0700480 }
481
482 result := CommandResult{
483 Format: format.Format(outputFormat),
484 Filter: options.Filter,
David Bainbridgea6722342019-10-24 23:55:53 +0000485 OrderBy: orderBy,
Zack Williamse940c7a2019-08-21 14:25:39 -0700486 OutputAs: toOutputType(options.OutputAs),
487 NameLimit: options.NameLimit,
Scott Baker9173ed82020-05-19 08:30:12 -0700488 Data: devices.Items,
Zack Williamse940c7a2019-08-21 14:25:39 -0700489 }
490
491 GenerateOutput(&result)
492 return nil
493}
494
495func (options *DeviceCreate) Execute(args []string) error {
496
Scott Baker9173ed82020-05-19 08:30:12 -0700497 device := voltha.Device{}
Zack Williamse940c7a2019-08-21 14:25:39 -0700498 if options.HostAndPort != "" {
Scott Baker9173ed82020-05-19 08:30:12 -0700499 device.Address = &voltha.Device_HostAndPort{HostAndPort: options.HostAndPort}
Zack Williamse940c7a2019-08-21 14:25:39 -0700500 } else if options.IPAddress != "" {
Scott Baker9173ed82020-05-19 08:30:12 -0700501 device.Address = &voltha.Device_Ipv4Address{Ipv4Address: options.IPAddress}
Hardik Windlassce1de342020-02-04 21:58:07 +0000502 }
503 if options.MACAddress != "" {
Scott Baker9173ed82020-05-19 08:30:12 -0700504 device.MacAddress = strings.ToLower(options.MACAddress)
Zack Williamse940c7a2019-08-21 14:25:39 -0700505 }
506 if options.DeviceType != "" {
Scott Baker9173ed82020-05-19 08:30:12 -0700507 device.Type = options.DeviceType
Zack Williamse940c7a2019-08-21 14:25:39 -0700508 }
509
510 conn, err := NewConnection()
511 if err != nil {
512 return err
513 }
514 defer conn.Close()
515
Scott Baker9173ed82020-05-19 08:30:12 -0700516 client := voltha.NewVolthaServiceClient(conn)
Zack Williamse940c7a2019-08-21 14:25:39 -0700517
518 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
519 defer cancel()
520
Scott Baker9173ed82020-05-19 08:30:12 -0700521 createdDevice, err := client.CreateDevice(ctx, &device)
Zack Williamse940c7a2019-08-21 14:25:39 -0700522 if err != nil {
523 return err
Zack Williamse940c7a2019-08-21 14:25:39 -0700524 }
525
Scott Baker9173ed82020-05-19 08:30:12 -0700526 fmt.Printf("%s\n", createdDevice.Id)
Zack Williamse940c7a2019-08-21 14:25:39 -0700527
528 return nil
529}
530
531func (options *DeviceDelete) Execute(args []string) error {
532
533 conn, err := NewConnection()
534 if err != nil {
535 return err
536 }
537 defer conn.Close()
538
Scott Baker9173ed82020-05-19 08:30:12 -0700539 client := voltha.NewVolthaServiceClient(conn)
Zack Williamse940c7a2019-08-21 14:25:39 -0700540
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)}
547
548 _, err := client.DeleteDevice(ctx, &id)
Zack Williamse940c7a2019-08-21 14:25:39 -0700549 if err != nil {
David Bainbridge0f758d42019-10-26 05:17:48 +0000550 Error.Printf("Error while deleting '%s': %s\n", i, err)
David Bainbridge7052fe82020-03-25 10:37:00 -0700551 lastErr = err
Zack Williamse940c7a2019-08-21 14:25:39 -0700552 continue
Zack Williamse940c7a2019-08-21 14:25:39 -0700553 }
554 fmt.Printf("%s\n", i)
555 }
556
David Bainbridge7052fe82020-03-25 10:37:00 -0700557 if lastErr != nil {
558 return NoReportErr
559 }
Zack Williamse940c7a2019-08-21 14:25:39 -0700560 return nil
561}
562
563func (options *DeviceEnable) Execute(args []string) error {
564 conn, err := NewConnection()
565 if err != nil {
566 return err
567 }
568 defer conn.Close()
569
Scott Baker9173ed82020-05-19 08:30:12 -0700570 client := voltha.NewVolthaServiceClient(conn)
Zack Williamse940c7a2019-08-21 14:25:39 -0700571
David Bainbridge7052fe82020-03-25 10:37:00 -0700572 var lastErr error
Zack Williamse940c7a2019-08-21 14:25:39 -0700573 for _, i := range options.Args.Ids {
Zack Williamse940c7a2019-08-21 14:25:39 -0700574 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
575 defer cancel()
576
Scott Baker9173ed82020-05-19 08:30:12 -0700577 id := voltha.ID{Id: string(i)}
578
579 _, err := client.EnableDevice(ctx, &id)
Zack Williamse940c7a2019-08-21 14:25:39 -0700580 if err != nil {
David Bainbridge0f758d42019-10-26 05:17:48 +0000581 Error.Printf("Error while enabling '%s': %s\n", i, err)
David Bainbridge7052fe82020-03-25 10:37:00 -0700582 lastErr = err
Zack Williamse940c7a2019-08-21 14:25:39 -0700583 continue
Zack Williamse940c7a2019-08-21 14:25:39 -0700584 }
585 fmt.Printf("%s\n", i)
586 }
587
David Bainbridge7052fe82020-03-25 10:37:00 -0700588 if lastErr != nil {
589 return NoReportErr
590 }
Zack Williamse940c7a2019-08-21 14:25:39 -0700591 return nil
592}
593
594func (options *DeviceDisable) Execute(args []string) error {
595 conn, err := NewConnection()
596 if err != nil {
597 return err
598 }
599 defer conn.Close()
600
Scott Baker9173ed82020-05-19 08:30:12 -0700601 client := voltha.NewVolthaServiceClient(conn)
Zack Williamse940c7a2019-08-21 14:25:39 -0700602
David Bainbridge7052fe82020-03-25 10:37:00 -0700603 var lastErr error
Zack Williamse940c7a2019-08-21 14:25:39 -0700604 for _, i := range options.Args.Ids {
Zack Williamse940c7a2019-08-21 14:25:39 -0700605 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
606 defer cancel()
607
Scott Baker9173ed82020-05-19 08:30:12 -0700608 id := voltha.ID{Id: string(i)}
609
610 _, err := client.DisableDevice(ctx, &id)
Zack Williamse940c7a2019-08-21 14:25:39 -0700611 if err != nil {
David Bainbridge0f758d42019-10-26 05:17:48 +0000612 Error.Printf("Error while disabling '%s': %s\n", i, err)
David Bainbridge7052fe82020-03-25 10:37:00 -0700613 lastErr = err
Zack Williamse940c7a2019-08-21 14:25:39 -0700614 continue
Zack Williamse940c7a2019-08-21 14:25:39 -0700615 }
616 fmt.Printf("%s\n", i)
617 }
618
David Bainbridge7052fe82020-03-25 10:37:00 -0700619 if lastErr != nil {
620 return NoReportErr
621 }
Zack Williamse940c7a2019-08-21 14:25:39 -0700622 return nil
623}
624
625func (options *DeviceReboot) Execute(args []string) error {
626 conn, err := NewConnection()
627 if err != nil {
628 return err
629 }
630 defer conn.Close()
631
Scott Baker9173ed82020-05-19 08:30:12 -0700632 client := voltha.NewVolthaServiceClient(conn)
Zack Williamse940c7a2019-08-21 14:25:39 -0700633
David Bainbridge7052fe82020-03-25 10:37:00 -0700634 var lastErr error
Zack Williamse940c7a2019-08-21 14:25:39 -0700635 for _, i := range options.Args.Ids {
Zack Williamse940c7a2019-08-21 14:25:39 -0700636 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
637 defer cancel()
638
Scott Baker9173ed82020-05-19 08:30:12 -0700639 id := voltha.ID{Id: string(i)}
640
641 _, err := client.RebootDevice(ctx, &id)
Zack Williamse940c7a2019-08-21 14:25:39 -0700642 if err != nil {
David Bainbridge0f758d42019-10-26 05:17:48 +0000643 Error.Printf("Error while rebooting '%s': %s\n", i, err)
David Bainbridge7052fe82020-03-25 10:37:00 -0700644 lastErr = err
Zack Williamse940c7a2019-08-21 14:25:39 -0700645 continue
Zack Williamse940c7a2019-08-21 14:25:39 -0700646 }
647 fmt.Printf("%s\n", i)
648 }
649
David Bainbridge7052fe82020-03-25 10:37:00 -0700650 if lastErr != nil {
651 return NoReportErr
652 }
Zack Williamse940c7a2019-08-21 14:25:39 -0700653 return nil
654}
655
656func (options *DevicePortList) Execute(args []string) error {
657
658 conn, err := NewConnection()
659 if err != nil {
660 return err
661 }
662 defer conn.Close()
663
Scott Baker9173ed82020-05-19 08:30:12 -0700664 client := voltha.NewVolthaServiceClient(conn)
Zack Williamse940c7a2019-08-21 14:25:39 -0700665
666 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
667 defer cancel()
668
Scott Baker9173ed82020-05-19 08:30:12 -0700669 id := voltha.ID{Id: string(options.Args.Id)}
Zack Williamse940c7a2019-08-21 14:25:39 -0700670
Scott Baker9173ed82020-05-19 08:30:12 -0700671 ports, err := client.ListDevicePorts(ctx, &id)
Zack Williamse940c7a2019-08-21 14:25:39 -0700672 if err != nil {
673 return err
674 }
675
676 outputFormat := CharReplacer.Replace(options.Format)
677 if outputFormat == "" {
David Bainbridgea6722342019-10-24 23:55:53 +0000678 outputFormat = GetCommandOptionWithDefault("device-ports", "format", DEFAULT_DEVICE_PORTS_FORMAT)
Zack Williamse940c7a2019-08-21 14:25:39 -0700679 }
Zack Williamse940c7a2019-08-21 14:25:39 -0700680
David Bainbridgea6722342019-10-24 23:55:53 +0000681 orderBy := options.OrderBy
682 if orderBy == "" {
683 orderBy = GetCommandOptionWithDefault("device-ports", "order", "")
684 }
685
Zack Williamse940c7a2019-08-21 14:25:39 -0700686 result := CommandResult{
687 Format: format.Format(outputFormat),
688 Filter: options.Filter,
David Bainbridgea6722342019-10-24 23:55:53 +0000689 OrderBy: orderBy,
Zack Williamse940c7a2019-08-21 14:25:39 -0700690 OutputAs: toOutputType(options.OutputAs),
691 NameLimit: options.NameLimit,
Scott Baker9173ed82020-05-19 08:30:12 -0700692 Data: ports.Items,
Zack Williamse940c7a2019-08-21 14:25:39 -0700693 }
694
695 GenerateOutput(&result)
696 return nil
697}
698
699func (options *DeviceFlowList) Execute(args []string) error {
700 fl := &FlowList{}
701 fl.ListOutputOptions = options.ListOutputOptions
Maninder045921e2020-09-29 16:46:02 +0530702 fl.FlowIdOptions = options.FlowIdOptions
Zack Williamse940c7a2019-08-21 14:25:39 -0700703 fl.Args.Id = string(options.Args.Id)
David Bainbridgea6722342019-10-24 23:55:53 +0000704 fl.Method = "device-flows"
Zack Williamse940c7a2019-08-21 14:25:39 -0700705 return fl.Execute(args)
706}
707
708func (options *DeviceInspect) Execute(args []string) error {
709 if len(args) > 0 {
710 return fmt.Errorf("only a single argument 'DEVICE_ID' can be provided")
711 }
712
713 conn, err := NewConnection()
714 if err != nil {
715 return err
716 }
717 defer conn.Close()
718
Scott Baker9173ed82020-05-19 08:30:12 -0700719 client := voltha.NewVolthaServiceClient(conn)
Zack Williamse940c7a2019-08-21 14:25:39 -0700720
721 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
722 defer cancel()
723
Scott Baker9173ed82020-05-19 08:30:12 -0700724 id := voltha.ID{Id: string(options.Args.Id)}
Zack Williamse940c7a2019-08-21 14:25:39 -0700725
Scott Baker9173ed82020-05-19 08:30:12 -0700726 device, err := client.GetDevice(ctx, &id)
Zack Williamse940c7a2019-08-21 14:25:39 -0700727 if err != nil {
728 return err
729 }
730
Zack Williamse940c7a2019-08-21 14:25:39 -0700731 outputFormat := CharReplacer.Replace(options.Format)
732 if outputFormat == "" {
David Bainbridgea6722342019-10-24 23:55:53 +0000733 outputFormat = GetCommandOptionWithDefault("device-inspect", "format", DEFAULT_DEVICE_INSPECT_FORMAT)
Zack Williamse940c7a2019-08-21 14:25:39 -0700734 }
735 if options.Quiet {
736 outputFormat = "{{.Id}}"
737 }
738
739 result := CommandResult{
740 Format: format.Format(outputFormat),
741 OutputAs: toOutputType(options.OutputAs),
742 NameLimit: options.NameLimit,
743 Data: device,
744 }
745 GenerateOutput(&result)
746 return nil
747}
kesavand12cd8eb2020-01-20 22:25:22 -0500748
749/*Device Port Enable */
750func (options *DevicePortEnable) Execute(args []string) error {
751 conn, err := NewConnection()
752 if err != nil {
753 return err
754 }
755 defer conn.Close()
756
Scott Baker9173ed82020-05-19 08:30:12 -0700757 client := voltha.NewVolthaServiceClient(conn)
kesavand12cd8eb2020-01-20 22:25:22 -0500758
kesavand12cd8eb2020-01-20 22:25:22 -0500759 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
760 defer cancel()
761
Scott Baker9173ed82020-05-19 08:30:12 -0700762 port := voltha.Port{DeviceId: string(options.Args.Id), PortNo: uint32(options.Args.PortId)}
763
764 _, err = client.EnablePort(ctx, &port)
kesavand12cd8eb2020-01-20 22:25:22 -0500765 if err != nil {
766 Error.Printf("Error enabling port number %v on device Id %s,err=%s\n", options.Args.PortId, options.Args.Id, ErrorToString(err))
767 return err
kesavand12cd8eb2020-01-20 22:25:22 -0500768 }
769
770 return nil
771}
772
Scott Baker9173ed82020-05-19 08:30:12 -0700773/*Device Port Disable */
kesavand12cd8eb2020-01-20 22:25:22 -0500774func (options *DevicePortDisable) Execute(args []string) error {
775 conn, err := NewConnection()
776 if err != nil {
777 return err
778 }
779 defer conn.Close()
780
Scott Baker9173ed82020-05-19 08:30:12 -0700781 client := voltha.NewVolthaServiceClient(conn)
782
kesavand12cd8eb2020-01-20 22:25:22 -0500783 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
784 defer cancel()
785
Scott Baker9173ed82020-05-19 08:30:12 -0700786 port := voltha.Port{DeviceId: string(options.Args.Id), PortNo: uint32(options.Args.PortId)}
787
788 _, err = client.DisablePort(ctx, &port)
kesavand12cd8eb2020-01-20 22:25:22 -0500789 if err != nil {
Scott Baker9173ed82020-05-19 08:30:12 -0700790 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 -0500791 return err
kesavand12cd8eb2020-01-20 22:25:22 -0500792 }
Scott Baker9173ed82020-05-19 08:30:12 -0700793
kesavand12cd8eb2020-01-20 22:25:22 -0500794 return nil
795}
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -0800796
Rohan Agrawald7df3772020-06-29 11:23:36 +0000797func (options *DevicePmConfigSetMaxSkew) Execute(args []string) error {
798 conn, err := NewConnection()
799 if err != nil {
800 return err
801 }
802 defer conn.Close()
803
804 client := voltha.NewVolthaServiceClient(conn)
805
806 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
807 defer cancel()
808
809 id := voltha.ID{Id: string(options.Args.Id)}
810
811 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
812 if err != nil {
813 return err
814 }
815
816 pmConfigs.MaxSkew = options.Args.MaxSkew
817
818 _, err = client.UpdateDevicePmConfigs(ctx, pmConfigs)
819 if err != nil {
820 return err
821 }
822
823 return nil
824}
825
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000826func (options *DevicePmConfigsGet) Execute(args []string) error {
827
828 conn, err := NewConnection()
829 if err != nil {
830 return err
831 }
832 defer conn.Close()
833
834 client := voltha.NewVolthaServiceClient(conn)
835
836 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
837 defer cancel()
838
839 id := voltha.ID{Id: string(options.Args.Id)}
840
841 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
842 if err != nil {
843 return err
844 }
845
846 outputFormat := CharReplacer.Replace(options.Format)
847 if outputFormat == "" {
848 outputFormat = GetCommandOptionWithDefault("device-pm-configs", "format", DEFAULT_DEVICE_PM_CONFIG_GET_FORMAT)
849 }
850
851 orderBy := options.OrderBy
852 if orderBy == "" {
853 orderBy = GetCommandOptionWithDefault("device-pm-configs", "order", "")
854 }
855
856 result := CommandResult{
857 Format: format.Format(outputFormat),
858 Filter: options.Filter,
859 OrderBy: orderBy,
860 OutputAs: toOutputType(options.OutputAs),
861 NameLimit: options.NameLimit,
862 Data: pmConfigs,
863 }
864
865 GenerateOutput(&result)
866 return nil
867
868}
869
870func (options *DevicePmConfigMetricList) Execute(args []string) error {
871
872 conn, err := NewConnection()
873 if err != nil {
874 return err
875 }
876 defer conn.Close()
877
878 client := voltha.NewVolthaServiceClient(conn)
879
880 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
881 defer cancel()
882
883 id := voltha.ID{Id: string(options.Args.Id)}
884
885 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
886 if err != nil {
887 return err
888 }
889
890 if !pmConfigs.Grouped {
891 for _, metric := range pmConfigs.Metrics {
892 if metric.SampleFreq == 0 {
893 metric.SampleFreq = pmConfigs.DefaultFreq
894 }
895 }
Rohan Agrawalbca69122020-06-17 14:59:03 +0000896 outputFormat := CharReplacer.Replace(options.Format)
897 if outputFormat == "" {
898 outputFormat = GetCommandOptionWithDefault("device-pm-configs", "format", DEFAULT_DEVICE_PM_CONFIG_METRIC_LIST_FORMAT)
899 }
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000900
Rohan Agrawalbca69122020-06-17 14:59:03 +0000901 orderBy := options.OrderBy
902 if orderBy == "" {
903 orderBy = GetCommandOptionWithDefault("device-pm-configs", "order", "")
904 }
905
906 result := CommandResult{
907 Format: format.Format(outputFormat),
908 Filter: options.Filter,
909 OrderBy: orderBy,
910 OutputAs: toOutputType(options.OutputAs),
911 NameLimit: options.NameLimit,
912 Data: pmConfigs.Metrics,
913 }
914
915 GenerateOutput(&result)
916 return nil
917 } else {
918 return fmt.Errorf("Device '%s' does not have Non Grouped Metrics", options.Args.Id)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000919 }
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000920}
921
922func (options *DevicePmConfigMetricEnable) Execute(args []string) error {
923
924 conn, err := NewConnection()
925 if err != nil {
926 return err
927 }
928 defer conn.Close()
929
930 client := voltha.NewVolthaServiceClient(conn)
931
932 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
933 defer cancel()
934
935 id := voltha.ID{Id: string(options.Args.Id)}
936
937 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
938 if err != nil {
939 return err
940 }
941
942 if !pmConfigs.Grouped {
Rohan Agrawalbca69122020-06-17 14:59:03 +0000943 metrics := make(map[string]struct{})
944 for _, metric := range pmConfigs.Metrics {
945 metrics[metric.Name] = struct{}{}
946 }
947
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000948 for _, metric := range pmConfigs.Metrics {
949 for _, mName := range options.Args.Metrics {
Rohan Agrawalbca69122020-06-17 14:59:03 +0000950 if _, exist := metrics[string(mName)]; !exist {
951 return fmt.Errorf("Metric Name '%s' does not exist", mName)
952 }
953
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000954 if string(mName) == metric.Name && !metric.Enabled {
955 metric.Enabled = true
956 _, err := client.UpdateDevicePmConfigs(ctx, pmConfigs)
957 if err != nil {
958 return err
959 }
960 }
961 }
962 }
Rohan Agrawalbca69122020-06-17 14:59:03 +0000963 } else {
964 return fmt.Errorf("Device '%s' does not have Non Grouped Metrics", options.Args.Id)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000965 }
966 return nil
967}
968
969func (options *DevicePmConfigMetricDisable) Execute(args []string) error {
970
971 conn, err := NewConnection()
972 if err != nil {
973 return err
974 }
975 defer conn.Close()
976
977 client := voltha.NewVolthaServiceClient(conn)
978
979 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
980 defer cancel()
981
982 id := voltha.ID{Id: string(options.Args.Id)}
983
984 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
985 if err != nil {
986 return err
987 }
988
989 if !pmConfigs.Grouped {
Rohan Agrawalbca69122020-06-17 14:59:03 +0000990 metrics := make(map[string]struct{})
991 for _, metric := range pmConfigs.Metrics {
992 metrics[metric.Name] = struct{}{}
993 }
994
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000995 for _, metric := range pmConfigs.Metrics {
996 for _, mName := range options.Args.Metrics {
Rohan Agrawalbca69122020-06-17 14:59:03 +0000997 if _, have := metrics[string(mName)]; !have {
998 return fmt.Errorf("Metric Name '%s' does not exist", mName)
999 }
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001000 if string(mName) == metric.Name && metric.Enabled {
1001 metric.Enabled = false
1002 _, err := client.UpdateDevicePmConfigs(ctx, pmConfigs)
1003 if err != nil {
1004 return err
1005 }
Rohan Agrawalbca69122020-06-17 14:59:03 +00001006 } else {
1007 return fmt.Errorf("Metric '%s' cannot be disabled", string(mName))
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001008 }
1009 }
1010 }
Rohan Agrawalbca69122020-06-17 14:59:03 +00001011 } else {
1012 return fmt.Errorf("Device '%s' does not have Non Grouped Metrics", options.Args.Id)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001013 }
1014 return nil
1015}
1016
1017func (options *DevicePmConfigGroupEnable) Execute(args []string) error {
1018
1019 conn, err := NewConnection()
1020 if err != nil {
1021 return err
1022 }
1023 defer conn.Close()
1024
1025 client := voltha.NewVolthaServiceClient(conn)
1026
1027 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
1028 defer cancel()
1029
1030 id := voltha.ID{Id: string(options.Args.Id)}
1031
1032 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
1033 if err != nil {
1034 return err
1035 }
1036
1037 if pmConfigs.Grouped {
Rohan Agrawalbca69122020-06-17 14:59:03 +00001038 groups := make(map[string]struct{})
1039 for _, group := range pmConfigs.Groups {
1040 groups[group.GroupName] = struct{}{}
1041 }
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001042 for _, group := range pmConfigs.Groups {
1043 for _, gName := range options.Args.Groups {
Rohan Agrawalbca69122020-06-17 14:59:03 +00001044 if _, have := groups[string(gName)]; !have {
1045 return fmt.Errorf("Group Name '%s' does not exist", gName)
1046 }
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001047 if string(gName) == group.GroupName && !group.Enabled {
1048 group.Enabled = true
1049 _, err := client.UpdateDevicePmConfigs(ctx, pmConfigs)
1050 if err != nil {
1051 return err
1052 }
1053 }
1054 }
1055 }
Rohan Agrawalbca69122020-06-17 14:59:03 +00001056 } else {
1057 return fmt.Errorf("Device '%s' does not have Group Metrics", options.Args.Id)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001058 }
1059 return nil
1060}
1061
1062func (options *DevicePmConfigGroupDisable) Execute(args []string) error {
1063
1064 conn, err := NewConnection()
1065 if err != nil {
1066 return err
1067 }
1068 defer conn.Close()
1069
1070 client := voltha.NewVolthaServiceClient(conn)
1071
1072 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
1073 defer cancel()
1074
1075 id := voltha.ID{Id: string(options.Args.Id)}
1076
1077 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
1078 if err != nil {
1079 return err
1080 }
1081
1082 if pmConfigs.Grouped {
Rohan Agrawalbca69122020-06-17 14:59:03 +00001083 groups := make(map[string]struct{})
1084 for _, group := range pmConfigs.Groups {
1085 groups[group.GroupName] = struct{}{}
1086 }
1087
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001088 for _, group := range pmConfigs.Groups {
1089 for _, gName := range options.Args.Groups {
Rohan Agrawalbca69122020-06-17 14:59:03 +00001090 if _, have := groups[string(gName)]; !have {
1091 return fmt.Errorf("Group Name '%s' does not exist", gName)
1092 }
1093
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001094 if string(gName) == group.GroupName && group.Enabled {
1095 group.Enabled = false
1096 _, err := client.UpdateDevicePmConfigs(ctx, pmConfigs)
1097 if err != nil {
1098 return err
1099 }
1100 }
1101 }
1102 }
Rohan Agrawalbca69122020-06-17 14:59:03 +00001103 } else {
1104 return fmt.Errorf("Device '%s' does not have Group Metrics", options.Args.Id)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001105 }
1106 return nil
1107}
1108
1109func (options *DevicePmConfigGroupList) Execute(args []string) error {
1110
1111 conn, err := NewConnection()
1112 if err != nil {
1113 return err
1114 }
1115 defer conn.Close()
1116
1117 client := voltha.NewVolthaServiceClient(conn)
1118
1119 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
1120 defer cancel()
1121
1122 id := voltha.ID{Id: string(options.Args.Id)}
1123
1124 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
1125 if err != nil {
1126 return err
1127 }
1128
1129 if pmConfigs.Grouped {
1130 for _, group := range pmConfigs.Groups {
1131 if group.GroupFreq == 0 {
1132 group.GroupFreq = pmConfigs.DefaultFreq
1133 }
1134 }
Rohan Agrawalbca69122020-06-17 14:59:03 +00001135 outputFormat := CharReplacer.Replace(options.Format)
1136 if outputFormat == "" {
1137 outputFormat = GetCommandOptionWithDefault("device-pm-configs", "format", DEFAULT_DEVICE_PM_CONFIG_GROUP_LIST_FORMAT)
1138 }
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001139
Rohan Agrawalbca69122020-06-17 14:59:03 +00001140 orderBy := options.OrderBy
1141 if orderBy == "" {
1142 orderBy = GetCommandOptionWithDefault("device-pm-configs", "order", "")
1143 }
1144
1145 result := CommandResult{
1146 Format: format.Format(outputFormat),
1147 Filter: options.Filter,
1148 OrderBy: orderBy,
1149 OutputAs: toOutputType(options.OutputAs),
1150 NameLimit: options.NameLimit,
1151 Data: pmConfigs.Groups,
1152 }
1153
1154 GenerateOutput(&result)
1155 } else {
1156 return fmt.Errorf("Device '%s' does not have Group Metrics", string(options.Args.Id))
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001157 }
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001158 return nil
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001159}
1160
1161func (options *DevicePmConfigGroupMetricList) Execute(args []string) error {
1162
1163 var metrics []*voltha.PmConfig
1164 conn, err := NewConnection()
1165 if err != nil {
1166 return err
1167 }
1168 defer conn.Close()
1169
1170 client := voltha.NewVolthaServiceClient(conn)
1171
1172 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
1173 defer cancel()
1174
1175 id := voltha.ID{Id: string(options.Args.Id)}
1176
1177 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
1178 if err != nil {
1179 return err
1180 }
1181
1182 for _, groups := range pmConfigs.Groups {
1183
1184 if string(options.Args.Group) == groups.GroupName {
1185 for _, metric := range groups.Metrics {
1186 if metric.SampleFreq == 0 && groups.GroupFreq == 0 {
1187 metric.SampleFreq = pmConfigs.DefaultFreq
1188 } else {
1189 metric.SampleFreq = groups.GroupFreq
1190 }
1191 }
1192 metrics = groups.Metrics
1193 }
1194 }
1195
1196 outputFormat := CharReplacer.Replace(options.Format)
1197 if outputFormat == "" {
1198 outputFormat = GetCommandOptionWithDefault("device-pm-configs", "format", DEFAULT_DEVICE_PM_CONFIG_METRIC_LIST_FORMAT)
1199 }
1200
1201 orderBy := options.OrderBy
1202 if orderBy == "" {
1203 orderBy = GetCommandOptionWithDefault("device-pm-configs", "order", "")
1204 }
1205
1206 result := CommandResult{
1207 Format: format.Format(outputFormat),
1208 Filter: options.Filter,
1209 OrderBy: orderBy,
1210 OutputAs: toOutputType(options.OutputAs),
1211 NameLimit: options.NameLimit,
1212 Data: metrics,
1213 }
1214
1215 GenerateOutput(&result)
1216 return nil
1217
1218}
1219
1220func (options *DevicePmConfigFrequencySet) Execute(args []string) error {
1221
1222 conn, err := NewConnection()
1223 if err != nil {
1224 return err
1225 }
1226 defer conn.Close()
1227
1228 client := voltha.NewVolthaServiceClient(conn)
1229
1230 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
1231 defer cancel()
1232
1233 id := voltha.ID{Id: string(options.Args.Id)}
1234
1235 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
1236 if err != nil {
1237 return err
1238 }
1239
1240 pmConfigs.DefaultFreq = options.Args.Frequency
1241
1242 _, err = client.UpdateDevicePmConfigs(ctx, pmConfigs)
1243 if err != nil {
1244 return err
1245 }
1246
1247 outputFormat := CharReplacer.Replace(options.Format)
1248 if outputFormat == "" {
1249 outputFormat = GetCommandOptionWithDefault("device-pm-configs", "format", DEFAULT_DEVICE_PM_CONFIG_GET_FORMAT)
1250 }
1251 if options.Quiet {
1252 outputFormat = "{{.Id}}"
1253 }
1254
1255 result := CommandResult{
1256 Format: format.Format(outputFormat),
1257 OutputAs: toOutputType(options.OutputAs),
1258 NameLimit: options.NameLimit,
1259 Data: pmConfigs,
1260 }
1261
1262 GenerateOutput(&result)
1263 return nil
1264
1265}
1266
Scott Baker9173ed82020-05-19 08:30:12 -07001267type ReturnValueRow struct {
1268 Name string `json:"name"`
1269 Result interface{} `json:"result"`
1270}
1271
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -08001272/*Device get Onu Distance */
1273func (options *DeviceGetExtValue) Execute(args []string) error {
1274 conn, err := NewConnection()
1275 if err != nil {
1276 return err
1277 }
1278 defer conn.Close()
1279
Scott Baker9173ed82020-05-19 08:30:12 -07001280 client := voltha.NewVolthaServiceClient(conn)
1281
1282 valueflag, okay := common.ValueType_Type_value[string(options.Args.Valueflag)]
1283 if !okay {
1284 Error.Printf("Unknown valueflag %s\n", options.Args.Valueflag)
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -08001285 }
1286
Scott Baker9173ed82020-05-19 08:30:12 -07001287 val := voltha.ValueSpecifier{Id: string(options.Args.Id), Value: common.ValueType_Type(valueflag)}
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -08001288
1289 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
1290 defer cancel()
1291
Scott Baker9173ed82020-05-19 08:30:12 -07001292 rv, err := client.GetExtValue(ctx, &val)
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -08001293 if err != nil {
1294 Error.Printf("Error getting value on device Id %s,err=%s\n", options.Args.Id, ErrorToString(err))
1295 return err
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -08001296 }
1297
Scott Baker9173ed82020-05-19 08:30:12 -07001298 var rows []ReturnValueRow
1299 for name, num := range common.ValueType_Type_value {
1300 if num == 0 {
1301 // EMPTY is not a real value
1302 continue
1303 }
1304 if (rv.Error & uint32(num)) != 0 {
1305 row := ReturnValueRow{Name: name, Result: "Error"}
1306 rows = append(rows, row)
1307 }
1308 if (rv.Unsupported & uint32(num)) != 0 {
1309 row := ReturnValueRow{Name: name, Result: "Unsupported"}
1310 rows = append(rows, row)
1311 }
1312 if (rv.Set & uint32(num)) != 0 {
1313 switch name {
1314 case "DISTANCE":
1315 row := ReturnValueRow{Name: name, Result: rv.Distance}
1316 rows = append(rows, row)
1317 default:
1318 row := ReturnValueRow{Name: name, Result: "Unimplemented-in-voltctl"}
1319 rows = append(rows, row)
1320 }
1321 }
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -08001322 }
1323
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -08001324 outputFormat := CharReplacer.Replace(options.Format)
1325 if outputFormat == "" {
1326 outputFormat = GetCommandOptionWithDefault("device-value-get", "format", DEFAULT_DEVICE_VALUE_GET_FORMAT)
1327 }
1328
1329 result := CommandResult{
1330 Format: format.Format(outputFormat),
1331 OutputAs: toOutputType(options.OutputAs),
1332 NameLimit: options.NameLimit,
Scott Baker9173ed82020-05-19 08:30:12 -07001333 Data: rows,
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -08001334 }
1335 GenerateOutput(&result)
1336 return nil
1337}