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" |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 21 | "github.com/fullstorydev/grpcurl" |
| 22 | flags "github.com/jessevdk/go-flags" |
| 23 | "github.com/jhump/protoreflect/dynamic" |
Scott Baker | 2b0ad65 | 2019-08-21 14:57:07 -0700 | [diff] [blame] | 24 | "github.com/opencord/voltctl/pkg/format" |
| 25 | "github.com/opencord/voltctl/pkg/model" |
kesavand | 12cd8eb | 2020-01-20 22:25:22 -0500 | [diff] [blame] | 26 | "os" |
| 27 | "strconv" |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 28 | "strings" |
| 29 | ) |
| 30 | |
| 31 | const ( |
| 32 | DEFAULT_DEVICE_FORMAT = "table{{ .Id }}\t{{.Type}}\t{{.Root}}\t{{.ParentId}}\t{{.SerialNumber}}\t{{.Vlan}}\t{{.AdminState}}\t{{.OperStatus}}\t{{.ConnectStatus}}" |
| 33 | DEFAULT_DEVICE_PORTS_FORMAT = "table{{.PortNo}}\t{{.Label}}\t{{.Type}}\t{{.AdminState}}\t{{.OperStatus}}\t{{.DeviceId}}\t{{.Peers}}" |
| 34 | DEFAULT_DEVICE_INSPECT_FORMAT = `ID: {{.Id}} |
| 35 | TYPE: {{.Type}} |
| 36 | ROOT: {{.Root}} |
| 37 | PARENTID: {{.ParentId}} |
| 38 | SERIALNUMBER: {{.SerialNumber}} |
| 39 | VLAN: {{.Vlan}} |
| 40 | ADMINSTATE: {{.AdminState}} |
| 41 | OPERSTATUS: {{.OperStatus}} |
| 42 | CONNECTSTATUS: {{.ConnectStatus}}` |
| 43 | ) |
| 44 | |
| 45 | type DeviceList struct { |
| 46 | ListOutputOptions |
| 47 | } |
| 48 | |
| 49 | type DeviceCreate struct { |
| 50 | DeviceType string `short:"t" long:"devicetype" default:"simulated_olt" description:"Device type"` |
| 51 | MACAddress string `short:"m" long:"macaddress" default:"00:0c:e2:31:40:00" description:"MAC Address"` |
| 52 | IPAddress string `short:"i" long:"ipaddress" default:"" description:"IP Address"` |
| 53 | HostAndPort string `short:"H" long:"hostandport" default:"" description:"Host and port"` |
| 54 | } |
| 55 | |
| 56 | type DeviceId string |
| 57 | |
kesavand | 12cd8eb | 2020-01-20 22:25:22 -0500 | [diff] [blame] | 58 | type PortNum uint32 |
| 59 | |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 60 | type DeviceDelete struct { |
| 61 | Args struct { |
| 62 | Ids []DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"` |
| 63 | } `positional-args:"yes"` |
| 64 | } |
| 65 | |
| 66 | type DeviceEnable struct { |
| 67 | Args struct { |
| 68 | Ids []DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"` |
| 69 | } `positional-args:"yes"` |
| 70 | } |
| 71 | |
| 72 | type DeviceDisable struct { |
| 73 | Args struct { |
| 74 | Ids []DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"` |
| 75 | } `positional-args:"yes"` |
| 76 | } |
| 77 | |
| 78 | type DeviceReboot struct { |
| 79 | Args struct { |
| 80 | Ids []DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"` |
| 81 | } `positional-args:"yes"` |
| 82 | } |
| 83 | |
| 84 | type DeviceFlowList struct { |
| 85 | ListOutputOptions |
| 86 | Args struct { |
| 87 | Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"` |
| 88 | } `positional-args:"yes"` |
| 89 | } |
| 90 | |
| 91 | type DevicePortList struct { |
| 92 | ListOutputOptions |
| 93 | Args struct { |
| 94 | Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"` |
| 95 | } `positional-args:"yes"` |
| 96 | } |
| 97 | |
| 98 | type DeviceInspect struct { |
| 99 | OutputOptionsJson |
| 100 | Args struct { |
| 101 | Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"` |
| 102 | } `positional-args:"yes"` |
| 103 | } |
| 104 | |
kesavand | 12cd8eb | 2020-01-20 22:25:22 -0500 | [diff] [blame] | 105 | type DevicePortEnable struct { |
| 106 | Args struct { |
| 107 | Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"` |
| 108 | PortId PortNum `positional-arg-name:"PORT_NUMBER" required:"yes"` |
| 109 | } `positional-args:"yes"` |
| 110 | } |
| 111 | |
| 112 | type DevicePortDisable struct { |
| 113 | Args struct { |
| 114 | Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"` |
| 115 | PortId PortNum `positional-arg-name:"PORT_NUMBER" required:"yes"` |
| 116 | } `positional-args:"yes"` |
| 117 | } |
| 118 | |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 119 | type DeviceOpts struct { |
| 120 | List DeviceList `command:"list"` |
| 121 | Create DeviceCreate `command:"create"` |
| 122 | Delete DeviceDelete `command:"delete"` |
| 123 | Enable DeviceEnable `command:"enable"` |
| 124 | Disable DeviceDisable `command:"disable"` |
| 125 | Flows DeviceFlowList `command:"flows"` |
kesavand | 12cd8eb | 2020-01-20 22:25:22 -0500 | [diff] [blame] | 126 | Port struct { |
| 127 | List DevicePortList `command:"list"` |
| 128 | Enable DevicePortEnable `command:"enable"` |
| 129 | Disable DevicePortDisable `command:"disable"` |
| 130 | } `command:"port"` |
| 131 | Inspect DeviceInspect `command:"inspect"` |
| 132 | Reboot DeviceReboot `command:"reboot"` |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | var deviceOpts = DeviceOpts{} |
| 136 | |
| 137 | func RegisterDeviceCommands(parser *flags.Parser) { |
David Bainbridge | 12f036f | 2019-10-15 22:09:04 +0000 | [diff] [blame] | 138 | 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] | 139 | Error.Fatalf("Unexpected error while attempting to register device commands : %s", err) |
David Bainbridge | 12f036f | 2019-10-15 22:09:04 +0000 | [diff] [blame] | 140 | } |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 141 | } |
| 142 | |
kesavand | 12cd8eb | 2020-01-20 22:25:22 -0500 | [diff] [blame] | 143 | func (i *PortNum) Complete(match string) []flags.Completion { |
| 144 | conn, err := NewConnection() |
| 145 | if err != nil { |
| 146 | return nil |
| 147 | } |
| 148 | defer conn.Close() |
| 149 | |
| 150 | descriptor, method, err := GetMethod("device-ports") |
| 151 | if err != nil { |
| 152 | return nil |
| 153 | } |
| 154 | |
| 155 | /* |
| 156 | * The command line args when completing for PortNum will be a DeviceId |
| 157 | * followed by one or more PortNums. So walk the argument list from the |
| 158 | * end and find the first argument that is enable/disable as those are |
| 159 | * the subcommands that come before the positional arguments. It would |
| 160 | * be nice if this package gave us the list of optional arguments |
| 161 | * already parsed. |
| 162 | */ |
| 163 | var deviceId string |
| 164 | found: |
| 165 | for i := len(os.Args) - 1; i >= 0; i -= 1 { |
| 166 | switch os.Args[i] { |
| 167 | case "enable": |
| 168 | fallthrough |
| 169 | case "disable": |
| 170 | if len(os.Args) > i+1 { |
| 171 | deviceId = os.Args[i+1] |
| 172 | } else { |
| 173 | return nil |
| 174 | } |
| 175 | break found |
| 176 | default: |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | if len(deviceId) == 0 { |
| 181 | return nil |
| 182 | } |
| 183 | |
| 184 | h := &RpcEventHandler{ |
| 185 | Fields: map[string]map[string]interface{}{ParamNames[GlobalConfig.ApiVersion]["ID"]: {"id": deviceId}}, |
| 186 | } |
| 187 | ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout) |
| 188 | defer cancel() |
| 189 | err = grpcurl.InvokeRPC(ctx, descriptor, conn, method, []string{}, h, h.GetParams) |
| 190 | if err != nil { |
| 191 | return nil |
| 192 | } |
| 193 | |
| 194 | if h.Status != nil && h.Status.Err() != nil { |
| 195 | return nil |
| 196 | } |
| 197 | |
| 198 | d, err := dynamic.AsDynamicMessage(h.Response) |
| 199 | if err != nil { |
| 200 | return nil |
| 201 | } |
| 202 | |
| 203 | items, err := d.TryGetFieldByName("items") |
| 204 | if err != nil { |
| 205 | return nil |
| 206 | } |
| 207 | |
| 208 | list := make([]flags.Completion, 0) |
| 209 | for _, item := range items.([]interface{}) { |
| 210 | val := item.(*dynamic.Message) |
| 211 | pn := strconv.FormatUint(uint64(val.GetFieldByName("port_no").(uint32)), 10) |
| 212 | if strings.HasPrefix(pn, match) { |
| 213 | list = append(list, flags.Completion{Item: pn}) |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | return list |
| 218 | } |
| 219 | |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 220 | func (i *DeviceId) Complete(match string) []flags.Completion { |
| 221 | conn, err := NewConnection() |
| 222 | if err != nil { |
| 223 | return nil |
| 224 | } |
| 225 | defer conn.Close() |
| 226 | |
| 227 | descriptor, method, err := GetMethod("device-list") |
| 228 | if err != nil { |
| 229 | return nil |
| 230 | } |
| 231 | |
| 232 | ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout) |
| 233 | defer cancel() |
| 234 | |
| 235 | h := &RpcEventHandler{} |
| 236 | err = grpcurl.InvokeRPC(ctx, descriptor, conn, method, []string{}, h, h.GetParams) |
| 237 | if err != nil { |
| 238 | return nil |
| 239 | } |
| 240 | |
| 241 | if h.Status != nil && h.Status.Err() != nil { |
| 242 | return nil |
| 243 | } |
| 244 | |
| 245 | d, err := dynamic.AsDynamicMessage(h.Response) |
| 246 | if err != nil { |
| 247 | return nil |
| 248 | } |
| 249 | |
| 250 | items, err := d.TryGetFieldByName("items") |
| 251 | if err != nil { |
| 252 | return nil |
| 253 | } |
| 254 | |
| 255 | list := make([]flags.Completion, 0) |
| 256 | for _, item := range items.([]interface{}) { |
| 257 | val := item.(*dynamic.Message) |
| 258 | id := val.GetFieldByName("id").(string) |
| 259 | if strings.HasPrefix(id, match) { |
| 260 | list = append(list, flags.Completion{Item: id}) |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | return list |
| 265 | } |
| 266 | |
| 267 | func (options *DeviceList) Execute(args []string) error { |
| 268 | |
| 269 | conn, err := NewConnection() |
| 270 | if err != nil { |
| 271 | return err |
| 272 | } |
| 273 | defer conn.Close() |
| 274 | |
| 275 | descriptor, method, err := GetMethod("device-list") |
| 276 | if err != nil { |
| 277 | return err |
| 278 | } |
| 279 | |
| 280 | ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout) |
| 281 | defer cancel() |
| 282 | |
| 283 | h := &RpcEventHandler{} |
| 284 | err = grpcurl.InvokeRPC(ctx, descriptor, conn, method, []string{}, h, h.GetParams) |
| 285 | if err != nil { |
| 286 | return err |
| 287 | } |
| 288 | |
| 289 | if h.Status != nil && h.Status.Err() != nil { |
| 290 | return h.Status.Err() |
| 291 | } |
| 292 | |
| 293 | d, err := dynamic.AsDynamicMessage(h.Response) |
| 294 | if err != nil { |
| 295 | return err |
| 296 | } |
| 297 | |
| 298 | items, err := d.TryGetFieldByName("items") |
| 299 | if err != nil { |
| 300 | return err |
| 301 | } |
| 302 | |
| 303 | outputFormat := CharReplacer.Replace(options.Format) |
| 304 | if outputFormat == "" { |
David Bainbridge | a672234 | 2019-10-24 23:55:53 +0000 | [diff] [blame] | 305 | outputFormat = GetCommandOptionWithDefault("device-list", "format", DEFAULT_DEVICE_FORMAT) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 306 | } |
| 307 | if options.Quiet { |
| 308 | outputFormat = "{{.Id}}" |
| 309 | } |
| 310 | |
David Bainbridge | a672234 | 2019-10-24 23:55:53 +0000 | [diff] [blame] | 311 | orderBy := options.OrderBy |
| 312 | if orderBy == "" { |
| 313 | orderBy = GetCommandOptionWithDefault("device-list", "order", "") |
| 314 | } |
| 315 | |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 316 | data := make([]model.Device, len(items.([]interface{}))) |
| 317 | for i, item := range items.([]interface{}) { |
| 318 | val := item.(*dynamic.Message) |
| 319 | data[i].PopulateFrom(val) |
| 320 | } |
| 321 | |
| 322 | result := CommandResult{ |
| 323 | Format: format.Format(outputFormat), |
| 324 | Filter: options.Filter, |
David Bainbridge | a672234 | 2019-10-24 23:55:53 +0000 | [diff] [blame] | 325 | OrderBy: orderBy, |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 326 | OutputAs: toOutputType(options.OutputAs), |
| 327 | NameLimit: options.NameLimit, |
| 328 | Data: data, |
| 329 | } |
| 330 | |
| 331 | GenerateOutput(&result) |
| 332 | return nil |
| 333 | } |
| 334 | |
| 335 | func (options *DeviceCreate) Execute(args []string) error { |
| 336 | |
| 337 | dm := make(map[string]interface{}) |
| 338 | if options.HostAndPort != "" { |
| 339 | dm["host_and_port"] = options.HostAndPort |
| 340 | } else if options.IPAddress != "" { |
| 341 | dm["ipv4_address"] = options.IPAddress |
Hardik Windlass | ce1de34 | 2020-02-04 21:58:07 +0000 | [diff] [blame] | 342 | } |
| 343 | if options.MACAddress != "" { |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 344 | dm["mac_address"] = strings.ToLower(options.MACAddress) |
| 345 | } |
| 346 | if options.DeviceType != "" { |
| 347 | dm["type"] = options.DeviceType |
| 348 | } |
| 349 | |
| 350 | conn, err := NewConnection() |
| 351 | if err != nil { |
| 352 | return err |
| 353 | } |
| 354 | defer conn.Close() |
| 355 | |
| 356 | descriptor, method, err := GetMethod("device-create") |
| 357 | if err != nil { |
| 358 | return err |
| 359 | } |
| 360 | |
| 361 | ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout) |
| 362 | defer cancel() |
| 363 | |
| 364 | h := &RpcEventHandler{ |
| 365 | Fields: map[string]map[string]interface{}{"voltha.Device": dm}, |
| 366 | } |
| 367 | err = grpcurl.InvokeRPC(ctx, descriptor, conn, method, []string{}, h, h.GetParams) |
| 368 | if err != nil { |
| 369 | return err |
| 370 | } else if h.Status != nil && h.Status.Err() != nil { |
| 371 | return h.Status.Err() |
| 372 | } |
| 373 | |
| 374 | resp, err := dynamic.AsDynamicMessage(h.Response) |
| 375 | if err != nil { |
| 376 | return err |
| 377 | } |
| 378 | fmt.Printf("%s\n", resp.GetFieldByName("id").(string)) |
| 379 | |
| 380 | return nil |
| 381 | } |
| 382 | |
| 383 | func (options *DeviceDelete) Execute(args []string) error { |
| 384 | |
| 385 | conn, err := NewConnection() |
| 386 | if err != nil { |
| 387 | return err |
| 388 | } |
| 389 | defer conn.Close() |
| 390 | |
| 391 | descriptor, method, err := GetMethod("device-delete") |
| 392 | if err != nil { |
| 393 | return err |
| 394 | } |
| 395 | |
| 396 | for _, i := range options.Args.Ids { |
| 397 | |
| 398 | h := &RpcEventHandler{ |
| 399 | Fields: map[string]map[string]interface{}{ParamNames[GlobalConfig.ApiVersion]["ID"]: {"id": i}}, |
| 400 | } |
| 401 | ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout) |
| 402 | defer cancel() |
| 403 | |
| 404 | err = grpcurl.InvokeRPC(ctx, descriptor, conn, method, []string{}, h, h.GetParams) |
| 405 | if err != nil { |
David Bainbridge | 0f758d4 | 2019-10-26 05:17:48 +0000 | [diff] [blame] | 406 | Error.Printf("Error while deleting '%s': %s\n", i, err) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 407 | continue |
| 408 | } else if h.Status != nil && h.Status.Err() != nil { |
David Bainbridge | 0f758d4 | 2019-10-26 05:17:48 +0000 | [diff] [blame] | 409 | Error.Printf("Error while deleting '%s': %s\n", i, ErrorToString(h.Status.Err())) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 410 | continue |
| 411 | } |
| 412 | fmt.Printf("%s\n", i) |
| 413 | } |
| 414 | |
| 415 | return nil |
| 416 | } |
| 417 | |
| 418 | func (options *DeviceEnable) Execute(args []string) error { |
| 419 | conn, err := NewConnection() |
| 420 | if err != nil { |
| 421 | return err |
| 422 | } |
| 423 | defer conn.Close() |
| 424 | |
| 425 | descriptor, method, err := GetMethod("device-enable") |
| 426 | if err != nil { |
| 427 | return err |
| 428 | } |
| 429 | |
| 430 | for _, i := range options.Args.Ids { |
| 431 | h := &RpcEventHandler{ |
| 432 | Fields: map[string]map[string]interface{}{ParamNames[GlobalConfig.ApiVersion]["ID"]: {"id": i}}, |
| 433 | } |
| 434 | ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout) |
| 435 | defer cancel() |
| 436 | |
| 437 | err = grpcurl.InvokeRPC(ctx, descriptor, conn, method, []string{}, h, h.GetParams) |
| 438 | if err != nil { |
David Bainbridge | 0f758d4 | 2019-10-26 05:17:48 +0000 | [diff] [blame] | 439 | Error.Printf("Error while enabling '%s': %s\n", i, err) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 440 | continue |
| 441 | } else if h.Status != nil && h.Status.Err() != nil { |
David Bainbridge | 0f758d4 | 2019-10-26 05:17:48 +0000 | [diff] [blame] | 442 | Error.Printf("Error while enabling '%s': %s\n", i, ErrorToString(h.Status.Err())) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 443 | continue |
| 444 | } |
| 445 | fmt.Printf("%s\n", i) |
| 446 | } |
| 447 | |
| 448 | return nil |
| 449 | } |
| 450 | |
| 451 | func (options *DeviceDisable) Execute(args []string) error { |
| 452 | conn, err := NewConnection() |
| 453 | if err != nil { |
| 454 | return err |
| 455 | } |
| 456 | defer conn.Close() |
| 457 | |
| 458 | descriptor, method, err := GetMethod("device-disable") |
| 459 | if err != nil { |
| 460 | return err |
| 461 | } |
| 462 | |
| 463 | for _, i := range options.Args.Ids { |
| 464 | h := &RpcEventHandler{ |
| 465 | Fields: map[string]map[string]interface{}{ParamNames[GlobalConfig.ApiVersion]["ID"]: {"id": i}}, |
| 466 | } |
| 467 | ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout) |
| 468 | defer cancel() |
| 469 | |
| 470 | err = grpcurl.InvokeRPC(ctx, descriptor, conn, method, []string{}, h, h.GetParams) |
| 471 | if err != nil { |
David Bainbridge | 0f758d4 | 2019-10-26 05:17:48 +0000 | [diff] [blame] | 472 | Error.Printf("Error while disabling '%s': %s\n", i, err) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 473 | continue |
| 474 | } else if h.Status != nil && h.Status.Err() != nil { |
David Bainbridge | 0f758d4 | 2019-10-26 05:17:48 +0000 | [diff] [blame] | 475 | Error.Printf("Error while disabling '%s': %s\n", i, ErrorToString(h.Status.Err())) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 476 | continue |
| 477 | } |
| 478 | fmt.Printf("%s\n", i) |
| 479 | } |
| 480 | |
| 481 | return nil |
| 482 | } |
| 483 | |
| 484 | func (options *DeviceReboot) Execute(args []string) error { |
| 485 | conn, err := NewConnection() |
| 486 | if err != nil { |
| 487 | return err |
| 488 | } |
| 489 | defer conn.Close() |
| 490 | |
| 491 | descriptor, method, err := GetMethod("device-reboot") |
| 492 | if err != nil { |
| 493 | return err |
| 494 | } |
| 495 | |
| 496 | for _, i := range options.Args.Ids { |
| 497 | h := &RpcEventHandler{ |
| 498 | Fields: map[string]map[string]interface{}{ParamNames[GlobalConfig.ApiVersion]["ID"]: {"id": i}}, |
| 499 | } |
| 500 | ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout) |
| 501 | defer cancel() |
| 502 | |
| 503 | err = grpcurl.InvokeRPC(ctx, descriptor, conn, method, []string{}, h, h.GetParams) |
| 504 | if err != nil { |
David Bainbridge | 0f758d4 | 2019-10-26 05:17:48 +0000 | [diff] [blame] | 505 | Error.Printf("Error while rebooting '%s': %s\n", i, err) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 506 | continue |
| 507 | } else if h.Status != nil && h.Status.Err() != nil { |
David Bainbridge | 0f758d4 | 2019-10-26 05:17:48 +0000 | [diff] [blame] | 508 | Error.Printf("Error while rebooting '%s': %s\n", i, ErrorToString(h.Status.Err())) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 509 | continue |
| 510 | } |
| 511 | fmt.Printf("%s\n", i) |
| 512 | } |
| 513 | |
| 514 | return nil |
| 515 | } |
| 516 | |
| 517 | func (options *DevicePortList) Execute(args []string) error { |
| 518 | |
| 519 | conn, err := NewConnection() |
| 520 | if err != nil { |
| 521 | return err |
| 522 | } |
| 523 | defer conn.Close() |
| 524 | |
| 525 | descriptor, method, err := GetMethod("device-ports") |
| 526 | if err != nil { |
| 527 | return err |
| 528 | } |
| 529 | |
| 530 | ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout) |
| 531 | defer cancel() |
| 532 | |
| 533 | h := &RpcEventHandler{ |
| 534 | Fields: map[string]map[string]interface{}{ParamNames[GlobalConfig.ApiVersion]["ID"]: {"id": options.Args.Id}}, |
| 535 | } |
| 536 | err = grpcurl.InvokeRPC(ctx, descriptor, conn, method, []string{}, h, h.GetParams) |
| 537 | if err != nil { |
| 538 | return err |
| 539 | } |
| 540 | |
| 541 | if h.Status != nil && h.Status.Err() != nil { |
| 542 | return h.Status.Err() |
| 543 | } |
| 544 | |
| 545 | d, err := dynamic.AsDynamicMessage(h.Response) |
| 546 | if err != nil { |
| 547 | return err |
| 548 | } |
| 549 | |
| 550 | items, err := d.TryGetFieldByName("items") |
| 551 | if err != nil { |
| 552 | return err |
| 553 | } |
| 554 | |
| 555 | outputFormat := CharReplacer.Replace(options.Format) |
| 556 | if outputFormat == "" { |
David Bainbridge | a672234 | 2019-10-24 23:55:53 +0000 | [diff] [blame] | 557 | outputFormat = GetCommandOptionWithDefault("device-ports", "format", DEFAULT_DEVICE_PORTS_FORMAT) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 558 | } |
| 559 | if options.Quiet { |
| 560 | outputFormat = "{{.Id}}" |
| 561 | } |
| 562 | |
David Bainbridge | a672234 | 2019-10-24 23:55:53 +0000 | [diff] [blame] | 563 | orderBy := options.OrderBy |
| 564 | if orderBy == "" { |
| 565 | orderBy = GetCommandOptionWithDefault("device-ports", "order", "") |
| 566 | } |
| 567 | |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 568 | data := make([]model.DevicePort, len(items.([]interface{}))) |
| 569 | for i, item := range items.([]interface{}) { |
| 570 | data[i].PopulateFrom(item.(*dynamic.Message)) |
| 571 | } |
| 572 | |
| 573 | result := CommandResult{ |
| 574 | Format: format.Format(outputFormat), |
| 575 | Filter: options.Filter, |
David Bainbridge | a672234 | 2019-10-24 23:55:53 +0000 | [diff] [blame] | 576 | OrderBy: orderBy, |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 577 | OutputAs: toOutputType(options.OutputAs), |
| 578 | NameLimit: options.NameLimit, |
| 579 | Data: data, |
| 580 | } |
| 581 | |
| 582 | GenerateOutput(&result) |
| 583 | return nil |
| 584 | } |
| 585 | |
| 586 | func (options *DeviceFlowList) Execute(args []string) error { |
| 587 | fl := &FlowList{} |
| 588 | fl.ListOutputOptions = options.ListOutputOptions |
| 589 | fl.Args.Id = string(options.Args.Id) |
David Bainbridge | a672234 | 2019-10-24 23:55:53 +0000 | [diff] [blame] | 590 | fl.Method = "device-flows" |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 591 | return fl.Execute(args) |
| 592 | } |
| 593 | |
| 594 | func (options *DeviceInspect) Execute(args []string) error { |
| 595 | if len(args) > 0 { |
| 596 | return fmt.Errorf("only a single argument 'DEVICE_ID' can be provided") |
| 597 | } |
| 598 | |
| 599 | conn, err := NewConnection() |
| 600 | if err != nil { |
| 601 | return err |
| 602 | } |
| 603 | defer conn.Close() |
| 604 | |
| 605 | descriptor, method, err := GetMethod("device-inspect") |
| 606 | if err != nil { |
| 607 | return err |
| 608 | } |
| 609 | |
| 610 | ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout) |
| 611 | defer cancel() |
| 612 | |
| 613 | h := &RpcEventHandler{ |
| 614 | Fields: map[string]map[string]interface{}{ParamNames[GlobalConfig.ApiVersion]["ID"]: {"id": options.Args.Id}}, |
| 615 | } |
| 616 | err = grpcurl.InvokeRPC(ctx, descriptor, conn, method, []string{"Get-Depth: 2"}, h, h.GetParams) |
| 617 | if err != nil { |
| 618 | return err |
| 619 | } else if h.Status != nil && h.Status.Err() != nil { |
| 620 | return h.Status.Err() |
| 621 | } |
| 622 | |
| 623 | d, err := dynamic.AsDynamicMessage(h.Response) |
| 624 | if err != nil { |
| 625 | return err |
| 626 | } |
| 627 | |
| 628 | device := &model.Device{} |
| 629 | device.PopulateFrom(d) |
| 630 | |
| 631 | outputFormat := CharReplacer.Replace(options.Format) |
| 632 | if outputFormat == "" { |
David Bainbridge | a672234 | 2019-10-24 23:55:53 +0000 | [diff] [blame] | 633 | outputFormat = GetCommandOptionWithDefault("device-inspect", "format", DEFAULT_DEVICE_INSPECT_FORMAT) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 634 | } |
| 635 | if options.Quiet { |
| 636 | outputFormat = "{{.Id}}" |
| 637 | } |
| 638 | |
| 639 | result := CommandResult{ |
| 640 | Format: format.Format(outputFormat), |
| 641 | OutputAs: toOutputType(options.OutputAs), |
| 642 | NameLimit: options.NameLimit, |
| 643 | Data: device, |
| 644 | } |
| 645 | GenerateOutput(&result) |
| 646 | return nil |
| 647 | } |
kesavand | 12cd8eb | 2020-01-20 22:25:22 -0500 | [diff] [blame] | 648 | |
| 649 | /*Device Port Enable */ |
| 650 | func (options *DevicePortEnable) Execute(args []string) error { |
| 651 | conn, err := NewConnection() |
| 652 | if err != nil { |
| 653 | return err |
| 654 | } |
| 655 | defer conn.Close() |
| 656 | |
| 657 | descriptor, method, err := GetMethod("device-port-enable") |
| 658 | if err != nil { |
| 659 | return err |
| 660 | } |
| 661 | |
| 662 | h := &RpcEventHandler{ |
| 663 | Fields: map[string]map[string]interface{}{ParamNames[GlobalConfig.ApiVersion]["port"]: {"device_id": options.Args.Id, "port_no": options.Args.PortId}}, |
| 664 | } |
| 665 | ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout) |
| 666 | defer cancel() |
| 667 | |
| 668 | err = grpcurl.InvokeRPC(ctx, descriptor, conn, method, []string{}, h, h.GetParams) |
| 669 | if err != nil { |
| 670 | Error.Printf("Error enabling port number %v on device Id %s,err=%s\n", options.Args.PortId, options.Args.Id, ErrorToString(err)) |
| 671 | return err |
| 672 | } else if h.Status != nil && h.Status.Err() != nil { |
| 673 | Error.Printf("Error enabling port number %v on device Id %s,err=%s\n", options.Args.PortId, options.Args.Id, ErrorToString(h.Status.Err())) |
| 674 | return h.Status.Err() |
| 675 | } |
| 676 | |
| 677 | return nil |
| 678 | } |
| 679 | |
| 680 | /*Device Port Disable */ |
| 681 | func (options *DevicePortDisable) Execute(args []string) error { |
| 682 | conn, err := NewConnection() |
| 683 | if err != nil { |
| 684 | return err |
| 685 | } |
| 686 | defer conn.Close() |
| 687 | |
| 688 | descriptor, method, err := GetMethod("device-port-disable") |
| 689 | if err != nil { |
| 690 | return err |
| 691 | } |
| 692 | h := &RpcEventHandler{ |
| 693 | Fields: map[string]map[string]interface{}{ParamNames[GlobalConfig.ApiVersion]["port"]: {"device_id": options.Args.Id, "port_no": options.Args.PortId}}, |
| 694 | } |
| 695 | ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout) |
| 696 | defer cancel() |
| 697 | |
| 698 | err = grpcurl.InvokeRPC(ctx, descriptor, conn, method, []string{}, h, h.GetParams) |
| 699 | if err != nil { |
| 700 | Error.Printf("Error disabling port number %v on device Id %s,err=%s\n", options.Args.PortId, options.Args.Id, ErrorToString(err)) |
| 701 | return err |
| 702 | } else if h.Status != nil && h.Status.Err() != nil { |
| 703 | Error.Printf("Error disabling port number %v on device Id %s,err=%s\n", options.Args.PortId, options.Args.Id, ErrorToString(h.Status.Err())) |
| 704 | return h.Status.Err() |
| 705 | } |
| 706 | return nil |
| 707 | } |