blob: f42547b6d8b4c5161b7bb609387892bd0b0c65e3 [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"
Zack Williamse940c7a2019-08-21 14:25:39 -070022 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 (
28 DEFAULT_DEVICE_GROUP_FORMAT = "table{{ .Id }}\t{{.LogicalDevices}}\t{{.Devices}}"
29)
30
31type DeviceGroupList struct {
32 ListOutputOptions
33}
34
35type DeviceGroupOpts struct {
36 List DeviceGroupList `command:"list"`
37}
38
39var deviceGroupOpts = DeviceGroupOpts{}
40
41func RegisterDeviceGroupCommands(parser *flags.Parser) {
David Bainbridge12f036f2019-10-15 22:09:04 +000042 if _, err := parser.AddCommand("devicegroup", "device group commands", "Commands to query and manipulate VOLTHA device groups",
43 &deviceGroupOpts); err != nil {
David Bainbridgea6722342019-10-24 23:55:53 +000044 Error.Fatalf("Unexpected error while attempting to register device group commands : %s", err)
David Bainbridge12f036f2019-10-15 22:09:04 +000045 }
Zack Williamse940c7a2019-08-21 14:25:39 -070046}
47
48func (options *DeviceGroupList) Execute(args []string) error {
49
50 conn, err := NewConnection()
51 if err != nil {
52 return err
53 }
54 defer conn.Close()
55
Scott Baker9173ed82020-05-19 08:30:12 -070056 client := voltha.NewVolthaServiceClient(conn)
Zack Williamse940c7a2019-08-21 14:25:39 -070057
David K. Bainbridge9189c632021-03-26 21:52:21 +000058 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Zack Williamse940c7a2019-08-21 14:25:39 -070059 defer cancel()
60
Scott Baker9173ed82020-05-19 08:30:12 -070061 deviceGroups, err := client.ListDeviceGroups(ctx, &empty.Empty{})
Zack Williamse940c7a2019-08-21 14:25:39 -070062 if err != nil {
63 return err
64 }
65
66 outputFormat := CharReplacer.Replace(options.Format)
67 if outputFormat == "" {
David Bainbridgea6722342019-10-24 23:55:53 +000068 outputFormat = GetCommandOptionWithDefault("device-group-list", "format", DEFAULT_DEVICE_GROUP_FORMAT)
Zack Williamse940c7a2019-08-21 14:25:39 -070069 }
70 if options.Quiet {
71 outputFormat = "{{.Id}}"
72 }
David Bainbridgea6722342019-10-24 23:55:53 +000073 orderBy := options.OrderBy
74 if orderBy == "" {
75 orderBy = GetCommandOptionWithDefault("device-group-list", "order", "")
76 }
Zack Williamse940c7a2019-08-21 14:25:39 -070077
Zack Williamse940c7a2019-08-21 14:25:39 -070078 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: deviceGroups.Items,
Zack Williamse940c7a2019-08-21 14:25:39 -070085 }
86
87 GenerateOutput(&result)
88 return nil
89}