blob: 1a901bc747bd60807acac129f38d7faf1336cbba [file] [log] [blame]
Zack Williamse940c7a2019-08-21 14:25:39 -07001/*
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 */
16package commands
17
18import (
19 "context"
Zack Williamse940c7a2019-08-21 14:25:39 -070020 "github.com/fullstorydev/grpcurl"
21 flags "github.com/jessevdk/go-flags"
22 "github.com/jhump/protoreflect/dynamic"
Scott Baker2b0ad652019-08-21 14:57:07 -070023 "github.com/opencord/voltctl/pkg/format"
24 "github.com/opencord/voltctl/pkg/model"
Zack Williamse940c7a2019-08-21 14:25:39 -070025)
26
27const (
Kent Hagermane00f4f62020-01-22 13:53:10 -050028 DEFAULT_OUTPUT_FORMAT = "table{{ .Id }}\t{{ .Vendor }}\t{{ .Version }}\t{{ .SinceLastCommunication }}"
Zack Williamse940c7a2019-08-21 14:25:39 -070029)
30
31type AdapterList struct {
32 ListOutputOptions
33}
34
35type AdapterOpts struct {
36 List AdapterList `command:"list"`
37}
38
39var adapterOpts = AdapterOpts{}
40
41func RegisterAdapterCommands(parent *flags.Parser) {
David Bainbridge12f036f2019-10-15 22:09:04 +000042 if _, err := parent.AddCommand("adapter", "adapter commands", "Commands to query and manipulate VOLTHA adapters", &adapterOpts); err != nil {
David Bainbridgea6722342019-10-24 23:55:53 +000043 Error.Fatalf("Unexpected error while attempting to register adapter commands : %s", err)
David Bainbridge12f036f2019-10-15 22:09:04 +000044 }
Zack Williamse940c7a2019-08-21 14:25:39 -070045}
46
47func (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 Bainbridgea6722342019-10-24 23:55:53 +000083 outputFormat = GetCommandOptionWithDefault("adapter-list", "format", DEFAULT_OUTPUT_FORMAT)
Zack Williamse940c7a2019-08-21 14:25:39 -070084 }
Zack Williamse940c7a2019-08-21 14:25:39 -070085 if options.Quiet {
86 outputFormat = "{{.Id}}"
87 }
David Bainbridgea6722342019-10-24 23:55:53 +000088 orderBy := options.OrderBy
89 if orderBy == "" {
90 orderBy = GetCommandOptionWithDefault("adapter-list", "order", "")
91 }
Zack Williamse940c7a2019-08-21 14:25:39 -070092
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 Bainbridgea6722342019-10-24 23:55:53 +0000101 OrderBy: orderBy,
Zack Williamse940c7a2019-08-21 14:25:39 -0700102 OutputAs: toOutputType(options.OutputAs),
103 NameLimit: options.NameLimit,
104 Data: data,
105 }
106 GenerateOutput(&result)
107
108 return nil
109}