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