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" |
Matteo Scandolo | 0a413a8 | 2020-04-15 13:51:14 -0700 | [diff] [blame] | 21 | "github.com/jessevdk/go-flags" |
Scott Baker | 2b0ad65 | 2019-08-21 14:57:07 -0700 | [diff] [blame] | 22 | "github.com/opencord/voltctl/pkg/format" |
kesavand | 8ec4fc0 | 2021-01-27 09:10:22 -0500 | [diff] [blame] | 23 | "github.com/opencord/voltha-protos/v4/go/voltha" |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 24 | ) |
| 25 | |
| 26 | const ( |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 27 | DEFAULT_OUTPUT_FORMAT = "table{{ .Id }}\t{{ .Vendor }}\t{{ .Type }}\t{{ .Endpoint }}\t{{ .Version }}\t{{ .CurrentReplica }}\t{{ .TotalReplicas }}\t{{ since .LastCommunication}}" |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 28 | ) |
| 29 | |
| 30 | type AdapterList struct { |
| 31 | ListOutputOptions |
| 32 | } |
| 33 | |
| 34 | type AdapterOpts struct { |
| 35 | List AdapterList `command:"list"` |
| 36 | } |
| 37 | |
| 38 | var adapterOpts = AdapterOpts{} |
| 39 | |
| 40 | func RegisterAdapterCommands(parent *flags.Parser) { |
David Bainbridge | 12f036f | 2019-10-15 22:09:04 +0000 | [diff] [blame] | 41 | if _, err := parent.AddCommand("adapter", "adapter commands", "Commands to query and manipulate VOLTHA adapters", &adapterOpts); err != nil { |
David Bainbridge | a672234 | 2019-10-24 23:55:53 +0000 | [diff] [blame] | 42 | Error.Fatalf("Unexpected error while attempting to register adapter commands : %s", err) |
David Bainbridge | 12f036f | 2019-10-15 22:09:04 +0000 | [diff] [blame] | 43 | } |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | func (options *AdapterList) Execute(args []string) error { |
| 47 | conn, err := NewConnection() |
| 48 | if err != nil { |
| 49 | return err |
| 50 | } |
| 51 | defer conn.Close() |
| 52 | |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 53 | client := voltha.NewVolthaServiceClient(conn) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 54 | |
| 55 | ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout) |
| 56 | defer cancel() |
| 57 | |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 58 | adapters, err := client.ListAdapters(ctx, &empty.Empty{}) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 59 | if err != nil { |
| 60 | return err |
| 61 | } |
| 62 | |
| 63 | outputFormat := CharReplacer.Replace(options.Format) |
| 64 | if outputFormat == "" { |
David Bainbridge | a672234 | 2019-10-24 23:55:53 +0000 | [diff] [blame] | 65 | outputFormat = GetCommandOptionWithDefault("adapter-list", "format", DEFAULT_OUTPUT_FORMAT) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 66 | } |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 67 | if options.Quiet { |
| 68 | outputFormat = "{{.Id}}" |
| 69 | } |
David Bainbridge | a672234 | 2019-10-24 23:55:53 +0000 | [diff] [blame] | 70 | orderBy := options.OrderBy |
| 71 | if orderBy == "" { |
| 72 | orderBy = GetCommandOptionWithDefault("adapter-list", "order", "") |
| 73 | } |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 74 | |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 75 | // TODO: lastCommunication ends up formatted as `seconds:1589415656 nanos:775740000` |
| 76 | // need to think through where to do presentation formatting. |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 77 | |
| 78 | result := CommandResult{ |
| 79 | Format: format.Format(outputFormat), |
| 80 | Filter: options.Filter, |
David Bainbridge | a672234 | 2019-10-24 23:55:53 +0000 | [diff] [blame] | 81 | OrderBy: orderBy, |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 82 | OutputAs: toOutputType(options.OutputAs), |
| 83 | NameLimit: options.NameLimit, |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 84 | Data: adapters.Items, |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 85 | } |
| 86 | GenerateOutput(&result) |
| 87 | |
| 88 | return nil |
| 89 | } |