Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package commands |
| 17 | |
| 18 | import ( |
| 19 | "context" |
| 20 | "fmt" |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 21 | "github.com/golang/protobuf/ptypes/empty" |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 22 | flags "github.com/jessevdk/go-flags" |
Scott Baker | 2b0ad65 | 2019-08-21 14:57:07 -0700 | [diff] [blame] | 23 | "github.com/opencord/voltctl/pkg/format" |
David K. Bainbridge | bd6b288 | 2021-08-26 13:31:02 +0000 | [diff] [blame] | 24 | "github.com/opencord/voltha-protos/v5/go/openflow_13" |
| 25 | "github.com/opencord/voltha-protos/v5/go/voltha" |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 26 | "strings" |
| 27 | ) |
| 28 | |
| 29 | const ( |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 30 | 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}}" |
Hardik Windlass | 9361bb8 | 2022-03-23 05:58:48 +0000 | [diff] [blame] | 31 | DEFAULT_LOGICAL_DEVICE_ORDER = "Id" |
Scott Baker | b6f0769 | 2020-06-10 12:52:15 -0700 | [diff] [blame] | 32 | DEFAULT_LOGICAL_DEVICE_PORT_FORMAT = "table{{.Id}}\t{{.DeviceId}}\t{{.DevicePortNo}}\t{{.RootPort}}\t{{.OfpPortStats.PortNo}}\t{{.OfpPort.HwAddr}}\t{{.OfpPort.Name}}\t{{printf \"0x%08x\" .OfpPort.State}}\t{{printf \"0x%08x\" .OfpPort.Curr}}\t{{.OfpPort.CurrSpeed}}" |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 33 | DEFAULT_LOGICAL_DEVICE_INSPECT_FORMAT = `ID: {{.Id}} |
| 34 | DATAPATHID: {{.DatapathId}} |
| 35 | ROOTDEVICEID: {{.RootDeviceId}} |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 36 | SERIALNUMNER: {{.Desc.SerialNum}}` |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 37 | ) |
| 38 | |
| 39 | type LogicalDeviceId string |
| 40 | |
| 41 | type LogicalDeviceList struct { |
| 42 | ListOutputOptions |
| 43 | } |
| 44 | |
| 45 | type LogicalDeviceFlowList struct { |
| 46 | ListOutputOptions |
Maninder | 045921e | 2020-09-29 16:46:02 +0530 | [diff] [blame] | 47 | FlowIdOptions |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 48 | Args struct { |
| 49 | Id LogicalDeviceId `positional-arg-name:"DEVICE_ID" required:"yes"` |
| 50 | } `positional-args:"yes"` |
| 51 | } |
| 52 | |
Himani Chawla | 3c161c6 | 2021-05-13 16:36:51 +0530 | [diff] [blame] | 53 | type LogicalDeviceFlowGroupList struct { |
| 54 | ListOutputOptions |
| 55 | GroupListOptions |
| 56 | Args struct { |
| 57 | Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"` |
| 58 | } `positional-args:"yes"` |
| 59 | } |
| 60 | |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 61 | type LogicalDevicePortList struct { |
| 62 | ListOutputOptions |
| 63 | Args struct { |
| 64 | Id LogicalDeviceId `positional-arg-name:"DEVICE_ID" required:"yes"` |
| 65 | } `positional-args:"yes"` |
| 66 | } |
| 67 | |
| 68 | type LogicalDeviceInspect struct { |
| 69 | OutputOptionsJson |
| 70 | Args struct { |
| 71 | Id LogicalDeviceId `positional-arg-name:"DEVICE_ID" required:"yes"` |
| 72 | } `positional-args:"yes"` |
| 73 | } |
| 74 | |
| 75 | type LogicalDeviceOpts struct { |
Himani Chawla | 3c161c6 | 2021-05-13 16:36:51 +0530 | [diff] [blame] | 76 | List LogicalDeviceList `command:"list"` |
| 77 | Flows LogicalDeviceFlowList `command:"flows"` |
| 78 | Groups LogicalDeviceFlowGroupList `command:"groups"` |
| 79 | Port struct { |
kesavand | 12cd8eb | 2020-01-20 22:25:22 -0500 | [diff] [blame] | 80 | List LogicalDevicePortList `command:"list"` |
| 81 | } `command:"port"` |
| 82 | Inspect LogicalDeviceInspect `command:"inspect"` |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | var logicalDeviceOpts = LogicalDeviceOpts{} |
| 86 | |
| 87 | func RegisterLogicalDeviceCommands(parser *flags.Parser) { |
David Bainbridge | 12f036f | 2019-10-15 22:09:04 +0000 | [diff] [blame] | 88 | if _, err := parser.AddCommand("logicaldevice", "logical device commands", "Commands to query and manipulate VOLTHA logical devices", &logicalDeviceOpts); err != nil { |
David Bainbridge | a672234 | 2019-10-24 23:55:53 +0000 | [diff] [blame] | 89 | Error.Fatalf("Unexpected error while attempting to register logical device commands : %s", err) |
David Bainbridge | 12f036f | 2019-10-15 22:09:04 +0000 | [diff] [blame] | 90 | } |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | func (i *LogicalDeviceId) Complete(match string) []flags.Completion { |
| 94 | conn, err := NewConnection() |
| 95 | if err != nil { |
| 96 | return nil |
| 97 | } |
| 98 | defer conn.Close() |
| 99 | |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 100 | client := voltha.NewVolthaServiceClient(conn) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 101 | |
David K. Bainbridge | 9189c63 | 2021-03-26 21:52:21 +0000 | [diff] [blame] | 102 | ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 103 | defer cancel() |
| 104 | |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 105 | logicalDevices, err := client.ListLogicalDevices(ctx, &empty.Empty{}) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 106 | if err != nil { |
| 107 | return nil |
| 108 | } |
| 109 | |
| 110 | list := make([]flags.Completion, 0) |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 111 | for _, item := range logicalDevices.Items { |
| 112 | if strings.HasPrefix(item.Id, match) { |
| 113 | list = append(list, flags.Completion{Item: item.Id}) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 114 | } |
| 115 | } |
| 116 | |
| 117 | return list |
| 118 | } |
| 119 | |
| 120 | func (options *LogicalDeviceList) Execute(args []string) error { |
| 121 | |
| 122 | conn, err := NewConnection() |
| 123 | if err != nil { |
| 124 | return err |
| 125 | } |
| 126 | defer conn.Close() |
| 127 | |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 128 | client := voltha.NewVolthaServiceClient(conn) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 129 | |
David K. Bainbridge | 9189c63 | 2021-03-26 21:52:21 +0000 | [diff] [blame] | 130 | ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 131 | defer cancel() |
| 132 | |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 133 | logicalDevices, err := client.ListLogicalDevices(ctx, &empty.Empty{}) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 134 | if err != nil { |
| 135 | return err |
| 136 | } |
| 137 | |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 138 | // Make sure json output prints an empty list, not "null" |
| 139 | if logicalDevices.Items == nil { |
| 140 | logicalDevices.Items = make([]*voltha.LogicalDevice, 0) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | outputFormat := CharReplacer.Replace(options.Format) |
| 144 | if outputFormat == "" { |
David Bainbridge | a672234 | 2019-10-24 23:55:53 +0000 | [diff] [blame] | 145 | outputFormat = GetCommandOptionWithDefault("logical-device-list", "format", DEFAULT_LOGICAL_DEVICE_FORMAT) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 146 | } |
| 147 | if options.Quiet { |
| 148 | outputFormat = "{{.Id}}" |
| 149 | } |
David Bainbridge | a672234 | 2019-10-24 23:55:53 +0000 | [diff] [blame] | 150 | orderBy := options.OrderBy |
| 151 | if orderBy == "" { |
Hardik Windlass | 9361bb8 | 2022-03-23 05:58:48 +0000 | [diff] [blame] | 152 | orderBy = GetCommandOptionWithDefault("local-device-list", "order", DEFAULT_LOGICAL_DEVICE_ORDER) |
David Bainbridge | a672234 | 2019-10-24 23:55:53 +0000 | [diff] [blame] | 153 | } |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 154 | |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 155 | result := CommandResult{ |
| 156 | Format: format.Format(outputFormat), |
| 157 | Filter: options.Filter, |
David Bainbridge | a672234 | 2019-10-24 23:55:53 +0000 | [diff] [blame] | 158 | OrderBy: orderBy, |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 159 | OutputAs: toOutputType(options.OutputAs), |
| 160 | NameLimit: options.NameLimit, |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 161 | Data: logicalDevices.Items, |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | GenerateOutput(&result) |
| 165 | return nil |
| 166 | } |
| 167 | |
| 168 | func (options *LogicalDevicePortList) Execute(args []string) error { |
| 169 | |
| 170 | conn, err := NewConnection() |
| 171 | if err != nil { |
| 172 | return err |
| 173 | } |
| 174 | defer conn.Close() |
| 175 | |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 176 | client := voltha.NewVolthaServiceClient(conn) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 177 | |
David K. Bainbridge | 9189c63 | 2021-03-26 21:52:21 +0000 | [diff] [blame] | 178 | ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 179 | defer cancel() |
| 180 | |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 181 | id := voltha.ID{Id: string(options.Args.Id)} |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 182 | |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 183 | ports, err := client.ListLogicalDevicePorts(ctx, &id) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 184 | if err != nil { |
| 185 | return err |
| 186 | } |
| 187 | |
Scott Baker | b6f0769 | 2020-06-10 12:52:15 -0700 | [diff] [blame] | 188 | // ensure no nil pointers |
| 189 | for _, v := range ports.Items { |
| 190 | if v.OfpPortStats == nil { |
| 191 | v.OfpPortStats = &openflow_13.OfpPortStats{} |
| 192 | } |
| 193 | if v.OfpPort == nil { |
| 194 | v.OfpPort = &openflow_13.OfpPort{} |
| 195 | } |
| 196 | } |
| 197 | |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 198 | outputFormat := CharReplacer.Replace(options.Format) |
| 199 | if outputFormat == "" { |
David Bainbridge | a672234 | 2019-10-24 23:55:53 +0000 | [diff] [blame] | 200 | outputFormat = GetCommandOptionWithDefault("logical-device-ports", "format", DEFAULT_LOGICAL_DEVICE_PORT_FORMAT) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 201 | } |
| 202 | if options.Quiet { |
| 203 | outputFormat = "{{.Id}}" |
| 204 | } |
David Bainbridge | a672234 | 2019-10-24 23:55:53 +0000 | [diff] [blame] | 205 | orderBy := options.OrderBy |
| 206 | if orderBy == "" { |
| 207 | orderBy = GetCommandOptionWithDefault("logical-device-ports", "order", "") |
| 208 | } |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 209 | |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 210 | result := CommandResult{ |
| 211 | Format: format.Format(outputFormat), |
| 212 | Filter: options.Filter, |
David Bainbridge | a672234 | 2019-10-24 23:55:53 +0000 | [diff] [blame] | 213 | OrderBy: orderBy, |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 214 | OutputAs: toOutputType(options.OutputAs), |
| 215 | NameLimit: options.NameLimit, |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 216 | Data: ports.Items, |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | GenerateOutput(&result) |
| 220 | return nil |
| 221 | } |
| 222 | |
| 223 | func (options *LogicalDeviceFlowList) Execute(args []string) error { |
| 224 | fl := &FlowList{} |
| 225 | fl.ListOutputOptions = options.ListOutputOptions |
Maninder | 045921e | 2020-09-29 16:46:02 +0530 | [diff] [blame] | 226 | fl.FlowIdOptions = options.FlowIdOptions |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 227 | fl.Args.Id = string(options.Args.Id) |
David Bainbridge | a672234 | 2019-10-24 23:55:53 +0000 | [diff] [blame] | 228 | fl.Method = "logical-device-flows" |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 229 | return fl.Execute(args) |
| 230 | } |
| 231 | |
Himani Chawla | 3c161c6 | 2021-05-13 16:36:51 +0530 | [diff] [blame] | 232 | func (options *LogicalDeviceFlowGroupList) Execute(args []string) error { |
| 233 | grp := &GroupList{} |
| 234 | grp.ListOutputOptions = options.ListOutputOptions |
| 235 | grp.GroupListOptions = options.GroupListOptions |
| 236 | grp.Args.Id = string(options.Args.Id) |
| 237 | grp.Method = "logical-device-groups" |
| 238 | return grp.Execute(args) |
| 239 | |
| 240 | } |
| 241 | |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 242 | func (options *LogicalDeviceInspect) Execute(args []string) error { |
| 243 | if len(args) > 0 { |
| 244 | return fmt.Errorf("only a single argument 'DEVICE_ID' can be provided") |
| 245 | } |
| 246 | |
| 247 | conn, err := NewConnection() |
| 248 | if err != nil { |
| 249 | return err |
| 250 | } |
| 251 | defer conn.Close() |
| 252 | |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 253 | client := voltha.NewVolthaServiceClient(conn) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 254 | |
David K. Bainbridge | 9189c63 | 2021-03-26 21:52:21 +0000 | [diff] [blame] | 255 | ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 256 | defer cancel() |
| 257 | |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 258 | id := voltha.ID{Id: string(options.Args.Id)} |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 259 | |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 260 | logicalDevice, err := client.GetLogicalDevice(ctx, &id) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 261 | if err != nil { |
| 262 | return err |
| 263 | } |
| 264 | |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 265 | outputFormat := CharReplacer.Replace(options.Format) |
| 266 | if outputFormat == "" { |
David Bainbridge | a672234 | 2019-10-24 23:55:53 +0000 | [diff] [blame] | 267 | outputFormat = GetCommandOptionWithDefault("logical-device-inspect", "format", DEFAULT_LOGICAL_DEVICE_INSPECT_FORMAT) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 268 | } |
| 269 | if options.Quiet { |
| 270 | outputFormat = "{{.Id}}" |
| 271 | } |
| 272 | |
| 273 | result := CommandResult{ |
| 274 | Format: format.Format(outputFormat), |
| 275 | OutputAs: toOutputType(options.OutputAs), |
| 276 | NameLimit: options.NameLimit, |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 277 | Data: logicalDevice, |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 278 | } |
| 279 | GenerateOutput(&result) |
| 280 | return nil |
| 281 | } |