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