blob: 05b681b92873d5c239652071262c9a999e5012e2 [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"
Zack Williamse940c7a2019-08-21 14:25:39 -070021 flags "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 (
27 DEFAULT_DEVICE_GROUP_FORMAT = "table{{ .Id }}\t{{.LogicalDevices}}\t{{.Devices}}"
28)
29
30type DeviceGroupList struct {
31 ListOutputOptions
32}
33
34type DeviceGroupOpts struct {
35 List DeviceGroupList `command:"list"`
36}
37
38var deviceGroupOpts = DeviceGroupOpts{}
39
40func RegisterDeviceGroupCommands(parser *flags.Parser) {
David Bainbridge12f036f2019-10-15 22:09:04 +000041 if _, err := parser.AddCommand("devicegroup", "device group commands", "Commands to query and manipulate VOLTHA device groups",
42 &deviceGroupOpts); err != nil {
David Bainbridgea6722342019-10-24 23:55:53 +000043 Error.Fatalf("Unexpected error while attempting to register device group commands : %s", err)
David Bainbridge12f036f2019-10-15 22:09:04 +000044 }
Zack Williamse940c7a2019-08-21 14:25:39 -070045}
46
47func (options *DeviceGroupList) Execute(args []string) error {
48
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
57 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
58 defer cancel()
59
Scott Baker9173ed82020-05-19 08:30:12 -070060 deviceGroups, err := client.ListDeviceGroups(ctx, &empty.Empty{})
Zack Williamse940c7a2019-08-21 14:25:39 -070061 if err != nil {
62 return err
63 }
64
65 outputFormat := CharReplacer.Replace(options.Format)
66 if outputFormat == "" {
David Bainbridgea6722342019-10-24 23:55:53 +000067 outputFormat = GetCommandOptionWithDefault("device-group-list", "format", DEFAULT_DEVICE_GROUP_FORMAT)
Zack Williamse940c7a2019-08-21 14:25:39 -070068 }
69 if options.Quiet {
70 outputFormat = "{{.Id}}"
71 }
David Bainbridgea6722342019-10-24 23:55:53 +000072 orderBy := options.OrderBy
73 if orderBy == "" {
74 orderBy = GetCommandOptionWithDefault("device-group-list", "order", "")
75 }
Zack Williamse940c7a2019-08-21 14:25:39 -070076
Zack Williamse940c7a2019-08-21 14:25:39 -070077 result := CommandResult{
78 Format: format.Format(outputFormat),
79 Filter: options.Filter,
David Bainbridgea6722342019-10-24 23:55:53 +000080 OrderBy: orderBy,
Zack Williamse940c7a2019-08-21 14:25:39 -070081 OutputAs: toOutputType(options.OutputAs),
82 NameLimit: options.NameLimit,
Scott Baker9173ed82020-05-19 08:30:12 -070083 Data: deviceGroups.Items,
Zack Williamse940c7a2019-08-21 14:25:39 -070084 }
85
86 GenerateOutput(&result)
87 return nil
88}