blob: 4d206c7c03333e461b1386de1b70581149806303 [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"
Zack Williamse940c7a2019-08-21 14:25:39 -070020 "github.com/fullstorydev/grpcurl"
21 flags "github.com/jessevdk/go-flags"
22 "github.com/jhump/protoreflect/dynamic"
Scott Baker2b0ad652019-08-21 14:57:07 -070023 "github.com/opencord/voltctl/pkg/format"
24 "github.com/opencord/voltctl/pkg/model"
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
David Bainbridgea6722342019-10-24 23:55:53 +000056 descriptor, method, err := GetMethod("device-group-list")
Zack Williamse940c7a2019-08-21 14:25:39 -070057 if err != nil {
58 return err
59 }
60
61 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
62 defer cancel()
63
64 h := &RpcEventHandler{}
65 err = grpcurl.InvokeRPC(ctx, descriptor, conn, method, []string{}, h, h.GetParams)
66 if err != nil {
67 return err
68 }
69
70 if h.Status != nil && h.Status.Err() != nil {
71 return h.Status.Err()
72 }
73
74 d, err := dynamic.AsDynamicMessage(h.Response)
75 if err != nil {
76 return err
77 }
78
79 items, err := d.TryGetFieldByName("items")
80 if err != nil {
81 return err
82 }
83
84 outputFormat := CharReplacer.Replace(options.Format)
85 if outputFormat == "" {
David Bainbridgea6722342019-10-24 23:55:53 +000086 outputFormat = GetCommandOptionWithDefault("device-group-list", "format", DEFAULT_DEVICE_GROUP_FORMAT)
Zack Williamse940c7a2019-08-21 14:25:39 -070087 }
88 if options.Quiet {
89 outputFormat = "{{.Id}}"
90 }
David Bainbridgea6722342019-10-24 23:55:53 +000091 orderBy := options.OrderBy
92 if orderBy == "" {
93 orderBy = GetCommandOptionWithDefault("device-group-list", "order", "")
94 }
Zack Williamse940c7a2019-08-21 14:25:39 -070095
96 data := make([]model.DeviceGroup, len(items.([]interface{})))
97 for i, item := range items.([]interface{}) {
98 val := item.(*dynamic.Message)
99 data[i].PopulateFrom(val)
100 }
101
102 result := CommandResult{
103 Format: format.Format(outputFormat),
104 Filter: options.Filter,
David Bainbridgea6722342019-10-24 23:55:53 +0000105 OrderBy: orderBy,
Zack Williamse940c7a2019-08-21 14:25:39 -0700106 OutputAs: toOutputType(options.OutputAs),
107 NameLimit: options.NameLimit,
108 Data: data,
109 }
110
111 GenerateOutput(&result)
112 return nil
113}