Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package commands |
| 17 | |
| 18 | import ( |
| 19 | "context" |
| 20 | "fmt" |
David Bainbridge | 7052fe8 | 2020-03-25 10:37:00 -0700 | [diff] [blame] | 21 | "os" |
| 22 | "strconv" |
| 23 | "strings" |
| 24 | |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 25 | "github.com/golang/protobuf/ptypes/empty" |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 26 | flags "github.com/jessevdk/go-flags" |
Scott Baker | 2b0ad65 | 2019-08-21 14:57:07 -0700 | [diff] [blame] | 27 | "github.com/opencord/voltctl/pkg/format" |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 28 | "github.com/opencord/voltha-protos/v3/go/common" |
| 29 | "github.com/opencord/voltha-protos/v3/go/voltha" |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 30 | ) |
| 31 | |
| 32 | const ( |
David K. Bainbridge | 89003c4 | 2020-02-27 17:22:49 -0800 | [diff] [blame] | 33 | DEFAULT_DEVICE_FORMAT = "table{{ .Id }}\t{{.Type}}\t{{.Root}}\t{{.ParentId}}\t{{.SerialNumber}}\t{{.AdminState}}\t{{.OperStatus}}\t{{.ConnectStatus}}\t{{.Reason}}" |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 34 | 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 Belwalkar | c9aa6d8 | 2020-03-04 15:22:17 -0800 | [diff] [blame] | 44 | DEFAULT_DEVICE_VALUE_GET_FORMAT = "table{{.Name}}\t{{.Result}}" |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 45 | ) |
| 46 | |
| 47 | type DeviceList struct { |
| 48 | ListOutputOptions |
| 49 | } |
| 50 | |
| 51 | type DeviceCreate struct { |
David Bainbridge | 835dd0e | 2020-04-01 10:30:09 -0700 | [diff] [blame] | 52 | DeviceType string `short:"t" long:"devicetype" default:"" description:"Device type"` |
| 53 | MACAddress string `short:"m" long:"macaddress" default:"" description:"MAC Address"` |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 54 | IPAddress string `short:"i" long:"ipaddress" default:"" description:"IP Address"` |
| 55 | HostAndPort string `short:"H" long:"hostandport" default:"" description:"Host and port"` |
| 56 | } |
| 57 | |
| 58 | type DeviceId string |
| 59 | |
kesavand | 12cd8eb | 2020-01-20 22:25:22 -0500 | [diff] [blame] | 60 | type PortNum uint32 |
Dinesh Belwalkar | c9aa6d8 | 2020-03-04 15:22:17 -0800 | [diff] [blame] | 61 | type ValueFlag string |
kesavand | 12cd8eb | 2020-01-20 22:25:22 -0500 | [diff] [blame] | 62 | |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 63 | type DeviceDelete struct { |
| 64 | Args struct { |
| 65 | Ids []DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"` |
| 66 | } `positional-args:"yes"` |
| 67 | } |
| 68 | |
| 69 | type DeviceEnable struct { |
| 70 | Args struct { |
| 71 | Ids []DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"` |
| 72 | } `positional-args:"yes"` |
| 73 | } |
| 74 | |
| 75 | type DeviceDisable struct { |
| 76 | Args struct { |
| 77 | Ids []DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"` |
| 78 | } `positional-args:"yes"` |
| 79 | } |
| 80 | |
| 81 | type DeviceReboot struct { |
| 82 | Args struct { |
| 83 | Ids []DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"` |
| 84 | } `positional-args:"yes"` |
| 85 | } |
| 86 | |
| 87 | type DeviceFlowList struct { |
| 88 | ListOutputOptions |
| 89 | Args struct { |
| 90 | Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"` |
| 91 | } `positional-args:"yes"` |
| 92 | } |
| 93 | |
| 94 | type DevicePortList struct { |
| 95 | ListOutputOptions |
| 96 | Args struct { |
| 97 | Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"` |
| 98 | } `positional-args:"yes"` |
| 99 | } |
| 100 | |
| 101 | type DeviceInspect struct { |
| 102 | OutputOptionsJson |
| 103 | Args struct { |
| 104 | Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"` |
| 105 | } `positional-args:"yes"` |
| 106 | } |
| 107 | |
kesavand | 12cd8eb | 2020-01-20 22:25:22 -0500 | [diff] [blame] | 108 | type 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 | |
| 115 | type 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 Belwalkar | c9aa6d8 | 2020-03-04 15:22:17 -0800 | [diff] [blame] | 122 | type 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 Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 129 | type 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"` |
kesavand | 12cd8eb | 2020-01-20 22:25:22 -0500 | [diff] [blame] | 136 | 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 Belwalkar | c9aa6d8 | 2020-03-04 15:22:17 -0800 | [diff] [blame] | 143 | Value struct { |
| 144 | Get DeviceGetExtValue `command:"get"` |
| 145 | } `command:"value"` |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | var deviceOpts = DeviceOpts{} |
| 149 | |
| 150 | func RegisterDeviceCommands(parser *flags.Parser) { |
David Bainbridge | 12f036f | 2019-10-15 22:09:04 +0000 | [diff] [blame] | 151 | if _, err := parser.AddCommand("device", "device commands", "Commands to query and manipulate VOLTHA devices", &deviceOpts); err != nil { |
David Bainbridge | a672234 | 2019-10-24 23:55:53 +0000 | [diff] [blame] | 152 | Error.Fatalf("Unexpected error while attempting to register device commands : %s", err) |
David Bainbridge | 12f036f | 2019-10-15 22:09:04 +0000 | [diff] [blame] | 153 | } |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 154 | } |
| 155 | |
kesavand | 12cd8eb | 2020-01-20 22:25:22 -0500 | [diff] [blame] | 156 | func (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 Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 163 | client := voltha.NewVolthaServiceClient(conn) |
kesavand | 12cd8eb | 2020-01-20 22:25:22 -0500 | [diff] [blame] | 164 | |
| 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 |
| 174 | found: |
| 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 | |
kesavand | 12cd8eb | 2020-01-20 22:25:22 -0500 | [diff] [blame] | 194 | ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout) |
| 195 | defer cancel() |
kesavand | 12cd8eb | 2020-01-20 22:25:22 -0500 | [diff] [blame] | 196 | |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 197 | id := voltha.ID{Id: string(deviceId)} |
kesavand | 12cd8eb | 2020-01-20 22:25:22 -0500 | [diff] [blame] | 198 | |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 199 | ports, err := client.ListDevicePorts(ctx, &id) |
kesavand | 12cd8eb | 2020-01-20 22:25:22 -0500 | [diff] [blame] | 200 | if err != nil { |
| 201 | return nil |
| 202 | } |
| 203 | |
| 204 | list := make([]flags.Completion, 0) |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 205 | for _, item := range ports.Items { |
| 206 | pn := strconv.FormatUint(uint64(item.PortNo), 10) |
kesavand | 12cd8eb | 2020-01-20 22:25:22 -0500 | [diff] [blame] | 207 | if strings.HasPrefix(pn, match) { |
| 208 | list = append(list, flags.Completion{Item: pn}) |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | return list |
| 213 | } |
| 214 | |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 215 | func (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 Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 222 | client := voltha.NewVolthaServiceClient(conn) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 223 | |
| 224 | ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout) |
| 225 | defer cancel() |
| 226 | |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 227 | devices, err := client.ListDevices(ctx, &empty.Empty{}) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 228 | if err != nil { |
| 229 | return nil |
| 230 | } |
| 231 | |
| 232 | list := make([]flags.Completion, 0) |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 233 | for _, item := range devices.Items { |
| 234 | if strings.HasPrefix(item.Id, match) { |
| 235 | list = append(list, flags.Completion{Item: item.Id}) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 236 | } |
| 237 | } |
| 238 | |
| 239 | return list |
| 240 | } |
| 241 | |
| 242 | func (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 Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 250 | client := voltha.NewVolthaServiceClient(conn) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 251 | |
| 252 | ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout) |
| 253 | defer cancel() |
| 254 | |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 255 | devices, err := client.ListDevices(ctx, &empty.Empty{}) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 256 | if err != nil { |
| 257 | return err |
| 258 | } |
| 259 | |
| 260 | outputFormat := CharReplacer.Replace(options.Format) |
| 261 | if outputFormat == "" { |
David Bainbridge | a672234 | 2019-10-24 23:55:53 +0000 | [diff] [blame] | 262 | outputFormat = GetCommandOptionWithDefault("device-list", "format", DEFAULT_DEVICE_FORMAT) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 263 | } |
| 264 | if options.Quiet { |
| 265 | outputFormat = "{{.Id}}" |
| 266 | } |
| 267 | |
David Bainbridge | a672234 | 2019-10-24 23:55:53 +0000 | [diff] [blame] | 268 | orderBy := options.OrderBy |
| 269 | if orderBy == "" { |
| 270 | orderBy = GetCommandOptionWithDefault("device-list", "order", "") |
| 271 | } |
| 272 | |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 273 | // Make sure json output prints an empty list, not "null" |
| 274 | if devices.Items == nil { |
| 275 | devices.Items = make([]*voltha.Device, 0) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | result := CommandResult{ |
| 279 | Format: format.Format(outputFormat), |
| 280 | Filter: options.Filter, |
David Bainbridge | a672234 | 2019-10-24 23:55:53 +0000 | [diff] [blame] | 281 | OrderBy: orderBy, |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 282 | OutputAs: toOutputType(options.OutputAs), |
| 283 | NameLimit: options.NameLimit, |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 284 | Data: devices.Items, |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | GenerateOutput(&result) |
| 288 | return nil |
| 289 | } |
| 290 | |
| 291 | func (options *DeviceCreate) Execute(args []string) error { |
| 292 | |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 293 | device := voltha.Device{} |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 294 | if options.HostAndPort != "" { |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 295 | device.Address = &voltha.Device_HostAndPort{HostAndPort: options.HostAndPort} |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 296 | } else if options.IPAddress != "" { |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 297 | device.Address = &voltha.Device_Ipv4Address{Ipv4Address: options.IPAddress} |
Hardik Windlass | ce1de34 | 2020-02-04 21:58:07 +0000 | [diff] [blame] | 298 | } |
| 299 | if options.MACAddress != "" { |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 300 | device.MacAddress = strings.ToLower(options.MACAddress) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 301 | } |
| 302 | if options.DeviceType != "" { |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 303 | device.Type = options.DeviceType |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | conn, err := NewConnection() |
| 307 | if err != nil { |
| 308 | return err |
| 309 | } |
| 310 | defer conn.Close() |
| 311 | |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 312 | client := voltha.NewVolthaServiceClient(conn) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 313 | |
| 314 | ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout) |
| 315 | defer cancel() |
| 316 | |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 317 | createdDevice, err := client.CreateDevice(ctx, &device) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 318 | if err != nil { |
| 319 | return err |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 320 | } |
| 321 | |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 322 | fmt.Printf("%s\n", createdDevice.Id) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 323 | |
| 324 | return nil |
| 325 | } |
| 326 | |
| 327 | func (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 Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 335 | client := voltha.NewVolthaServiceClient(conn) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 336 | |
David Bainbridge | 7052fe8 | 2020-03-25 10:37:00 -0700 | [diff] [blame] | 337 | var lastErr error |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 338 | for _, i := range options.Args.Ids { |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 339 | ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout) |
| 340 | defer cancel() |
| 341 | |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 342 | id := voltha.ID{Id: string(i)} |
| 343 | |
| 344 | _, err := client.DeleteDevice(ctx, &id) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 345 | if err != nil { |
David Bainbridge | 0f758d4 | 2019-10-26 05:17:48 +0000 | [diff] [blame] | 346 | Error.Printf("Error while deleting '%s': %s\n", i, err) |
David Bainbridge | 7052fe8 | 2020-03-25 10:37:00 -0700 | [diff] [blame] | 347 | lastErr = err |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 348 | continue |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 349 | } |
| 350 | fmt.Printf("%s\n", i) |
| 351 | } |
| 352 | |
David Bainbridge | 7052fe8 | 2020-03-25 10:37:00 -0700 | [diff] [blame] | 353 | if lastErr != nil { |
| 354 | return NoReportErr |
| 355 | } |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 356 | return nil |
| 357 | } |
| 358 | |
| 359 | func (options *DeviceEnable) Execute(args []string) error { |
| 360 | conn, err := NewConnection() |
| 361 | if err != nil { |
| 362 | return err |
| 363 | } |
| 364 | defer conn.Close() |
| 365 | |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 366 | client := voltha.NewVolthaServiceClient(conn) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 367 | |
David Bainbridge | 7052fe8 | 2020-03-25 10:37:00 -0700 | [diff] [blame] | 368 | var lastErr error |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 369 | for _, i := range options.Args.Ids { |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 370 | ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout) |
| 371 | defer cancel() |
| 372 | |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 373 | id := voltha.ID{Id: string(i)} |
| 374 | |
| 375 | _, err := client.EnableDevice(ctx, &id) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 376 | if err != nil { |
David Bainbridge | 0f758d4 | 2019-10-26 05:17:48 +0000 | [diff] [blame] | 377 | Error.Printf("Error while enabling '%s': %s\n", i, err) |
David Bainbridge | 7052fe8 | 2020-03-25 10:37:00 -0700 | [diff] [blame] | 378 | lastErr = err |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 379 | continue |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 380 | } |
| 381 | fmt.Printf("%s\n", i) |
| 382 | } |
| 383 | |
David Bainbridge | 7052fe8 | 2020-03-25 10:37:00 -0700 | [diff] [blame] | 384 | if lastErr != nil { |
| 385 | return NoReportErr |
| 386 | } |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 387 | return nil |
| 388 | } |
| 389 | |
| 390 | func (options *DeviceDisable) Execute(args []string) error { |
| 391 | conn, err := NewConnection() |
| 392 | if err != nil { |
| 393 | return err |
| 394 | } |
| 395 | defer conn.Close() |
| 396 | |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 397 | client := voltha.NewVolthaServiceClient(conn) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 398 | |
David Bainbridge | 7052fe8 | 2020-03-25 10:37:00 -0700 | [diff] [blame] | 399 | var lastErr error |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 400 | for _, i := range options.Args.Ids { |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 401 | ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout) |
| 402 | defer cancel() |
| 403 | |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 404 | id := voltha.ID{Id: string(i)} |
| 405 | |
| 406 | _, err := client.DisableDevice(ctx, &id) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 407 | if err != nil { |
David Bainbridge | 0f758d4 | 2019-10-26 05:17:48 +0000 | [diff] [blame] | 408 | Error.Printf("Error while disabling '%s': %s\n", i, err) |
David Bainbridge | 7052fe8 | 2020-03-25 10:37:00 -0700 | [diff] [blame] | 409 | lastErr = err |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 410 | continue |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 411 | } |
| 412 | fmt.Printf("%s\n", i) |
| 413 | } |
| 414 | |
David Bainbridge | 7052fe8 | 2020-03-25 10:37:00 -0700 | [diff] [blame] | 415 | if lastErr != nil { |
| 416 | return NoReportErr |
| 417 | } |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 418 | return nil |
| 419 | } |
| 420 | |
| 421 | func (options *DeviceReboot) Execute(args []string) error { |
| 422 | conn, err := NewConnection() |
| 423 | if err != nil { |
| 424 | return err |
| 425 | } |
| 426 | defer conn.Close() |
| 427 | |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 428 | client := voltha.NewVolthaServiceClient(conn) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 429 | |
David Bainbridge | 7052fe8 | 2020-03-25 10:37:00 -0700 | [diff] [blame] | 430 | var lastErr error |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 431 | for _, i := range options.Args.Ids { |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 432 | ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout) |
| 433 | defer cancel() |
| 434 | |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 435 | id := voltha.ID{Id: string(i)} |
| 436 | |
| 437 | _, err := client.RebootDevice(ctx, &id) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 438 | if err != nil { |
David Bainbridge | 0f758d4 | 2019-10-26 05:17:48 +0000 | [diff] [blame] | 439 | Error.Printf("Error while rebooting '%s': %s\n", i, err) |
David Bainbridge | 7052fe8 | 2020-03-25 10:37:00 -0700 | [diff] [blame] | 440 | lastErr = err |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 441 | continue |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 442 | } |
| 443 | fmt.Printf("%s\n", i) |
| 444 | } |
| 445 | |
David Bainbridge | 7052fe8 | 2020-03-25 10:37:00 -0700 | [diff] [blame] | 446 | if lastErr != nil { |
| 447 | return NoReportErr |
| 448 | } |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 449 | return nil |
| 450 | } |
| 451 | |
| 452 | func (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 Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 460 | client := voltha.NewVolthaServiceClient(conn) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 461 | |
| 462 | ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout) |
| 463 | defer cancel() |
| 464 | |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 465 | id := voltha.ID{Id: string(options.Args.Id)} |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 466 | |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 467 | ports, err := client.ListDevicePorts(ctx, &id) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 468 | if err != nil { |
| 469 | return err |
| 470 | } |
| 471 | |
| 472 | outputFormat := CharReplacer.Replace(options.Format) |
| 473 | if outputFormat == "" { |
David Bainbridge | a672234 | 2019-10-24 23:55:53 +0000 | [diff] [blame] | 474 | outputFormat = GetCommandOptionWithDefault("device-ports", "format", DEFAULT_DEVICE_PORTS_FORMAT) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 475 | } |
| 476 | if options.Quiet { |
| 477 | outputFormat = "{{.Id}}" |
| 478 | } |
| 479 | |
David Bainbridge | a672234 | 2019-10-24 23:55:53 +0000 | [diff] [blame] | 480 | orderBy := options.OrderBy |
| 481 | if orderBy == "" { |
| 482 | orderBy = GetCommandOptionWithDefault("device-ports", "order", "") |
| 483 | } |
| 484 | |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 485 | result := CommandResult{ |
| 486 | Format: format.Format(outputFormat), |
| 487 | Filter: options.Filter, |
David Bainbridge | a672234 | 2019-10-24 23:55:53 +0000 | [diff] [blame] | 488 | OrderBy: orderBy, |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 489 | OutputAs: toOutputType(options.OutputAs), |
| 490 | NameLimit: options.NameLimit, |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 491 | Data: ports.Items, |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | GenerateOutput(&result) |
| 495 | return nil |
| 496 | } |
| 497 | |
| 498 | func (options *DeviceFlowList) Execute(args []string) error { |
| 499 | fl := &FlowList{} |
| 500 | fl.ListOutputOptions = options.ListOutputOptions |
| 501 | fl.Args.Id = string(options.Args.Id) |
David Bainbridge | a672234 | 2019-10-24 23:55:53 +0000 | [diff] [blame] | 502 | fl.Method = "device-flows" |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 503 | return fl.Execute(args) |
| 504 | } |
| 505 | |
| 506 | func (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 Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 517 | client := voltha.NewVolthaServiceClient(conn) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 518 | |
| 519 | ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout) |
| 520 | defer cancel() |
| 521 | |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 522 | id := voltha.ID{Id: string(options.Args.Id)} |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 523 | |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 524 | device, err := client.GetDevice(ctx, &id) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 525 | if err != nil { |
| 526 | return err |
| 527 | } |
| 528 | |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 529 | outputFormat := CharReplacer.Replace(options.Format) |
| 530 | if outputFormat == "" { |
David Bainbridge | a672234 | 2019-10-24 23:55:53 +0000 | [diff] [blame] | 531 | outputFormat = GetCommandOptionWithDefault("device-inspect", "format", DEFAULT_DEVICE_INSPECT_FORMAT) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 532 | } |
| 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 | } |
kesavand | 12cd8eb | 2020-01-20 22:25:22 -0500 | [diff] [blame] | 546 | |
| 547 | /*Device Port Enable */ |
| 548 | func (options *DevicePortEnable) Execute(args []string) error { |
| 549 | conn, err := NewConnection() |
| 550 | if err != nil { |
| 551 | return err |
| 552 | } |
| 553 | defer conn.Close() |
| 554 | |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 555 | client := voltha.NewVolthaServiceClient(conn) |
kesavand | 12cd8eb | 2020-01-20 22:25:22 -0500 | [diff] [blame] | 556 | |
kesavand | 12cd8eb | 2020-01-20 22:25:22 -0500 | [diff] [blame] | 557 | ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout) |
| 558 | defer cancel() |
| 559 | |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 560 | port := voltha.Port{DeviceId: string(options.Args.Id), PortNo: uint32(options.Args.PortId)} |
| 561 | |
| 562 | _, err = client.EnablePort(ctx, &port) |
kesavand | 12cd8eb | 2020-01-20 22:25:22 -0500 | [diff] [blame] | 563 | 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 |
kesavand | 12cd8eb | 2020-01-20 22:25:22 -0500 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | return nil |
| 569 | } |
| 570 | |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 571 | /*Device Port Disable */ |
kesavand | 12cd8eb | 2020-01-20 22:25:22 -0500 | [diff] [blame] | 572 | func (options *DevicePortDisable) Execute(args []string) error { |
| 573 | conn, err := NewConnection() |
| 574 | if err != nil { |
| 575 | return err |
| 576 | } |
| 577 | defer conn.Close() |
| 578 | |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 579 | client := voltha.NewVolthaServiceClient(conn) |
| 580 | |
kesavand | 12cd8eb | 2020-01-20 22:25:22 -0500 | [diff] [blame] | 581 | ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout) |
| 582 | defer cancel() |
| 583 | |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 584 | port := voltha.Port{DeviceId: string(options.Args.Id), PortNo: uint32(options.Args.PortId)} |
| 585 | |
| 586 | _, err = client.DisablePort(ctx, &port) |
kesavand | 12cd8eb | 2020-01-20 22:25:22 -0500 | [diff] [blame] | 587 | if err != nil { |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 588 | Error.Printf("Error enabling port number %v on device Id %s,err=%s\n", options.Args.PortId, options.Args.Id, ErrorToString(err)) |
kesavand | 12cd8eb | 2020-01-20 22:25:22 -0500 | [diff] [blame] | 589 | return err |
kesavand | 12cd8eb | 2020-01-20 22:25:22 -0500 | [diff] [blame] | 590 | } |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 591 | |
kesavand | 12cd8eb | 2020-01-20 22:25:22 -0500 | [diff] [blame] | 592 | return nil |
| 593 | } |
Dinesh Belwalkar | c9aa6d8 | 2020-03-04 15:22:17 -0800 | [diff] [blame] | 594 | |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 595 | type ReturnValueRow struct { |
| 596 | Name string `json:"name"` |
| 597 | Result interface{} `json:"result"` |
| 598 | } |
| 599 | |
Dinesh Belwalkar | c9aa6d8 | 2020-03-04 15:22:17 -0800 | [diff] [blame] | 600 | /*Device get Onu Distance */ |
| 601 | func (options *DeviceGetExtValue) Execute(args []string) error { |
| 602 | conn, err := NewConnection() |
| 603 | if err != nil { |
| 604 | return err |
| 605 | } |
| 606 | defer conn.Close() |
| 607 | |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 608 | 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 Belwalkar | c9aa6d8 | 2020-03-04 15:22:17 -0800 | [diff] [blame] | 613 | } |
| 614 | |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 615 | val := voltha.ValueSpecifier{Id: string(options.Args.Id), Value: common.ValueType_Type(valueflag)} |
Dinesh Belwalkar | c9aa6d8 | 2020-03-04 15:22:17 -0800 | [diff] [blame] | 616 | |
| 617 | ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout) |
| 618 | defer cancel() |
| 619 | |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 620 | rv, err := client.GetExtValue(ctx, &val) |
Dinesh Belwalkar | c9aa6d8 | 2020-03-04 15:22:17 -0800 | [diff] [blame] | 621 | 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 Belwalkar | c9aa6d8 | 2020-03-04 15:22:17 -0800 | [diff] [blame] | 624 | } |
| 625 | |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 626 | 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 Belwalkar | c9aa6d8 | 2020-03-04 15:22:17 -0800 | [diff] [blame] | 650 | } |
| 651 | |
Dinesh Belwalkar | c9aa6d8 | 2020-03-04 15:22:17 -0800 | [diff] [blame] | 652 | 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 Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 661 | Data: rows, |
Dinesh Belwalkar | c9aa6d8 | 2020-03-04 15:22:17 -0800 | [diff] [blame] | 662 | } |
| 663 | GenerateOutput(&result) |
| 664 | return nil |
| 665 | } |