blob: 7314b0acb0fc4b55d5b4ff7c33a979246c7a1074 [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"
David Bainbridge12f036f2019-10-15 22:09:04 +000025 "log"
Zack Williamse940c7a2019-08-21 14:25:39 -070026)
27
28const (
29 DEFAULT_DEVICE_GROUP_FORMAT = "table{{ .Id }}\t{{.LogicalDevices}}\t{{.Devices}}"
30)
31
32type DeviceGroupList struct {
33 ListOutputOptions
34}
35
36type DeviceGroupOpts struct {
37 List DeviceGroupList `command:"list"`
38}
39
40var deviceGroupOpts = DeviceGroupOpts{}
41
42func RegisterDeviceGroupCommands(parser *flags.Parser) {
David Bainbridge12f036f2019-10-15 22:09:04 +000043 if _, err := parser.AddCommand("devicegroup", "device group commands", "Commands to query and manipulate VOLTHA device groups",
44 &deviceGroupOpts); err != nil {
45 log.Fatalf("Unexpected error while attempting to register device group commands : %s", err)
46 }
Zack Williamse940c7a2019-08-21 14:25:39 -070047}
48
49func (options *DeviceGroupList) Execute(args []string) error {
50
51 conn, err := NewConnection()
52 if err != nil {
53 return err
54 }
55 defer conn.Close()
56
57 descriptor, method, err := GetMethod("devicegroup-list")
58 if err != nil {
59 return err
60 }
61
62 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
63 defer cancel()
64
65 h := &RpcEventHandler{}
66 err = grpcurl.InvokeRPC(ctx, descriptor, conn, method, []string{}, h, h.GetParams)
67 if err != nil {
68 return err
69 }
70
71 if h.Status != nil && h.Status.Err() != nil {
72 return h.Status.Err()
73 }
74
75 d, err := dynamic.AsDynamicMessage(h.Response)
76 if err != nil {
77 return err
78 }
79
80 items, err := d.TryGetFieldByName("items")
81 if err != nil {
82 return err
83 }
84
85 outputFormat := CharReplacer.Replace(options.Format)
86 if outputFormat == "" {
87 outputFormat = DEFAULT_DEVICE_GROUP_FORMAT
88 }
89 if options.Quiet {
90 outputFormat = "{{.Id}}"
91 }
92
93 data := make([]model.DeviceGroup, len(items.([]interface{})))
94 for i, item := range items.([]interface{}) {
95 val := item.(*dynamic.Message)
96 data[i].PopulateFrom(val)
97 }
98
99 result := CommandResult{
100 Format: format.Format(outputFormat),
101 Filter: options.Filter,
102 OrderBy: options.OrderBy,
103 OutputAs: toOutputType(options.OutputAs),
104 NameLimit: options.NameLimit,
105 Data: data,
106 }
107
108 GenerateOutput(&result)
109 return nil
110}