blob: b0f0ab98c1c3fbde185314a0abfac9ca27769749 [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}}"
ssiddiqui7bc89e92021-05-20 20:58:02 +053051 ONU_IMAGE_LIST_FORMAT = "table{{.Version}}\t{{.IsCommited}}\t{{.IsActive}}\t{{.IsValid}}\t{{.ProductCode}}\t{{.Hash}}"
52 ONU_IMAGE_STATUS_FORMAT = "table{{.DeviceId}}\t{{.ImageState.Version}}\t{{.ImageState.DownloadState}}\t{{.ImageState.Reason}}\t{{.ImageState.ImageState}}\t"
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}}`
Girish Gowdra4f5ce7c2021-04-29 18:53:21 -070070 DEFAULT_ONU_PON_OPTICAL_INFO_STATUS_FORMAT = `
71 POWER_FEED_VOLTAGE__VOLTS: {{.PowerFeedVoltage}}
72 RECEIVED_OPTICAL_POWER__dBm: {{.ReceivedOpticalPower}}
73 MEAN_OPTICAL_LAUNCH_POWER__dBm: {{.MeanOpticalLaunchPower}}
74 LASER_BIAS_CURRENT__mA: {{.LaserBiasCurrent}}
75 TEMPERATURE__Celsius: {{.Temperature}}`
Zack Williamse940c7a2019-08-21 14:25:39 -070076)
77
78type DeviceList struct {
79 ListOutputOptions
80}
81
82type DeviceCreate struct {
David Bainbridge1a514392020-06-23 11:12:51 -070083 DeviceType string `short:"t" required:"true" long:"devicetype" description:"Device type"`
David Bainbridge835dd0e2020-04-01 10:30:09 -070084 MACAddress string `short:"m" long:"macaddress" default:"" description:"MAC Address"`
Zack Williamse940c7a2019-08-21 14:25:39 -070085 IPAddress string `short:"i" long:"ipaddress" default:"" description:"IP Address"`
86 HostAndPort string `short:"H" long:"hostandport" default:"" description:"Host and port"`
87}
88
89type DeviceId string
90
Rohan Agrawal9228d2f2020-06-03 07:48:50 +000091type MetricName string
92type GroupName string
kesavand12cd8eb2020-01-20 22:25:22 -050093type PortNum uint32
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -080094type ValueFlag string
kesavand12cd8eb2020-01-20 22:25:22 -050095
Zack Williamse940c7a2019-08-21 14:25:39 -070096type DeviceDelete struct {
Himani Chawla9933ddc2020-10-12 23:53:27 +053097 Force bool `long:"force" description:"Delete device forcefully"`
98 Args struct {
Zack Williamse940c7a2019-08-21 14:25:39 -070099 Ids []DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
100 } `positional-args:"yes"`
101}
102
103type DeviceEnable struct {
104 Args struct {
105 Ids []DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
106 } `positional-args:"yes"`
107}
108
109type DeviceDisable struct {
110 Args struct {
111 Ids []DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
112 } `positional-args:"yes"`
113}
114
115type DeviceReboot struct {
116 Args struct {
117 Ids []DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
118 } `positional-args:"yes"`
119}
120
121type DeviceFlowList struct {
122 ListOutputOptions
Maninder045921e2020-09-29 16:46:02 +0530123 FlowIdOptions
Zack Williamse940c7a2019-08-21 14:25:39 -0700124 Args struct {
125 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
126 } `positional-args:"yes"`
127}
128
129type DevicePortList struct {
130 ListOutputOptions
131 Args struct {
132 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
133 } `positional-args:"yes"`
134}
135
136type DeviceInspect struct {
137 OutputOptionsJson
138 Args struct {
139 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
140 } `positional-args:"yes"`
141}
142
kesavand12cd8eb2020-01-20 22:25:22 -0500143type DevicePortEnable struct {
144 Args struct {
145 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
146 PortId PortNum `positional-arg-name:"PORT_NUMBER" required:"yes"`
147 } `positional-args:"yes"`
148}
149
150type DevicePortDisable struct {
151 Args struct {
152 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
153 PortId PortNum `positional-arg-name:"PORT_NUMBER" required:"yes"`
154 } `positional-args:"yes"`
155}
156
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000157type DevicePmConfigsGet struct {
158 ListOutputOptions
159 Args struct {
160 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
161 } `positional-args:"yes"`
162}
163
164type DevicePmConfigMetricList struct {
165 ListOutputOptions
166 Args struct {
167 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
168 } `positional-args:"yes"`
169}
170
171type DevicePmConfigGroupList struct {
172 ListOutputOptions
173 Args struct {
174 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
175 } `positional-args:"yes"`
176}
177
178type DevicePmConfigGroupMetricList struct {
179 ListOutputOptions
180 Args struct {
181 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
182 Group GroupName `positional-arg-name:"GROUP_NAME" required:"yes"`
183 } `positional-args:"yes"`
184}
185
186type DevicePmConfigFrequencySet struct {
187 OutputOptions
188 Args struct {
Girish Gowdra610acb42021-01-27 13:33:57 -0800189 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
190 Interval time.Duration `positional-arg-name:"INTERVAL" required:"yes"`
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000191 } `positional-args:"yes"`
192}
193
194type DevicePmConfigMetricEnable struct {
195 Args struct {
196 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
197 Metrics []MetricName `positional-arg-name:"METRIC_NAME" required:"yes"`
198 } `positional-args:"yes"`
199}
200
201type DevicePmConfigMetricDisable struct {
202 Args struct {
203 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
204 Metrics []MetricName `positional-arg-name:"METRIC_NAME" required:"yes"`
205 } `positional-args:"yes"`
206}
207
208type DevicePmConfigGroupEnable struct {
209 Args struct {
Girish Gowdra610acb42021-01-27 13:33:57 -0800210 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
211 Group GroupName `positional-arg-name:"GROUP_NAME" required:"yes"`
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000212 } `positional-args:"yes"`
213}
214
215type DevicePmConfigGroupDisable struct {
216 Args struct {
Girish Gowdra610acb42021-01-27 13:33:57 -0800217 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
218 Group GroupName `positional-arg-name:"GROUP_NAME" required:"yes"`
219 } `positional-args:"yes"`
220}
221
222type DevicePmConfigGroupFrequencySet struct {
223 OutputOptions
224 Args struct {
225 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
226 Group GroupName `positional-arg-name:"GROUP_NAME" required:"yes"`
227 Interval time.Duration `positional-arg-name:"INTERVAL" required:"yes"`
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000228 } `positional-args:"yes"`
229}
230
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -0800231type DeviceGetExtValue struct {
232 ListOutputOptions
233 Args struct {
234 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
235 Valueflag ValueFlag `positional-arg-name:"VALUE_FLAG" required:"yes"`
236 } `positional-args:"yes"`
237}
Rohan Agrawald7df3772020-06-29 11:23:36 +0000238
239type DevicePmConfigSetMaxSkew struct {
240 Args struct {
241 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
242 MaxSkew uint32 `positional-arg-name:"MAX_SKEW" required:"yes"`
243 } `positional-args:"yes"`
244}
245
Andrea Campanella791d88b2021-01-08 13:29:00 +0100246type DeviceOnuListImages struct {
247 ListOutputOptions
248 Args struct {
249 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
250 } `positional-args:"yes"`
251}
252
253type DeviceOnuDownloadImage struct {
254 Args struct {
255 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
256 Name string `positional-arg-name:"IMAGE_NAME" required:"yes"`
257 Url string `positional-arg-name:"IMAGE_URL" required:"yes"`
258 ImageVersion string `positional-arg-name:"IMAGE_VERSION" required:"yes"`
259 Crc uint32 `positional-arg-name:"IMAGE_CRC" required:"yes"`
260 LocalDir string `positional-arg-name:"IMAGE_LOCAL_DIRECTORY"`
261 } `positional-args:"yes"`
262}
263
264type DeviceOnuActivateImageUpdate struct {
265 Args struct {
266 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
267 Name string `positional-arg-name:"IMAGE_NAME" required:"yes"`
268 ImageVersion string `positional-arg-name:"IMAGE_VERSION" required:"yes"`
269 SaveConfig bool `positional-arg-name:"SAVE_EXISTING_CONFIG"`
270 LocalDir string `positional-arg-name:"IMAGE_LOCAL_DIRECTORY"`
Andrea Campanella7b2ecf42021-02-25 12:27:15 +0100271 } `positional-args:"yes"`
kesavand8ec4fc02021-01-27 09:10:22 -0500272}
kesavand3e2f9f62021-04-22 11:06:38 +0530273
274type OnuDownloadImage struct {
275 Args struct {
276 ImageVersion string `positional-arg-name:"IMAGE_VERSION" required:"yes"`
277 Url string `positional-arg-name:"IMAGE_URL" required:"yes"`
ssiddiqui7bc89e92021-05-20 20:58:02 +0530278 Vendor string `positional-arg-name:"IMAGE_VENDOR"`
kesavand3e2f9f62021-04-22 11:06:38 +0530279 ActivateOnSuccess bool `positional-arg-name:"IMAGE_ACTIVATE_ON_SUCCESS"`
280 CommitOnSuccess bool `positional-arg-name:"IMAGE_COMMIT_ON_SUCCESS"`
281 Crc uint32 `positional-arg-name:"IMAGE_CRC"`
282 IDs []DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
283 } `positional-args:"yes"`
284}
285
286type OnuActivateImage struct {
287 Args struct {
288 ImageVersion string `positional-arg-name:"IMAGE_VERSION" required:"yes"`
289 CommitOnSuccess bool `positional-arg-name:"IMAGE_COMMIT_ON_SUCCESS"`
290 IDs []DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
291 } `positional-args:"yes"`
292}
293
294type OnuAbortUpgradeImage struct {
295 Args struct {
296 ImageVersion string `positional-arg-name:"IMAGE_VERSION" required:"yes"`
297 IDs []DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
298 } `positional-args:"yes"`
299}
300
301type OnuCommitImage struct {
302 Args struct {
303 ImageVersion string `positional-arg-name:"IMAGE_VERSION" required:"yes"`
304 IDs []DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
305 } `positional-args:"yes"`
306}
307
308type OnuImageStatus struct {
309 ListOutputOptions
310 Args struct {
311 ImageVersion string `positional-arg-name:"IMAGE_VERSION" required:"yes"`
312 IDs []DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
313 } `positional-args:"yes"`
314}
315
316type OnuListImages struct {
317 ListOutputOptions
318 Args struct {
319 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
320 } `positional-args:"yes"`
321}
322
kesavand8ec4fc02021-01-27 09:10:22 -0500323type DeviceGetPortStats struct {
324 ListOutputOptions
325 Args struct {
326 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
327 PortNo uint32 `positional-arg-name:"PORT_NO" required:"yes"`
328 PortType string `positional-arg-name:"PORT_TYPE" required:"yes"`
Andrea Campanella791d88b2021-01-08 13:29:00 +0100329 } `positional-args:"yes"`
330}
kesavand6d1131f2021-02-05 22:38:15 +0530331type UniStatus struct {
332 ListOutputOptions
333 Args struct {
334 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
335 UniIndex uint32 `positional-arg-name:"UNI_INDEX" required:"yes"`
336 } `positional-args:"yes"`
337}
Girish Gowdra4f5ce7c2021-04-29 18:53:21 -0700338type OnuPonOpticalInfo struct {
339 ListOutputOptions
340 Args struct {
341 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
342 } `positional-args:"yes"`
343}
Zack Williamse940c7a2019-08-21 14:25:39 -0700344type DeviceOpts struct {
345 List DeviceList `command:"list"`
346 Create DeviceCreate `command:"create"`
347 Delete DeviceDelete `command:"delete"`
348 Enable DeviceEnable `command:"enable"`
349 Disable DeviceDisable `command:"disable"`
350 Flows DeviceFlowList `command:"flows"`
kesavand12cd8eb2020-01-20 22:25:22 -0500351 Port struct {
352 List DevicePortList `command:"list"`
353 Enable DevicePortEnable `command:"enable"`
354 Disable DevicePortDisable `command:"disable"`
355 } `command:"port"`
356 Inspect DeviceInspect `command:"inspect"`
357 Reboot DeviceReboot `command:"reboot"`
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -0800358 Value struct {
359 Get DeviceGetExtValue `command:"get"`
360 } `command:"value"`
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000361 PmConfig struct {
Rohan Agrawald7df3772020-06-29 11:23:36 +0000362 Get DevicePmConfigsGet `command:"get"`
363 MaxSkew struct {
364 Set DevicePmConfigSetMaxSkew `command:"set"`
365 } `command:"maxskew"`
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000366 Frequency struct {
367 Set DevicePmConfigFrequencySet `command:"set"`
368 } `command:"frequency"`
369 Metric struct {
370 List DevicePmConfigMetricList `command:"list"`
371 Enable DevicePmConfigMetricEnable `command:"enable"`
372 Disable DevicePmConfigMetricDisable `command:"disable"`
373 } `command:"metric"`
374 Group struct {
Girish Gowdra610acb42021-01-27 13:33:57 -0800375 List DevicePmConfigGroupList `command:"list"`
376 Enable DevicePmConfigGroupEnable `command:"enable"`
377 Disable DevicePmConfigGroupDisable `command:"disable"`
378 Set DevicePmConfigGroupFrequencySet `command:"set"`
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000379 } `command:"group"`
380 GroupMetric struct {
381 List DevicePmConfigGroupMetricList `command:"list"`
382 } `command:"groupmetric"`
383 } `command:"pmconfig"`
Andrea Campanella791d88b2021-01-08 13:29:00 +0100384 Image struct {
385 Get DeviceOnuListImages `command:"list"`
386 Download DeviceOnuDownloadImage `command:"download"`
387 Activate DeviceOnuActivateImageUpdate `command:"activate"`
388 } `command:"image"`
kesavand3e2f9f62021-04-22 11:06:38 +0530389 DownloadImage struct {
390 Download OnuDownloadImage `command:"download"`
391 Activate OnuActivateImage `command:"activate"`
392 Commit OnuCommitImage `command:"commit"`
393 AbortUpgrade OnuAbortUpgradeImage `command:"abort"`
394 Status OnuImageStatus `command:"status"`
395 List OnuListImages `command:"list" `
396 } `command:"onuimage"`
kesavand8ec4fc02021-01-27 09:10:22 -0500397 GetExtVal struct {
Girish Gowdra4f5ce7c2021-04-29 18:53:21 -0700398 Stats DeviceGetPortStats `command:"portstats"`
399 UniStatus UniStatus `command:"unistatus"`
400 OpticalInfo OnuPonOpticalInfo `command:"onu_pon_optical_info"`
kesavand8ec4fc02021-01-27 09:10:22 -0500401 } `command:"getextval"`
Zack Williamse940c7a2019-08-21 14:25:39 -0700402}
403
404var deviceOpts = DeviceOpts{}
405
406func RegisterDeviceCommands(parser *flags.Parser) {
David Bainbridge12f036f2019-10-15 22:09:04 +0000407 if _, err := parser.AddCommand("device", "device commands", "Commands to query and manipulate VOLTHA devices", &deviceOpts); err != nil {
David Bainbridgea6722342019-10-24 23:55:53 +0000408 Error.Fatalf("Unexpected error while attempting to register device commands : %s", err)
David Bainbridge12f036f2019-10-15 22:09:04 +0000409 }
Zack Williamse940c7a2019-08-21 14:25:39 -0700410}
411
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000412func (i *MetricName) Complete(match string) []flags.Completion {
413 conn, err := NewConnection()
414 if err != nil {
415 return nil
416 }
417 defer conn.Close()
418
419 client := voltha.NewVolthaServiceClient(conn)
420
421 var deviceId string
422found:
423 for i := len(os.Args) - 1; i >= 0; i -= 1 {
424 switch os.Args[i] {
425 case "enable":
426 fallthrough
427 case "disable":
428 if len(os.Args) > i+1 {
429 deviceId = os.Args[i+1]
430 } else {
431 return nil
432 }
433 break found
434 default:
435 }
436 }
437
438 if len(deviceId) == 0 {
439 return nil
440 }
441
David K. Bainbridge9189c632021-03-26 21:52:21 +0000442 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000443 defer cancel()
444
445 id := voltha.ID{Id: string(deviceId)}
446
447 pmconfigs, err := client.ListDevicePmConfigs(ctx, &id)
448
449 if err != nil {
450 return nil
451 }
452
453 list := make([]flags.Completion, 0)
454 for _, metrics := range pmconfigs.Metrics {
455 if strings.HasPrefix(metrics.Name, match) {
456 list = append(list, flags.Completion{Item: metrics.Name})
457 }
458 }
459
460 return list
461}
462
463func (i *GroupName) Complete(match string) []flags.Completion {
464 conn, err := NewConnection()
465 if err != nil {
466 return nil
467 }
468 defer conn.Close()
469
470 client := voltha.NewVolthaServiceClient(conn)
471
472 var deviceId string
473found:
474 for i := len(os.Args) - 1; i >= 0; i -= 1 {
475 switch os.Args[i] {
476 case "list":
477 fallthrough
478 case "enable":
479 fallthrough
480 case "disable":
481 if len(os.Args) > i+1 {
482 deviceId = os.Args[i+1]
483 } else {
484 return nil
485 }
486 break found
487 default:
488 }
489 }
490
491 if len(deviceId) == 0 {
492 return nil
493 }
494
David K. Bainbridge9189c632021-03-26 21:52:21 +0000495 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000496 defer cancel()
497
498 id := voltha.ID{Id: string(deviceId)}
499
500 pmconfigs, err := client.ListDevicePmConfigs(ctx, &id)
501
502 if err != nil {
503 return nil
504 }
505
506 list := make([]flags.Completion, 0)
507 for _, group := range pmconfigs.Groups {
508 if strings.HasPrefix(group.GroupName, match) {
509 list = append(list, flags.Completion{Item: group.GroupName})
510 }
511 }
512 return list
513}
514
kesavand12cd8eb2020-01-20 22:25:22 -0500515func (i *PortNum) Complete(match string) []flags.Completion {
516 conn, err := NewConnection()
517 if err != nil {
518 return nil
519 }
520 defer conn.Close()
521
Scott Baker9173ed82020-05-19 08:30:12 -0700522 client := voltha.NewVolthaServiceClient(conn)
kesavand12cd8eb2020-01-20 22:25:22 -0500523
524 /*
525 * The command line args when completing for PortNum will be a DeviceId
526 * followed by one or more PortNums. So walk the argument list from the
527 * end and find the first argument that is enable/disable as those are
528 * the subcommands that come before the positional arguments. It would
529 * be nice if this package gave us the list of optional arguments
530 * already parsed.
531 */
532 var deviceId string
533found:
534 for i := len(os.Args) - 1; i >= 0; i -= 1 {
535 switch os.Args[i] {
536 case "enable":
537 fallthrough
538 case "disable":
539 if len(os.Args) > i+1 {
540 deviceId = os.Args[i+1]
541 } else {
542 return nil
543 }
544 break found
545 default:
546 }
547 }
548
549 if len(deviceId) == 0 {
550 return nil
551 }
552
David K. Bainbridge9189c632021-03-26 21:52:21 +0000553 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
kesavand12cd8eb2020-01-20 22:25:22 -0500554 defer cancel()
kesavand12cd8eb2020-01-20 22:25:22 -0500555
Scott Baker9173ed82020-05-19 08:30:12 -0700556 id := voltha.ID{Id: string(deviceId)}
kesavand12cd8eb2020-01-20 22:25:22 -0500557
Scott Baker9173ed82020-05-19 08:30:12 -0700558 ports, err := client.ListDevicePorts(ctx, &id)
kesavand12cd8eb2020-01-20 22:25:22 -0500559 if err != nil {
560 return nil
561 }
562
563 list := make([]flags.Completion, 0)
Scott Baker9173ed82020-05-19 08:30:12 -0700564 for _, item := range ports.Items {
565 pn := strconv.FormatUint(uint64(item.PortNo), 10)
kesavand12cd8eb2020-01-20 22:25:22 -0500566 if strings.HasPrefix(pn, match) {
567 list = append(list, flags.Completion{Item: pn})
568 }
569 }
570
571 return list
572}
573
Zack Williamse940c7a2019-08-21 14:25:39 -0700574func (i *DeviceId) Complete(match string) []flags.Completion {
575 conn, err := NewConnection()
576 if err != nil {
577 return nil
578 }
579 defer conn.Close()
580
Scott Baker9173ed82020-05-19 08:30:12 -0700581 client := voltha.NewVolthaServiceClient(conn)
Zack Williamse940c7a2019-08-21 14:25:39 -0700582
David K. Bainbridge9189c632021-03-26 21:52:21 +0000583 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Zack Williamse940c7a2019-08-21 14:25:39 -0700584 defer cancel()
585
Scott Baker9173ed82020-05-19 08:30:12 -0700586 devices, err := client.ListDevices(ctx, &empty.Empty{})
Zack Williamse940c7a2019-08-21 14:25:39 -0700587 if err != nil {
588 return nil
589 }
590
591 list := make([]flags.Completion, 0)
Scott Baker9173ed82020-05-19 08:30:12 -0700592 for _, item := range devices.Items {
593 if strings.HasPrefix(item.Id, match) {
594 list = append(list, flags.Completion{Item: item.Id})
Zack Williamse940c7a2019-08-21 14:25:39 -0700595 }
596 }
597
598 return list
599}
600
601func (options *DeviceList) Execute(args []string) error {
602
603 conn, err := NewConnection()
604 if err != nil {
605 return err
606 }
607 defer conn.Close()
608
Scott Baker9173ed82020-05-19 08:30:12 -0700609 client := voltha.NewVolthaServiceClient(conn)
Zack Williamse940c7a2019-08-21 14:25:39 -0700610
David K. Bainbridge9189c632021-03-26 21:52:21 +0000611 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Zack Williamse940c7a2019-08-21 14:25:39 -0700612 defer cancel()
613
Scott Baker9173ed82020-05-19 08:30:12 -0700614 devices, err := client.ListDevices(ctx, &empty.Empty{})
Zack Williamse940c7a2019-08-21 14:25:39 -0700615 if err != nil {
616 return err
617 }
618
619 outputFormat := CharReplacer.Replace(options.Format)
620 if outputFormat == "" {
David Bainbridgea6722342019-10-24 23:55:53 +0000621 outputFormat = GetCommandOptionWithDefault("device-list", "format", DEFAULT_DEVICE_FORMAT)
Zack Williamse940c7a2019-08-21 14:25:39 -0700622 }
623 if options.Quiet {
624 outputFormat = "{{.Id}}"
625 }
626
David Bainbridgea6722342019-10-24 23:55:53 +0000627 orderBy := options.OrderBy
628 if orderBy == "" {
629 orderBy = GetCommandOptionWithDefault("device-list", "order", "")
630 }
631
Scott Baker9173ed82020-05-19 08:30:12 -0700632 // Make sure json output prints an empty list, not "null"
633 if devices.Items == nil {
634 devices.Items = make([]*voltha.Device, 0)
Zack Williamse940c7a2019-08-21 14:25:39 -0700635 }
636
637 result := CommandResult{
638 Format: format.Format(outputFormat),
639 Filter: options.Filter,
David Bainbridgea6722342019-10-24 23:55:53 +0000640 OrderBy: orderBy,
Zack Williamse940c7a2019-08-21 14:25:39 -0700641 OutputAs: toOutputType(options.OutputAs),
642 NameLimit: options.NameLimit,
Scott Baker9173ed82020-05-19 08:30:12 -0700643 Data: devices.Items,
Zack Williamse940c7a2019-08-21 14:25:39 -0700644 }
645
646 GenerateOutput(&result)
647 return nil
648}
649
650func (options *DeviceCreate) Execute(args []string) error {
651
Scott Baker9173ed82020-05-19 08:30:12 -0700652 device := voltha.Device{}
Zack Williamse940c7a2019-08-21 14:25:39 -0700653 if options.HostAndPort != "" {
Scott Baker9173ed82020-05-19 08:30:12 -0700654 device.Address = &voltha.Device_HostAndPort{HostAndPort: options.HostAndPort}
Zack Williamse940c7a2019-08-21 14:25:39 -0700655 } else if options.IPAddress != "" {
Scott Baker9173ed82020-05-19 08:30:12 -0700656 device.Address = &voltha.Device_Ipv4Address{Ipv4Address: options.IPAddress}
Hardik Windlassce1de342020-02-04 21:58:07 +0000657 }
658 if options.MACAddress != "" {
Scott Baker9173ed82020-05-19 08:30:12 -0700659 device.MacAddress = strings.ToLower(options.MACAddress)
Zack Williamse940c7a2019-08-21 14:25:39 -0700660 }
661 if options.DeviceType != "" {
Scott Baker9173ed82020-05-19 08:30:12 -0700662 device.Type = options.DeviceType
Zack Williamse940c7a2019-08-21 14:25:39 -0700663 }
664
665 conn, err := NewConnection()
666 if err != nil {
667 return err
668 }
669 defer conn.Close()
670
Scott Baker9173ed82020-05-19 08:30:12 -0700671 client := voltha.NewVolthaServiceClient(conn)
Zack Williamse940c7a2019-08-21 14:25:39 -0700672
David K. Bainbridge9189c632021-03-26 21:52:21 +0000673 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Zack Williamse940c7a2019-08-21 14:25:39 -0700674 defer cancel()
675
Scott Baker9173ed82020-05-19 08:30:12 -0700676 createdDevice, err := client.CreateDevice(ctx, &device)
Zack Williamse940c7a2019-08-21 14:25:39 -0700677 if err != nil {
678 return err
Zack Williamse940c7a2019-08-21 14:25:39 -0700679 }
680
Scott Baker9173ed82020-05-19 08:30:12 -0700681 fmt.Printf("%s\n", createdDevice.Id)
Zack Williamse940c7a2019-08-21 14:25:39 -0700682
683 return nil
684}
685
686func (options *DeviceDelete) Execute(args []string) error {
687
688 conn, err := NewConnection()
689 if err != nil {
690 return err
691 }
692 defer conn.Close()
693
Scott Baker9173ed82020-05-19 08:30:12 -0700694 client := voltha.NewVolthaServiceClient(conn)
David Bainbridge7052fe82020-03-25 10:37:00 -0700695 var lastErr error
Zack Williamse940c7a2019-08-21 14:25:39 -0700696 for _, i := range options.Args.Ids {
David K. Bainbridge9189c632021-03-26 21:52:21 +0000697 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Zack Williamse940c7a2019-08-21 14:25:39 -0700698 defer cancel()
699
Scott Baker9173ed82020-05-19 08:30:12 -0700700 id := voltha.ID{Id: string(i)}
Himani Chawla9933ddc2020-10-12 23:53:27 +0530701 if options.Force {
702 _, err = client.ForceDeleteDevice(ctx, &id)
703 } else {
704 _, err = client.DeleteDevice(ctx, &id)
705 }
Scott Baker9173ed82020-05-19 08:30:12 -0700706
Zack Williamse940c7a2019-08-21 14:25:39 -0700707 if err != nil {
David Bainbridge0f758d42019-10-26 05:17:48 +0000708 Error.Printf("Error while deleting '%s': %s\n", i, err)
David Bainbridge7052fe82020-03-25 10:37:00 -0700709 lastErr = err
Zack Williamse940c7a2019-08-21 14:25:39 -0700710 continue
Zack Williamse940c7a2019-08-21 14:25:39 -0700711 }
712 fmt.Printf("%s\n", i)
713 }
714
David Bainbridge7052fe82020-03-25 10:37:00 -0700715 if lastErr != nil {
716 return NoReportErr
717 }
Zack Williamse940c7a2019-08-21 14:25:39 -0700718 return nil
719}
720
721func (options *DeviceEnable) Execute(args []string) error {
722 conn, err := NewConnection()
723 if err != nil {
724 return err
725 }
726 defer conn.Close()
727
Scott Baker9173ed82020-05-19 08:30:12 -0700728 client := voltha.NewVolthaServiceClient(conn)
Zack Williamse940c7a2019-08-21 14:25:39 -0700729
David Bainbridge7052fe82020-03-25 10:37:00 -0700730 var lastErr error
Zack Williamse940c7a2019-08-21 14:25:39 -0700731 for _, i := range options.Args.Ids {
David K. Bainbridge9189c632021-03-26 21:52:21 +0000732 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Zack Williamse940c7a2019-08-21 14:25:39 -0700733 defer cancel()
734
Scott Baker9173ed82020-05-19 08:30:12 -0700735 id := voltha.ID{Id: string(i)}
736
737 _, err := client.EnableDevice(ctx, &id)
Zack Williamse940c7a2019-08-21 14:25:39 -0700738 if err != nil {
David Bainbridge0f758d42019-10-26 05:17:48 +0000739 Error.Printf("Error while enabling '%s': %s\n", i, err)
David Bainbridge7052fe82020-03-25 10:37:00 -0700740 lastErr = err
Zack Williamse940c7a2019-08-21 14:25:39 -0700741 continue
Zack Williamse940c7a2019-08-21 14:25:39 -0700742 }
743 fmt.Printf("%s\n", i)
744 }
745
David Bainbridge7052fe82020-03-25 10:37:00 -0700746 if lastErr != nil {
747 return NoReportErr
748 }
Zack Williamse940c7a2019-08-21 14:25:39 -0700749 return nil
750}
751
752func (options *DeviceDisable) Execute(args []string) error {
753 conn, err := NewConnection()
754 if err != nil {
755 return err
756 }
757 defer conn.Close()
758
Scott Baker9173ed82020-05-19 08:30:12 -0700759 client := voltha.NewVolthaServiceClient(conn)
Zack Williamse940c7a2019-08-21 14:25:39 -0700760
David Bainbridge7052fe82020-03-25 10:37:00 -0700761 var lastErr error
Zack Williamse940c7a2019-08-21 14:25:39 -0700762 for _, i := range options.Args.Ids {
David K. Bainbridge9189c632021-03-26 21:52:21 +0000763 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Zack Williamse940c7a2019-08-21 14:25:39 -0700764 defer cancel()
765
Scott Baker9173ed82020-05-19 08:30:12 -0700766 id := voltha.ID{Id: string(i)}
767
768 _, err := client.DisableDevice(ctx, &id)
Zack Williamse940c7a2019-08-21 14:25:39 -0700769 if err != nil {
David Bainbridge0f758d42019-10-26 05:17:48 +0000770 Error.Printf("Error while disabling '%s': %s\n", i, err)
David Bainbridge7052fe82020-03-25 10:37:00 -0700771 lastErr = err
Zack Williamse940c7a2019-08-21 14:25:39 -0700772 continue
Zack Williamse940c7a2019-08-21 14:25:39 -0700773 }
774 fmt.Printf("%s\n", i)
775 }
776
David Bainbridge7052fe82020-03-25 10:37:00 -0700777 if lastErr != nil {
778 return NoReportErr
779 }
Zack Williamse940c7a2019-08-21 14:25:39 -0700780 return nil
781}
782
783func (options *DeviceReboot) Execute(args []string) error {
784 conn, err := NewConnection()
785 if err != nil {
786 return err
787 }
788 defer conn.Close()
789
Scott Baker9173ed82020-05-19 08:30:12 -0700790 client := voltha.NewVolthaServiceClient(conn)
Zack Williamse940c7a2019-08-21 14:25:39 -0700791
David Bainbridge7052fe82020-03-25 10:37:00 -0700792 var lastErr error
Zack Williamse940c7a2019-08-21 14:25:39 -0700793 for _, i := range options.Args.Ids {
David K. Bainbridge9189c632021-03-26 21:52:21 +0000794 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Zack Williamse940c7a2019-08-21 14:25:39 -0700795 defer cancel()
796
Scott Baker9173ed82020-05-19 08:30:12 -0700797 id := voltha.ID{Id: string(i)}
798
799 _, err := client.RebootDevice(ctx, &id)
Zack Williamse940c7a2019-08-21 14:25:39 -0700800 if err != nil {
David Bainbridge0f758d42019-10-26 05:17:48 +0000801 Error.Printf("Error while rebooting '%s': %s\n", i, err)
David Bainbridge7052fe82020-03-25 10:37:00 -0700802 lastErr = err
Zack Williamse940c7a2019-08-21 14:25:39 -0700803 continue
Zack Williamse940c7a2019-08-21 14:25:39 -0700804 }
805 fmt.Printf("%s\n", i)
806 }
807
David Bainbridge7052fe82020-03-25 10:37:00 -0700808 if lastErr != nil {
809 return NoReportErr
810 }
Zack Williamse940c7a2019-08-21 14:25:39 -0700811 return nil
812}
813
814func (options *DevicePortList) Execute(args []string) error {
815
816 conn, err := NewConnection()
817 if err != nil {
818 return err
819 }
820 defer conn.Close()
821
Scott Baker9173ed82020-05-19 08:30:12 -0700822 client := voltha.NewVolthaServiceClient(conn)
Zack Williamse940c7a2019-08-21 14:25:39 -0700823
David K. Bainbridge9189c632021-03-26 21:52:21 +0000824 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Zack Williamse940c7a2019-08-21 14:25:39 -0700825 defer cancel()
826
Scott Baker9173ed82020-05-19 08:30:12 -0700827 id := voltha.ID{Id: string(options.Args.Id)}
Zack Williamse940c7a2019-08-21 14:25:39 -0700828
Scott Baker9173ed82020-05-19 08:30:12 -0700829 ports, err := client.ListDevicePorts(ctx, &id)
Zack Williamse940c7a2019-08-21 14:25:39 -0700830 if err != nil {
831 return err
832 }
833
834 outputFormat := CharReplacer.Replace(options.Format)
835 if outputFormat == "" {
David Bainbridgea6722342019-10-24 23:55:53 +0000836 outputFormat = GetCommandOptionWithDefault("device-ports", "format", DEFAULT_DEVICE_PORTS_FORMAT)
Zack Williamse940c7a2019-08-21 14:25:39 -0700837 }
Zack Williamse940c7a2019-08-21 14:25:39 -0700838
David Bainbridgea6722342019-10-24 23:55:53 +0000839 orderBy := options.OrderBy
840 if orderBy == "" {
841 orderBy = GetCommandOptionWithDefault("device-ports", "order", "")
842 }
843
Zack Williamse940c7a2019-08-21 14:25:39 -0700844 result := CommandResult{
845 Format: format.Format(outputFormat),
846 Filter: options.Filter,
David Bainbridgea6722342019-10-24 23:55:53 +0000847 OrderBy: orderBy,
Zack Williamse940c7a2019-08-21 14:25:39 -0700848 OutputAs: toOutputType(options.OutputAs),
849 NameLimit: options.NameLimit,
Scott Baker9173ed82020-05-19 08:30:12 -0700850 Data: ports.Items,
Zack Williamse940c7a2019-08-21 14:25:39 -0700851 }
852
853 GenerateOutput(&result)
854 return nil
855}
856
857func (options *DeviceFlowList) Execute(args []string) error {
858 fl := &FlowList{}
859 fl.ListOutputOptions = options.ListOutputOptions
Maninder045921e2020-09-29 16:46:02 +0530860 fl.FlowIdOptions = options.FlowIdOptions
Zack Williamse940c7a2019-08-21 14:25:39 -0700861 fl.Args.Id = string(options.Args.Id)
David Bainbridgea6722342019-10-24 23:55:53 +0000862 fl.Method = "device-flows"
Zack Williamse940c7a2019-08-21 14:25:39 -0700863 return fl.Execute(args)
864}
865
866func (options *DeviceInspect) Execute(args []string) error {
867 if len(args) > 0 {
868 return fmt.Errorf("only a single argument 'DEVICE_ID' can be provided")
869 }
870
871 conn, err := NewConnection()
872 if err != nil {
873 return err
874 }
875 defer conn.Close()
876
Scott Baker9173ed82020-05-19 08:30:12 -0700877 client := voltha.NewVolthaServiceClient(conn)
Zack Williamse940c7a2019-08-21 14:25:39 -0700878
David K. Bainbridge9189c632021-03-26 21:52:21 +0000879 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Zack Williamse940c7a2019-08-21 14:25:39 -0700880 defer cancel()
881
Scott Baker9173ed82020-05-19 08:30:12 -0700882 id := voltha.ID{Id: string(options.Args.Id)}
Zack Williamse940c7a2019-08-21 14:25:39 -0700883
Scott Baker9173ed82020-05-19 08:30:12 -0700884 device, err := client.GetDevice(ctx, &id)
Zack Williamse940c7a2019-08-21 14:25:39 -0700885 if err != nil {
886 return err
887 }
888
Zack Williamse940c7a2019-08-21 14:25:39 -0700889 outputFormat := CharReplacer.Replace(options.Format)
890 if outputFormat == "" {
David Bainbridgea6722342019-10-24 23:55:53 +0000891 outputFormat = GetCommandOptionWithDefault("device-inspect", "format", DEFAULT_DEVICE_INSPECT_FORMAT)
Zack Williamse940c7a2019-08-21 14:25:39 -0700892 }
893 if options.Quiet {
894 outputFormat = "{{.Id}}"
895 }
896
897 result := CommandResult{
898 Format: format.Format(outputFormat),
899 OutputAs: toOutputType(options.OutputAs),
900 NameLimit: options.NameLimit,
901 Data: device,
902 }
903 GenerateOutput(&result)
904 return nil
905}
kesavand12cd8eb2020-01-20 22:25:22 -0500906
907/*Device Port Enable */
908func (options *DevicePortEnable) Execute(args []string) error {
909 conn, err := NewConnection()
910 if err != nil {
911 return err
912 }
913 defer conn.Close()
914
Scott Baker9173ed82020-05-19 08:30:12 -0700915 client := voltha.NewVolthaServiceClient(conn)
kesavand12cd8eb2020-01-20 22:25:22 -0500916
David K. Bainbridge9189c632021-03-26 21:52:21 +0000917 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
kesavand12cd8eb2020-01-20 22:25:22 -0500918 defer cancel()
919
Scott Baker9173ed82020-05-19 08:30:12 -0700920 port := voltha.Port{DeviceId: string(options.Args.Id), PortNo: uint32(options.Args.PortId)}
921
922 _, err = client.EnablePort(ctx, &port)
kesavand12cd8eb2020-01-20 22:25:22 -0500923 if err != nil {
924 Error.Printf("Error enabling port number %v on device Id %s,err=%s\n", options.Args.PortId, options.Args.Id, ErrorToString(err))
925 return err
kesavand12cd8eb2020-01-20 22:25:22 -0500926 }
927
928 return nil
929}
930
Scott Baker9173ed82020-05-19 08:30:12 -0700931/*Device Port Disable */
kesavand12cd8eb2020-01-20 22:25:22 -0500932func (options *DevicePortDisable) Execute(args []string) error {
933 conn, err := NewConnection()
934 if err != nil {
935 return err
936 }
937 defer conn.Close()
938
Scott Baker9173ed82020-05-19 08:30:12 -0700939 client := voltha.NewVolthaServiceClient(conn)
940
David K. Bainbridge9189c632021-03-26 21:52:21 +0000941 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
kesavand12cd8eb2020-01-20 22:25:22 -0500942 defer cancel()
943
Scott Baker9173ed82020-05-19 08:30:12 -0700944 port := voltha.Port{DeviceId: string(options.Args.Id), PortNo: uint32(options.Args.PortId)}
945
946 _, err = client.DisablePort(ctx, &port)
kesavand12cd8eb2020-01-20 22:25:22 -0500947 if err != nil {
Scott Baker9173ed82020-05-19 08:30:12 -0700948 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 -0500949 return err
kesavand12cd8eb2020-01-20 22:25:22 -0500950 }
Scott Baker9173ed82020-05-19 08:30:12 -0700951
kesavand12cd8eb2020-01-20 22:25:22 -0500952 return nil
953}
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -0800954
Rohan Agrawald7df3772020-06-29 11:23:36 +0000955func (options *DevicePmConfigSetMaxSkew) Execute(args []string) error {
956 conn, err := NewConnection()
957 if err != nil {
958 return err
959 }
960 defer conn.Close()
961
962 client := voltha.NewVolthaServiceClient(conn)
963
David K. Bainbridge9189c632021-03-26 21:52:21 +0000964 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Rohan Agrawald7df3772020-06-29 11:23:36 +0000965 defer cancel()
966
967 id := voltha.ID{Id: string(options.Args.Id)}
968
969 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
970 if err != nil {
971 return err
972 }
973
974 pmConfigs.MaxSkew = options.Args.MaxSkew
975
976 _, err = client.UpdateDevicePmConfigs(ctx, pmConfigs)
977 if err != nil {
978 return err
979 }
980
981 return nil
982}
983
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000984func (options *DevicePmConfigsGet) Execute(args []string) error {
985
986 conn, err := NewConnection()
987 if err != nil {
988 return err
989 }
990 defer conn.Close()
991
992 client := voltha.NewVolthaServiceClient(conn)
993
David K. Bainbridge9189c632021-03-26 21:52:21 +0000994 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000995 defer cancel()
996
997 id := voltha.ID{Id: string(options.Args.Id)}
998
999 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
1000 if err != nil {
1001 return err
1002 }
1003
1004 outputFormat := CharReplacer.Replace(options.Format)
1005 if outputFormat == "" {
1006 outputFormat = GetCommandOptionWithDefault("device-pm-configs", "format", DEFAULT_DEVICE_PM_CONFIG_GET_FORMAT)
1007 }
1008
1009 orderBy := options.OrderBy
1010 if orderBy == "" {
1011 orderBy = GetCommandOptionWithDefault("device-pm-configs", "order", "")
1012 }
1013
1014 result := CommandResult{
1015 Format: format.Format(outputFormat),
1016 Filter: options.Filter,
1017 OrderBy: orderBy,
1018 OutputAs: toOutputType(options.OutputAs),
1019 NameLimit: options.NameLimit,
1020 Data: pmConfigs,
1021 }
1022
1023 GenerateOutput(&result)
1024 return nil
1025
1026}
1027
1028func (options *DevicePmConfigMetricList) Execute(args []string) error {
1029
1030 conn, err := NewConnection()
1031 if err != nil {
1032 return err
1033 }
1034 defer conn.Close()
1035
1036 client := voltha.NewVolthaServiceClient(conn)
1037
David K. Bainbridge9189c632021-03-26 21:52:21 +00001038 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001039 defer cancel()
1040
1041 id := voltha.ID{Id: string(options.Args.Id)}
1042
1043 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
1044 if err != nil {
1045 return err
1046 }
1047
1048 if !pmConfigs.Grouped {
1049 for _, metric := range pmConfigs.Metrics {
1050 if metric.SampleFreq == 0 {
1051 metric.SampleFreq = pmConfigs.DefaultFreq
1052 }
1053 }
Rohan Agrawalbca69122020-06-17 14:59:03 +00001054 outputFormat := CharReplacer.Replace(options.Format)
1055 if outputFormat == "" {
1056 outputFormat = GetCommandOptionWithDefault("device-pm-configs", "format", DEFAULT_DEVICE_PM_CONFIG_METRIC_LIST_FORMAT)
1057 }
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001058
Rohan Agrawalbca69122020-06-17 14:59:03 +00001059 orderBy := options.OrderBy
1060 if orderBy == "" {
1061 orderBy = GetCommandOptionWithDefault("device-pm-configs", "order", "")
1062 }
1063
1064 result := CommandResult{
1065 Format: format.Format(outputFormat),
1066 Filter: options.Filter,
1067 OrderBy: orderBy,
1068 OutputAs: toOutputType(options.OutputAs),
1069 NameLimit: options.NameLimit,
1070 Data: pmConfigs.Metrics,
1071 }
1072
1073 GenerateOutput(&result)
1074 return nil
1075 } else {
1076 return fmt.Errorf("Device '%s' does not have Non Grouped Metrics", options.Args.Id)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001077 }
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001078}
1079
1080func (options *DevicePmConfigMetricEnable) Execute(args []string) error {
1081
1082 conn, err := NewConnection()
1083 if err != nil {
1084 return err
1085 }
1086 defer conn.Close()
1087
1088 client := voltha.NewVolthaServiceClient(conn)
1089
David K. Bainbridge9189c632021-03-26 21:52:21 +00001090 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001091 defer cancel()
1092
1093 id := voltha.ID{Id: string(options.Args.Id)}
1094
1095 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
1096 if err != nil {
1097 return err
1098 }
1099
1100 if !pmConfigs.Grouped {
Rohan Agrawalbca69122020-06-17 14:59:03 +00001101 metrics := make(map[string]struct{})
1102 for _, metric := range pmConfigs.Metrics {
1103 metrics[metric.Name] = struct{}{}
1104 }
1105
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001106 for _, metric := range pmConfigs.Metrics {
1107 for _, mName := range options.Args.Metrics {
Rohan Agrawalbca69122020-06-17 14:59:03 +00001108 if _, exist := metrics[string(mName)]; !exist {
1109 return fmt.Errorf("Metric Name '%s' does not exist", mName)
1110 }
1111
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001112 if string(mName) == metric.Name && !metric.Enabled {
1113 metric.Enabled = true
1114 _, err := client.UpdateDevicePmConfigs(ctx, pmConfigs)
1115 if err != nil {
1116 return err
1117 }
1118 }
1119 }
1120 }
Rohan Agrawalbca69122020-06-17 14:59:03 +00001121 } else {
1122 return fmt.Errorf("Device '%s' does not have Non Grouped Metrics", options.Args.Id)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001123 }
1124 return nil
1125}
1126
1127func (options *DevicePmConfigMetricDisable) Execute(args []string) error {
1128
1129 conn, err := NewConnection()
1130 if err != nil {
1131 return err
1132 }
1133 defer conn.Close()
1134
1135 client := voltha.NewVolthaServiceClient(conn)
1136
David K. Bainbridge9189c632021-03-26 21:52:21 +00001137 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001138 defer cancel()
1139
1140 id := voltha.ID{Id: string(options.Args.Id)}
1141
1142 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
1143 if err != nil {
1144 return err
1145 }
1146
1147 if !pmConfigs.Grouped {
Rohan Agrawalbca69122020-06-17 14:59:03 +00001148 metrics := make(map[string]struct{})
1149 for _, metric := range pmConfigs.Metrics {
1150 metrics[metric.Name] = struct{}{}
1151 }
1152
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001153 for _, metric := range pmConfigs.Metrics {
1154 for _, mName := range options.Args.Metrics {
Rohan Agrawalbca69122020-06-17 14:59:03 +00001155 if _, have := metrics[string(mName)]; !have {
1156 return fmt.Errorf("Metric Name '%s' does not exist", mName)
1157 }
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001158 if string(mName) == metric.Name && metric.Enabled {
1159 metric.Enabled = false
1160 _, err := client.UpdateDevicePmConfigs(ctx, pmConfigs)
1161 if err != nil {
1162 return err
1163 }
Rohan Agrawalbca69122020-06-17 14:59:03 +00001164 } else {
1165 return fmt.Errorf("Metric '%s' cannot be disabled", string(mName))
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001166 }
1167 }
1168 }
Rohan Agrawalbca69122020-06-17 14:59:03 +00001169 } else {
1170 return fmt.Errorf("Device '%s' does not have Non Grouped Metrics", options.Args.Id)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001171 }
1172 return nil
1173}
1174
1175func (options *DevicePmConfigGroupEnable) Execute(args []string) error {
1176
1177 conn, err := NewConnection()
1178 if err != nil {
1179 return err
1180 }
1181 defer conn.Close()
1182
1183 client := voltha.NewVolthaServiceClient(conn)
1184
David K. Bainbridge9189c632021-03-26 21:52:21 +00001185 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001186 defer cancel()
1187
1188 id := voltha.ID{Id: string(options.Args.Id)}
1189
1190 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
1191 if err != nil {
1192 return err
1193 }
1194
1195 if pmConfigs.Grouped {
Rohan Agrawalbca69122020-06-17 14:59:03 +00001196 groups := make(map[string]struct{})
1197 for _, group := range pmConfigs.Groups {
1198 groups[group.GroupName] = struct{}{}
1199 }
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001200 for _, group := range pmConfigs.Groups {
Girish Gowdra610acb42021-01-27 13:33:57 -08001201 if _, have := groups[string(options.Args.Group)]; !have {
1202 return fmt.Errorf("Group Name '%s' does not exist", options.Args.Group)
1203 }
1204 if string(options.Args.Group) == group.GroupName && !group.Enabled {
1205 group.Enabled = true
1206 _, err := client.UpdateDevicePmConfigs(ctx, pmConfigs)
1207 if err != nil {
1208 return err
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001209 }
1210 }
1211 }
Rohan Agrawalbca69122020-06-17 14:59:03 +00001212 } else {
1213 return fmt.Errorf("Device '%s' does not have Group Metrics", options.Args.Id)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001214 }
1215 return nil
1216}
1217
1218func (options *DevicePmConfigGroupDisable) Execute(args []string) error {
1219
1220 conn, err := NewConnection()
1221 if err != nil {
1222 return err
1223 }
1224 defer conn.Close()
1225
1226 client := voltha.NewVolthaServiceClient(conn)
1227
David K. Bainbridge9189c632021-03-26 21:52:21 +00001228 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001229 defer cancel()
1230
1231 id := voltha.ID{Id: string(options.Args.Id)}
1232
1233 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
1234 if err != nil {
1235 return err
1236 }
1237
1238 if pmConfigs.Grouped {
Rohan Agrawalbca69122020-06-17 14:59:03 +00001239 groups := make(map[string]struct{})
1240 for _, group := range pmConfigs.Groups {
1241 groups[group.GroupName] = struct{}{}
1242 }
1243
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001244 for _, group := range pmConfigs.Groups {
Girish Gowdra610acb42021-01-27 13:33:57 -08001245 if _, have := groups[string(options.Args.Group)]; !have {
1246 return fmt.Errorf("Group Name '%s' does not exist", options.Args.Group)
1247 }
Rohan Agrawalbca69122020-06-17 14:59:03 +00001248
Girish Gowdra610acb42021-01-27 13:33:57 -08001249 if string(options.Args.Group) == group.GroupName && group.Enabled {
1250 group.Enabled = false
1251 _, err := client.UpdateDevicePmConfigs(ctx, pmConfigs)
1252 if err != nil {
1253 return err
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001254 }
1255 }
1256 }
Rohan Agrawalbca69122020-06-17 14:59:03 +00001257 } else {
1258 return fmt.Errorf("Device '%s' does not have Group Metrics", options.Args.Id)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001259 }
1260 return nil
1261}
1262
Girish Gowdra610acb42021-01-27 13:33:57 -08001263func (options *DevicePmConfigGroupFrequencySet) Execute(args []string) error {
1264
1265 conn, err := NewConnection()
1266 if err != nil {
1267 return err
1268 }
1269 defer conn.Close()
1270
1271 client := voltha.NewVolthaServiceClient(conn)
1272
David K. Bainbridge9189c632021-03-26 21:52:21 +00001273 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Girish Gowdra610acb42021-01-27 13:33:57 -08001274 defer cancel()
1275
1276 id := voltha.ID{Id: string(options.Args.Id)}
1277
1278 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
1279 if err != nil {
1280 return err
1281 }
1282
1283 if pmConfigs.Grouped {
1284 groups := make(map[string]struct{})
1285 for _, group := range pmConfigs.Groups {
1286 groups[group.GroupName] = struct{}{}
1287 }
1288
1289 for _, group := range pmConfigs.Groups {
1290 if _, have := groups[string(options.Args.Group)]; !have {
1291 return fmt.Errorf("group name '%s' does not exist", options.Args.Group)
1292 }
1293
1294 if string(options.Args.Group) == group.GroupName {
1295 if !group.Enabled {
1296 return fmt.Errorf("group '%s' is not enabled", options.Args.Group)
1297 }
1298 group.GroupFreq = uint32(options.Args.Interval.Seconds())
1299 _, err = client.UpdateDevicePmConfigs(ctx, pmConfigs)
1300 if err != nil {
1301 return err
1302 }
1303 }
1304 }
1305 } else {
1306 return fmt.Errorf("device '%s' does not have group metrics", options.Args.Id)
1307 }
1308 return nil
1309}
1310
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001311func (options *DevicePmConfigGroupList) Execute(args []string) error {
1312
1313 conn, err := NewConnection()
1314 if err != nil {
1315 return err
1316 }
1317 defer conn.Close()
1318
1319 client := voltha.NewVolthaServiceClient(conn)
1320
David K. Bainbridge9189c632021-03-26 21:52:21 +00001321 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001322 defer cancel()
1323
1324 id := voltha.ID{Id: string(options.Args.Id)}
1325
1326 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
1327 if err != nil {
1328 return err
1329 }
1330
1331 if pmConfigs.Grouped {
1332 for _, group := range pmConfigs.Groups {
1333 if group.GroupFreq == 0 {
1334 group.GroupFreq = pmConfigs.DefaultFreq
1335 }
1336 }
Rohan Agrawalbca69122020-06-17 14:59:03 +00001337 outputFormat := CharReplacer.Replace(options.Format)
1338 if outputFormat == "" {
1339 outputFormat = GetCommandOptionWithDefault("device-pm-configs", "format", DEFAULT_DEVICE_PM_CONFIG_GROUP_LIST_FORMAT)
1340 }
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001341
Rohan Agrawalbca69122020-06-17 14:59:03 +00001342 orderBy := options.OrderBy
1343 if orderBy == "" {
1344 orderBy = GetCommandOptionWithDefault("device-pm-configs", "order", "")
1345 }
1346
1347 result := CommandResult{
1348 Format: format.Format(outputFormat),
1349 Filter: options.Filter,
1350 OrderBy: orderBy,
1351 OutputAs: toOutputType(options.OutputAs),
1352 NameLimit: options.NameLimit,
1353 Data: pmConfigs.Groups,
1354 }
1355
1356 GenerateOutput(&result)
1357 } else {
1358 return fmt.Errorf("Device '%s' does not have Group Metrics", string(options.Args.Id))
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001359 }
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001360 return nil
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001361}
1362
1363func (options *DevicePmConfigGroupMetricList) Execute(args []string) error {
1364
1365 var metrics []*voltha.PmConfig
1366 conn, err := NewConnection()
1367 if err != nil {
1368 return err
1369 }
1370 defer conn.Close()
1371
1372 client := voltha.NewVolthaServiceClient(conn)
1373
David K. Bainbridge9189c632021-03-26 21:52:21 +00001374 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001375 defer cancel()
1376
1377 id := voltha.ID{Id: string(options.Args.Id)}
1378
1379 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
1380 if err != nil {
1381 return err
1382 }
1383
1384 for _, groups := range pmConfigs.Groups {
1385
1386 if string(options.Args.Group) == groups.GroupName {
1387 for _, metric := range groups.Metrics {
1388 if metric.SampleFreq == 0 && groups.GroupFreq == 0 {
1389 metric.SampleFreq = pmConfigs.DefaultFreq
1390 } else {
1391 metric.SampleFreq = groups.GroupFreq
1392 }
1393 }
1394 metrics = groups.Metrics
1395 }
1396 }
1397
1398 outputFormat := CharReplacer.Replace(options.Format)
1399 if outputFormat == "" {
1400 outputFormat = GetCommandOptionWithDefault("device-pm-configs", "format", DEFAULT_DEVICE_PM_CONFIG_METRIC_LIST_FORMAT)
1401 }
1402
1403 orderBy := options.OrderBy
1404 if orderBy == "" {
1405 orderBy = GetCommandOptionWithDefault("device-pm-configs", "order", "")
1406 }
1407
1408 result := CommandResult{
1409 Format: format.Format(outputFormat),
1410 Filter: options.Filter,
1411 OrderBy: orderBy,
1412 OutputAs: toOutputType(options.OutputAs),
1413 NameLimit: options.NameLimit,
1414 Data: metrics,
1415 }
1416
1417 GenerateOutput(&result)
1418 return nil
1419
1420}
1421
1422func (options *DevicePmConfigFrequencySet) Execute(args []string) error {
1423
1424 conn, err := NewConnection()
1425 if err != nil {
1426 return err
1427 }
1428 defer conn.Close()
1429
1430 client := voltha.NewVolthaServiceClient(conn)
1431
David K. Bainbridge9189c632021-03-26 21:52:21 +00001432 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001433 defer cancel()
1434
1435 id := voltha.ID{Id: string(options.Args.Id)}
1436
1437 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
1438 if err != nil {
1439 return err
1440 }
1441
Girish Gowdra610acb42021-01-27 13:33:57 -08001442 pmConfigs.DefaultFreq = uint32(options.Args.Interval.Seconds())
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001443
1444 _, err = client.UpdateDevicePmConfigs(ctx, pmConfigs)
1445 if err != nil {
1446 return err
1447 }
1448
1449 outputFormat := CharReplacer.Replace(options.Format)
1450 if outputFormat == "" {
1451 outputFormat = GetCommandOptionWithDefault("device-pm-configs", "format", DEFAULT_DEVICE_PM_CONFIG_GET_FORMAT)
1452 }
1453 if options.Quiet {
1454 outputFormat = "{{.Id}}"
1455 }
1456
1457 result := CommandResult{
1458 Format: format.Format(outputFormat),
1459 OutputAs: toOutputType(options.OutputAs),
1460 NameLimit: options.NameLimit,
1461 Data: pmConfigs,
1462 }
1463
1464 GenerateOutput(&result)
1465 return nil
1466
1467}
1468
kesavand3e2f9f62021-04-22 11:06:38 +05301469func (options *OnuDownloadImage) Execute(args []string) error {
1470
1471 conn, err := NewConnection()
1472 if err != nil {
1473 return err
1474 }
1475 defer conn.Close()
1476
1477 client := voltha.NewVolthaServiceClient(conn)
1478
1479 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
1480 defer cancel()
1481
1482 var devIDList []*common.ID
1483 for _, i := range options.Args.IDs {
1484
1485 devIDList = append(devIDList, &common.ID{Id: string(i)})
1486 }
1487
1488 downloadImage := voltha.DeviceImageDownloadRequest{
1489 DeviceId: devIDList,
1490 Image: &voltha.Image{
1491 Url: options.Args.Url,
1492 Crc32: options.Args.Crc,
ssiddiqui7bc89e92021-05-20 20:58:02 +05301493 Vendor: options.Args.Vendor,
kesavand3e2f9f62021-04-22 11:06:38 +05301494 Version: options.Args.ImageVersion,
1495 },
1496 ActivateOnSuccess: options.Args.ActivateOnSuccess,
1497 CommitOnSuccess: options.Args.CommitOnSuccess,
1498 }
1499
1500 _, err = client.DownloadImageToDevice(ctx, &downloadImage)
1501 if err != nil {
1502 return err
1503 }
1504
1505 return nil
1506
1507}
1508
1509func (options *OnuActivateImage) Execute(args []string) error {
1510
1511 conn, err := NewConnection()
1512 if err != nil {
1513 return err
1514 }
1515 defer conn.Close()
1516
1517 client := voltha.NewVolthaServiceClient(conn)
1518
1519 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
1520 defer cancel()
1521
1522 var devIDList []*common.ID
1523 for _, i := range options.Args.IDs {
1524
1525 devIDList = append(devIDList, &common.ID{Id: string(i)})
1526 }
1527
1528 downloadImage := voltha.DeviceImageRequest{
1529 DeviceId: devIDList,
1530 Version: options.Args.ImageVersion,
1531 CommitOnSuccess: options.Args.CommitOnSuccess,
1532 }
1533
1534 _, err = client.ActivateImage(ctx, &downloadImage)
1535 if err != nil {
1536 return err
1537 }
1538
1539 return nil
1540
1541}
1542
1543func (options *OnuAbortUpgradeImage) Execute(args []string) error {
1544
1545 conn, err := NewConnection()
1546 if err != nil {
1547 return err
1548 }
1549 defer conn.Close()
1550
1551 client := voltha.NewVolthaServiceClient(conn)
1552
1553 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
1554 defer cancel()
1555
1556 var devIDList []*common.ID
1557 for _, i := range options.Args.IDs {
1558
1559 devIDList = append(devIDList, &common.ID{Id: string(i)})
1560 }
1561
1562 downloadImage := voltha.DeviceImageRequest{
1563 DeviceId: devIDList,
1564 Version: options.Args.ImageVersion,
1565 }
1566
1567 _, err = client.AbortImageUpgradeToDevice(ctx, &downloadImage)
1568 if err != nil {
1569 return err
1570 }
1571
1572 return nil
1573
1574}
1575
1576func (options *OnuCommitImage) Execute(args []string) error {
1577
1578 conn, err := NewConnection()
1579 if err != nil {
1580 return err
1581 }
1582 defer conn.Close()
1583
1584 client := voltha.NewVolthaServiceClient(conn)
1585
1586 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
1587 defer cancel()
1588
1589 var devIDList []*common.ID
1590 for _, i := range options.Args.IDs {
1591
1592 devIDList = append(devIDList, &common.ID{Id: string(i)})
1593 }
1594 downloadImage := voltha.DeviceImageRequest{
1595 DeviceId: devIDList,
1596 Version: options.Args.ImageVersion,
1597 }
1598
1599 _, err = client.CommitImage(ctx, &downloadImage)
1600 if err != nil {
1601 return err
1602 }
1603
1604 return nil
1605
1606}
1607
1608func (options *OnuListImages) Execute(args []string) error {
1609
1610 conn, err := NewConnection()
1611 if err != nil {
1612 return err
1613 }
1614 defer conn.Close()
1615
1616 client := voltha.NewVolthaServiceClient(conn)
1617
1618 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
1619 defer cancel()
1620
1621 id := common.ID{Id: string(options.Args.Id)}
1622
1623 onuImages, err := client.GetOnuImages(ctx, &id)
1624 if err != nil {
1625 return err
1626 }
1627
1628 outputFormat := CharReplacer.Replace(options.Format)
1629 if outputFormat == "" {
1630 outputFormat = GetCommandOptionWithDefault("onu-image-list", "format", ONU_IMAGE_LIST_FORMAT)
1631 }
1632
1633 if options.Quiet {
1634 outputFormat = "{{.Id}}"
1635 }
1636
1637 //TODO orderby
1638
1639 // Make sure json output prints an empty list, not "null"
1640 if onuImages.Items == nil {
1641 onuImages.Items = make([]*voltha.OnuImage, 0)
1642 }
1643
1644 result := CommandResult{
1645 Format: format.Format(outputFormat),
1646 OutputAs: toOutputType(options.OutputAs),
1647 NameLimit: options.NameLimit,
1648 Data: onuImages.Items,
1649 }
1650
1651 GenerateOutput(&result)
1652 return nil
1653
1654}
1655
1656func (options *OnuImageStatus) Execute(args []string) error {
1657
1658 conn, err := NewConnection()
1659 if err != nil {
1660 return err
1661 }
1662 defer conn.Close()
1663
1664 client := voltha.NewVolthaServiceClient(conn)
1665
1666 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
1667 defer cancel()
1668
1669 var devIDList []*common.ID
1670 for _, i := range options.Args.IDs {
1671
1672 devIDList = append(devIDList, &common.ID{Id: string(i)})
1673 }
1674
1675 imageStatusReq := voltha.DeviceImageRequest{
1676 DeviceId: devIDList,
1677 Version: options.Args.ImageVersion,
1678 }
1679 imageStatus, err := client.GetImageStatus(ctx, &imageStatusReq)
1680 if err != nil {
1681 return err
1682 }
1683
1684 outputFormat := CharReplacer.Replace(options.Format)
1685 if outputFormat == "" {
1686 outputFormat = GetCommandOptionWithDefault("device-image-list", "format", ONU_IMAGE_STATUS_FORMAT)
1687 }
1688
1689 if options.Quiet {
1690 outputFormat = "{{.Id}}"
1691 }
1692
1693 //TODO orderby
1694
1695 // Make sure json output prints an empty list, not "null"
1696 if imageStatus.DeviceImageStates == nil {
1697 imageStatus.DeviceImageStates = make([]*voltha.DeviceImageState, 0)
1698 }
1699
1700 result := CommandResult{
1701 Format: format.Format(outputFormat),
1702 OutputAs: toOutputType(options.OutputAs),
1703 NameLimit: options.NameLimit,
1704 Data: imageStatus.DeviceImageStates,
1705 }
1706
1707 GenerateOutput(&result)
1708 return nil
1709
1710}
1711
Andrea Campanella791d88b2021-01-08 13:29:00 +01001712func (options *DeviceOnuListImages) Execute(args []string) error {
1713
1714 conn, err := NewConnection()
1715 if err != nil {
1716 return err
1717 }
1718 defer conn.Close()
1719
1720 client := voltha.NewVolthaServiceClient(conn)
1721
David K. Bainbridge9189c632021-03-26 21:52:21 +00001722 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Andrea Campanella791d88b2021-01-08 13:29:00 +01001723 defer cancel()
1724
1725 id := common.ID{Id: string(options.Args.Id)}
1726
1727 imageDownloads, err := client.ListImageDownloads(ctx, &id)
1728 if err != nil {
1729 return err
1730 }
1731
1732 outputFormat := CharReplacer.Replace(options.Format)
1733 if outputFormat == "" {
1734 outputFormat = GetCommandOptionWithDefault("device-image-list", "format", DEFAULT_DEVICE_IMAGE_LIST_GET_FORMAT)
1735 }
1736
1737 if options.Quiet {
1738 outputFormat = "{{.Id}}"
1739 }
1740
1741 //TODO orderby
1742
1743 // Make sure json output prints an empty list, not "null"
1744 if imageDownloads.Items == nil {
1745 imageDownloads.Items = make([]*voltha.ImageDownload, 0)
1746 }
1747
1748 result := CommandResult{
1749 Format: format.Format(outputFormat),
1750 OutputAs: toOutputType(options.OutputAs),
1751 NameLimit: options.NameLimit,
1752 Data: imageDownloads.Items,
1753 }
1754
1755 GenerateOutput(&result)
1756 return nil
1757
1758}
1759
1760func (options *DeviceOnuDownloadImage) Execute(args []string) error {
1761
1762 conn, err := NewConnection()
1763 if err != nil {
1764 return err
1765 }
1766 defer conn.Close()
1767
1768 client := voltha.NewVolthaServiceClient(conn)
1769
David K. Bainbridge9189c632021-03-26 21:52:21 +00001770 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Andrea Campanella791d88b2021-01-08 13:29:00 +01001771 defer cancel()
1772
1773 downloadImage := voltha.ImageDownload{
1774 Id: string(options.Args.Id),
1775 Name: options.Args.Name,
1776 Url: options.Args.Url,
1777 Crc: options.Args.Crc,
1778 LocalDir: options.Args.LocalDir,
1779 }
1780
1781 _, err = client.DownloadImage(ctx, &downloadImage)
1782 if err != nil {
1783 return err
1784 }
1785
1786 return nil
1787
1788}
1789
1790func (options *DeviceOnuActivateImageUpdate) Execute(args []string) error {
1791
1792 conn, err := NewConnection()
1793 if err != nil {
1794 return err
1795 }
1796 defer conn.Close()
1797
1798 client := voltha.NewVolthaServiceClient(conn)
1799
David K. Bainbridge9189c632021-03-26 21:52:21 +00001800 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Andrea Campanella791d88b2021-01-08 13:29:00 +01001801 defer cancel()
1802
1803 downloadImage := voltha.ImageDownload{
1804 Id: string(options.Args.Id),
1805 Name: options.Args.Name,
1806 ImageVersion: options.Args.ImageVersion,
1807 SaveConfig: options.Args.SaveConfig,
1808 LocalDir: options.Args.LocalDir,
1809 }
1810
1811 _, err = client.ActivateImageUpdate(ctx, &downloadImage)
1812 if err != nil {
1813 return err
1814 }
1815
1816 return nil
1817
1818}
1819
Scott Baker9173ed82020-05-19 08:30:12 -07001820type ReturnValueRow struct {
1821 Name string `json:"name"`
1822 Result interface{} `json:"result"`
1823}
1824
kesavand8ec4fc02021-01-27 09:10:22 -05001825func (options *DeviceGetPortStats) Execute(args []string) error {
1826 conn, err := NewConnection()
1827 if err != nil {
1828 return err
1829 }
1830 defer conn.Close()
1831 client := extension.NewExtensionClient(conn)
1832 var portType extension.GetOltPortCounters_PortType
1833
1834 if options.Args.PortType == "pon" {
1835 portType = extension.GetOltPortCounters_Port_PON_OLT
1836 } else if options.Args.PortType == "nni" {
1837
1838 portType = extension.GetOltPortCounters_Port_ETHERNET_NNI
1839 } else {
1840 return fmt.Errorf("expected interface type pon/nni, provided %s", options.Args.PortType)
1841 }
1842
1843 singleGetValReq := extension.SingleGetValueRequest{
1844 TargetId: string(options.Args.Id),
1845 Request: &extension.GetValueRequest{
1846 Request: &extension.GetValueRequest_OltPortInfo{
1847 OltPortInfo: &extension.GetOltPortCounters{
1848 PortNo: options.Args.PortNo,
1849 PortType: portType,
1850 },
1851 },
1852 },
1853 }
1854
David K. Bainbridge9189c632021-03-26 21:52:21 +00001855 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
kesavand8ec4fc02021-01-27 09:10:22 -05001856 defer cancel()
1857 rv, err := client.GetExtValue(ctx, &singleGetValReq)
1858 if err != nil {
1859 Error.Printf("Error getting value on device Id %s,err=%s\n", options.Args.Id, ErrorToString(err))
1860 return err
1861 }
1862
1863 if rv.Response.Status != extension.GetValueResponse_OK {
1864 return fmt.Errorf("failed to get port stats %v", rv.Response.ErrReason.String())
1865 }
1866
1867 outputFormat := CharReplacer.Replace(options.Format)
1868 if outputFormat == "" {
1869 outputFormat = GetCommandOptionWithDefault("device-get-port-status", "format", DEFAULT_DEVICE_GET_PORT_STATUS_FORMAT)
1870 }
1871
1872 result := CommandResult{
1873 Format: format.Format(outputFormat),
1874 OutputAs: toOutputType(options.OutputAs),
1875 NameLimit: options.NameLimit,
1876 Data: rv.GetResponse().GetPortCoutners(),
1877 }
1878 GenerateOutput(&result)
1879 return nil
1880}
1881
kesavand6d1131f2021-02-05 22:38:15 +05301882func (options *UniStatus) Execute(args []string) error {
1883 conn, err := NewConnection()
1884 if err != nil {
1885 return err
1886 }
1887 defer conn.Close()
1888 client := extension.NewExtensionClient(conn)
1889
1890 singleGetValReq := extension.SingleGetValueRequest{
1891 TargetId: string(options.Args.Id),
1892 Request: &extension.GetValueRequest{
1893 Request: &extension.GetValueRequest_UniInfo{
1894 UniInfo: &extension.GetOnuUniInfoRequest{
1895 UniIndex: options.Args.UniIndex,
1896 },
1897 },
1898 },
1899 }
David K. Bainbridge9189c632021-03-26 21:52:21 +00001900 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
kesavand6d1131f2021-02-05 22:38:15 +05301901 defer cancel()
1902 rv, err := client.GetExtValue(ctx, &singleGetValReq)
1903 if err != nil {
1904 Error.Printf("Error getting value on device Id %s,err=%s\n", options.Args.Id, ErrorToString(err))
1905 return err
1906 }
1907 if rv.Response.Status != extension.GetValueResponse_OK {
1908 return fmt.Errorf("failed to get uni status %v", rv.Response.ErrReason.String())
1909 }
1910 outputFormat := CharReplacer.Replace(options.Format)
1911 if outputFormat == "" {
1912 outputFormat = GetCommandOptionWithDefault("device-get-uni-status", "format", DEFAULT_DEVICE_GET_UNI_STATUS_FORMAT)
1913 }
1914 result := CommandResult{
1915 Format: format.Format(outputFormat),
1916 OutputAs: toOutputType(options.OutputAs),
1917 NameLimit: options.NameLimit,
1918 Data: rv.GetResponse().GetUniInfo(),
1919 }
1920 GenerateOutput(&result)
1921 return nil
1922}
1923
Girish Gowdra4f5ce7c2021-04-29 18:53:21 -07001924func (options *OnuPonOpticalInfo) Execute(args []string) error {
1925 conn, err := NewConnection()
1926 if err != nil {
1927 return err
1928 }
1929 defer conn.Close()
1930 client := extension.NewExtensionClient(conn)
1931
1932 singleGetValReq := extension.SingleGetValueRequest{
1933 TargetId: string(options.Args.Id),
1934 Request: &extension.GetValueRequest{
1935 Request: &extension.GetValueRequest_OnuOpticalInfo{
1936 OnuOpticalInfo: &extension.GetOnuPonOpticalInfo{},
1937 },
1938 },
1939 }
1940 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
1941 defer cancel()
1942 rv, err := client.GetExtValue(ctx, &singleGetValReq)
1943 if err != nil {
1944 Error.Printf("Error getting value on device Id %s,err=%s\n", options.Args.Id, ErrorToString(err))
1945 return err
1946 }
1947 if rv.Response.Status != extension.GetValueResponse_OK {
1948 return fmt.Errorf("failed to get onu pon optical info %v", rv.Response.ErrReason.String())
1949 }
1950 outputFormat := CharReplacer.Replace(options.Format)
1951 if outputFormat == "" {
1952 outputFormat = GetCommandOptionWithDefault("device-get-onu-pon-optical-info", "format", DEFAULT_ONU_PON_OPTICAL_INFO_STATUS_FORMAT)
1953 }
1954 result := CommandResult{
1955 Format: format.Format(outputFormat),
1956 OutputAs: toOutputType(options.OutputAs),
1957 NameLimit: options.NameLimit,
1958 Data: rv.GetResponse().GetOnuOpticalInfo(),
1959 }
1960 GenerateOutput(&result)
1961 return nil
1962}
1963
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -08001964/*Device get Onu Distance */
1965func (options *DeviceGetExtValue) Execute(args []string) error {
1966 conn, err := NewConnection()
1967 if err != nil {
1968 return err
1969 }
1970 defer conn.Close()
1971
Scott Baker9173ed82020-05-19 08:30:12 -07001972 client := voltha.NewVolthaServiceClient(conn)
1973
1974 valueflag, okay := common.ValueType_Type_value[string(options.Args.Valueflag)]
1975 if !okay {
1976 Error.Printf("Unknown valueflag %s\n", options.Args.Valueflag)
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -08001977 }
1978
Scott Baker9173ed82020-05-19 08:30:12 -07001979 val := voltha.ValueSpecifier{Id: string(options.Args.Id), Value: common.ValueType_Type(valueflag)}
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -08001980
David K. Bainbridge9189c632021-03-26 21:52:21 +00001981 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -08001982 defer cancel()
1983
Scott Baker9173ed82020-05-19 08:30:12 -07001984 rv, err := client.GetExtValue(ctx, &val)
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -08001985 if err != nil {
1986 Error.Printf("Error getting value on device Id %s,err=%s\n", options.Args.Id, ErrorToString(err))
1987 return err
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -08001988 }
1989
Scott Baker9173ed82020-05-19 08:30:12 -07001990 var rows []ReturnValueRow
1991 for name, num := range common.ValueType_Type_value {
1992 if num == 0 {
1993 // EMPTY is not a real value
1994 continue
1995 }
1996 if (rv.Error & uint32(num)) != 0 {
1997 row := ReturnValueRow{Name: name, Result: "Error"}
1998 rows = append(rows, row)
1999 }
2000 if (rv.Unsupported & uint32(num)) != 0 {
2001 row := ReturnValueRow{Name: name, Result: "Unsupported"}
2002 rows = append(rows, row)
2003 }
2004 if (rv.Set & uint32(num)) != 0 {
2005 switch name {
2006 case "DISTANCE":
2007 row := ReturnValueRow{Name: name, Result: rv.Distance}
2008 rows = append(rows, row)
2009 default:
2010 row := ReturnValueRow{Name: name, Result: "Unimplemented-in-voltctl"}
2011 rows = append(rows, row)
2012 }
2013 }
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -08002014 }
2015
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -08002016 outputFormat := CharReplacer.Replace(options.Format)
2017 if outputFormat == "" {
2018 outputFormat = GetCommandOptionWithDefault("device-value-get", "format", DEFAULT_DEVICE_VALUE_GET_FORMAT)
2019 }
2020
2021 result := CommandResult{
2022 Format: format.Format(outputFormat),
2023 OutputAs: toOutputType(options.OutputAs),
2024 NameLimit: options.NameLimit,
Scott Baker9173ed82020-05-19 08:30:12 -07002025 Data: rows,
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -08002026 }
2027 GenerateOutput(&result)
2028 return nil
2029}