blob: 067b64c1ad0d6306b66e4592a9009fc480056ac1 [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"
David Bainbridge12f036f2019-10-15 22:09:04 +000025 "log"
Zack Williamse940c7a2019-08-21 14:25:39 -070026)
27
28const (
29 DEFAULT_OUTPUT_FORMAT = "table{{ .Id }}\t{{.Vendor}}\t{{.Version}}"
30)
31
32type AdapterList struct {
33 ListOutputOptions
34}
35
36type AdapterOpts struct {
37 List AdapterList `command:"list"`
38}
39
40var adapterOpts = AdapterOpts{}
41
42func RegisterAdapterCommands(parent *flags.Parser) {
David Bainbridge12f036f2019-10-15 22:09:04 +000043 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 Williamse940c7a2019-08-21 14:25:39 -070046}
47
48func (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}