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" |
David Bainbridge | 7052fe8 | 2020-03-25 10:37:00 -0700 | [diff] [blame] | 21 | "os" |
| 22 | "strconv" |
| 23 | "strings" |
| 24 | |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 25 | "github.com/golang/protobuf/ptypes/empty" |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 26 | flags "github.com/jessevdk/go-flags" |
Scott Baker | 2b0ad65 | 2019-08-21 14:57:07 -0700 | [diff] [blame] | 27 | "github.com/opencord/voltctl/pkg/format" |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 28 | "github.com/opencord/voltha-protos/v3/go/common" |
| 29 | "github.com/opencord/voltha-protos/v3/go/voltha" |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 30 | ) |
| 31 | |
| 32 | const ( |
David K. Bainbridge | 89003c4 | 2020-02-27 17:22:49 -0800 | [diff] [blame] | 33 | DEFAULT_DEVICE_FORMAT = "table{{ .Id }}\t{{.Type}}\t{{.Root}}\t{{.ParentId}}\t{{.SerialNumber}}\t{{.AdminState}}\t{{.OperStatus}}\t{{.ConnectStatus}}\t{{.Reason}}" |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 34 | DEFAULT_DEVICE_PORTS_FORMAT = "table{{.PortNo}}\t{{.Label}}\t{{.Type}}\t{{.AdminState}}\t{{.OperStatus}}\t{{.DeviceId}}\t{{.Peers}}" |
| 35 | DEFAULT_DEVICE_INSPECT_FORMAT = `ID: {{.Id}} |
| 36 | TYPE: {{.Type}} |
| 37 | ROOT: {{.Root}} |
| 38 | PARENTID: {{.ParentId}} |
| 39 | SERIALNUMBER: {{.SerialNumber}} |
| 40 | VLAN: {{.Vlan}} |
| 41 | ADMINSTATE: {{.AdminState}} |
| 42 | OPERSTATUS: {{.OperStatus}} |
| 43 | CONNECTSTATUS: {{.ConnectStatus}}` |
Rohan Agrawal | 9228d2f | 2020-06-03 07:48:50 +0000 | [diff] [blame] | 44 | DEFAULT_DEVICE_PM_CONFIG_GET_FORMAT = "table{{.DefaultFreq}}\t{{.Grouped}}\t{{.FreqOverride}}" |
| 45 | DEFAULT_DEVICE_PM_CONFIG_METRIC_LIST_FORMAT = "table{{.Name}}\t{{.Type}}\t{{.Enabled}}\t{{.SampleFreq}}" |
| 46 | DEFAULT_DEVICE_PM_CONFIG_GROUP_LIST_FORMAT = "table{{.GroupName}}\t{{.Enabled}}\t{{.GroupFreq}}" |
| 47 | DEFAULT_DEVICE_VALUE_GET_FORMAT = "table{{.Name}}\t{{.Result}}" |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 48 | ) |
| 49 | |
| 50 | type DeviceList struct { |
| 51 | ListOutputOptions |
| 52 | } |
| 53 | |
| 54 | type DeviceCreate struct { |
David Bainbridge | 835dd0e | 2020-04-01 10:30:09 -0700 | [diff] [blame] | 55 | DeviceType string `short:"t" long:"devicetype" default:"" description:"Device type"` |
| 56 | MACAddress string `short:"m" long:"macaddress" default:"" description:"MAC Address"` |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 57 | IPAddress string `short:"i" long:"ipaddress" default:"" description:"IP Address"` |
| 58 | HostAndPort string `short:"H" long:"hostandport" default:"" description:"Host and port"` |
| 59 | } |
| 60 | |
| 61 | type DeviceId string |
| 62 | |
Rohan Agrawal | 9228d2f | 2020-06-03 07:48:50 +0000 | [diff] [blame] | 63 | type MetricName string |
| 64 | type GroupName string |
kesavand | 12cd8eb | 2020-01-20 22:25:22 -0500 | [diff] [blame] | 65 | type PortNum uint32 |
Dinesh Belwalkar | c9aa6d8 | 2020-03-04 15:22:17 -0800 | [diff] [blame] | 66 | type ValueFlag string |
kesavand | 12cd8eb | 2020-01-20 22:25:22 -0500 | [diff] [blame] | 67 | |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 68 | type DeviceDelete struct { |
| 69 | Args struct { |
| 70 | Ids []DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"` |
| 71 | } `positional-args:"yes"` |
| 72 | } |
| 73 | |
| 74 | type DeviceEnable struct { |
| 75 | Args struct { |
| 76 | Ids []DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"` |
| 77 | } `positional-args:"yes"` |
| 78 | } |
| 79 | |
| 80 | type DeviceDisable struct { |
| 81 | Args struct { |
| 82 | Ids []DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"` |
| 83 | } `positional-args:"yes"` |
| 84 | } |
| 85 | |
| 86 | type DeviceReboot struct { |
| 87 | Args struct { |
| 88 | Ids []DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"` |
| 89 | } `positional-args:"yes"` |
| 90 | } |
| 91 | |
| 92 | type DeviceFlowList struct { |
| 93 | ListOutputOptions |
| 94 | Args struct { |
| 95 | Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"` |
| 96 | } `positional-args:"yes"` |
| 97 | } |
| 98 | |
| 99 | type DevicePortList struct { |
| 100 | ListOutputOptions |
| 101 | Args struct { |
| 102 | Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"` |
| 103 | } `positional-args:"yes"` |
| 104 | } |
| 105 | |
| 106 | type DeviceInspect struct { |
| 107 | OutputOptionsJson |
| 108 | Args struct { |
| 109 | Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"` |
| 110 | } `positional-args:"yes"` |
| 111 | } |
| 112 | |
kesavand | 12cd8eb | 2020-01-20 22:25:22 -0500 | [diff] [blame] | 113 | type DevicePortEnable struct { |
| 114 | Args struct { |
| 115 | Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"` |
| 116 | PortId PortNum `positional-arg-name:"PORT_NUMBER" required:"yes"` |
| 117 | } `positional-args:"yes"` |
| 118 | } |
| 119 | |
| 120 | type DevicePortDisable struct { |
| 121 | Args struct { |
| 122 | Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"` |
| 123 | PortId PortNum `positional-arg-name:"PORT_NUMBER" required:"yes"` |
| 124 | } `positional-args:"yes"` |
| 125 | } |
| 126 | |
Rohan Agrawal | 9228d2f | 2020-06-03 07:48:50 +0000 | [diff] [blame] | 127 | type DevicePmConfigsGet struct { |
| 128 | ListOutputOptions |
| 129 | Args struct { |
| 130 | Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"` |
| 131 | } `positional-args:"yes"` |
| 132 | } |
| 133 | |
| 134 | type DevicePmConfigMetricList struct { |
| 135 | ListOutputOptions |
| 136 | Args struct { |
| 137 | Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"` |
| 138 | } `positional-args:"yes"` |
| 139 | } |
| 140 | |
| 141 | type DevicePmConfigGroupList struct { |
| 142 | ListOutputOptions |
| 143 | Args struct { |
| 144 | Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"` |
| 145 | } `positional-args:"yes"` |
| 146 | } |
| 147 | |
| 148 | type DevicePmConfigGroupMetricList struct { |
| 149 | ListOutputOptions |
| 150 | Args struct { |
| 151 | Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"` |
| 152 | Group GroupName `positional-arg-name:"GROUP_NAME" required:"yes"` |
| 153 | } `positional-args:"yes"` |
| 154 | } |
| 155 | |
| 156 | type DevicePmConfigFrequencySet struct { |
| 157 | OutputOptions |
| 158 | Args struct { |
| 159 | Frequency uint32 `positional-arg-name:"FREQUENCY" required:"yes"` |
| 160 | Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"` |
| 161 | } `positional-args:"yes"` |
| 162 | } |
| 163 | |
| 164 | type DevicePmConfigMetricEnable struct { |
| 165 | Args struct { |
| 166 | Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"` |
| 167 | Metrics []MetricName `positional-arg-name:"METRIC_NAME" required:"yes"` |
| 168 | } `positional-args:"yes"` |
| 169 | } |
| 170 | |
| 171 | type DevicePmConfigMetricDisable struct { |
| 172 | Args struct { |
| 173 | Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"` |
| 174 | Metrics []MetricName `positional-arg-name:"METRIC_NAME" required:"yes"` |
| 175 | } `positional-args:"yes"` |
| 176 | } |
| 177 | |
| 178 | type DevicePmConfigGroupEnable struct { |
| 179 | Args struct { |
| 180 | Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"` |
| 181 | Groups []GroupName `positional-arg-name:"GROUP_NAME" required:"yes"` |
| 182 | } `positional-args:"yes"` |
| 183 | } |
| 184 | |
| 185 | type DevicePmConfigGroupDisable struct { |
| 186 | Args struct { |
| 187 | Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"` |
| 188 | Groups []GroupName `positional-arg-name:"GROUP_NAME" required:"yes"` |
| 189 | } `positional-args:"yes"` |
| 190 | } |
| 191 | |
Dinesh Belwalkar | c9aa6d8 | 2020-03-04 15:22:17 -0800 | [diff] [blame] | 192 | type DeviceGetExtValue struct { |
| 193 | ListOutputOptions |
| 194 | Args struct { |
| 195 | Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"` |
| 196 | Valueflag ValueFlag `positional-arg-name:"VALUE_FLAG" required:"yes"` |
| 197 | } `positional-args:"yes"` |
| 198 | } |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 199 | type DeviceOpts struct { |
| 200 | List DeviceList `command:"list"` |
| 201 | Create DeviceCreate `command:"create"` |
| 202 | Delete DeviceDelete `command:"delete"` |
| 203 | Enable DeviceEnable `command:"enable"` |
| 204 | Disable DeviceDisable `command:"disable"` |
| 205 | Flows DeviceFlowList `command:"flows"` |
kesavand | 12cd8eb | 2020-01-20 22:25:22 -0500 | [diff] [blame] | 206 | Port struct { |
| 207 | List DevicePortList `command:"list"` |
| 208 | Enable DevicePortEnable `command:"enable"` |
| 209 | Disable DevicePortDisable `command:"disable"` |
| 210 | } `command:"port"` |
| 211 | Inspect DeviceInspect `command:"inspect"` |
| 212 | Reboot DeviceReboot `command:"reboot"` |
Dinesh Belwalkar | c9aa6d8 | 2020-03-04 15:22:17 -0800 | [diff] [blame] | 213 | Value struct { |
| 214 | Get DeviceGetExtValue `command:"get"` |
| 215 | } `command:"value"` |
Rohan Agrawal | 9228d2f | 2020-06-03 07:48:50 +0000 | [diff] [blame] | 216 | PmConfig struct { |
| 217 | Get DevicePmConfigsGet `command:"get"` |
| 218 | Frequency struct { |
| 219 | Set DevicePmConfigFrequencySet `command:"set"` |
| 220 | } `command:"frequency"` |
| 221 | Metric struct { |
| 222 | List DevicePmConfigMetricList `command:"list"` |
| 223 | Enable DevicePmConfigMetricEnable `command:"enable"` |
| 224 | Disable DevicePmConfigMetricDisable `command:"disable"` |
| 225 | } `command:"metric"` |
| 226 | Group struct { |
| 227 | List DevicePmConfigGroupList `command:"list"` |
| 228 | Enable DevicePmConfigGroupEnable `command:"enable"` |
| 229 | Disable DevicePmConfigGroupDisable `command:"disable"` |
| 230 | } `command:"group"` |
| 231 | GroupMetric struct { |
| 232 | List DevicePmConfigGroupMetricList `command:"list"` |
| 233 | } `command:"groupmetric"` |
| 234 | } `command:"pmconfig"` |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | var deviceOpts = DeviceOpts{} |
| 238 | |
| 239 | func RegisterDeviceCommands(parser *flags.Parser) { |
David Bainbridge | 12f036f | 2019-10-15 22:09:04 +0000 | [diff] [blame] | 240 | if _, err := parser.AddCommand("device", "device commands", "Commands to query and manipulate VOLTHA devices", &deviceOpts); err != nil { |
David Bainbridge | a672234 | 2019-10-24 23:55:53 +0000 | [diff] [blame] | 241 | Error.Fatalf("Unexpected error while attempting to register device commands : %s", err) |
David Bainbridge | 12f036f | 2019-10-15 22:09:04 +0000 | [diff] [blame] | 242 | } |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 243 | } |
| 244 | |
Rohan Agrawal | 9228d2f | 2020-06-03 07:48:50 +0000 | [diff] [blame] | 245 | func (i *MetricName) Complete(match string) []flags.Completion { |
| 246 | conn, err := NewConnection() |
| 247 | if err != nil { |
| 248 | return nil |
| 249 | } |
| 250 | defer conn.Close() |
| 251 | |
| 252 | client := voltha.NewVolthaServiceClient(conn) |
| 253 | |
| 254 | var deviceId string |
| 255 | found: |
| 256 | for i := len(os.Args) - 1; i >= 0; i -= 1 { |
| 257 | switch os.Args[i] { |
| 258 | case "enable": |
| 259 | fallthrough |
| 260 | case "disable": |
| 261 | if len(os.Args) > i+1 { |
| 262 | deviceId = os.Args[i+1] |
| 263 | } else { |
| 264 | return nil |
| 265 | } |
| 266 | break found |
| 267 | default: |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | if len(deviceId) == 0 { |
| 272 | return nil |
| 273 | } |
| 274 | |
| 275 | ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout) |
| 276 | defer cancel() |
| 277 | |
| 278 | id := voltha.ID{Id: string(deviceId)} |
| 279 | |
| 280 | pmconfigs, err := client.ListDevicePmConfigs(ctx, &id) |
| 281 | |
| 282 | if err != nil { |
| 283 | return nil |
| 284 | } |
| 285 | |
| 286 | list := make([]flags.Completion, 0) |
| 287 | for _, metrics := range pmconfigs.Metrics { |
| 288 | if strings.HasPrefix(metrics.Name, match) { |
| 289 | list = append(list, flags.Completion{Item: metrics.Name}) |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | return list |
| 294 | } |
| 295 | |
| 296 | func (i *GroupName) Complete(match string) []flags.Completion { |
| 297 | conn, err := NewConnection() |
| 298 | if err != nil { |
| 299 | return nil |
| 300 | } |
| 301 | defer conn.Close() |
| 302 | |
| 303 | client := voltha.NewVolthaServiceClient(conn) |
| 304 | |
| 305 | var deviceId string |
| 306 | found: |
| 307 | for i := len(os.Args) - 1; i >= 0; i -= 1 { |
| 308 | switch os.Args[i] { |
| 309 | case "list": |
| 310 | fallthrough |
| 311 | case "enable": |
| 312 | fallthrough |
| 313 | case "disable": |
| 314 | if len(os.Args) > i+1 { |
| 315 | deviceId = os.Args[i+1] |
| 316 | } else { |
| 317 | return nil |
| 318 | } |
| 319 | break found |
| 320 | default: |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | if len(deviceId) == 0 { |
| 325 | return nil |
| 326 | } |
| 327 | |
| 328 | ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout) |
| 329 | defer cancel() |
| 330 | |
| 331 | id := voltha.ID{Id: string(deviceId)} |
| 332 | |
| 333 | pmconfigs, err := client.ListDevicePmConfigs(ctx, &id) |
| 334 | |
| 335 | if err != nil { |
| 336 | return nil |
| 337 | } |
| 338 | |
| 339 | list := make([]flags.Completion, 0) |
| 340 | for _, group := range pmconfigs.Groups { |
| 341 | if strings.HasPrefix(group.GroupName, match) { |
| 342 | list = append(list, flags.Completion{Item: group.GroupName}) |
| 343 | } |
| 344 | } |
| 345 | return list |
| 346 | } |
| 347 | |
kesavand | 12cd8eb | 2020-01-20 22:25:22 -0500 | [diff] [blame] | 348 | func (i *PortNum) Complete(match string) []flags.Completion { |
| 349 | conn, err := NewConnection() |
| 350 | if err != nil { |
| 351 | return nil |
| 352 | } |
| 353 | defer conn.Close() |
| 354 | |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 355 | client := voltha.NewVolthaServiceClient(conn) |
kesavand | 12cd8eb | 2020-01-20 22:25:22 -0500 | [diff] [blame] | 356 | |
| 357 | /* |
| 358 | * The command line args when completing for PortNum will be a DeviceId |
| 359 | * followed by one or more PortNums. So walk the argument list from the |
| 360 | * end and find the first argument that is enable/disable as those are |
| 361 | * the subcommands that come before the positional arguments. It would |
| 362 | * be nice if this package gave us the list of optional arguments |
| 363 | * already parsed. |
| 364 | */ |
| 365 | var deviceId string |
| 366 | found: |
| 367 | for i := len(os.Args) - 1; i >= 0; i -= 1 { |
| 368 | switch os.Args[i] { |
| 369 | case "enable": |
| 370 | fallthrough |
| 371 | case "disable": |
| 372 | if len(os.Args) > i+1 { |
| 373 | deviceId = os.Args[i+1] |
| 374 | } else { |
| 375 | return nil |
| 376 | } |
| 377 | break found |
| 378 | default: |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | if len(deviceId) == 0 { |
| 383 | return nil |
| 384 | } |
| 385 | |
kesavand | 12cd8eb | 2020-01-20 22:25:22 -0500 | [diff] [blame] | 386 | ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout) |
| 387 | defer cancel() |
kesavand | 12cd8eb | 2020-01-20 22:25:22 -0500 | [diff] [blame] | 388 | |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 389 | id := voltha.ID{Id: string(deviceId)} |
kesavand | 12cd8eb | 2020-01-20 22:25:22 -0500 | [diff] [blame] | 390 | |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 391 | ports, err := client.ListDevicePorts(ctx, &id) |
kesavand | 12cd8eb | 2020-01-20 22:25:22 -0500 | [diff] [blame] | 392 | if err != nil { |
| 393 | return nil |
| 394 | } |
| 395 | |
| 396 | list := make([]flags.Completion, 0) |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 397 | for _, item := range ports.Items { |
| 398 | pn := strconv.FormatUint(uint64(item.PortNo), 10) |
kesavand | 12cd8eb | 2020-01-20 22:25:22 -0500 | [diff] [blame] | 399 | if strings.HasPrefix(pn, match) { |
| 400 | list = append(list, flags.Completion{Item: pn}) |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | return list |
| 405 | } |
| 406 | |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 407 | func (i *DeviceId) Complete(match string) []flags.Completion { |
| 408 | conn, err := NewConnection() |
| 409 | if err != nil { |
| 410 | return nil |
| 411 | } |
| 412 | defer conn.Close() |
| 413 | |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 414 | client := voltha.NewVolthaServiceClient(conn) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 415 | |
| 416 | ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout) |
| 417 | defer cancel() |
| 418 | |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 419 | devices, err := client.ListDevices(ctx, &empty.Empty{}) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 420 | if err != nil { |
| 421 | return nil |
| 422 | } |
| 423 | |
| 424 | list := make([]flags.Completion, 0) |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 425 | for _, item := range devices.Items { |
| 426 | if strings.HasPrefix(item.Id, match) { |
| 427 | list = append(list, flags.Completion{Item: item.Id}) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 428 | } |
| 429 | } |
| 430 | |
| 431 | return list |
| 432 | } |
| 433 | |
| 434 | func (options *DeviceList) Execute(args []string) error { |
| 435 | |
| 436 | conn, err := NewConnection() |
| 437 | if err != nil { |
| 438 | return err |
| 439 | } |
| 440 | defer conn.Close() |
| 441 | |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 442 | client := voltha.NewVolthaServiceClient(conn) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 443 | |
| 444 | ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout) |
| 445 | defer cancel() |
| 446 | |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 447 | devices, err := client.ListDevices(ctx, &empty.Empty{}) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 448 | if err != nil { |
| 449 | return err |
| 450 | } |
| 451 | |
| 452 | outputFormat := CharReplacer.Replace(options.Format) |
| 453 | if outputFormat == "" { |
David Bainbridge | a672234 | 2019-10-24 23:55:53 +0000 | [diff] [blame] | 454 | outputFormat = GetCommandOptionWithDefault("device-list", "format", DEFAULT_DEVICE_FORMAT) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 455 | } |
| 456 | if options.Quiet { |
| 457 | outputFormat = "{{.Id}}" |
| 458 | } |
| 459 | |
David Bainbridge | a672234 | 2019-10-24 23:55:53 +0000 | [diff] [blame] | 460 | orderBy := options.OrderBy |
| 461 | if orderBy == "" { |
| 462 | orderBy = GetCommandOptionWithDefault("device-list", "order", "") |
| 463 | } |
| 464 | |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 465 | // Make sure json output prints an empty list, not "null" |
| 466 | if devices.Items == nil { |
| 467 | devices.Items = make([]*voltha.Device, 0) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | result := CommandResult{ |
| 471 | Format: format.Format(outputFormat), |
| 472 | Filter: options.Filter, |
David Bainbridge | a672234 | 2019-10-24 23:55:53 +0000 | [diff] [blame] | 473 | OrderBy: orderBy, |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 474 | OutputAs: toOutputType(options.OutputAs), |
| 475 | NameLimit: options.NameLimit, |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 476 | Data: devices.Items, |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | GenerateOutput(&result) |
| 480 | return nil |
| 481 | } |
| 482 | |
| 483 | func (options *DeviceCreate) Execute(args []string) error { |
| 484 | |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 485 | device := voltha.Device{} |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 486 | if options.HostAndPort != "" { |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 487 | device.Address = &voltha.Device_HostAndPort{HostAndPort: options.HostAndPort} |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 488 | } else if options.IPAddress != "" { |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 489 | device.Address = &voltha.Device_Ipv4Address{Ipv4Address: options.IPAddress} |
Hardik Windlass | ce1de34 | 2020-02-04 21:58:07 +0000 | [diff] [blame] | 490 | } |
| 491 | if options.MACAddress != "" { |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 492 | device.MacAddress = strings.ToLower(options.MACAddress) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 493 | } |
| 494 | if options.DeviceType != "" { |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 495 | device.Type = options.DeviceType |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | conn, err := NewConnection() |
| 499 | if err != nil { |
| 500 | return err |
| 501 | } |
| 502 | defer conn.Close() |
| 503 | |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 504 | client := voltha.NewVolthaServiceClient(conn) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 505 | |
| 506 | ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout) |
| 507 | defer cancel() |
| 508 | |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 509 | createdDevice, err := client.CreateDevice(ctx, &device) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 510 | if err != nil { |
| 511 | return err |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 512 | } |
| 513 | |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 514 | fmt.Printf("%s\n", createdDevice.Id) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 515 | |
| 516 | return nil |
| 517 | } |
| 518 | |
| 519 | func (options *DeviceDelete) Execute(args []string) error { |
| 520 | |
| 521 | conn, err := NewConnection() |
| 522 | if err != nil { |
| 523 | return err |
| 524 | } |
| 525 | defer conn.Close() |
| 526 | |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 527 | client := voltha.NewVolthaServiceClient(conn) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 528 | |
David Bainbridge | 7052fe8 | 2020-03-25 10:37:00 -0700 | [diff] [blame] | 529 | var lastErr error |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 530 | for _, i := range options.Args.Ids { |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 531 | ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout) |
| 532 | defer cancel() |
| 533 | |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 534 | id := voltha.ID{Id: string(i)} |
| 535 | |
| 536 | _, err := client.DeleteDevice(ctx, &id) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 537 | if err != nil { |
David Bainbridge | 0f758d4 | 2019-10-26 05:17:48 +0000 | [diff] [blame] | 538 | Error.Printf("Error while deleting '%s': %s\n", i, err) |
David Bainbridge | 7052fe8 | 2020-03-25 10:37:00 -0700 | [diff] [blame] | 539 | lastErr = err |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 540 | continue |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 541 | } |
| 542 | fmt.Printf("%s\n", i) |
| 543 | } |
| 544 | |
David Bainbridge | 7052fe8 | 2020-03-25 10:37:00 -0700 | [diff] [blame] | 545 | if lastErr != nil { |
| 546 | return NoReportErr |
| 547 | } |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 548 | return nil |
| 549 | } |
| 550 | |
| 551 | func (options *DeviceEnable) Execute(args []string) error { |
| 552 | conn, err := NewConnection() |
| 553 | if err != nil { |
| 554 | return err |
| 555 | } |
| 556 | defer conn.Close() |
| 557 | |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 558 | client := voltha.NewVolthaServiceClient(conn) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 559 | |
David Bainbridge | 7052fe8 | 2020-03-25 10:37:00 -0700 | [diff] [blame] | 560 | var lastErr error |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 561 | for _, i := range options.Args.Ids { |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 562 | ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout) |
| 563 | defer cancel() |
| 564 | |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 565 | id := voltha.ID{Id: string(i)} |
| 566 | |
| 567 | _, err := client.EnableDevice(ctx, &id) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 568 | if err != nil { |
David Bainbridge | 0f758d4 | 2019-10-26 05:17:48 +0000 | [diff] [blame] | 569 | Error.Printf("Error while enabling '%s': %s\n", i, err) |
David Bainbridge | 7052fe8 | 2020-03-25 10:37:00 -0700 | [diff] [blame] | 570 | lastErr = err |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 571 | continue |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 572 | } |
| 573 | fmt.Printf("%s\n", i) |
| 574 | } |
| 575 | |
David Bainbridge | 7052fe8 | 2020-03-25 10:37:00 -0700 | [diff] [blame] | 576 | if lastErr != nil { |
| 577 | return NoReportErr |
| 578 | } |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 579 | return nil |
| 580 | } |
| 581 | |
| 582 | func (options *DeviceDisable) Execute(args []string) error { |
| 583 | conn, err := NewConnection() |
| 584 | if err != nil { |
| 585 | return err |
| 586 | } |
| 587 | defer conn.Close() |
| 588 | |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 589 | client := voltha.NewVolthaServiceClient(conn) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 590 | |
David Bainbridge | 7052fe8 | 2020-03-25 10:37:00 -0700 | [diff] [blame] | 591 | var lastErr error |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 592 | for _, i := range options.Args.Ids { |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 593 | ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout) |
| 594 | defer cancel() |
| 595 | |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 596 | id := voltha.ID{Id: string(i)} |
| 597 | |
| 598 | _, err := client.DisableDevice(ctx, &id) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 599 | if err != nil { |
David Bainbridge | 0f758d4 | 2019-10-26 05:17:48 +0000 | [diff] [blame] | 600 | Error.Printf("Error while disabling '%s': %s\n", i, err) |
David Bainbridge | 7052fe8 | 2020-03-25 10:37:00 -0700 | [diff] [blame] | 601 | lastErr = err |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 602 | continue |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 603 | } |
| 604 | fmt.Printf("%s\n", i) |
| 605 | } |
| 606 | |
David Bainbridge | 7052fe8 | 2020-03-25 10:37:00 -0700 | [diff] [blame] | 607 | if lastErr != nil { |
| 608 | return NoReportErr |
| 609 | } |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 610 | return nil |
| 611 | } |
| 612 | |
| 613 | func (options *DeviceReboot) Execute(args []string) error { |
| 614 | conn, err := NewConnection() |
| 615 | if err != nil { |
| 616 | return err |
| 617 | } |
| 618 | defer conn.Close() |
| 619 | |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 620 | client := voltha.NewVolthaServiceClient(conn) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 621 | |
David Bainbridge | 7052fe8 | 2020-03-25 10:37:00 -0700 | [diff] [blame] | 622 | var lastErr error |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 623 | for _, i := range options.Args.Ids { |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 624 | ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout) |
| 625 | defer cancel() |
| 626 | |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 627 | id := voltha.ID{Id: string(i)} |
| 628 | |
| 629 | _, err := client.RebootDevice(ctx, &id) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 630 | if err != nil { |
David Bainbridge | 0f758d4 | 2019-10-26 05:17:48 +0000 | [diff] [blame] | 631 | Error.Printf("Error while rebooting '%s': %s\n", i, err) |
David Bainbridge | 7052fe8 | 2020-03-25 10:37:00 -0700 | [diff] [blame] | 632 | lastErr = err |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 633 | continue |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 634 | } |
| 635 | fmt.Printf("%s\n", i) |
| 636 | } |
| 637 | |
David Bainbridge | 7052fe8 | 2020-03-25 10:37:00 -0700 | [diff] [blame] | 638 | if lastErr != nil { |
| 639 | return NoReportErr |
| 640 | } |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 641 | return nil |
| 642 | } |
| 643 | |
| 644 | func (options *DevicePortList) Execute(args []string) error { |
| 645 | |
| 646 | conn, err := NewConnection() |
| 647 | if err != nil { |
| 648 | return err |
| 649 | } |
| 650 | defer conn.Close() |
| 651 | |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 652 | client := voltha.NewVolthaServiceClient(conn) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 653 | |
| 654 | ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout) |
| 655 | defer cancel() |
| 656 | |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 657 | id := voltha.ID{Id: string(options.Args.Id)} |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 658 | |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 659 | ports, err := client.ListDevicePorts(ctx, &id) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 660 | if err != nil { |
| 661 | return err |
| 662 | } |
| 663 | |
| 664 | outputFormat := CharReplacer.Replace(options.Format) |
| 665 | if outputFormat == "" { |
David Bainbridge | a672234 | 2019-10-24 23:55:53 +0000 | [diff] [blame] | 666 | outputFormat = GetCommandOptionWithDefault("device-ports", "format", DEFAULT_DEVICE_PORTS_FORMAT) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 667 | } |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 668 | |
David Bainbridge | a672234 | 2019-10-24 23:55:53 +0000 | [diff] [blame] | 669 | orderBy := options.OrderBy |
| 670 | if orderBy == "" { |
| 671 | orderBy = GetCommandOptionWithDefault("device-ports", "order", "") |
| 672 | } |
| 673 | |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 674 | result := CommandResult{ |
| 675 | Format: format.Format(outputFormat), |
| 676 | Filter: options.Filter, |
David Bainbridge | a672234 | 2019-10-24 23:55:53 +0000 | [diff] [blame] | 677 | OrderBy: orderBy, |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 678 | OutputAs: toOutputType(options.OutputAs), |
| 679 | NameLimit: options.NameLimit, |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 680 | Data: ports.Items, |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 681 | } |
| 682 | |
| 683 | GenerateOutput(&result) |
| 684 | return nil |
| 685 | } |
| 686 | |
| 687 | func (options *DeviceFlowList) Execute(args []string) error { |
| 688 | fl := &FlowList{} |
| 689 | fl.ListOutputOptions = options.ListOutputOptions |
| 690 | fl.Args.Id = string(options.Args.Id) |
David Bainbridge | a672234 | 2019-10-24 23:55:53 +0000 | [diff] [blame] | 691 | fl.Method = "device-flows" |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 692 | return fl.Execute(args) |
| 693 | } |
| 694 | |
| 695 | func (options *DeviceInspect) Execute(args []string) error { |
| 696 | if len(args) > 0 { |
| 697 | return fmt.Errorf("only a single argument 'DEVICE_ID' can be provided") |
| 698 | } |
| 699 | |
| 700 | conn, err := NewConnection() |
| 701 | if err != nil { |
| 702 | return err |
| 703 | } |
| 704 | defer conn.Close() |
| 705 | |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 706 | client := voltha.NewVolthaServiceClient(conn) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 707 | |
| 708 | ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout) |
| 709 | defer cancel() |
| 710 | |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 711 | id := voltha.ID{Id: string(options.Args.Id)} |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 712 | |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 713 | device, err := client.GetDevice(ctx, &id) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 714 | if err != nil { |
| 715 | return err |
| 716 | } |
| 717 | |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 718 | outputFormat := CharReplacer.Replace(options.Format) |
| 719 | if outputFormat == "" { |
David Bainbridge | a672234 | 2019-10-24 23:55:53 +0000 | [diff] [blame] | 720 | outputFormat = GetCommandOptionWithDefault("device-inspect", "format", DEFAULT_DEVICE_INSPECT_FORMAT) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 721 | } |
| 722 | if options.Quiet { |
| 723 | outputFormat = "{{.Id}}" |
| 724 | } |
| 725 | |
| 726 | result := CommandResult{ |
| 727 | Format: format.Format(outputFormat), |
| 728 | OutputAs: toOutputType(options.OutputAs), |
| 729 | NameLimit: options.NameLimit, |
| 730 | Data: device, |
| 731 | } |
| 732 | GenerateOutput(&result) |
| 733 | return nil |
| 734 | } |
kesavand | 12cd8eb | 2020-01-20 22:25:22 -0500 | [diff] [blame] | 735 | |
| 736 | /*Device Port Enable */ |
| 737 | func (options *DevicePortEnable) Execute(args []string) error { |
| 738 | conn, err := NewConnection() |
| 739 | if err != nil { |
| 740 | return err |
| 741 | } |
| 742 | defer conn.Close() |
| 743 | |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 744 | client := voltha.NewVolthaServiceClient(conn) |
kesavand | 12cd8eb | 2020-01-20 22:25:22 -0500 | [diff] [blame] | 745 | |
kesavand | 12cd8eb | 2020-01-20 22:25:22 -0500 | [diff] [blame] | 746 | ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout) |
| 747 | defer cancel() |
| 748 | |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 749 | port := voltha.Port{DeviceId: string(options.Args.Id), PortNo: uint32(options.Args.PortId)} |
| 750 | |
| 751 | _, err = client.EnablePort(ctx, &port) |
kesavand | 12cd8eb | 2020-01-20 22:25:22 -0500 | [diff] [blame] | 752 | if err != nil { |
| 753 | Error.Printf("Error enabling port number %v on device Id %s,err=%s\n", options.Args.PortId, options.Args.Id, ErrorToString(err)) |
| 754 | return err |
kesavand | 12cd8eb | 2020-01-20 22:25:22 -0500 | [diff] [blame] | 755 | } |
| 756 | |
| 757 | return nil |
| 758 | } |
| 759 | |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 760 | /*Device Port Disable */ |
kesavand | 12cd8eb | 2020-01-20 22:25:22 -0500 | [diff] [blame] | 761 | func (options *DevicePortDisable) Execute(args []string) error { |
| 762 | conn, err := NewConnection() |
| 763 | if err != nil { |
| 764 | return err |
| 765 | } |
| 766 | defer conn.Close() |
| 767 | |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 768 | client := voltha.NewVolthaServiceClient(conn) |
| 769 | |
kesavand | 12cd8eb | 2020-01-20 22:25:22 -0500 | [diff] [blame] | 770 | ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout) |
| 771 | defer cancel() |
| 772 | |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 773 | port := voltha.Port{DeviceId: string(options.Args.Id), PortNo: uint32(options.Args.PortId)} |
| 774 | |
| 775 | _, err = client.DisablePort(ctx, &port) |
kesavand | 12cd8eb | 2020-01-20 22:25:22 -0500 | [diff] [blame] | 776 | if err != nil { |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 777 | Error.Printf("Error enabling port number %v on device Id %s,err=%s\n", options.Args.PortId, options.Args.Id, ErrorToString(err)) |
kesavand | 12cd8eb | 2020-01-20 22:25:22 -0500 | [diff] [blame] | 778 | return err |
kesavand | 12cd8eb | 2020-01-20 22:25:22 -0500 | [diff] [blame] | 779 | } |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 780 | |
kesavand | 12cd8eb | 2020-01-20 22:25:22 -0500 | [diff] [blame] | 781 | return nil |
| 782 | } |
Dinesh Belwalkar | c9aa6d8 | 2020-03-04 15:22:17 -0800 | [diff] [blame] | 783 | |
Rohan Agrawal | 9228d2f | 2020-06-03 07:48:50 +0000 | [diff] [blame] | 784 | func (options *DevicePmConfigsGet) Execute(args []string) error { |
| 785 | |
| 786 | conn, err := NewConnection() |
| 787 | if err != nil { |
| 788 | return err |
| 789 | } |
| 790 | defer conn.Close() |
| 791 | |
| 792 | client := voltha.NewVolthaServiceClient(conn) |
| 793 | |
| 794 | ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout) |
| 795 | defer cancel() |
| 796 | |
| 797 | id := voltha.ID{Id: string(options.Args.Id)} |
| 798 | |
| 799 | pmConfigs, err := client.ListDevicePmConfigs(ctx, &id) |
| 800 | if err != nil { |
| 801 | return err |
| 802 | } |
| 803 | |
| 804 | outputFormat := CharReplacer.Replace(options.Format) |
| 805 | if outputFormat == "" { |
| 806 | outputFormat = GetCommandOptionWithDefault("device-pm-configs", "format", DEFAULT_DEVICE_PM_CONFIG_GET_FORMAT) |
| 807 | } |
| 808 | |
| 809 | orderBy := options.OrderBy |
| 810 | if orderBy == "" { |
| 811 | orderBy = GetCommandOptionWithDefault("device-pm-configs", "order", "") |
| 812 | } |
| 813 | |
| 814 | result := CommandResult{ |
| 815 | Format: format.Format(outputFormat), |
| 816 | Filter: options.Filter, |
| 817 | OrderBy: orderBy, |
| 818 | OutputAs: toOutputType(options.OutputAs), |
| 819 | NameLimit: options.NameLimit, |
| 820 | Data: pmConfigs, |
| 821 | } |
| 822 | |
| 823 | GenerateOutput(&result) |
| 824 | return nil |
| 825 | |
| 826 | } |
| 827 | |
| 828 | func (options *DevicePmConfigMetricList) Execute(args []string) error { |
| 829 | |
| 830 | conn, err := NewConnection() |
| 831 | if err != nil { |
| 832 | return err |
| 833 | } |
| 834 | defer conn.Close() |
| 835 | |
| 836 | client := voltha.NewVolthaServiceClient(conn) |
| 837 | |
| 838 | ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout) |
| 839 | defer cancel() |
| 840 | |
| 841 | id := voltha.ID{Id: string(options.Args.Id)} |
| 842 | |
| 843 | pmConfigs, err := client.ListDevicePmConfigs(ctx, &id) |
| 844 | if err != nil { |
| 845 | return err |
| 846 | } |
| 847 | |
| 848 | if !pmConfigs.Grouped { |
| 849 | for _, metric := range pmConfigs.Metrics { |
| 850 | if metric.SampleFreq == 0 { |
| 851 | metric.SampleFreq = pmConfigs.DefaultFreq |
| 852 | } |
| 853 | } |
| 854 | |
| 855 | } |
| 856 | |
| 857 | outputFormat := CharReplacer.Replace(options.Format) |
| 858 | if outputFormat == "" { |
| 859 | outputFormat = GetCommandOptionWithDefault("device-pm-configs", "format", DEFAULT_DEVICE_PM_CONFIG_METRIC_LIST_FORMAT) |
| 860 | } |
| 861 | |
| 862 | orderBy := options.OrderBy |
| 863 | if orderBy == "" { |
| 864 | orderBy = GetCommandOptionWithDefault("device-pm-configs", "order", "") |
| 865 | } |
| 866 | |
| 867 | result := CommandResult{ |
| 868 | Format: format.Format(outputFormat), |
| 869 | Filter: options.Filter, |
| 870 | OrderBy: orderBy, |
| 871 | OutputAs: toOutputType(options.OutputAs), |
| 872 | NameLimit: options.NameLimit, |
| 873 | Data: pmConfigs.Metrics, |
| 874 | } |
| 875 | |
| 876 | GenerateOutput(&result) |
| 877 | return nil |
| 878 | |
| 879 | } |
| 880 | |
| 881 | func (options *DevicePmConfigMetricEnable) Execute(args []string) error { |
| 882 | |
| 883 | conn, err := NewConnection() |
| 884 | if err != nil { |
| 885 | return err |
| 886 | } |
| 887 | defer conn.Close() |
| 888 | |
| 889 | client := voltha.NewVolthaServiceClient(conn) |
| 890 | |
| 891 | ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout) |
| 892 | defer cancel() |
| 893 | |
| 894 | id := voltha.ID{Id: string(options.Args.Id)} |
| 895 | |
| 896 | pmConfigs, err := client.ListDevicePmConfigs(ctx, &id) |
| 897 | if err != nil { |
| 898 | return err |
| 899 | } |
| 900 | |
| 901 | if !pmConfigs.Grouped { |
| 902 | for _, metric := range pmConfigs.Metrics { |
| 903 | for _, mName := range options.Args.Metrics { |
| 904 | if string(mName) == metric.Name && !metric.Enabled { |
| 905 | metric.Enabled = true |
| 906 | _, err := client.UpdateDevicePmConfigs(ctx, pmConfigs) |
| 907 | if err != nil { |
| 908 | return err |
| 909 | } |
| 910 | } |
| 911 | } |
| 912 | } |
| 913 | } |
| 914 | return nil |
| 915 | } |
| 916 | |
| 917 | func (options *DevicePmConfigMetricDisable) Execute(args []string) error { |
| 918 | |
| 919 | conn, err := NewConnection() |
| 920 | if err != nil { |
| 921 | return err |
| 922 | } |
| 923 | defer conn.Close() |
| 924 | |
| 925 | client := voltha.NewVolthaServiceClient(conn) |
| 926 | |
| 927 | ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout) |
| 928 | defer cancel() |
| 929 | |
| 930 | id := voltha.ID{Id: string(options.Args.Id)} |
| 931 | |
| 932 | pmConfigs, err := client.ListDevicePmConfigs(ctx, &id) |
| 933 | if err != nil { |
| 934 | return err |
| 935 | } |
| 936 | |
| 937 | if !pmConfigs.Grouped { |
| 938 | for _, metric := range pmConfigs.Metrics { |
| 939 | for _, mName := range options.Args.Metrics { |
| 940 | if string(mName) == metric.Name && metric.Enabled { |
| 941 | metric.Enabled = false |
| 942 | _, err := client.UpdateDevicePmConfigs(ctx, pmConfigs) |
| 943 | if err != nil { |
| 944 | return err |
| 945 | } |
| 946 | } |
| 947 | } |
| 948 | } |
| 949 | } |
| 950 | return nil |
| 951 | } |
| 952 | |
| 953 | func (options *DevicePmConfigGroupEnable) Execute(args []string) error { |
| 954 | |
| 955 | conn, err := NewConnection() |
| 956 | if err != nil { |
| 957 | return err |
| 958 | } |
| 959 | defer conn.Close() |
| 960 | |
| 961 | client := voltha.NewVolthaServiceClient(conn) |
| 962 | |
| 963 | ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout) |
| 964 | defer cancel() |
| 965 | |
| 966 | id := voltha.ID{Id: string(options.Args.Id)} |
| 967 | |
| 968 | pmConfigs, err := client.ListDevicePmConfigs(ctx, &id) |
| 969 | if err != nil { |
| 970 | return err |
| 971 | } |
| 972 | |
| 973 | if pmConfigs.Grouped { |
| 974 | for _, group := range pmConfigs.Groups { |
| 975 | for _, gName := range options.Args.Groups { |
| 976 | if string(gName) == group.GroupName && !group.Enabled { |
| 977 | group.Enabled = true |
| 978 | _, err := client.UpdateDevicePmConfigs(ctx, pmConfigs) |
| 979 | if err != nil { |
| 980 | return err |
| 981 | } |
| 982 | } |
| 983 | } |
| 984 | } |
| 985 | } |
| 986 | return nil |
| 987 | } |
| 988 | |
| 989 | func (options *DevicePmConfigGroupDisable) Execute(args []string) error { |
| 990 | |
| 991 | conn, err := NewConnection() |
| 992 | if err != nil { |
| 993 | return err |
| 994 | } |
| 995 | defer conn.Close() |
| 996 | |
| 997 | client := voltha.NewVolthaServiceClient(conn) |
| 998 | |
| 999 | ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout) |
| 1000 | defer cancel() |
| 1001 | |
| 1002 | id := voltha.ID{Id: string(options.Args.Id)} |
| 1003 | |
| 1004 | pmConfigs, err := client.ListDevicePmConfigs(ctx, &id) |
| 1005 | if err != nil { |
| 1006 | return err |
| 1007 | } |
| 1008 | |
| 1009 | if pmConfigs.Grouped { |
| 1010 | for _, group := range pmConfigs.Groups { |
| 1011 | for _, gName := range options.Args.Groups { |
| 1012 | if string(gName) == group.GroupName && group.Enabled { |
| 1013 | group.Enabled = false |
| 1014 | _, err := client.UpdateDevicePmConfigs(ctx, pmConfigs) |
| 1015 | if err != nil { |
| 1016 | return err |
| 1017 | } |
| 1018 | } |
| 1019 | } |
| 1020 | } |
| 1021 | } |
| 1022 | return nil |
| 1023 | } |
| 1024 | |
| 1025 | func (options *DevicePmConfigGroupList) Execute(args []string) error { |
| 1026 | |
| 1027 | conn, err := NewConnection() |
| 1028 | if err != nil { |
| 1029 | return err |
| 1030 | } |
| 1031 | defer conn.Close() |
| 1032 | |
| 1033 | client := voltha.NewVolthaServiceClient(conn) |
| 1034 | |
| 1035 | ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout) |
| 1036 | defer cancel() |
| 1037 | |
| 1038 | id := voltha.ID{Id: string(options.Args.Id)} |
| 1039 | |
| 1040 | pmConfigs, err := client.ListDevicePmConfigs(ctx, &id) |
| 1041 | if err != nil { |
| 1042 | return err |
| 1043 | } |
| 1044 | |
| 1045 | if pmConfigs.Grouped { |
| 1046 | for _, group := range pmConfigs.Groups { |
| 1047 | if group.GroupFreq == 0 { |
| 1048 | group.GroupFreq = pmConfigs.DefaultFreq |
| 1049 | } |
| 1050 | } |
| 1051 | |
| 1052 | } |
| 1053 | |
| 1054 | outputFormat := CharReplacer.Replace(options.Format) |
| 1055 | if outputFormat == "" { |
| 1056 | outputFormat = GetCommandOptionWithDefault("device-pm-configs", "format", DEFAULT_DEVICE_PM_CONFIG_GROUP_LIST_FORMAT) |
| 1057 | } |
| 1058 | |
| 1059 | orderBy := options.OrderBy |
| 1060 | if orderBy == "" { |
| 1061 | orderBy = GetCommandOptionWithDefault("device-pm-configs", "order", "") |
| 1062 | } |
| 1063 | |
| 1064 | result := CommandResult{ |
| 1065 | Format: format.Format(outputFormat), |
| 1066 | Filter: options.Filter, |
| 1067 | OrderBy: orderBy, |
| 1068 | OutputAs: toOutputType(options.OutputAs), |
| 1069 | NameLimit: options.NameLimit, |
| 1070 | Data: pmConfigs.Groups, |
| 1071 | } |
| 1072 | |
| 1073 | GenerateOutput(&result) |
| 1074 | return nil |
| 1075 | |
| 1076 | } |
| 1077 | |
| 1078 | func (options *DevicePmConfigGroupMetricList) Execute(args []string) error { |
| 1079 | |
| 1080 | var metrics []*voltha.PmConfig |
| 1081 | conn, err := NewConnection() |
| 1082 | if err != nil { |
| 1083 | return err |
| 1084 | } |
| 1085 | defer conn.Close() |
| 1086 | |
| 1087 | client := voltha.NewVolthaServiceClient(conn) |
| 1088 | |
| 1089 | ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout) |
| 1090 | defer cancel() |
| 1091 | |
| 1092 | id := voltha.ID{Id: string(options.Args.Id)} |
| 1093 | |
| 1094 | pmConfigs, err := client.ListDevicePmConfigs(ctx, &id) |
| 1095 | if err != nil { |
| 1096 | return err |
| 1097 | } |
| 1098 | |
| 1099 | for _, groups := range pmConfigs.Groups { |
| 1100 | |
| 1101 | if string(options.Args.Group) == groups.GroupName { |
| 1102 | for _, metric := range groups.Metrics { |
| 1103 | if metric.SampleFreq == 0 && groups.GroupFreq == 0 { |
| 1104 | metric.SampleFreq = pmConfigs.DefaultFreq |
| 1105 | } else { |
| 1106 | metric.SampleFreq = groups.GroupFreq |
| 1107 | } |
| 1108 | } |
| 1109 | metrics = groups.Metrics |
| 1110 | } |
| 1111 | } |
| 1112 | |
| 1113 | outputFormat := CharReplacer.Replace(options.Format) |
| 1114 | if outputFormat == "" { |
| 1115 | outputFormat = GetCommandOptionWithDefault("device-pm-configs", "format", DEFAULT_DEVICE_PM_CONFIG_METRIC_LIST_FORMAT) |
| 1116 | } |
| 1117 | |
| 1118 | orderBy := options.OrderBy |
| 1119 | if orderBy == "" { |
| 1120 | orderBy = GetCommandOptionWithDefault("device-pm-configs", "order", "") |
| 1121 | } |
| 1122 | |
| 1123 | result := CommandResult{ |
| 1124 | Format: format.Format(outputFormat), |
| 1125 | Filter: options.Filter, |
| 1126 | OrderBy: orderBy, |
| 1127 | OutputAs: toOutputType(options.OutputAs), |
| 1128 | NameLimit: options.NameLimit, |
| 1129 | Data: metrics, |
| 1130 | } |
| 1131 | |
| 1132 | GenerateOutput(&result) |
| 1133 | return nil |
| 1134 | |
| 1135 | } |
| 1136 | |
| 1137 | func (options *DevicePmConfigFrequencySet) Execute(args []string) error { |
| 1138 | |
| 1139 | conn, err := NewConnection() |
| 1140 | if err != nil { |
| 1141 | return err |
| 1142 | } |
| 1143 | defer conn.Close() |
| 1144 | |
| 1145 | client := voltha.NewVolthaServiceClient(conn) |
| 1146 | |
| 1147 | ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout) |
| 1148 | defer cancel() |
| 1149 | |
| 1150 | id := voltha.ID{Id: string(options.Args.Id)} |
| 1151 | |
| 1152 | pmConfigs, err := client.ListDevicePmConfigs(ctx, &id) |
| 1153 | if err != nil { |
| 1154 | return err |
| 1155 | } |
| 1156 | |
| 1157 | pmConfigs.DefaultFreq = options.Args.Frequency |
| 1158 | |
| 1159 | _, err = client.UpdateDevicePmConfigs(ctx, pmConfigs) |
| 1160 | if err != nil { |
| 1161 | return err |
| 1162 | } |
| 1163 | |
| 1164 | outputFormat := CharReplacer.Replace(options.Format) |
| 1165 | if outputFormat == "" { |
| 1166 | outputFormat = GetCommandOptionWithDefault("device-pm-configs", "format", DEFAULT_DEVICE_PM_CONFIG_GET_FORMAT) |
| 1167 | } |
| 1168 | if options.Quiet { |
| 1169 | outputFormat = "{{.Id}}" |
| 1170 | } |
| 1171 | |
| 1172 | result := CommandResult{ |
| 1173 | Format: format.Format(outputFormat), |
| 1174 | OutputAs: toOutputType(options.OutputAs), |
| 1175 | NameLimit: options.NameLimit, |
| 1176 | Data: pmConfigs, |
| 1177 | } |
| 1178 | |
| 1179 | GenerateOutput(&result) |
| 1180 | return nil |
| 1181 | |
| 1182 | } |
| 1183 | |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 1184 | type ReturnValueRow struct { |
| 1185 | Name string `json:"name"` |
| 1186 | Result interface{} `json:"result"` |
| 1187 | } |
| 1188 | |
Dinesh Belwalkar | c9aa6d8 | 2020-03-04 15:22:17 -0800 | [diff] [blame] | 1189 | /*Device get Onu Distance */ |
| 1190 | func (options *DeviceGetExtValue) Execute(args []string) error { |
| 1191 | conn, err := NewConnection() |
| 1192 | if err != nil { |
| 1193 | return err |
| 1194 | } |
| 1195 | defer conn.Close() |
| 1196 | |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 1197 | client := voltha.NewVolthaServiceClient(conn) |
| 1198 | |
| 1199 | valueflag, okay := common.ValueType_Type_value[string(options.Args.Valueflag)] |
| 1200 | if !okay { |
| 1201 | Error.Printf("Unknown valueflag %s\n", options.Args.Valueflag) |
Dinesh Belwalkar | c9aa6d8 | 2020-03-04 15:22:17 -0800 | [diff] [blame] | 1202 | } |
| 1203 | |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 1204 | val := voltha.ValueSpecifier{Id: string(options.Args.Id), Value: common.ValueType_Type(valueflag)} |
Dinesh Belwalkar | c9aa6d8 | 2020-03-04 15:22:17 -0800 | [diff] [blame] | 1205 | |
| 1206 | ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout) |
| 1207 | defer cancel() |
| 1208 | |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 1209 | rv, err := client.GetExtValue(ctx, &val) |
Dinesh Belwalkar | c9aa6d8 | 2020-03-04 15:22:17 -0800 | [diff] [blame] | 1210 | if err != nil { |
| 1211 | Error.Printf("Error getting value on device Id %s,err=%s\n", options.Args.Id, ErrorToString(err)) |
| 1212 | return err |
Dinesh Belwalkar | c9aa6d8 | 2020-03-04 15:22:17 -0800 | [diff] [blame] | 1213 | } |
| 1214 | |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 1215 | var rows []ReturnValueRow |
| 1216 | for name, num := range common.ValueType_Type_value { |
| 1217 | if num == 0 { |
| 1218 | // EMPTY is not a real value |
| 1219 | continue |
| 1220 | } |
| 1221 | if (rv.Error & uint32(num)) != 0 { |
| 1222 | row := ReturnValueRow{Name: name, Result: "Error"} |
| 1223 | rows = append(rows, row) |
| 1224 | } |
| 1225 | if (rv.Unsupported & uint32(num)) != 0 { |
| 1226 | row := ReturnValueRow{Name: name, Result: "Unsupported"} |
| 1227 | rows = append(rows, row) |
| 1228 | } |
| 1229 | if (rv.Set & uint32(num)) != 0 { |
| 1230 | switch name { |
| 1231 | case "DISTANCE": |
| 1232 | row := ReturnValueRow{Name: name, Result: rv.Distance} |
| 1233 | rows = append(rows, row) |
| 1234 | default: |
| 1235 | row := ReturnValueRow{Name: name, Result: "Unimplemented-in-voltctl"} |
| 1236 | rows = append(rows, row) |
| 1237 | } |
| 1238 | } |
Dinesh Belwalkar | c9aa6d8 | 2020-03-04 15:22:17 -0800 | [diff] [blame] | 1239 | } |
| 1240 | |
Dinesh Belwalkar | c9aa6d8 | 2020-03-04 15:22:17 -0800 | [diff] [blame] | 1241 | outputFormat := CharReplacer.Replace(options.Format) |
| 1242 | if outputFormat == "" { |
| 1243 | outputFormat = GetCommandOptionWithDefault("device-value-get", "format", DEFAULT_DEVICE_VALUE_GET_FORMAT) |
| 1244 | } |
| 1245 | |
| 1246 | result := CommandResult{ |
| 1247 | Format: format.Format(outputFormat), |
| 1248 | OutputAs: toOutputType(options.OutputAs), |
| 1249 | NameLimit: options.NameLimit, |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 1250 | Data: rows, |
Dinesh Belwalkar | c9aa6d8 | 2020-03-04 15:22:17 -0800 | [diff] [blame] | 1251 | } |
| 1252 | GenerateOutput(&result) |
| 1253 | return nil |
| 1254 | } |