blob: b6864f66ba9cb2bfb814ecfbcdf9e64db2767b80 [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}}`
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -080044 DEFAULT_DEVICE_VALUE_GET_FORMAT = "table{{.Name}}\t{{.Result}}"
Zack Williamse940c7a2019-08-21 14:25:39 -070045)
46
47type DeviceList struct {
48 ListOutputOptions
49}
50
51type DeviceCreate struct {
David Bainbridge835dd0e2020-04-01 10:30:09 -070052 DeviceType string `short:"t" long:"devicetype" default:"" description:"Device type"`
53 MACAddress string `short:"m" long:"macaddress" default:"" description:"MAC Address"`
Zack Williamse940c7a2019-08-21 14:25:39 -070054 IPAddress string `short:"i" long:"ipaddress" default:"" description:"IP Address"`
55 HostAndPort string `short:"H" long:"hostandport" default:"" description:"Host and port"`
56}
57
58type DeviceId string
59
kesavand12cd8eb2020-01-20 22:25:22 -050060type PortNum uint32
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -080061type ValueFlag string
kesavand12cd8eb2020-01-20 22:25:22 -050062
Zack Williamse940c7a2019-08-21 14:25:39 -070063type DeviceDelete struct {
64 Args struct {
65 Ids []DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
66 } `positional-args:"yes"`
67}
68
69type DeviceEnable struct {
70 Args struct {
71 Ids []DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
72 } `positional-args:"yes"`
73}
74
75type DeviceDisable struct {
76 Args struct {
77 Ids []DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
78 } `positional-args:"yes"`
79}
80
81type DeviceReboot struct {
82 Args struct {
83 Ids []DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
84 } `positional-args:"yes"`
85}
86
87type DeviceFlowList struct {
88 ListOutputOptions
89 Args struct {
90 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
91 } `positional-args:"yes"`
92}
93
94type DevicePortList struct {
95 ListOutputOptions
96 Args struct {
97 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
98 } `positional-args:"yes"`
99}
100
101type DeviceInspect struct {
102 OutputOptionsJson
103 Args struct {
104 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
105 } `positional-args:"yes"`
106}
107
kesavand12cd8eb2020-01-20 22:25:22 -0500108type DevicePortEnable struct {
109 Args struct {
110 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
111 PortId PortNum `positional-arg-name:"PORT_NUMBER" required:"yes"`
112 } `positional-args:"yes"`
113}
114
115type DevicePortDisable struct {
116 Args struct {
117 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
118 PortId PortNum `positional-arg-name:"PORT_NUMBER" required:"yes"`
119 } `positional-args:"yes"`
120}
121
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -0800122type DeviceGetExtValue struct {
123 ListOutputOptions
124 Args struct {
125 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
126 Valueflag ValueFlag `positional-arg-name:"VALUE_FLAG" required:"yes"`
127 } `positional-args:"yes"`
128}
Zack Williamse940c7a2019-08-21 14:25:39 -0700129type DeviceOpts struct {
130 List DeviceList `command:"list"`
131 Create DeviceCreate `command:"create"`
132 Delete DeviceDelete `command:"delete"`
133 Enable DeviceEnable `command:"enable"`
134 Disable DeviceDisable `command:"disable"`
135 Flows DeviceFlowList `command:"flows"`
kesavand12cd8eb2020-01-20 22:25:22 -0500136 Port struct {
137 List DevicePortList `command:"list"`
138 Enable DevicePortEnable `command:"enable"`
139 Disable DevicePortDisable `command:"disable"`
140 } `command:"port"`
141 Inspect DeviceInspect `command:"inspect"`
142 Reboot DeviceReboot `command:"reboot"`
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -0800143 Value struct {
144 Get DeviceGetExtValue `command:"get"`
145 } `command:"value"`
Zack Williamse940c7a2019-08-21 14:25:39 -0700146}
147
148var deviceOpts = DeviceOpts{}
149
150func RegisterDeviceCommands(parser *flags.Parser) {
David Bainbridge12f036f2019-10-15 22:09:04 +0000151 if _, err := parser.AddCommand("device", "device commands", "Commands to query and manipulate VOLTHA devices", &deviceOpts); err != nil {
David Bainbridgea6722342019-10-24 23:55:53 +0000152 Error.Fatalf("Unexpected error while attempting to register device commands : %s", err)
David Bainbridge12f036f2019-10-15 22:09:04 +0000153 }
Zack Williamse940c7a2019-08-21 14:25:39 -0700154}
155
kesavand12cd8eb2020-01-20 22:25:22 -0500156func (i *PortNum) Complete(match string) []flags.Completion {
157 conn, err := NewConnection()
158 if err != nil {
159 return nil
160 }
161 defer conn.Close()
162
Scott Baker9173ed82020-05-19 08:30:12 -0700163 client := voltha.NewVolthaServiceClient(conn)
kesavand12cd8eb2020-01-20 22:25:22 -0500164
165 /*
166 * The command line args when completing for PortNum will be a DeviceId
167 * followed by one or more PortNums. So walk the argument list from the
168 * end and find the first argument that is enable/disable as those are
169 * the subcommands that come before the positional arguments. It would
170 * be nice if this package gave us the list of optional arguments
171 * already parsed.
172 */
173 var deviceId string
174found:
175 for i := len(os.Args) - 1; i >= 0; i -= 1 {
176 switch os.Args[i] {
177 case "enable":
178 fallthrough
179 case "disable":
180 if len(os.Args) > i+1 {
181 deviceId = os.Args[i+1]
182 } else {
183 return nil
184 }
185 break found
186 default:
187 }
188 }
189
190 if len(deviceId) == 0 {
191 return nil
192 }
193
kesavand12cd8eb2020-01-20 22:25:22 -0500194 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
195 defer cancel()
kesavand12cd8eb2020-01-20 22:25:22 -0500196
Scott Baker9173ed82020-05-19 08:30:12 -0700197 id := voltha.ID{Id: string(deviceId)}
kesavand12cd8eb2020-01-20 22:25:22 -0500198
Scott Baker9173ed82020-05-19 08:30:12 -0700199 ports, err := client.ListDevicePorts(ctx, &id)
kesavand12cd8eb2020-01-20 22:25:22 -0500200 if err != nil {
201 return nil
202 }
203
204 list := make([]flags.Completion, 0)
Scott Baker9173ed82020-05-19 08:30:12 -0700205 for _, item := range ports.Items {
206 pn := strconv.FormatUint(uint64(item.PortNo), 10)
kesavand12cd8eb2020-01-20 22:25:22 -0500207 if strings.HasPrefix(pn, match) {
208 list = append(list, flags.Completion{Item: pn})
209 }
210 }
211
212 return list
213}
214
Zack Williamse940c7a2019-08-21 14:25:39 -0700215func (i *DeviceId) Complete(match string) []flags.Completion {
216 conn, err := NewConnection()
217 if err != nil {
218 return nil
219 }
220 defer conn.Close()
221
Scott Baker9173ed82020-05-19 08:30:12 -0700222 client := voltha.NewVolthaServiceClient(conn)
Zack Williamse940c7a2019-08-21 14:25:39 -0700223
224 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
225 defer cancel()
226
Scott Baker9173ed82020-05-19 08:30:12 -0700227 devices, err := client.ListDevices(ctx, &empty.Empty{})
Zack Williamse940c7a2019-08-21 14:25:39 -0700228 if err != nil {
229 return nil
230 }
231
232 list := make([]flags.Completion, 0)
Scott Baker9173ed82020-05-19 08:30:12 -0700233 for _, item := range devices.Items {
234 if strings.HasPrefix(item.Id, match) {
235 list = append(list, flags.Completion{Item: item.Id})
Zack Williamse940c7a2019-08-21 14:25:39 -0700236 }
237 }
238
239 return list
240}
241
242func (options *DeviceList) Execute(args []string) error {
243
244 conn, err := NewConnection()
245 if err != nil {
246 return err
247 }
248 defer conn.Close()
249
Scott Baker9173ed82020-05-19 08:30:12 -0700250 client := voltha.NewVolthaServiceClient(conn)
Zack Williamse940c7a2019-08-21 14:25:39 -0700251
252 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
253 defer cancel()
254
Scott Baker9173ed82020-05-19 08:30:12 -0700255 devices, err := client.ListDevices(ctx, &empty.Empty{})
Zack Williamse940c7a2019-08-21 14:25:39 -0700256 if err != nil {
257 return err
258 }
259
260 outputFormat := CharReplacer.Replace(options.Format)
261 if outputFormat == "" {
David Bainbridgea6722342019-10-24 23:55:53 +0000262 outputFormat = GetCommandOptionWithDefault("device-list", "format", DEFAULT_DEVICE_FORMAT)
Zack Williamse940c7a2019-08-21 14:25:39 -0700263 }
264 if options.Quiet {
265 outputFormat = "{{.Id}}"
266 }
267
David Bainbridgea6722342019-10-24 23:55:53 +0000268 orderBy := options.OrderBy
269 if orderBy == "" {
270 orderBy = GetCommandOptionWithDefault("device-list", "order", "")
271 }
272
Scott Baker9173ed82020-05-19 08:30:12 -0700273 // Make sure json output prints an empty list, not "null"
274 if devices.Items == nil {
275 devices.Items = make([]*voltha.Device, 0)
Zack Williamse940c7a2019-08-21 14:25:39 -0700276 }
277
278 result := CommandResult{
279 Format: format.Format(outputFormat),
280 Filter: options.Filter,
David Bainbridgea6722342019-10-24 23:55:53 +0000281 OrderBy: orderBy,
Zack Williamse940c7a2019-08-21 14:25:39 -0700282 OutputAs: toOutputType(options.OutputAs),
283 NameLimit: options.NameLimit,
Scott Baker9173ed82020-05-19 08:30:12 -0700284 Data: devices.Items,
Zack Williamse940c7a2019-08-21 14:25:39 -0700285 }
286
287 GenerateOutput(&result)
288 return nil
289}
290
291func (options *DeviceCreate) Execute(args []string) error {
292
Scott Baker9173ed82020-05-19 08:30:12 -0700293 device := voltha.Device{}
Zack Williamse940c7a2019-08-21 14:25:39 -0700294 if options.HostAndPort != "" {
Scott Baker9173ed82020-05-19 08:30:12 -0700295 device.Address = &voltha.Device_HostAndPort{HostAndPort: options.HostAndPort}
Zack Williamse940c7a2019-08-21 14:25:39 -0700296 } else if options.IPAddress != "" {
Scott Baker9173ed82020-05-19 08:30:12 -0700297 device.Address = &voltha.Device_Ipv4Address{Ipv4Address: options.IPAddress}
Hardik Windlassce1de342020-02-04 21:58:07 +0000298 }
299 if options.MACAddress != "" {
Scott Baker9173ed82020-05-19 08:30:12 -0700300 device.MacAddress = strings.ToLower(options.MACAddress)
Zack Williamse940c7a2019-08-21 14:25:39 -0700301 }
302 if options.DeviceType != "" {
Scott Baker9173ed82020-05-19 08:30:12 -0700303 device.Type = options.DeviceType
Zack Williamse940c7a2019-08-21 14:25:39 -0700304 }
305
306 conn, err := NewConnection()
307 if err != nil {
308 return err
309 }
310 defer conn.Close()
311
Scott Baker9173ed82020-05-19 08:30:12 -0700312 client := voltha.NewVolthaServiceClient(conn)
Zack Williamse940c7a2019-08-21 14:25:39 -0700313
314 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
315 defer cancel()
316
Scott Baker9173ed82020-05-19 08:30:12 -0700317 createdDevice, err := client.CreateDevice(ctx, &device)
Zack Williamse940c7a2019-08-21 14:25:39 -0700318 if err != nil {
319 return err
Zack Williamse940c7a2019-08-21 14:25:39 -0700320 }
321
Scott Baker9173ed82020-05-19 08:30:12 -0700322 fmt.Printf("%s\n", createdDevice.Id)
Zack Williamse940c7a2019-08-21 14:25:39 -0700323
324 return nil
325}
326
327func (options *DeviceDelete) Execute(args []string) error {
328
329 conn, err := NewConnection()
330 if err != nil {
331 return err
332 }
333 defer conn.Close()
334
Scott Baker9173ed82020-05-19 08:30:12 -0700335 client := voltha.NewVolthaServiceClient(conn)
Zack Williamse940c7a2019-08-21 14:25:39 -0700336
David Bainbridge7052fe82020-03-25 10:37:00 -0700337 var lastErr error
Zack Williamse940c7a2019-08-21 14:25:39 -0700338 for _, i := range options.Args.Ids {
Zack Williamse940c7a2019-08-21 14:25:39 -0700339 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
340 defer cancel()
341
Scott Baker9173ed82020-05-19 08:30:12 -0700342 id := voltha.ID{Id: string(i)}
343
344 _, err := client.DeleteDevice(ctx, &id)
Zack Williamse940c7a2019-08-21 14:25:39 -0700345 if err != nil {
David Bainbridge0f758d42019-10-26 05:17:48 +0000346 Error.Printf("Error while deleting '%s': %s\n", i, err)
David Bainbridge7052fe82020-03-25 10:37:00 -0700347 lastErr = err
Zack Williamse940c7a2019-08-21 14:25:39 -0700348 continue
Zack Williamse940c7a2019-08-21 14:25:39 -0700349 }
350 fmt.Printf("%s\n", i)
351 }
352
David Bainbridge7052fe82020-03-25 10:37:00 -0700353 if lastErr != nil {
354 return NoReportErr
355 }
Zack Williamse940c7a2019-08-21 14:25:39 -0700356 return nil
357}
358
359func (options *DeviceEnable) Execute(args []string) error {
360 conn, err := NewConnection()
361 if err != nil {
362 return err
363 }
364 defer conn.Close()
365
Scott Baker9173ed82020-05-19 08:30:12 -0700366 client := voltha.NewVolthaServiceClient(conn)
Zack Williamse940c7a2019-08-21 14:25:39 -0700367
David Bainbridge7052fe82020-03-25 10:37:00 -0700368 var lastErr error
Zack Williamse940c7a2019-08-21 14:25:39 -0700369 for _, i := range options.Args.Ids {
Zack Williamse940c7a2019-08-21 14:25:39 -0700370 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
371 defer cancel()
372
Scott Baker9173ed82020-05-19 08:30:12 -0700373 id := voltha.ID{Id: string(i)}
374
375 _, err := client.EnableDevice(ctx, &id)
Zack Williamse940c7a2019-08-21 14:25:39 -0700376 if err != nil {
David Bainbridge0f758d42019-10-26 05:17:48 +0000377 Error.Printf("Error while enabling '%s': %s\n", i, err)
David Bainbridge7052fe82020-03-25 10:37:00 -0700378 lastErr = err
Zack Williamse940c7a2019-08-21 14:25:39 -0700379 continue
Zack Williamse940c7a2019-08-21 14:25:39 -0700380 }
381 fmt.Printf("%s\n", i)
382 }
383
David Bainbridge7052fe82020-03-25 10:37:00 -0700384 if lastErr != nil {
385 return NoReportErr
386 }
Zack Williamse940c7a2019-08-21 14:25:39 -0700387 return nil
388}
389
390func (options *DeviceDisable) Execute(args []string) error {
391 conn, err := NewConnection()
392 if err != nil {
393 return err
394 }
395 defer conn.Close()
396
Scott Baker9173ed82020-05-19 08:30:12 -0700397 client := voltha.NewVolthaServiceClient(conn)
Zack Williamse940c7a2019-08-21 14:25:39 -0700398
David Bainbridge7052fe82020-03-25 10:37:00 -0700399 var lastErr error
Zack Williamse940c7a2019-08-21 14:25:39 -0700400 for _, i := range options.Args.Ids {
Zack Williamse940c7a2019-08-21 14:25:39 -0700401 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
402 defer cancel()
403
Scott Baker9173ed82020-05-19 08:30:12 -0700404 id := voltha.ID{Id: string(i)}
405
406 _, err := client.DisableDevice(ctx, &id)
Zack Williamse940c7a2019-08-21 14:25:39 -0700407 if err != nil {
David Bainbridge0f758d42019-10-26 05:17:48 +0000408 Error.Printf("Error while disabling '%s': %s\n", i, err)
David Bainbridge7052fe82020-03-25 10:37:00 -0700409 lastErr = err
Zack Williamse940c7a2019-08-21 14:25:39 -0700410 continue
Zack Williamse940c7a2019-08-21 14:25:39 -0700411 }
412 fmt.Printf("%s\n", i)
413 }
414
David Bainbridge7052fe82020-03-25 10:37:00 -0700415 if lastErr != nil {
416 return NoReportErr
417 }
Zack Williamse940c7a2019-08-21 14:25:39 -0700418 return nil
419}
420
421func (options *DeviceReboot) Execute(args []string) error {
422 conn, err := NewConnection()
423 if err != nil {
424 return err
425 }
426 defer conn.Close()
427
Scott Baker9173ed82020-05-19 08:30:12 -0700428 client := voltha.NewVolthaServiceClient(conn)
Zack Williamse940c7a2019-08-21 14:25:39 -0700429
David Bainbridge7052fe82020-03-25 10:37:00 -0700430 var lastErr error
Zack Williamse940c7a2019-08-21 14:25:39 -0700431 for _, i := range options.Args.Ids {
Zack Williamse940c7a2019-08-21 14:25:39 -0700432 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
433 defer cancel()
434
Scott Baker9173ed82020-05-19 08:30:12 -0700435 id := voltha.ID{Id: string(i)}
436
437 _, err := client.RebootDevice(ctx, &id)
Zack Williamse940c7a2019-08-21 14:25:39 -0700438 if err != nil {
David Bainbridge0f758d42019-10-26 05:17:48 +0000439 Error.Printf("Error while rebooting '%s': %s\n", i, err)
David Bainbridge7052fe82020-03-25 10:37:00 -0700440 lastErr = err
Zack Williamse940c7a2019-08-21 14:25:39 -0700441 continue
Zack Williamse940c7a2019-08-21 14:25:39 -0700442 }
443 fmt.Printf("%s\n", i)
444 }
445
David Bainbridge7052fe82020-03-25 10:37:00 -0700446 if lastErr != nil {
447 return NoReportErr
448 }
Zack Williamse940c7a2019-08-21 14:25:39 -0700449 return nil
450}
451
452func (options *DevicePortList) Execute(args []string) error {
453
454 conn, err := NewConnection()
455 if err != nil {
456 return err
457 }
458 defer conn.Close()
459
Scott Baker9173ed82020-05-19 08:30:12 -0700460 client := voltha.NewVolthaServiceClient(conn)
Zack Williamse940c7a2019-08-21 14:25:39 -0700461
462 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
463 defer cancel()
464
Scott Baker9173ed82020-05-19 08:30:12 -0700465 id := voltha.ID{Id: string(options.Args.Id)}
Zack Williamse940c7a2019-08-21 14:25:39 -0700466
Scott Baker9173ed82020-05-19 08:30:12 -0700467 ports, err := client.ListDevicePorts(ctx, &id)
Zack Williamse940c7a2019-08-21 14:25:39 -0700468 if err != nil {
469 return err
470 }
471
472 outputFormat := CharReplacer.Replace(options.Format)
473 if outputFormat == "" {
David Bainbridgea6722342019-10-24 23:55:53 +0000474 outputFormat = GetCommandOptionWithDefault("device-ports", "format", DEFAULT_DEVICE_PORTS_FORMAT)
Zack Williamse940c7a2019-08-21 14:25:39 -0700475 }
476 if options.Quiet {
477 outputFormat = "{{.Id}}"
478 }
479
David Bainbridgea6722342019-10-24 23:55:53 +0000480 orderBy := options.OrderBy
481 if orderBy == "" {
482 orderBy = GetCommandOptionWithDefault("device-ports", "order", "")
483 }
484
Zack Williamse940c7a2019-08-21 14:25:39 -0700485 result := CommandResult{
486 Format: format.Format(outputFormat),
487 Filter: options.Filter,
David Bainbridgea6722342019-10-24 23:55:53 +0000488 OrderBy: orderBy,
Zack Williamse940c7a2019-08-21 14:25:39 -0700489 OutputAs: toOutputType(options.OutputAs),
490 NameLimit: options.NameLimit,
Scott Baker9173ed82020-05-19 08:30:12 -0700491 Data: ports.Items,
Zack Williamse940c7a2019-08-21 14:25:39 -0700492 }
493
494 GenerateOutput(&result)
495 return nil
496}
497
498func (options *DeviceFlowList) Execute(args []string) error {
499 fl := &FlowList{}
500 fl.ListOutputOptions = options.ListOutputOptions
501 fl.Args.Id = string(options.Args.Id)
David Bainbridgea6722342019-10-24 23:55:53 +0000502 fl.Method = "device-flows"
Zack Williamse940c7a2019-08-21 14:25:39 -0700503 return fl.Execute(args)
504}
505
506func (options *DeviceInspect) Execute(args []string) error {
507 if len(args) > 0 {
508 return fmt.Errorf("only a single argument 'DEVICE_ID' can be provided")
509 }
510
511 conn, err := NewConnection()
512 if err != nil {
513 return err
514 }
515 defer conn.Close()
516
Scott Baker9173ed82020-05-19 08:30:12 -0700517 client := voltha.NewVolthaServiceClient(conn)
Zack Williamse940c7a2019-08-21 14:25:39 -0700518
519 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
520 defer cancel()
521
Scott Baker9173ed82020-05-19 08:30:12 -0700522 id := voltha.ID{Id: string(options.Args.Id)}
Zack Williamse940c7a2019-08-21 14:25:39 -0700523
Scott Baker9173ed82020-05-19 08:30:12 -0700524 device, err := client.GetDevice(ctx, &id)
Zack Williamse940c7a2019-08-21 14:25:39 -0700525 if err != nil {
526 return err
527 }
528
Zack Williamse940c7a2019-08-21 14:25:39 -0700529 outputFormat := CharReplacer.Replace(options.Format)
530 if outputFormat == "" {
David Bainbridgea6722342019-10-24 23:55:53 +0000531 outputFormat = GetCommandOptionWithDefault("device-inspect", "format", DEFAULT_DEVICE_INSPECT_FORMAT)
Zack Williamse940c7a2019-08-21 14:25:39 -0700532 }
533 if options.Quiet {
534 outputFormat = "{{.Id}}"
535 }
536
537 result := CommandResult{
538 Format: format.Format(outputFormat),
539 OutputAs: toOutputType(options.OutputAs),
540 NameLimit: options.NameLimit,
541 Data: device,
542 }
543 GenerateOutput(&result)
544 return nil
545}
kesavand12cd8eb2020-01-20 22:25:22 -0500546
547/*Device Port Enable */
548func (options *DevicePortEnable) Execute(args []string) error {
549 conn, err := NewConnection()
550 if err != nil {
551 return err
552 }
553 defer conn.Close()
554
Scott Baker9173ed82020-05-19 08:30:12 -0700555 client := voltha.NewVolthaServiceClient(conn)
kesavand12cd8eb2020-01-20 22:25:22 -0500556
kesavand12cd8eb2020-01-20 22:25:22 -0500557 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
558 defer cancel()
559
Scott Baker9173ed82020-05-19 08:30:12 -0700560 port := voltha.Port{DeviceId: string(options.Args.Id), PortNo: uint32(options.Args.PortId)}
561
562 _, err = client.EnablePort(ctx, &port)
kesavand12cd8eb2020-01-20 22:25:22 -0500563 if err != nil {
564 Error.Printf("Error enabling port number %v on device Id %s,err=%s\n", options.Args.PortId, options.Args.Id, ErrorToString(err))
565 return err
kesavand12cd8eb2020-01-20 22:25:22 -0500566 }
567
568 return nil
569}
570
Scott Baker9173ed82020-05-19 08:30:12 -0700571/*Device Port Disable */
kesavand12cd8eb2020-01-20 22:25:22 -0500572func (options *DevicePortDisable) Execute(args []string) error {
573 conn, err := NewConnection()
574 if err != nil {
575 return err
576 }
577 defer conn.Close()
578
Scott Baker9173ed82020-05-19 08:30:12 -0700579 client := voltha.NewVolthaServiceClient(conn)
580
kesavand12cd8eb2020-01-20 22:25:22 -0500581 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
582 defer cancel()
583
Scott Baker9173ed82020-05-19 08:30:12 -0700584 port := voltha.Port{DeviceId: string(options.Args.Id), PortNo: uint32(options.Args.PortId)}
585
586 _, err = client.DisablePort(ctx, &port)
kesavand12cd8eb2020-01-20 22:25:22 -0500587 if err != nil {
Scott Baker9173ed82020-05-19 08:30:12 -0700588 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 -0500589 return err
kesavand12cd8eb2020-01-20 22:25:22 -0500590 }
Scott Baker9173ed82020-05-19 08:30:12 -0700591
kesavand12cd8eb2020-01-20 22:25:22 -0500592 return nil
593}
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -0800594
Scott Baker9173ed82020-05-19 08:30:12 -0700595type ReturnValueRow struct {
596 Name string `json:"name"`
597 Result interface{} `json:"result"`
598}
599
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -0800600/*Device get Onu Distance */
601func (options *DeviceGetExtValue) Execute(args []string) error {
602 conn, err := NewConnection()
603 if err != nil {
604 return err
605 }
606 defer conn.Close()
607
Scott Baker9173ed82020-05-19 08:30:12 -0700608 client := voltha.NewVolthaServiceClient(conn)
609
610 valueflag, okay := common.ValueType_Type_value[string(options.Args.Valueflag)]
611 if !okay {
612 Error.Printf("Unknown valueflag %s\n", options.Args.Valueflag)
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -0800613 }
614
Scott Baker9173ed82020-05-19 08:30:12 -0700615 val := voltha.ValueSpecifier{Id: string(options.Args.Id), Value: common.ValueType_Type(valueflag)}
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -0800616
617 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
618 defer cancel()
619
Scott Baker9173ed82020-05-19 08:30:12 -0700620 rv, err := client.GetExtValue(ctx, &val)
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -0800621 if err != nil {
622 Error.Printf("Error getting value on device Id %s,err=%s\n", options.Args.Id, ErrorToString(err))
623 return err
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -0800624 }
625
Scott Baker9173ed82020-05-19 08:30:12 -0700626 var rows []ReturnValueRow
627 for name, num := range common.ValueType_Type_value {
628 if num == 0 {
629 // EMPTY is not a real value
630 continue
631 }
632 if (rv.Error & uint32(num)) != 0 {
633 row := ReturnValueRow{Name: name, Result: "Error"}
634 rows = append(rows, row)
635 }
636 if (rv.Unsupported & uint32(num)) != 0 {
637 row := ReturnValueRow{Name: name, Result: "Unsupported"}
638 rows = append(rows, row)
639 }
640 if (rv.Set & uint32(num)) != 0 {
641 switch name {
642 case "DISTANCE":
643 row := ReturnValueRow{Name: name, Result: rv.Distance}
644 rows = append(rows, row)
645 default:
646 row := ReturnValueRow{Name: name, Result: "Unimplemented-in-voltctl"}
647 rows = append(rows, row)
648 }
649 }
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -0800650 }
651
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -0800652 outputFormat := CharReplacer.Replace(options.Format)
653 if outputFormat == "" {
654 outputFormat = GetCommandOptionWithDefault("device-value-get", "format", DEFAULT_DEVICE_VALUE_GET_FORMAT)
655 }
656
657 result := CommandResult{
658 Format: format.Format(outputFormat),
659 OutputAs: toOutputType(options.OutputAs),
660 NameLimit: options.NameLimit,
Scott Baker9173ed82020-05-19 08:30:12 -0700661 Data: rows,
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -0800662 }
663 GenerateOutput(&result)
664 return nil
665}