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