blob: b299a8e3a7c794a04ef2afa82963c38212f502df [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"
David K. Bainbridgebd6b2882021-08-26 13:31:02 +000024 "github.com/opencord/voltctl/pkg/model"
25 "github.com/opencord/voltha-protos/v5/go/voltha"
Zack Williamse940c7a2019-08-21 14:25:39 -070026)
27
28const (
David K. Bainbridgebd6b2882021-08-26 13:31:02 +000029 DEFAULT_OUTPUT_FORMAT = "table{{ .Id }}\t{{ .Vendor }}\t{{ .Type }}\t{{ .Endpoint }}\t{{ .Version }}\t{{ .CurrentReplica }}\t{{ .TotalReplicas }}\t{{ gosince .LastCommunication}}"
Zack Williamse940c7a2019-08-21 14:25:39 -070030)
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 {
David Bainbridgea6722342019-10-24 23:55:53 +000044 Error.Fatalf("Unexpected error while attempting to register adapter commands : %s", err)
David Bainbridge12f036f2019-10-15 22:09:04 +000045 }
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
Scott Baker9173ed82020-05-19 08:30:12 -070055 client := voltha.NewVolthaServiceClient(conn)
Zack Williamse940c7a2019-08-21 14:25:39 -070056
David K. Bainbridge9189c632021-03-26 21:52:21 +000057 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Zack Williamse940c7a2019-08-21 14:25:39 -070058 defer cancel()
59
Scott Baker9173ed82020-05-19 08:30:12 -070060 adapters, err := client.ListAdapters(ctx, &empty.Empty{})
Zack Williamse940c7a2019-08-21 14:25:39 -070061 if err != nil {
62 return err
63 }
David K. Bainbridgebd6b2882021-08-26 13:31:02 +000064 data := make([]model.AdapterInstance, len(adapters.Items))
65 for i, item := range adapters.Items {
66 data[i].PopulateFrom(item)
67 }
Zack Williamse940c7a2019-08-21 14:25:39 -070068
69 outputFormat := CharReplacer.Replace(options.Format)
70 if outputFormat == "" {
David Bainbridgea6722342019-10-24 23:55:53 +000071 outputFormat = GetCommandOptionWithDefault("adapter-list", "format", DEFAULT_OUTPUT_FORMAT)
Zack Williamse940c7a2019-08-21 14:25:39 -070072 }
Zack Williamse940c7a2019-08-21 14:25:39 -070073 if options.Quiet {
74 outputFormat = "{{.Id}}"
75 }
David Bainbridgea6722342019-10-24 23:55:53 +000076 orderBy := options.OrderBy
77 if orderBy == "" {
78 orderBy = GetCommandOptionWithDefault("adapter-list", "order", "")
79 }
Zack Williamse940c7a2019-08-21 14:25:39 -070080
Scott Baker9173ed82020-05-19 08:30:12 -070081 // TODO: lastCommunication ends up formatted as `seconds:1589415656 nanos:775740000`
82 // need to think through where to do presentation formatting.
Zack Williamse940c7a2019-08-21 14:25:39 -070083
84 result := CommandResult{
85 Format: format.Format(outputFormat),
86 Filter: options.Filter,
David Bainbridgea6722342019-10-24 23:55:53 +000087 OrderBy: orderBy,
Zack Williamse940c7a2019-08-21 14:25:39 -070088 OutputAs: toOutputType(options.OutputAs),
89 NameLimit: options.NameLimit,
David K. Bainbridgebd6b2882021-08-26 13:31:02 +000090 Data: data,
Zack Williamse940c7a2019-08-21 14:25:39 -070091 }
92 GenerateOutput(&result)
93
94 return nil
95}