blob: c499afc4753ab1e6d541cd20f853b6ebc7b1c22d [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) {
42 parser.AddCommand("devicegroup", "device group commands", "Commands to query and manipulate VOLTHA device groups",
43 &deviceGroupOpts)
44}
45
46func (options *DeviceGroupList) Execute(args []string) error {
47
48 conn, err := NewConnection()
49 if err != nil {
50 return err
51 }
52 defer conn.Close()
53
54 descriptor, method, err := GetMethod("devicegroup-list")
55 if err != nil {
56 return err
57 }
58
59 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
60 defer cancel()
61
62 h := &RpcEventHandler{}
63 err = grpcurl.InvokeRPC(ctx, descriptor, conn, method, []string{}, h, h.GetParams)
64 if err != nil {
65 return err
66 }
67
68 if h.Status != nil && h.Status.Err() != nil {
69 return h.Status.Err()
70 }
71
72 d, err := dynamic.AsDynamicMessage(h.Response)
73 if err != nil {
74 return err
75 }
76
77 items, err := d.TryGetFieldByName("items")
78 if err != nil {
79 return err
80 }
81
82 outputFormat := CharReplacer.Replace(options.Format)
83 if outputFormat == "" {
84 outputFormat = DEFAULT_DEVICE_GROUP_FORMAT
85 }
86 if options.Quiet {
87 outputFormat = "{{.Id}}"
88 }
89
90 data := make([]model.DeviceGroup, len(items.([]interface{})))
91 for i, item := range items.([]interface{}) {
92 val := item.(*dynamic.Message)
93 data[i].PopulateFrom(val)
94 }
95
96 result := CommandResult{
97 Format: format.Format(outputFormat),
98 Filter: options.Filter,
99 OrderBy: options.OrderBy,
100 OutputAs: toOutputType(options.OutputAs),
101 NameLimit: options.NameLimit,
102 Data: data,
103 }
104
105 GenerateOutput(&result)
106 return nil
107}