blob: dcd992003d14c12a0f6ab1690b5587c68242c6ed [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"
Matteo Scandolo0a413a82020-04-15 13:51:14 -070021 "github.com/jessevdk/go-flags"
Zack Williamse940c7a2019-08-21 14:25:39 -070022 "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 (
Matteo Scandolo0a413a82020-04-15 13:51:14 -070028 DEFAULT_OUTPUT_FORMAT = "table{{ .Id }}\t{{ .Vendor }}\t{{ .Type }}\t{{ .Endpoint }}\t{{ .Version }}\t{{ .CurrentReplica }}\t{{ .TotalReplicas }}\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 }
Matteo Scandolo0a413a82020-04-15 13:51:14 -070076
Zack Williamse940c7a2019-08-21 14:25:39 -070077 items, err := d.TryGetFieldByName("items")
78 if err != nil {
79 return err
80 }
81
82 outputFormat := CharReplacer.Replace(options.Format)
83 if outputFormat == "" {
David Bainbridgea6722342019-10-24 23:55:53 +000084 outputFormat = GetCommandOptionWithDefault("adapter-list", "format", DEFAULT_OUTPUT_FORMAT)
Zack Williamse940c7a2019-08-21 14:25:39 -070085 }
Zack Williamse940c7a2019-08-21 14:25:39 -070086 if options.Quiet {
87 outputFormat = "{{.Id}}"
88 }
David Bainbridgea6722342019-10-24 23:55:53 +000089 orderBy := options.OrderBy
90 if orderBy == "" {
91 orderBy = GetCommandOptionWithDefault("adapter-list", "order", "")
92 }
Zack Williamse940c7a2019-08-21 14:25:39 -070093
94 data := make([]model.Adapter, len(items.([]interface{})))
95 for i, item := range items.([]interface{}) {
96 data[i].PopulateFrom(item.(*dynamic.Message))
97 }
98
99 result := CommandResult{
100 Format: format.Format(outputFormat),
101 Filter: options.Filter,
David Bainbridgea6722342019-10-24 23:55:53 +0000102 OrderBy: orderBy,
Zack Williamse940c7a2019-08-21 14:25:39 -0700103 OutputAs: toOutputType(options.OutputAs),
104 NameLimit: options.NameLimit,
105 Data: data,
106 }
107 GenerateOutput(&result)
108
109 return nil
110}