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" |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 20 | "github.com/fullstorydev/grpcurl" |
| 21 | flags "github.com/jessevdk/go-flags" |
| 22 | "github.com/jhump/protoreflect/dynamic" |
Scott Baker | 2b0ad65 | 2019-08-21 14:57:07 -0700 | [diff] [blame] | 23 | "github.com/opencord/voltctl/pkg/format" |
| 24 | "github.com/opencord/voltctl/pkg/model" |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 25 | ) |
| 26 | |
| 27 | const ( |
Kent Hagerman | e00f4f6 | 2020-01-22 13:53:10 -0500 | [diff] [blame] | 28 | DEFAULT_OUTPUT_FORMAT = "table{{ .Id }}\t{{ .Vendor }}\t{{ .Version }}\t{{ .SinceLastCommunication }}" |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 29 | ) |
| 30 | |
| 31 | type AdapterList struct { |
| 32 | ListOutputOptions |
| 33 | } |
| 34 | |
| 35 | type AdapterOpts struct { |
| 36 | List AdapterList `command:"list"` |
| 37 | } |
| 38 | |
| 39 | var adapterOpts = AdapterOpts{} |
| 40 | |
| 41 | func RegisterAdapterCommands(parent *flags.Parser) { |
David Bainbridge | 12f036f | 2019-10-15 22:09:04 +0000 | [diff] [blame] | 42 | 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] | 43 | Error.Fatalf("Unexpected error while attempting to register adapter 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 *AdapterList) Execute(args []string) error { |
| 48 | conn, err := NewConnection() |
| 49 | if err != nil { |
| 50 | return err |
| 51 | } |
| 52 | defer conn.Close() |
| 53 | |
| 54 | descriptor, method, err := GetMethod("adapter-list") |
| 55 | if err != nil { |
| 56 | return err |
| 57 | } |
| 58 | |
| 59 | ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout) |
| 60 | defer cancel() |
| 61 | |
| 62 | h := &RpcEventHandler{} |
| 63 | err = grpcurl.InvokeRPC(ctx, descriptor, conn, method, []string{}, h, h.GetParams) |
| 64 | if err != nil { |
| 65 | return err |
| 66 | } |
| 67 | |
| 68 | if h.Status != nil && h.Status.Err() != nil { |
| 69 | return h.Status.Err() |
| 70 | } |
| 71 | |
| 72 | d, err := dynamic.AsDynamicMessage(h.Response) |
| 73 | if err != nil { |
| 74 | return err |
| 75 | } |
| 76 | items, err := d.TryGetFieldByName("items") |
| 77 | if err != nil { |
| 78 | return err |
| 79 | } |
| 80 | |
| 81 | outputFormat := CharReplacer.Replace(options.Format) |
| 82 | if outputFormat == "" { |
David Bainbridge | a672234 | 2019-10-24 23:55:53 +0000 | [diff] [blame] | 83 | outputFormat = GetCommandOptionWithDefault("adapter-list", "format", DEFAULT_OUTPUT_FORMAT) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 84 | } |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 85 | if options.Quiet { |
| 86 | outputFormat = "{{.Id}}" |
| 87 | } |
David Bainbridge | a672234 | 2019-10-24 23:55:53 +0000 | [diff] [blame] | 88 | orderBy := options.OrderBy |
| 89 | if orderBy == "" { |
| 90 | orderBy = GetCommandOptionWithDefault("adapter-list", "order", "") |
| 91 | } |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 92 | |
| 93 | data := make([]model.Adapter, len(items.([]interface{}))) |
| 94 | for i, item := range items.([]interface{}) { |
| 95 | data[i].PopulateFrom(item.(*dynamic.Message)) |
| 96 | } |
| 97 | |
| 98 | result := CommandResult{ |
| 99 | Format: format.Format(outputFormat), |
| 100 | Filter: options.Filter, |
David Bainbridge | a672234 | 2019-10-24 23:55:53 +0000 | [diff] [blame] | 101 | OrderBy: orderBy, |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 102 | OutputAs: toOutputType(options.OutputAs), |
| 103 | NameLimit: options.NameLimit, |
| 104 | Data: data, |
| 105 | } |
| 106 | GenerateOutput(&result) |
| 107 | |
| 108 | return nil |
| 109 | } |