blob: 416e1c5bacc7393a5f26441273c6c6c4f2e24fee [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}}"
Andrea Campanella791d88b2021-01-08 13:29:00 +010048 DEFAULT_DEVICE_IMAGE_LIST_GET_FORMAT = "table{{.Name}}\t{{.Url}}\t{{.Crc}}\t{{.DownloadState}}\t{{.ImageVersion}}\t{{.LocalDir}}\t{{.ImageState}}\t{{.FileSize}}"
Zack Williamse940c7a2019-08-21 14:25:39 -070049)
50
51type DeviceList struct {
52 ListOutputOptions
53}
54
55type DeviceCreate struct {
David Bainbridge1a514392020-06-23 11:12:51 -070056 DeviceType string `short:"t" required:"true" long:"devicetype" description:"Device type"`
David Bainbridge835dd0e2020-04-01 10:30:09 -070057 MACAddress string `short:"m" long:"macaddress" default:"" description:"MAC Address"`
Zack Williamse940c7a2019-08-21 14:25:39 -070058 IPAddress string `short:"i" long:"ipaddress" default:"" description:"IP Address"`
59 HostAndPort string `short:"H" long:"hostandport" default:"" description:"Host and port"`
60}
61
62type DeviceId string
63
Rohan Agrawal9228d2f2020-06-03 07:48:50 +000064type MetricName string
65type GroupName string
kesavand12cd8eb2020-01-20 22:25:22 -050066type PortNum uint32
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -080067type ValueFlag string
kesavand12cd8eb2020-01-20 22:25:22 -050068
Zack Williamse940c7a2019-08-21 14:25:39 -070069type DeviceDelete struct {
Himani Chawla9933ddc2020-10-12 23:53:27 +053070 Force bool `long:"force" description:"Delete device forcefully"`
71 Args struct {
Zack Williamse940c7a2019-08-21 14:25:39 -070072 Ids []DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
73 } `positional-args:"yes"`
74}
75
76type DeviceEnable struct {
77 Args struct {
78 Ids []DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
79 } `positional-args:"yes"`
80}
81
82type DeviceDisable struct {
83 Args struct {
84 Ids []DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
85 } `positional-args:"yes"`
86}
87
88type DeviceReboot struct {
89 Args struct {
90 Ids []DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
91 } `positional-args:"yes"`
92}
93
94type DeviceFlowList struct {
95 ListOutputOptions
Maninder045921e2020-09-29 16:46:02 +053096 FlowIdOptions
Zack Williamse940c7a2019-08-21 14:25:39 -070097 Args struct {
98 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
99 } `positional-args:"yes"`
100}
101
102type DevicePortList struct {
103 ListOutputOptions
104 Args struct {
105 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
106 } `positional-args:"yes"`
107}
108
109type DeviceInspect struct {
110 OutputOptionsJson
111 Args struct {
112 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
113 } `positional-args:"yes"`
114}
115
kesavand12cd8eb2020-01-20 22:25:22 -0500116type DevicePortEnable struct {
117 Args struct {
118 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
119 PortId PortNum `positional-arg-name:"PORT_NUMBER" required:"yes"`
120 } `positional-args:"yes"`
121}
122
123type DevicePortDisable struct {
124 Args struct {
125 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
126 PortId PortNum `positional-arg-name:"PORT_NUMBER" required:"yes"`
127 } `positional-args:"yes"`
128}
129
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000130type DevicePmConfigsGet struct {
131 ListOutputOptions
132 Args struct {
133 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
134 } `positional-args:"yes"`
135}
136
137type DevicePmConfigMetricList struct {
138 ListOutputOptions
139 Args struct {
140 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
141 } `positional-args:"yes"`
142}
143
144type DevicePmConfigGroupList struct {
145 ListOutputOptions
146 Args struct {
147 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
148 } `positional-args:"yes"`
149}
150
151type DevicePmConfigGroupMetricList struct {
152 ListOutputOptions
153 Args struct {
154 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
155 Group GroupName `positional-arg-name:"GROUP_NAME" required:"yes"`
156 } `positional-args:"yes"`
157}
158
159type DevicePmConfigFrequencySet struct {
160 OutputOptions
161 Args struct {
162 Frequency uint32 `positional-arg-name:"FREQUENCY" required:"yes"`
163 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
164 } `positional-args:"yes"`
165}
166
167type DevicePmConfigMetricEnable struct {
168 Args struct {
169 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
170 Metrics []MetricName `positional-arg-name:"METRIC_NAME" required:"yes"`
171 } `positional-args:"yes"`
172}
173
174type DevicePmConfigMetricDisable struct {
175 Args struct {
176 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
177 Metrics []MetricName `positional-arg-name:"METRIC_NAME" required:"yes"`
178 } `positional-args:"yes"`
179}
180
181type DevicePmConfigGroupEnable struct {
182 Args struct {
183 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
184 Groups []GroupName `positional-arg-name:"GROUP_NAME" required:"yes"`
185 } `positional-args:"yes"`
186}
187
188type DevicePmConfigGroupDisable struct {
189 Args struct {
190 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
191 Groups []GroupName `positional-arg-name:"GROUP_NAME" required:"yes"`
192 } `positional-args:"yes"`
193}
194
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -0800195type DeviceGetExtValue struct {
196 ListOutputOptions
197 Args struct {
198 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
199 Valueflag ValueFlag `positional-arg-name:"VALUE_FLAG" required:"yes"`
200 } `positional-args:"yes"`
201}
Rohan Agrawald7df3772020-06-29 11:23:36 +0000202
203type DevicePmConfigSetMaxSkew struct {
204 Args struct {
205 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
206 MaxSkew uint32 `positional-arg-name:"MAX_SKEW" required:"yes"`
207 } `positional-args:"yes"`
208}
209
Andrea Campanella791d88b2021-01-08 13:29:00 +0100210type DeviceOnuListImages struct {
211 ListOutputOptions
212 Args struct {
213 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
214 } `positional-args:"yes"`
215}
216
217type DeviceOnuDownloadImage struct {
218 Args struct {
219 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
220 Name string `positional-arg-name:"IMAGE_NAME" required:"yes"`
221 Url string `positional-arg-name:"IMAGE_URL" required:"yes"`
222 ImageVersion string `positional-arg-name:"IMAGE_VERSION" required:"yes"`
223 Crc uint32 `positional-arg-name:"IMAGE_CRC" required:"yes"`
224 LocalDir string `positional-arg-name:"IMAGE_LOCAL_DIRECTORY"`
225 } `positional-args:"yes"`
226}
227
228type DeviceOnuActivateImageUpdate struct {
229 Args struct {
230 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
231 Name string `positional-arg-name:"IMAGE_NAME" required:"yes"`
232 ImageVersion string `positional-arg-name:"IMAGE_VERSION" required:"yes"`
233 SaveConfig bool `positional-arg-name:"SAVE_EXISTING_CONFIG"`
234 LocalDir string `positional-arg-name:"IMAGE_LOCAL_DIRECTORY"`
235 } `positional-args:"yes"`
236}
237
Zack Williamse940c7a2019-08-21 14:25:39 -0700238type DeviceOpts struct {
239 List DeviceList `command:"list"`
240 Create DeviceCreate `command:"create"`
241 Delete DeviceDelete `command:"delete"`
242 Enable DeviceEnable `command:"enable"`
243 Disable DeviceDisable `command:"disable"`
244 Flows DeviceFlowList `command:"flows"`
kesavand12cd8eb2020-01-20 22:25:22 -0500245 Port struct {
246 List DevicePortList `command:"list"`
247 Enable DevicePortEnable `command:"enable"`
248 Disable DevicePortDisable `command:"disable"`
249 } `command:"port"`
250 Inspect DeviceInspect `command:"inspect"`
251 Reboot DeviceReboot `command:"reboot"`
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -0800252 Value struct {
253 Get DeviceGetExtValue `command:"get"`
254 } `command:"value"`
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000255 PmConfig struct {
Rohan Agrawald7df3772020-06-29 11:23:36 +0000256 Get DevicePmConfigsGet `command:"get"`
257 MaxSkew struct {
258 Set DevicePmConfigSetMaxSkew `command:"set"`
259 } `command:"maxskew"`
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000260 Frequency struct {
261 Set DevicePmConfigFrequencySet `command:"set"`
262 } `command:"frequency"`
263 Metric struct {
264 List DevicePmConfigMetricList `command:"list"`
265 Enable DevicePmConfigMetricEnable `command:"enable"`
266 Disable DevicePmConfigMetricDisable `command:"disable"`
267 } `command:"metric"`
268 Group struct {
269 List DevicePmConfigGroupList `command:"list"`
270 Enable DevicePmConfigGroupEnable `command:"enable"`
271 Disable DevicePmConfigGroupDisable `command:"disable"`
272 } `command:"group"`
273 GroupMetric struct {
274 List DevicePmConfigGroupMetricList `command:"list"`
275 } `command:"groupmetric"`
276 } `command:"pmconfig"`
Andrea Campanella791d88b2021-01-08 13:29:00 +0100277
278 Image struct {
279 Get DeviceOnuListImages `command:"list"`
280 Download DeviceOnuDownloadImage `command:"download"`
281 Activate DeviceOnuActivateImageUpdate `command:"activate"`
282 } `command:"image"`
Zack Williamse940c7a2019-08-21 14:25:39 -0700283}
284
285var deviceOpts = DeviceOpts{}
286
287func RegisterDeviceCommands(parser *flags.Parser) {
David Bainbridge12f036f2019-10-15 22:09:04 +0000288 if _, err := parser.AddCommand("device", "device commands", "Commands to query and manipulate VOLTHA devices", &deviceOpts); err != nil {
David Bainbridgea6722342019-10-24 23:55:53 +0000289 Error.Fatalf("Unexpected error while attempting to register device commands : %s", err)
David Bainbridge12f036f2019-10-15 22:09:04 +0000290 }
Zack Williamse940c7a2019-08-21 14:25:39 -0700291}
292
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000293func (i *MetricName) Complete(match string) []flags.Completion {
294 conn, err := NewConnection()
295 if err != nil {
296 return nil
297 }
298 defer conn.Close()
299
300 client := voltha.NewVolthaServiceClient(conn)
301
302 var deviceId string
303found:
304 for i := len(os.Args) - 1; i >= 0; i -= 1 {
305 switch os.Args[i] {
306 case "enable":
307 fallthrough
308 case "disable":
309 if len(os.Args) > i+1 {
310 deviceId = os.Args[i+1]
311 } else {
312 return nil
313 }
314 break found
315 default:
316 }
317 }
318
319 if len(deviceId) == 0 {
320 return nil
321 }
322
323 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
324 defer cancel()
325
326 id := voltha.ID{Id: string(deviceId)}
327
328 pmconfigs, err := client.ListDevicePmConfigs(ctx, &id)
329
330 if err != nil {
331 return nil
332 }
333
334 list := make([]flags.Completion, 0)
335 for _, metrics := range pmconfigs.Metrics {
336 if strings.HasPrefix(metrics.Name, match) {
337 list = append(list, flags.Completion{Item: metrics.Name})
338 }
339 }
340
341 return list
342}
343
344func (i *GroupName) Complete(match string) []flags.Completion {
345 conn, err := NewConnection()
346 if err != nil {
347 return nil
348 }
349 defer conn.Close()
350
351 client := voltha.NewVolthaServiceClient(conn)
352
353 var deviceId string
354found:
355 for i := len(os.Args) - 1; i >= 0; i -= 1 {
356 switch os.Args[i] {
357 case "list":
358 fallthrough
359 case "enable":
360 fallthrough
361 case "disable":
362 if len(os.Args) > i+1 {
363 deviceId = os.Args[i+1]
364 } else {
365 return nil
366 }
367 break found
368 default:
369 }
370 }
371
372 if len(deviceId) == 0 {
373 return nil
374 }
375
376 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
377 defer cancel()
378
379 id := voltha.ID{Id: string(deviceId)}
380
381 pmconfigs, err := client.ListDevicePmConfigs(ctx, &id)
382
383 if err != nil {
384 return nil
385 }
386
387 list := make([]flags.Completion, 0)
388 for _, group := range pmconfigs.Groups {
389 if strings.HasPrefix(group.GroupName, match) {
390 list = append(list, flags.Completion{Item: group.GroupName})
391 }
392 }
393 return list
394}
395
kesavand12cd8eb2020-01-20 22:25:22 -0500396func (i *PortNum) Complete(match string) []flags.Completion {
397 conn, err := NewConnection()
398 if err != nil {
399 return nil
400 }
401 defer conn.Close()
402
Scott Baker9173ed82020-05-19 08:30:12 -0700403 client := voltha.NewVolthaServiceClient(conn)
kesavand12cd8eb2020-01-20 22:25:22 -0500404
405 /*
406 * The command line args when completing for PortNum will be a DeviceId
407 * followed by one or more PortNums. So walk the argument list from the
408 * end and find the first argument that is enable/disable as those are
409 * the subcommands that come before the positional arguments. It would
410 * be nice if this package gave us the list of optional arguments
411 * already parsed.
412 */
413 var deviceId string
414found:
415 for i := len(os.Args) - 1; i >= 0; i -= 1 {
416 switch os.Args[i] {
417 case "enable":
418 fallthrough
419 case "disable":
420 if len(os.Args) > i+1 {
421 deviceId = os.Args[i+1]
422 } else {
423 return nil
424 }
425 break found
426 default:
427 }
428 }
429
430 if len(deviceId) == 0 {
431 return nil
432 }
433
kesavand12cd8eb2020-01-20 22:25:22 -0500434 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
435 defer cancel()
kesavand12cd8eb2020-01-20 22:25:22 -0500436
Scott Baker9173ed82020-05-19 08:30:12 -0700437 id := voltha.ID{Id: string(deviceId)}
kesavand12cd8eb2020-01-20 22:25:22 -0500438
Scott Baker9173ed82020-05-19 08:30:12 -0700439 ports, err := client.ListDevicePorts(ctx, &id)
kesavand12cd8eb2020-01-20 22:25:22 -0500440 if err != nil {
441 return nil
442 }
443
444 list := make([]flags.Completion, 0)
Scott Baker9173ed82020-05-19 08:30:12 -0700445 for _, item := range ports.Items {
446 pn := strconv.FormatUint(uint64(item.PortNo), 10)
kesavand12cd8eb2020-01-20 22:25:22 -0500447 if strings.HasPrefix(pn, match) {
448 list = append(list, flags.Completion{Item: pn})
449 }
450 }
451
452 return list
453}
454
Zack Williamse940c7a2019-08-21 14:25:39 -0700455func (i *DeviceId) Complete(match string) []flags.Completion {
456 conn, err := NewConnection()
457 if err != nil {
458 return nil
459 }
460 defer conn.Close()
461
Scott Baker9173ed82020-05-19 08:30:12 -0700462 client := voltha.NewVolthaServiceClient(conn)
Zack Williamse940c7a2019-08-21 14:25:39 -0700463
464 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
465 defer cancel()
466
Scott Baker9173ed82020-05-19 08:30:12 -0700467 devices, err := client.ListDevices(ctx, &empty.Empty{})
Zack Williamse940c7a2019-08-21 14:25:39 -0700468 if err != nil {
469 return nil
470 }
471
472 list := make([]flags.Completion, 0)
Scott Baker9173ed82020-05-19 08:30:12 -0700473 for _, item := range devices.Items {
474 if strings.HasPrefix(item.Id, match) {
475 list = append(list, flags.Completion{Item: item.Id})
Zack Williamse940c7a2019-08-21 14:25:39 -0700476 }
477 }
478
479 return list
480}
481
482func (options *DeviceList) Execute(args []string) error {
483
484 conn, err := NewConnection()
485 if err != nil {
486 return err
487 }
488 defer conn.Close()
489
Scott Baker9173ed82020-05-19 08:30:12 -0700490 client := voltha.NewVolthaServiceClient(conn)
Zack Williamse940c7a2019-08-21 14:25:39 -0700491
492 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
493 defer cancel()
494
Scott Baker9173ed82020-05-19 08:30:12 -0700495 devices, err := client.ListDevices(ctx, &empty.Empty{})
Zack Williamse940c7a2019-08-21 14:25:39 -0700496 if err != nil {
497 return err
498 }
499
500 outputFormat := CharReplacer.Replace(options.Format)
501 if outputFormat == "" {
David Bainbridgea6722342019-10-24 23:55:53 +0000502 outputFormat = GetCommandOptionWithDefault("device-list", "format", DEFAULT_DEVICE_FORMAT)
Zack Williamse940c7a2019-08-21 14:25:39 -0700503 }
504 if options.Quiet {
505 outputFormat = "{{.Id}}"
506 }
507
David Bainbridgea6722342019-10-24 23:55:53 +0000508 orderBy := options.OrderBy
509 if orderBy == "" {
510 orderBy = GetCommandOptionWithDefault("device-list", "order", "")
511 }
512
Scott Baker9173ed82020-05-19 08:30:12 -0700513 // Make sure json output prints an empty list, not "null"
514 if devices.Items == nil {
515 devices.Items = make([]*voltha.Device, 0)
Zack Williamse940c7a2019-08-21 14:25:39 -0700516 }
517
518 result := CommandResult{
519 Format: format.Format(outputFormat),
520 Filter: options.Filter,
David Bainbridgea6722342019-10-24 23:55:53 +0000521 OrderBy: orderBy,
Zack Williamse940c7a2019-08-21 14:25:39 -0700522 OutputAs: toOutputType(options.OutputAs),
523 NameLimit: options.NameLimit,
Scott Baker9173ed82020-05-19 08:30:12 -0700524 Data: devices.Items,
Zack Williamse940c7a2019-08-21 14:25:39 -0700525 }
526
527 GenerateOutput(&result)
528 return nil
529}
530
531func (options *DeviceCreate) Execute(args []string) error {
532
Scott Baker9173ed82020-05-19 08:30:12 -0700533 device := voltha.Device{}
Zack Williamse940c7a2019-08-21 14:25:39 -0700534 if options.HostAndPort != "" {
Scott Baker9173ed82020-05-19 08:30:12 -0700535 device.Address = &voltha.Device_HostAndPort{HostAndPort: options.HostAndPort}
Zack Williamse940c7a2019-08-21 14:25:39 -0700536 } else if options.IPAddress != "" {
Scott Baker9173ed82020-05-19 08:30:12 -0700537 device.Address = &voltha.Device_Ipv4Address{Ipv4Address: options.IPAddress}
Hardik Windlassce1de342020-02-04 21:58:07 +0000538 }
539 if options.MACAddress != "" {
Scott Baker9173ed82020-05-19 08:30:12 -0700540 device.MacAddress = strings.ToLower(options.MACAddress)
Zack Williamse940c7a2019-08-21 14:25:39 -0700541 }
542 if options.DeviceType != "" {
Scott Baker9173ed82020-05-19 08:30:12 -0700543 device.Type = options.DeviceType
Zack Williamse940c7a2019-08-21 14:25:39 -0700544 }
545
546 conn, err := NewConnection()
547 if err != nil {
548 return err
549 }
550 defer conn.Close()
551
Scott Baker9173ed82020-05-19 08:30:12 -0700552 client := voltha.NewVolthaServiceClient(conn)
Zack Williamse940c7a2019-08-21 14:25:39 -0700553
554 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
555 defer cancel()
556
Scott Baker9173ed82020-05-19 08:30:12 -0700557 createdDevice, err := client.CreateDevice(ctx, &device)
Zack Williamse940c7a2019-08-21 14:25:39 -0700558 if err != nil {
559 return err
Zack Williamse940c7a2019-08-21 14:25:39 -0700560 }
561
Scott Baker9173ed82020-05-19 08:30:12 -0700562 fmt.Printf("%s\n", createdDevice.Id)
Zack Williamse940c7a2019-08-21 14:25:39 -0700563
564 return nil
565}
566
567func (options *DeviceDelete) Execute(args []string) error {
568
569 conn, err := NewConnection()
570 if err != nil {
571 return err
572 }
573 defer conn.Close()
574
Scott Baker9173ed82020-05-19 08:30:12 -0700575 client := voltha.NewVolthaServiceClient(conn)
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)}
Himani Chawla9933ddc2020-10-12 23:53:27 +0530582 if options.Force {
583 _, err = client.ForceDeleteDevice(ctx, &id)
584 } else {
585 _, err = client.DeleteDevice(ctx, &id)
586 }
Scott Baker9173ed82020-05-19 08:30:12 -0700587
Zack Williamse940c7a2019-08-21 14:25:39 -0700588 if err != nil {
David Bainbridge0f758d42019-10-26 05:17:48 +0000589 Error.Printf("Error while deleting '%s': %s\n", i, err)
David Bainbridge7052fe82020-03-25 10:37:00 -0700590 lastErr = err
Zack Williamse940c7a2019-08-21 14:25:39 -0700591 continue
Zack Williamse940c7a2019-08-21 14:25:39 -0700592 }
593 fmt.Printf("%s\n", i)
594 }
595
David Bainbridge7052fe82020-03-25 10:37:00 -0700596 if lastErr != nil {
597 return NoReportErr
598 }
Zack Williamse940c7a2019-08-21 14:25:39 -0700599 return nil
600}
601
602func (options *DeviceEnable) Execute(args []string) error {
603 conn, err := NewConnection()
604 if err != nil {
605 return err
606 }
607 defer conn.Close()
608
Scott Baker9173ed82020-05-19 08:30:12 -0700609 client := voltha.NewVolthaServiceClient(conn)
Zack Williamse940c7a2019-08-21 14:25:39 -0700610
David Bainbridge7052fe82020-03-25 10:37:00 -0700611 var lastErr error
Zack Williamse940c7a2019-08-21 14:25:39 -0700612 for _, i := range options.Args.Ids {
Zack Williamse940c7a2019-08-21 14:25:39 -0700613 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
614 defer cancel()
615
Scott Baker9173ed82020-05-19 08:30:12 -0700616 id := voltha.ID{Id: string(i)}
617
618 _, err := client.EnableDevice(ctx, &id)
Zack Williamse940c7a2019-08-21 14:25:39 -0700619 if err != nil {
David Bainbridge0f758d42019-10-26 05:17:48 +0000620 Error.Printf("Error while enabling '%s': %s\n", i, err)
David Bainbridge7052fe82020-03-25 10:37:00 -0700621 lastErr = err
Zack Williamse940c7a2019-08-21 14:25:39 -0700622 continue
Zack Williamse940c7a2019-08-21 14:25:39 -0700623 }
624 fmt.Printf("%s\n", i)
625 }
626
David Bainbridge7052fe82020-03-25 10:37:00 -0700627 if lastErr != nil {
628 return NoReportErr
629 }
Zack Williamse940c7a2019-08-21 14:25:39 -0700630 return nil
631}
632
633func (options *DeviceDisable) Execute(args []string) error {
634 conn, err := NewConnection()
635 if err != nil {
636 return err
637 }
638 defer conn.Close()
639
Scott Baker9173ed82020-05-19 08:30:12 -0700640 client := voltha.NewVolthaServiceClient(conn)
Zack Williamse940c7a2019-08-21 14:25:39 -0700641
David Bainbridge7052fe82020-03-25 10:37:00 -0700642 var lastErr error
Zack Williamse940c7a2019-08-21 14:25:39 -0700643 for _, i := range options.Args.Ids {
Zack Williamse940c7a2019-08-21 14:25:39 -0700644 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
645 defer cancel()
646
Scott Baker9173ed82020-05-19 08:30:12 -0700647 id := voltha.ID{Id: string(i)}
648
649 _, err := client.DisableDevice(ctx, &id)
Zack Williamse940c7a2019-08-21 14:25:39 -0700650 if err != nil {
David Bainbridge0f758d42019-10-26 05:17:48 +0000651 Error.Printf("Error while disabling '%s': %s\n", i, err)
David Bainbridge7052fe82020-03-25 10:37:00 -0700652 lastErr = err
Zack Williamse940c7a2019-08-21 14:25:39 -0700653 continue
Zack Williamse940c7a2019-08-21 14:25:39 -0700654 }
655 fmt.Printf("%s\n", i)
656 }
657
David Bainbridge7052fe82020-03-25 10:37:00 -0700658 if lastErr != nil {
659 return NoReportErr
660 }
Zack Williamse940c7a2019-08-21 14:25:39 -0700661 return nil
662}
663
664func (options *DeviceReboot) Execute(args []string) error {
665 conn, err := NewConnection()
666 if err != nil {
667 return err
668 }
669 defer conn.Close()
670
Scott Baker9173ed82020-05-19 08:30:12 -0700671 client := voltha.NewVolthaServiceClient(conn)
Zack Williamse940c7a2019-08-21 14:25:39 -0700672
David Bainbridge7052fe82020-03-25 10:37:00 -0700673 var lastErr error
Zack Williamse940c7a2019-08-21 14:25:39 -0700674 for _, i := range options.Args.Ids {
Zack Williamse940c7a2019-08-21 14:25:39 -0700675 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
676 defer cancel()
677
Scott Baker9173ed82020-05-19 08:30:12 -0700678 id := voltha.ID{Id: string(i)}
679
680 _, err := client.RebootDevice(ctx, &id)
Zack Williamse940c7a2019-08-21 14:25:39 -0700681 if err != nil {
David Bainbridge0f758d42019-10-26 05:17:48 +0000682 Error.Printf("Error while rebooting '%s': %s\n", i, err)
David Bainbridge7052fe82020-03-25 10:37:00 -0700683 lastErr = err
Zack Williamse940c7a2019-08-21 14:25:39 -0700684 continue
Zack Williamse940c7a2019-08-21 14:25:39 -0700685 }
686 fmt.Printf("%s\n", i)
687 }
688
David Bainbridge7052fe82020-03-25 10:37:00 -0700689 if lastErr != nil {
690 return NoReportErr
691 }
Zack Williamse940c7a2019-08-21 14:25:39 -0700692 return nil
693}
694
695func (options *DevicePortList) Execute(args []string) error {
696
697 conn, err := NewConnection()
698 if err != nil {
699 return err
700 }
701 defer conn.Close()
702
Scott Baker9173ed82020-05-19 08:30:12 -0700703 client := voltha.NewVolthaServiceClient(conn)
Zack Williamse940c7a2019-08-21 14:25:39 -0700704
705 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
706 defer cancel()
707
Scott Baker9173ed82020-05-19 08:30:12 -0700708 id := voltha.ID{Id: string(options.Args.Id)}
Zack Williamse940c7a2019-08-21 14:25:39 -0700709
Scott Baker9173ed82020-05-19 08:30:12 -0700710 ports, err := client.ListDevicePorts(ctx, &id)
Zack Williamse940c7a2019-08-21 14:25:39 -0700711 if err != nil {
712 return err
713 }
714
715 outputFormat := CharReplacer.Replace(options.Format)
716 if outputFormat == "" {
David Bainbridgea6722342019-10-24 23:55:53 +0000717 outputFormat = GetCommandOptionWithDefault("device-ports", "format", DEFAULT_DEVICE_PORTS_FORMAT)
Zack Williamse940c7a2019-08-21 14:25:39 -0700718 }
Zack Williamse940c7a2019-08-21 14:25:39 -0700719
David Bainbridgea6722342019-10-24 23:55:53 +0000720 orderBy := options.OrderBy
721 if orderBy == "" {
722 orderBy = GetCommandOptionWithDefault("device-ports", "order", "")
723 }
724
Zack Williamse940c7a2019-08-21 14:25:39 -0700725 result := CommandResult{
726 Format: format.Format(outputFormat),
727 Filter: options.Filter,
David Bainbridgea6722342019-10-24 23:55:53 +0000728 OrderBy: orderBy,
Zack Williamse940c7a2019-08-21 14:25:39 -0700729 OutputAs: toOutputType(options.OutputAs),
730 NameLimit: options.NameLimit,
Scott Baker9173ed82020-05-19 08:30:12 -0700731 Data: ports.Items,
Zack Williamse940c7a2019-08-21 14:25:39 -0700732 }
733
734 GenerateOutput(&result)
735 return nil
736}
737
738func (options *DeviceFlowList) Execute(args []string) error {
739 fl := &FlowList{}
740 fl.ListOutputOptions = options.ListOutputOptions
Maninder045921e2020-09-29 16:46:02 +0530741 fl.FlowIdOptions = options.FlowIdOptions
Zack Williamse940c7a2019-08-21 14:25:39 -0700742 fl.Args.Id = string(options.Args.Id)
David Bainbridgea6722342019-10-24 23:55:53 +0000743 fl.Method = "device-flows"
Zack Williamse940c7a2019-08-21 14:25:39 -0700744 return fl.Execute(args)
745}
746
747func (options *DeviceInspect) Execute(args []string) error {
748 if len(args) > 0 {
749 return fmt.Errorf("only a single argument 'DEVICE_ID' can be provided")
750 }
751
752 conn, err := NewConnection()
753 if err != nil {
754 return err
755 }
756 defer conn.Close()
757
Scott Baker9173ed82020-05-19 08:30:12 -0700758 client := voltha.NewVolthaServiceClient(conn)
Zack Williamse940c7a2019-08-21 14:25:39 -0700759
760 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
761 defer cancel()
762
Scott Baker9173ed82020-05-19 08:30:12 -0700763 id := voltha.ID{Id: string(options.Args.Id)}
Zack Williamse940c7a2019-08-21 14:25:39 -0700764
Scott Baker9173ed82020-05-19 08:30:12 -0700765 device, err := client.GetDevice(ctx, &id)
Zack Williamse940c7a2019-08-21 14:25:39 -0700766 if err != nil {
767 return err
768 }
769
Zack Williamse940c7a2019-08-21 14:25:39 -0700770 outputFormat := CharReplacer.Replace(options.Format)
771 if outputFormat == "" {
David Bainbridgea6722342019-10-24 23:55:53 +0000772 outputFormat = GetCommandOptionWithDefault("device-inspect", "format", DEFAULT_DEVICE_INSPECT_FORMAT)
Zack Williamse940c7a2019-08-21 14:25:39 -0700773 }
774 if options.Quiet {
775 outputFormat = "{{.Id}}"
776 }
777
778 result := CommandResult{
779 Format: format.Format(outputFormat),
780 OutputAs: toOutputType(options.OutputAs),
781 NameLimit: options.NameLimit,
782 Data: device,
783 }
784 GenerateOutput(&result)
785 return nil
786}
kesavand12cd8eb2020-01-20 22:25:22 -0500787
788/*Device Port Enable */
789func (options *DevicePortEnable) Execute(args []string) error {
790 conn, err := NewConnection()
791 if err != nil {
792 return err
793 }
794 defer conn.Close()
795
Scott Baker9173ed82020-05-19 08:30:12 -0700796 client := voltha.NewVolthaServiceClient(conn)
kesavand12cd8eb2020-01-20 22:25:22 -0500797
kesavand12cd8eb2020-01-20 22:25:22 -0500798 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
799 defer cancel()
800
Scott Baker9173ed82020-05-19 08:30:12 -0700801 port := voltha.Port{DeviceId: string(options.Args.Id), PortNo: uint32(options.Args.PortId)}
802
803 _, err = client.EnablePort(ctx, &port)
kesavand12cd8eb2020-01-20 22:25:22 -0500804 if err != nil {
805 Error.Printf("Error enabling port number %v on device Id %s,err=%s\n", options.Args.PortId, options.Args.Id, ErrorToString(err))
806 return err
kesavand12cd8eb2020-01-20 22:25:22 -0500807 }
808
809 return nil
810}
811
Scott Baker9173ed82020-05-19 08:30:12 -0700812/*Device Port Disable */
kesavand12cd8eb2020-01-20 22:25:22 -0500813func (options *DevicePortDisable) Execute(args []string) error {
814 conn, err := NewConnection()
815 if err != nil {
816 return err
817 }
818 defer conn.Close()
819
Scott Baker9173ed82020-05-19 08:30:12 -0700820 client := voltha.NewVolthaServiceClient(conn)
821
kesavand12cd8eb2020-01-20 22:25:22 -0500822 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
823 defer cancel()
824
Scott Baker9173ed82020-05-19 08:30:12 -0700825 port := voltha.Port{DeviceId: string(options.Args.Id), PortNo: uint32(options.Args.PortId)}
826
827 _, err = client.DisablePort(ctx, &port)
kesavand12cd8eb2020-01-20 22:25:22 -0500828 if err != nil {
Scott Baker9173ed82020-05-19 08:30:12 -0700829 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 -0500830 return err
kesavand12cd8eb2020-01-20 22:25:22 -0500831 }
Scott Baker9173ed82020-05-19 08:30:12 -0700832
kesavand12cd8eb2020-01-20 22:25:22 -0500833 return nil
834}
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -0800835
Rohan Agrawald7df3772020-06-29 11:23:36 +0000836func (options *DevicePmConfigSetMaxSkew) Execute(args []string) error {
837 conn, err := NewConnection()
838 if err != nil {
839 return err
840 }
841 defer conn.Close()
842
843 client := voltha.NewVolthaServiceClient(conn)
844
845 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
846 defer cancel()
847
848 id := voltha.ID{Id: string(options.Args.Id)}
849
850 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
851 if err != nil {
852 return err
853 }
854
855 pmConfigs.MaxSkew = options.Args.MaxSkew
856
857 _, err = client.UpdateDevicePmConfigs(ctx, pmConfigs)
858 if err != nil {
859 return err
860 }
861
862 return nil
863}
864
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000865func (options *DevicePmConfigsGet) Execute(args []string) error {
866
867 conn, err := NewConnection()
868 if err != nil {
869 return err
870 }
871 defer conn.Close()
872
873 client := voltha.NewVolthaServiceClient(conn)
874
875 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
876 defer cancel()
877
878 id := voltha.ID{Id: string(options.Args.Id)}
879
880 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
881 if err != nil {
882 return err
883 }
884
885 outputFormat := CharReplacer.Replace(options.Format)
886 if outputFormat == "" {
887 outputFormat = GetCommandOptionWithDefault("device-pm-configs", "format", DEFAULT_DEVICE_PM_CONFIG_GET_FORMAT)
888 }
889
890 orderBy := options.OrderBy
891 if orderBy == "" {
892 orderBy = GetCommandOptionWithDefault("device-pm-configs", "order", "")
893 }
894
895 result := CommandResult{
896 Format: format.Format(outputFormat),
897 Filter: options.Filter,
898 OrderBy: orderBy,
899 OutputAs: toOutputType(options.OutputAs),
900 NameLimit: options.NameLimit,
901 Data: pmConfigs,
902 }
903
904 GenerateOutput(&result)
905 return nil
906
907}
908
909func (options *DevicePmConfigMetricList) Execute(args []string) error {
910
911 conn, err := NewConnection()
912 if err != nil {
913 return err
914 }
915 defer conn.Close()
916
917 client := voltha.NewVolthaServiceClient(conn)
918
919 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
920 defer cancel()
921
922 id := voltha.ID{Id: string(options.Args.Id)}
923
924 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
925 if err != nil {
926 return err
927 }
928
929 if !pmConfigs.Grouped {
930 for _, metric := range pmConfigs.Metrics {
931 if metric.SampleFreq == 0 {
932 metric.SampleFreq = pmConfigs.DefaultFreq
933 }
934 }
Rohan Agrawalbca69122020-06-17 14:59:03 +0000935 outputFormat := CharReplacer.Replace(options.Format)
936 if outputFormat == "" {
937 outputFormat = GetCommandOptionWithDefault("device-pm-configs", "format", DEFAULT_DEVICE_PM_CONFIG_METRIC_LIST_FORMAT)
938 }
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000939
Rohan Agrawalbca69122020-06-17 14:59:03 +0000940 orderBy := options.OrderBy
941 if orderBy == "" {
942 orderBy = GetCommandOptionWithDefault("device-pm-configs", "order", "")
943 }
944
945 result := CommandResult{
946 Format: format.Format(outputFormat),
947 Filter: options.Filter,
948 OrderBy: orderBy,
949 OutputAs: toOutputType(options.OutputAs),
950 NameLimit: options.NameLimit,
951 Data: pmConfigs.Metrics,
952 }
953
954 GenerateOutput(&result)
955 return nil
956 } else {
957 return fmt.Errorf("Device '%s' does not have Non Grouped Metrics", options.Args.Id)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000958 }
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000959}
960
961func (options *DevicePmConfigMetricEnable) Execute(args []string) error {
962
963 conn, err := NewConnection()
964 if err != nil {
965 return err
966 }
967 defer conn.Close()
968
969 client := voltha.NewVolthaServiceClient(conn)
970
971 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
972 defer cancel()
973
974 id := voltha.ID{Id: string(options.Args.Id)}
975
976 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
977 if err != nil {
978 return err
979 }
980
981 if !pmConfigs.Grouped {
Rohan Agrawalbca69122020-06-17 14:59:03 +0000982 metrics := make(map[string]struct{})
983 for _, metric := range pmConfigs.Metrics {
984 metrics[metric.Name] = struct{}{}
985 }
986
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000987 for _, metric := range pmConfigs.Metrics {
988 for _, mName := range options.Args.Metrics {
Rohan Agrawalbca69122020-06-17 14:59:03 +0000989 if _, exist := metrics[string(mName)]; !exist {
990 return fmt.Errorf("Metric Name '%s' does not exist", mName)
991 }
992
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000993 if string(mName) == metric.Name && !metric.Enabled {
994 metric.Enabled = true
995 _, err := client.UpdateDevicePmConfigs(ctx, pmConfigs)
996 if err != nil {
997 return err
998 }
999 }
1000 }
1001 }
Rohan Agrawalbca69122020-06-17 14:59:03 +00001002 } else {
1003 return fmt.Errorf("Device '%s' does not have Non Grouped Metrics", options.Args.Id)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001004 }
1005 return nil
1006}
1007
1008func (options *DevicePmConfigMetricDisable) Execute(args []string) error {
1009
1010 conn, err := NewConnection()
1011 if err != nil {
1012 return err
1013 }
1014 defer conn.Close()
1015
1016 client := voltha.NewVolthaServiceClient(conn)
1017
1018 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
1019 defer cancel()
1020
1021 id := voltha.ID{Id: string(options.Args.Id)}
1022
1023 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
1024 if err != nil {
1025 return err
1026 }
1027
1028 if !pmConfigs.Grouped {
Rohan Agrawalbca69122020-06-17 14:59:03 +00001029 metrics := make(map[string]struct{})
1030 for _, metric := range pmConfigs.Metrics {
1031 metrics[metric.Name] = struct{}{}
1032 }
1033
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001034 for _, metric := range pmConfigs.Metrics {
1035 for _, mName := range options.Args.Metrics {
Rohan Agrawalbca69122020-06-17 14:59:03 +00001036 if _, have := metrics[string(mName)]; !have {
1037 return fmt.Errorf("Metric Name '%s' does not exist", mName)
1038 }
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001039 if string(mName) == metric.Name && metric.Enabled {
1040 metric.Enabled = false
1041 _, err := client.UpdateDevicePmConfigs(ctx, pmConfigs)
1042 if err != nil {
1043 return err
1044 }
Rohan Agrawalbca69122020-06-17 14:59:03 +00001045 } else {
1046 return fmt.Errorf("Metric '%s' cannot be disabled", string(mName))
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001047 }
1048 }
1049 }
Rohan Agrawalbca69122020-06-17 14:59:03 +00001050 } else {
1051 return fmt.Errorf("Device '%s' does not have Non Grouped Metrics", options.Args.Id)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001052 }
1053 return nil
1054}
1055
1056func (options *DevicePmConfigGroupEnable) Execute(args []string) error {
1057
1058 conn, err := NewConnection()
1059 if err != nil {
1060 return err
1061 }
1062 defer conn.Close()
1063
1064 client := voltha.NewVolthaServiceClient(conn)
1065
1066 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
1067 defer cancel()
1068
1069 id := voltha.ID{Id: string(options.Args.Id)}
1070
1071 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
1072 if err != nil {
1073 return err
1074 }
1075
1076 if pmConfigs.Grouped {
Rohan Agrawalbca69122020-06-17 14:59:03 +00001077 groups := make(map[string]struct{})
1078 for _, group := range pmConfigs.Groups {
1079 groups[group.GroupName] = struct{}{}
1080 }
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001081 for _, group := range pmConfigs.Groups {
1082 for _, gName := range options.Args.Groups {
Rohan Agrawalbca69122020-06-17 14:59:03 +00001083 if _, have := groups[string(gName)]; !have {
1084 return fmt.Errorf("Group Name '%s' does not exist", gName)
1085 }
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001086 if string(gName) == group.GroupName && !group.Enabled {
1087 group.Enabled = true
1088 _, err := client.UpdateDevicePmConfigs(ctx, pmConfigs)
1089 if err != nil {
1090 return err
1091 }
1092 }
1093 }
1094 }
Rohan Agrawalbca69122020-06-17 14:59:03 +00001095 } else {
1096 return fmt.Errorf("Device '%s' does not have Group Metrics", options.Args.Id)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001097 }
1098 return nil
1099}
1100
1101func (options *DevicePmConfigGroupDisable) Execute(args []string) error {
1102
1103 conn, err := NewConnection()
1104 if err != nil {
1105 return err
1106 }
1107 defer conn.Close()
1108
1109 client := voltha.NewVolthaServiceClient(conn)
1110
1111 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
1112 defer cancel()
1113
1114 id := voltha.ID{Id: string(options.Args.Id)}
1115
1116 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
1117 if err != nil {
1118 return err
1119 }
1120
1121 if pmConfigs.Grouped {
Rohan Agrawalbca69122020-06-17 14:59:03 +00001122 groups := make(map[string]struct{})
1123 for _, group := range pmConfigs.Groups {
1124 groups[group.GroupName] = struct{}{}
1125 }
1126
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001127 for _, group := range pmConfigs.Groups {
1128 for _, gName := range options.Args.Groups {
Rohan Agrawalbca69122020-06-17 14:59:03 +00001129 if _, have := groups[string(gName)]; !have {
1130 return fmt.Errorf("Group Name '%s' does not exist", gName)
1131 }
1132
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001133 if string(gName) == group.GroupName && group.Enabled {
1134 group.Enabled = false
1135 _, err := client.UpdateDevicePmConfigs(ctx, pmConfigs)
1136 if err != nil {
1137 return err
1138 }
1139 }
1140 }
1141 }
Rohan Agrawalbca69122020-06-17 14:59:03 +00001142 } else {
1143 return fmt.Errorf("Device '%s' does not have Group Metrics", options.Args.Id)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001144 }
1145 return nil
1146}
1147
1148func (options *DevicePmConfigGroupList) Execute(args []string) error {
1149
1150 conn, err := NewConnection()
1151 if err != nil {
1152 return err
1153 }
1154 defer conn.Close()
1155
1156 client := voltha.NewVolthaServiceClient(conn)
1157
1158 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
1159 defer cancel()
1160
1161 id := voltha.ID{Id: string(options.Args.Id)}
1162
1163 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
1164 if err != nil {
1165 return err
1166 }
1167
1168 if pmConfigs.Grouped {
1169 for _, group := range pmConfigs.Groups {
1170 if group.GroupFreq == 0 {
1171 group.GroupFreq = pmConfigs.DefaultFreq
1172 }
1173 }
Rohan Agrawalbca69122020-06-17 14:59:03 +00001174 outputFormat := CharReplacer.Replace(options.Format)
1175 if outputFormat == "" {
1176 outputFormat = GetCommandOptionWithDefault("device-pm-configs", "format", DEFAULT_DEVICE_PM_CONFIG_GROUP_LIST_FORMAT)
1177 }
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001178
Rohan Agrawalbca69122020-06-17 14:59:03 +00001179 orderBy := options.OrderBy
1180 if orderBy == "" {
1181 orderBy = GetCommandOptionWithDefault("device-pm-configs", "order", "")
1182 }
1183
1184 result := CommandResult{
1185 Format: format.Format(outputFormat),
1186 Filter: options.Filter,
1187 OrderBy: orderBy,
1188 OutputAs: toOutputType(options.OutputAs),
1189 NameLimit: options.NameLimit,
1190 Data: pmConfigs.Groups,
1191 }
1192
1193 GenerateOutput(&result)
1194 } else {
1195 return fmt.Errorf("Device '%s' does not have Group Metrics", string(options.Args.Id))
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001196 }
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001197 return nil
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001198}
1199
1200func (options *DevicePmConfigGroupMetricList) Execute(args []string) error {
1201
1202 var metrics []*voltha.PmConfig
1203 conn, err := NewConnection()
1204 if err != nil {
1205 return err
1206 }
1207 defer conn.Close()
1208
1209 client := voltha.NewVolthaServiceClient(conn)
1210
1211 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
1212 defer cancel()
1213
1214 id := voltha.ID{Id: string(options.Args.Id)}
1215
1216 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
1217 if err != nil {
1218 return err
1219 }
1220
1221 for _, groups := range pmConfigs.Groups {
1222
1223 if string(options.Args.Group) == groups.GroupName {
1224 for _, metric := range groups.Metrics {
1225 if metric.SampleFreq == 0 && groups.GroupFreq == 0 {
1226 metric.SampleFreq = pmConfigs.DefaultFreq
1227 } else {
1228 metric.SampleFreq = groups.GroupFreq
1229 }
1230 }
1231 metrics = groups.Metrics
1232 }
1233 }
1234
1235 outputFormat := CharReplacer.Replace(options.Format)
1236 if outputFormat == "" {
1237 outputFormat = GetCommandOptionWithDefault("device-pm-configs", "format", DEFAULT_DEVICE_PM_CONFIG_METRIC_LIST_FORMAT)
1238 }
1239
1240 orderBy := options.OrderBy
1241 if orderBy == "" {
1242 orderBy = GetCommandOptionWithDefault("device-pm-configs", "order", "")
1243 }
1244
1245 result := CommandResult{
1246 Format: format.Format(outputFormat),
1247 Filter: options.Filter,
1248 OrderBy: orderBy,
1249 OutputAs: toOutputType(options.OutputAs),
1250 NameLimit: options.NameLimit,
1251 Data: metrics,
1252 }
1253
1254 GenerateOutput(&result)
1255 return nil
1256
1257}
1258
1259func (options *DevicePmConfigFrequencySet) Execute(args []string) error {
1260
1261 conn, err := NewConnection()
1262 if err != nil {
1263 return err
1264 }
1265 defer conn.Close()
1266
1267 client := voltha.NewVolthaServiceClient(conn)
1268
1269 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
1270 defer cancel()
1271
1272 id := voltha.ID{Id: string(options.Args.Id)}
1273
1274 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
1275 if err != nil {
1276 return err
1277 }
1278
1279 pmConfigs.DefaultFreq = options.Args.Frequency
1280
1281 _, err = client.UpdateDevicePmConfigs(ctx, pmConfigs)
1282 if err != nil {
1283 return err
1284 }
1285
1286 outputFormat := CharReplacer.Replace(options.Format)
1287 if outputFormat == "" {
1288 outputFormat = GetCommandOptionWithDefault("device-pm-configs", "format", DEFAULT_DEVICE_PM_CONFIG_GET_FORMAT)
1289 }
1290 if options.Quiet {
1291 outputFormat = "{{.Id}}"
1292 }
1293
1294 result := CommandResult{
1295 Format: format.Format(outputFormat),
1296 OutputAs: toOutputType(options.OutputAs),
1297 NameLimit: options.NameLimit,
1298 Data: pmConfigs,
1299 }
1300
1301 GenerateOutput(&result)
1302 return nil
1303
1304}
1305
Andrea Campanella791d88b2021-01-08 13:29:00 +01001306func (options *DeviceOnuListImages) Execute(args []string) error {
1307
1308 conn, err := NewConnection()
1309 if err != nil {
1310 return err
1311 }
1312 defer conn.Close()
1313
1314 client := voltha.NewVolthaServiceClient(conn)
1315
1316 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
1317 defer cancel()
1318
1319 id := common.ID{Id: string(options.Args.Id)}
1320
1321 imageDownloads, err := client.ListImageDownloads(ctx, &id)
1322 if err != nil {
1323 return err
1324 }
1325
1326 outputFormat := CharReplacer.Replace(options.Format)
1327 if outputFormat == "" {
1328 outputFormat = GetCommandOptionWithDefault("device-image-list", "format", DEFAULT_DEVICE_IMAGE_LIST_GET_FORMAT)
1329 }
1330
1331 if options.Quiet {
1332 outputFormat = "{{.Id}}"
1333 }
1334
1335 //TODO orderby
1336
1337 // Make sure json output prints an empty list, not "null"
1338 if imageDownloads.Items == nil {
1339 imageDownloads.Items = make([]*voltha.ImageDownload, 0)
1340 }
1341
1342 result := CommandResult{
1343 Format: format.Format(outputFormat),
1344 OutputAs: toOutputType(options.OutputAs),
1345 NameLimit: options.NameLimit,
1346 Data: imageDownloads.Items,
1347 }
1348
1349 GenerateOutput(&result)
1350 return nil
1351
1352}
1353
1354func (options *DeviceOnuDownloadImage) Execute(args []string) error {
1355
1356 conn, err := NewConnection()
1357 if err != nil {
1358 return err
1359 }
1360 defer conn.Close()
1361
1362 client := voltha.NewVolthaServiceClient(conn)
1363
1364 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
1365 defer cancel()
1366
1367 downloadImage := voltha.ImageDownload{
1368 Id: string(options.Args.Id),
1369 Name: options.Args.Name,
1370 Url: options.Args.Url,
1371 Crc: options.Args.Crc,
1372 LocalDir: options.Args.LocalDir,
1373 }
1374
1375 _, err = client.DownloadImage(ctx, &downloadImage)
1376 if err != nil {
1377 return err
1378 }
1379
1380 return nil
1381
1382}
1383
1384func (options *DeviceOnuActivateImageUpdate) Execute(args []string) error {
1385
1386 conn, err := NewConnection()
1387 if err != nil {
1388 return err
1389 }
1390 defer conn.Close()
1391
1392 client := voltha.NewVolthaServiceClient(conn)
1393
1394 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
1395 defer cancel()
1396
1397 downloadImage := voltha.ImageDownload{
1398 Id: string(options.Args.Id),
1399 Name: options.Args.Name,
1400 ImageVersion: options.Args.ImageVersion,
1401 SaveConfig: options.Args.SaveConfig,
1402 LocalDir: options.Args.LocalDir,
1403 }
1404
1405 _, err = client.ActivateImageUpdate(ctx, &downloadImage)
1406 if err != nil {
1407 return err
1408 }
1409
1410 return nil
1411
1412}
1413
Scott Baker9173ed82020-05-19 08:30:12 -07001414type ReturnValueRow struct {
1415 Name string `json:"name"`
1416 Result interface{} `json:"result"`
1417}
1418
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -08001419/*Device get Onu Distance */
1420func (options *DeviceGetExtValue) Execute(args []string) error {
1421 conn, err := NewConnection()
1422 if err != nil {
1423 return err
1424 }
1425 defer conn.Close()
1426
Scott Baker9173ed82020-05-19 08:30:12 -07001427 client := voltha.NewVolthaServiceClient(conn)
1428
1429 valueflag, okay := common.ValueType_Type_value[string(options.Args.Valueflag)]
1430 if !okay {
1431 Error.Printf("Unknown valueflag %s\n", options.Args.Valueflag)
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -08001432 }
1433
Scott Baker9173ed82020-05-19 08:30:12 -07001434 val := voltha.ValueSpecifier{Id: string(options.Args.Id), Value: common.ValueType_Type(valueflag)}
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -08001435
1436 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
1437 defer cancel()
1438
Scott Baker9173ed82020-05-19 08:30:12 -07001439 rv, err := client.GetExtValue(ctx, &val)
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -08001440 if err != nil {
1441 Error.Printf("Error getting value on device Id %s,err=%s\n", options.Args.Id, ErrorToString(err))
1442 return err
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -08001443 }
1444
Scott Baker9173ed82020-05-19 08:30:12 -07001445 var rows []ReturnValueRow
1446 for name, num := range common.ValueType_Type_value {
1447 if num == 0 {
1448 // EMPTY is not a real value
1449 continue
1450 }
1451 if (rv.Error & uint32(num)) != 0 {
1452 row := ReturnValueRow{Name: name, Result: "Error"}
1453 rows = append(rows, row)
1454 }
1455 if (rv.Unsupported & uint32(num)) != 0 {
1456 row := ReturnValueRow{Name: name, Result: "Unsupported"}
1457 rows = append(rows, row)
1458 }
1459 if (rv.Set & uint32(num)) != 0 {
1460 switch name {
1461 case "DISTANCE":
1462 row := ReturnValueRow{Name: name, Result: rv.Distance}
1463 rows = append(rows, row)
1464 default:
1465 row := ReturnValueRow{Name: name, Result: "Unimplemented-in-voltctl"}
1466 rows = append(rows, row)
1467 }
1468 }
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -08001469 }
1470
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -08001471 outputFormat := CharReplacer.Replace(options.Format)
1472 if outputFormat == "" {
1473 outputFormat = GetCommandOptionWithDefault("device-value-get", "format", DEFAULT_DEVICE_VALUE_GET_FORMAT)
1474 }
1475
1476 result := CommandResult{
1477 Format: format.Format(outputFormat),
1478 OutputAs: toOutputType(options.OutputAs),
1479 NameLimit: options.NameLimit,
Scott Baker9173ed82020-05-19 08:30:12 -07001480 Data: rows,
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -08001481 }
1482 GenerateOutput(&result)
1483 return nil
1484}