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