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" |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 20 | "github.com/golang/protobuf/ptypes/empty" |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 21 | flags "github.com/jessevdk/go-flags" |
Scott Baker | 2b0ad65 | 2019-08-21 14:57:07 -0700 | [diff] [blame] | 22 | "github.com/opencord/voltctl/pkg/format" |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 23 | "github.com/opencord/voltha-protos/v3/go/voltha" |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 24 | ) |
| 25 | |
| 26 | const ( |
| 27 | DEFAULT_DEVICE_GROUP_FORMAT = "table{{ .Id }}\t{{.LogicalDevices}}\t{{.Devices}}" |
| 28 | ) |
| 29 | |
| 30 | type DeviceGroupList struct { |
| 31 | ListOutputOptions |
| 32 | } |
| 33 | |
| 34 | type DeviceGroupOpts struct { |
| 35 | List DeviceGroupList `command:"list"` |
| 36 | } |
| 37 | |
| 38 | var deviceGroupOpts = DeviceGroupOpts{} |
| 39 | |
| 40 | func RegisterDeviceGroupCommands(parser *flags.Parser) { |
David Bainbridge | 12f036f | 2019-10-15 22:09:04 +0000 | [diff] [blame] | 41 | if _, err := parser.AddCommand("devicegroup", "device group commands", "Commands to query and manipulate VOLTHA device groups", |
| 42 | &deviceGroupOpts); err != nil { |
David Bainbridge | a672234 | 2019-10-24 23:55:53 +0000 | [diff] [blame] | 43 | Error.Fatalf("Unexpected error while attempting to register device group commands : %s", err) |
David Bainbridge | 12f036f | 2019-10-15 22:09:04 +0000 | [diff] [blame] | 44 | } |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | func (options *DeviceGroupList) Execute(args []string) error { |
| 48 | |
| 49 | conn, err := NewConnection() |
| 50 | if err != nil { |
| 51 | return err |
| 52 | } |
| 53 | defer conn.Close() |
| 54 | |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 55 | client := voltha.NewVolthaServiceClient(conn) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 56 | |
| 57 | ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout) |
| 58 | defer cancel() |
| 59 | |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 60 | deviceGroups, err := client.ListDeviceGroups(ctx, &empty.Empty{}) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 61 | if err != nil { |
| 62 | return err |
| 63 | } |
| 64 | |
| 65 | outputFormat := CharReplacer.Replace(options.Format) |
| 66 | if outputFormat == "" { |
David Bainbridge | a672234 | 2019-10-24 23:55:53 +0000 | [diff] [blame] | 67 | outputFormat = GetCommandOptionWithDefault("device-group-list", "format", DEFAULT_DEVICE_GROUP_FORMAT) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 68 | } |
| 69 | if options.Quiet { |
| 70 | outputFormat = "{{.Id}}" |
| 71 | } |
David Bainbridge | a672234 | 2019-10-24 23:55:53 +0000 | [diff] [blame] | 72 | orderBy := options.OrderBy |
| 73 | if orderBy == "" { |
| 74 | orderBy = GetCommandOptionWithDefault("device-group-list", "order", "") |
| 75 | } |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 76 | |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 77 | result := CommandResult{ |
| 78 | Format: format.Format(outputFormat), |
| 79 | Filter: options.Filter, |
David Bainbridge | a672234 | 2019-10-24 23:55:53 +0000 | [diff] [blame] | 80 | OrderBy: orderBy, |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 81 | OutputAs: toOutputType(options.OutputAs), |
| 82 | NameLimit: options.NameLimit, |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 83 | Data: deviceGroups.Items, |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | GenerateOutput(&result) |
| 87 | return nil |
| 88 | } |