blob: cecd5ec9c7cf74f79182e3bbe9bb2af04841db98 [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"
20 "fmt"
Zack Williamse940c7a2019-08-21 14:25:39 -070021 "github.com/fullstorydev/grpcurl"
22 flags "github.com/jessevdk/go-flags"
23 "github.com/jhump/protoreflect/dynamic"
Scott Baker2b0ad652019-08-21 14:57:07 -070024 "github.com/opencord/voltctl/pkg/format"
25 "github.com/opencord/voltctl/pkg/model"
Zack Williamse940c7a2019-08-21 14:25:39 -070026 "strings"
27)
28
29const (
30 DEFAULT_LOGICAL_DEVICE_FORMAT = "table{{ .Id }}\t{{.DatapathId}}\t{{.RootDeviceId}}\t{{.SerialNumber}}\t{{.Features.NBuffers}}\t{{.Features.NTables}}\t{{.Features.Capabilities}}"
31 DEFAULT_LOGICAL_DEVICE_PORT_FORMAT = "table{{.Id}}\t{{.DeviceId}}\t{{.DevicePortNo}}\t{{.RootPort}}\t{{.Openflow.PortNo}}\t{{.Openflow.HwAddr}}\t{{.Openflow.Name}}\t{{.Openflow.State}}\t{{.Openflow.Features.Current}}\t{{.Openflow.Bitrate.Current}}"
32 DEFAULT_LOGICAL_DEVICE_INSPECT_FORMAT = `ID: {{.Id}}
33 DATAPATHID: {{.DatapathId}}
34 ROOTDEVICEID: {{.RootDeviceId}}
35 SERIALNUMNER: {{.SerialNumber}}`
36)
37
38type LogicalDeviceId string
39
40type LogicalDeviceList struct {
41 ListOutputOptions
42}
43
44type LogicalDeviceFlowList struct {
45 ListOutputOptions
46 Args struct {
47 Id LogicalDeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
48 } `positional-args:"yes"`
49}
50
51type LogicalDevicePortList struct {
52 ListOutputOptions
53 Args struct {
54 Id LogicalDeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
55 } `positional-args:"yes"`
56}
57
58type LogicalDeviceInspect struct {
59 OutputOptionsJson
60 Args struct {
61 Id LogicalDeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
62 } `positional-args:"yes"`
63}
64
65type LogicalDeviceOpts struct {
66 List LogicalDeviceList `command:"list"`
67 Flows LogicalDeviceFlowList `command:"flows"`
68 Ports LogicalDevicePortList `command:"ports"`
69 Inspect LogicalDeviceInspect `command:"inspect"`
70}
71
72var logicalDeviceOpts = LogicalDeviceOpts{}
73
74func RegisterLogicalDeviceCommands(parser *flags.Parser) {
David Bainbridge12f036f2019-10-15 22:09:04 +000075 if _, err := parser.AddCommand("logicaldevice", "logical device commands", "Commands to query and manipulate VOLTHA logical devices", &logicalDeviceOpts); err != nil {
David Bainbridgea6722342019-10-24 23:55:53 +000076 Error.Fatalf("Unexpected error while attempting to register logical device commands : %s", err)
David Bainbridge12f036f2019-10-15 22:09:04 +000077 }
Zack Williamse940c7a2019-08-21 14:25:39 -070078}
79
80func (i *LogicalDeviceId) Complete(match string) []flags.Completion {
81 conn, err := NewConnection()
82 if err != nil {
83 return nil
84 }
85 defer conn.Close()
86
87 descriptor, method, err := GetMethod("logical-device-list")
88 if err != nil {
89 return nil
90 }
91
92 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
93 defer cancel()
94
95 h := &RpcEventHandler{}
96 err = grpcurl.InvokeRPC(ctx, descriptor, conn, method, []string{}, h, h.GetParams)
97 if err != nil {
98 return nil
99 }
100
101 if h.Status != nil && h.Status.Err() != nil {
102 return nil
103 }
104
105 d, err := dynamic.AsDynamicMessage(h.Response)
106 if err != nil {
107 return nil
108 }
109
110 items, err := d.TryGetFieldByName("items")
111 if err != nil {
112 return nil
113 }
114
115 list := make([]flags.Completion, 0)
116 for _, item := range items.([]interface{}) {
117 val := item.(*dynamic.Message)
118 id := val.GetFieldByName("id").(string)
119 if strings.HasPrefix(id, match) {
120 list = append(list, flags.Completion{Item: id})
121 }
122 }
123
124 return list
125}
126
127func (options *LogicalDeviceList) Execute(args []string) error {
128
129 conn, err := NewConnection()
130 if err != nil {
131 return err
132 }
133 defer conn.Close()
134
135 descriptor, method, err := GetMethod("logical-device-list")
136 if err != nil {
137 return err
138 }
139
140 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
141 defer cancel()
142
143 h := &RpcEventHandler{}
144 err = grpcurl.InvokeRPC(ctx, descriptor, conn, method, []string{}, h, h.GetParams)
145 if err != nil {
146 return err
147 }
148
149 if h.Status != nil && h.Status.Err() != nil {
150 return h.Status.Err()
151 }
152
153 d, err := dynamic.AsDynamicMessage(h.Response)
154 if err != nil {
155 return err
156 }
157
158 items, err := d.TryGetFieldByName("items")
159 if err != nil {
160 return err
161 }
162
163 outputFormat := CharReplacer.Replace(options.Format)
164 if outputFormat == "" {
David Bainbridgea6722342019-10-24 23:55:53 +0000165 outputFormat = GetCommandOptionWithDefault("logical-device-list", "format", DEFAULT_LOGICAL_DEVICE_FORMAT)
Zack Williamse940c7a2019-08-21 14:25:39 -0700166 }
167 if options.Quiet {
168 outputFormat = "{{.Id}}"
169 }
David Bainbridgea6722342019-10-24 23:55:53 +0000170 orderBy := options.OrderBy
171 if orderBy == "" {
172 orderBy = GetCommandOptionWithDefault("local-device-list", "order", "")
173 }
Zack Williamse940c7a2019-08-21 14:25:39 -0700174
175 data := make([]model.LogicalDevice, len(items.([]interface{})))
176 for i, item := range items.([]interface{}) {
177 data[i].PopulateFrom(item.(*dynamic.Message))
178 }
179
180 result := CommandResult{
181 Format: format.Format(outputFormat),
182 Filter: options.Filter,
David Bainbridgea6722342019-10-24 23:55:53 +0000183 OrderBy: orderBy,
Zack Williamse940c7a2019-08-21 14:25:39 -0700184 OutputAs: toOutputType(options.OutputAs),
185 NameLimit: options.NameLimit,
186 Data: data,
187 }
188
189 GenerateOutput(&result)
190 return nil
191}
192
193func (options *LogicalDevicePortList) Execute(args []string) error {
194
195 conn, err := NewConnection()
196 if err != nil {
197 return err
198 }
199 defer conn.Close()
200
201 descriptor, method, err := GetMethod("logical-device-ports")
202 if err != nil {
203 return err
204 }
205
206 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
207 defer cancel()
208
209 h := &RpcEventHandler{
210 Fields: map[string]map[string]interface{}{ParamNames[GlobalConfig.ApiVersion]["ID"]: {"id": options.Args.Id}},
211 }
212 err = grpcurl.InvokeRPC(ctx, descriptor, conn, method, []string{}, h, h.GetParams)
213 if err != nil {
214 return err
215 }
216
217 if h.Status != nil && h.Status.Err() != nil {
218 return h.Status.Err()
219 }
220
221 d, err := dynamic.AsDynamicMessage(h.Response)
222 if err != nil {
223 return err
224 }
225
226 items, err := d.TryGetFieldByName("items")
227 if err != nil {
228 return err
229 }
230
231 outputFormat := CharReplacer.Replace(options.Format)
232 if outputFormat == "" {
David Bainbridgea6722342019-10-24 23:55:53 +0000233 outputFormat = GetCommandOptionWithDefault("logical-device-ports", "format", DEFAULT_LOGICAL_DEVICE_PORT_FORMAT)
Zack Williamse940c7a2019-08-21 14:25:39 -0700234 }
235 if options.Quiet {
236 outputFormat = "{{.Id}}"
237 }
David Bainbridgea6722342019-10-24 23:55:53 +0000238 orderBy := options.OrderBy
239 if orderBy == "" {
240 orderBy = GetCommandOptionWithDefault("logical-device-ports", "order", "")
241 }
Zack Williamse940c7a2019-08-21 14:25:39 -0700242
243 data := make([]model.LogicalPort, len(items.([]interface{})))
244 for i, item := range items.([]interface{}) {
245 data[i].PopulateFrom(item.(*dynamic.Message))
246 }
247
248 result := CommandResult{
249 Format: format.Format(outputFormat),
250 Filter: options.Filter,
David Bainbridgea6722342019-10-24 23:55:53 +0000251 OrderBy: orderBy,
Zack Williamse940c7a2019-08-21 14:25:39 -0700252 OutputAs: toOutputType(options.OutputAs),
253 NameLimit: options.NameLimit,
254 Data: data,
255 }
256
257 GenerateOutput(&result)
258 return nil
259}
260
261func (options *LogicalDeviceFlowList) Execute(args []string) error {
262 fl := &FlowList{}
263 fl.ListOutputOptions = options.ListOutputOptions
264 fl.Args.Id = string(options.Args.Id)
David Bainbridgea6722342019-10-24 23:55:53 +0000265 fl.Method = "logical-device-flows"
Zack Williamse940c7a2019-08-21 14:25:39 -0700266 return fl.Execute(args)
267}
268
269func (options *LogicalDeviceInspect) Execute(args []string) error {
270 if len(args) > 0 {
271 return fmt.Errorf("only a single argument 'DEVICE_ID' can be provided")
272 }
273
274 conn, err := NewConnection()
275 if err != nil {
276 return err
277 }
278 defer conn.Close()
279
280 descriptor, method, err := GetMethod("logical-device-inspect")
281 if err != nil {
282 return err
283 }
284
285 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
286 defer cancel()
287
288 h := &RpcEventHandler{
289 Fields: map[string]map[string]interface{}{ParamNames[GlobalConfig.ApiVersion]["ID"]: {"id": options.Args.Id}},
290 }
291 err = grpcurl.InvokeRPC(ctx, descriptor, conn, method, []string{"Get-Depth: 2"}, h, h.GetParams)
292 if err != nil {
293 return err
294 } else if h.Status != nil && h.Status.Err() != nil {
295 return h.Status.Err()
296 }
297
298 d, err := dynamic.AsDynamicMessage(h.Response)
299 if err != nil {
300 return err
301 }
302
303 device := &model.LogicalDevice{}
304 device.PopulateFrom(d)
305
306 outputFormat := CharReplacer.Replace(options.Format)
307 if outputFormat == "" {
David Bainbridgea6722342019-10-24 23:55:53 +0000308 outputFormat = GetCommandOptionWithDefault("logical-device-inspect", "format", DEFAULT_LOGICAL_DEVICE_INSPECT_FORMAT)
Zack Williamse940c7a2019-08-21 14:25:39 -0700309 }
310 if options.Quiet {
311 outputFormat = "{{.Id}}"
312 }
313
314 result := CommandResult{
315 Format: format.Format(outputFormat),
316 OutputAs: toOutputType(options.OutputAs),
317 NameLimit: options.NameLimit,
318 Data: device,
319 }
320 GenerateOutput(&result)
321 return nil
322}