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 | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 24 | "github.com/opencord/voltha-protos/v3/go/voltha" |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 25 | "strings" |
| 26 | ) |
| 27 | |
| 28 | const ( |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 29 | 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 Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 30 | 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 Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 34 | SERIALNUMNER: {{.Desc.SerialNum}}` |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 35 | ) |
| 36 | |
| 37 | type LogicalDeviceId string |
| 38 | |
| 39 | type LogicalDeviceList struct { |
| 40 | ListOutputOptions |
| 41 | } |
| 42 | |
| 43 | type LogicalDeviceFlowList struct { |
| 44 | ListOutputOptions |
| 45 | Args struct { |
| 46 | Id LogicalDeviceId `positional-arg-name:"DEVICE_ID" required:"yes"` |
| 47 | } `positional-args:"yes"` |
| 48 | } |
| 49 | |
| 50 | type LogicalDevicePortList struct { |
| 51 | ListOutputOptions |
| 52 | Args struct { |
| 53 | Id LogicalDeviceId `positional-arg-name:"DEVICE_ID" required:"yes"` |
| 54 | } `positional-args:"yes"` |
| 55 | } |
| 56 | |
| 57 | type LogicalDeviceInspect struct { |
| 58 | OutputOptionsJson |
| 59 | Args struct { |
| 60 | Id LogicalDeviceId `positional-arg-name:"DEVICE_ID" required:"yes"` |
| 61 | } `positional-args:"yes"` |
| 62 | } |
| 63 | |
| 64 | type LogicalDeviceOpts struct { |
kesavand | 12cd8eb | 2020-01-20 22:25:22 -0500 | [diff] [blame] | 65 | 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 Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | var logicalDeviceOpts = LogicalDeviceOpts{} |
| 74 | |
| 75 | func RegisterLogicalDeviceCommands(parser *flags.Parser) { |
David Bainbridge | 12f036f | 2019-10-15 22:09:04 +0000 | [diff] [blame] | 76 | 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] | 77 | 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] | 78 | } |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | func (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 Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 88 | client := voltha.NewVolthaServiceClient(conn) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 89 | |
| 90 | ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout) |
| 91 | defer cancel() |
| 92 | |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 93 | logicalDevices, err := client.ListLogicalDevices(ctx, &empty.Empty{}) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 94 | if err != nil { |
| 95 | return nil |
| 96 | } |
| 97 | |
| 98 | list := make([]flags.Completion, 0) |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 99 | for _, item := range logicalDevices.Items { |
| 100 | if strings.HasPrefix(item.Id, match) { |
| 101 | list = append(list, flags.Completion{Item: item.Id}) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 102 | } |
| 103 | } |
| 104 | |
| 105 | return list |
| 106 | } |
| 107 | |
| 108 | func (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 Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 116 | client := voltha.NewVolthaServiceClient(conn) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 117 | |
| 118 | ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout) |
| 119 | defer cancel() |
| 120 | |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 121 | logicalDevices, err := client.ListLogicalDevices(ctx, &empty.Empty{}) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 122 | if err != nil { |
| 123 | return err |
| 124 | } |
| 125 | |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 126 | // Make sure json output prints an empty list, not "null" |
| 127 | if logicalDevices.Items == nil { |
| 128 | logicalDevices.Items = make([]*voltha.LogicalDevice, 0) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | outputFormat := CharReplacer.Replace(options.Format) |
| 132 | if outputFormat == "" { |
David Bainbridge | a672234 | 2019-10-24 23:55:53 +0000 | [diff] [blame] | 133 | outputFormat = GetCommandOptionWithDefault("logical-device-list", "format", DEFAULT_LOGICAL_DEVICE_FORMAT) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 134 | } |
| 135 | if options.Quiet { |
| 136 | outputFormat = "{{.Id}}" |
| 137 | } |
David Bainbridge | a672234 | 2019-10-24 23:55:53 +0000 | [diff] [blame] | 138 | orderBy := options.OrderBy |
| 139 | if orderBy == "" { |
| 140 | orderBy = GetCommandOptionWithDefault("local-device-list", "order", "") |
| 141 | } |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 142 | |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 143 | result := CommandResult{ |
| 144 | Format: format.Format(outputFormat), |
| 145 | Filter: options.Filter, |
David Bainbridge | a672234 | 2019-10-24 23:55:53 +0000 | [diff] [blame] | 146 | OrderBy: orderBy, |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 147 | OutputAs: toOutputType(options.OutputAs), |
| 148 | NameLimit: options.NameLimit, |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 149 | Data: logicalDevices.Items, |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | GenerateOutput(&result) |
| 153 | return nil |
| 154 | } |
| 155 | |
| 156 | func (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 Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 164 | client := voltha.NewVolthaServiceClient(conn) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 165 | |
| 166 | ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout) |
| 167 | defer cancel() |
| 168 | |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 169 | id := voltha.ID{Id: string(options.Args.Id)} |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 170 | |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 171 | ports, err := client.ListLogicalDevicePorts(ctx, &id) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 172 | if err != nil { |
| 173 | return err |
| 174 | } |
| 175 | |
| 176 | outputFormat := CharReplacer.Replace(options.Format) |
| 177 | if outputFormat == "" { |
David Bainbridge | a672234 | 2019-10-24 23:55:53 +0000 | [diff] [blame] | 178 | outputFormat = GetCommandOptionWithDefault("logical-device-ports", "format", DEFAULT_LOGICAL_DEVICE_PORT_FORMAT) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 179 | } |
| 180 | if options.Quiet { |
| 181 | outputFormat = "{{.Id}}" |
| 182 | } |
David Bainbridge | a672234 | 2019-10-24 23:55:53 +0000 | [diff] [blame] | 183 | orderBy := options.OrderBy |
| 184 | if orderBy == "" { |
| 185 | orderBy = GetCommandOptionWithDefault("logical-device-ports", "order", "") |
| 186 | } |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 187 | |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 188 | result := CommandResult{ |
| 189 | Format: format.Format(outputFormat), |
| 190 | Filter: options.Filter, |
David Bainbridge | a672234 | 2019-10-24 23:55:53 +0000 | [diff] [blame] | 191 | OrderBy: orderBy, |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 192 | OutputAs: toOutputType(options.OutputAs), |
| 193 | NameLimit: options.NameLimit, |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 194 | Data: ports.Items, |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | GenerateOutput(&result) |
| 198 | return nil |
| 199 | } |
| 200 | |
| 201 | func (options *LogicalDeviceFlowList) Execute(args []string) error { |
| 202 | fl := &FlowList{} |
| 203 | fl.ListOutputOptions = options.ListOutputOptions |
| 204 | fl.Args.Id = string(options.Args.Id) |
David Bainbridge | a672234 | 2019-10-24 23:55:53 +0000 | [diff] [blame] | 205 | fl.Method = "logical-device-flows" |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 206 | return fl.Execute(args) |
| 207 | } |
| 208 | |
| 209 | func (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 Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 220 | client := voltha.NewVolthaServiceClient(conn) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 221 | |
| 222 | ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout) |
| 223 | defer cancel() |
| 224 | |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 225 | id := voltha.ID{Id: string(options.Args.Id)} |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 226 | |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 227 | logicalDevice, err := client.GetLogicalDevice(ctx, &id) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 228 | if err != nil { |
| 229 | return err |
| 230 | } |
| 231 | |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 232 | outputFormat := CharReplacer.Replace(options.Format) |
| 233 | if outputFormat == "" { |
David Bainbridge | a672234 | 2019-10-24 23:55:53 +0000 | [diff] [blame] | 234 | outputFormat = GetCommandOptionWithDefault("logical-device-inspect", "format", DEFAULT_LOGICAL_DEVICE_INSPECT_FORMAT) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 235 | } |
| 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 Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 244 | Data: logicalDevice, |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 245 | } |
| 246 | GenerateOutput(&result) |
| 247 | return nil |
| 248 | } |