blob: d7553179e83c48345c23f89016f67d725b2976b9 [file] [log] [blame]
Zack Williamse940c7a2019-08-21 14:25:39 -07001/*
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 */
16package commands
17
18import (
19 "context"
20 "fmt"
David Bainbridge7052fe82020-03-25 10:37:00 -070021 "os"
22 "strconv"
23 "strings"
Girish Gowdra610acb42021-01-27 13:33:57 -080024 "time"
David Bainbridge7052fe82020-03-25 10:37:00 -070025
Scott Baker9173ed82020-05-19 08:30:12 -070026 "github.com/golang/protobuf/ptypes/empty"
Zack Williamse940c7a2019-08-21 14:25:39 -070027 flags "github.com/jessevdk/go-flags"
Scott Baker2b0ad652019-08-21 14:57:07 -070028 "github.com/opencord/voltctl/pkg/format"
kesavand8ec4fc02021-01-27 09:10:22 -050029 "github.com/opencord/voltha-protos/v4/go/common"
30 "github.com/opencord/voltha-protos/v4/go/extension"
31 "github.com/opencord/voltha-protos/v4/go/voltha"
Zack Williamse940c7a2019-08-21 14:25:39 -070032)
33
34const (
David K. Bainbridge89003c42020-02-27 17:22:49 -080035 DEFAULT_DEVICE_FORMAT = "table{{ .Id }}\t{{.Type}}\t{{.Root}}\t{{.ParentId}}\t{{.SerialNumber}}\t{{.AdminState}}\t{{.OperStatus}}\t{{.ConnectStatus}}\t{{.Reason}}"
Zack Williamse940c7a2019-08-21 14:25:39 -070036 DEFAULT_DEVICE_PORTS_FORMAT = "table{{.PortNo}}\t{{.Label}}\t{{.Type}}\t{{.AdminState}}\t{{.OperStatus}}\t{{.DeviceId}}\t{{.Peers}}"
37 DEFAULT_DEVICE_INSPECT_FORMAT = `ID: {{.Id}}
38 TYPE: {{.Type}}
39 ROOT: {{.Root}}
40 PARENTID: {{.ParentId}}
41 SERIALNUMBER: {{.SerialNumber}}
42 VLAN: {{.Vlan}}
43 ADMINSTATE: {{.AdminState}}
44 OPERSTATUS: {{.OperStatus}}
45 CONNECTSTATUS: {{.ConnectStatus}}`
Rohan Agrawal9228d2f2020-06-03 07:48:50 +000046 DEFAULT_DEVICE_PM_CONFIG_GET_FORMAT = "table{{.DefaultFreq}}\t{{.Grouped}}\t{{.FreqOverride}}"
47 DEFAULT_DEVICE_PM_CONFIG_METRIC_LIST_FORMAT = "table{{.Name}}\t{{.Type}}\t{{.Enabled}}\t{{.SampleFreq}}"
48 DEFAULT_DEVICE_PM_CONFIG_GROUP_LIST_FORMAT = "table{{.GroupName}}\t{{.Enabled}}\t{{.GroupFreq}}"
49 DEFAULT_DEVICE_VALUE_GET_FORMAT = "table{{.Name}}\t{{.Result}}"
Andrea Campanella791d88b2021-01-08 13:29:00 +010050 DEFAULT_DEVICE_IMAGE_LIST_GET_FORMAT = "table{{.Name}}\t{{.Url}}\t{{.Crc}}\t{{.DownloadState}}\t{{.ImageVersion}}\t{{.LocalDir}}\t{{.ImageState}}\t{{.FileSize}}"
kesavand3e2f9f62021-04-22 11:06:38 +053051 ONU_IMAGE_LIST_FORMAT = "table{{.Version}}\t{{.Committed}}\t{{.Active}}\t{{.Valid}}\t{{.ProductCode}}"
52 ONU_IMAGE_STATUS_FORMAT = "table{{.Version}}\t{{.DownloadState}}\t{{.Reason}}\t{{.ImageState}}"
kesavand8ec4fc02021-01-27 09:10:22 -050053 DEFAULT_DEVICE_GET_PORT_STATUS_FORMAT = `
54 TXBYTES: {{.TxBytes}}
55 TXPACKETS: {{.TxPackets}}
56 TXERRPACKETS: {{.TxErrorPackets}}
57 TXBCASTPACKETS: {{.TxBcastPackets}}
58 TXUCASTPACKETS: {{.TxUcastPackets}}
59 TXMCASTPACKETS: {{.TxMcastPackets}}
60 RXBYTES: {{.RxBytes}}
61 RXPACKETS: {{.RxPackets}}
62 RXERRPACKETS: {{.RxErrorPackets}}
63 RXBCASTPACKETS: {{.RxBcastPackets}}
64 RXUCASTPACKETS: {{.RxUcastPackets}}
65 RXMCASTPACKETS: {{.RxMcastPackets}}`
kesavand6d1131f2021-02-05 22:38:15 +053066 DEFAULT_DEVICE_GET_UNI_STATUS_FORMAT = `
67 ADMIN_STATE: {{.AdmState}}
68 OPERATIONAL_STATE: {{.OperState}}
69 CONFIG_IND: {{.ConfigInd}}`
Zack Williamse940c7a2019-08-21 14:25:39 -070070)
71
72type DeviceList struct {
73 ListOutputOptions
74}
75
76type DeviceCreate struct {
David Bainbridge1a514392020-06-23 11:12:51 -070077 DeviceType string `short:"t" required:"true" long:"devicetype" description:"Device type"`
David Bainbridge835dd0e2020-04-01 10:30:09 -070078 MACAddress string `short:"m" long:"macaddress" default:"" description:"MAC Address"`
Zack Williamse940c7a2019-08-21 14:25:39 -070079 IPAddress string `short:"i" long:"ipaddress" default:"" description:"IP Address"`
80 HostAndPort string `short:"H" long:"hostandport" default:"" description:"Host and port"`
81}
82
83type DeviceId string
84
Rohan Agrawal9228d2f2020-06-03 07:48:50 +000085type MetricName string
86type GroupName string
kesavand12cd8eb2020-01-20 22:25:22 -050087type PortNum uint32
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -080088type ValueFlag string
kesavand12cd8eb2020-01-20 22:25:22 -050089
Zack Williamse940c7a2019-08-21 14:25:39 -070090type DeviceDelete struct {
Himani Chawla9933ddc2020-10-12 23:53:27 +053091 Force bool `long:"force" description:"Delete device forcefully"`
92 Args struct {
Zack Williamse940c7a2019-08-21 14:25:39 -070093 Ids []DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
94 } `positional-args:"yes"`
95}
96
97type DeviceEnable struct {
98 Args struct {
99 Ids []DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
100 } `positional-args:"yes"`
101}
102
103type DeviceDisable struct {
104 Args struct {
105 Ids []DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
106 } `positional-args:"yes"`
107}
108
109type DeviceReboot struct {
110 Args struct {
111 Ids []DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
112 } `positional-args:"yes"`
113}
114
115type DeviceFlowList struct {
116 ListOutputOptions
Maninder045921e2020-09-29 16:46:02 +0530117 FlowIdOptions
Zack Williamse940c7a2019-08-21 14:25:39 -0700118 Args struct {
119 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
120 } `positional-args:"yes"`
121}
122
123type DevicePortList struct {
124 ListOutputOptions
125 Args struct {
126 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
127 } `positional-args:"yes"`
128}
129
130type DeviceInspect struct {
131 OutputOptionsJson
132 Args struct {
133 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
134 } `positional-args:"yes"`
135}
136
kesavand12cd8eb2020-01-20 22:25:22 -0500137type DevicePortEnable struct {
138 Args struct {
139 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
140 PortId PortNum `positional-arg-name:"PORT_NUMBER" required:"yes"`
141 } `positional-args:"yes"`
142}
143
144type DevicePortDisable struct {
145 Args struct {
146 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
147 PortId PortNum `positional-arg-name:"PORT_NUMBER" required:"yes"`
148 } `positional-args:"yes"`
149}
150
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000151type DevicePmConfigsGet struct {
152 ListOutputOptions
153 Args struct {
154 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
155 } `positional-args:"yes"`
156}
157
158type DevicePmConfigMetricList struct {
159 ListOutputOptions
160 Args struct {
161 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
162 } `positional-args:"yes"`
163}
164
165type DevicePmConfigGroupList struct {
166 ListOutputOptions
167 Args struct {
168 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
169 } `positional-args:"yes"`
170}
171
172type DevicePmConfigGroupMetricList struct {
173 ListOutputOptions
174 Args struct {
175 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
176 Group GroupName `positional-arg-name:"GROUP_NAME" required:"yes"`
177 } `positional-args:"yes"`
178}
179
180type DevicePmConfigFrequencySet struct {
181 OutputOptions
182 Args struct {
Girish Gowdra610acb42021-01-27 13:33:57 -0800183 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
184 Interval time.Duration `positional-arg-name:"INTERVAL" required:"yes"`
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000185 } `positional-args:"yes"`
186}
187
188type DevicePmConfigMetricEnable struct {
189 Args struct {
190 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
191 Metrics []MetricName `positional-arg-name:"METRIC_NAME" required:"yes"`
192 } `positional-args:"yes"`
193}
194
195type DevicePmConfigMetricDisable struct {
196 Args struct {
197 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
198 Metrics []MetricName `positional-arg-name:"METRIC_NAME" required:"yes"`
199 } `positional-args:"yes"`
200}
201
202type DevicePmConfigGroupEnable struct {
203 Args struct {
Girish Gowdra610acb42021-01-27 13:33:57 -0800204 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
205 Group GroupName `positional-arg-name:"GROUP_NAME" required:"yes"`
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000206 } `positional-args:"yes"`
207}
208
209type DevicePmConfigGroupDisable struct {
210 Args struct {
Girish Gowdra610acb42021-01-27 13:33:57 -0800211 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
212 Group GroupName `positional-arg-name:"GROUP_NAME" required:"yes"`
213 } `positional-args:"yes"`
214}
215
216type DevicePmConfigGroupFrequencySet struct {
217 OutputOptions
218 Args struct {
219 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
220 Group GroupName `positional-arg-name:"GROUP_NAME" required:"yes"`
221 Interval time.Duration `positional-arg-name:"INTERVAL" required:"yes"`
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000222 } `positional-args:"yes"`
223}
224
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -0800225type DeviceGetExtValue struct {
226 ListOutputOptions
227 Args struct {
228 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
229 Valueflag ValueFlag `positional-arg-name:"VALUE_FLAG" required:"yes"`
230 } `positional-args:"yes"`
231}
Rohan Agrawald7df3772020-06-29 11:23:36 +0000232
233type DevicePmConfigSetMaxSkew struct {
234 Args struct {
235 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
236 MaxSkew uint32 `positional-arg-name:"MAX_SKEW" required:"yes"`
237 } `positional-args:"yes"`
238}
239
Andrea Campanella791d88b2021-01-08 13:29:00 +0100240type DeviceOnuListImages struct {
241 ListOutputOptions
242 Args struct {
243 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
244 } `positional-args:"yes"`
245}
246
247type DeviceOnuDownloadImage struct {
248 Args struct {
249 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
250 Name string `positional-arg-name:"IMAGE_NAME" required:"yes"`
251 Url string `positional-arg-name:"IMAGE_URL" required:"yes"`
252 ImageVersion string `positional-arg-name:"IMAGE_VERSION" required:"yes"`
253 Crc uint32 `positional-arg-name:"IMAGE_CRC" required:"yes"`
254 LocalDir string `positional-arg-name:"IMAGE_LOCAL_DIRECTORY"`
255 } `positional-args:"yes"`
256}
257
258type DeviceOnuActivateImageUpdate struct {
259 Args struct {
260 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
261 Name string `positional-arg-name:"IMAGE_NAME" required:"yes"`
262 ImageVersion string `positional-arg-name:"IMAGE_VERSION" required:"yes"`
263 SaveConfig bool `positional-arg-name:"SAVE_EXISTING_CONFIG"`
264 LocalDir string `positional-arg-name:"IMAGE_LOCAL_DIRECTORY"`
Andrea Campanella7b2ecf42021-02-25 12:27:15 +0100265 } `positional-args:"yes"`
kesavand8ec4fc02021-01-27 09:10:22 -0500266}
kesavand3e2f9f62021-04-22 11:06:38 +0530267
268type OnuDownloadImage struct {
269 Args struct {
270 ImageVersion string `positional-arg-name:"IMAGE_VERSION" required:"yes"`
271 Url string `positional-arg-name:"IMAGE_URL" required:"yes"`
272 vendor string `positional-arg-name:"IMAGE_VENDOR"`
273 ActivateOnSuccess bool `positional-arg-name:"IMAGE_ACTIVATE_ON_SUCCESS"`
274 CommitOnSuccess bool `positional-arg-name:"IMAGE_COMMIT_ON_SUCCESS"`
275 Crc uint32 `positional-arg-name:"IMAGE_CRC"`
276 IDs []DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
277 } `positional-args:"yes"`
278}
279
280type OnuActivateImage struct {
281 Args struct {
282 ImageVersion string `positional-arg-name:"IMAGE_VERSION" required:"yes"`
283 CommitOnSuccess bool `positional-arg-name:"IMAGE_COMMIT_ON_SUCCESS"`
284 IDs []DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
285 } `positional-args:"yes"`
286}
287
288type OnuAbortUpgradeImage struct {
289 Args struct {
290 ImageVersion string `positional-arg-name:"IMAGE_VERSION" required:"yes"`
291 IDs []DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
292 } `positional-args:"yes"`
293}
294
295type OnuCommitImage struct {
296 Args struct {
297 ImageVersion string `positional-arg-name:"IMAGE_VERSION" required:"yes"`
298 IDs []DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
299 } `positional-args:"yes"`
300}
301
302type OnuImageStatus struct {
303 ListOutputOptions
304 Args struct {
305 ImageVersion string `positional-arg-name:"IMAGE_VERSION" required:"yes"`
306 IDs []DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
307 } `positional-args:"yes"`
308}
309
310type OnuListImages struct {
311 ListOutputOptions
312 Args struct {
313 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
314 } `positional-args:"yes"`
315}
316
kesavand8ec4fc02021-01-27 09:10:22 -0500317type DeviceGetPortStats struct {
318 ListOutputOptions
319 Args struct {
320 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
321 PortNo uint32 `positional-arg-name:"PORT_NO" required:"yes"`
322 PortType string `positional-arg-name:"PORT_TYPE" required:"yes"`
Andrea Campanella791d88b2021-01-08 13:29:00 +0100323 } `positional-args:"yes"`
324}
kesavand6d1131f2021-02-05 22:38:15 +0530325type UniStatus struct {
326 ListOutputOptions
327 Args struct {
328 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
329 UniIndex uint32 `positional-arg-name:"UNI_INDEX" required:"yes"`
330 } `positional-args:"yes"`
331}
Zack Williamse940c7a2019-08-21 14:25:39 -0700332type DeviceOpts struct {
333 List DeviceList `command:"list"`
334 Create DeviceCreate `command:"create"`
335 Delete DeviceDelete `command:"delete"`
336 Enable DeviceEnable `command:"enable"`
337 Disable DeviceDisable `command:"disable"`
338 Flows DeviceFlowList `command:"flows"`
kesavand12cd8eb2020-01-20 22:25:22 -0500339 Port struct {
340 List DevicePortList `command:"list"`
341 Enable DevicePortEnable `command:"enable"`
342 Disable DevicePortDisable `command:"disable"`
343 } `command:"port"`
344 Inspect DeviceInspect `command:"inspect"`
345 Reboot DeviceReboot `command:"reboot"`
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -0800346 Value struct {
347 Get DeviceGetExtValue `command:"get"`
348 } `command:"value"`
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000349 PmConfig struct {
Rohan Agrawald7df3772020-06-29 11:23:36 +0000350 Get DevicePmConfigsGet `command:"get"`
351 MaxSkew struct {
352 Set DevicePmConfigSetMaxSkew `command:"set"`
353 } `command:"maxskew"`
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000354 Frequency struct {
355 Set DevicePmConfigFrequencySet `command:"set"`
356 } `command:"frequency"`
357 Metric struct {
358 List DevicePmConfigMetricList `command:"list"`
359 Enable DevicePmConfigMetricEnable `command:"enable"`
360 Disable DevicePmConfigMetricDisable `command:"disable"`
361 } `command:"metric"`
362 Group struct {
Girish Gowdra610acb42021-01-27 13:33:57 -0800363 List DevicePmConfigGroupList `command:"list"`
364 Enable DevicePmConfigGroupEnable `command:"enable"`
365 Disable DevicePmConfigGroupDisable `command:"disable"`
366 Set DevicePmConfigGroupFrequencySet `command:"set"`
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000367 } `command:"group"`
368 GroupMetric struct {
369 List DevicePmConfigGroupMetricList `command:"list"`
370 } `command:"groupmetric"`
371 } `command:"pmconfig"`
Andrea Campanella791d88b2021-01-08 13:29:00 +0100372 Image struct {
373 Get DeviceOnuListImages `command:"list"`
374 Download DeviceOnuDownloadImage `command:"download"`
375 Activate DeviceOnuActivateImageUpdate `command:"activate"`
376 } `command:"image"`
kesavand3e2f9f62021-04-22 11:06:38 +0530377 DownloadImage struct {
378 Download OnuDownloadImage `command:"download"`
379 Activate OnuActivateImage `command:"activate"`
380 Commit OnuCommitImage `command:"commit"`
381 AbortUpgrade OnuAbortUpgradeImage `command:"abort"`
382 Status OnuImageStatus `command:"status"`
383 List OnuListImages `command:"list" `
384 } `command:"onuimage"`
kesavand8ec4fc02021-01-27 09:10:22 -0500385 GetExtVal struct {
kesavand6d1131f2021-02-05 22:38:15 +0530386 Stats DeviceGetPortStats `command:"portstats"`
387 UniStatus UniStatus `command:"unistatus"`
kesavand8ec4fc02021-01-27 09:10:22 -0500388 } `command:"getextval"`
Zack Williamse940c7a2019-08-21 14:25:39 -0700389}
390
391var deviceOpts = DeviceOpts{}
392
393func RegisterDeviceCommands(parser *flags.Parser) {
David Bainbridge12f036f2019-10-15 22:09:04 +0000394 if _, err := parser.AddCommand("device", "device commands", "Commands to query and manipulate VOLTHA devices", &deviceOpts); err != nil {
David Bainbridgea6722342019-10-24 23:55:53 +0000395 Error.Fatalf("Unexpected error while attempting to register device commands : %s", err)
David Bainbridge12f036f2019-10-15 22:09:04 +0000396 }
Zack Williamse940c7a2019-08-21 14:25:39 -0700397}
398
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000399func (i *MetricName) Complete(match string) []flags.Completion {
400 conn, err := NewConnection()
401 if err != nil {
402 return nil
403 }
404 defer conn.Close()
405
406 client := voltha.NewVolthaServiceClient(conn)
407
408 var deviceId string
409found:
410 for i := len(os.Args) - 1; i >= 0; i -= 1 {
411 switch os.Args[i] {
412 case "enable":
413 fallthrough
414 case "disable":
415 if len(os.Args) > i+1 {
416 deviceId = os.Args[i+1]
417 } else {
418 return nil
419 }
420 break found
421 default:
422 }
423 }
424
425 if len(deviceId) == 0 {
426 return nil
427 }
428
David K. Bainbridge9189c632021-03-26 21:52:21 +0000429 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000430 defer cancel()
431
432 id := voltha.ID{Id: string(deviceId)}
433
434 pmconfigs, err := client.ListDevicePmConfigs(ctx, &id)
435
436 if err != nil {
437 return nil
438 }
439
440 list := make([]flags.Completion, 0)
441 for _, metrics := range pmconfigs.Metrics {
442 if strings.HasPrefix(metrics.Name, match) {
443 list = append(list, flags.Completion{Item: metrics.Name})
444 }
445 }
446
447 return list
448}
449
450func (i *GroupName) Complete(match string) []flags.Completion {
451 conn, err := NewConnection()
452 if err != nil {
453 return nil
454 }
455 defer conn.Close()
456
457 client := voltha.NewVolthaServiceClient(conn)
458
459 var deviceId string
460found:
461 for i := len(os.Args) - 1; i >= 0; i -= 1 {
462 switch os.Args[i] {
463 case "list":
464 fallthrough
465 case "enable":
466 fallthrough
467 case "disable":
468 if len(os.Args) > i+1 {
469 deviceId = os.Args[i+1]
470 } else {
471 return nil
472 }
473 break found
474 default:
475 }
476 }
477
478 if len(deviceId) == 0 {
479 return nil
480 }
481
David K. Bainbridge9189c632021-03-26 21:52:21 +0000482 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000483 defer cancel()
484
485 id := voltha.ID{Id: string(deviceId)}
486
487 pmconfigs, err := client.ListDevicePmConfigs(ctx, &id)
488
489 if err != nil {
490 return nil
491 }
492
493 list := make([]flags.Completion, 0)
494 for _, group := range pmconfigs.Groups {
495 if strings.HasPrefix(group.GroupName, match) {
496 list = append(list, flags.Completion{Item: group.GroupName})
497 }
498 }
499 return list
500}
501
kesavand12cd8eb2020-01-20 22:25:22 -0500502func (i *PortNum) Complete(match string) []flags.Completion {
503 conn, err := NewConnection()
504 if err != nil {
505 return nil
506 }
507 defer conn.Close()
508
Scott Baker9173ed82020-05-19 08:30:12 -0700509 client := voltha.NewVolthaServiceClient(conn)
kesavand12cd8eb2020-01-20 22:25:22 -0500510
511 /*
512 * The command line args when completing for PortNum will be a DeviceId
513 * followed by one or more PortNums. So walk the argument list from the
514 * end and find the first argument that is enable/disable as those are
515 * the subcommands that come before the positional arguments. It would
516 * be nice if this package gave us the list of optional arguments
517 * already parsed.
518 */
519 var deviceId string
520found:
521 for i := len(os.Args) - 1; i >= 0; i -= 1 {
522 switch os.Args[i] {
523 case "enable":
524 fallthrough
525 case "disable":
526 if len(os.Args) > i+1 {
527 deviceId = os.Args[i+1]
528 } else {
529 return nil
530 }
531 break found
532 default:
533 }
534 }
535
536 if len(deviceId) == 0 {
537 return nil
538 }
539
David K. Bainbridge9189c632021-03-26 21:52:21 +0000540 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
kesavand12cd8eb2020-01-20 22:25:22 -0500541 defer cancel()
kesavand12cd8eb2020-01-20 22:25:22 -0500542
Scott Baker9173ed82020-05-19 08:30:12 -0700543 id := voltha.ID{Id: string(deviceId)}
kesavand12cd8eb2020-01-20 22:25:22 -0500544
Scott Baker9173ed82020-05-19 08:30:12 -0700545 ports, err := client.ListDevicePorts(ctx, &id)
kesavand12cd8eb2020-01-20 22:25:22 -0500546 if err != nil {
547 return nil
548 }
549
550 list := make([]flags.Completion, 0)
Scott Baker9173ed82020-05-19 08:30:12 -0700551 for _, item := range ports.Items {
552 pn := strconv.FormatUint(uint64(item.PortNo), 10)
kesavand12cd8eb2020-01-20 22:25:22 -0500553 if strings.HasPrefix(pn, match) {
554 list = append(list, flags.Completion{Item: pn})
555 }
556 }
557
558 return list
559}
560
Zack Williamse940c7a2019-08-21 14:25:39 -0700561func (i *DeviceId) Complete(match string) []flags.Completion {
562 conn, err := NewConnection()
563 if err != nil {
564 return nil
565 }
566 defer conn.Close()
567
Scott Baker9173ed82020-05-19 08:30:12 -0700568 client := voltha.NewVolthaServiceClient(conn)
Zack Williamse940c7a2019-08-21 14:25:39 -0700569
David K. Bainbridge9189c632021-03-26 21:52:21 +0000570 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Zack Williamse940c7a2019-08-21 14:25:39 -0700571 defer cancel()
572
Scott Baker9173ed82020-05-19 08:30:12 -0700573 devices, err := client.ListDevices(ctx, &empty.Empty{})
Zack Williamse940c7a2019-08-21 14:25:39 -0700574 if err != nil {
575 return nil
576 }
577
578 list := make([]flags.Completion, 0)
Scott Baker9173ed82020-05-19 08:30:12 -0700579 for _, item := range devices.Items {
580 if strings.HasPrefix(item.Id, match) {
581 list = append(list, flags.Completion{Item: item.Id})
Zack Williamse940c7a2019-08-21 14:25:39 -0700582 }
583 }
584
585 return list
586}
587
588func (options *DeviceList) Execute(args []string) error {
589
590 conn, err := NewConnection()
591 if err != nil {
592 return err
593 }
594 defer conn.Close()
595
Scott Baker9173ed82020-05-19 08:30:12 -0700596 client := voltha.NewVolthaServiceClient(conn)
Zack Williamse940c7a2019-08-21 14:25:39 -0700597
David K. Bainbridge9189c632021-03-26 21:52:21 +0000598 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Zack Williamse940c7a2019-08-21 14:25:39 -0700599 defer cancel()
600
Scott Baker9173ed82020-05-19 08:30:12 -0700601 devices, err := client.ListDevices(ctx, &empty.Empty{})
Zack Williamse940c7a2019-08-21 14:25:39 -0700602 if err != nil {
603 return err
604 }
605
606 outputFormat := CharReplacer.Replace(options.Format)
607 if outputFormat == "" {
David Bainbridgea6722342019-10-24 23:55:53 +0000608 outputFormat = GetCommandOptionWithDefault("device-list", "format", DEFAULT_DEVICE_FORMAT)
Zack Williamse940c7a2019-08-21 14:25:39 -0700609 }
610 if options.Quiet {
611 outputFormat = "{{.Id}}"
612 }
613
David Bainbridgea6722342019-10-24 23:55:53 +0000614 orderBy := options.OrderBy
615 if orderBy == "" {
616 orderBy = GetCommandOptionWithDefault("device-list", "order", "")
617 }
618
Scott Baker9173ed82020-05-19 08:30:12 -0700619 // Make sure json output prints an empty list, not "null"
620 if devices.Items == nil {
621 devices.Items = make([]*voltha.Device, 0)
Zack Williamse940c7a2019-08-21 14:25:39 -0700622 }
623
624 result := CommandResult{
625 Format: format.Format(outputFormat),
626 Filter: options.Filter,
David Bainbridgea6722342019-10-24 23:55:53 +0000627 OrderBy: orderBy,
Zack Williamse940c7a2019-08-21 14:25:39 -0700628 OutputAs: toOutputType(options.OutputAs),
629 NameLimit: options.NameLimit,
Scott Baker9173ed82020-05-19 08:30:12 -0700630 Data: devices.Items,
Zack Williamse940c7a2019-08-21 14:25:39 -0700631 }
632
633 GenerateOutput(&result)
634 return nil
635}
636
637func (options *DeviceCreate) Execute(args []string) error {
638
Scott Baker9173ed82020-05-19 08:30:12 -0700639 device := voltha.Device{}
Zack Williamse940c7a2019-08-21 14:25:39 -0700640 if options.HostAndPort != "" {
Scott Baker9173ed82020-05-19 08:30:12 -0700641 device.Address = &voltha.Device_HostAndPort{HostAndPort: options.HostAndPort}
Zack Williamse940c7a2019-08-21 14:25:39 -0700642 } else if options.IPAddress != "" {
Scott Baker9173ed82020-05-19 08:30:12 -0700643 device.Address = &voltha.Device_Ipv4Address{Ipv4Address: options.IPAddress}
Hardik Windlassce1de342020-02-04 21:58:07 +0000644 }
645 if options.MACAddress != "" {
Scott Baker9173ed82020-05-19 08:30:12 -0700646 device.MacAddress = strings.ToLower(options.MACAddress)
Zack Williamse940c7a2019-08-21 14:25:39 -0700647 }
648 if options.DeviceType != "" {
Scott Baker9173ed82020-05-19 08:30:12 -0700649 device.Type = options.DeviceType
Zack Williamse940c7a2019-08-21 14:25:39 -0700650 }
651
652 conn, err := NewConnection()
653 if err != nil {
654 return err
655 }
656 defer conn.Close()
657
Scott Baker9173ed82020-05-19 08:30:12 -0700658 client := voltha.NewVolthaServiceClient(conn)
Zack Williamse940c7a2019-08-21 14:25:39 -0700659
David K. Bainbridge9189c632021-03-26 21:52:21 +0000660 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Zack Williamse940c7a2019-08-21 14:25:39 -0700661 defer cancel()
662
Scott Baker9173ed82020-05-19 08:30:12 -0700663 createdDevice, err := client.CreateDevice(ctx, &device)
Zack Williamse940c7a2019-08-21 14:25:39 -0700664 if err != nil {
665 return err
Zack Williamse940c7a2019-08-21 14:25:39 -0700666 }
667
Scott Baker9173ed82020-05-19 08:30:12 -0700668 fmt.Printf("%s\n", createdDevice.Id)
Zack Williamse940c7a2019-08-21 14:25:39 -0700669
670 return nil
671}
672
673func (options *DeviceDelete) Execute(args []string) error {
674
675 conn, err := NewConnection()
676 if err != nil {
677 return err
678 }
679 defer conn.Close()
680
Scott Baker9173ed82020-05-19 08:30:12 -0700681 client := voltha.NewVolthaServiceClient(conn)
David Bainbridge7052fe82020-03-25 10:37:00 -0700682 var lastErr error
Zack Williamse940c7a2019-08-21 14:25:39 -0700683 for _, i := range options.Args.Ids {
David K. Bainbridge9189c632021-03-26 21:52:21 +0000684 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Zack Williamse940c7a2019-08-21 14:25:39 -0700685 defer cancel()
686
Scott Baker9173ed82020-05-19 08:30:12 -0700687 id := voltha.ID{Id: string(i)}
Himani Chawla9933ddc2020-10-12 23:53:27 +0530688 if options.Force {
689 _, err = client.ForceDeleteDevice(ctx, &id)
690 } else {
691 _, err = client.DeleteDevice(ctx, &id)
692 }
Scott Baker9173ed82020-05-19 08:30:12 -0700693
Zack Williamse940c7a2019-08-21 14:25:39 -0700694 if err != nil {
David Bainbridge0f758d42019-10-26 05:17:48 +0000695 Error.Printf("Error while deleting '%s': %s\n", i, err)
David Bainbridge7052fe82020-03-25 10:37:00 -0700696 lastErr = err
Zack Williamse940c7a2019-08-21 14:25:39 -0700697 continue
Zack Williamse940c7a2019-08-21 14:25:39 -0700698 }
699 fmt.Printf("%s\n", i)
700 }
701
David Bainbridge7052fe82020-03-25 10:37:00 -0700702 if lastErr != nil {
703 return NoReportErr
704 }
Zack Williamse940c7a2019-08-21 14:25:39 -0700705 return nil
706}
707
708func (options *DeviceEnable) Execute(args []string) error {
709 conn, err := NewConnection()
710 if err != nil {
711 return err
712 }
713 defer conn.Close()
714
Scott Baker9173ed82020-05-19 08:30:12 -0700715 client := voltha.NewVolthaServiceClient(conn)
Zack Williamse940c7a2019-08-21 14:25:39 -0700716
David Bainbridge7052fe82020-03-25 10:37:00 -0700717 var lastErr error
Zack Williamse940c7a2019-08-21 14:25:39 -0700718 for _, i := range options.Args.Ids {
David K. Bainbridge9189c632021-03-26 21:52:21 +0000719 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Zack Williamse940c7a2019-08-21 14:25:39 -0700720 defer cancel()
721
Scott Baker9173ed82020-05-19 08:30:12 -0700722 id := voltha.ID{Id: string(i)}
723
724 _, err := client.EnableDevice(ctx, &id)
Zack Williamse940c7a2019-08-21 14:25:39 -0700725 if err != nil {
David Bainbridge0f758d42019-10-26 05:17:48 +0000726 Error.Printf("Error while enabling '%s': %s\n", i, err)
David Bainbridge7052fe82020-03-25 10:37:00 -0700727 lastErr = err
Zack Williamse940c7a2019-08-21 14:25:39 -0700728 continue
Zack Williamse940c7a2019-08-21 14:25:39 -0700729 }
730 fmt.Printf("%s\n", i)
731 }
732
David Bainbridge7052fe82020-03-25 10:37:00 -0700733 if lastErr != nil {
734 return NoReportErr
735 }
Zack Williamse940c7a2019-08-21 14:25:39 -0700736 return nil
737}
738
739func (options *DeviceDisable) Execute(args []string) error {
740 conn, err := NewConnection()
741 if err != nil {
742 return err
743 }
744 defer conn.Close()
745
Scott Baker9173ed82020-05-19 08:30:12 -0700746 client := voltha.NewVolthaServiceClient(conn)
Zack Williamse940c7a2019-08-21 14:25:39 -0700747
David Bainbridge7052fe82020-03-25 10:37:00 -0700748 var lastErr error
Zack Williamse940c7a2019-08-21 14:25:39 -0700749 for _, i := range options.Args.Ids {
David K. Bainbridge9189c632021-03-26 21:52:21 +0000750 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Zack Williamse940c7a2019-08-21 14:25:39 -0700751 defer cancel()
752
Scott Baker9173ed82020-05-19 08:30:12 -0700753 id := voltha.ID{Id: string(i)}
754
755 _, err := client.DisableDevice(ctx, &id)
Zack Williamse940c7a2019-08-21 14:25:39 -0700756 if err != nil {
David Bainbridge0f758d42019-10-26 05:17:48 +0000757 Error.Printf("Error while disabling '%s': %s\n", i, err)
David Bainbridge7052fe82020-03-25 10:37:00 -0700758 lastErr = err
Zack Williamse940c7a2019-08-21 14:25:39 -0700759 continue
Zack Williamse940c7a2019-08-21 14:25:39 -0700760 }
761 fmt.Printf("%s\n", i)
762 }
763
David Bainbridge7052fe82020-03-25 10:37:00 -0700764 if lastErr != nil {
765 return NoReportErr
766 }
Zack Williamse940c7a2019-08-21 14:25:39 -0700767 return nil
768}
769
770func (options *DeviceReboot) Execute(args []string) error {
771 conn, err := NewConnection()
772 if err != nil {
773 return err
774 }
775 defer conn.Close()
776
Scott Baker9173ed82020-05-19 08:30:12 -0700777 client := voltha.NewVolthaServiceClient(conn)
Zack Williamse940c7a2019-08-21 14:25:39 -0700778
David Bainbridge7052fe82020-03-25 10:37:00 -0700779 var lastErr error
Zack Williamse940c7a2019-08-21 14:25:39 -0700780 for _, i := range options.Args.Ids {
David K. Bainbridge9189c632021-03-26 21:52:21 +0000781 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Zack Williamse940c7a2019-08-21 14:25:39 -0700782 defer cancel()
783
Scott Baker9173ed82020-05-19 08:30:12 -0700784 id := voltha.ID{Id: string(i)}
785
786 _, err := client.RebootDevice(ctx, &id)
Zack Williamse940c7a2019-08-21 14:25:39 -0700787 if err != nil {
David Bainbridge0f758d42019-10-26 05:17:48 +0000788 Error.Printf("Error while rebooting '%s': %s\n", i, err)
David Bainbridge7052fe82020-03-25 10:37:00 -0700789 lastErr = err
Zack Williamse940c7a2019-08-21 14:25:39 -0700790 continue
Zack Williamse940c7a2019-08-21 14:25:39 -0700791 }
792 fmt.Printf("%s\n", i)
793 }
794
David Bainbridge7052fe82020-03-25 10:37:00 -0700795 if lastErr != nil {
796 return NoReportErr
797 }
Zack Williamse940c7a2019-08-21 14:25:39 -0700798 return nil
799}
800
801func (options *DevicePortList) Execute(args []string) error {
802
803 conn, err := NewConnection()
804 if err != nil {
805 return err
806 }
807 defer conn.Close()
808
Scott Baker9173ed82020-05-19 08:30:12 -0700809 client := voltha.NewVolthaServiceClient(conn)
Zack Williamse940c7a2019-08-21 14:25:39 -0700810
David K. Bainbridge9189c632021-03-26 21:52:21 +0000811 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Zack Williamse940c7a2019-08-21 14:25:39 -0700812 defer cancel()
813
Scott Baker9173ed82020-05-19 08:30:12 -0700814 id := voltha.ID{Id: string(options.Args.Id)}
Zack Williamse940c7a2019-08-21 14:25:39 -0700815
Scott Baker9173ed82020-05-19 08:30:12 -0700816 ports, err := client.ListDevicePorts(ctx, &id)
Zack Williamse940c7a2019-08-21 14:25:39 -0700817 if err != nil {
818 return err
819 }
820
821 outputFormat := CharReplacer.Replace(options.Format)
822 if outputFormat == "" {
David Bainbridgea6722342019-10-24 23:55:53 +0000823 outputFormat = GetCommandOptionWithDefault("device-ports", "format", DEFAULT_DEVICE_PORTS_FORMAT)
Zack Williamse940c7a2019-08-21 14:25:39 -0700824 }
Zack Williamse940c7a2019-08-21 14:25:39 -0700825
David Bainbridgea6722342019-10-24 23:55:53 +0000826 orderBy := options.OrderBy
827 if orderBy == "" {
828 orderBy = GetCommandOptionWithDefault("device-ports", "order", "")
829 }
830
Zack Williamse940c7a2019-08-21 14:25:39 -0700831 result := CommandResult{
832 Format: format.Format(outputFormat),
833 Filter: options.Filter,
David Bainbridgea6722342019-10-24 23:55:53 +0000834 OrderBy: orderBy,
Zack Williamse940c7a2019-08-21 14:25:39 -0700835 OutputAs: toOutputType(options.OutputAs),
836 NameLimit: options.NameLimit,
Scott Baker9173ed82020-05-19 08:30:12 -0700837 Data: ports.Items,
Zack Williamse940c7a2019-08-21 14:25:39 -0700838 }
839
840 GenerateOutput(&result)
841 return nil
842}
843
844func (options *DeviceFlowList) Execute(args []string) error {
845 fl := &FlowList{}
846 fl.ListOutputOptions = options.ListOutputOptions
Maninder045921e2020-09-29 16:46:02 +0530847 fl.FlowIdOptions = options.FlowIdOptions
Zack Williamse940c7a2019-08-21 14:25:39 -0700848 fl.Args.Id = string(options.Args.Id)
David Bainbridgea6722342019-10-24 23:55:53 +0000849 fl.Method = "device-flows"
Zack Williamse940c7a2019-08-21 14:25:39 -0700850 return fl.Execute(args)
851}
852
853func (options *DeviceInspect) Execute(args []string) error {
854 if len(args) > 0 {
855 return fmt.Errorf("only a single argument 'DEVICE_ID' can be provided")
856 }
857
858 conn, err := NewConnection()
859 if err != nil {
860 return err
861 }
862 defer conn.Close()
863
Scott Baker9173ed82020-05-19 08:30:12 -0700864 client := voltha.NewVolthaServiceClient(conn)
Zack Williamse940c7a2019-08-21 14:25:39 -0700865
David K. Bainbridge9189c632021-03-26 21:52:21 +0000866 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Zack Williamse940c7a2019-08-21 14:25:39 -0700867 defer cancel()
868
Scott Baker9173ed82020-05-19 08:30:12 -0700869 id := voltha.ID{Id: string(options.Args.Id)}
Zack Williamse940c7a2019-08-21 14:25:39 -0700870
Scott Baker9173ed82020-05-19 08:30:12 -0700871 device, err := client.GetDevice(ctx, &id)
Zack Williamse940c7a2019-08-21 14:25:39 -0700872 if err != nil {
873 return err
874 }
875
Zack Williamse940c7a2019-08-21 14:25:39 -0700876 outputFormat := CharReplacer.Replace(options.Format)
877 if outputFormat == "" {
David Bainbridgea6722342019-10-24 23:55:53 +0000878 outputFormat = GetCommandOptionWithDefault("device-inspect", "format", DEFAULT_DEVICE_INSPECT_FORMAT)
Zack Williamse940c7a2019-08-21 14:25:39 -0700879 }
880 if options.Quiet {
881 outputFormat = "{{.Id}}"
882 }
883
884 result := CommandResult{
885 Format: format.Format(outputFormat),
886 OutputAs: toOutputType(options.OutputAs),
887 NameLimit: options.NameLimit,
888 Data: device,
889 }
890 GenerateOutput(&result)
891 return nil
892}
kesavand12cd8eb2020-01-20 22:25:22 -0500893
894/*Device Port Enable */
895func (options *DevicePortEnable) Execute(args []string) error {
896 conn, err := NewConnection()
897 if err != nil {
898 return err
899 }
900 defer conn.Close()
901
Scott Baker9173ed82020-05-19 08:30:12 -0700902 client := voltha.NewVolthaServiceClient(conn)
kesavand12cd8eb2020-01-20 22:25:22 -0500903
David K. Bainbridge9189c632021-03-26 21:52:21 +0000904 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
kesavand12cd8eb2020-01-20 22:25:22 -0500905 defer cancel()
906
Scott Baker9173ed82020-05-19 08:30:12 -0700907 port := voltha.Port{DeviceId: string(options.Args.Id), PortNo: uint32(options.Args.PortId)}
908
909 _, err = client.EnablePort(ctx, &port)
kesavand12cd8eb2020-01-20 22:25:22 -0500910 if err != nil {
911 Error.Printf("Error enabling port number %v on device Id %s,err=%s\n", options.Args.PortId, options.Args.Id, ErrorToString(err))
912 return err
kesavand12cd8eb2020-01-20 22:25:22 -0500913 }
914
915 return nil
916}
917
Scott Baker9173ed82020-05-19 08:30:12 -0700918/*Device Port Disable */
kesavand12cd8eb2020-01-20 22:25:22 -0500919func (options *DevicePortDisable) Execute(args []string) error {
920 conn, err := NewConnection()
921 if err != nil {
922 return err
923 }
924 defer conn.Close()
925
Scott Baker9173ed82020-05-19 08:30:12 -0700926 client := voltha.NewVolthaServiceClient(conn)
927
David K. Bainbridge9189c632021-03-26 21:52:21 +0000928 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
kesavand12cd8eb2020-01-20 22:25:22 -0500929 defer cancel()
930
Scott Baker9173ed82020-05-19 08:30:12 -0700931 port := voltha.Port{DeviceId: string(options.Args.Id), PortNo: uint32(options.Args.PortId)}
932
933 _, err = client.DisablePort(ctx, &port)
kesavand12cd8eb2020-01-20 22:25:22 -0500934 if err != nil {
Scott Baker9173ed82020-05-19 08:30:12 -0700935 Error.Printf("Error enabling port number %v on device Id %s,err=%s\n", options.Args.PortId, options.Args.Id, ErrorToString(err))
kesavand12cd8eb2020-01-20 22:25:22 -0500936 return err
kesavand12cd8eb2020-01-20 22:25:22 -0500937 }
Scott Baker9173ed82020-05-19 08:30:12 -0700938
kesavand12cd8eb2020-01-20 22:25:22 -0500939 return nil
940}
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -0800941
Rohan Agrawald7df3772020-06-29 11:23:36 +0000942func (options *DevicePmConfigSetMaxSkew) Execute(args []string) error {
943 conn, err := NewConnection()
944 if err != nil {
945 return err
946 }
947 defer conn.Close()
948
949 client := voltha.NewVolthaServiceClient(conn)
950
David K. Bainbridge9189c632021-03-26 21:52:21 +0000951 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Rohan Agrawald7df3772020-06-29 11:23:36 +0000952 defer cancel()
953
954 id := voltha.ID{Id: string(options.Args.Id)}
955
956 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
957 if err != nil {
958 return err
959 }
960
961 pmConfigs.MaxSkew = options.Args.MaxSkew
962
963 _, err = client.UpdateDevicePmConfigs(ctx, pmConfigs)
964 if err != nil {
965 return err
966 }
967
968 return nil
969}
970
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000971func (options *DevicePmConfigsGet) Execute(args []string) error {
972
973 conn, err := NewConnection()
974 if err != nil {
975 return err
976 }
977 defer conn.Close()
978
979 client := voltha.NewVolthaServiceClient(conn)
980
David K. Bainbridge9189c632021-03-26 21:52:21 +0000981 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000982 defer cancel()
983
984 id := voltha.ID{Id: string(options.Args.Id)}
985
986 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
987 if err != nil {
988 return err
989 }
990
991 outputFormat := CharReplacer.Replace(options.Format)
992 if outputFormat == "" {
993 outputFormat = GetCommandOptionWithDefault("device-pm-configs", "format", DEFAULT_DEVICE_PM_CONFIG_GET_FORMAT)
994 }
995
996 orderBy := options.OrderBy
997 if orderBy == "" {
998 orderBy = GetCommandOptionWithDefault("device-pm-configs", "order", "")
999 }
1000
1001 result := CommandResult{
1002 Format: format.Format(outputFormat),
1003 Filter: options.Filter,
1004 OrderBy: orderBy,
1005 OutputAs: toOutputType(options.OutputAs),
1006 NameLimit: options.NameLimit,
1007 Data: pmConfigs,
1008 }
1009
1010 GenerateOutput(&result)
1011 return nil
1012
1013}
1014
1015func (options *DevicePmConfigMetricList) Execute(args []string) error {
1016
1017 conn, err := NewConnection()
1018 if err != nil {
1019 return err
1020 }
1021 defer conn.Close()
1022
1023 client := voltha.NewVolthaServiceClient(conn)
1024
David K. Bainbridge9189c632021-03-26 21:52:21 +00001025 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001026 defer cancel()
1027
1028 id := voltha.ID{Id: string(options.Args.Id)}
1029
1030 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
1031 if err != nil {
1032 return err
1033 }
1034
1035 if !pmConfigs.Grouped {
1036 for _, metric := range pmConfigs.Metrics {
1037 if metric.SampleFreq == 0 {
1038 metric.SampleFreq = pmConfigs.DefaultFreq
1039 }
1040 }
Rohan Agrawalbca69122020-06-17 14:59:03 +00001041 outputFormat := CharReplacer.Replace(options.Format)
1042 if outputFormat == "" {
1043 outputFormat = GetCommandOptionWithDefault("device-pm-configs", "format", DEFAULT_DEVICE_PM_CONFIG_METRIC_LIST_FORMAT)
1044 }
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001045
Rohan Agrawalbca69122020-06-17 14:59:03 +00001046 orderBy := options.OrderBy
1047 if orderBy == "" {
1048 orderBy = GetCommandOptionWithDefault("device-pm-configs", "order", "")
1049 }
1050
1051 result := CommandResult{
1052 Format: format.Format(outputFormat),
1053 Filter: options.Filter,
1054 OrderBy: orderBy,
1055 OutputAs: toOutputType(options.OutputAs),
1056 NameLimit: options.NameLimit,
1057 Data: pmConfigs.Metrics,
1058 }
1059
1060 GenerateOutput(&result)
1061 return nil
1062 } else {
1063 return fmt.Errorf("Device '%s' does not have Non Grouped Metrics", options.Args.Id)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001064 }
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001065}
1066
1067func (options *DevicePmConfigMetricEnable) Execute(args []string) error {
1068
1069 conn, err := NewConnection()
1070 if err != nil {
1071 return err
1072 }
1073 defer conn.Close()
1074
1075 client := voltha.NewVolthaServiceClient(conn)
1076
David K. Bainbridge9189c632021-03-26 21:52:21 +00001077 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001078 defer cancel()
1079
1080 id := voltha.ID{Id: string(options.Args.Id)}
1081
1082 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
1083 if err != nil {
1084 return err
1085 }
1086
1087 if !pmConfigs.Grouped {
Rohan Agrawalbca69122020-06-17 14:59:03 +00001088 metrics := make(map[string]struct{})
1089 for _, metric := range pmConfigs.Metrics {
1090 metrics[metric.Name] = struct{}{}
1091 }
1092
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001093 for _, metric := range pmConfigs.Metrics {
1094 for _, mName := range options.Args.Metrics {
Rohan Agrawalbca69122020-06-17 14:59:03 +00001095 if _, exist := metrics[string(mName)]; !exist {
1096 return fmt.Errorf("Metric Name '%s' does not exist", mName)
1097 }
1098
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001099 if string(mName) == metric.Name && !metric.Enabled {
1100 metric.Enabled = true
1101 _, err := client.UpdateDevicePmConfigs(ctx, pmConfigs)
1102 if err != nil {
1103 return err
1104 }
1105 }
1106 }
1107 }
Rohan Agrawalbca69122020-06-17 14:59:03 +00001108 } else {
1109 return fmt.Errorf("Device '%s' does not have Non Grouped Metrics", options.Args.Id)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001110 }
1111 return nil
1112}
1113
1114func (options *DevicePmConfigMetricDisable) Execute(args []string) error {
1115
1116 conn, err := NewConnection()
1117 if err != nil {
1118 return err
1119 }
1120 defer conn.Close()
1121
1122 client := voltha.NewVolthaServiceClient(conn)
1123
David K. Bainbridge9189c632021-03-26 21:52:21 +00001124 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001125 defer cancel()
1126
1127 id := voltha.ID{Id: string(options.Args.Id)}
1128
1129 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
1130 if err != nil {
1131 return err
1132 }
1133
1134 if !pmConfigs.Grouped {
Rohan Agrawalbca69122020-06-17 14:59:03 +00001135 metrics := make(map[string]struct{})
1136 for _, metric := range pmConfigs.Metrics {
1137 metrics[metric.Name] = struct{}{}
1138 }
1139
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001140 for _, metric := range pmConfigs.Metrics {
1141 for _, mName := range options.Args.Metrics {
Rohan Agrawalbca69122020-06-17 14:59:03 +00001142 if _, have := metrics[string(mName)]; !have {
1143 return fmt.Errorf("Metric Name '%s' does not exist", mName)
1144 }
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001145 if string(mName) == metric.Name && metric.Enabled {
1146 metric.Enabled = false
1147 _, err := client.UpdateDevicePmConfigs(ctx, pmConfigs)
1148 if err != nil {
1149 return err
1150 }
Rohan Agrawalbca69122020-06-17 14:59:03 +00001151 } else {
1152 return fmt.Errorf("Metric '%s' cannot be disabled", string(mName))
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001153 }
1154 }
1155 }
Rohan Agrawalbca69122020-06-17 14:59:03 +00001156 } else {
1157 return fmt.Errorf("Device '%s' does not have Non Grouped Metrics", options.Args.Id)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001158 }
1159 return nil
1160}
1161
1162func (options *DevicePmConfigGroupEnable) Execute(args []string) error {
1163
1164 conn, err := NewConnection()
1165 if err != nil {
1166 return err
1167 }
1168 defer conn.Close()
1169
1170 client := voltha.NewVolthaServiceClient(conn)
1171
David K. Bainbridge9189c632021-03-26 21:52:21 +00001172 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001173 defer cancel()
1174
1175 id := voltha.ID{Id: string(options.Args.Id)}
1176
1177 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
1178 if err != nil {
1179 return err
1180 }
1181
1182 if pmConfigs.Grouped {
Rohan Agrawalbca69122020-06-17 14:59:03 +00001183 groups := make(map[string]struct{})
1184 for _, group := range pmConfigs.Groups {
1185 groups[group.GroupName] = struct{}{}
1186 }
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001187 for _, group := range pmConfigs.Groups {
Girish Gowdra610acb42021-01-27 13:33:57 -08001188 if _, have := groups[string(options.Args.Group)]; !have {
1189 return fmt.Errorf("Group Name '%s' does not exist", options.Args.Group)
1190 }
1191 if string(options.Args.Group) == group.GroupName && !group.Enabled {
1192 group.Enabled = true
1193 _, err := client.UpdateDevicePmConfigs(ctx, pmConfigs)
1194 if err != nil {
1195 return err
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001196 }
1197 }
1198 }
Rohan Agrawalbca69122020-06-17 14:59:03 +00001199 } else {
1200 return fmt.Errorf("Device '%s' does not have Group Metrics", options.Args.Id)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001201 }
1202 return nil
1203}
1204
1205func (options *DevicePmConfigGroupDisable) Execute(args []string) error {
1206
1207 conn, err := NewConnection()
1208 if err != nil {
1209 return err
1210 }
1211 defer conn.Close()
1212
1213 client := voltha.NewVolthaServiceClient(conn)
1214
David K. Bainbridge9189c632021-03-26 21:52:21 +00001215 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001216 defer cancel()
1217
1218 id := voltha.ID{Id: string(options.Args.Id)}
1219
1220 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
1221 if err != nil {
1222 return err
1223 }
1224
1225 if pmConfigs.Grouped {
Rohan Agrawalbca69122020-06-17 14:59:03 +00001226 groups := make(map[string]struct{})
1227 for _, group := range pmConfigs.Groups {
1228 groups[group.GroupName] = struct{}{}
1229 }
1230
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001231 for _, group := range pmConfigs.Groups {
Girish Gowdra610acb42021-01-27 13:33:57 -08001232 if _, have := groups[string(options.Args.Group)]; !have {
1233 return fmt.Errorf("Group Name '%s' does not exist", options.Args.Group)
1234 }
Rohan Agrawalbca69122020-06-17 14:59:03 +00001235
Girish Gowdra610acb42021-01-27 13:33:57 -08001236 if string(options.Args.Group) == group.GroupName && group.Enabled {
1237 group.Enabled = false
1238 _, err := client.UpdateDevicePmConfigs(ctx, pmConfigs)
1239 if err != nil {
1240 return err
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001241 }
1242 }
1243 }
Rohan Agrawalbca69122020-06-17 14:59:03 +00001244 } else {
1245 return fmt.Errorf("Device '%s' does not have Group Metrics", options.Args.Id)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001246 }
1247 return nil
1248}
1249
Girish Gowdra610acb42021-01-27 13:33:57 -08001250func (options *DevicePmConfigGroupFrequencySet) Execute(args []string) error {
1251
1252 conn, err := NewConnection()
1253 if err != nil {
1254 return err
1255 }
1256 defer conn.Close()
1257
1258 client := voltha.NewVolthaServiceClient(conn)
1259
David K. Bainbridge9189c632021-03-26 21:52:21 +00001260 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Girish Gowdra610acb42021-01-27 13:33:57 -08001261 defer cancel()
1262
1263 id := voltha.ID{Id: string(options.Args.Id)}
1264
1265 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
1266 if err != nil {
1267 return err
1268 }
1269
1270 if pmConfigs.Grouped {
1271 groups := make(map[string]struct{})
1272 for _, group := range pmConfigs.Groups {
1273 groups[group.GroupName] = struct{}{}
1274 }
1275
1276 for _, group := range pmConfigs.Groups {
1277 if _, have := groups[string(options.Args.Group)]; !have {
1278 return fmt.Errorf("group name '%s' does not exist", options.Args.Group)
1279 }
1280
1281 if string(options.Args.Group) == group.GroupName {
1282 if !group.Enabled {
1283 return fmt.Errorf("group '%s' is not enabled", options.Args.Group)
1284 }
1285 group.GroupFreq = uint32(options.Args.Interval.Seconds())
1286 _, err = client.UpdateDevicePmConfigs(ctx, pmConfigs)
1287 if err != nil {
1288 return err
1289 }
1290 }
1291 }
1292 } else {
1293 return fmt.Errorf("device '%s' does not have group metrics", options.Args.Id)
1294 }
1295 return nil
1296}
1297
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001298func (options *DevicePmConfigGroupList) Execute(args []string) error {
1299
1300 conn, err := NewConnection()
1301 if err != nil {
1302 return err
1303 }
1304 defer conn.Close()
1305
1306 client := voltha.NewVolthaServiceClient(conn)
1307
David K. Bainbridge9189c632021-03-26 21:52:21 +00001308 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001309 defer cancel()
1310
1311 id := voltha.ID{Id: string(options.Args.Id)}
1312
1313 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
1314 if err != nil {
1315 return err
1316 }
1317
1318 if pmConfigs.Grouped {
1319 for _, group := range pmConfigs.Groups {
1320 if group.GroupFreq == 0 {
1321 group.GroupFreq = pmConfigs.DefaultFreq
1322 }
1323 }
Rohan Agrawalbca69122020-06-17 14:59:03 +00001324 outputFormat := CharReplacer.Replace(options.Format)
1325 if outputFormat == "" {
1326 outputFormat = GetCommandOptionWithDefault("device-pm-configs", "format", DEFAULT_DEVICE_PM_CONFIG_GROUP_LIST_FORMAT)
1327 }
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001328
Rohan Agrawalbca69122020-06-17 14:59:03 +00001329 orderBy := options.OrderBy
1330 if orderBy == "" {
1331 orderBy = GetCommandOptionWithDefault("device-pm-configs", "order", "")
1332 }
1333
1334 result := CommandResult{
1335 Format: format.Format(outputFormat),
1336 Filter: options.Filter,
1337 OrderBy: orderBy,
1338 OutputAs: toOutputType(options.OutputAs),
1339 NameLimit: options.NameLimit,
1340 Data: pmConfigs.Groups,
1341 }
1342
1343 GenerateOutput(&result)
1344 } else {
1345 return fmt.Errorf("Device '%s' does not have Group Metrics", string(options.Args.Id))
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001346 }
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001347 return nil
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001348}
1349
1350func (options *DevicePmConfigGroupMetricList) Execute(args []string) error {
1351
1352 var metrics []*voltha.PmConfig
1353 conn, err := NewConnection()
1354 if err != nil {
1355 return err
1356 }
1357 defer conn.Close()
1358
1359 client := voltha.NewVolthaServiceClient(conn)
1360
David K. Bainbridge9189c632021-03-26 21:52:21 +00001361 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001362 defer cancel()
1363
1364 id := voltha.ID{Id: string(options.Args.Id)}
1365
1366 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
1367 if err != nil {
1368 return err
1369 }
1370
1371 for _, groups := range pmConfigs.Groups {
1372
1373 if string(options.Args.Group) == groups.GroupName {
1374 for _, metric := range groups.Metrics {
1375 if metric.SampleFreq == 0 && groups.GroupFreq == 0 {
1376 metric.SampleFreq = pmConfigs.DefaultFreq
1377 } else {
1378 metric.SampleFreq = groups.GroupFreq
1379 }
1380 }
1381 metrics = groups.Metrics
1382 }
1383 }
1384
1385 outputFormat := CharReplacer.Replace(options.Format)
1386 if outputFormat == "" {
1387 outputFormat = GetCommandOptionWithDefault("device-pm-configs", "format", DEFAULT_DEVICE_PM_CONFIG_METRIC_LIST_FORMAT)
1388 }
1389
1390 orderBy := options.OrderBy
1391 if orderBy == "" {
1392 orderBy = GetCommandOptionWithDefault("device-pm-configs", "order", "")
1393 }
1394
1395 result := CommandResult{
1396 Format: format.Format(outputFormat),
1397 Filter: options.Filter,
1398 OrderBy: orderBy,
1399 OutputAs: toOutputType(options.OutputAs),
1400 NameLimit: options.NameLimit,
1401 Data: metrics,
1402 }
1403
1404 GenerateOutput(&result)
1405 return nil
1406
1407}
1408
1409func (options *DevicePmConfigFrequencySet) Execute(args []string) error {
1410
1411 conn, err := NewConnection()
1412 if err != nil {
1413 return err
1414 }
1415 defer conn.Close()
1416
1417 client := voltha.NewVolthaServiceClient(conn)
1418
David K. Bainbridge9189c632021-03-26 21:52:21 +00001419 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001420 defer cancel()
1421
1422 id := voltha.ID{Id: string(options.Args.Id)}
1423
1424 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
1425 if err != nil {
1426 return err
1427 }
1428
Girish Gowdra610acb42021-01-27 13:33:57 -08001429 pmConfigs.DefaultFreq = uint32(options.Args.Interval.Seconds())
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001430
1431 _, err = client.UpdateDevicePmConfigs(ctx, pmConfigs)
1432 if err != nil {
1433 return err
1434 }
1435
1436 outputFormat := CharReplacer.Replace(options.Format)
1437 if outputFormat == "" {
1438 outputFormat = GetCommandOptionWithDefault("device-pm-configs", "format", DEFAULT_DEVICE_PM_CONFIG_GET_FORMAT)
1439 }
1440 if options.Quiet {
1441 outputFormat = "{{.Id}}"
1442 }
1443
1444 result := CommandResult{
1445 Format: format.Format(outputFormat),
1446 OutputAs: toOutputType(options.OutputAs),
1447 NameLimit: options.NameLimit,
1448 Data: pmConfigs,
1449 }
1450
1451 GenerateOutput(&result)
1452 return nil
1453
1454}
1455
kesavand3e2f9f62021-04-22 11:06:38 +05301456func (options *OnuDownloadImage) Execute(args []string) error {
1457
1458 conn, err := NewConnection()
1459 if err != nil {
1460 return err
1461 }
1462 defer conn.Close()
1463
1464 client := voltha.NewVolthaServiceClient(conn)
1465
1466 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
1467 defer cancel()
1468
1469 var devIDList []*common.ID
1470 for _, i := range options.Args.IDs {
1471
1472 devIDList = append(devIDList, &common.ID{Id: string(i)})
1473 }
1474
1475 downloadImage := voltha.DeviceImageDownloadRequest{
1476 DeviceId: devIDList,
1477 Image: &voltha.Image{
1478 Url: options.Args.Url,
1479 Crc32: options.Args.Crc,
1480 Vendor: options.Args.vendor,
1481 Version: options.Args.ImageVersion,
1482 },
1483 ActivateOnSuccess: options.Args.ActivateOnSuccess,
1484 CommitOnSuccess: options.Args.CommitOnSuccess,
1485 }
1486
1487 _, err = client.DownloadImageToDevice(ctx, &downloadImage)
1488 if err != nil {
1489 return err
1490 }
1491
1492 return nil
1493
1494}
1495
1496func (options *OnuActivateImage) Execute(args []string) error {
1497
1498 conn, err := NewConnection()
1499 if err != nil {
1500 return err
1501 }
1502 defer conn.Close()
1503
1504 client := voltha.NewVolthaServiceClient(conn)
1505
1506 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
1507 defer cancel()
1508
1509 var devIDList []*common.ID
1510 for _, i := range options.Args.IDs {
1511
1512 devIDList = append(devIDList, &common.ID{Id: string(i)})
1513 }
1514
1515 downloadImage := voltha.DeviceImageRequest{
1516 DeviceId: devIDList,
1517 Version: options.Args.ImageVersion,
1518 CommitOnSuccess: options.Args.CommitOnSuccess,
1519 }
1520
1521 _, err = client.ActivateImage(ctx, &downloadImage)
1522 if err != nil {
1523 return err
1524 }
1525
1526 return nil
1527
1528}
1529
1530func (options *OnuAbortUpgradeImage) Execute(args []string) error {
1531
1532 conn, err := NewConnection()
1533 if err != nil {
1534 return err
1535 }
1536 defer conn.Close()
1537
1538 client := voltha.NewVolthaServiceClient(conn)
1539
1540 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
1541 defer cancel()
1542
1543 var devIDList []*common.ID
1544 for _, i := range options.Args.IDs {
1545
1546 devIDList = append(devIDList, &common.ID{Id: string(i)})
1547 }
1548
1549 downloadImage := voltha.DeviceImageRequest{
1550 DeviceId: devIDList,
1551 Version: options.Args.ImageVersion,
1552 }
1553
1554 _, err = client.AbortImageUpgradeToDevice(ctx, &downloadImage)
1555 if err != nil {
1556 return err
1557 }
1558
1559 return nil
1560
1561}
1562
1563func (options *OnuCommitImage) Execute(args []string) error {
1564
1565 conn, err := NewConnection()
1566 if err != nil {
1567 return err
1568 }
1569 defer conn.Close()
1570
1571 client := voltha.NewVolthaServiceClient(conn)
1572
1573 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
1574 defer cancel()
1575
1576 var devIDList []*common.ID
1577 for _, i := range options.Args.IDs {
1578
1579 devIDList = append(devIDList, &common.ID{Id: string(i)})
1580 }
1581 downloadImage := voltha.DeviceImageRequest{
1582 DeviceId: devIDList,
1583 Version: options.Args.ImageVersion,
1584 }
1585
1586 _, err = client.CommitImage(ctx, &downloadImage)
1587 if err != nil {
1588 return err
1589 }
1590
1591 return nil
1592
1593}
1594
1595func (options *OnuListImages) Execute(args []string) error {
1596
1597 conn, err := NewConnection()
1598 if err != nil {
1599 return err
1600 }
1601 defer conn.Close()
1602
1603 client := voltha.NewVolthaServiceClient(conn)
1604
1605 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
1606 defer cancel()
1607
1608 id := common.ID{Id: string(options.Args.Id)}
1609
1610 onuImages, err := client.GetOnuImages(ctx, &id)
1611 if err != nil {
1612 return err
1613 }
1614
1615 outputFormat := CharReplacer.Replace(options.Format)
1616 if outputFormat == "" {
1617 outputFormat = GetCommandOptionWithDefault("onu-image-list", "format", ONU_IMAGE_LIST_FORMAT)
1618 }
1619
1620 if options.Quiet {
1621 outputFormat = "{{.Id}}"
1622 }
1623
1624 //TODO orderby
1625
1626 // Make sure json output prints an empty list, not "null"
1627 if onuImages.Items == nil {
1628 onuImages.Items = make([]*voltha.OnuImage, 0)
1629 }
1630
1631 result := CommandResult{
1632 Format: format.Format(outputFormat),
1633 OutputAs: toOutputType(options.OutputAs),
1634 NameLimit: options.NameLimit,
1635 Data: onuImages.Items,
1636 }
1637
1638 GenerateOutput(&result)
1639 return nil
1640
1641}
1642
1643func (options *OnuImageStatus) Execute(args []string) error {
1644
1645 conn, err := NewConnection()
1646 if err != nil {
1647 return err
1648 }
1649 defer conn.Close()
1650
1651 client := voltha.NewVolthaServiceClient(conn)
1652
1653 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
1654 defer cancel()
1655
1656 var devIDList []*common.ID
1657 for _, i := range options.Args.IDs {
1658
1659 devIDList = append(devIDList, &common.ID{Id: string(i)})
1660 }
1661
1662 imageStatusReq := voltha.DeviceImageRequest{
1663 DeviceId: devIDList,
1664 Version: options.Args.ImageVersion,
1665 }
1666 imageStatus, err := client.GetImageStatus(ctx, &imageStatusReq)
1667 if err != nil {
1668 return err
1669 }
1670
1671 outputFormat := CharReplacer.Replace(options.Format)
1672 if outputFormat == "" {
1673 outputFormat = GetCommandOptionWithDefault("device-image-list", "format", ONU_IMAGE_STATUS_FORMAT)
1674 }
1675
1676 if options.Quiet {
1677 outputFormat = "{{.Id}}"
1678 }
1679
1680 //TODO orderby
1681
1682 // Make sure json output prints an empty list, not "null"
1683 if imageStatus.DeviceImageStates == nil {
1684 imageStatus.DeviceImageStates = make([]*voltha.DeviceImageState, 0)
1685 }
1686
1687 result := CommandResult{
1688 Format: format.Format(outputFormat),
1689 OutputAs: toOutputType(options.OutputAs),
1690 NameLimit: options.NameLimit,
1691 Data: imageStatus.DeviceImageStates,
1692 }
1693
1694 GenerateOutput(&result)
1695 return nil
1696
1697}
1698
Andrea Campanella791d88b2021-01-08 13:29:00 +01001699func (options *DeviceOnuListImages) Execute(args []string) error {
1700
1701 conn, err := NewConnection()
1702 if err != nil {
1703 return err
1704 }
1705 defer conn.Close()
1706
1707 client := voltha.NewVolthaServiceClient(conn)
1708
David K. Bainbridge9189c632021-03-26 21:52:21 +00001709 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Andrea Campanella791d88b2021-01-08 13:29:00 +01001710 defer cancel()
1711
1712 id := common.ID{Id: string(options.Args.Id)}
1713
1714 imageDownloads, err := client.ListImageDownloads(ctx, &id)
1715 if err != nil {
1716 return err
1717 }
1718
1719 outputFormat := CharReplacer.Replace(options.Format)
1720 if outputFormat == "" {
1721 outputFormat = GetCommandOptionWithDefault("device-image-list", "format", DEFAULT_DEVICE_IMAGE_LIST_GET_FORMAT)
1722 }
1723
1724 if options.Quiet {
1725 outputFormat = "{{.Id}}"
1726 }
1727
1728 //TODO orderby
1729
1730 // Make sure json output prints an empty list, not "null"
1731 if imageDownloads.Items == nil {
1732 imageDownloads.Items = make([]*voltha.ImageDownload, 0)
1733 }
1734
1735 result := CommandResult{
1736 Format: format.Format(outputFormat),
1737 OutputAs: toOutputType(options.OutputAs),
1738 NameLimit: options.NameLimit,
1739 Data: imageDownloads.Items,
1740 }
1741
1742 GenerateOutput(&result)
1743 return nil
1744
1745}
1746
1747func (options *DeviceOnuDownloadImage) Execute(args []string) error {
1748
1749 conn, err := NewConnection()
1750 if err != nil {
1751 return err
1752 }
1753 defer conn.Close()
1754
1755 client := voltha.NewVolthaServiceClient(conn)
1756
David K. Bainbridge9189c632021-03-26 21:52:21 +00001757 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Andrea Campanella791d88b2021-01-08 13:29:00 +01001758 defer cancel()
1759
1760 downloadImage := voltha.ImageDownload{
1761 Id: string(options.Args.Id),
1762 Name: options.Args.Name,
1763 Url: options.Args.Url,
1764 Crc: options.Args.Crc,
1765 LocalDir: options.Args.LocalDir,
1766 }
1767
1768 _, err = client.DownloadImage(ctx, &downloadImage)
1769 if err != nil {
1770 return err
1771 }
1772
1773 return nil
1774
1775}
1776
1777func (options *DeviceOnuActivateImageUpdate) Execute(args []string) error {
1778
1779 conn, err := NewConnection()
1780 if err != nil {
1781 return err
1782 }
1783 defer conn.Close()
1784
1785 client := voltha.NewVolthaServiceClient(conn)
1786
David K. Bainbridge9189c632021-03-26 21:52:21 +00001787 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Andrea Campanella791d88b2021-01-08 13:29:00 +01001788 defer cancel()
1789
1790 downloadImage := voltha.ImageDownload{
1791 Id: string(options.Args.Id),
1792 Name: options.Args.Name,
1793 ImageVersion: options.Args.ImageVersion,
1794 SaveConfig: options.Args.SaveConfig,
1795 LocalDir: options.Args.LocalDir,
1796 }
1797
1798 _, err = client.ActivateImageUpdate(ctx, &downloadImage)
1799 if err != nil {
1800 return err
1801 }
1802
1803 return nil
1804
1805}
1806
Scott Baker9173ed82020-05-19 08:30:12 -07001807type ReturnValueRow struct {
1808 Name string `json:"name"`
1809 Result interface{} `json:"result"`
1810}
1811
kesavand8ec4fc02021-01-27 09:10:22 -05001812func (options *DeviceGetPortStats) Execute(args []string) error {
1813 conn, err := NewConnection()
1814 if err != nil {
1815 return err
1816 }
1817 defer conn.Close()
1818 client := extension.NewExtensionClient(conn)
1819 var portType extension.GetOltPortCounters_PortType
1820
1821 if options.Args.PortType == "pon" {
1822 portType = extension.GetOltPortCounters_Port_PON_OLT
1823 } else if options.Args.PortType == "nni" {
1824
1825 portType = extension.GetOltPortCounters_Port_ETHERNET_NNI
1826 } else {
1827 return fmt.Errorf("expected interface type pon/nni, provided %s", options.Args.PortType)
1828 }
1829
1830 singleGetValReq := extension.SingleGetValueRequest{
1831 TargetId: string(options.Args.Id),
1832 Request: &extension.GetValueRequest{
1833 Request: &extension.GetValueRequest_OltPortInfo{
1834 OltPortInfo: &extension.GetOltPortCounters{
1835 PortNo: options.Args.PortNo,
1836 PortType: portType,
1837 },
1838 },
1839 },
1840 }
1841
David K. Bainbridge9189c632021-03-26 21:52:21 +00001842 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
kesavand8ec4fc02021-01-27 09:10:22 -05001843 defer cancel()
1844 rv, err := client.GetExtValue(ctx, &singleGetValReq)
1845 if err != nil {
1846 Error.Printf("Error getting value on device Id %s,err=%s\n", options.Args.Id, ErrorToString(err))
1847 return err
1848 }
1849
1850 if rv.Response.Status != extension.GetValueResponse_OK {
1851 return fmt.Errorf("failed to get port stats %v", rv.Response.ErrReason.String())
1852 }
1853
1854 outputFormat := CharReplacer.Replace(options.Format)
1855 if outputFormat == "" {
1856 outputFormat = GetCommandOptionWithDefault("device-get-port-status", "format", DEFAULT_DEVICE_GET_PORT_STATUS_FORMAT)
1857 }
1858
1859 result := CommandResult{
1860 Format: format.Format(outputFormat),
1861 OutputAs: toOutputType(options.OutputAs),
1862 NameLimit: options.NameLimit,
1863 Data: rv.GetResponse().GetPortCoutners(),
1864 }
1865 GenerateOutput(&result)
1866 return nil
1867}
1868
kesavand6d1131f2021-02-05 22:38:15 +05301869func (options *UniStatus) Execute(args []string) error {
1870 conn, err := NewConnection()
1871 if err != nil {
1872 return err
1873 }
1874 defer conn.Close()
1875 client := extension.NewExtensionClient(conn)
1876
1877 singleGetValReq := extension.SingleGetValueRequest{
1878 TargetId: string(options.Args.Id),
1879 Request: &extension.GetValueRequest{
1880 Request: &extension.GetValueRequest_UniInfo{
1881 UniInfo: &extension.GetOnuUniInfoRequest{
1882 UniIndex: options.Args.UniIndex,
1883 },
1884 },
1885 },
1886 }
David K. Bainbridge9189c632021-03-26 21:52:21 +00001887 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
kesavand6d1131f2021-02-05 22:38:15 +05301888 defer cancel()
1889 rv, err := client.GetExtValue(ctx, &singleGetValReq)
1890 if err != nil {
1891 Error.Printf("Error getting value on device Id %s,err=%s\n", options.Args.Id, ErrorToString(err))
1892 return err
1893 }
1894 if rv.Response.Status != extension.GetValueResponse_OK {
1895 return fmt.Errorf("failed to get uni status %v", rv.Response.ErrReason.String())
1896 }
1897 outputFormat := CharReplacer.Replace(options.Format)
1898 if outputFormat == "" {
1899 outputFormat = GetCommandOptionWithDefault("device-get-uni-status", "format", DEFAULT_DEVICE_GET_UNI_STATUS_FORMAT)
1900 }
1901 result := CommandResult{
1902 Format: format.Format(outputFormat),
1903 OutputAs: toOutputType(options.OutputAs),
1904 NameLimit: options.NameLimit,
1905 Data: rv.GetResponse().GetUniInfo(),
1906 }
1907 GenerateOutput(&result)
1908 return nil
1909}
1910
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -08001911/*Device get Onu Distance */
1912func (options *DeviceGetExtValue) Execute(args []string) error {
1913 conn, err := NewConnection()
1914 if err != nil {
1915 return err
1916 }
1917 defer conn.Close()
1918
Scott Baker9173ed82020-05-19 08:30:12 -07001919 client := voltha.NewVolthaServiceClient(conn)
1920
1921 valueflag, okay := common.ValueType_Type_value[string(options.Args.Valueflag)]
1922 if !okay {
1923 Error.Printf("Unknown valueflag %s\n", options.Args.Valueflag)
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -08001924 }
1925
Scott Baker9173ed82020-05-19 08:30:12 -07001926 val := voltha.ValueSpecifier{Id: string(options.Args.Id), Value: common.ValueType_Type(valueflag)}
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -08001927
David K. Bainbridge9189c632021-03-26 21:52:21 +00001928 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -08001929 defer cancel()
1930
Scott Baker9173ed82020-05-19 08:30:12 -07001931 rv, err := client.GetExtValue(ctx, &val)
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -08001932 if err != nil {
1933 Error.Printf("Error getting value on device Id %s,err=%s\n", options.Args.Id, ErrorToString(err))
1934 return err
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -08001935 }
1936
Scott Baker9173ed82020-05-19 08:30:12 -07001937 var rows []ReturnValueRow
1938 for name, num := range common.ValueType_Type_value {
1939 if num == 0 {
1940 // EMPTY is not a real value
1941 continue
1942 }
1943 if (rv.Error & uint32(num)) != 0 {
1944 row := ReturnValueRow{Name: name, Result: "Error"}
1945 rows = append(rows, row)
1946 }
1947 if (rv.Unsupported & uint32(num)) != 0 {
1948 row := ReturnValueRow{Name: name, Result: "Unsupported"}
1949 rows = append(rows, row)
1950 }
1951 if (rv.Set & uint32(num)) != 0 {
1952 switch name {
1953 case "DISTANCE":
1954 row := ReturnValueRow{Name: name, Result: rv.Distance}
1955 rows = append(rows, row)
1956 default:
1957 row := ReturnValueRow{Name: name, Result: "Unimplemented-in-voltctl"}
1958 rows = append(rows, row)
1959 }
1960 }
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -08001961 }
1962
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -08001963 outputFormat := CharReplacer.Replace(options.Format)
1964 if outputFormat == "" {
1965 outputFormat = GetCommandOptionWithDefault("device-value-get", "format", DEFAULT_DEVICE_VALUE_GET_FORMAT)
1966 }
1967
1968 result := CommandResult{
1969 Format: format.Format(outputFormat),
1970 OutputAs: toOutputType(options.OutputAs),
1971 NameLimit: options.NameLimit,
Scott Baker9173ed82020-05-19 08:30:12 -07001972 Data: rows,
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -08001973 }
1974 GenerateOutput(&result)
1975 return nil
1976}