blob: e59f69d8330c1c2dea7f492c27bdbe1902cfa313 [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"
Scott Baker9173ed82020-05-19 08:30:12 -070021 "github.com/golang/protobuf/ptypes/empty"
Zack Williamse940c7a2019-08-21 14:25:39 -070022 flags "github.com/jessevdk/go-flags"
Scott Baker2b0ad652019-08-21 14:57:07 -070023 "github.com/opencord/voltctl/pkg/format"
Scott Baker9173ed82020-05-19 08:30:12 -070024 "github.com/opencord/voltha-protos/v3/go/voltha"
Zack Williamse940c7a2019-08-21 14:25:39 -070025 "strings"
26)
27
28const (
Scott Baker9173ed82020-05-19 08:30:12 -070029 DEFAULT_LOGICAL_DEVICE_FORMAT = "table{{ .Id }}\t{{printf \"%016x\" .DatapathId}}\t{{.RootDeviceId}}\t{{.Desc.SerialNum}}\t{{.SwitchFeatures.NBuffers}}\t{{.SwitchFeatures.NTables}}\t{{printf \"0x%08x\" .SwitchFeatures.Capabilities}}"
Zack Williamse940c7a2019-08-21 14:25:39 -070030 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}}"
31 DEFAULT_LOGICAL_DEVICE_INSPECT_FORMAT = `ID: {{.Id}}
32 DATAPATHID: {{.DatapathId}}
33 ROOTDEVICEID: {{.RootDeviceId}}
Scott Baker9173ed82020-05-19 08:30:12 -070034 SERIALNUMNER: {{.Desc.SerialNum}}`
Zack Williamse940c7a2019-08-21 14:25:39 -070035)
36
37type LogicalDeviceId string
38
39type LogicalDeviceList struct {
40 ListOutputOptions
41}
42
43type LogicalDeviceFlowList struct {
44 ListOutputOptions
45 Args struct {
46 Id LogicalDeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
47 } `positional-args:"yes"`
48}
49
50type LogicalDevicePortList struct {
51 ListOutputOptions
52 Args struct {
53 Id LogicalDeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
54 } `positional-args:"yes"`
55}
56
57type LogicalDeviceInspect struct {
58 OutputOptionsJson
59 Args struct {
60 Id LogicalDeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
61 } `positional-args:"yes"`
62}
63
64type LogicalDeviceOpts struct {
kesavand12cd8eb2020-01-20 22:25:22 -050065 List LogicalDeviceList `command:"list"`
66 Flows LogicalDeviceFlowList `command:"flows"`
67 Port struct {
68 List LogicalDevicePortList `command:"list"`
69 } `command:"port"`
70 Inspect LogicalDeviceInspect `command:"inspect"`
Zack Williamse940c7a2019-08-21 14:25:39 -070071}
72
73var logicalDeviceOpts = LogicalDeviceOpts{}
74
75func RegisterLogicalDeviceCommands(parser *flags.Parser) {
David Bainbridge12f036f2019-10-15 22:09:04 +000076 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 +000077 Error.Fatalf("Unexpected error while attempting to register logical device commands : %s", err)
David Bainbridge12f036f2019-10-15 22:09:04 +000078 }
Zack Williamse940c7a2019-08-21 14:25:39 -070079}
80
81func (i *LogicalDeviceId) Complete(match string) []flags.Completion {
82 conn, err := NewConnection()
83 if err != nil {
84 return nil
85 }
86 defer conn.Close()
87
Scott Baker9173ed82020-05-19 08:30:12 -070088 client := voltha.NewVolthaServiceClient(conn)
Zack Williamse940c7a2019-08-21 14:25:39 -070089
90 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
91 defer cancel()
92
Scott Baker9173ed82020-05-19 08:30:12 -070093 logicalDevices, err := client.ListLogicalDevices(ctx, &empty.Empty{})
Zack Williamse940c7a2019-08-21 14:25:39 -070094 if err != nil {
95 return nil
96 }
97
98 list := make([]flags.Completion, 0)
Scott Baker9173ed82020-05-19 08:30:12 -070099 for _, item := range logicalDevices.Items {
100 if strings.HasPrefix(item.Id, match) {
101 list = append(list, flags.Completion{Item: item.Id})
Zack Williamse940c7a2019-08-21 14:25:39 -0700102 }
103 }
104
105 return list
106}
107
108func (options *LogicalDeviceList) Execute(args []string) error {
109
110 conn, err := NewConnection()
111 if err != nil {
112 return err
113 }
114 defer conn.Close()
115
Scott Baker9173ed82020-05-19 08:30:12 -0700116 client := voltha.NewVolthaServiceClient(conn)
Zack Williamse940c7a2019-08-21 14:25:39 -0700117
118 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
119 defer cancel()
120
Scott Baker9173ed82020-05-19 08:30:12 -0700121 logicalDevices, err := client.ListLogicalDevices(ctx, &empty.Empty{})
Zack Williamse940c7a2019-08-21 14:25:39 -0700122 if err != nil {
123 return err
124 }
125
Scott Baker9173ed82020-05-19 08:30:12 -0700126 // Make sure json output prints an empty list, not "null"
127 if logicalDevices.Items == nil {
128 logicalDevices.Items = make([]*voltha.LogicalDevice, 0)
Zack Williamse940c7a2019-08-21 14:25:39 -0700129 }
130
131 outputFormat := CharReplacer.Replace(options.Format)
132 if outputFormat == "" {
David Bainbridgea6722342019-10-24 23:55:53 +0000133 outputFormat = GetCommandOptionWithDefault("logical-device-list", "format", DEFAULT_LOGICAL_DEVICE_FORMAT)
Zack Williamse940c7a2019-08-21 14:25:39 -0700134 }
135 if options.Quiet {
136 outputFormat = "{{.Id}}"
137 }
David Bainbridgea6722342019-10-24 23:55:53 +0000138 orderBy := options.OrderBy
139 if orderBy == "" {
140 orderBy = GetCommandOptionWithDefault("local-device-list", "order", "")
141 }
Zack Williamse940c7a2019-08-21 14:25:39 -0700142
Zack Williamse940c7a2019-08-21 14:25:39 -0700143 result := CommandResult{
144 Format: format.Format(outputFormat),
145 Filter: options.Filter,
David Bainbridgea6722342019-10-24 23:55:53 +0000146 OrderBy: orderBy,
Zack Williamse940c7a2019-08-21 14:25:39 -0700147 OutputAs: toOutputType(options.OutputAs),
148 NameLimit: options.NameLimit,
Scott Baker9173ed82020-05-19 08:30:12 -0700149 Data: logicalDevices.Items,
Zack Williamse940c7a2019-08-21 14:25:39 -0700150 }
151
152 GenerateOutput(&result)
153 return nil
154}
155
156func (options *LogicalDevicePortList) Execute(args []string) error {
157
158 conn, err := NewConnection()
159 if err != nil {
160 return err
161 }
162 defer conn.Close()
163
Scott Baker9173ed82020-05-19 08:30:12 -0700164 client := voltha.NewVolthaServiceClient(conn)
Zack Williamse940c7a2019-08-21 14:25:39 -0700165
166 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
167 defer cancel()
168
Scott Baker9173ed82020-05-19 08:30:12 -0700169 id := voltha.ID{Id: string(options.Args.Id)}
Zack Williamse940c7a2019-08-21 14:25:39 -0700170
Scott Baker9173ed82020-05-19 08:30:12 -0700171 ports, err := client.ListLogicalDevicePorts(ctx, &id)
Zack Williamse940c7a2019-08-21 14:25:39 -0700172 if err != nil {
173 return err
174 }
175
176 outputFormat := CharReplacer.Replace(options.Format)
177 if outputFormat == "" {
David Bainbridgea6722342019-10-24 23:55:53 +0000178 outputFormat = GetCommandOptionWithDefault("logical-device-ports", "format", DEFAULT_LOGICAL_DEVICE_PORT_FORMAT)
Zack Williamse940c7a2019-08-21 14:25:39 -0700179 }
180 if options.Quiet {
181 outputFormat = "{{.Id}}"
182 }
David Bainbridgea6722342019-10-24 23:55:53 +0000183 orderBy := options.OrderBy
184 if orderBy == "" {
185 orderBy = GetCommandOptionWithDefault("logical-device-ports", "order", "")
186 }
Zack Williamse940c7a2019-08-21 14:25:39 -0700187
Zack Williamse940c7a2019-08-21 14:25:39 -0700188 result := CommandResult{
189 Format: format.Format(outputFormat),
190 Filter: options.Filter,
David Bainbridgea6722342019-10-24 23:55:53 +0000191 OrderBy: orderBy,
Zack Williamse940c7a2019-08-21 14:25:39 -0700192 OutputAs: toOutputType(options.OutputAs),
193 NameLimit: options.NameLimit,
Scott Baker9173ed82020-05-19 08:30:12 -0700194 Data: ports.Items,
Zack Williamse940c7a2019-08-21 14:25:39 -0700195 }
196
197 GenerateOutput(&result)
198 return nil
199}
200
201func (options *LogicalDeviceFlowList) Execute(args []string) error {
202 fl := &FlowList{}
203 fl.ListOutputOptions = options.ListOutputOptions
204 fl.Args.Id = string(options.Args.Id)
David Bainbridgea6722342019-10-24 23:55:53 +0000205 fl.Method = "logical-device-flows"
Zack Williamse940c7a2019-08-21 14:25:39 -0700206 return fl.Execute(args)
207}
208
209func (options *LogicalDeviceInspect) Execute(args []string) error {
210 if len(args) > 0 {
211 return fmt.Errorf("only a single argument 'DEVICE_ID' can be provided")
212 }
213
214 conn, err := NewConnection()
215 if err != nil {
216 return err
217 }
218 defer conn.Close()
219
Scott Baker9173ed82020-05-19 08:30:12 -0700220 client := voltha.NewVolthaServiceClient(conn)
Zack Williamse940c7a2019-08-21 14:25:39 -0700221
222 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
223 defer cancel()
224
Scott Baker9173ed82020-05-19 08:30:12 -0700225 id := voltha.ID{Id: string(options.Args.Id)}
Zack Williamse940c7a2019-08-21 14:25:39 -0700226
Scott Baker9173ed82020-05-19 08:30:12 -0700227 logicalDevice, err := client.GetLogicalDevice(ctx, &id)
Zack Williamse940c7a2019-08-21 14:25:39 -0700228 if err != nil {
229 return err
230 }
231
Zack Williamse940c7a2019-08-21 14:25:39 -0700232 outputFormat := CharReplacer.Replace(options.Format)
233 if outputFormat == "" {
David Bainbridgea6722342019-10-24 23:55:53 +0000234 outputFormat = GetCommandOptionWithDefault("logical-device-inspect", "format", DEFAULT_LOGICAL_DEVICE_INSPECT_FORMAT)
Zack Williamse940c7a2019-08-21 14:25:39 -0700235 }
236 if options.Quiet {
237 outputFormat = "{{.Id}}"
238 }
239
240 result := CommandResult{
241 Format: format.Format(outputFormat),
242 OutputAs: toOutputType(options.OutputAs),
243 NameLimit: options.NameLimit,
Scott Baker9173ed82020-05-19 08:30:12 -0700244 Data: logicalDevice,
Zack Williamse940c7a2019-08-21 14:25:39 -0700245 }
246 GenerateOutput(&result)
247 return nil
248}