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 | 1a51439 | 2020-06-23 11:12:51 -0700 | [diff] [blame] | 55 | DeviceType string `short:"t" required:"true" long:"devicetype" description:"Device type"` |
David Bainbridge | 835dd0e | 2020-04-01 10:30:09 -0700 | [diff] [blame] | 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 | } |
Rohan Agrawal | bca6912 | 2020-06-17 14:59:03 +0000 | [diff] [blame] | 854 | outputFormat := CharReplacer.Replace(options.Format) |
| 855 | if outputFormat == "" { |
| 856 | outputFormat = GetCommandOptionWithDefault("device-pm-configs", "format", DEFAULT_DEVICE_PM_CONFIG_METRIC_LIST_FORMAT) |
| 857 | } |
Rohan Agrawal | 9228d2f | 2020-06-03 07:48:50 +0000 | [diff] [blame] | 858 | |
Rohan Agrawal | bca6912 | 2020-06-17 14:59:03 +0000 | [diff] [blame] | 859 | orderBy := options.OrderBy |
| 860 | if orderBy == "" { |
| 861 | orderBy = GetCommandOptionWithDefault("device-pm-configs", "order", "") |
| 862 | } |
| 863 | |
| 864 | result := CommandResult{ |
| 865 | Format: format.Format(outputFormat), |
| 866 | Filter: options.Filter, |
| 867 | OrderBy: orderBy, |
| 868 | OutputAs: toOutputType(options.OutputAs), |
| 869 | NameLimit: options.NameLimit, |
| 870 | Data: pmConfigs.Metrics, |
| 871 | } |
| 872 | |
| 873 | GenerateOutput(&result) |
| 874 | return nil |
| 875 | } else { |
| 876 | return fmt.Errorf("Device '%s' does not have Non Grouped Metrics", options.Args.Id) |
Rohan Agrawal | 9228d2f | 2020-06-03 07:48:50 +0000 | [diff] [blame] | 877 | } |
Rohan Agrawal | 9228d2f | 2020-06-03 07:48:50 +0000 | [diff] [blame] | 878 | } |
| 879 | |
| 880 | func (options *DevicePmConfigMetricEnable) Execute(args []string) error { |
| 881 | |
| 882 | conn, err := NewConnection() |
| 883 | if err != nil { |
| 884 | return err |
| 885 | } |
| 886 | defer conn.Close() |
| 887 | |
| 888 | client := voltha.NewVolthaServiceClient(conn) |
| 889 | |
| 890 | ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout) |
| 891 | defer cancel() |
| 892 | |
| 893 | id := voltha.ID{Id: string(options.Args.Id)} |
| 894 | |
| 895 | pmConfigs, err := client.ListDevicePmConfigs(ctx, &id) |
| 896 | if err != nil { |
| 897 | return err |
| 898 | } |
| 899 | |
| 900 | if !pmConfigs.Grouped { |
Rohan Agrawal | bca6912 | 2020-06-17 14:59:03 +0000 | [diff] [blame] | 901 | metrics := make(map[string]struct{}) |
| 902 | for _, metric := range pmConfigs.Metrics { |
| 903 | metrics[metric.Name] = struct{}{} |
| 904 | } |
| 905 | |
Rohan Agrawal | 9228d2f | 2020-06-03 07:48:50 +0000 | [diff] [blame] | 906 | for _, metric := range pmConfigs.Metrics { |
| 907 | for _, mName := range options.Args.Metrics { |
Rohan Agrawal | bca6912 | 2020-06-17 14:59:03 +0000 | [diff] [blame] | 908 | if _, exist := metrics[string(mName)]; !exist { |
| 909 | return fmt.Errorf("Metric Name '%s' does not exist", mName) |
| 910 | } |
| 911 | |
Rohan Agrawal | 9228d2f | 2020-06-03 07:48:50 +0000 | [diff] [blame] | 912 | if string(mName) == metric.Name && !metric.Enabled { |
| 913 | metric.Enabled = true |
| 914 | _, err := client.UpdateDevicePmConfigs(ctx, pmConfigs) |
| 915 | if err != nil { |
| 916 | return err |
| 917 | } |
| 918 | } |
| 919 | } |
| 920 | } |
Rohan Agrawal | bca6912 | 2020-06-17 14:59:03 +0000 | [diff] [blame] | 921 | } else { |
| 922 | return fmt.Errorf("Device '%s' does not have Non Grouped Metrics", options.Args.Id) |
Rohan Agrawal | 9228d2f | 2020-06-03 07:48:50 +0000 | [diff] [blame] | 923 | } |
| 924 | return nil |
| 925 | } |
| 926 | |
| 927 | func (options *DevicePmConfigMetricDisable) Execute(args []string) error { |
| 928 | |
| 929 | conn, err := NewConnection() |
| 930 | if err != nil { |
| 931 | return err |
| 932 | } |
| 933 | defer conn.Close() |
| 934 | |
| 935 | client := voltha.NewVolthaServiceClient(conn) |
| 936 | |
| 937 | ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout) |
| 938 | defer cancel() |
| 939 | |
| 940 | id := voltha.ID{Id: string(options.Args.Id)} |
| 941 | |
| 942 | pmConfigs, err := client.ListDevicePmConfigs(ctx, &id) |
| 943 | if err != nil { |
| 944 | return err |
| 945 | } |
| 946 | |
| 947 | if !pmConfigs.Grouped { |
Rohan Agrawal | bca6912 | 2020-06-17 14:59:03 +0000 | [diff] [blame] | 948 | metrics := make(map[string]struct{}) |
| 949 | for _, metric := range pmConfigs.Metrics { |
| 950 | metrics[metric.Name] = struct{}{} |
| 951 | } |
| 952 | |
Rohan Agrawal | 9228d2f | 2020-06-03 07:48:50 +0000 | [diff] [blame] | 953 | for _, metric := range pmConfigs.Metrics { |
| 954 | for _, mName := range options.Args.Metrics { |
Rohan Agrawal | bca6912 | 2020-06-17 14:59:03 +0000 | [diff] [blame] | 955 | if _, have := metrics[string(mName)]; !have { |
| 956 | return fmt.Errorf("Metric Name '%s' does not exist", mName) |
| 957 | } |
Rohan Agrawal | 9228d2f | 2020-06-03 07:48:50 +0000 | [diff] [blame] | 958 | if string(mName) == metric.Name && metric.Enabled { |
| 959 | metric.Enabled = false |
| 960 | _, err := client.UpdateDevicePmConfigs(ctx, pmConfigs) |
| 961 | if err != nil { |
| 962 | return err |
| 963 | } |
Rohan Agrawal | bca6912 | 2020-06-17 14:59:03 +0000 | [diff] [blame] | 964 | } else { |
| 965 | return fmt.Errorf("Metric '%s' cannot be disabled", string(mName)) |
Rohan Agrawal | 9228d2f | 2020-06-03 07:48:50 +0000 | [diff] [blame] | 966 | } |
| 967 | } |
| 968 | } |
Rohan Agrawal | bca6912 | 2020-06-17 14:59:03 +0000 | [diff] [blame] | 969 | } else { |
| 970 | return fmt.Errorf("Device '%s' does not have Non Grouped Metrics", options.Args.Id) |
Rohan Agrawal | 9228d2f | 2020-06-03 07:48:50 +0000 | [diff] [blame] | 971 | } |
| 972 | return nil |
| 973 | } |
| 974 | |
| 975 | func (options *DevicePmConfigGroupEnable) Execute(args []string) error { |
| 976 | |
| 977 | conn, err := NewConnection() |
| 978 | if err != nil { |
| 979 | return err |
| 980 | } |
| 981 | defer conn.Close() |
| 982 | |
| 983 | client := voltha.NewVolthaServiceClient(conn) |
| 984 | |
| 985 | ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout) |
| 986 | defer cancel() |
| 987 | |
| 988 | id := voltha.ID{Id: string(options.Args.Id)} |
| 989 | |
| 990 | pmConfigs, err := client.ListDevicePmConfigs(ctx, &id) |
| 991 | if err != nil { |
| 992 | return err |
| 993 | } |
| 994 | |
| 995 | if pmConfigs.Grouped { |
Rohan Agrawal | bca6912 | 2020-06-17 14:59:03 +0000 | [diff] [blame] | 996 | groups := make(map[string]struct{}) |
| 997 | for _, group := range pmConfigs.Groups { |
| 998 | groups[group.GroupName] = struct{}{} |
| 999 | } |
Rohan Agrawal | 9228d2f | 2020-06-03 07:48:50 +0000 | [diff] [blame] | 1000 | for _, group := range pmConfigs.Groups { |
| 1001 | for _, gName := range options.Args.Groups { |
Rohan Agrawal | bca6912 | 2020-06-17 14:59:03 +0000 | [diff] [blame] | 1002 | if _, have := groups[string(gName)]; !have { |
| 1003 | return fmt.Errorf("Group Name '%s' does not exist", gName) |
| 1004 | } |
Rohan Agrawal | 9228d2f | 2020-06-03 07:48:50 +0000 | [diff] [blame] | 1005 | if string(gName) == group.GroupName && !group.Enabled { |
| 1006 | group.Enabled = true |
| 1007 | _, err := client.UpdateDevicePmConfigs(ctx, pmConfigs) |
| 1008 | if err != nil { |
| 1009 | return err |
| 1010 | } |
| 1011 | } |
| 1012 | } |
| 1013 | } |
Rohan Agrawal | bca6912 | 2020-06-17 14:59:03 +0000 | [diff] [blame] | 1014 | } else { |
| 1015 | return fmt.Errorf("Device '%s' does not have Group Metrics", options.Args.Id) |
Rohan Agrawal | 9228d2f | 2020-06-03 07:48:50 +0000 | [diff] [blame] | 1016 | } |
| 1017 | return nil |
| 1018 | } |
| 1019 | |
| 1020 | func (options *DevicePmConfigGroupDisable) Execute(args []string) error { |
| 1021 | |
| 1022 | conn, err := NewConnection() |
| 1023 | if err != nil { |
| 1024 | return err |
| 1025 | } |
| 1026 | defer conn.Close() |
| 1027 | |
| 1028 | client := voltha.NewVolthaServiceClient(conn) |
| 1029 | |
| 1030 | ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout) |
| 1031 | defer cancel() |
| 1032 | |
| 1033 | id := voltha.ID{Id: string(options.Args.Id)} |
| 1034 | |
| 1035 | pmConfigs, err := client.ListDevicePmConfigs(ctx, &id) |
| 1036 | if err != nil { |
| 1037 | return err |
| 1038 | } |
| 1039 | |
| 1040 | if pmConfigs.Grouped { |
Rohan Agrawal | bca6912 | 2020-06-17 14:59:03 +0000 | [diff] [blame] | 1041 | groups := make(map[string]struct{}) |
| 1042 | for _, group := range pmConfigs.Groups { |
| 1043 | groups[group.GroupName] = struct{}{} |
| 1044 | } |
| 1045 | |
Rohan Agrawal | 9228d2f | 2020-06-03 07:48:50 +0000 | [diff] [blame] | 1046 | for _, group := range pmConfigs.Groups { |
| 1047 | for _, gName := range options.Args.Groups { |
Rohan Agrawal | bca6912 | 2020-06-17 14:59:03 +0000 | [diff] [blame] | 1048 | if _, have := groups[string(gName)]; !have { |
| 1049 | return fmt.Errorf("Group Name '%s' does not exist", gName) |
| 1050 | } |
| 1051 | |
Rohan Agrawal | 9228d2f | 2020-06-03 07:48:50 +0000 | [diff] [blame] | 1052 | if string(gName) == group.GroupName && group.Enabled { |
| 1053 | group.Enabled = false |
| 1054 | _, err := client.UpdateDevicePmConfigs(ctx, pmConfigs) |
| 1055 | if err != nil { |
| 1056 | return err |
| 1057 | } |
| 1058 | } |
| 1059 | } |
| 1060 | } |
Rohan Agrawal | bca6912 | 2020-06-17 14:59:03 +0000 | [diff] [blame] | 1061 | } else { |
| 1062 | return fmt.Errorf("Device '%s' does not have Group Metrics", options.Args.Id) |
Rohan Agrawal | 9228d2f | 2020-06-03 07:48:50 +0000 | [diff] [blame] | 1063 | } |
| 1064 | return nil |
| 1065 | } |
| 1066 | |
| 1067 | func (options *DevicePmConfigGroupList) Execute(args []string) error { |
| 1068 | |
| 1069 | conn, err := NewConnection() |
| 1070 | if err != nil { |
| 1071 | return err |
| 1072 | } |
| 1073 | defer conn.Close() |
| 1074 | |
| 1075 | client := voltha.NewVolthaServiceClient(conn) |
| 1076 | |
| 1077 | ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout) |
| 1078 | defer cancel() |
| 1079 | |
| 1080 | id := voltha.ID{Id: string(options.Args.Id)} |
| 1081 | |
| 1082 | pmConfigs, err := client.ListDevicePmConfigs(ctx, &id) |
| 1083 | if err != nil { |
| 1084 | return err |
| 1085 | } |
| 1086 | |
| 1087 | if pmConfigs.Grouped { |
| 1088 | for _, group := range pmConfigs.Groups { |
| 1089 | if group.GroupFreq == 0 { |
| 1090 | group.GroupFreq = pmConfigs.DefaultFreq |
| 1091 | } |
| 1092 | } |
Rohan Agrawal | bca6912 | 2020-06-17 14:59:03 +0000 | [diff] [blame] | 1093 | outputFormat := CharReplacer.Replace(options.Format) |
| 1094 | if outputFormat == "" { |
| 1095 | outputFormat = GetCommandOptionWithDefault("device-pm-configs", "format", DEFAULT_DEVICE_PM_CONFIG_GROUP_LIST_FORMAT) |
| 1096 | } |
Rohan Agrawal | 9228d2f | 2020-06-03 07:48:50 +0000 | [diff] [blame] | 1097 | |
Rohan Agrawal | bca6912 | 2020-06-17 14:59:03 +0000 | [diff] [blame] | 1098 | orderBy := options.OrderBy |
| 1099 | if orderBy == "" { |
| 1100 | orderBy = GetCommandOptionWithDefault("device-pm-configs", "order", "") |
| 1101 | } |
| 1102 | |
| 1103 | result := CommandResult{ |
| 1104 | Format: format.Format(outputFormat), |
| 1105 | Filter: options.Filter, |
| 1106 | OrderBy: orderBy, |
| 1107 | OutputAs: toOutputType(options.OutputAs), |
| 1108 | NameLimit: options.NameLimit, |
| 1109 | Data: pmConfigs.Groups, |
| 1110 | } |
| 1111 | |
| 1112 | GenerateOutput(&result) |
| 1113 | } else { |
| 1114 | return fmt.Errorf("Device '%s' does not have Group Metrics", string(options.Args.Id)) |
Rohan Agrawal | 9228d2f | 2020-06-03 07:48:50 +0000 | [diff] [blame] | 1115 | } |
Rohan Agrawal | 9228d2f | 2020-06-03 07:48:50 +0000 | [diff] [blame] | 1116 | return nil |
Rohan Agrawal | 9228d2f | 2020-06-03 07:48:50 +0000 | [diff] [blame] | 1117 | } |
| 1118 | |
| 1119 | func (options *DevicePmConfigGroupMetricList) Execute(args []string) error { |
| 1120 | |
| 1121 | var metrics []*voltha.PmConfig |
| 1122 | conn, err := NewConnection() |
| 1123 | if err != nil { |
| 1124 | return err |
| 1125 | } |
| 1126 | defer conn.Close() |
| 1127 | |
| 1128 | client := voltha.NewVolthaServiceClient(conn) |
| 1129 | |
| 1130 | ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout) |
| 1131 | defer cancel() |
| 1132 | |
| 1133 | id := voltha.ID{Id: string(options.Args.Id)} |
| 1134 | |
| 1135 | pmConfigs, err := client.ListDevicePmConfigs(ctx, &id) |
| 1136 | if err != nil { |
| 1137 | return err |
| 1138 | } |
| 1139 | |
| 1140 | for _, groups := range pmConfigs.Groups { |
| 1141 | |
| 1142 | if string(options.Args.Group) == groups.GroupName { |
| 1143 | for _, metric := range groups.Metrics { |
| 1144 | if metric.SampleFreq == 0 && groups.GroupFreq == 0 { |
| 1145 | metric.SampleFreq = pmConfigs.DefaultFreq |
| 1146 | } else { |
| 1147 | metric.SampleFreq = groups.GroupFreq |
| 1148 | } |
| 1149 | } |
| 1150 | metrics = groups.Metrics |
| 1151 | } |
| 1152 | } |
| 1153 | |
| 1154 | outputFormat := CharReplacer.Replace(options.Format) |
| 1155 | if outputFormat == "" { |
| 1156 | outputFormat = GetCommandOptionWithDefault("device-pm-configs", "format", DEFAULT_DEVICE_PM_CONFIG_METRIC_LIST_FORMAT) |
| 1157 | } |
| 1158 | |
| 1159 | orderBy := options.OrderBy |
| 1160 | if orderBy == "" { |
| 1161 | orderBy = GetCommandOptionWithDefault("device-pm-configs", "order", "") |
| 1162 | } |
| 1163 | |
| 1164 | result := CommandResult{ |
| 1165 | Format: format.Format(outputFormat), |
| 1166 | Filter: options.Filter, |
| 1167 | OrderBy: orderBy, |
| 1168 | OutputAs: toOutputType(options.OutputAs), |
| 1169 | NameLimit: options.NameLimit, |
| 1170 | Data: metrics, |
| 1171 | } |
| 1172 | |
| 1173 | GenerateOutput(&result) |
| 1174 | return nil |
| 1175 | |
| 1176 | } |
| 1177 | |
| 1178 | func (options *DevicePmConfigFrequencySet) Execute(args []string) error { |
| 1179 | |
| 1180 | conn, err := NewConnection() |
| 1181 | if err != nil { |
| 1182 | return err |
| 1183 | } |
| 1184 | defer conn.Close() |
| 1185 | |
| 1186 | client := voltha.NewVolthaServiceClient(conn) |
| 1187 | |
| 1188 | ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout) |
| 1189 | defer cancel() |
| 1190 | |
| 1191 | id := voltha.ID{Id: string(options.Args.Id)} |
| 1192 | |
| 1193 | pmConfigs, err := client.ListDevicePmConfigs(ctx, &id) |
| 1194 | if err != nil { |
| 1195 | return err |
| 1196 | } |
| 1197 | |
| 1198 | pmConfigs.DefaultFreq = options.Args.Frequency |
| 1199 | |
| 1200 | _, err = client.UpdateDevicePmConfigs(ctx, pmConfigs) |
| 1201 | if err != nil { |
| 1202 | return err |
| 1203 | } |
| 1204 | |
| 1205 | outputFormat := CharReplacer.Replace(options.Format) |
| 1206 | if outputFormat == "" { |
| 1207 | outputFormat = GetCommandOptionWithDefault("device-pm-configs", "format", DEFAULT_DEVICE_PM_CONFIG_GET_FORMAT) |
| 1208 | } |
| 1209 | if options.Quiet { |
| 1210 | outputFormat = "{{.Id}}" |
| 1211 | } |
| 1212 | |
| 1213 | result := CommandResult{ |
| 1214 | Format: format.Format(outputFormat), |
| 1215 | OutputAs: toOutputType(options.OutputAs), |
| 1216 | NameLimit: options.NameLimit, |
| 1217 | Data: pmConfigs, |
| 1218 | } |
| 1219 | |
| 1220 | GenerateOutput(&result) |
| 1221 | return nil |
| 1222 | |
| 1223 | } |
| 1224 | |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 1225 | type ReturnValueRow struct { |
| 1226 | Name string `json:"name"` |
| 1227 | Result interface{} `json:"result"` |
| 1228 | } |
| 1229 | |
Dinesh Belwalkar | c9aa6d8 | 2020-03-04 15:22:17 -0800 | [diff] [blame] | 1230 | /*Device get Onu Distance */ |
| 1231 | func (options *DeviceGetExtValue) Execute(args []string) error { |
| 1232 | conn, err := NewConnection() |
| 1233 | if err != nil { |
| 1234 | return err |
| 1235 | } |
| 1236 | defer conn.Close() |
| 1237 | |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 1238 | client := voltha.NewVolthaServiceClient(conn) |
| 1239 | |
| 1240 | valueflag, okay := common.ValueType_Type_value[string(options.Args.Valueflag)] |
| 1241 | if !okay { |
| 1242 | Error.Printf("Unknown valueflag %s\n", options.Args.Valueflag) |
Dinesh Belwalkar | c9aa6d8 | 2020-03-04 15:22:17 -0800 | [diff] [blame] | 1243 | } |
| 1244 | |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 1245 | 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] | 1246 | |
| 1247 | ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout) |
| 1248 | defer cancel() |
| 1249 | |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 1250 | rv, err := client.GetExtValue(ctx, &val) |
Dinesh Belwalkar | c9aa6d8 | 2020-03-04 15:22:17 -0800 | [diff] [blame] | 1251 | if err != nil { |
| 1252 | Error.Printf("Error getting value on device Id %s,err=%s\n", options.Args.Id, ErrorToString(err)) |
| 1253 | return err |
Dinesh Belwalkar | c9aa6d8 | 2020-03-04 15:22:17 -0800 | [diff] [blame] | 1254 | } |
| 1255 | |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 1256 | var rows []ReturnValueRow |
| 1257 | for name, num := range common.ValueType_Type_value { |
| 1258 | if num == 0 { |
| 1259 | // EMPTY is not a real value |
| 1260 | continue |
| 1261 | } |
| 1262 | if (rv.Error & uint32(num)) != 0 { |
| 1263 | row := ReturnValueRow{Name: name, Result: "Error"} |
| 1264 | rows = append(rows, row) |
| 1265 | } |
| 1266 | if (rv.Unsupported & uint32(num)) != 0 { |
| 1267 | row := ReturnValueRow{Name: name, Result: "Unsupported"} |
| 1268 | rows = append(rows, row) |
| 1269 | } |
| 1270 | if (rv.Set & uint32(num)) != 0 { |
| 1271 | switch name { |
| 1272 | case "DISTANCE": |
| 1273 | row := ReturnValueRow{Name: name, Result: rv.Distance} |
| 1274 | rows = append(rows, row) |
| 1275 | default: |
| 1276 | row := ReturnValueRow{Name: name, Result: "Unimplemented-in-voltctl"} |
| 1277 | rows = append(rows, row) |
| 1278 | } |
| 1279 | } |
Dinesh Belwalkar | c9aa6d8 | 2020-03-04 15:22:17 -0800 | [diff] [blame] | 1280 | } |
| 1281 | |
Dinesh Belwalkar | c9aa6d8 | 2020-03-04 15:22:17 -0800 | [diff] [blame] | 1282 | outputFormat := CharReplacer.Replace(options.Format) |
| 1283 | if outputFormat == "" { |
| 1284 | outputFormat = GetCommandOptionWithDefault("device-value-get", "format", DEFAULT_DEVICE_VALUE_GET_FORMAT) |
| 1285 | } |
| 1286 | |
| 1287 | result := CommandResult{ |
| 1288 | Format: format.Format(outputFormat), |
| 1289 | OutputAs: toOutputType(options.OutputAs), |
| 1290 | NameLimit: options.NameLimit, |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 1291 | Data: rows, |
Dinesh Belwalkar | c9aa6d8 | 2020-03-04 15:22:17 -0800 | [diff] [blame] | 1292 | } |
| 1293 | GenerateOutput(&result) |
| 1294 | return nil |
| 1295 | } |