blob: dd687a1831fcf02b4ce63c25ad29f2a32fda07d9 [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}}`
Gamze Abakac857a462021-05-26 13:45:54 +000076 DEFAULT_RX_POWER_STATUS_FORMAT = `
77 INTF_ID: {{.IntfId}}
78 ONU_ID: {{.OnuId}}
79 STATUS: {{.Status}}
80 FAIL_REASON: {{.FailReason}}
81 RX_POWER : {{.RxPower}}`
Zack Williamse940c7a2019-08-21 14:25:39 -070082)
83
84type DeviceList struct {
85 ListOutputOptions
86}
87
88type DeviceCreate struct {
David Bainbridge1a514392020-06-23 11:12:51 -070089 DeviceType string `short:"t" required:"true" long:"devicetype" description:"Device type"`
David Bainbridge835dd0e2020-04-01 10:30:09 -070090 MACAddress string `short:"m" long:"macaddress" default:"" description:"MAC Address"`
Zack Williamse940c7a2019-08-21 14:25:39 -070091 IPAddress string `short:"i" long:"ipaddress" default:"" description:"IP Address"`
92 HostAndPort string `short:"H" long:"hostandport" default:"" description:"Host and port"`
93}
94
95type DeviceId string
96
Rohan Agrawal9228d2f2020-06-03 07:48:50 +000097type MetricName string
98type GroupName string
kesavand12cd8eb2020-01-20 22:25:22 -050099type PortNum uint32
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -0800100type ValueFlag string
kesavand12cd8eb2020-01-20 22:25:22 -0500101
Zack Williamse940c7a2019-08-21 14:25:39 -0700102type DeviceDelete struct {
Himani Chawla9933ddc2020-10-12 23:53:27 +0530103 Force bool `long:"force" description:"Delete device forcefully"`
104 Args struct {
Zack Williamse940c7a2019-08-21 14:25:39 -0700105 Ids []DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
106 } `positional-args:"yes"`
107}
108
109type DeviceEnable struct {
110 Args struct {
111 Ids []DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
112 } `positional-args:"yes"`
113}
114
115type DeviceDisable struct {
116 Args struct {
117 Ids []DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
118 } `positional-args:"yes"`
119}
120
121type DeviceReboot struct {
122 Args struct {
123 Ids []DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
124 } `positional-args:"yes"`
125}
126
127type DeviceFlowList struct {
128 ListOutputOptions
Maninder045921e2020-09-29 16:46:02 +0530129 FlowIdOptions
Zack Williamse940c7a2019-08-21 14:25:39 -0700130 Args struct {
131 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
132 } `positional-args:"yes"`
133}
134
Himani Chawla3c161c62021-05-13 16:36:51 +0530135type DeviceFlowGroupList struct {
136 ListOutputOptions
137 GroupListOptions
138 Args struct {
139 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
140 } `positional-args:"yes"`
141}
Zack Williamse940c7a2019-08-21 14:25:39 -0700142type DevicePortList struct {
143 ListOutputOptions
144 Args struct {
145 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
146 } `positional-args:"yes"`
147}
148
149type DeviceInspect struct {
150 OutputOptionsJson
151 Args struct {
152 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
153 } `positional-args:"yes"`
154}
155
kesavand12cd8eb2020-01-20 22:25:22 -0500156type DevicePortEnable struct {
157 Args struct {
158 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
159 PortId PortNum `positional-arg-name:"PORT_NUMBER" required:"yes"`
160 } `positional-args:"yes"`
161}
162
163type DevicePortDisable struct {
164 Args struct {
165 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
166 PortId PortNum `positional-arg-name:"PORT_NUMBER" required:"yes"`
167 } `positional-args:"yes"`
168}
169
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000170type DevicePmConfigsGet struct {
171 ListOutputOptions
172 Args struct {
173 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
174 } `positional-args:"yes"`
175}
176
177type DevicePmConfigMetricList struct {
178 ListOutputOptions
179 Args struct {
180 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
181 } `positional-args:"yes"`
182}
183
184type DevicePmConfigGroupList struct {
185 ListOutputOptions
186 Args struct {
187 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
188 } `positional-args:"yes"`
189}
190
191type DevicePmConfigGroupMetricList struct {
192 ListOutputOptions
193 Args struct {
194 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
195 Group GroupName `positional-arg-name:"GROUP_NAME" required:"yes"`
196 } `positional-args:"yes"`
197}
198
199type DevicePmConfigFrequencySet struct {
200 OutputOptions
201 Args struct {
Girish Gowdra610acb42021-01-27 13:33:57 -0800202 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
203 Interval time.Duration `positional-arg-name:"INTERVAL" required:"yes"`
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000204 } `positional-args:"yes"`
205}
206
207type DevicePmConfigMetricEnable struct {
208 Args struct {
209 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
210 Metrics []MetricName `positional-arg-name:"METRIC_NAME" required:"yes"`
211 } `positional-args:"yes"`
212}
213
214type DevicePmConfigMetricDisable struct {
215 Args struct {
216 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
217 Metrics []MetricName `positional-arg-name:"METRIC_NAME" required:"yes"`
218 } `positional-args:"yes"`
219}
220
221type DevicePmConfigGroupEnable struct {
222 Args struct {
Girish Gowdra610acb42021-01-27 13:33:57 -0800223 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
224 Group GroupName `positional-arg-name:"GROUP_NAME" required:"yes"`
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000225 } `positional-args:"yes"`
226}
227
228type DevicePmConfigGroupDisable struct {
229 Args struct {
Girish Gowdra610acb42021-01-27 13:33:57 -0800230 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
231 Group GroupName `positional-arg-name:"GROUP_NAME" required:"yes"`
232 } `positional-args:"yes"`
233}
234
235type DevicePmConfigGroupFrequencySet struct {
236 OutputOptions
237 Args struct {
238 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
239 Group GroupName `positional-arg-name:"GROUP_NAME" required:"yes"`
240 Interval time.Duration `positional-arg-name:"INTERVAL" required:"yes"`
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000241 } `positional-args:"yes"`
242}
243
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -0800244type DeviceGetExtValue struct {
245 ListOutputOptions
246 Args struct {
247 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
248 Valueflag ValueFlag `positional-arg-name:"VALUE_FLAG" required:"yes"`
249 } `positional-args:"yes"`
250}
Rohan Agrawald7df3772020-06-29 11:23:36 +0000251
252type DevicePmConfigSetMaxSkew struct {
253 Args struct {
254 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
255 MaxSkew uint32 `positional-arg-name:"MAX_SKEW" required:"yes"`
256 } `positional-args:"yes"`
257}
258
Andrea Campanella791d88b2021-01-08 13:29:00 +0100259type DeviceOnuListImages struct {
260 ListOutputOptions
261 Args struct {
262 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
263 } `positional-args:"yes"`
264}
265
266type DeviceOnuDownloadImage struct {
267 Args struct {
268 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
269 Name string `positional-arg-name:"IMAGE_NAME" required:"yes"`
270 Url string `positional-arg-name:"IMAGE_URL" required:"yes"`
271 ImageVersion string `positional-arg-name:"IMAGE_VERSION" required:"yes"`
272 Crc uint32 `positional-arg-name:"IMAGE_CRC" required:"yes"`
273 LocalDir string `positional-arg-name:"IMAGE_LOCAL_DIRECTORY"`
274 } `positional-args:"yes"`
275}
276
277type DeviceOnuActivateImageUpdate struct {
278 Args struct {
279 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
280 Name string `positional-arg-name:"IMAGE_NAME" required:"yes"`
281 ImageVersion string `positional-arg-name:"IMAGE_VERSION" required:"yes"`
282 SaveConfig bool `positional-arg-name:"SAVE_EXISTING_CONFIG"`
283 LocalDir string `positional-arg-name:"IMAGE_LOCAL_DIRECTORY"`
Andrea Campanella7b2ecf42021-02-25 12:27:15 +0100284 } `positional-args:"yes"`
kesavand8ec4fc02021-01-27 09:10:22 -0500285}
kesavand3e2f9f62021-04-22 11:06:38 +0530286
287type OnuDownloadImage struct {
Andrea Campanellaeaf1e0c2021-06-07 14:41:34 +0200288 ListOutputOptions
kesavand3e2f9f62021-04-22 11:06:38 +0530289 Args struct {
290 ImageVersion string `positional-arg-name:"IMAGE_VERSION" required:"yes"`
291 Url string `positional-arg-name:"IMAGE_URL" required:"yes"`
ssiddiqui7bc89e92021-05-20 20:58:02 +0530292 Vendor string `positional-arg-name:"IMAGE_VENDOR"`
kesavand3e2f9f62021-04-22 11:06:38 +0530293 ActivateOnSuccess bool `positional-arg-name:"IMAGE_ACTIVATE_ON_SUCCESS"`
294 CommitOnSuccess bool `positional-arg-name:"IMAGE_COMMIT_ON_SUCCESS"`
295 Crc uint32 `positional-arg-name:"IMAGE_CRC"`
296 IDs []DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
297 } `positional-args:"yes"`
298}
299
300type OnuActivateImage struct {
Andrea Campanellaeaf1e0c2021-06-07 14:41:34 +0200301 ListOutputOptions
kesavand3e2f9f62021-04-22 11:06:38 +0530302 Args struct {
303 ImageVersion string `positional-arg-name:"IMAGE_VERSION" required:"yes"`
304 CommitOnSuccess bool `positional-arg-name:"IMAGE_COMMIT_ON_SUCCESS"`
305 IDs []DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
306 } `positional-args:"yes"`
307}
308
309type OnuAbortUpgradeImage struct {
Andrea Campanellaeaf1e0c2021-06-07 14:41:34 +0200310 ListOutputOptions
kesavand3e2f9f62021-04-22 11:06:38 +0530311 Args struct {
312 ImageVersion string `positional-arg-name:"IMAGE_VERSION" required:"yes"`
313 IDs []DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
314 } `positional-args:"yes"`
315}
316
317type OnuCommitImage struct {
Andrea Campanellaeaf1e0c2021-06-07 14:41:34 +0200318 ListOutputOptions
kesavand3e2f9f62021-04-22 11:06:38 +0530319 Args struct {
320 ImageVersion string `positional-arg-name:"IMAGE_VERSION" required:"yes"`
321 IDs []DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
322 } `positional-args:"yes"`
323}
324
325type OnuImageStatus struct {
326 ListOutputOptions
327 Args struct {
328 ImageVersion string `positional-arg-name:"IMAGE_VERSION" required:"yes"`
329 IDs []DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
330 } `positional-args:"yes"`
331}
332
333type OnuListImages struct {
334 ListOutputOptions
335 Args struct {
336 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
337 } `positional-args:"yes"`
338}
339
kesavand8ec4fc02021-01-27 09:10:22 -0500340type DeviceGetPortStats struct {
341 ListOutputOptions
342 Args struct {
343 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
344 PortNo uint32 `positional-arg-name:"PORT_NO" required:"yes"`
345 PortType string `positional-arg-name:"PORT_TYPE" required:"yes"`
Andrea Campanella791d88b2021-01-08 13:29:00 +0100346 } `positional-args:"yes"`
347}
kesavand6d1131f2021-02-05 22:38:15 +0530348type UniStatus struct {
349 ListOutputOptions
350 Args struct {
351 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
352 UniIndex uint32 `positional-arg-name:"UNI_INDEX" required:"yes"`
353 } `positional-args:"yes"`
354}
Girish Gowdra4f5ce7c2021-04-29 18:53:21 -0700355type OnuPonOpticalInfo struct {
356 ListOutputOptions
357 Args struct {
358 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
359 } `positional-args:"yes"`
360}
Himani Chawla40acc122021-05-26 18:52:29 +0530361
362type GetOnuStats struct {
363 ListOutputOptions
364 Args struct {
365 OltId DeviceId `positional-arg-name:"OLT_DEVICE_ID" required:"yes"`
366 IntfId uint32 `positional-arg-name:"PON_INTF_ID" required:"yes"`
367 OnuId uint32 `positional-arg-name:"ONU_ID" required:"yes"`
368 } `positional-args:"yes"`
369}
370
Gamze Abakac857a462021-05-26 13:45:54 +0000371type RxPower struct {
372 ListOutputOptions
373 Args struct {
374 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
375 PortNo uint32 `positional-arg-name:"PORT_NO" required:"yes"`
376 OnuNo uint32 `positional-arg-name:"ONU_NO" required:"yes"`
377 } `positional-args:"yes"`
378}
379
Zack Williamse940c7a2019-08-21 14:25:39 -0700380type DeviceOpts struct {
Himani Chawla3c161c62021-05-13 16:36:51 +0530381 List DeviceList `command:"list"`
382 Create DeviceCreate `command:"create"`
383 Delete DeviceDelete `command:"delete"`
384 Enable DeviceEnable `command:"enable"`
385 Disable DeviceDisable `command:"disable"`
386 Flows DeviceFlowList `command:"flows"`
387 Groups DeviceFlowGroupList `command:"groups"`
kesavand12cd8eb2020-01-20 22:25:22 -0500388 Port struct {
389 List DevicePortList `command:"list"`
390 Enable DevicePortEnable `command:"enable"`
391 Disable DevicePortDisable `command:"disable"`
392 } `command:"port"`
393 Inspect DeviceInspect `command:"inspect"`
394 Reboot DeviceReboot `command:"reboot"`
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -0800395 Value struct {
396 Get DeviceGetExtValue `command:"get"`
397 } `command:"value"`
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000398 PmConfig struct {
Rohan Agrawald7df3772020-06-29 11:23:36 +0000399 Get DevicePmConfigsGet `command:"get"`
400 MaxSkew struct {
401 Set DevicePmConfigSetMaxSkew `command:"set"`
402 } `command:"maxskew"`
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000403 Frequency struct {
404 Set DevicePmConfigFrequencySet `command:"set"`
405 } `command:"frequency"`
406 Metric struct {
407 List DevicePmConfigMetricList `command:"list"`
408 Enable DevicePmConfigMetricEnable `command:"enable"`
409 Disable DevicePmConfigMetricDisable `command:"disable"`
410 } `command:"metric"`
411 Group struct {
Girish Gowdra610acb42021-01-27 13:33:57 -0800412 List DevicePmConfigGroupList `command:"list"`
413 Enable DevicePmConfigGroupEnable `command:"enable"`
414 Disable DevicePmConfigGroupDisable `command:"disable"`
415 Set DevicePmConfigGroupFrequencySet `command:"set"`
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000416 } `command:"group"`
417 GroupMetric struct {
418 List DevicePmConfigGroupMetricList `command:"list"`
419 } `command:"groupmetric"`
420 } `command:"pmconfig"`
Andrea Campanella791d88b2021-01-08 13:29:00 +0100421 Image struct {
422 Get DeviceOnuListImages `command:"list"`
423 Download DeviceOnuDownloadImage `command:"download"`
424 Activate DeviceOnuActivateImageUpdate `command:"activate"`
425 } `command:"image"`
kesavand3e2f9f62021-04-22 11:06:38 +0530426 DownloadImage struct {
427 Download OnuDownloadImage `command:"download"`
428 Activate OnuActivateImage `command:"activate"`
429 Commit OnuCommitImage `command:"commit"`
430 AbortUpgrade OnuAbortUpgradeImage `command:"abort"`
431 Status OnuImageStatus `command:"status"`
432 List OnuListImages `command:"list" `
433 } `command:"onuimage"`
kesavand8ec4fc02021-01-27 09:10:22 -0500434 GetExtVal struct {
Girish Gowdra4f5ce7c2021-04-29 18:53:21 -0700435 Stats DeviceGetPortStats `command:"portstats"`
436 UniStatus UniStatus `command:"unistatus"`
437 OpticalInfo OnuPonOpticalInfo `command:"onu_pon_optical_info"`
Himani Chawla40acc122021-05-26 18:52:29 +0530438 OnuStats GetOnuStats `command:"onu_stats"`
Gamze Abakac857a462021-05-26 13:45:54 +0000439 RxPower RxPower `command:"rxpower"`
kesavand8ec4fc02021-01-27 09:10:22 -0500440 } `command:"getextval"`
Zack Williamse940c7a2019-08-21 14:25:39 -0700441}
442
443var deviceOpts = DeviceOpts{}
444
445func RegisterDeviceCommands(parser *flags.Parser) {
David Bainbridge12f036f2019-10-15 22:09:04 +0000446 if _, err := parser.AddCommand("device", "device commands", "Commands to query and manipulate VOLTHA devices", &deviceOpts); err != nil {
David Bainbridgea6722342019-10-24 23:55:53 +0000447 Error.Fatalf("Unexpected error while attempting to register device commands : %s", err)
David Bainbridge12f036f2019-10-15 22:09:04 +0000448 }
Zack Williamse940c7a2019-08-21 14:25:39 -0700449}
450
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000451func (i *MetricName) Complete(match string) []flags.Completion {
452 conn, err := NewConnection()
453 if err != nil {
454 return nil
455 }
456 defer conn.Close()
457
458 client := voltha.NewVolthaServiceClient(conn)
459
460 var deviceId string
461found:
462 for i := len(os.Args) - 1; i >= 0; i -= 1 {
463 switch os.Args[i] {
464 case "enable":
465 fallthrough
466 case "disable":
467 if len(os.Args) > i+1 {
468 deviceId = os.Args[i+1]
469 } else {
470 return nil
471 }
472 break found
473 default:
474 }
475 }
476
477 if len(deviceId) == 0 {
478 return nil
479 }
480
David K. Bainbridge9189c632021-03-26 21:52:21 +0000481 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000482 defer cancel()
483
484 id := voltha.ID{Id: string(deviceId)}
485
486 pmconfigs, err := client.ListDevicePmConfigs(ctx, &id)
487
488 if err != nil {
489 return nil
490 }
491
492 list := make([]flags.Completion, 0)
493 for _, metrics := range pmconfigs.Metrics {
494 if strings.HasPrefix(metrics.Name, match) {
495 list = append(list, flags.Completion{Item: metrics.Name})
496 }
497 }
498
499 return list
500}
501
502func (i *GroupName) Complete(match string) []flags.Completion {
503 conn, err := NewConnection()
504 if err != nil {
505 return nil
506 }
507 defer conn.Close()
508
509 client := voltha.NewVolthaServiceClient(conn)
510
511 var deviceId string
512found:
513 for i := len(os.Args) - 1; i >= 0; i -= 1 {
514 switch os.Args[i] {
515 case "list":
516 fallthrough
517 case "enable":
518 fallthrough
519 case "disable":
520 if len(os.Args) > i+1 {
521 deviceId = os.Args[i+1]
522 } else {
523 return nil
524 }
525 break found
526 default:
527 }
528 }
529
530 if len(deviceId) == 0 {
531 return nil
532 }
533
David K. Bainbridge9189c632021-03-26 21:52:21 +0000534 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000535 defer cancel()
536
537 id := voltha.ID{Id: string(deviceId)}
538
539 pmconfigs, err := client.ListDevicePmConfigs(ctx, &id)
540
541 if err != nil {
542 return nil
543 }
544
545 list := make([]flags.Completion, 0)
546 for _, group := range pmconfigs.Groups {
547 if strings.HasPrefix(group.GroupName, match) {
548 list = append(list, flags.Completion{Item: group.GroupName})
549 }
550 }
551 return list
552}
553
kesavand12cd8eb2020-01-20 22:25:22 -0500554func (i *PortNum) Complete(match string) []flags.Completion {
555 conn, err := NewConnection()
556 if err != nil {
557 return nil
558 }
559 defer conn.Close()
560
Scott Baker9173ed82020-05-19 08:30:12 -0700561 client := voltha.NewVolthaServiceClient(conn)
kesavand12cd8eb2020-01-20 22:25:22 -0500562
563 /*
564 * The command line args when completing for PortNum will be a DeviceId
565 * followed by one or more PortNums. So walk the argument list from the
566 * end and find the first argument that is enable/disable as those are
567 * the subcommands that come before the positional arguments. It would
568 * be nice if this package gave us the list of optional arguments
569 * already parsed.
570 */
571 var deviceId string
572found:
573 for i := len(os.Args) - 1; i >= 0; i -= 1 {
574 switch os.Args[i] {
575 case "enable":
576 fallthrough
577 case "disable":
578 if len(os.Args) > i+1 {
579 deviceId = os.Args[i+1]
580 } else {
581 return nil
582 }
583 break found
584 default:
585 }
586 }
587
588 if len(deviceId) == 0 {
589 return nil
590 }
591
David K. Bainbridge9189c632021-03-26 21:52:21 +0000592 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
kesavand12cd8eb2020-01-20 22:25:22 -0500593 defer cancel()
kesavand12cd8eb2020-01-20 22:25:22 -0500594
Scott Baker9173ed82020-05-19 08:30:12 -0700595 id := voltha.ID{Id: string(deviceId)}
kesavand12cd8eb2020-01-20 22:25:22 -0500596
Scott Baker9173ed82020-05-19 08:30:12 -0700597 ports, err := client.ListDevicePorts(ctx, &id)
kesavand12cd8eb2020-01-20 22:25:22 -0500598 if err != nil {
599 return nil
600 }
601
602 list := make([]flags.Completion, 0)
Scott Baker9173ed82020-05-19 08:30:12 -0700603 for _, item := range ports.Items {
604 pn := strconv.FormatUint(uint64(item.PortNo), 10)
kesavand12cd8eb2020-01-20 22:25:22 -0500605 if strings.HasPrefix(pn, match) {
606 list = append(list, flags.Completion{Item: pn})
607 }
608 }
609
610 return list
611}
612
Zack Williamse940c7a2019-08-21 14:25:39 -0700613func (i *DeviceId) Complete(match string) []flags.Completion {
614 conn, err := NewConnection()
615 if err != nil {
616 return nil
617 }
618 defer conn.Close()
619
Scott Baker9173ed82020-05-19 08:30:12 -0700620 client := voltha.NewVolthaServiceClient(conn)
Zack Williamse940c7a2019-08-21 14:25:39 -0700621
David K. Bainbridge9189c632021-03-26 21:52:21 +0000622 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Zack Williamse940c7a2019-08-21 14:25:39 -0700623 defer cancel()
624
Scott Baker9173ed82020-05-19 08:30:12 -0700625 devices, err := client.ListDevices(ctx, &empty.Empty{})
Zack Williamse940c7a2019-08-21 14:25:39 -0700626 if err != nil {
627 return nil
628 }
629
630 list := make([]flags.Completion, 0)
Scott Baker9173ed82020-05-19 08:30:12 -0700631 for _, item := range devices.Items {
632 if strings.HasPrefix(item.Id, match) {
633 list = append(list, flags.Completion{Item: item.Id})
Zack Williamse940c7a2019-08-21 14:25:39 -0700634 }
635 }
636
637 return list
638}
639
640func (options *DeviceList) Execute(args []string) error {
641
642 conn, err := NewConnection()
643 if err != nil {
644 return err
645 }
646 defer conn.Close()
647
Scott Baker9173ed82020-05-19 08:30:12 -0700648 client := voltha.NewVolthaServiceClient(conn)
Zack Williamse940c7a2019-08-21 14:25:39 -0700649
David K. Bainbridge9189c632021-03-26 21:52:21 +0000650 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Zack Williamse940c7a2019-08-21 14:25:39 -0700651 defer cancel()
652
Scott Baker9173ed82020-05-19 08:30:12 -0700653 devices, err := client.ListDevices(ctx, &empty.Empty{})
Zack Williamse940c7a2019-08-21 14:25:39 -0700654 if err != nil {
655 return err
656 }
657
658 outputFormat := CharReplacer.Replace(options.Format)
659 if outputFormat == "" {
David Bainbridgea6722342019-10-24 23:55:53 +0000660 outputFormat = GetCommandOptionWithDefault("device-list", "format", DEFAULT_DEVICE_FORMAT)
Zack Williamse940c7a2019-08-21 14:25:39 -0700661 }
662 if options.Quiet {
663 outputFormat = "{{.Id}}"
664 }
665
David Bainbridgea6722342019-10-24 23:55:53 +0000666 orderBy := options.OrderBy
667 if orderBy == "" {
668 orderBy = GetCommandOptionWithDefault("device-list", "order", "")
669 }
670
Scott Baker9173ed82020-05-19 08:30:12 -0700671 // Make sure json output prints an empty list, not "null"
672 if devices.Items == nil {
673 devices.Items = make([]*voltha.Device, 0)
Zack Williamse940c7a2019-08-21 14:25:39 -0700674 }
675
676 result := CommandResult{
677 Format: format.Format(outputFormat),
678 Filter: options.Filter,
David Bainbridgea6722342019-10-24 23:55:53 +0000679 OrderBy: orderBy,
Zack Williamse940c7a2019-08-21 14:25:39 -0700680 OutputAs: toOutputType(options.OutputAs),
681 NameLimit: options.NameLimit,
Scott Baker9173ed82020-05-19 08:30:12 -0700682 Data: devices.Items,
Zack Williamse940c7a2019-08-21 14:25:39 -0700683 }
684
685 GenerateOutput(&result)
686 return nil
687}
688
689func (options *DeviceCreate) Execute(args []string) error {
690
Scott Baker9173ed82020-05-19 08:30:12 -0700691 device := voltha.Device{}
Zack Williamse940c7a2019-08-21 14:25:39 -0700692 if options.HostAndPort != "" {
Scott Baker9173ed82020-05-19 08:30:12 -0700693 device.Address = &voltha.Device_HostAndPort{HostAndPort: options.HostAndPort}
Zack Williamse940c7a2019-08-21 14:25:39 -0700694 } else if options.IPAddress != "" {
Scott Baker9173ed82020-05-19 08:30:12 -0700695 device.Address = &voltha.Device_Ipv4Address{Ipv4Address: options.IPAddress}
Hardik Windlassce1de342020-02-04 21:58:07 +0000696 }
697 if options.MACAddress != "" {
Scott Baker9173ed82020-05-19 08:30:12 -0700698 device.MacAddress = strings.ToLower(options.MACAddress)
Zack Williamse940c7a2019-08-21 14:25:39 -0700699 }
700 if options.DeviceType != "" {
Scott Baker9173ed82020-05-19 08:30:12 -0700701 device.Type = options.DeviceType
Zack Williamse940c7a2019-08-21 14:25:39 -0700702 }
703
704 conn, err := NewConnection()
705 if err != nil {
706 return err
707 }
708 defer conn.Close()
709
Scott Baker9173ed82020-05-19 08:30:12 -0700710 client := voltha.NewVolthaServiceClient(conn)
Zack Williamse940c7a2019-08-21 14:25:39 -0700711
David K. Bainbridge9189c632021-03-26 21:52:21 +0000712 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Zack Williamse940c7a2019-08-21 14:25:39 -0700713 defer cancel()
714
Scott Baker9173ed82020-05-19 08:30:12 -0700715 createdDevice, err := client.CreateDevice(ctx, &device)
Zack Williamse940c7a2019-08-21 14:25:39 -0700716 if err != nil {
717 return err
Zack Williamse940c7a2019-08-21 14:25:39 -0700718 }
719
Scott Baker9173ed82020-05-19 08:30:12 -0700720 fmt.Printf("%s\n", createdDevice.Id)
Zack Williamse940c7a2019-08-21 14:25:39 -0700721
722 return nil
723}
724
725func (options *DeviceDelete) Execute(args []string) error {
726
727 conn, err := NewConnection()
728 if err != nil {
729 return err
730 }
731 defer conn.Close()
732
Scott Baker9173ed82020-05-19 08:30:12 -0700733 client := voltha.NewVolthaServiceClient(conn)
David Bainbridge7052fe82020-03-25 10:37:00 -0700734 var lastErr error
Zack Williamse940c7a2019-08-21 14:25:39 -0700735 for _, i := range options.Args.Ids {
David K. Bainbridge9189c632021-03-26 21:52:21 +0000736 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Zack Williamse940c7a2019-08-21 14:25:39 -0700737 defer cancel()
738
Scott Baker9173ed82020-05-19 08:30:12 -0700739 id := voltha.ID{Id: string(i)}
Himani Chawla9933ddc2020-10-12 23:53:27 +0530740 if options.Force {
741 _, err = client.ForceDeleteDevice(ctx, &id)
742 } else {
743 _, err = client.DeleteDevice(ctx, &id)
744 }
Scott Baker9173ed82020-05-19 08:30:12 -0700745
Zack Williamse940c7a2019-08-21 14:25:39 -0700746 if err != nil {
David Bainbridge0f758d42019-10-26 05:17:48 +0000747 Error.Printf("Error while deleting '%s': %s\n", i, err)
David Bainbridge7052fe82020-03-25 10:37:00 -0700748 lastErr = err
Zack Williamse940c7a2019-08-21 14:25:39 -0700749 continue
Zack Williamse940c7a2019-08-21 14:25:39 -0700750 }
751 fmt.Printf("%s\n", i)
752 }
753
David Bainbridge7052fe82020-03-25 10:37:00 -0700754 if lastErr != nil {
755 return NoReportErr
756 }
Zack Williamse940c7a2019-08-21 14:25:39 -0700757 return nil
758}
759
760func (options *DeviceEnable) Execute(args []string) error {
761 conn, err := NewConnection()
762 if err != nil {
763 return err
764 }
765 defer conn.Close()
766
Scott Baker9173ed82020-05-19 08:30:12 -0700767 client := voltha.NewVolthaServiceClient(conn)
Zack Williamse940c7a2019-08-21 14:25:39 -0700768
David Bainbridge7052fe82020-03-25 10:37:00 -0700769 var lastErr error
Zack Williamse940c7a2019-08-21 14:25:39 -0700770 for _, i := range options.Args.Ids {
David K. Bainbridge9189c632021-03-26 21:52:21 +0000771 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Zack Williamse940c7a2019-08-21 14:25:39 -0700772 defer cancel()
773
Scott Baker9173ed82020-05-19 08:30:12 -0700774 id := voltha.ID{Id: string(i)}
775
776 _, err := client.EnableDevice(ctx, &id)
Zack Williamse940c7a2019-08-21 14:25:39 -0700777 if err != nil {
David Bainbridge0f758d42019-10-26 05:17:48 +0000778 Error.Printf("Error while enabling '%s': %s\n", i, err)
David Bainbridge7052fe82020-03-25 10:37:00 -0700779 lastErr = err
Zack Williamse940c7a2019-08-21 14:25:39 -0700780 continue
Zack Williamse940c7a2019-08-21 14:25:39 -0700781 }
782 fmt.Printf("%s\n", i)
783 }
784
David Bainbridge7052fe82020-03-25 10:37:00 -0700785 if lastErr != nil {
786 return NoReportErr
787 }
Zack Williamse940c7a2019-08-21 14:25:39 -0700788 return nil
789}
790
791func (options *DeviceDisable) Execute(args []string) error {
792 conn, err := NewConnection()
793 if err != nil {
794 return err
795 }
796 defer conn.Close()
797
Scott Baker9173ed82020-05-19 08:30:12 -0700798 client := voltha.NewVolthaServiceClient(conn)
Zack Williamse940c7a2019-08-21 14:25:39 -0700799
David Bainbridge7052fe82020-03-25 10:37:00 -0700800 var lastErr error
Zack Williamse940c7a2019-08-21 14:25:39 -0700801 for _, i := range options.Args.Ids {
David K. Bainbridge9189c632021-03-26 21:52:21 +0000802 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Zack Williamse940c7a2019-08-21 14:25:39 -0700803 defer cancel()
804
Scott Baker9173ed82020-05-19 08:30:12 -0700805 id := voltha.ID{Id: string(i)}
806
807 _, err := client.DisableDevice(ctx, &id)
Zack Williamse940c7a2019-08-21 14:25:39 -0700808 if err != nil {
David Bainbridge0f758d42019-10-26 05:17:48 +0000809 Error.Printf("Error while disabling '%s': %s\n", i, err)
David Bainbridge7052fe82020-03-25 10:37:00 -0700810 lastErr = err
Zack Williamse940c7a2019-08-21 14:25:39 -0700811 continue
Zack Williamse940c7a2019-08-21 14:25:39 -0700812 }
813 fmt.Printf("%s\n", i)
814 }
815
David Bainbridge7052fe82020-03-25 10:37:00 -0700816 if lastErr != nil {
817 return NoReportErr
818 }
Zack Williamse940c7a2019-08-21 14:25:39 -0700819 return nil
820}
821
822func (options *DeviceReboot) Execute(args []string) error {
823 conn, err := NewConnection()
824 if err != nil {
825 return err
826 }
827 defer conn.Close()
828
Scott Baker9173ed82020-05-19 08:30:12 -0700829 client := voltha.NewVolthaServiceClient(conn)
Zack Williamse940c7a2019-08-21 14:25:39 -0700830
David Bainbridge7052fe82020-03-25 10:37:00 -0700831 var lastErr error
Zack Williamse940c7a2019-08-21 14:25:39 -0700832 for _, i := range options.Args.Ids {
David K. Bainbridge9189c632021-03-26 21:52:21 +0000833 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Zack Williamse940c7a2019-08-21 14:25:39 -0700834 defer cancel()
835
Scott Baker9173ed82020-05-19 08:30:12 -0700836 id := voltha.ID{Id: string(i)}
837
838 _, err := client.RebootDevice(ctx, &id)
Zack Williamse940c7a2019-08-21 14:25:39 -0700839 if err != nil {
David Bainbridge0f758d42019-10-26 05:17:48 +0000840 Error.Printf("Error while rebooting '%s': %s\n", i, err)
David Bainbridge7052fe82020-03-25 10:37:00 -0700841 lastErr = err
Zack Williamse940c7a2019-08-21 14:25:39 -0700842 continue
Zack Williamse940c7a2019-08-21 14:25:39 -0700843 }
844 fmt.Printf("%s\n", i)
845 }
846
David Bainbridge7052fe82020-03-25 10:37:00 -0700847 if lastErr != nil {
848 return NoReportErr
849 }
Zack Williamse940c7a2019-08-21 14:25:39 -0700850 return nil
851}
852
853func (options *DevicePortList) Execute(args []string) error {
854
855 conn, err := NewConnection()
856 if err != nil {
857 return err
858 }
859 defer conn.Close()
860
Scott Baker9173ed82020-05-19 08:30:12 -0700861 client := voltha.NewVolthaServiceClient(conn)
Zack Williamse940c7a2019-08-21 14:25:39 -0700862
David K. Bainbridge9189c632021-03-26 21:52:21 +0000863 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Zack Williamse940c7a2019-08-21 14:25:39 -0700864 defer cancel()
865
Scott Baker9173ed82020-05-19 08:30:12 -0700866 id := voltha.ID{Id: string(options.Args.Id)}
Zack Williamse940c7a2019-08-21 14:25:39 -0700867
Scott Baker9173ed82020-05-19 08:30:12 -0700868 ports, err := client.ListDevicePorts(ctx, &id)
Zack Williamse940c7a2019-08-21 14:25:39 -0700869 if err != nil {
870 return err
871 }
872
873 outputFormat := CharReplacer.Replace(options.Format)
874 if outputFormat == "" {
David Bainbridgea6722342019-10-24 23:55:53 +0000875 outputFormat = GetCommandOptionWithDefault("device-ports", "format", DEFAULT_DEVICE_PORTS_FORMAT)
Zack Williamse940c7a2019-08-21 14:25:39 -0700876 }
Zack Williamse940c7a2019-08-21 14:25:39 -0700877
David Bainbridgea6722342019-10-24 23:55:53 +0000878 orderBy := options.OrderBy
879 if orderBy == "" {
880 orderBy = GetCommandOptionWithDefault("device-ports", "order", "")
881 }
882
Zack Williamse940c7a2019-08-21 14:25:39 -0700883 result := CommandResult{
884 Format: format.Format(outputFormat),
885 Filter: options.Filter,
David Bainbridgea6722342019-10-24 23:55:53 +0000886 OrderBy: orderBy,
Zack Williamse940c7a2019-08-21 14:25:39 -0700887 OutputAs: toOutputType(options.OutputAs),
888 NameLimit: options.NameLimit,
Scott Baker9173ed82020-05-19 08:30:12 -0700889 Data: ports.Items,
Zack Williamse940c7a2019-08-21 14:25:39 -0700890 }
891
892 GenerateOutput(&result)
893 return nil
894}
895
896func (options *DeviceFlowList) Execute(args []string) error {
897 fl := &FlowList{}
898 fl.ListOutputOptions = options.ListOutputOptions
Maninder045921e2020-09-29 16:46:02 +0530899 fl.FlowIdOptions = options.FlowIdOptions
Zack Williamse940c7a2019-08-21 14:25:39 -0700900 fl.Args.Id = string(options.Args.Id)
David Bainbridgea6722342019-10-24 23:55:53 +0000901 fl.Method = "device-flows"
Zack Williamse940c7a2019-08-21 14:25:39 -0700902 return fl.Execute(args)
903}
904
Himani Chawla3c161c62021-05-13 16:36:51 +0530905func (options *DeviceFlowGroupList) Execute(args []string) error {
906 grp := &GroupList{}
907 grp.ListOutputOptions = options.ListOutputOptions
908 grp.GroupListOptions = options.GroupListOptions
909 grp.Args.Id = string(options.Args.Id)
910 grp.Method = "device-groups"
911 return grp.Execute(args)
912}
913
Zack Williamse940c7a2019-08-21 14:25:39 -0700914func (options *DeviceInspect) Execute(args []string) error {
915 if len(args) > 0 {
916 return fmt.Errorf("only a single argument 'DEVICE_ID' can be provided")
917 }
918
919 conn, err := NewConnection()
920 if err != nil {
921 return err
922 }
923 defer conn.Close()
924
Scott Baker9173ed82020-05-19 08:30:12 -0700925 client := voltha.NewVolthaServiceClient(conn)
Zack Williamse940c7a2019-08-21 14:25:39 -0700926
David K. Bainbridge9189c632021-03-26 21:52:21 +0000927 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Zack Williamse940c7a2019-08-21 14:25:39 -0700928 defer cancel()
929
Scott Baker9173ed82020-05-19 08:30:12 -0700930 id := voltha.ID{Id: string(options.Args.Id)}
Zack Williamse940c7a2019-08-21 14:25:39 -0700931
Scott Baker9173ed82020-05-19 08:30:12 -0700932 device, err := client.GetDevice(ctx, &id)
Zack Williamse940c7a2019-08-21 14:25:39 -0700933 if err != nil {
934 return err
935 }
936
Zack Williamse940c7a2019-08-21 14:25:39 -0700937 outputFormat := CharReplacer.Replace(options.Format)
938 if outputFormat == "" {
David Bainbridgea6722342019-10-24 23:55:53 +0000939 outputFormat = GetCommandOptionWithDefault("device-inspect", "format", DEFAULT_DEVICE_INSPECT_FORMAT)
Zack Williamse940c7a2019-08-21 14:25:39 -0700940 }
941 if options.Quiet {
942 outputFormat = "{{.Id}}"
943 }
944
945 result := CommandResult{
946 Format: format.Format(outputFormat),
947 OutputAs: toOutputType(options.OutputAs),
948 NameLimit: options.NameLimit,
949 Data: device,
950 }
951 GenerateOutput(&result)
952 return nil
953}
kesavand12cd8eb2020-01-20 22:25:22 -0500954
955/*Device Port Enable */
956func (options *DevicePortEnable) Execute(args []string) error {
957 conn, err := NewConnection()
958 if err != nil {
959 return err
960 }
961 defer conn.Close()
962
Scott Baker9173ed82020-05-19 08:30:12 -0700963 client := voltha.NewVolthaServiceClient(conn)
kesavand12cd8eb2020-01-20 22:25:22 -0500964
David K. Bainbridge9189c632021-03-26 21:52:21 +0000965 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
kesavand12cd8eb2020-01-20 22:25:22 -0500966 defer cancel()
967
Scott Baker9173ed82020-05-19 08:30:12 -0700968 port := voltha.Port{DeviceId: string(options.Args.Id), PortNo: uint32(options.Args.PortId)}
969
970 _, err = client.EnablePort(ctx, &port)
kesavand12cd8eb2020-01-20 22:25:22 -0500971 if err != nil {
972 Error.Printf("Error enabling port number %v on device Id %s,err=%s\n", options.Args.PortId, options.Args.Id, ErrorToString(err))
973 return err
kesavand12cd8eb2020-01-20 22:25:22 -0500974 }
975
976 return nil
977}
978
Scott Baker9173ed82020-05-19 08:30:12 -0700979/*Device Port Disable */
kesavand12cd8eb2020-01-20 22:25:22 -0500980func (options *DevicePortDisable) Execute(args []string) error {
981 conn, err := NewConnection()
982 if err != nil {
983 return err
984 }
985 defer conn.Close()
986
Scott Baker9173ed82020-05-19 08:30:12 -0700987 client := voltha.NewVolthaServiceClient(conn)
988
David K. Bainbridge9189c632021-03-26 21:52:21 +0000989 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
kesavand12cd8eb2020-01-20 22:25:22 -0500990 defer cancel()
991
Scott Baker9173ed82020-05-19 08:30:12 -0700992 port := voltha.Port{DeviceId: string(options.Args.Id), PortNo: uint32(options.Args.PortId)}
993
994 _, err = client.DisablePort(ctx, &port)
kesavand12cd8eb2020-01-20 22:25:22 -0500995 if err != nil {
Scott Baker9173ed82020-05-19 08:30:12 -0700996 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 -0500997 return err
kesavand12cd8eb2020-01-20 22:25:22 -0500998 }
Scott Baker9173ed82020-05-19 08:30:12 -0700999
kesavand12cd8eb2020-01-20 22:25:22 -05001000 return nil
1001}
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -08001002
Rohan Agrawald7df3772020-06-29 11:23:36 +00001003func (options *DevicePmConfigSetMaxSkew) Execute(args []string) error {
1004 conn, err := NewConnection()
1005 if err != nil {
1006 return err
1007 }
1008 defer conn.Close()
1009
1010 client := voltha.NewVolthaServiceClient(conn)
1011
David K. Bainbridge9189c632021-03-26 21:52:21 +00001012 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Rohan Agrawald7df3772020-06-29 11:23:36 +00001013 defer cancel()
1014
1015 id := voltha.ID{Id: string(options.Args.Id)}
1016
1017 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
1018 if err != nil {
1019 return err
1020 }
1021
1022 pmConfigs.MaxSkew = options.Args.MaxSkew
1023
1024 _, err = client.UpdateDevicePmConfigs(ctx, pmConfigs)
1025 if err != nil {
1026 return err
1027 }
1028
1029 return nil
1030}
1031
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001032func (options *DevicePmConfigsGet) Execute(args []string) error {
1033
1034 conn, err := NewConnection()
1035 if err != nil {
1036 return err
1037 }
1038 defer conn.Close()
1039
1040 client := voltha.NewVolthaServiceClient(conn)
1041
David K. Bainbridge9189c632021-03-26 21:52:21 +00001042 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001043 defer cancel()
1044
1045 id := voltha.ID{Id: string(options.Args.Id)}
1046
1047 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
1048 if err != nil {
1049 return err
1050 }
1051
1052 outputFormat := CharReplacer.Replace(options.Format)
1053 if outputFormat == "" {
1054 outputFormat = GetCommandOptionWithDefault("device-pm-configs", "format", DEFAULT_DEVICE_PM_CONFIG_GET_FORMAT)
1055 }
1056
1057 orderBy := options.OrderBy
1058 if orderBy == "" {
1059 orderBy = GetCommandOptionWithDefault("device-pm-configs", "order", "")
1060 }
1061
1062 result := CommandResult{
1063 Format: format.Format(outputFormat),
1064 Filter: options.Filter,
1065 OrderBy: orderBy,
1066 OutputAs: toOutputType(options.OutputAs),
1067 NameLimit: options.NameLimit,
1068 Data: pmConfigs,
1069 }
1070
1071 GenerateOutput(&result)
1072 return nil
1073
1074}
1075
1076func (options *DevicePmConfigMetricList) Execute(args []string) error {
1077
1078 conn, err := NewConnection()
1079 if err != nil {
1080 return err
1081 }
1082 defer conn.Close()
1083
1084 client := voltha.NewVolthaServiceClient(conn)
1085
David K. Bainbridge9189c632021-03-26 21:52:21 +00001086 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001087 defer cancel()
1088
1089 id := voltha.ID{Id: string(options.Args.Id)}
1090
1091 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
1092 if err != nil {
1093 return err
1094 }
1095
1096 if !pmConfigs.Grouped {
1097 for _, metric := range pmConfigs.Metrics {
1098 if metric.SampleFreq == 0 {
1099 metric.SampleFreq = pmConfigs.DefaultFreq
1100 }
1101 }
Rohan Agrawalbca69122020-06-17 14:59:03 +00001102 outputFormat := CharReplacer.Replace(options.Format)
1103 if outputFormat == "" {
1104 outputFormat = GetCommandOptionWithDefault("device-pm-configs", "format", DEFAULT_DEVICE_PM_CONFIG_METRIC_LIST_FORMAT)
1105 }
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001106
Rohan Agrawalbca69122020-06-17 14:59:03 +00001107 orderBy := options.OrderBy
1108 if orderBy == "" {
1109 orderBy = GetCommandOptionWithDefault("device-pm-configs", "order", "")
1110 }
1111
1112 result := CommandResult{
1113 Format: format.Format(outputFormat),
1114 Filter: options.Filter,
1115 OrderBy: orderBy,
1116 OutputAs: toOutputType(options.OutputAs),
1117 NameLimit: options.NameLimit,
1118 Data: pmConfigs.Metrics,
1119 }
1120
1121 GenerateOutput(&result)
1122 return nil
1123 } else {
1124 return fmt.Errorf("Device '%s' does not have Non Grouped Metrics", options.Args.Id)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001125 }
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001126}
1127
1128func (options *DevicePmConfigMetricEnable) Execute(args []string) error {
1129
1130 conn, err := NewConnection()
1131 if err != nil {
1132 return err
1133 }
1134 defer conn.Close()
1135
1136 client := voltha.NewVolthaServiceClient(conn)
1137
David K. Bainbridge9189c632021-03-26 21:52:21 +00001138 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001139 defer cancel()
1140
1141 id := voltha.ID{Id: string(options.Args.Id)}
1142
1143 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
1144 if err != nil {
1145 return err
1146 }
1147
1148 if !pmConfigs.Grouped {
Rohan Agrawalbca69122020-06-17 14:59:03 +00001149 metrics := make(map[string]struct{})
1150 for _, metric := range pmConfigs.Metrics {
1151 metrics[metric.Name] = struct{}{}
1152 }
1153
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001154 for _, metric := range pmConfigs.Metrics {
1155 for _, mName := range options.Args.Metrics {
Rohan Agrawalbca69122020-06-17 14:59:03 +00001156 if _, exist := metrics[string(mName)]; !exist {
1157 return fmt.Errorf("Metric Name '%s' does not exist", mName)
1158 }
1159
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001160 if string(mName) == metric.Name && !metric.Enabled {
1161 metric.Enabled = true
1162 _, err := client.UpdateDevicePmConfigs(ctx, pmConfigs)
1163 if err != nil {
1164 return err
1165 }
1166 }
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 *DevicePmConfigMetricDisable) 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 metrics := make(map[string]struct{})
1197 for _, metric := range pmConfigs.Metrics {
1198 metrics[metric.Name] = struct{}{}
1199 }
1200
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001201 for _, metric := range pmConfigs.Metrics {
1202 for _, mName := range options.Args.Metrics {
Rohan Agrawalbca69122020-06-17 14:59:03 +00001203 if _, have := metrics[string(mName)]; !have {
1204 return fmt.Errorf("Metric Name '%s' does not exist", mName)
1205 }
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001206 if string(mName) == metric.Name && metric.Enabled {
1207 metric.Enabled = false
1208 _, err := client.UpdateDevicePmConfigs(ctx, pmConfigs)
1209 if err != nil {
1210 return err
1211 }
Rohan Agrawalbca69122020-06-17 14:59:03 +00001212 } else {
1213 return fmt.Errorf("Metric '%s' cannot be disabled", string(mName))
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001214 }
1215 }
1216 }
Rohan Agrawalbca69122020-06-17 14:59:03 +00001217 } else {
1218 return fmt.Errorf("Device '%s' does not have Non Grouped Metrics", options.Args.Id)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001219 }
1220 return nil
1221}
1222
1223func (options *DevicePmConfigGroupEnable) Execute(args []string) error {
1224
1225 conn, err := NewConnection()
1226 if err != nil {
1227 return err
1228 }
1229 defer conn.Close()
1230
1231 client := voltha.NewVolthaServiceClient(conn)
1232
David K. Bainbridge9189c632021-03-26 21:52:21 +00001233 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001234 defer cancel()
1235
1236 id := voltha.ID{Id: string(options.Args.Id)}
1237
1238 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
1239 if err != nil {
1240 return err
1241 }
1242
1243 if pmConfigs.Grouped {
Rohan Agrawalbca69122020-06-17 14:59:03 +00001244 groups := make(map[string]struct{})
1245 for _, group := range pmConfigs.Groups {
1246 groups[group.GroupName] = struct{}{}
1247 }
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001248 for _, group := range pmConfigs.Groups {
Girish Gowdra610acb42021-01-27 13:33:57 -08001249 if _, have := groups[string(options.Args.Group)]; !have {
1250 return fmt.Errorf("Group Name '%s' does not exist", options.Args.Group)
1251 }
1252 if string(options.Args.Group) == group.GroupName && !group.Enabled {
1253 group.Enabled = true
1254 _, err := client.UpdateDevicePmConfigs(ctx, pmConfigs)
1255 if err != nil {
1256 return err
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001257 }
1258 }
1259 }
Rohan Agrawalbca69122020-06-17 14:59:03 +00001260 } else {
1261 return fmt.Errorf("Device '%s' does not have Group Metrics", options.Args.Id)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001262 }
1263 return nil
1264}
1265
1266func (options *DevicePmConfigGroupDisable) Execute(args []string) error {
1267
1268 conn, err := NewConnection()
1269 if err != nil {
1270 return err
1271 }
1272 defer conn.Close()
1273
1274 client := voltha.NewVolthaServiceClient(conn)
1275
David K. Bainbridge9189c632021-03-26 21:52:21 +00001276 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001277 defer cancel()
1278
1279 id := voltha.ID{Id: string(options.Args.Id)}
1280
1281 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
1282 if err != nil {
1283 return err
1284 }
1285
1286 if pmConfigs.Grouped {
Rohan Agrawalbca69122020-06-17 14:59:03 +00001287 groups := make(map[string]struct{})
1288 for _, group := range pmConfigs.Groups {
1289 groups[group.GroupName] = struct{}{}
1290 }
1291
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001292 for _, group := range pmConfigs.Groups {
Girish Gowdra610acb42021-01-27 13:33:57 -08001293 if _, have := groups[string(options.Args.Group)]; !have {
1294 return fmt.Errorf("Group Name '%s' does not exist", options.Args.Group)
1295 }
Rohan Agrawalbca69122020-06-17 14:59:03 +00001296
Girish Gowdra610acb42021-01-27 13:33:57 -08001297 if string(options.Args.Group) == group.GroupName && group.Enabled {
1298 group.Enabled = false
1299 _, err := client.UpdateDevicePmConfigs(ctx, pmConfigs)
1300 if err != nil {
1301 return err
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001302 }
1303 }
1304 }
Rohan Agrawalbca69122020-06-17 14:59:03 +00001305 } else {
1306 return fmt.Errorf("Device '%s' does not have Group Metrics", options.Args.Id)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001307 }
1308 return nil
1309}
1310
Girish Gowdra610acb42021-01-27 13:33:57 -08001311func (options *DevicePmConfigGroupFrequencySet) 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)
Girish Gowdra610acb42021-01-27 13:33:57 -08001322 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 groups := make(map[string]struct{})
1333 for _, group := range pmConfigs.Groups {
1334 groups[group.GroupName] = struct{}{}
1335 }
1336
1337 for _, group := range pmConfigs.Groups {
1338 if _, have := groups[string(options.Args.Group)]; !have {
1339 return fmt.Errorf("group name '%s' does not exist", options.Args.Group)
1340 }
1341
1342 if string(options.Args.Group) == group.GroupName {
1343 if !group.Enabled {
1344 return fmt.Errorf("group '%s' is not enabled", options.Args.Group)
1345 }
1346 group.GroupFreq = uint32(options.Args.Interval.Seconds())
1347 _, err = client.UpdateDevicePmConfigs(ctx, pmConfigs)
1348 if err != nil {
1349 return err
1350 }
1351 }
1352 }
1353 } else {
1354 return fmt.Errorf("device '%s' does not have group metrics", options.Args.Id)
1355 }
1356 return nil
1357}
1358
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001359func (options *DevicePmConfigGroupList) Execute(args []string) error {
1360
1361 conn, err := NewConnection()
1362 if err != nil {
1363 return err
1364 }
1365 defer conn.Close()
1366
1367 client := voltha.NewVolthaServiceClient(conn)
1368
David K. Bainbridge9189c632021-03-26 21:52:21 +00001369 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001370 defer cancel()
1371
1372 id := voltha.ID{Id: string(options.Args.Id)}
1373
1374 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
1375 if err != nil {
1376 return err
1377 }
1378
1379 if pmConfigs.Grouped {
1380 for _, group := range pmConfigs.Groups {
1381 if group.GroupFreq == 0 {
1382 group.GroupFreq = pmConfigs.DefaultFreq
1383 }
1384 }
Rohan Agrawalbca69122020-06-17 14:59:03 +00001385 outputFormat := CharReplacer.Replace(options.Format)
1386 if outputFormat == "" {
1387 outputFormat = GetCommandOptionWithDefault("device-pm-configs", "format", DEFAULT_DEVICE_PM_CONFIG_GROUP_LIST_FORMAT)
1388 }
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001389
Rohan Agrawalbca69122020-06-17 14:59:03 +00001390 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: pmConfigs.Groups,
1402 }
1403
1404 GenerateOutput(&result)
1405 } else {
1406 return fmt.Errorf("Device '%s' does not have Group Metrics", string(options.Args.Id))
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001407 }
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001408 return nil
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001409}
1410
1411func (options *DevicePmConfigGroupMetricList) Execute(args []string) error {
1412
1413 var metrics []*voltha.PmConfig
1414 conn, err := NewConnection()
1415 if err != nil {
1416 return err
1417 }
1418 defer conn.Close()
1419
1420 client := voltha.NewVolthaServiceClient(conn)
1421
David K. Bainbridge9189c632021-03-26 21:52:21 +00001422 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001423 defer cancel()
1424
1425 id := voltha.ID{Id: string(options.Args.Id)}
1426
1427 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
1428 if err != nil {
1429 return err
1430 }
1431
1432 for _, groups := range pmConfigs.Groups {
1433
1434 if string(options.Args.Group) == groups.GroupName {
1435 for _, metric := range groups.Metrics {
1436 if metric.SampleFreq == 0 && groups.GroupFreq == 0 {
1437 metric.SampleFreq = pmConfigs.DefaultFreq
1438 } else {
1439 metric.SampleFreq = groups.GroupFreq
1440 }
1441 }
1442 metrics = groups.Metrics
1443 }
1444 }
1445
1446 outputFormat := CharReplacer.Replace(options.Format)
1447 if outputFormat == "" {
1448 outputFormat = GetCommandOptionWithDefault("device-pm-configs", "format", DEFAULT_DEVICE_PM_CONFIG_METRIC_LIST_FORMAT)
1449 }
1450
1451 orderBy := options.OrderBy
1452 if orderBy == "" {
1453 orderBy = GetCommandOptionWithDefault("device-pm-configs", "order", "")
1454 }
1455
1456 result := CommandResult{
1457 Format: format.Format(outputFormat),
1458 Filter: options.Filter,
1459 OrderBy: orderBy,
1460 OutputAs: toOutputType(options.OutputAs),
1461 NameLimit: options.NameLimit,
1462 Data: metrics,
1463 }
1464
1465 GenerateOutput(&result)
1466 return nil
1467
1468}
1469
1470func (options *DevicePmConfigFrequencySet) Execute(args []string) error {
1471
1472 conn, err := NewConnection()
1473 if err != nil {
1474 return err
1475 }
1476 defer conn.Close()
1477
1478 client := voltha.NewVolthaServiceClient(conn)
1479
David K. Bainbridge9189c632021-03-26 21:52:21 +00001480 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001481 defer cancel()
1482
1483 id := voltha.ID{Id: string(options.Args.Id)}
1484
1485 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
1486 if err != nil {
1487 return err
1488 }
1489
Girish Gowdra610acb42021-01-27 13:33:57 -08001490 pmConfigs.DefaultFreq = uint32(options.Args.Interval.Seconds())
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001491
1492 _, err = client.UpdateDevicePmConfigs(ctx, pmConfigs)
1493 if err != nil {
1494 return err
1495 }
1496
1497 outputFormat := CharReplacer.Replace(options.Format)
1498 if outputFormat == "" {
1499 outputFormat = GetCommandOptionWithDefault("device-pm-configs", "format", DEFAULT_DEVICE_PM_CONFIG_GET_FORMAT)
1500 }
1501 if options.Quiet {
1502 outputFormat = "{{.Id}}"
1503 }
1504
1505 result := CommandResult{
1506 Format: format.Format(outputFormat),
1507 OutputAs: toOutputType(options.OutputAs),
1508 NameLimit: options.NameLimit,
1509 Data: pmConfigs,
1510 }
1511
1512 GenerateOutput(&result)
1513 return nil
1514
1515}
1516
kesavand3e2f9f62021-04-22 11:06:38 +05301517func (options *OnuDownloadImage) Execute(args []string) error {
1518
1519 conn, err := NewConnection()
1520 if err != nil {
1521 return err
1522 }
1523 defer conn.Close()
1524
1525 client := voltha.NewVolthaServiceClient(conn)
1526
1527 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
1528 defer cancel()
1529
1530 var devIDList []*common.ID
1531 for _, i := range options.Args.IDs {
1532
1533 devIDList = append(devIDList, &common.ID{Id: string(i)})
1534 }
1535
1536 downloadImage := voltha.DeviceImageDownloadRequest{
1537 DeviceId: devIDList,
1538 Image: &voltha.Image{
1539 Url: options.Args.Url,
1540 Crc32: options.Args.Crc,
ssiddiqui7bc89e92021-05-20 20:58:02 +05301541 Vendor: options.Args.Vendor,
kesavand3e2f9f62021-04-22 11:06:38 +05301542 Version: options.Args.ImageVersion,
1543 },
1544 ActivateOnSuccess: options.Args.ActivateOnSuccess,
1545 CommitOnSuccess: options.Args.CommitOnSuccess,
1546 }
1547
Andrea Campanellaeaf1e0c2021-06-07 14:41:34 +02001548 deviceImageResp, err := client.DownloadImageToDevice(ctx, &downloadImage)
kesavand3e2f9f62021-04-22 11:06:38 +05301549 if err != nil {
1550 return err
1551 }
1552
Andrea Campanellaeaf1e0c2021-06-07 14:41:34 +02001553 outputFormat := GetCommandOptionWithDefault("onu-image-download", "format", ONU_IMAGE_STATUS_FORMAT)
1554 // Make sure json output prints an empty list, not "null"
1555 if deviceImageResp.DeviceImageStates == nil {
1556 deviceImageResp.DeviceImageStates = make([]*voltha.DeviceImageState, 0)
1557 }
1558 result := CommandResult{
1559 Format: format.Format(outputFormat),
1560 OutputAs: toOutputType(options.OutputAs),
1561 NameLimit: options.NameLimit,
1562 Data: deviceImageResp.DeviceImageStates,
1563 }
1564 GenerateOutput(&result)
kesavand3e2f9f62021-04-22 11:06:38 +05301565 return nil
1566
1567}
1568
1569func (options *OnuActivateImage) Execute(args []string) error {
1570
1571 conn, err := NewConnection()
1572 if err != nil {
1573 return err
1574 }
1575 defer conn.Close()
1576
1577 client := voltha.NewVolthaServiceClient(conn)
1578
1579 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
1580 defer cancel()
1581
1582 var devIDList []*common.ID
1583 for _, i := range options.Args.IDs {
1584
1585 devIDList = append(devIDList, &common.ID{Id: string(i)})
1586 }
1587
1588 downloadImage := voltha.DeviceImageRequest{
1589 DeviceId: devIDList,
1590 Version: options.Args.ImageVersion,
1591 CommitOnSuccess: options.Args.CommitOnSuccess,
1592 }
1593
Andrea Campanellaeaf1e0c2021-06-07 14:41:34 +02001594 deviceImageResp, err := client.ActivateImage(ctx, &downloadImage)
kesavand3e2f9f62021-04-22 11:06:38 +05301595 if err != nil {
1596 return err
1597 }
1598
Andrea Campanellaeaf1e0c2021-06-07 14:41:34 +02001599 outputFormat := GetCommandOptionWithDefault("onu-image-activate", "format", ONU_IMAGE_STATUS_FORMAT)
1600 // Make sure json output prints an empty list, not "null"
1601 if deviceImageResp.DeviceImageStates == nil {
1602 deviceImageResp.DeviceImageStates = make([]*voltha.DeviceImageState, 0)
1603 }
1604 result := CommandResult{
1605 Format: format.Format(outputFormat),
1606 OutputAs: toOutputType(options.OutputAs),
1607 NameLimit: options.NameLimit,
1608 Data: deviceImageResp.DeviceImageStates,
1609 }
1610 GenerateOutput(&result)
1611
kesavand3e2f9f62021-04-22 11:06:38 +05301612 return nil
1613
1614}
1615
1616func (options *OnuAbortUpgradeImage) Execute(args []string) error {
1617
1618 conn, err := NewConnection()
1619 if err != nil {
1620 return err
1621 }
1622 defer conn.Close()
1623
1624 client := voltha.NewVolthaServiceClient(conn)
1625
1626 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
1627 defer cancel()
1628
1629 var devIDList []*common.ID
1630 for _, i := range options.Args.IDs {
1631
1632 devIDList = append(devIDList, &common.ID{Id: string(i)})
1633 }
1634
1635 downloadImage := voltha.DeviceImageRequest{
1636 DeviceId: devIDList,
1637 Version: options.Args.ImageVersion,
1638 }
1639
Andrea Campanellaeaf1e0c2021-06-07 14:41:34 +02001640 deviceImageResp, err := client.AbortImageUpgradeToDevice(ctx, &downloadImage)
kesavand3e2f9f62021-04-22 11:06:38 +05301641 if err != nil {
1642 return err
1643 }
1644
Andrea Campanellaeaf1e0c2021-06-07 14:41:34 +02001645 outputFormat := GetCommandOptionWithDefault("onu-image-abort", "format", ONU_IMAGE_STATUS_FORMAT)
1646 // Make sure json output prints an empty list, not "null"
1647 if deviceImageResp.DeviceImageStates == nil {
1648 deviceImageResp.DeviceImageStates = make([]*voltha.DeviceImageState, 0)
1649 }
1650 result := CommandResult{
1651 Format: format.Format(outputFormat),
1652 OutputAs: toOutputType(options.OutputAs),
1653 NameLimit: options.NameLimit,
1654 Data: deviceImageResp.DeviceImageStates,
1655 }
1656 GenerateOutput(&result)
1657
kesavand3e2f9f62021-04-22 11:06:38 +05301658 return nil
1659
1660}
1661
1662func (options *OnuCommitImage) Execute(args []string) error {
1663
1664 conn, err := NewConnection()
1665 if err != nil {
1666 return err
1667 }
1668 defer conn.Close()
1669
1670 client := voltha.NewVolthaServiceClient(conn)
1671
1672 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
1673 defer cancel()
1674
1675 var devIDList []*common.ID
1676 for _, i := range options.Args.IDs {
1677
1678 devIDList = append(devIDList, &common.ID{Id: string(i)})
1679 }
1680 downloadImage := voltha.DeviceImageRequest{
1681 DeviceId: devIDList,
1682 Version: options.Args.ImageVersion,
1683 }
1684
Andrea Campanellaeaf1e0c2021-06-07 14:41:34 +02001685 deviceImageResp, err := client.CommitImage(ctx, &downloadImage)
kesavand3e2f9f62021-04-22 11:06:38 +05301686 if err != nil {
1687 return err
1688 }
1689
Andrea Campanellaeaf1e0c2021-06-07 14:41:34 +02001690 outputFormat := GetCommandOptionWithDefault("onu-image-commit", "format", ONU_IMAGE_STATUS_FORMAT)
1691 // Make sure json output prints an empty list, not "null"
1692 if deviceImageResp.DeviceImageStates == nil {
1693 deviceImageResp.DeviceImageStates = make([]*voltha.DeviceImageState, 0)
1694 }
1695 result := CommandResult{
1696 Format: format.Format(outputFormat),
1697 OutputAs: toOutputType(options.OutputAs),
1698 NameLimit: options.NameLimit,
1699 Data: deviceImageResp.DeviceImageStates,
1700 }
1701 GenerateOutput(&result)
1702
kesavand3e2f9f62021-04-22 11:06:38 +05301703 return nil
1704
1705}
1706
1707func (options *OnuListImages) Execute(args []string) error {
1708
1709 conn, err := NewConnection()
1710 if err != nil {
1711 return err
1712 }
1713 defer conn.Close()
1714
1715 client := voltha.NewVolthaServiceClient(conn)
1716
1717 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
1718 defer cancel()
1719
1720 id := common.ID{Id: string(options.Args.Id)}
1721
1722 onuImages, err := client.GetOnuImages(ctx, &id)
1723 if err != nil {
1724 return err
1725 }
1726
1727 outputFormat := CharReplacer.Replace(options.Format)
1728 if outputFormat == "" {
1729 outputFormat = GetCommandOptionWithDefault("onu-image-list", "format", ONU_IMAGE_LIST_FORMAT)
1730 }
1731
1732 if options.Quiet {
1733 outputFormat = "{{.Id}}"
1734 }
1735
1736 //TODO orderby
1737
1738 // Make sure json output prints an empty list, not "null"
1739 if onuImages.Items == nil {
1740 onuImages.Items = make([]*voltha.OnuImage, 0)
1741 }
1742
1743 result := CommandResult{
1744 Format: format.Format(outputFormat),
1745 OutputAs: toOutputType(options.OutputAs),
1746 NameLimit: options.NameLimit,
1747 Data: onuImages.Items,
1748 }
1749
1750 GenerateOutput(&result)
1751 return nil
1752
1753}
1754
1755func (options *OnuImageStatus) Execute(args []string) error {
1756
1757 conn, err := NewConnection()
1758 if err != nil {
1759 return err
1760 }
1761 defer conn.Close()
1762
1763 client := voltha.NewVolthaServiceClient(conn)
1764
1765 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
1766 defer cancel()
1767
1768 var devIDList []*common.ID
1769 for _, i := range options.Args.IDs {
1770
1771 devIDList = append(devIDList, &common.ID{Id: string(i)})
1772 }
1773
1774 imageStatusReq := voltha.DeviceImageRequest{
1775 DeviceId: devIDList,
1776 Version: options.Args.ImageVersion,
1777 }
1778 imageStatus, err := client.GetImageStatus(ctx, &imageStatusReq)
1779 if err != nil {
1780 return err
1781 }
1782
1783 outputFormat := CharReplacer.Replace(options.Format)
1784 if outputFormat == "" {
1785 outputFormat = GetCommandOptionWithDefault("device-image-list", "format", ONU_IMAGE_STATUS_FORMAT)
1786 }
1787
1788 if options.Quiet {
1789 outputFormat = "{{.Id}}"
1790 }
1791
1792 //TODO orderby
1793
1794 // Make sure json output prints an empty list, not "null"
1795 if imageStatus.DeviceImageStates == nil {
1796 imageStatus.DeviceImageStates = make([]*voltha.DeviceImageState, 0)
1797 }
1798
1799 result := CommandResult{
1800 Format: format.Format(outputFormat),
1801 OutputAs: toOutputType(options.OutputAs),
1802 NameLimit: options.NameLimit,
1803 Data: imageStatus.DeviceImageStates,
1804 }
1805
1806 GenerateOutput(&result)
1807 return nil
1808
1809}
1810
Andrea Campanella791d88b2021-01-08 13:29:00 +01001811func (options *DeviceOnuListImages) Execute(args []string) error {
1812
1813 conn, err := NewConnection()
1814 if err != nil {
1815 return err
1816 }
1817 defer conn.Close()
1818
1819 client := voltha.NewVolthaServiceClient(conn)
1820
David K. Bainbridge9189c632021-03-26 21:52:21 +00001821 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Andrea Campanella791d88b2021-01-08 13:29:00 +01001822 defer cancel()
1823
1824 id := common.ID{Id: string(options.Args.Id)}
1825
1826 imageDownloads, err := client.ListImageDownloads(ctx, &id)
1827 if err != nil {
1828 return err
1829 }
1830
1831 outputFormat := CharReplacer.Replace(options.Format)
1832 if outputFormat == "" {
1833 outputFormat = GetCommandOptionWithDefault("device-image-list", "format", DEFAULT_DEVICE_IMAGE_LIST_GET_FORMAT)
1834 }
1835
1836 if options.Quiet {
1837 outputFormat = "{{.Id}}"
1838 }
1839
1840 //TODO orderby
1841
1842 // Make sure json output prints an empty list, not "null"
1843 if imageDownloads.Items == nil {
1844 imageDownloads.Items = make([]*voltha.ImageDownload, 0)
1845 }
1846
1847 result := CommandResult{
1848 Format: format.Format(outputFormat),
1849 OutputAs: toOutputType(options.OutputAs),
1850 NameLimit: options.NameLimit,
1851 Data: imageDownloads.Items,
1852 }
1853
1854 GenerateOutput(&result)
1855 return nil
1856
1857}
1858
1859func (options *DeviceOnuDownloadImage) Execute(args []string) error {
1860
1861 conn, err := NewConnection()
1862 if err != nil {
1863 return err
1864 }
1865 defer conn.Close()
1866
1867 client := voltha.NewVolthaServiceClient(conn)
1868
David K. Bainbridge9189c632021-03-26 21:52:21 +00001869 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Andrea Campanella791d88b2021-01-08 13:29:00 +01001870 defer cancel()
1871
1872 downloadImage := voltha.ImageDownload{
1873 Id: string(options.Args.Id),
1874 Name: options.Args.Name,
1875 Url: options.Args.Url,
1876 Crc: options.Args.Crc,
1877 LocalDir: options.Args.LocalDir,
1878 }
1879
1880 _, err = client.DownloadImage(ctx, &downloadImage)
1881 if err != nil {
1882 return err
1883 }
1884
1885 return nil
1886
1887}
1888
1889func (options *DeviceOnuActivateImageUpdate) Execute(args []string) error {
1890
1891 conn, err := NewConnection()
1892 if err != nil {
1893 return err
1894 }
1895 defer conn.Close()
1896
1897 client := voltha.NewVolthaServiceClient(conn)
1898
David K. Bainbridge9189c632021-03-26 21:52:21 +00001899 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Andrea Campanella791d88b2021-01-08 13:29:00 +01001900 defer cancel()
1901
1902 downloadImage := voltha.ImageDownload{
1903 Id: string(options.Args.Id),
1904 Name: options.Args.Name,
1905 ImageVersion: options.Args.ImageVersion,
1906 SaveConfig: options.Args.SaveConfig,
1907 LocalDir: options.Args.LocalDir,
1908 }
1909
1910 _, err = client.ActivateImageUpdate(ctx, &downloadImage)
1911 if err != nil {
1912 return err
1913 }
1914
1915 return nil
1916
1917}
1918
Scott Baker9173ed82020-05-19 08:30:12 -07001919type ReturnValueRow struct {
1920 Name string `json:"name"`
1921 Result interface{} `json:"result"`
1922}
1923
kesavand8ec4fc02021-01-27 09:10:22 -05001924func (options *DeviceGetPortStats) 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 var portType extension.GetOltPortCounters_PortType
1932
1933 if options.Args.PortType == "pon" {
1934 portType = extension.GetOltPortCounters_Port_PON_OLT
1935 } else if options.Args.PortType == "nni" {
1936
1937 portType = extension.GetOltPortCounters_Port_ETHERNET_NNI
1938 } else {
1939 return fmt.Errorf("expected interface type pon/nni, provided %s", options.Args.PortType)
1940 }
1941
1942 singleGetValReq := extension.SingleGetValueRequest{
1943 TargetId: string(options.Args.Id),
1944 Request: &extension.GetValueRequest{
1945 Request: &extension.GetValueRequest_OltPortInfo{
1946 OltPortInfo: &extension.GetOltPortCounters{
1947 PortNo: options.Args.PortNo,
1948 PortType: portType,
1949 },
1950 },
1951 },
1952 }
1953
David K. Bainbridge9189c632021-03-26 21:52:21 +00001954 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
kesavand8ec4fc02021-01-27 09:10:22 -05001955 defer cancel()
1956 rv, err := client.GetExtValue(ctx, &singleGetValReq)
1957 if err != nil {
1958 Error.Printf("Error getting value on device Id %s,err=%s\n", options.Args.Id, ErrorToString(err))
1959 return err
1960 }
1961
1962 if rv.Response.Status != extension.GetValueResponse_OK {
1963 return fmt.Errorf("failed to get port stats %v", rv.Response.ErrReason.String())
1964 }
1965
1966 outputFormat := CharReplacer.Replace(options.Format)
1967 if outputFormat == "" {
1968 outputFormat = GetCommandOptionWithDefault("device-get-port-status", "format", DEFAULT_DEVICE_GET_PORT_STATUS_FORMAT)
1969 }
1970
1971 result := CommandResult{
1972 Format: format.Format(outputFormat),
1973 OutputAs: toOutputType(options.OutputAs),
1974 NameLimit: options.NameLimit,
1975 Data: rv.GetResponse().GetPortCoutners(),
1976 }
1977 GenerateOutput(&result)
1978 return nil
1979}
1980
Himani Chawla40acc122021-05-26 18:52:29 +05301981func (options *GetOnuStats) Execute(args []string) error {
1982 conn, err := NewConnection()
1983 if err != nil {
1984 return err
1985 }
1986 defer conn.Close()
1987 client := extension.NewExtensionClient(conn)
1988
1989 singleGetValReq := extension.SingleGetValueRequest{
1990 TargetId: string(options.Args.OltId),
1991 Request: &extension.GetValueRequest{
1992 Request: &extension.GetValueRequest_OnuPonInfo{
1993 OnuPonInfo: &extension.GetOnuCountersRequest{
1994 IntfId: options.Args.IntfId,
1995 OnuId: options.Args.OnuId,
1996 },
1997 },
1998 },
1999 }
2000 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
2001 defer cancel()
2002 rv, err := client.GetExtValue(ctx, &singleGetValReq)
2003 if err != nil {
2004 Error.Printf("Error getting value on device Id %s,err=%s\n", options.Args.OltId, ErrorToString(err))
2005 return err
2006 }
2007
2008 if rv.Response.Status != extension.GetValueResponse_OK {
2009 return fmt.Errorf("failed to get onu stats %v", rv.Response.ErrReason.String())
2010 }
2011 outputFormat := CharReplacer.Replace(options.Format)
2012 data, formatStr := buildOnuStatsOutputFormat(rv.GetResponse().GetOnuPonCounters())
2013 if outputFormat == "" {
2014 outputFormat = GetCommandOptionWithDefault("device-get-onu-status", "format", formatStr)
2015 }
2016
2017 result := CommandResult{
2018 Format: format.Format(outputFormat),
2019 OutputAs: toOutputType(options.OutputAs),
2020 NameLimit: options.NameLimit,
2021 Data: data,
2022 }
2023 GenerateOutput(&result)
2024 return nil
2025}
2026
kesavand6d1131f2021-02-05 22:38:15 +05302027func (options *UniStatus) Execute(args []string) error {
2028 conn, err := NewConnection()
2029 if err != nil {
2030 return err
2031 }
2032 defer conn.Close()
2033 client := extension.NewExtensionClient(conn)
2034
2035 singleGetValReq := extension.SingleGetValueRequest{
2036 TargetId: string(options.Args.Id),
2037 Request: &extension.GetValueRequest{
2038 Request: &extension.GetValueRequest_UniInfo{
2039 UniInfo: &extension.GetOnuUniInfoRequest{
2040 UniIndex: options.Args.UniIndex,
2041 },
2042 },
2043 },
2044 }
David K. Bainbridge9189c632021-03-26 21:52:21 +00002045 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
kesavand6d1131f2021-02-05 22:38:15 +05302046 defer cancel()
2047 rv, err := client.GetExtValue(ctx, &singleGetValReq)
2048 if err != nil {
2049 Error.Printf("Error getting value on device Id %s,err=%s\n", options.Args.Id, ErrorToString(err))
2050 return err
2051 }
2052 if rv.Response.Status != extension.GetValueResponse_OK {
2053 return fmt.Errorf("failed to get uni status %v", rv.Response.ErrReason.String())
2054 }
2055 outputFormat := CharReplacer.Replace(options.Format)
2056 if outputFormat == "" {
2057 outputFormat = GetCommandOptionWithDefault("device-get-uni-status", "format", DEFAULT_DEVICE_GET_UNI_STATUS_FORMAT)
2058 }
2059 result := CommandResult{
2060 Format: format.Format(outputFormat),
2061 OutputAs: toOutputType(options.OutputAs),
2062 NameLimit: options.NameLimit,
2063 Data: rv.GetResponse().GetUniInfo(),
2064 }
2065 GenerateOutput(&result)
2066 return nil
2067}
2068
Girish Gowdra4f5ce7c2021-04-29 18:53:21 -07002069func (options *OnuPonOpticalInfo) Execute(args []string) error {
2070 conn, err := NewConnection()
2071 if err != nil {
2072 return err
2073 }
2074 defer conn.Close()
2075 client := extension.NewExtensionClient(conn)
2076
2077 singleGetValReq := extension.SingleGetValueRequest{
2078 TargetId: string(options.Args.Id),
2079 Request: &extension.GetValueRequest{
2080 Request: &extension.GetValueRequest_OnuOpticalInfo{
2081 OnuOpticalInfo: &extension.GetOnuPonOpticalInfo{},
2082 },
2083 },
2084 }
2085 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
2086 defer cancel()
2087 rv, err := client.GetExtValue(ctx, &singleGetValReq)
2088 if err != nil {
2089 Error.Printf("Error getting value on device Id %s,err=%s\n", options.Args.Id, ErrorToString(err))
2090 return err
2091 }
2092 if rv.Response.Status != extension.GetValueResponse_OK {
2093 return fmt.Errorf("failed to get onu pon optical info %v", rv.Response.ErrReason.String())
2094 }
2095 outputFormat := CharReplacer.Replace(options.Format)
2096 if outputFormat == "" {
2097 outputFormat = GetCommandOptionWithDefault("device-get-onu-pon-optical-info", "format", DEFAULT_ONU_PON_OPTICAL_INFO_STATUS_FORMAT)
2098 }
2099 result := CommandResult{
2100 Format: format.Format(outputFormat),
2101 OutputAs: toOutputType(options.OutputAs),
2102 NameLimit: options.NameLimit,
2103 Data: rv.GetResponse().GetOnuOpticalInfo(),
2104 }
2105 GenerateOutput(&result)
2106 return nil
2107}
2108
Gamze Abakac857a462021-05-26 13:45:54 +00002109func (options *RxPower) Execute(args []string) error {
2110 conn, err := NewConnection()
2111 if err != nil {
2112 return err
2113 }
2114 defer conn.Close()
2115 client := extension.NewExtensionClient(conn)
2116
2117 singleGetValReq := extension.SingleGetValueRequest{
2118 TargetId: string(options.Args.Id),
2119 Request: &extension.GetValueRequest{
2120 Request: &extension.GetValueRequest_RxPower{
2121 RxPower: &extension.GetRxPowerRequest{
2122 IntfId: options.Args.PortNo,
2123 OnuId: options.Args.OnuNo,
2124 },
2125 },
2126 },
2127 }
2128
2129 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
2130 defer cancel()
2131 rv, err := client.GetExtValue(ctx, &singleGetValReq)
2132 if err != nil {
2133 Error.Printf("Error getting value on device Id %s,err=%s\n", options.Args.Id, ErrorToString(err))
2134 return err
2135 }
2136 if rv.Response.Status != extension.GetValueResponse_OK {
2137 return fmt.Errorf("failed to get rx power %v", rv.Response.ErrReason.String())
2138 }
2139 outputFormat := CharReplacer.Replace(options.Format)
2140 if outputFormat == "" {
2141 outputFormat = GetCommandOptionWithDefault("device-get-rx-power", "format", DEFAULT_RX_POWER_STATUS_FORMAT)
2142 }
2143 result := CommandResult{
2144 Format: format.Format(outputFormat),
2145 OutputAs: toOutputType(options.OutputAs),
2146 NameLimit: options.NameLimit,
2147 Data: rv.GetResponse().GetRxPower(),
2148 }
2149 GenerateOutput(&result)
2150 return nil
2151}
2152
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -08002153/*Device get Onu Distance */
2154func (options *DeviceGetExtValue) Execute(args []string) error {
2155 conn, err := NewConnection()
2156 if err != nil {
2157 return err
2158 }
2159 defer conn.Close()
2160
Scott Baker9173ed82020-05-19 08:30:12 -07002161 client := voltha.NewVolthaServiceClient(conn)
2162
2163 valueflag, okay := common.ValueType_Type_value[string(options.Args.Valueflag)]
2164 if !okay {
2165 Error.Printf("Unknown valueflag %s\n", options.Args.Valueflag)
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -08002166 }
2167
Scott Baker9173ed82020-05-19 08:30:12 -07002168 val := voltha.ValueSpecifier{Id: string(options.Args.Id), Value: common.ValueType_Type(valueflag)}
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -08002169
David K. Bainbridge9189c632021-03-26 21:52:21 +00002170 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -08002171 defer cancel()
2172
Scott Baker9173ed82020-05-19 08:30:12 -07002173 rv, err := client.GetExtValue(ctx, &val)
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -08002174 if err != nil {
2175 Error.Printf("Error getting value on device Id %s,err=%s\n", options.Args.Id, ErrorToString(err))
2176 return err
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -08002177 }
2178
Scott Baker9173ed82020-05-19 08:30:12 -07002179 var rows []ReturnValueRow
2180 for name, num := range common.ValueType_Type_value {
2181 if num == 0 {
2182 // EMPTY is not a real value
2183 continue
2184 }
2185 if (rv.Error & uint32(num)) != 0 {
2186 row := ReturnValueRow{Name: name, Result: "Error"}
2187 rows = append(rows, row)
2188 }
2189 if (rv.Unsupported & uint32(num)) != 0 {
2190 row := ReturnValueRow{Name: name, Result: "Unsupported"}
2191 rows = append(rows, row)
2192 }
2193 if (rv.Set & uint32(num)) != 0 {
2194 switch name {
2195 case "DISTANCE":
2196 row := ReturnValueRow{Name: name, Result: rv.Distance}
2197 rows = append(rows, row)
2198 default:
2199 row := ReturnValueRow{Name: name, Result: "Unimplemented-in-voltctl"}
2200 rows = append(rows, row)
2201 }
2202 }
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -08002203 }
2204
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -08002205 outputFormat := CharReplacer.Replace(options.Format)
2206 if outputFormat == "" {
2207 outputFormat = GetCommandOptionWithDefault("device-value-get", "format", DEFAULT_DEVICE_VALUE_GET_FORMAT)
2208 }
2209
2210 result := CommandResult{
2211 Format: format.Format(outputFormat),
2212 OutputAs: toOutputType(options.OutputAs),
2213 NameLimit: options.NameLimit,
Scott Baker9173ed82020-05-19 08:30:12 -07002214 Data: rows,
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -08002215 }
2216 GenerateOutput(&result)
2217 return nil
2218}