blob: 73b91e1ed4870079911d9aabaf74bf83b7f25a05 [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"
David K. Bainbridge9189c632021-03-26 21:52:21 +000020
Scott Baker9173ed82020-05-19 08:30:12 -070021 "github.com/golang/protobuf/ptypes/empty"
David K. Bainbridge9189c632021-03-26 21:52:21 +000022 flags "github.com/jessevdk/go-flags"
Scott Baker2b0ad652019-08-21 14:57:07 -070023 "github.com/opencord/voltctl/pkg/format"
kesavand8ec4fc02021-01-27 09:10:22 -050024 "github.com/opencord/voltha-protos/v4/go/voltha"
Zack Williamse940c7a2019-08-21 14:25:39 -070025)
26
27const (
Scott Baker9173ed82020-05-19 08:30:12 -070028 DEFAULT_OUTPUT_FORMAT = "table{{ .Id }}\t{{ .Vendor }}\t{{ .Type }}\t{{ .Endpoint }}\t{{ .Version }}\t{{ .CurrentReplica }}\t{{ .TotalReplicas }}\t{{ since .LastCommunication}}"
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
Scott Baker9173ed82020-05-19 08:30:12 -070054 client := voltha.NewVolthaServiceClient(conn)
Zack Williamse940c7a2019-08-21 14:25:39 -070055
David K. Bainbridge9189c632021-03-26 21:52:21 +000056 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Zack Williamse940c7a2019-08-21 14:25:39 -070057 defer cancel()
58
Scott Baker9173ed82020-05-19 08:30:12 -070059 adapters, err := client.ListAdapters(ctx, &empty.Empty{})
Zack Williamse940c7a2019-08-21 14:25:39 -070060 if err != nil {
61 return err
62 }
63
64 outputFormat := CharReplacer.Replace(options.Format)
65 if outputFormat == "" {
David Bainbridgea6722342019-10-24 23:55:53 +000066 outputFormat = GetCommandOptionWithDefault("adapter-list", "format", DEFAULT_OUTPUT_FORMAT)
Zack Williamse940c7a2019-08-21 14:25:39 -070067 }
Zack Williamse940c7a2019-08-21 14:25:39 -070068 if options.Quiet {
69 outputFormat = "{{.Id}}"
70 }
David Bainbridgea6722342019-10-24 23:55:53 +000071 orderBy := options.OrderBy
72 if orderBy == "" {
73 orderBy = GetCommandOptionWithDefault("adapter-list", "order", "")
74 }
Zack Williamse940c7a2019-08-21 14:25:39 -070075
Scott Baker9173ed82020-05-19 08:30:12 -070076 // TODO: lastCommunication ends up formatted as `seconds:1589415656 nanos:775740000`
77 // need to think through where to do presentation formatting.
Zack Williamse940c7a2019-08-21 14:25:39 -070078
79 result := CommandResult{
80 Format: format.Format(outputFormat),
81 Filter: options.Filter,
David Bainbridgea6722342019-10-24 23:55:53 +000082 OrderBy: orderBy,
Zack Williamse940c7a2019-08-21 14:25:39 -070083 OutputAs: toOutputType(options.OutputAs),
84 NameLimit: options.NameLimit,
Scott Baker9173ed82020-05-19 08:30:12 -070085 Data: adapters.Items,
Zack Williamse940c7a2019-08-21 14:25:39 -070086 }
87 GenerateOutput(&result)
88
89 return nil
90}