blob: b4183c04296840b851c4ad3a2d91a350bad6bb7c [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"
Scott Baker9173ed82020-05-19 08:30:12 -070020 "github.com/golang/protobuf/ptypes/empty"
Matteo Scandolo0a413a82020-04-15 13:51:14 -070021 "github.com/jessevdk/go-flags"
Scott Baker2b0ad652019-08-21 14:57:07 -070022 "github.com/opencord/voltctl/pkg/format"
kesavand8ec4fc02021-01-27 09:10:22 -050023 "github.com/opencord/voltha-protos/v4/go/voltha"
Zack Williamse940c7a2019-08-21 14:25:39 -070024)
25
26const (
Scott Baker9173ed82020-05-19 08:30:12 -070027 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 -070028)
29
30type AdapterList struct {
31 ListOutputOptions
32}
33
34type AdapterOpts struct {
35 List AdapterList `command:"list"`
36}
37
38var adapterOpts = AdapterOpts{}
39
40func RegisterAdapterCommands(parent *flags.Parser) {
David Bainbridge12f036f2019-10-15 22:09:04 +000041 if _, err := parent.AddCommand("adapter", "adapter commands", "Commands to query and manipulate VOLTHA adapters", &adapterOpts); err != nil {
David Bainbridgea6722342019-10-24 23:55:53 +000042 Error.Fatalf("Unexpected error while attempting to register adapter commands : %s", err)
David Bainbridge12f036f2019-10-15 22:09:04 +000043 }
Zack Williamse940c7a2019-08-21 14:25:39 -070044}
45
46func (options *AdapterList) Execute(args []string) error {
47 conn, err := NewConnection()
48 if err != nil {
49 return err
50 }
51 defer conn.Close()
52
Scott Baker9173ed82020-05-19 08:30:12 -070053 client := voltha.NewVolthaServiceClient(conn)
Zack Williamse940c7a2019-08-21 14:25:39 -070054
55 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
56 defer cancel()
57
Scott Baker9173ed82020-05-19 08:30:12 -070058 adapters, err := client.ListAdapters(ctx, &empty.Empty{})
Zack Williamse940c7a2019-08-21 14:25:39 -070059 if err != nil {
60 return err
61 }
62
63 outputFormat := CharReplacer.Replace(options.Format)
64 if outputFormat == "" {
David Bainbridgea6722342019-10-24 23:55:53 +000065 outputFormat = GetCommandOptionWithDefault("adapter-list", "format", DEFAULT_OUTPUT_FORMAT)
Zack Williamse940c7a2019-08-21 14:25:39 -070066 }
Zack Williamse940c7a2019-08-21 14:25:39 -070067 if options.Quiet {
68 outputFormat = "{{.Id}}"
69 }
David Bainbridgea6722342019-10-24 23:55:53 +000070 orderBy := options.OrderBy
71 if orderBy == "" {
72 orderBy = GetCommandOptionWithDefault("adapter-list", "order", "")
73 }
Zack Williamse940c7a2019-08-21 14:25:39 -070074
Scott Baker9173ed82020-05-19 08:30:12 -070075 // TODO: lastCommunication ends up formatted as `seconds:1589415656 nanos:775740000`
76 // need to think through where to do presentation formatting.
Zack Williamse940c7a2019-08-21 14:25:39 -070077
78 result := CommandResult{
79 Format: format.Format(outputFormat),
80 Filter: options.Filter,
David Bainbridgea6722342019-10-24 23:55:53 +000081 OrderBy: orderBy,
Zack Williamse940c7a2019-08-21 14:25:39 -070082 OutputAs: toOutputType(options.OutputAs),
83 NameLimit: options.NameLimit,
Scott Baker9173ed82020-05-19 08:30:12 -070084 Data: adapters.Items,
Zack Williamse940c7a2019-08-21 14:25:39 -070085 }
86 GenerateOutput(&result)
87
88 return nil
89}