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