blob: e8485659bf207ae223978edcd76040219b24454c [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 (
28 DEFAULT_OUTPUT_FORMAT = "table{{ .Id }}\t{{.Vendor}}\t{{.Version}}"
29)
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) {
42 parent.AddCommand("adapter", "adapter commands", "Commands to query and manipulate VOLTHA adapters", &adapterOpts)
43}
44
45func (options *AdapterList) Execute(args []string) error {
46 conn, err := NewConnection()
47 if err != nil {
48 return err
49 }
50 defer conn.Close()
51
52 descriptor, method, err := GetMethod("adapter-list")
53 if err != nil {
54 return err
55 }
56
57 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
58 defer cancel()
59
60 h := &RpcEventHandler{}
61 err = grpcurl.InvokeRPC(ctx, descriptor, conn, method, []string{}, h, h.GetParams)
62 if err != nil {
63 return err
64 }
65
66 if h.Status != nil && h.Status.Err() != nil {
67 return h.Status.Err()
68 }
69
70 d, err := dynamic.AsDynamicMessage(h.Response)
71 if err != nil {
72 return err
73 }
74 items, err := d.TryGetFieldByName("items")
75 if err != nil {
76 return err
77 }
78
79 outputFormat := CharReplacer.Replace(options.Format)
80 if outputFormat == "" {
81 outputFormat = DEFAULT_OUTPUT_FORMAT
82 }
83
84 if options.Quiet {
85 outputFormat = "{{.Id}}"
86 }
87
88 data := make([]model.Adapter, len(items.([]interface{})))
89 for i, item := range items.([]interface{}) {
90 data[i].PopulateFrom(item.(*dynamic.Message))
91 }
92
93 result := CommandResult{
94 Format: format.Format(outputFormat),
95 Filter: options.Filter,
96 OrderBy: options.OrderBy,
97 OutputAs: toOutputType(options.OutputAs),
98 NameLimit: options.NameLimit,
99 Data: data,
100 }
101 GenerateOutput(&result)
102
103 return nil
104}