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