blob: ba2b8c2ce4bc9e0885571307189a2c61e4211cc8 [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"
Akash Soni6e879c22024-12-20 17:01:34 +053020 "encoding/json"
Zack Williamse940c7a2019-08-21 14:25:39 -070021 "fmt"
David Bainbridge7052fe82020-03-25 10:37:00 -070022 "os"
23 "strconv"
24 "strings"
Girish Gowdra610acb42021-01-27 13:33:57 -080025 "time"
David Bainbridge7052fe82020-03-25 10:37:00 -070026
Scott Baker9173ed82020-05-19 08:30:12 -070027 "github.com/golang/protobuf/ptypes/empty"
Zack Williamse940c7a2019-08-21 14:25:39 -070028 flags "github.com/jessevdk/go-flags"
Scott Baker2b0ad652019-08-21 14:57:07 -070029 "github.com/opencord/voltctl/pkg/format"
David K. Bainbridgebd6b2882021-08-26 13:31:02 +000030 "github.com/opencord/voltha-protos/v5/go/common"
31 "github.com/opencord/voltha-protos/v5/go/extension"
32 "github.com/opencord/voltha-protos/v5/go/voltha"
Zack Williamse940c7a2019-08-21 14:25:39 -070033)
34
35const (
David K. Bainbridge89003c42020-02-27 17:22:49 -080036 DEFAULT_DEVICE_FORMAT = "table{{ .Id }}\t{{.Type}}\t{{.Root}}\t{{.ParentId}}\t{{.SerialNumber}}\t{{.AdminState}}\t{{.OperStatus}}\t{{.ConnectStatus}}\t{{.Reason}}"
Hardik Windlass9361bb82022-03-23 05:58:48 +000037 DEFAULT_DEVICE_ORDER = "Type,Id"
Zack Williamse940c7a2019-08-21 14:25:39 -070038 DEFAULT_DEVICE_PORTS_FORMAT = "table{{.PortNo}}\t{{.Label}}\t{{.Type}}\t{{.AdminState}}\t{{.OperStatus}}\t{{.DeviceId}}\t{{.Peers}}"
39 DEFAULT_DEVICE_INSPECT_FORMAT = `ID: {{.Id}}
40 TYPE: {{.Type}}
41 ROOT: {{.Root}}
42 PARENTID: {{.ParentId}}
43 SERIALNUMBER: {{.SerialNumber}}
44 VLAN: {{.Vlan}}
45 ADMINSTATE: {{.AdminState}}
46 OPERSTATUS: {{.OperStatus}}
47 CONNECTSTATUS: {{.ConnectStatus}}`
Rohan Agrawal9228d2f2020-06-03 07:48:50 +000048 DEFAULT_DEVICE_PM_CONFIG_GET_FORMAT = "table{{.DefaultFreq}}\t{{.Grouped}}\t{{.FreqOverride}}"
49 DEFAULT_DEVICE_PM_CONFIG_METRIC_LIST_FORMAT = "table{{.Name}}\t{{.Type}}\t{{.Enabled}}\t{{.SampleFreq}}"
50 DEFAULT_DEVICE_PM_CONFIG_GROUP_LIST_FORMAT = "table{{.GroupName}}\t{{.Enabled}}\t{{.GroupFreq}}"
51 DEFAULT_DEVICE_VALUE_GET_FORMAT = "table{{.Name}}\t{{.Result}}"
Andrea Campanella791d88b2021-01-08 13:29:00 +010052 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 +053053 ONU_IMAGE_LIST_FORMAT = "table{{.Version}}\t{{.IsCommited}}\t{{.IsActive}}\t{{.IsValid}}\t{{.ProductCode}}\t{{.Hash}}"
54 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 -050055 DEFAULT_DEVICE_GET_PORT_STATUS_FORMAT = `
56 TXBYTES: {{.TxBytes}}
57 TXPACKETS: {{.TxPackets}}
58 TXERRPACKETS: {{.TxErrorPackets}}
59 TXBCASTPACKETS: {{.TxBcastPackets}}
60 TXUCASTPACKETS: {{.TxUcastPackets}}
61 TXMCASTPACKETS: {{.TxMcastPackets}}
62 RXBYTES: {{.RxBytes}}
63 RXPACKETS: {{.RxPackets}}
64 RXERRPACKETS: {{.RxErrorPackets}}
65 RXBCASTPACKETS: {{.RxBcastPackets}}
66 RXUCASTPACKETS: {{.RxUcastPackets}}
67 RXMCASTPACKETS: {{.RxMcastPackets}}`
kesavand6d1131f2021-02-05 22:38:15 +053068 DEFAULT_DEVICE_GET_UNI_STATUS_FORMAT = `
69 ADMIN_STATE: {{.AdmState}}
70 OPERATIONAL_STATE: {{.OperState}}
71 CONFIG_IND: {{.ConfigInd}}`
Girish Gowdra4f5ce7c2021-04-29 18:53:21 -070072 DEFAULT_ONU_PON_OPTICAL_INFO_STATUS_FORMAT = `
73 POWER_FEED_VOLTAGE__VOLTS: {{.PowerFeedVoltage}}
74 RECEIVED_OPTICAL_POWER__dBm: {{.ReceivedOpticalPower}}
75 MEAN_OPTICAL_LAUNCH_POWER__dBm: {{.MeanOpticalLaunchPower}}
76 LASER_BIAS_CURRENT__mA: {{.LaserBiasCurrent}}
77 TEMPERATURE__Celsius: {{.Temperature}}`
Gamze Abakac857a462021-05-26 13:45:54 +000078 DEFAULT_RX_POWER_STATUS_FORMAT = `
79 INTF_ID: {{.IntfId}}
80 ONU_ID: {{.OnuId}}
81 STATUS: {{.Status}}
82 FAIL_REASON: {{.FailReason}}
83 RX_POWER : {{.RxPower}}`
Himani Chawla553a1392021-06-10 23:39:17 +053084 DEFAULT_ETHERNET_FRAME_EXTENDED_PM_COUNTERS_FORMAT = `Upstream_Drop_Events: {{.UDropEvents}}
85Upstream_Octets: {{.UOctets}}
86UFrames: {{.UFrames}}
87UBroadcastFrames: {{.UBroadcastFrames}}
88UMulticastFrames: {{.UMulticastFrames}}
89UCrcErroredFrames: {{.UCrcErroredFrames}}
90UUndersizeFrames: {{.UUndersizeFrames}}
91UOversizeFrames: {{.UOversizeFrames}}
92UFrames_64Octets: {{.UFrames_64Octets}}
93UFrames_65To_127Octets: {{.UFrames_65To_127Octets}}
94UFrames_128To_255Octets: {{.UFrames_128To_255Octets}}
95UFrames_256To_511Octets: {{.UFrames_256To_511Octets}}
96UFrames_512To_1023Octets: {{.UFrames_512To_1023Octets}}
97UFrames_1024To_1518Octets: {{.UFrames_1024To_1518Octets}}
98DDropEvents: {{.DDropEvents}}
99DOctets: {{.DOctets}}
100DFrames: {{.DFrames}}
101DBroadcastFrames: {{.DBroadcastFrames}}
102DMulticastFrames: {{.DMulticastFrames}}
103DCrcErroredFrames: {{.DCrcErroredFrames}}
104DUndersizeFrames: {{.DUndersizeFrames}}
105DOversizeFrames: {{.DOversizeFrames}}
106DFrames_64Octets: {{.DFrames_64Octets}}
107DFrames_65To_127Octets: {{.DFrames_65To_127Octets}}
108DFrames_128To_255Octets: {{.DFrames_128To_255Octets}}
109DFrames_256To_511Octets: {{.DFrames_256To_511Octets}}
110DFrames_512To_1023Octets: {{.DFrames_512To_1023Octets}}
Himani Chawlabac0f892021-08-25 17:14:06 +0530111DFrames_1024To_1518Octets: {{.DFrames_1024To_1518Octets}}
112PmFormat: {{.PmFormat}}`
serkantul3d22fc72022-09-14 12:22:56 +0300113 DEFAULT_ONU_OMCI_TX_RX_STATS_FORMAT = `BaseTxArFrames: {{.BaseTxArFrames}}
114BaseRxAkFrames: {{.BaseRxAkFrames}}
115BaseTxNoArFrames: {{.BaseTxNoArFrames}}
116BaseRxNoAkFrames: {{.BaseRxNoAkFrames}}
117ExtTxArFrames: {{.ExtTxArFrames}}
118ExtRxAkFrames: {{.ExtRxAkFrames}}
119ExtTxNoArFrames: {{.ExtTxNoArFrames}}
120ExtRxNoAkFrames: {{.ExtRxNoAkFrames}}
121TxOmciCounterRetries: {{.TxOmciCounterRetries}}
122TxOmciCounterTimeouts: {{.TxOmciCounterTimeouts}}`
praneeth nalmas39c71ad2023-09-27 18:29:04 +0530123 DEFAULT_DEVICE_ALARMS_FORMAT = "table{{ .ClassId }}\t{{.InstanceId}}\t{{.Name}}\t{{.Description}}"
124 DEFAULT_DEVICE_ALARMS_ORDER = "ClassId,InstanceId"
125 DEFAULT_PON_RX_POWER_STATUS_FORMAT = "table{{.OnuSn}}\t{{.Status}}\t{{.FailReason}}\t{{.RxPower}}\t"
Akash Soni66db9632024-04-15 09:05:15 +0530126 DEFAULT_ONU_DISTANCE_FORMAT = `Distance`
Zack Williamse940c7a2019-08-21 14:25:39 -0700127)
128
129type DeviceList struct {
130 ListOutputOptions
131}
132
133type DeviceCreate struct {
David Bainbridge1a514392020-06-23 11:12:51 -0700134 DeviceType string `short:"t" required:"true" long:"devicetype" description:"Device type"`
David Bainbridge835dd0e2020-04-01 10:30:09 -0700135 MACAddress string `short:"m" long:"macaddress" default:"" description:"MAC Address"`
Zack Williamse940c7a2019-08-21 14:25:39 -0700136 IPAddress string `short:"i" long:"ipaddress" default:"" description:"IP Address"`
137 HostAndPort string `short:"H" long:"hostandport" default:"" description:"Host and port"`
138}
139
140type DeviceId string
Akash Soni66db9632024-04-15 09:05:15 +0530141type OnuId string
Zack Williamse940c7a2019-08-21 14:25:39 -0700142
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000143type MetricName string
144type GroupName string
kesavand12cd8eb2020-01-20 22:25:22 -0500145type PortNum uint32
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -0800146type ValueFlag string
kesavand12cd8eb2020-01-20 22:25:22 -0500147
Zack Williamse940c7a2019-08-21 14:25:39 -0700148type DeviceDelete struct {
Himani Chawla9933ddc2020-10-12 23:53:27 +0530149 Force bool `long:"force" description:"Delete device forcefully"`
150 Args struct {
Zack Williamse940c7a2019-08-21 14:25:39 -0700151 Ids []DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
152 } `positional-args:"yes"`
153}
154
155type DeviceEnable struct {
156 Args struct {
157 Ids []DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
158 } `positional-args:"yes"`
159}
160
161type DeviceDisable struct {
162 Args struct {
163 Ids []DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
164 } `positional-args:"yes"`
165}
166
167type DeviceReboot struct {
168 Args struct {
169 Ids []DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
170 } `positional-args:"yes"`
171}
172
173type DeviceFlowList struct {
174 ListOutputOptions
Maninder045921e2020-09-29 16:46:02 +0530175 FlowIdOptions
Zack Williamse940c7a2019-08-21 14:25:39 -0700176 Args struct {
177 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
178 } `positional-args:"yes"`
179}
180
Himani Chawla3c161c62021-05-13 16:36:51 +0530181type DeviceFlowGroupList struct {
182 ListOutputOptions
183 GroupListOptions
184 Args struct {
185 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
186 } `positional-args:"yes"`
187}
Zack Williamse940c7a2019-08-21 14:25:39 -0700188type DevicePortList struct {
189 ListOutputOptions
190 Args struct {
191 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
192 } `positional-args:"yes"`
193}
194
195type DeviceInspect struct {
196 OutputOptionsJson
197 Args struct {
198 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
199 } `positional-args:"yes"`
200}
201
kesavand12cd8eb2020-01-20 22:25:22 -0500202type DevicePortEnable struct {
203 Args struct {
204 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
205 PortId PortNum `positional-arg-name:"PORT_NUMBER" required:"yes"`
206 } `positional-args:"yes"`
207}
208
209type DevicePortDisable struct {
210 Args struct {
211 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
212 PortId PortNum `positional-arg-name:"PORT_NUMBER" required:"yes"`
213 } `positional-args:"yes"`
214}
215
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000216type DevicePmConfigsGet struct {
217 ListOutputOptions
218 Args struct {
219 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
220 } `positional-args:"yes"`
221}
222
223type DevicePmConfigMetricList struct {
224 ListOutputOptions
225 Args struct {
226 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
227 } `positional-args:"yes"`
228}
229
230type DevicePmConfigGroupList struct {
231 ListOutputOptions
232 Args struct {
233 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
234 } `positional-args:"yes"`
235}
236
237type DevicePmConfigGroupMetricList struct {
238 ListOutputOptions
239 Args struct {
240 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
241 Group GroupName `positional-arg-name:"GROUP_NAME" required:"yes"`
242 } `positional-args:"yes"`
243}
244
245type DevicePmConfigFrequencySet struct {
246 OutputOptions
247 Args struct {
Girish Gowdra610acb42021-01-27 13:33:57 -0800248 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
249 Interval time.Duration `positional-arg-name:"INTERVAL" required:"yes"`
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000250 } `positional-args:"yes"`
251}
252
253type DevicePmConfigMetricEnable struct {
254 Args struct {
255 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
256 Metrics []MetricName `positional-arg-name:"METRIC_NAME" required:"yes"`
257 } `positional-args:"yes"`
258}
259
260type DevicePmConfigMetricDisable struct {
261 Args struct {
262 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
263 Metrics []MetricName `positional-arg-name:"METRIC_NAME" required:"yes"`
264 } `positional-args:"yes"`
265}
266
267type DevicePmConfigGroupEnable struct {
268 Args struct {
Girish Gowdra610acb42021-01-27 13:33:57 -0800269 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
270 Group GroupName `positional-arg-name:"GROUP_NAME" required:"yes"`
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000271 } `positional-args:"yes"`
272}
273
274type DevicePmConfigGroupDisable struct {
275 Args struct {
Girish Gowdra610acb42021-01-27 13:33:57 -0800276 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
277 Group GroupName `positional-arg-name:"GROUP_NAME" required:"yes"`
278 } `positional-args:"yes"`
279}
280
281type DevicePmConfigGroupFrequencySet struct {
282 OutputOptions
283 Args struct {
284 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
285 Group GroupName `positional-arg-name:"GROUP_NAME" required:"yes"`
286 Interval time.Duration `positional-arg-name:"INTERVAL" required:"yes"`
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000287 } `positional-args:"yes"`
288}
289
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -0800290type DeviceGetExtValue struct {
291 ListOutputOptions
292 Args struct {
293 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
294 Valueflag ValueFlag `positional-arg-name:"VALUE_FLAG" required:"yes"`
295 } `positional-args:"yes"`
296}
Rohan Agrawald7df3772020-06-29 11:23:36 +0000297
298type DevicePmConfigSetMaxSkew struct {
299 Args struct {
300 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
301 MaxSkew uint32 `positional-arg-name:"MAX_SKEW" required:"yes"`
302 } `positional-args:"yes"`
303}
304
Andrea Campanella791d88b2021-01-08 13:29:00 +0100305type DeviceOnuListImages struct {
306 ListOutputOptions
307 Args struct {
308 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
309 } `positional-args:"yes"`
310}
311
312type DeviceOnuDownloadImage struct {
313 Args struct {
314 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
315 Name string `positional-arg-name:"IMAGE_NAME" required:"yes"`
316 Url string `positional-arg-name:"IMAGE_URL" required:"yes"`
317 ImageVersion string `positional-arg-name:"IMAGE_VERSION" required:"yes"`
318 Crc uint32 `positional-arg-name:"IMAGE_CRC" required:"yes"`
319 LocalDir string `positional-arg-name:"IMAGE_LOCAL_DIRECTORY"`
320 } `positional-args:"yes"`
321}
322
323type DeviceOnuActivateImageUpdate struct {
324 Args struct {
325 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
326 Name string `positional-arg-name:"IMAGE_NAME" required:"yes"`
327 ImageVersion string `positional-arg-name:"IMAGE_VERSION" required:"yes"`
328 SaveConfig bool `positional-arg-name:"SAVE_EXISTING_CONFIG"`
329 LocalDir string `positional-arg-name:"IMAGE_LOCAL_DIRECTORY"`
Andrea Campanella7b2ecf42021-02-25 12:27:15 +0100330 } `positional-args:"yes"`
kesavand8ec4fc02021-01-27 09:10:22 -0500331}
kesavand3e2f9f62021-04-22 11:06:38 +0530332
333type OnuDownloadImage struct {
Andrea Campanellaeaf1e0c2021-06-07 14:41:34 +0200334 ListOutputOptions
kesavand3e2f9f62021-04-22 11:06:38 +0530335 Args struct {
336 ImageVersion string `positional-arg-name:"IMAGE_VERSION" required:"yes"`
337 Url string `positional-arg-name:"IMAGE_URL" required:"yes"`
ssiddiqui7bc89e92021-05-20 20:58:02 +0530338 Vendor string `positional-arg-name:"IMAGE_VENDOR"`
kesavand3e2f9f62021-04-22 11:06:38 +0530339 ActivateOnSuccess bool `positional-arg-name:"IMAGE_ACTIVATE_ON_SUCCESS"`
340 CommitOnSuccess bool `positional-arg-name:"IMAGE_COMMIT_ON_SUCCESS"`
341 Crc uint32 `positional-arg-name:"IMAGE_CRC"`
342 IDs []DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
343 } `positional-args:"yes"`
344}
345
346type OnuActivateImage struct {
Andrea Campanellaeaf1e0c2021-06-07 14:41:34 +0200347 ListOutputOptions
kesavand3e2f9f62021-04-22 11:06:38 +0530348 Args struct {
349 ImageVersion string `positional-arg-name:"IMAGE_VERSION" required:"yes"`
350 CommitOnSuccess bool `positional-arg-name:"IMAGE_COMMIT_ON_SUCCESS"`
351 IDs []DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
352 } `positional-args:"yes"`
353}
354
355type OnuAbortUpgradeImage struct {
Andrea Campanellaeaf1e0c2021-06-07 14:41:34 +0200356 ListOutputOptions
kesavand3e2f9f62021-04-22 11:06:38 +0530357 Args struct {
358 ImageVersion string `positional-arg-name:"IMAGE_VERSION" required:"yes"`
359 IDs []DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
360 } `positional-args:"yes"`
361}
362
363type OnuCommitImage struct {
Andrea Campanellaeaf1e0c2021-06-07 14:41:34 +0200364 ListOutputOptions
kesavand3e2f9f62021-04-22 11:06:38 +0530365 Args struct {
366 ImageVersion string `positional-arg-name:"IMAGE_VERSION" required:"yes"`
367 IDs []DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
368 } `positional-args:"yes"`
369}
370
371type OnuImageStatus struct {
372 ListOutputOptions
373 Args struct {
374 ImageVersion string `positional-arg-name:"IMAGE_VERSION" required:"yes"`
Elia Battiston859f3e62022-02-08 15:57:52 +0100375 IDs []DeviceId `positional-arg-name:"DEVICE_ID"`
kesavand3e2f9f62021-04-22 11:06:38 +0530376 } `positional-args:"yes"`
377}
378
379type OnuListImages struct {
380 ListOutputOptions
381 Args struct {
382 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
383 } `positional-args:"yes"`
384}
385
kesavand8ec4fc02021-01-27 09:10:22 -0500386type DeviceGetPortStats struct {
387 ListOutputOptions
388 Args struct {
389 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
390 PortNo uint32 `positional-arg-name:"PORT_NO" required:"yes"`
391 PortType string `positional-arg-name:"PORT_TYPE" required:"yes"`
Andrea Campanella791d88b2021-01-08 13:29:00 +0100392 } `positional-args:"yes"`
393}
kesavand6d1131f2021-02-05 22:38:15 +0530394type UniStatus struct {
395 ListOutputOptions
396 Args struct {
397 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
398 UniIndex uint32 `positional-arg-name:"UNI_INDEX" required:"yes"`
399 } `positional-args:"yes"`
400}
Girish Gowdra4f5ce7c2021-04-29 18:53:21 -0700401type OnuPonOpticalInfo struct {
402 ListOutputOptions
403 Args struct {
404 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
405 } `positional-args:"yes"`
406}
Himani Chawla40acc122021-05-26 18:52:29 +0530407
408type GetOnuStats struct {
409 ListOutputOptions
410 Args struct {
411 OltId DeviceId `positional-arg-name:"OLT_DEVICE_ID" required:"yes"`
412 IntfId uint32 `positional-arg-name:"PON_INTF_ID" required:"yes"`
413 OnuId uint32 `positional-arg-name:"ONU_ID" required:"yes"`
414 } `positional-args:"yes"`
415}
416
Akash Soni6e879c22024-12-20 17:01:34 +0530417type GetOffloadApp struct {
Akash Soni51b6b7a2024-11-20 11:39:38 +0530418 ListOutputOptions
419 Args struct {
420 OltId DeviceId `positional-arg-name:"OLT_DEVICE_ID" required:"yes"`
421 StatsFor extension.GetOffloadedAppsStatisticsRequest_OffloadedApp `positional-arg-name:"OFFLOADED_APP" required:"yes"`
422 } `positional-args:"yes"`
423}
424
Akash Soni6e879c22024-12-20 17:01:34 +0530425type SetOffloadApp struct {
426 ListOutputOptions
Akash Soni51b6b7a2024-11-20 11:39:38 +0530427 Args struct {
Akash Soni6e879c22024-12-20 17:01:34 +0530428 OltId DeviceId `positional-arg-name:"OLT_DEVICE_ID" required:"yes"`
429 Config string `positional-arg-name:"CONFIG" required:"yes"` // Accept JSON or CSV input
Akash Soni51b6b7a2024-11-20 11:39:38 +0530430 } `positional-args:"yes"`
431}
432
Akash Soni6e879c22024-12-20 17:01:34 +0530433type AppOffloadOnuConfig struct {
434 AgentRemoteID string
435 AgentCircuitID string
436 OnuUniId uint32
437}
438
439type SetOnuOffload struct {
440 ListOutputOptions
Akash Soni51b6b7a2024-11-20 11:39:38 +0530441 Args struct {
Akash Soni6e879c22024-12-20 17:01:34 +0530442 OltId DeviceId `positional-arg-name:"OLT_DEVICE_ID" required:"yes"`
443 OnuDeviceId string `positional-arg-name:"ONU_DEVICE_ID" required:"yes"`
444 PerUniInfo string `positional-arg-name:"PER_UNI_INFO" json:"per_uni_info" required:"yes"` // Accept list as JSON or CSV
Akash Soni51b6b7a2024-11-20 11:39:38 +0530445 } `positional-args:"yes"`
446}
447
Himani Chawla553a1392021-06-10 23:39:17 +0530448type GetOnuEthernetFrameExtendedPmCounters struct {
449 ListOutputOptions
Himani Chawla806aa892021-08-30 15:51:46 +0530450 Reset bool `long:"reset" description:"Reset the counters"`
451 Args struct {
452 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
453 UniIndex *uint32 `positional-arg-name:"UNI_INDEX"`
Himani Chawla553a1392021-06-10 23:39:17 +0530454 } `positional-args:"yes"`
455}
456
Gamze Abakac857a462021-05-26 13:45:54 +0000457type RxPower struct {
458 ListOutputOptions
459 Args struct {
460 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
461 PortNo uint32 `positional-arg-name:"PORT_NO" required:"yes"`
462 OnuNo uint32 `positional-arg-name:"ONU_NO" required:"yes"`
463 } `positional-args:"yes"`
464}
465
praneeth nalmas39c71ad2023-09-27 18:29:04 +0530466type PonRxPower struct {
467 ListOutputOptions
468 Args struct {
469 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
470 PortLabel string `positional-arg-name:"PORT_LABEL" required:"yes"`
471 SerialNo string `positional-arg-name:"ONU_SERIAL_NUMBER"`
472 } `positional-args:"yes"`
473}
474
serkantul3d22fc72022-09-14 12:22:56 +0300475type OnuOmciTxRxStats struct {
476 ListOutputOptions
477 Args struct {
478 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
479 } `positional-args:"yes"`
480}
481
praneeth nalmas1dd094c2022-12-22 14:15:13 +0530482type GetOnuOmciActiveAlarms struct {
483 ListOutputOptions
484 Args struct {
485 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
486 } `positional-args:"yes"`
487}
488
Akash Soni66db9632024-04-15 09:05:15 +0530489type GetOnuDistance struct {
490 ListOutputOptions
491 Args struct {
492 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
493 OnuId OnuId `positional-arg-name:"ONU_ID" required:"yes"`
494 } `positional-args:"yes"` //onu device id
495}
496
Zack Williamse940c7a2019-08-21 14:25:39 -0700497type DeviceOpts struct {
Himani Chawla3c161c62021-05-13 16:36:51 +0530498 List DeviceList `command:"list"`
499 Create DeviceCreate `command:"create"`
500 Delete DeviceDelete `command:"delete"`
501 Enable DeviceEnable `command:"enable"`
502 Disable DeviceDisable `command:"disable"`
503 Flows DeviceFlowList `command:"flows"`
504 Groups DeviceFlowGroupList `command:"groups"`
kesavand12cd8eb2020-01-20 22:25:22 -0500505 Port struct {
506 List DevicePortList `command:"list"`
507 Enable DevicePortEnable `command:"enable"`
508 Disable DevicePortDisable `command:"disable"`
509 } `command:"port"`
510 Inspect DeviceInspect `command:"inspect"`
511 Reboot DeviceReboot `command:"reboot"`
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -0800512 Value struct {
513 Get DeviceGetExtValue `command:"get"`
514 } `command:"value"`
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000515 PmConfig struct {
Rohan Agrawald7df3772020-06-29 11:23:36 +0000516 Get DevicePmConfigsGet `command:"get"`
517 MaxSkew struct {
518 Set DevicePmConfigSetMaxSkew `command:"set"`
519 } `command:"maxskew"`
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000520 Frequency struct {
521 Set DevicePmConfigFrequencySet `command:"set"`
522 } `command:"frequency"`
523 Metric struct {
524 List DevicePmConfigMetricList `command:"list"`
525 Enable DevicePmConfigMetricEnable `command:"enable"`
526 Disable DevicePmConfigMetricDisable `command:"disable"`
527 } `command:"metric"`
528 Group struct {
Girish Gowdra610acb42021-01-27 13:33:57 -0800529 List DevicePmConfigGroupList `command:"list"`
530 Enable DevicePmConfigGroupEnable `command:"enable"`
531 Disable DevicePmConfigGroupDisable `command:"disable"`
532 Set DevicePmConfigGroupFrequencySet `command:"set"`
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000533 } `command:"group"`
534 GroupMetric struct {
535 List DevicePmConfigGroupMetricList `command:"list"`
536 } `command:"groupmetric"`
537 } `command:"pmconfig"`
Andrea Campanella791d88b2021-01-08 13:29:00 +0100538 Image struct {
539 Get DeviceOnuListImages `command:"list"`
540 Download DeviceOnuDownloadImage `command:"download"`
541 Activate DeviceOnuActivateImageUpdate `command:"activate"`
542 } `command:"image"`
kesavand3e2f9f62021-04-22 11:06:38 +0530543 DownloadImage struct {
544 Download OnuDownloadImage `command:"download"`
545 Activate OnuActivateImage `command:"activate"`
546 Commit OnuCommitImage `command:"commit"`
547 AbortUpgrade OnuAbortUpgradeImage `command:"abort"`
548 Status OnuImageStatus `command:"status"`
549 List OnuListImages `command:"list" `
550 } `command:"onuimage"`
kesavand8ec4fc02021-01-27 09:10:22 -0500551 GetExtVal struct {
Himani Chawla553a1392021-06-10 23:39:17 +0530552 Stats DeviceGetPortStats `command:"portstats"`
553 UniStatus UniStatus `command:"unistatus"`
554 OpticalInfo OnuPonOpticalInfo `command:"onu_pon_optical_info"`
555 OnuStats GetOnuStats `command:"onu_stats"`
556 EthernetFrameExtendedPm GetOnuEthernetFrameExtendedPmCounters `command:"ethernet_frame_extended_pm"`
557 RxPower RxPower `command:"rxpower"`
serkantul3d22fc72022-09-14 12:22:56 +0300558 OnuOmciStats OnuOmciTxRxStats `command:"onu_omci_stats"`
praneeth nalmas1dd094c2022-12-22 14:15:13 +0530559 OnuOmciActiveAlarms GetOnuOmciActiveAlarms `command:"onu_omci_active_alarms"`
praneeth nalmas39c71ad2023-09-27 18:29:04 +0530560 PonRxPower PonRxPower `command:"pon_rx_power"`
Akash Soni66db9632024-04-15 09:05:15 +0530561 OnuDistance GetOnuDistance `command:"onu_distance"`
Akash Soni6e879c22024-12-20 17:01:34 +0530562 OffloadAppStats GetOffloadApp `command:"offload_app_stats"`
kesavand8ec4fc02021-01-27 09:10:22 -0500563 } `command:"getextval"`
Akash Soni51b6b7a2024-11-20 11:39:38 +0530564 SetExtVal struct {
Akash Soni6e879c22024-12-20 17:01:34 +0530565 OffloadAppStatsSet SetOffloadApp `command:"set_offload_app"`
566 OnuOffloadStatsSet SetOnuOffload `command:"set_onu_offload"`
Akash Soni51b6b7a2024-11-20 11:39:38 +0530567 } `command:"setextval"`
Zack Williamse940c7a2019-08-21 14:25:39 -0700568}
569
570var deviceOpts = DeviceOpts{}
571
572func RegisterDeviceCommands(parser *flags.Parser) {
David Bainbridge12f036f2019-10-15 22:09:04 +0000573 if _, err := parser.AddCommand("device", "device commands", "Commands to query and manipulate VOLTHA devices", &deviceOpts); err != nil {
David Bainbridgea6722342019-10-24 23:55:53 +0000574 Error.Fatalf("Unexpected error while attempting to register device commands : %s", err)
David Bainbridge12f036f2019-10-15 22:09:04 +0000575 }
Zack Williamse940c7a2019-08-21 14:25:39 -0700576}
577
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000578func (i *MetricName) Complete(match string) []flags.Completion {
579 conn, err := NewConnection()
580 if err != nil {
581 return nil
582 }
583 defer conn.Close()
584
585 client := voltha.NewVolthaServiceClient(conn)
586
587 var deviceId string
588found:
589 for i := len(os.Args) - 1; i >= 0; i -= 1 {
590 switch os.Args[i] {
591 case "enable":
592 fallthrough
593 case "disable":
594 if len(os.Args) > i+1 {
595 deviceId = os.Args[i+1]
596 } else {
597 return nil
598 }
599 break found
600 default:
601 }
602 }
603
604 if len(deviceId) == 0 {
605 return nil
606 }
607
David K. Bainbridge9189c632021-03-26 21:52:21 +0000608 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000609 defer cancel()
610
611 id := voltha.ID{Id: string(deviceId)}
612
613 pmconfigs, err := client.ListDevicePmConfigs(ctx, &id)
614
615 if err != nil {
616 return nil
617 }
618
619 list := make([]flags.Completion, 0)
620 for _, metrics := range pmconfigs.Metrics {
621 if strings.HasPrefix(metrics.Name, match) {
622 list = append(list, flags.Completion{Item: metrics.Name})
623 }
624 }
625
626 return list
627}
628
629func (i *GroupName) Complete(match string) []flags.Completion {
630 conn, err := NewConnection()
631 if err != nil {
632 return nil
633 }
634 defer conn.Close()
635
636 client := voltha.NewVolthaServiceClient(conn)
637
638 var deviceId string
639found:
640 for i := len(os.Args) - 1; i >= 0; i -= 1 {
641 switch os.Args[i] {
642 case "list":
643 fallthrough
644 case "enable":
645 fallthrough
646 case "disable":
647 if len(os.Args) > i+1 {
648 deviceId = os.Args[i+1]
649 } else {
650 return nil
651 }
652 break found
653 default:
654 }
655 }
656
657 if len(deviceId) == 0 {
658 return nil
659 }
660
David K. Bainbridge9189c632021-03-26 21:52:21 +0000661 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000662 defer cancel()
663
664 id := voltha.ID{Id: string(deviceId)}
665
666 pmconfigs, err := client.ListDevicePmConfigs(ctx, &id)
667
668 if err != nil {
669 return nil
670 }
671
672 list := make([]flags.Completion, 0)
673 for _, group := range pmconfigs.Groups {
674 if strings.HasPrefix(group.GroupName, match) {
675 list = append(list, flags.Completion{Item: group.GroupName})
676 }
677 }
678 return list
679}
680
kesavand12cd8eb2020-01-20 22:25:22 -0500681func (i *PortNum) Complete(match string) []flags.Completion {
682 conn, err := NewConnection()
683 if err != nil {
684 return nil
685 }
686 defer conn.Close()
687
Scott Baker9173ed82020-05-19 08:30:12 -0700688 client := voltha.NewVolthaServiceClient(conn)
kesavand12cd8eb2020-01-20 22:25:22 -0500689
690 /*
691 * The command line args when completing for PortNum will be a DeviceId
692 * followed by one or more PortNums. So walk the argument list from the
693 * end and find the first argument that is enable/disable as those are
694 * the subcommands that come before the positional arguments. It would
695 * be nice if this package gave us the list of optional arguments
696 * already parsed.
697 */
698 var deviceId string
699found:
700 for i := len(os.Args) - 1; i >= 0; i -= 1 {
701 switch os.Args[i] {
702 case "enable":
703 fallthrough
704 case "disable":
705 if len(os.Args) > i+1 {
706 deviceId = os.Args[i+1]
707 } else {
708 return nil
709 }
710 break found
711 default:
712 }
713 }
714
715 if len(deviceId) == 0 {
716 return nil
717 }
718
David K. Bainbridge9189c632021-03-26 21:52:21 +0000719 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
kesavand12cd8eb2020-01-20 22:25:22 -0500720 defer cancel()
kesavand12cd8eb2020-01-20 22:25:22 -0500721
Scott Baker9173ed82020-05-19 08:30:12 -0700722 id := voltha.ID{Id: string(deviceId)}
kesavand12cd8eb2020-01-20 22:25:22 -0500723
Scott Baker9173ed82020-05-19 08:30:12 -0700724 ports, err := client.ListDevicePorts(ctx, &id)
kesavand12cd8eb2020-01-20 22:25:22 -0500725 if err != nil {
726 return nil
727 }
728
729 list := make([]flags.Completion, 0)
Scott Baker9173ed82020-05-19 08:30:12 -0700730 for _, item := range ports.Items {
731 pn := strconv.FormatUint(uint64(item.PortNo), 10)
kesavand12cd8eb2020-01-20 22:25:22 -0500732 if strings.HasPrefix(pn, match) {
733 list = append(list, flags.Completion{Item: pn})
734 }
735 }
736
737 return list
738}
739
Zack Williamse940c7a2019-08-21 14:25:39 -0700740func (i *DeviceId) Complete(match string) []flags.Completion {
741 conn, err := NewConnection()
742 if err != nil {
743 return nil
744 }
745 defer conn.Close()
746
Scott Baker9173ed82020-05-19 08:30:12 -0700747 client := voltha.NewVolthaServiceClient(conn)
Zack Williamse940c7a2019-08-21 14:25:39 -0700748
David K. Bainbridge9189c632021-03-26 21:52:21 +0000749 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Zack Williamse940c7a2019-08-21 14:25:39 -0700750 defer cancel()
751
Scott Baker9173ed82020-05-19 08:30:12 -0700752 devices, err := client.ListDevices(ctx, &empty.Empty{})
Zack Williamse940c7a2019-08-21 14:25:39 -0700753 if err != nil {
754 return nil
755 }
756
757 list := make([]flags.Completion, 0)
Scott Baker9173ed82020-05-19 08:30:12 -0700758 for _, item := range devices.Items {
759 if strings.HasPrefix(item.Id, match) {
760 list = append(list, flags.Completion{Item: item.Id})
Zack Williamse940c7a2019-08-21 14:25:39 -0700761 }
762 }
763
764 return list
765}
766
767func (options *DeviceList) Execute(args []string) error {
768
769 conn, err := NewConnection()
770 if err != nil {
771 return err
772 }
773 defer conn.Close()
774
Scott Baker9173ed82020-05-19 08:30:12 -0700775 client := voltha.NewVolthaServiceClient(conn)
Zack Williamse940c7a2019-08-21 14:25:39 -0700776
David K. Bainbridge9189c632021-03-26 21:52:21 +0000777 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Zack Williamse940c7a2019-08-21 14:25:39 -0700778 defer cancel()
779
Scott Baker9173ed82020-05-19 08:30:12 -0700780 devices, err := client.ListDevices(ctx, &empty.Empty{})
Zack Williamse940c7a2019-08-21 14:25:39 -0700781 if err != nil {
782 return err
783 }
784
785 outputFormat := CharReplacer.Replace(options.Format)
786 if outputFormat == "" {
David Bainbridgea6722342019-10-24 23:55:53 +0000787 outputFormat = GetCommandOptionWithDefault("device-list", "format", DEFAULT_DEVICE_FORMAT)
Zack Williamse940c7a2019-08-21 14:25:39 -0700788 }
789 if options.Quiet {
790 outputFormat = "{{.Id}}"
791 }
792
David Bainbridgea6722342019-10-24 23:55:53 +0000793 orderBy := options.OrderBy
794 if orderBy == "" {
Hardik Windlass9361bb82022-03-23 05:58:48 +0000795 orderBy = GetCommandOptionWithDefault("device-list", "order", DEFAULT_DEVICE_ORDER)
David Bainbridgea6722342019-10-24 23:55:53 +0000796 }
797
Scott Baker9173ed82020-05-19 08:30:12 -0700798 // Make sure json output prints an empty list, not "null"
799 if devices.Items == nil {
800 devices.Items = make([]*voltha.Device, 0)
Zack Williamse940c7a2019-08-21 14:25:39 -0700801 }
802
803 result := CommandResult{
804 Format: format.Format(outputFormat),
805 Filter: options.Filter,
David Bainbridgea6722342019-10-24 23:55:53 +0000806 OrderBy: orderBy,
Zack Williamse940c7a2019-08-21 14:25:39 -0700807 OutputAs: toOutputType(options.OutputAs),
808 NameLimit: options.NameLimit,
Scott Baker9173ed82020-05-19 08:30:12 -0700809 Data: devices.Items,
Zack Williamse940c7a2019-08-21 14:25:39 -0700810 }
811
812 GenerateOutput(&result)
813 return nil
814}
815
816func (options *DeviceCreate) Execute(args []string) error {
817
Scott Baker9173ed82020-05-19 08:30:12 -0700818 device := voltha.Device{}
Zack Williamse940c7a2019-08-21 14:25:39 -0700819 if options.HostAndPort != "" {
Scott Baker9173ed82020-05-19 08:30:12 -0700820 device.Address = &voltha.Device_HostAndPort{HostAndPort: options.HostAndPort}
Zack Williamse940c7a2019-08-21 14:25:39 -0700821 } else if options.IPAddress != "" {
Scott Baker9173ed82020-05-19 08:30:12 -0700822 device.Address = &voltha.Device_Ipv4Address{Ipv4Address: options.IPAddress}
Hardik Windlassce1de342020-02-04 21:58:07 +0000823 }
824 if options.MACAddress != "" {
Scott Baker9173ed82020-05-19 08:30:12 -0700825 device.MacAddress = strings.ToLower(options.MACAddress)
Zack Williamse940c7a2019-08-21 14:25:39 -0700826 }
827 if options.DeviceType != "" {
Scott Baker9173ed82020-05-19 08:30:12 -0700828 device.Type = options.DeviceType
Zack Williamse940c7a2019-08-21 14:25:39 -0700829 }
830
831 conn, err := NewConnection()
832 if err != nil {
833 return err
834 }
835 defer conn.Close()
836
Scott Baker9173ed82020-05-19 08:30:12 -0700837 client := voltha.NewVolthaServiceClient(conn)
Zack Williamse940c7a2019-08-21 14:25:39 -0700838
David K. Bainbridge9189c632021-03-26 21:52:21 +0000839 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Zack Williamse940c7a2019-08-21 14:25:39 -0700840 defer cancel()
841
Scott Baker9173ed82020-05-19 08:30:12 -0700842 createdDevice, err := client.CreateDevice(ctx, &device)
Zack Williamse940c7a2019-08-21 14:25:39 -0700843 if err != nil {
844 return err
Zack Williamse940c7a2019-08-21 14:25:39 -0700845 }
846
Scott Baker9173ed82020-05-19 08:30:12 -0700847 fmt.Printf("%s\n", createdDevice.Id)
Zack Williamse940c7a2019-08-21 14:25:39 -0700848
849 return nil
850}
851
852func (options *DeviceDelete) Execute(args []string) error {
853
854 conn, err := NewConnection()
855 if err != nil {
856 return err
857 }
858 defer conn.Close()
859
Scott Baker9173ed82020-05-19 08:30:12 -0700860 client := voltha.NewVolthaServiceClient(conn)
David Bainbridge7052fe82020-03-25 10:37:00 -0700861 var lastErr error
Zack Williamse940c7a2019-08-21 14:25:39 -0700862 for _, i := range options.Args.Ids {
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(i)}
Himani Chawla9933ddc2020-10-12 23:53:27 +0530867 if options.Force {
868 _, err = client.ForceDeleteDevice(ctx, &id)
869 } else {
870 _, err = client.DeleteDevice(ctx, &id)
871 }
Scott Baker9173ed82020-05-19 08:30:12 -0700872
Zack Williamse940c7a2019-08-21 14:25:39 -0700873 if err != nil {
David Bainbridge0f758d42019-10-26 05:17:48 +0000874 Error.Printf("Error while deleting '%s': %s\n", i, err)
David Bainbridge7052fe82020-03-25 10:37:00 -0700875 lastErr = err
Zack Williamse940c7a2019-08-21 14:25:39 -0700876 continue
Zack Williamse940c7a2019-08-21 14:25:39 -0700877 }
878 fmt.Printf("%s\n", i)
879 }
880
David Bainbridge7052fe82020-03-25 10:37:00 -0700881 if lastErr != nil {
882 return NoReportErr
883 }
Zack Williamse940c7a2019-08-21 14:25:39 -0700884 return nil
885}
886
887func (options *DeviceEnable) Execute(args []string) error {
888 conn, err := NewConnection()
889 if err != nil {
890 return err
891 }
892 defer conn.Close()
893
Scott Baker9173ed82020-05-19 08:30:12 -0700894 client := voltha.NewVolthaServiceClient(conn)
Zack Williamse940c7a2019-08-21 14:25:39 -0700895
David Bainbridge7052fe82020-03-25 10:37:00 -0700896 var lastErr error
Zack Williamse940c7a2019-08-21 14:25:39 -0700897 for _, i := range options.Args.Ids {
David K. Bainbridge9189c632021-03-26 21:52:21 +0000898 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Zack Williamse940c7a2019-08-21 14:25:39 -0700899 defer cancel()
900
Scott Baker9173ed82020-05-19 08:30:12 -0700901 id := voltha.ID{Id: string(i)}
902
903 _, err := client.EnableDevice(ctx, &id)
Zack Williamse940c7a2019-08-21 14:25:39 -0700904 if err != nil {
David Bainbridge0f758d42019-10-26 05:17:48 +0000905 Error.Printf("Error while enabling '%s': %s\n", i, err)
David Bainbridge7052fe82020-03-25 10:37:00 -0700906 lastErr = err
Zack Williamse940c7a2019-08-21 14:25:39 -0700907 continue
Zack Williamse940c7a2019-08-21 14:25:39 -0700908 }
909 fmt.Printf("%s\n", i)
910 }
911
David Bainbridge7052fe82020-03-25 10:37:00 -0700912 if lastErr != nil {
913 return NoReportErr
914 }
Zack Williamse940c7a2019-08-21 14:25:39 -0700915 return nil
916}
917
918func (options *DeviceDisable) Execute(args []string) error {
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 Bainbridge7052fe82020-03-25 10:37:00 -0700927 var lastErr error
Zack Williamse940c7a2019-08-21 14:25:39 -0700928 for _, i := range options.Args.Ids {
David K. Bainbridge9189c632021-03-26 21:52:21 +0000929 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Zack Williamse940c7a2019-08-21 14:25:39 -0700930 defer cancel()
931
Scott Baker9173ed82020-05-19 08:30:12 -0700932 id := voltha.ID{Id: string(i)}
933
934 _, err := client.DisableDevice(ctx, &id)
Zack Williamse940c7a2019-08-21 14:25:39 -0700935 if err != nil {
David Bainbridge0f758d42019-10-26 05:17:48 +0000936 Error.Printf("Error while disabling '%s': %s\n", i, err)
David Bainbridge7052fe82020-03-25 10:37:00 -0700937 lastErr = err
Zack Williamse940c7a2019-08-21 14:25:39 -0700938 continue
Zack Williamse940c7a2019-08-21 14:25:39 -0700939 }
940 fmt.Printf("%s\n", i)
941 }
942
David Bainbridge7052fe82020-03-25 10:37:00 -0700943 if lastErr != nil {
944 return NoReportErr
945 }
Zack Williamse940c7a2019-08-21 14:25:39 -0700946 return nil
947}
948
949func (options *DeviceReboot) Execute(args []string) error {
950 conn, err := NewConnection()
951 if err != nil {
952 return err
953 }
954 defer conn.Close()
955
Scott Baker9173ed82020-05-19 08:30:12 -0700956 client := voltha.NewVolthaServiceClient(conn)
Zack Williamse940c7a2019-08-21 14:25:39 -0700957
David Bainbridge7052fe82020-03-25 10:37:00 -0700958 var lastErr error
Zack Williamse940c7a2019-08-21 14:25:39 -0700959 for _, i := range options.Args.Ids {
David K. Bainbridge9189c632021-03-26 21:52:21 +0000960 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Zack Williamse940c7a2019-08-21 14:25:39 -0700961 defer cancel()
962
Scott Baker9173ed82020-05-19 08:30:12 -0700963 id := voltha.ID{Id: string(i)}
964
965 _, err := client.RebootDevice(ctx, &id)
Zack Williamse940c7a2019-08-21 14:25:39 -0700966 if err != nil {
David Bainbridge0f758d42019-10-26 05:17:48 +0000967 Error.Printf("Error while rebooting '%s': %s\n", i, err)
David Bainbridge7052fe82020-03-25 10:37:00 -0700968 lastErr = err
Zack Williamse940c7a2019-08-21 14:25:39 -0700969 continue
Zack Williamse940c7a2019-08-21 14:25:39 -0700970 }
971 fmt.Printf("%s\n", i)
972 }
973
David Bainbridge7052fe82020-03-25 10:37:00 -0700974 if lastErr != nil {
975 return NoReportErr
976 }
Zack Williamse940c7a2019-08-21 14:25:39 -0700977 return nil
978}
979
980func (options *DevicePortList) Execute(args []string) error {
981
982 conn, err := NewConnection()
983 if err != nil {
984 return err
985 }
986 defer conn.Close()
987
Scott Baker9173ed82020-05-19 08:30:12 -0700988 client := voltha.NewVolthaServiceClient(conn)
Zack Williamse940c7a2019-08-21 14:25:39 -0700989
David K. Bainbridge9189c632021-03-26 21:52:21 +0000990 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Zack Williamse940c7a2019-08-21 14:25:39 -0700991 defer cancel()
992
Scott Baker9173ed82020-05-19 08:30:12 -0700993 id := voltha.ID{Id: string(options.Args.Id)}
Zack Williamse940c7a2019-08-21 14:25:39 -0700994
Scott Baker9173ed82020-05-19 08:30:12 -0700995 ports, err := client.ListDevicePorts(ctx, &id)
Zack Williamse940c7a2019-08-21 14:25:39 -0700996 if err != nil {
997 return err
998 }
999
1000 outputFormat := CharReplacer.Replace(options.Format)
1001 if outputFormat == "" {
David Bainbridgea6722342019-10-24 23:55:53 +00001002 outputFormat = GetCommandOptionWithDefault("device-ports", "format", DEFAULT_DEVICE_PORTS_FORMAT)
Zack Williamse940c7a2019-08-21 14:25:39 -07001003 }
Zack Williamse940c7a2019-08-21 14:25:39 -07001004
David Bainbridgea6722342019-10-24 23:55:53 +00001005 orderBy := options.OrderBy
1006 if orderBy == "" {
1007 orderBy = GetCommandOptionWithDefault("device-ports", "order", "")
1008 }
1009
Zack Williamse940c7a2019-08-21 14:25:39 -07001010 result := CommandResult{
1011 Format: format.Format(outputFormat),
1012 Filter: options.Filter,
David Bainbridgea6722342019-10-24 23:55:53 +00001013 OrderBy: orderBy,
Zack Williamse940c7a2019-08-21 14:25:39 -07001014 OutputAs: toOutputType(options.OutputAs),
1015 NameLimit: options.NameLimit,
Scott Baker9173ed82020-05-19 08:30:12 -07001016 Data: ports.Items,
Zack Williamse940c7a2019-08-21 14:25:39 -07001017 }
1018
1019 GenerateOutput(&result)
1020 return nil
1021}
1022
1023func (options *DeviceFlowList) Execute(args []string) error {
1024 fl := &FlowList{}
1025 fl.ListOutputOptions = options.ListOutputOptions
Maninder045921e2020-09-29 16:46:02 +05301026 fl.FlowIdOptions = options.FlowIdOptions
Zack Williamse940c7a2019-08-21 14:25:39 -07001027 fl.Args.Id = string(options.Args.Id)
David Bainbridgea6722342019-10-24 23:55:53 +00001028 fl.Method = "device-flows"
Zack Williamse940c7a2019-08-21 14:25:39 -07001029 return fl.Execute(args)
1030}
1031
Himani Chawla3c161c62021-05-13 16:36:51 +05301032func (options *DeviceFlowGroupList) Execute(args []string) error {
1033 grp := &GroupList{}
1034 grp.ListOutputOptions = options.ListOutputOptions
1035 grp.GroupListOptions = options.GroupListOptions
1036 grp.Args.Id = string(options.Args.Id)
1037 grp.Method = "device-groups"
1038 return grp.Execute(args)
1039}
1040
Zack Williamse940c7a2019-08-21 14:25:39 -07001041func (options *DeviceInspect) Execute(args []string) error {
1042 if len(args) > 0 {
1043 return fmt.Errorf("only a single argument 'DEVICE_ID' can be provided")
1044 }
1045
1046 conn, err := NewConnection()
1047 if err != nil {
1048 return err
1049 }
1050 defer conn.Close()
1051
Scott Baker9173ed82020-05-19 08:30:12 -07001052 client := voltha.NewVolthaServiceClient(conn)
Zack Williamse940c7a2019-08-21 14:25:39 -07001053
David K. Bainbridge9189c632021-03-26 21:52:21 +00001054 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Zack Williamse940c7a2019-08-21 14:25:39 -07001055 defer cancel()
1056
Scott Baker9173ed82020-05-19 08:30:12 -07001057 id := voltha.ID{Id: string(options.Args.Id)}
Zack Williamse940c7a2019-08-21 14:25:39 -07001058
Scott Baker9173ed82020-05-19 08:30:12 -07001059 device, err := client.GetDevice(ctx, &id)
Zack Williamse940c7a2019-08-21 14:25:39 -07001060 if err != nil {
1061 return err
1062 }
1063
Zack Williamse940c7a2019-08-21 14:25:39 -07001064 outputFormat := CharReplacer.Replace(options.Format)
1065 if outputFormat == "" {
David Bainbridgea6722342019-10-24 23:55:53 +00001066 outputFormat = GetCommandOptionWithDefault("device-inspect", "format", DEFAULT_DEVICE_INSPECT_FORMAT)
Zack Williamse940c7a2019-08-21 14:25:39 -07001067 }
1068 if options.Quiet {
1069 outputFormat = "{{.Id}}"
1070 }
1071
1072 result := CommandResult{
1073 Format: format.Format(outputFormat),
1074 OutputAs: toOutputType(options.OutputAs),
1075 NameLimit: options.NameLimit,
1076 Data: device,
1077 }
1078 GenerateOutput(&result)
1079 return nil
1080}
kesavand12cd8eb2020-01-20 22:25:22 -05001081
1082/*Device Port Enable */
1083func (options *DevicePortEnable) Execute(args []string) error {
1084 conn, err := NewConnection()
1085 if err != nil {
1086 return err
1087 }
1088 defer conn.Close()
1089
Scott Baker9173ed82020-05-19 08:30:12 -07001090 client := voltha.NewVolthaServiceClient(conn)
kesavand12cd8eb2020-01-20 22:25:22 -05001091
David K. Bainbridge9189c632021-03-26 21:52:21 +00001092 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
kesavand12cd8eb2020-01-20 22:25:22 -05001093 defer cancel()
1094
Scott Baker9173ed82020-05-19 08:30:12 -07001095 port := voltha.Port{DeviceId: string(options.Args.Id), PortNo: uint32(options.Args.PortId)}
1096
1097 _, err = client.EnablePort(ctx, &port)
kesavand12cd8eb2020-01-20 22:25:22 -05001098 if err != nil {
1099 Error.Printf("Error enabling port number %v on device Id %s,err=%s\n", options.Args.PortId, options.Args.Id, ErrorToString(err))
1100 return err
kesavand12cd8eb2020-01-20 22:25:22 -05001101 }
1102
1103 return nil
1104}
1105
Scott Baker9173ed82020-05-19 08:30:12 -07001106/*Device Port Disable */
kesavand12cd8eb2020-01-20 22:25:22 -05001107func (options *DevicePortDisable) Execute(args []string) error {
1108 conn, err := NewConnection()
1109 if err != nil {
1110 return err
1111 }
1112 defer conn.Close()
1113
Scott Baker9173ed82020-05-19 08:30:12 -07001114 client := voltha.NewVolthaServiceClient(conn)
1115
David K. Bainbridge9189c632021-03-26 21:52:21 +00001116 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
kesavand12cd8eb2020-01-20 22:25:22 -05001117 defer cancel()
1118
Scott Baker9173ed82020-05-19 08:30:12 -07001119 port := voltha.Port{DeviceId: string(options.Args.Id), PortNo: uint32(options.Args.PortId)}
1120
1121 _, err = client.DisablePort(ctx, &port)
kesavand12cd8eb2020-01-20 22:25:22 -05001122 if err != nil {
Scott Baker9173ed82020-05-19 08:30:12 -07001123 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 -05001124 return err
kesavand12cd8eb2020-01-20 22:25:22 -05001125 }
Scott Baker9173ed82020-05-19 08:30:12 -07001126
kesavand12cd8eb2020-01-20 22:25:22 -05001127 return nil
1128}
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -08001129
Rohan Agrawald7df3772020-06-29 11:23:36 +00001130func (options *DevicePmConfigSetMaxSkew) Execute(args []string) error {
1131 conn, err := NewConnection()
1132 if err != nil {
1133 return err
1134 }
1135 defer conn.Close()
1136
1137 client := voltha.NewVolthaServiceClient(conn)
1138
David K. Bainbridge9189c632021-03-26 21:52:21 +00001139 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Rohan Agrawald7df3772020-06-29 11:23:36 +00001140 defer cancel()
1141
1142 id := voltha.ID{Id: string(options.Args.Id)}
1143
1144 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
1145 if err != nil {
1146 return err
1147 }
1148
1149 pmConfigs.MaxSkew = options.Args.MaxSkew
1150
1151 _, err = client.UpdateDevicePmConfigs(ctx, pmConfigs)
1152 if err != nil {
1153 return err
1154 }
1155
1156 return nil
1157}
1158
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001159func (options *DevicePmConfigsGet) Execute(args []string) error {
1160
1161 conn, err := NewConnection()
1162 if err != nil {
1163 return err
1164 }
1165 defer conn.Close()
1166
1167 client := voltha.NewVolthaServiceClient(conn)
1168
David K. Bainbridge9189c632021-03-26 21:52:21 +00001169 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001170 defer cancel()
1171
1172 id := voltha.ID{Id: string(options.Args.Id)}
1173
1174 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
1175 if err != nil {
1176 return err
1177 }
1178
1179 outputFormat := CharReplacer.Replace(options.Format)
1180 if outputFormat == "" {
1181 outputFormat = GetCommandOptionWithDefault("device-pm-configs", "format", DEFAULT_DEVICE_PM_CONFIG_GET_FORMAT)
1182 }
1183
1184 orderBy := options.OrderBy
1185 if orderBy == "" {
1186 orderBy = GetCommandOptionWithDefault("device-pm-configs", "order", "")
1187 }
1188
1189 result := CommandResult{
1190 Format: format.Format(outputFormat),
1191 Filter: options.Filter,
1192 OrderBy: orderBy,
1193 OutputAs: toOutputType(options.OutputAs),
1194 NameLimit: options.NameLimit,
1195 Data: pmConfigs,
1196 }
1197
1198 GenerateOutput(&result)
1199 return nil
1200
1201}
1202
1203func (options *DevicePmConfigMetricList) Execute(args []string) error {
1204
1205 conn, err := NewConnection()
1206 if err != nil {
1207 return err
1208 }
1209 defer conn.Close()
1210
1211 client := voltha.NewVolthaServiceClient(conn)
1212
David K. Bainbridge9189c632021-03-26 21:52:21 +00001213 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001214 defer cancel()
1215
1216 id := voltha.ID{Id: string(options.Args.Id)}
1217
1218 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
1219 if err != nil {
1220 return err
1221 }
1222
1223 if !pmConfigs.Grouped {
1224 for _, metric := range pmConfigs.Metrics {
1225 if metric.SampleFreq == 0 {
1226 metric.SampleFreq = pmConfigs.DefaultFreq
1227 }
1228 }
Rohan Agrawalbca69122020-06-17 14:59:03 +00001229 outputFormat := CharReplacer.Replace(options.Format)
1230 if outputFormat == "" {
1231 outputFormat = GetCommandOptionWithDefault("device-pm-configs", "format", DEFAULT_DEVICE_PM_CONFIG_METRIC_LIST_FORMAT)
1232 }
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001233
Rohan Agrawalbca69122020-06-17 14:59:03 +00001234 orderBy := options.OrderBy
1235 if orderBy == "" {
1236 orderBy = GetCommandOptionWithDefault("device-pm-configs", "order", "")
1237 }
1238
1239 result := CommandResult{
1240 Format: format.Format(outputFormat),
1241 Filter: options.Filter,
1242 OrderBy: orderBy,
1243 OutputAs: toOutputType(options.OutputAs),
1244 NameLimit: options.NameLimit,
1245 Data: pmConfigs.Metrics,
1246 }
1247
1248 GenerateOutput(&result)
1249 return nil
1250 } else {
1251 return fmt.Errorf("Device '%s' does not have Non Grouped Metrics", options.Args.Id)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001252 }
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001253}
1254
1255func (options *DevicePmConfigMetricEnable) Execute(args []string) error {
1256
1257 conn, err := NewConnection()
1258 if err != nil {
1259 return err
1260 }
1261 defer conn.Close()
1262
1263 client := voltha.NewVolthaServiceClient(conn)
1264
David K. Bainbridge9189c632021-03-26 21:52:21 +00001265 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001266 defer cancel()
1267
1268 id := voltha.ID{Id: string(options.Args.Id)}
1269
1270 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
1271 if err != nil {
1272 return err
1273 }
1274
1275 if !pmConfigs.Grouped {
Rohan Agrawalbca69122020-06-17 14:59:03 +00001276 metrics := make(map[string]struct{})
1277 for _, metric := range pmConfigs.Metrics {
1278 metrics[metric.Name] = struct{}{}
1279 }
1280
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001281 for _, metric := range pmConfigs.Metrics {
1282 for _, mName := range options.Args.Metrics {
Rohan Agrawalbca69122020-06-17 14:59:03 +00001283 if _, exist := metrics[string(mName)]; !exist {
1284 return fmt.Errorf("Metric Name '%s' does not exist", mName)
1285 }
1286
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001287 if string(mName) == metric.Name && !metric.Enabled {
1288 metric.Enabled = true
1289 _, err := client.UpdateDevicePmConfigs(ctx, pmConfigs)
1290 if err != nil {
1291 return err
1292 }
1293 }
1294 }
1295 }
Rohan Agrawalbca69122020-06-17 14:59:03 +00001296 } else {
1297 return fmt.Errorf("Device '%s' does not have Non Grouped Metrics", options.Args.Id)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001298 }
1299 return nil
1300}
1301
1302func (options *DevicePmConfigMetricDisable) Execute(args []string) error {
1303
1304 conn, err := NewConnection()
1305 if err != nil {
1306 return err
1307 }
1308 defer conn.Close()
1309
1310 client := voltha.NewVolthaServiceClient(conn)
1311
David K. Bainbridge9189c632021-03-26 21:52:21 +00001312 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001313 defer cancel()
1314
1315 id := voltha.ID{Id: string(options.Args.Id)}
1316
1317 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
1318 if err != nil {
1319 return err
1320 }
1321
1322 if !pmConfigs.Grouped {
Rohan Agrawalbca69122020-06-17 14:59:03 +00001323 metrics := make(map[string]struct{})
1324 for _, metric := range pmConfigs.Metrics {
1325 metrics[metric.Name] = struct{}{}
1326 }
1327
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001328 for _, metric := range pmConfigs.Metrics {
1329 for _, mName := range options.Args.Metrics {
Rohan Agrawalbca69122020-06-17 14:59:03 +00001330 if _, have := metrics[string(mName)]; !have {
1331 return fmt.Errorf("Metric Name '%s' does not exist", mName)
1332 }
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001333 if string(mName) == metric.Name && metric.Enabled {
1334 metric.Enabled = false
1335 _, err := client.UpdateDevicePmConfigs(ctx, pmConfigs)
1336 if err != nil {
1337 return err
1338 }
Rohan Agrawalbca69122020-06-17 14:59:03 +00001339 } else {
1340 return fmt.Errorf("Metric '%s' cannot be disabled", string(mName))
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001341 }
1342 }
1343 }
Rohan Agrawalbca69122020-06-17 14:59:03 +00001344 } else {
1345 return fmt.Errorf("Device '%s' does not have Non Grouped Metrics", options.Args.Id)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001346 }
1347 return nil
1348}
1349
1350func (options *DevicePmConfigGroupEnable) Execute(args []string) error {
1351
1352 conn, err := NewConnection()
1353 if err != nil {
1354 return err
1355 }
1356 defer conn.Close()
1357
1358 client := voltha.NewVolthaServiceClient(conn)
1359
David K. Bainbridge9189c632021-03-26 21:52:21 +00001360 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001361 defer cancel()
1362
1363 id := voltha.ID{Id: string(options.Args.Id)}
1364
1365 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
1366 if err != nil {
1367 return err
1368 }
1369
1370 if pmConfigs.Grouped {
Rohan Agrawalbca69122020-06-17 14:59:03 +00001371 groups := make(map[string]struct{})
1372 for _, group := range pmConfigs.Groups {
1373 groups[group.GroupName] = struct{}{}
1374 }
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001375 for _, group := range pmConfigs.Groups {
Girish Gowdra610acb42021-01-27 13:33:57 -08001376 if _, have := groups[string(options.Args.Group)]; !have {
1377 return fmt.Errorf("Group Name '%s' does not exist", options.Args.Group)
1378 }
1379 if string(options.Args.Group) == group.GroupName && !group.Enabled {
1380 group.Enabled = true
1381 _, err := client.UpdateDevicePmConfigs(ctx, pmConfigs)
1382 if err != nil {
1383 return err
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001384 }
1385 }
1386 }
Rohan Agrawalbca69122020-06-17 14:59:03 +00001387 } else {
1388 return fmt.Errorf("Device '%s' does not have Group Metrics", options.Args.Id)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001389 }
1390 return nil
1391}
1392
1393func (options *DevicePmConfigGroupDisable) Execute(args []string) error {
1394
1395 conn, err := NewConnection()
1396 if err != nil {
1397 return err
1398 }
1399 defer conn.Close()
1400
1401 client := voltha.NewVolthaServiceClient(conn)
1402
David K. Bainbridge9189c632021-03-26 21:52:21 +00001403 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001404 defer cancel()
1405
1406 id := voltha.ID{Id: string(options.Args.Id)}
1407
1408 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
1409 if err != nil {
1410 return err
1411 }
1412
1413 if pmConfigs.Grouped {
Rohan Agrawalbca69122020-06-17 14:59:03 +00001414 groups := make(map[string]struct{})
1415 for _, group := range pmConfigs.Groups {
1416 groups[group.GroupName] = struct{}{}
1417 }
1418
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001419 for _, group := range pmConfigs.Groups {
Girish Gowdra610acb42021-01-27 13:33:57 -08001420 if _, have := groups[string(options.Args.Group)]; !have {
1421 return fmt.Errorf("Group Name '%s' does not exist", options.Args.Group)
1422 }
Rohan Agrawalbca69122020-06-17 14:59:03 +00001423
Girish Gowdra610acb42021-01-27 13:33:57 -08001424 if string(options.Args.Group) == group.GroupName && group.Enabled {
1425 group.Enabled = false
1426 _, err := client.UpdateDevicePmConfigs(ctx, pmConfigs)
1427 if err != nil {
1428 return err
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001429 }
1430 }
1431 }
Rohan Agrawalbca69122020-06-17 14:59:03 +00001432 } else {
1433 return fmt.Errorf("Device '%s' does not have Group Metrics", options.Args.Id)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001434 }
1435 return nil
1436}
1437
Girish Gowdra610acb42021-01-27 13:33:57 -08001438func (options *DevicePmConfigGroupFrequencySet) Execute(args []string) error {
1439
1440 conn, err := NewConnection()
1441 if err != nil {
1442 return err
1443 }
1444 defer conn.Close()
1445
1446 client := voltha.NewVolthaServiceClient(conn)
1447
David K. Bainbridge9189c632021-03-26 21:52:21 +00001448 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Girish Gowdra610acb42021-01-27 13:33:57 -08001449 defer cancel()
1450
1451 id := voltha.ID{Id: string(options.Args.Id)}
1452
1453 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
1454 if err != nil {
1455 return err
1456 }
1457
1458 if pmConfigs.Grouped {
1459 groups := make(map[string]struct{})
1460 for _, group := range pmConfigs.Groups {
1461 groups[group.GroupName] = struct{}{}
1462 }
1463
1464 for _, group := range pmConfigs.Groups {
1465 if _, have := groups[string(options.Args.Group)]; !have {
1466 return fmt.Errorf("group name '%s' does not exist", options.Args.Group)
1467 }
1468
1469 if string(options.Args.Group) == group.GroupName {
1470 if !group.Enabled {
1471 return fmt.Errorf("group '%s' is not enabled", options.Args.Group)
1472 }
1473 group.GroupFreq = uint32(options.Args.Interval.Seconds())
1474 _, err = client.UpdateDevicePmConfigs(ctx, pmConfigs)
1475 if err != nil {
1476 return err
1477 }
1478 }
1479 }
1480 } else {
1481 return fmt.Errorf("device '%s' does not have group metrics", options.Args.Id)
1482 }
1483 return nil
1484}
1485
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001486func (options *DevicePmConfigGroupList) Execute(args []string) error {
1487
1488 conn, err := NewConnection()
1489 if err != nil {
1490 return err
1491 }
1492 defer conn.Close()
1493
1494 client := voltha.NewVolthaServiceClient(conn)
1495
David K. Bainbridge9189c632021-03-26 21:52:21 +00001496 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001497 defer cancel()
1498
1499 id := voltha.ID{Id: string(options.Args.Id)}
1500
1501 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
1502 if err != nil {
1503 return err
1504 }
1505
1506 if pmConfigs.Grouped {
1507 for _, group := range pmConfigs.Groups {
1508 if group.GroupFreq == 0 {
1509 group.GroupFreq = pmConfigs.DefaultFreq
1510 }
1511 }
Rohan Agrawalbca69122020-06-17 14:59:03 +00001512 outputFormat := CharReplacer.Replace(options.Format)
1513 if outputFormat == "" {
1514 outputFormat = GetCommandOptionWithDefault("device-pm-configs", "format", DEFAULT_DEVICE_PM_CONFIG_GROUP_LIST_FORMAT)
1515 }
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001516
Rohan Agrawalbca69122020-06-17 14:59:03 +00001517 orderBy := options.OrderBy
1518 if orderBy == "" {
1519 orderBy = GetCommandOptionWithDefault("device-pm-configs", "order", "")
1520 }
1521
1522 result := CommandResult{
1523 Format: format.Format(outputFormat),
1524 Filter: options.Filter,
1525 OrderBy: orderBy,
1526 OutputAs: toOutputType(options.OutputAs),
1527 NameLimit: options.NameLimit,
1528 Data: pmConfigs.Groups,
1529 }
1530
1531 GenerateOutput(&result)
1532 } else {
1533 return fmt.Errorf("Device '%s' does not have Group Metrics", string(options.Args.Id))
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001534 }
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001535 return nil
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001536}
1537
1538func (options *DevicePmConfigGroupMetricList) Execute(args []string) error {
1539
1540 var metrics []*voltha.PmConfig
1541 conn, err := NewConnection()
1542 if err != nil {
1543 return err
1544 }
1545 defer conn.Close()
1546
1547 client := voltha.NewVolthaServiceClient(conn)
1548
David K. Bainbridge9189c632021-03-26 21:52:21 +00001549 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001550 defer cancel()
1551
1552 id := voltha.ID{Id: string(options.Args.Id)}
1553
1554 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
1555 if err != nil {
1556 return err
1557 }
1558
1559 for _, groups := range pmConfigs.Groups {
1560
1561 if string(options.Args.Group) == groups.GroupName {
1562 for _, metric := range groups.Metrics {
1563 if metric.SampleFreq == 0 && groups.GroupFreq == 0 {
1564 metric.SampleFreq = pmConfigs.DefaultFreq
1565 } else {
1566 metric.SampleFreq = groups.GroupFreq
1567 }
1568 }
1569 metrics = groups.Metrics
1570 }
1571 }
1572
1573 outputFormat := CharReplacer.Replace(options.Format)
1574 if outputFormat == "" {
1575 outputFormat = GetCommandOptionWithDefault("device-pm-configs", "format", DEFAULT_DEVICE_PM_CONFIG_METRIC_LIST_FORMAT)
1576 }
1577
1578 orderBy := options.OrderBy
1579 if orderBy == "" {
1580 orderBy = GetCommandOptionWithDefault("device-pm-configs", "order", "")
1581 }
1582
1583 result := CommandResult{
1584 Format: format.Format(outputFormat),
1585 Filter: options.Filter,
1586 OrderBy: orderBy,
1587 OutputAs: toOutputType(options.OutputAs),
1588 NameLimit: options.NameLimit,
1589 Data: metrics,
1590 }
1591
1592 GenerateOutput(&result)
1593 return nil
1594
1595}
1596
1597func (options *DevicePmConfigFrequencySet) Execute(args []string) error {
1598
1599 conn, err := NewConnection()
1600 if err != nil {
1601 return err
1602 }
1603 defer conn.Close()
1604
1605 client := voltha.NewVolthaServiceClient(conn)
1606
David K. Bainbridge9189c632021-03-26 21:52:21 +00001607 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001608 defer cancel()
1609
1610 id := voltha.ID{Id: string(options.Args.Id)}
1611
1612 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
1613 if err != nil {
1614 return err
1615 }
1616
Girish Gowdra610acb42021-01-27 13:33:57 -08001617 pmConfigs.DefaultFreq = uint32(options.Args.Interval.Seconds())
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001618
1619 _, err = client.UpdateDevicePmConfigs(ctx, pmConfigs)
1620 if err != nil {
1621 return err
1622 }
1623
1624 outputFormat := CharReplacer.Replace(options.Format)
1625 if outputFormat == "" {
1626 outputFormat = GetCommandOptionWithDefault("device-pm-configs", "format", DEFAULT_DEVICE_PM_CONFIG_GET_FORMAT)
1627 }
1628 if options.Quiet {
1629 outputFormat = "{{.Id}}"
1630 }
1631
1632 result := CommandResult{
1633 Format: format.Format(outputFormat),
1634 OutputAs: toOutputType(options.OutputAs),
1635 NameLimit: options.NameLimit,
1636 Data: pmConfigs,
1637 }
1638
1639 GenerateOutput(&result)
1640 return nil
1641
1642}
1643
kesavand3e2f9f62021-04-22 11:06:38 +05301644func (options *OnuDownloadImage) Execute(args []string) error {
1645
1646 conn, err := NewConnection()
1647 if err != nil {
1648 return err
1649 }
1650 defer conn.Close()
1651
1652 client := voltha.NewVolthaServiceClient(conn)
1653
1654 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
1655 defer cancel()
1656
1657 var devIDList []*common.ID
1658 for _, i := range options.Args.IDs {
1659
1660 devIDList = append(devIDList, &common.ID{Id: string(i)})
1661 }
1662
1663 downloadImage := voltha.DeviceImageDownloadRequest{
1664 DeviceId: devIDList,
1665 Image: &voltha.Image{
1666 Url: options.Args.Url,
1667 Crc32: options.Args.Crc,
ssiddiqui7bc89e92021-05-20 20:58:02 +05301668 Vendor: options.Args.Vendor,
kesavand3e2f9f62021-04-22 11:06:38 +05301669 Version: options.Args.ImageVersion,
1670 },
1671 ActivateOnSuccess: options.Args.ActivateOnSuccess,
1672 CommitOnSuccess: options.Args.CommitOnSuccess,
1673 }
1674
Andrea Campanellaeaf1e0c2021-06-07 14:41:34 +02001675 deviceImageResp, err := client.DownloadImageToDevice(ctx, &downloadImage)
kesavand3e2f9f62021-04-22 11:06:38 +05301676 if err != nil {
1677 return err
1678 }
1679
Andrea Campanellaeaf1e0c2021-06-07 14:41:34 +02001680 outputFormat := GetCommandOptionWithDefault("onu-image-download", "format", ONU_IMAGE_STATUS_FORMAT)
1681 // Make sure json output prints an empty list, not "null"
1682 if deviceImageResp.DeviceImageStates == nil {
1683 deviceImageResp.DeviceImageStates = make([]*voltha.DeviceImageState, 0)
1684 }
1685 result := CommandResult{
1686 Format: format.Format(outputFormat),
1687 OutputAs: toOutputType(options.OutputAs),
1688 NameLimit: options.NameLimit,
1689 Data: deviceImageResp.DeviceImageStates,
1690 }
1691 GenerateOutput(&result)
kesavand3e2f9f62021-04-22 11:06:38 +05301692 return nil
1693
1694}
1695
1696func (options *OnuActivateImage) Execute(args []string) error {
1697
1698 conn, err := NewConnection()
1699 if err != nil {
1700 return err
1701 }
1702 defer conn.Close()
1703
1704 client := voltha.NewVolthaServiceClient(conn)
1705
1706 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
1707 defer cancel()
1708
1709 var devIDList []*common.ID
1710 for _, i := range options.Args.IDs {
1711
1712 devIDList = append(devIDList, &common.ID{Id: string(i)})
1713 }
1714
1715 downloadImage := voltha.DeviceImageRequest{
1716 DeviceId: devIDList,
1717 Version: options.Args.ImageVersion,
1718 CommitOnSuccess: options.Args.CommitOnSuccess,
1719 }
1720
Andrea Campanellaeaf1e0c2021-06-07 14:41:34 +02001721 deviceImageResp, err := client.ActivateImage(ctx, &downloadImage)
kesavand3e2f9f62021-04-22 11:06:38 +05301722 if err != nil {
1723 return err
1724 }
1725
Andrea Campanellaeaf1e0c2021-06-07 14:41:34 +02001726 outputFormat := GetCommandOptionWithDefault("onu-image-activate", "format", ONU_IMAGE_STATUS_FORMAT)
1727 // Make sure json output prints an empty list, not "null"
1728 if deviceImageResp.DeviceImageStates == nil {
1729 deviceImageResp.DeviceImageStates = make([]*voltha.DeviceImageState, 0)
1730 }
1731 result := CommandResult{
1732 Format: format.Format(outputFormat),
1733 OutputAs: toOutputType(options.OutputAs),
1734 NameLimit: options.NameLimit,
1735 Data: deviceImageResp.DeviceImageStates,
1736 }
1737 GenerateOutput(&result)
1738
kesavand3e2f9f62021-04-22 11:06:38 +05301739 return nil
1740
1741}
1742
1743func (options *OnuAbortUpgradeImage) Execute(args []string) error {
1744
1745 conn, err := NewConnection()
1746 if err != nil {
1747 return err
1748 }
1749 defer conn.Close()
1750
1751 client := voltha.NewVolthaServiceClient(conn)
1752
1753 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
1754 defer cancel()
1755
1756 var devIDList []*common.ID
1757 for _, i := range options.Args.IDs {
1758
1759 devIDList = append(devIDList, &common.ID{Id: string(i)})
1760 }
1761
1762 downloadImage := voltha.DeviceImageRequest{
1763 DeviceId: devIDList,
1764 Version: options.Args.ImageVersion,
1765 }
1766
Andrea Campanellaeaf1e0c2021-06-07 14:41:34 +02001767 deviceImageResp, err := client.AbortImageUpgradeToDevice(ctx, &downloadImage)
kesavand3e2f9f62021-04-22 11:06:38 +05301768 if err != nil {
1769 return err
1770 }
1771
Andrea Campanellaeaf1e0c2021-06-07 14:41:34 +02001772 outputFormat := GetCommandOptionWithDefault("onu-image-abort", "format", ONU_IMAGE_STATUS_FORMAT)
1773 // Make sure json output prints an empty list, not "null"
1774 if deviceImageResp.DeviceImageStates == nil {
1775 deviceImageResp.DeviceImageStates = make([]*voltha.DeviceImageState, 0)
1776 }
1777 result := CommandResult{
1778 Format: format.Format(outputFormat),
1779 OutputAs: toOutputType(options.OutputAs),
1780 NameLimit: options.NameLimit,
1781 Data: deviceImageResp.DeviceImageStates,
1782 }
1783 GenerateOutput(&result)
1784
kesavand3e2f9f62021-04-22 11:06:38 +05301785 return nil
1786
1787}
1788
1789func (options *OnuCommitImage) Execute(args []string) error {
1790
1791 conn, err := NewConnection()
1792 if err != nil {
1793 return err
1794 }
1795 defer conn.Close()
1796
1797 client := voltha.NewVolthaServiceClient(conn)
1798
1799 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
1800 defer cancel()
1801
1802 var devIDList []*common.ID
1803 for _, i := range options.Args.IDs {
1804
1805 devIDList = append(devIDList, &common.ID{Id: string(i)})
1806 }
1807 downloadImage := voltha.DeviceImageRequest{
1808 DeviceId: devIDList,
1809 Version: options.Args.ImageVersion,
1810 }
1811
Andrea Campanellaeaf1e0c2021-06-07 14:41:34 +02001812 deviceImageResp, err := client.CommitImage(ctx, &downloadImage)
kesavand3e2f9f62021-04-22 11:06:38 +05301813 if err != nil {
1814 return err
1815 }
1816
Andrea Campanellaeaf1e0c2021-06-07 14:41:34 +02001817 outputFormat := GetCommandOptionWithDefault("onu-image-commit", "format", ONU_IMAGE_STATUS_FORMAT)
1818 // Make sure json output prints an empty list, not "null"
1819 if deviceImageResp.DeviceImageStates == nil {
1820 deviceImageResp.DeviceImageStates = make([]*voltha.DeviceImageState, 0)
1821 }
1822 result := CommandResult{
1823 Format: format.Format(outputFormat),
1824 OutputAs: toOutputType(options.OutputAs),
1825 NameLimit: options.NameLimit,
1826 Data: deviceImageResp.DeviceImageStates,
1827 }
1828 GenerateOutput(&result)
1829
kesavand3e2f9f62021-04-22 11:06:38 +05301830 return nil
1831
1832}
1833
1834func (options *OnuListImages) Execute(args []string) error {
1835
1836 conn, err := NewConnection()
1837 if err != nil {
1838 return err
1839 }
1840 defer conn.Close()
1841
1842 client := voltha.NewVolthaServiceClient(conn)
1843
1844 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
1845 defer cancel()
1846
1847 id := common.ID{Id: string(options.Args.Id)}
1848
1849 onuImages, err := client.GetOnuImages(ctx, &id)
1850 if err != nil {
1851 return err
1852 }
1853
1854 outputFormat := CharReplacer.Replace(options.Format)
1855 if outputFormat == "" {
1856 outputFormat = GetCommandOptionWithDefault("onu-image-list", "format", ONU_IMAGE_LIST_FORMAT)
1857 }
1858
1859 if options.Quiet {
1860 outputFormat = "{{.Id}}"
1861 }
1862
1863 //TODO orderby
1864
1865 // Make sure json output prints an empty list, not "null"
1866 if onuImages.Items == nil {
1867 onuImages.Items = make([]*voltha.OnuImage, 0)
1868 }
1869
1870 result := CommandResult{
1871 Format: format.Format(outputFormat),
1872 OutputAs: toOutputType(options.OutputAs),
1873 NameLimit: options.NameLimit,
1874 Data: onuImages.Items,
1875 }
1876
1877 GenerateOutput(&result)
1878 return nil
1879
1880}
1881
1882func (options *OnuImageStatus) Execute(args []string) error {
1883
1884 conn, err := NewConnection()
1885 if err != nil {
1886 return err
1887 }
1888 defer conn.Close()
1889
1890 client := voltha.NewVolthaServiceClient(conn)
1891
1892 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
1893 defer cancel()
1894
1895 var devIDList []*common.ID
kesavand3e2f9f62021-04-22 11:06:38 +05301896
Elia Battiston859f3e62022-02-08 15:57:52 +01001897 if options.Args.IDs == nil {
1898 //Use an empty IDs list to retrieve the status of all devices
1899 //with the requested image version
1900 devIDList = []*common.ID{}
1901 } else {
1902 for _, i := range options.Args.IDs {
1903 devIDList = append(devIDList, &common.ID{Id: string(i)})
1904 }
kesavand3e2f9f62021-04-22 11:06:38 +05301905 }
1906
1907 imageStatusReq := voltha.DeviceImageRequest{
1908 DeviceId: devIDList,
1909 Version: options.Args.ImageVersion,
1910 }
1911 imageStatus, err := client.GetImageStatus(ctx, &imageStatusReq)
1912 if err != nil {
1913 return err
1914 }
1915
1916 outputFormat := CharReplacer.Replace(options.Format)
1917 if outputFormat == "" {
1918 outputFormat = GetCommandOptionWithDefault("device-image-list", "format", ONU_IMAGE_STATUS_FORMAT)
1919 }
1920
1921 if options.Quiet {
1922 outputFormat = "{{.Id}}"
1923 }
1924
1925 //TODO orderby
1926
1927 // Make sure json output prints an empty list, not "null"
1928 if imageStatus.DeviceImageStates == nil {
1929 imageStatus.DeviceImageStates = make([]*voltha.DeviceImageState, 0)
1930 }
1931
1932 result := CommandResult{
1933 Format: format.Format(outputFormat),
1934 OutputAs: toOutputType(options.OutputAs),
1935 NameLimit: options.NameLimit,
1936 Data: imageStatus.DeviceImageStates,
1937 }
1938
1939 GenerateOutput(&result)
1940 return nil
1941
1942}
1943
Andrea Campanella791d88b2021-01-08 13:29:00 +01001944func (options *DeviceOnuListImages) Execute(args []string) error {
1945
1946 conn, err := NewConnection()
1947 if err != nil {
1948 return err
1949 }
1950 defer conn.Close()
1951
1952 client := voltha.NewVolthaServiceClient(conn)
1953
David K. Bainbridge9189c632021-03-26 21:52:21 +00001954 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Andrea Campanella791d88b2021-01-08 13:29:00 +01001955 defer cancel()
1956
1957 id := common.ID{Id: string(options.Args.Id)}
1958
1959 imageDownloads, err := client.ListImageDownloads(ctx, &id)
1960 if err != nil {
1961 return err
1962 }
1963
1964 outputFormat := CharReplacer.Replace(options.Format)
1965 if outputFormat == "" {
1966 outputFormat = GetCommandOptionWithDefault("device-image-list", "format", DEFAULT_DEVICE_IMAGE_LIST_GET_FORMAT)
1967 }
1968
1969 if options.Quiet {
1970 outputFormat = "{{.Id}}"
1971 }
1972
1973 //TODO orderby
1974
1975 // Make sure json output prints an empty list, not "null"
1976 if imageDownloads.Items == nil {
1977 imageDownloads.Items = make([]*voltha.ImageDownload, 0)
1978 }
1979
1980 result := CommandResult{
1981 Format: format.Format(outputFormat),
1982 OutputAs: toOutputType(options.OutputAs),
1983 NameLimit: options.NameLimit,
1984 Data: imageDownloads.Items,
1985 }
1986
1987 GenerateOutput(&result)
1988 return nil
1989
1990}
1991
1992func (options *DeviceOnuDownloadImage) Execute(args []string) error {
1993
1994 conn, err := NewConnection()
1995 if err != nil {
1996 return err
1997 }
1998 defer conn.Close()
1999
2000 client := voltha.NewVolthaServiceClient(conn)
2001
David K. Bainbridge9189c632021-03-26 21:52:21 +00002002 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Andrea Campanella791d88b2021-01-08 13:29:00 +01002003 defer cancel()
2004
2005 downloadImage := voltha.ImageDownload{
2006 Id: string(options.Args.Id),
2007 Name: options.Args.Name,
2008 Url: options.Args.Url,
2009 Crc: options.Args.Crc,
2010 LocalDir: options.Args.LocalDir,
2011 }
2012
2013 _, err = client.DownloadImage(ctx, &downloadImage)
2014 if err != nil {
2015 return err
2016 }
2017
2018 return nil
2019
2020}
2021
2022func (options *DeviceOnuActivateImageUpdate) Execute(args []string) error {
2023
2024 conn, err := NewConnection()
2025 if err != nil {
2026 return err
2027 }
2028 defer conn.Close()
2029
2030 client := voltha.NewVolthaServiceClient(conn)
2031
David K. Bainbridge9189c632021-03-26 21:52:21 +00002032 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Andrea Campanella791d88b2021-01-08 13:29:00 +01002033 defer cancel()
2034
2035 downloadImage := voltha.ImageDownload{
2036 Id: string(options.Args.Id),
2037 Name: options.Args.Name,
2038 ImageVersion: options.Args.ImageVersion,
2039 SaveConfig: options.Args.SaveConfig,
2040 LocalDir: options.Args.LocalDir,
2041 }
2042
2043 _, err = client.ActivateImageUpdate(ctx, &downloadImage)
2044 if err != nil {
2045 return err
2046 }
2047
2048 return nil
2049
2050}
2051
Scott Baker9173ed82020-05-19 08:30:12 -07002052type ReturnValueRow struct {
2053 Name string `json:"name"`
2054 Result interface{} `json:"result"`
2055}
2056
kesavand8ec4fc02021-01-27 09:10:22 -05002057func (options *DeviceGetPortStats) Execute(args []string) error {
2058 conn, err := NewConnection()
2059 if err != nil {
2060 return err
2061 }
2062 defer conn.Close()
2063 client := extension.NewExtensionClient(conn)
2064 var portType extension.GetOltPortCounters_PortType
2065
2066 if options.Args.PortType == "pon" {
2067 portType = extension.GetOltPortCounters_Port_PON_OLT
2068 } else if options.Args.PortType == "nni" {
2069
2070 portType = extension.GetOltPortCounters_Port_ETHERNET_NNI
2071 } else {
2072 return fmt.Errorf("expected interface type pon/nni, provided %s", options.Args.PortType)
2073 }
2074
2075 singleGetValReq := extension.SingleGetValueRequest{
2076 TargetId: string(options.Args.Id),
2077 Request: &extension.GetValueRequest{
2078 Request: &extension.GetValueRequest_OltPortInfo{
2079 OltPortInfo: &extension.GetOltPortCounters{
2080 PortNo: options.Args.PortNo,
2081 PortType: portType,
2082 },
2083 },
2084 },
2085 }
2086
David K. Bainbridge9189c632021-03-26 21:52:21 +00002087 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
kesavand8ec4fc02021-01-27 09:10:22 -05002088 defer cancel()
2089 rv, err := client.GetExtValue(ctx, &singleGetValReq)
2090 if err != nil {
2091 Error.Printf("Error getting value on device Id %s,err=%s\n", options.Args.Id, ErrorToString(err))
2092 return err
2093 }
2094
2095 if rv.Response.Status != extension.GetValueResponse_OK {
2096 return fmt.Errorf("failed to get port stats %v", rv.Response.ErrReason.String())
2097 }
2098
2099 outputFormat := CharReplacer.Replace(options.Format)
2100 if outputFormat == "" {
2101 outputFormat = GetCommandOptionWithDefault("device-get-port-status", "format", DEFAULT_DEVICE_GET_PORT_STATUS_FORMAT)
2102 }
2103
2104 result := CommandResult{
2105 Format: format.Format(outputFormat),
2106 OutputAs: toOutputType(options.OutputAs),
2107 NameLimit: options.NameLimit,
2108 Data: rv.GetResponse().GetPortCoutners(),
2109 }
2110 GenerateOutput(&result)
2111 return nil
2112}
2113
Himani Chawla40acc122021-05-26 18:52:29 +05302114func (options *GetOnuStats) Execute(args []string) error {
2115 conn, err := NewConnection()
2116 if err != nil {
2117 return err
2118 }
2119 defer conn.Close()
2120 client := extension.NewExtensionClient(conn)
2121
2122 singleGetValReq := extension.SingleGetValueRequest{
2123 TargetId: string(options.Args.OltId),
2124 Request: &extension.GetValueRequest{
2125 Request: &extension.GetValueRequest_OnuPonInfo{
2126 OnuPonInfo: &extension.GetOnuCountersRequest{
2127 IntfId: options.Args.IntfId,
2128 OnuId: options.Args.OnuId,
2129 },
2130 },
2131 },
2132 }
2133 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
2134 defer cancel()
2135 rv, err := client.GetExtValue(ctx, &singleGetValReq)
2136 if err != nil {
2137 Error.Printf("Error getting value on device Id %s,err=%s\n", options.Args.OltId, ErrorToString(err))
2138 return err
2139 }
2140
2141 if rv.Response.Status != extension.GetValueResponse_OK {
2142 return fmt.Errorf("failed to get onu stats %v", rv.Response.ErrReason.String())
2143 }
2144 outputFormat := CharReplacer.Replace(options.Format)
2145 data, formatStr := buildOnuStatsOutputFormat(rv.GetResponse().GetOnuPonCounters())
2146 if outputFormat == "" {
2147 outputFormat = GetCommandOptionWithDefault("device-get-onu-status", "format", formatStr)
2148 }
Himani Chawla553a1392021-06-10 23:39:17 +05302149 result := CommandResult{
2150 Format: format.Format(outputFormat),
2151 OutputAs: toOutputType(options.OutputAs),
2152 NameLimit: options.NameLimit,
2153 Data: data,
2154 }
2155 GenerateOutput(&result)
2156 return nil
2157}
Himani Chawla40acc122021-05-26 18:52:29 +05302158
Akash Soni6e879c22024-12-20 17:01:34 +05302159func (options *GetOffloadApp) Execute(args []string) error {
Akash Soni51b6b7a2024-11-20 11:39:38 +05302160 // Establish a connection to the gRPC server
2161 conn, err := NewConnection()
2162 if err != nil {
2163 return err
2164 }
2165 defer conn.Close()
2166
2167 client := extension.NewExtensionClient(conn)
2168
2169 // Build the request
2170 singleGetValReq := &extension.SingleGetValueRequest{
2171 TargetId: string(options.Args.OltId),
2172 Request: &extension.GetValueRequest{
2173 Request: &extension.GetValueRequest_OffloadedAppsStats{
2174 OffloadedAppsStats: &extension.GetOffloadedAppsStatisticsRequest{
2175 StatsFor: options.Args.StatsFor,
2176 },
2177 },
2178 },
2179 }
2180
2181 // Set a context with timeout
2182 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
2183 defer cancel()
2184
2185 // Perform the gRPC call
2186 rv, err := client.GetExtValue(ctx, singleGetValReq)
2187 if err != nil {
2188 Error.Printf("Error getting value for device ID %s, err=%s\n", options.Args.OltId, ErrorToString(err))
2189 return err
2190 }
2191
2192 // Check response status
2193 if rv.GetResponse().GetStatus() != extension.GetValueResponse_OK {
2194 return fmt.Errorf("failed to get offloaded app stats: %v", rv.GetResponse().GetErrReason().String())
2195 }
2196
2197 // Process the response data
2198 stats, formatStr := buildOffloadAppStatsOutputFormat(rv.GetResponse().GetOffloadedAppsStats())
2199 outputFormat := CharReplacer.Replace(options.Format)
2200 if outputFormat == "" {
2201 outputFormat = GetCommandOptionWithDefault("device-get-offload-app-stats", "format", formatStr)
2202 }
2203
2204 // Generate and display the output
2205 result := CommandResult{
2206 Format: format.Format(outputFormat),
2207 OutputAs: toOutputType(options.OutputAs),
2208 NameLimit: options.NameLimit,
2209 Data: stats,
2210 }
2211 GenerateOutput(&result)
2212
2213 return nil
2214}
2215
Akash Soni6e879c22024-12-20 17:01:34 +05302216func (options *SetOffloadApp) Execute(args []string) error {
Akash Soni51b6b7a2024-11-20 11:39:38 +05302217 conn, err := NewConnection()
2218 if err != nil {
Akash Soni6e879c22024-12-20 17:01:34 +05302219 return fmt.Errorf("failed to establish gRPC connection: %w", err)
Akash Soni51b6b7a2024-11-20 11:39:38 +05302220 }
2221 defer conn.Close()
2222
2223 client := extension.NewExtensionClient(conn)
2224
Akash Soni6e879c22024-12-20 17:01:34 +05302225 // Parse JSON input
2226 var config extension.AppOffloadConfig
2227 if err := json.Unmarshal([]byte(options.Args.Config), &config); err != nil {
2228 return fmt.Errorf("failed to parse CONFIG as JSON: %w", err)
2229 }
2230
Akash Soni51b6b7a2024-11-20 11:39:38 +05302231 setValueRequest := &extension.SetValueRequest{
2232 Request: &extension.SetValueRequest_AppOffloadConfig{
Akash Soni6e879c22024-12-20 17:01:34 +05302233 AppOffloadConfig: &config,
Akash Soni51b6b7a2024-11-20 11:39:38 +05302234 },
2235 }
2236
2237 singleSetValReq := &extension.SingleSetValueRequest{
2238 TargetId: string(options.Args.OltId),
2239 Request: setValueRequest,
2240 }
2241
Akash Soni6e879c22024-12-20 17:01:34 +05302242 // Log the request object
2243 logRequestAppOffloadConfig(singleSetValReq)
2244
Akash Soni51b6b7a2024-11-20 11:39:38 +05302245 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
2246 defer cancel()
2247
2248 resp, err := client.SetExtValue(ctx, singleSetValReq)
2249 if err != nil {
Akash Soni6e879c22024-12-20 17:01:34 +05302250 return fmt.Errorf("failed to set AppOffloadConfig: %w", err)
Akash Soni51b6b7a2024-11-20 11:39:38 +05302251 }
2252
2253 if resp.Response.Status != extension.SetValueResponse_OK {
Akash Soni6e879c22024-12-20 17:01:34 +05302254 return fmt.Errorf("operation failed with status %v: %s", resp.Response.Status, resp.Response.ErrReason)
Akash Soni51b6b7a2024-11-20 11:39:38 +05302255 }
2256
2257 fmt.Printf("AppOffloadConfig successfully set for OLT ID: %s\n", options.Args.OltId)
2258 return nil
2259}
2260
Akash Soni6e879c22024-12-20 17:01:34 +05302261func logRequestAppOffloadConfig(req *extension.SingleSetValueRequest) {
2262 fmt.Printf("Request details:\n")
2263 fmt.Printf("TargetId: %s\n", req.TargetId)
2264 if config, ok := req.Request.Request.(*extension.SetValueRequest_AppOffloadConfig); ok {
2265 fmt.Printf("AppOffloadConfig:\n")
2266 fmt.Printf(" EnableDHCPv4RA: %t\n", config.AppOffloadConfig.EnableDHCPv4RA)
2267 fmt.Printf(" EnableDHCPv6RA: %t\n", config.AppOffloadConfig.EnableDHCPv6RA)
2268 fmt.Printf(" EnablePPPoEIA: %t\n", config.AppOffloadConfig.EnablePPPoEIA)
2269 fmt.Printf(" AccessNodeID: %s\n", config.AppOffloadConfig.AccessNodeID)
2270 }
2271}
2272
2273func (options *SetOnuOffload) Execute(args []string) error {
2274 // Create the gRPC client connection
Akash Soni51b6b7a2024-11-20 11:39:38 +05302275 conn, err := NewConnection()
2276 if err != nil {
2277 return fmt.Errorf("failed to establish gRPC connection: %v", err)
2278 }
2279 defer conn.Close()
2280
2281 client := extension.NewExtensionClient(conn)
2282
Akash Soni6e879c22024-12-20 17:01:34 +05302283 // Parse PerUniInfo into a slice of PerUniConfig
2284 var perUniConfigs []AppOffloadOnuConfig
2285 if err := json.Unmarshal([]byte(options.Args.PerUniInfo), &perUniConfigs); err != nil {
2286 return fmt.Errorf("failed to parse PerUniInfo as JSON: %v", err)
Akash Soni51b6b7a2024-11-20 11:39:38 +05302287 }
Akash Soni6e879c22024-12-20 17:01:34 +05302288
2289 // Convert to []*AppOffloadOnuConfig_PerUniConfig for gRPC
2290 var grpcPerUniInfo []*extension.AppOffloadOnuConfig_PerUniConfig
2291 for _, config := range perUniConfigs {
2292 grpcPerUniInfo = append(grpcPerUniInfo, &extension.AppOffloadOnuConfig_PerUniConfig{
2293 AgentRemoteID: config.AgentRemoteID,
2294 AgentCircuitID: config.AgentCircuitID,
2295 OnuUniId: config.OnuUniId,
2296 })
2297 }
2298
Akash Soni51b6b7a2024-11-20 11:39:38 +05302299 // Build the AppOffloadOnuConfig request
2300 onuConfig := &extension.AppOffloadOnuConfig{
2301 OnuDeviceId: options.Args.OnuDeviceId,
Akash Soni6e879c22024-12-20 17:01:34 +05302302 PerUniInfo: grpcPerUniInfo,
Akash Soni51b6b7a2024-11-20 11:39:38 +05302303 }
2304
2305 setValueRequest := &extension.SetValueRequest{
2306 Request: &extension.SetValueRequest_AppOffloadOnuConfig{
2307 AppOffloadOnuConfig: onuConfig,
2308 },
2309 }
2310
2311 singleSetValReq := &extension.SingleSetValueRequest{
Akash Soni6e879c22024-12-20 17:01:34 +05302312 TargetId: string(options.Args.OltId),
Akash Soni51b6b7a2024-11-20 11:39:38 +05302313 Request: setValueRequest,
2314 }
2315
Akash Soni6e879c22024-12-20 17:01:34 +05302316 // Log the request object
2317 logRequestAppOffloadOnuConfig(singleSetValReq)
2318
2319 // Make the gRPC call
Akash Soni51b6b7a2024-11-20 11:39:38 +05302320 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
2321 defer cancel()
2322
2323 resp, err := client.SetExtValue(ctx, singleSetValReq)
2324 if err != nil {
2325 return fmt.Errorf("failed to set AppOffloadOnuConfig: %v", err)
2326 }
2327
2328 if resp.Response.Status != extension.SetValueResponse_OK {
Akash Soni6e879c22024-12-20 17:01:34 +05302329 return fmt.Errorf("operation failed with status %v: %s", resp.Response.Status, resp.Response.ErrReason)
Akash Soni51b6b7a2024-11-20 11:39:38 +05302330 }
2331
2332 fmt.Printf("AppOffloadOnuConfig successfully set for ONU ID: %s\n", options.Args.OnuDeviceId)
2333 return nil
2334}
2335
Akash Soni6e879c22024-12-20 17:01:34 +05302336// Debugging helper to log the gRPC request details
2337func logRequestAppOffloadOnuConfig(req *extension.SingleSetValueRequest) {
2338 fmt.Printf("Request details:\n")
2339 fmt.Printf("TargetId: %s\n", req.TargetId)
2340 fmt.Printf("OnuDeviceId: %s\n", req.Request.GetAppOffloadOnuConfig().OnuDeviceId)
2341 if config, ok := req.Request.Request.(*extension.SetValueRequest_AppOffloadOnuConfig); ok {
2342 fmt.Printf("AppOffloadOnuConfig:\n")
2343 for i, uniInfo := range config.AppOffloadOnuConfig.PerUniInfo {
2344 fmt.Printf(" UniInfo %d:\n", i+1)
2345 fmt.Printf(" AgentRemoteID: %s\n", uniInfo.AgentRemoteID)
2346 fmt.Printf(" AgentCircuitID: %s\n", uniInfo.AgentCircuitID)
2347 fmt.Printf(" OnuUniId: %d\n", uniInfo.OnuUniId)
2348 }
2349 }
2350}
2351
Himani Chawla553a1392021-06-10 23:39:17 +05302352func (options *GetOnuEthernetFrameExtendedPmCounters) Execute(args []string) error {
2353 conn, err := NewConnection()
2354 if err != nil {
2355 return err
2356 }
2357 defer conn.Close()
2358 client := extension.NewExtensionClient(conn)
Himani Chawla806aa892021-08-30 15:51:46 +05302359 var singleGetValReq extension.SingleGetValueRequest
Himani Chawla553a1392021-06-10 23:39:17 +05302360
Himani Chawla806aa892021-08-30 15:51:46 +05302361 if options.Args.UniIndex != nil {
2362 singleGetValReq = extension.SingleGetValueRequest{
2363 TargetId: string(options.Args.Id),
2364 Request: &extension.GetValueRequest{
2365 Request: &extension.GetValueRequest_OnuInfo{
2366 OnuInfo: &extension.GetOmciEthernetFrameExtendedPmRequest{
2367 OnuDeviceId: string(options.Args.Id),
2368 Reset_: options.Reset,
2369 IsUniIndex: &extension.GetOmciEthernetFrameExtendedPmRequest_UniIndex{
2370 UniIndex: *options.Args.UniIndex,
2371 },
2372 },
Himani Chawla553a1392021-06-10 23:39:17 +05302373 },
2374 },
Himani Chawla806aa892021-08-30 15:51:46 +05302375 }
2376 } else {
2377 singleGetValReq = extension.SingleGetValueRequest{
2378 TargetId: string(options.Args.Id),
2379 Request: &extension.GetValueRequest{
2380 Request: &extension.GetValueRequest_OnuInfo{
2381 OnuInfo: &extension.GetOmciEthernetFrameExtendedPmRequest{
2382 OnuDeviceId: string(options.Args.Id),
2383 Reset_: options.Reset,
2384 },
2385 },
2386 },
2387 }
Himani Chawla553a1392021-06-10 23:39:17 +05302388 }
Himani Chawla806aa892021-08-30 15:51:46 +05302389
Himani Chawla553a1392021-06-10 23:39:17 +05302390 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
2391 defer cancel()
2392 rv, err := client.GetExtValue(ctx, &singleGetValReq)
2393 if err != nil {
2394 Error.Printf("Error getting value on device Id %s,err=%s\n", options.Args.Id, ErrorToString(err))
2395 return err
2396 }
2397
2398 if rv.Response.Status != extension.GetValueResponse_OK {
2399 return fmt.Errorf("failed to get ethernet frame extended pm counters %v", rv.Response.ErrReason.String())
2400 }
2401 outputFormat := CharReplacer.Replace(options.Format)
2402 data := buildOnuEthernetFrameExtendedPmOutputFormat(rv.GetResponse().GetOnuCounters())
2403 if outputFormat == "" {
2404 outputFormat = GetCommandOptionWithDefault("device-get-onu-status", "format", DEFAULT_ETHERNET_FRAME_EXTENDED_PM_COUNTERS_FORMAT)
2405 }
Himani Chawla40acc122021-05-26 18:52:29 +05302406 result := CommandResult{
2407 Format: format.Format(outputFormat),
2408 OutputAs: toOutputType(options.OutputAs),
2409 NameLimit: options.NameLimit,
2410 Data: data,
2411 }
2412 GenerateOutput(&result)
2413 return nil
2414}
2415
kesavand6d1131f2021-02-05 22:38:15 +05302416func (options *UniStatus) Execute(args []string) error {
2417 conn, err := NewConnection()
2418 if err != nil {
2419 return err
2420 }
2421 defer conn.Close()
2422 client := extension.NewExtensionClient(conn)
2423
2424 singleGetValReq := extension.SingleGetValueRequest{
2425 TargetId: string(options.Args.Id),
2426 Request: &extension.GetValueRequest{
2427 Request: &extension.GetValueRequest_UniInfo{
2428 UniInfo: &extension.GetOnuUniInfoRequest{
2429 UniIndex: options.Args.UniIndex,
2430 },
2431 },
2432 },
2433 }
David K. Bainbridge9189c632021-03-26 21:52:21 +00002434 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
kesavand6d1131f2021-02-05 22:38:15 +05302435 defer cancel()
2436 rv, err := client.GetExtValue(ctx, &singleGetValReq)
2437 if err != nil {
2438 Error.Printf("Error getting value on device Id %s,err=%s\n", options.Args.Id, ErrorToString(err))
2439 return err
2440 }
2441 if rv.Response.Status != extension.GetValueResponse_OK {
2442 return fmt.Errorf("failed to get uni status %v", rv.Response.ErrReason.String())
2443 }
2444 outputFormat := CharReplacer.Replace(options.Format)
2445 if outputFormat == "" {
2446 outputFormat = GetCommandOptionWithDefault("device-get-uni-status", "format", DEFAULT_DEVICE_GET_UNI_STATUS_FORMAT)
2447 }
2448 result := CommandResult{
2449 Format: format.Format(outputFormat),
2450 OutputAs: toOutputType(options.OutputAs),
2451 NameLimit: options.NameLimit,
2452 Data: rv.GetResponse().GetUniInfo(),
2453 }
2454 GenerateOutput(&result)
2455 return nil
2456}
2457
Girish Gowdra4f5ce7c2021-04-29 18:53:21 -07002458func (options *OnuPonOpticalInfo) Execute(args []string) error {
2459 conn, err := NewConnection()
2460 if err != nil {
2461 return err
2462 }
2463 defer conn.Close()
2464 client := extension.NewExtensionClient(conn)
2465
2466 singleGetValReq := extension.SingleGetValueRequest{
2467 TargetId: string(options.Args.Id),
2468 Request: &extension.GetValueRequest{
2469 Request: &extension.GetValueRequest_OnuOpticalInfo{
2470 OnuOpticalInfo: &extension.GetOnuPonOpticalInfo{},
2471 },
2472 },
2473 }
2474 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
2475 defer cancel()
2476 rv, err := client.GetExtValue(ctx, &singleGetValReq)
2477 if err != nil {
2478 Error.Printf("Error getting value on device Id %s,err=%s\n", options.Args.Id, ErrorToString(err))
2479 return err
2480 }
2481 if rv.Response.Status != extension.GetValueResponse_OK {
2482 return fmt.Errorf("failed to get onu pon optical info %v", rv.Response.ErrReason.String())
2483 }
2484 outputFormat := CharReplacer.Replace(options.Format)
2485 if outputFormat == "" {
2486 outputFormat = GetCommandOptionWithDefault("device-get-onu-pon-optical-info", "format", DEFAULT_ONU_PON_OPTICAL_INFO_STATUS_FORMAT)
2487 }
2488 result := CommandResult{
2489 Format: format.Format(outputFormat),
2490 OutputAs: toOutputType(options.OutputAs),
2491 NameLimit: options.NameLimit,
2492 Data: rv.GetResponse().GetOnuOpticalInfo(),
2493 }
2494 GenerateOutput(&result)
2495 return nil
2496}
2497
Gamze Abakac857a462021-05-26 13:45:54 +00002498func (options *RxPower) Execute(args []string) error {
2499 conn, err := NewConnection()
2500 if err != nil {
2501 return err
2502 }
2503 defer conn.Close()
2504 client := extension.NewExtensionClient(conn)
2505
2506 singleGetValReq := extension.SingleGetValueRequest{
2507 TargetId: string(options.Args.Id),
2508 Request: &extension.GetValueRequest{
2509 Request: &extension.GetValueRequest_RxPower{
2510 RxPower: &extension.GetRxPowerRequest{
2511 IntfId: options.Args.PortNo,
2512 OnuId: options.Args.OnuNo,
2513 },
2514 },
2515 },
2516 }
2517
2518 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
2519 defer cancel()
2520 rv, err := client.GetExtValue(ctx, &singleGetValReq)
2521 if err != nil {
2522 Error.Printf("Error getting value on device Id %s,err=%s\n", options.Args.Id, ErrorToString(err))
2523 return err
2524 }
2525 if rv.Response.Status != extension.GetValueResponse_OK {
2526 return fmt.Errorf("failed to get rx power %v", rv.Response.ErrReason.String())
2527 }
2528 outputFormat := CharReplacer.Replace(options.Format)
2529 if outputFormat == "" {
2530 outputFormat = GetCommandOptionWithDefault("device-get-rx-power", "format", DEFAULT_RX_POWER_STATUS_FORMAT)
2531 }
2532 result := CommandResult{
2533 Format: format.Format(outputFormat),
2534 OutputAs: toOutputType(options.OutputAs),
2535 NameLimit: options.NameLimit,
2536 Data: rv.GetResponse().GetRxPower(),
2537 }
2538 GenerateOutput(&result)
2539 return nil
2540}
2541
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -08002542/*Device get Onu Distance */
2543func (options *DeviceGetExtValue) Execute(args []string) error {
2544 conn, err := NewConnection()
2545 if err != nil {
2546 return err
2547 }
2548 defer conn.Close()
2549
Scott Baker9173ed82020-05-19 08:30:12 -07002550 client := voltha.NewVolthaServiceClient(conn)
2551
khenaidoo080ce882021-10-19 17:35:08 -04002552 valueflag, okay := extension.ValueType_Type_value[string(options.Args.Valueflag)]
Scott Baker9173ed82020-05-19 08:30:12 -07002553 if !okay {
2554 Error.Printf("Unknown valueflag %s\n", options.Args.Valueflag)
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -08002555 }
2556
khenaidoo080ce882021-10-19 17:35:08 -04002557 val := extension.ValueSpecifier{Id: string(options.Args.Id), Value: extension.ValueType_Type(valueflag)}
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -08002558
David K. Bainbridge9189c632021-03-26 21:52:21 +00002559 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -08002560 defer cancel()
2561
Scott Baker9173ed82020-05-19 08:30:12 -07002562 rv, err := client.GetExtValue(ctx, &val)
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -08002563 if err != nil {
2564 Error.Printf("Error getting value on device Id %s,err=%s\n", options.Args.Id, ErrorToString(err))
2565 return err
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -08002566 }
2567
Scott Baker9173ed82020-05-19 08:30:12 -07002568 var rows []ReturnValueRow
khenaidoo080ce882021-10-19 17:35:08 -04002569 for name, num := range extension.ValueType_Type_value {
Scott Baker9173ed82020-05-19 08:30:12 -07002570 if num == 0 {
2571 // EMPTY is not a real value
2572 continue
2573 }
2574 if (rv.Error & uint32(num)) != 0 {
2575 row := ReturnValueRow{Name: name, Result: "Error"}
2576 rows = append(rows, row)
2577 }
2578 if (rv.Unsupported & uint32(num)) != 0 {
2579 row := ReturnValueRow{Name: name, Result: "Unsupported"}
2580 rows = append(rows, row)
2581 }
2582 if (rv.Set & uint32(num)) != 0 {
2583 switch name {
2584 case "DISTANCE":
2585 row := ReturnValueRow{Name: name, Result: rv.Distance}
2586 rows = append(rows, row)
2587 default:
2588 row := ReturnValueRow{Name: name, Result: "Unimplemented-in-voltctl"}
2589 rows = append(rows, row)
2590 }
2591 }
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -08002592 }
2593
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -08002594 outputFormat := CharReplacer.Replace(options.Format)
2595 if outputFormat == "" {
2596 outputFormat = GetCommandOptionWithDefault("device-value-get", "format", DEFAULT_DEVICE_VALUE_GET_FORMAT)
2597 }
2598
2599 result := CommandResult{
2600 Format: format.Format(outputFormat),
2601 OutputAs: toOutputType(options.OutputAs),
2602 NameLimit: options.NameLimit,
Scott Baker9173ed82020-05-19 08:30:12 -07002603 Data: rows,
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -08002604 }
2605 GenerateOutput(&result)
2606 return nil
2607}
serkantul3d22fc72022-09-14 12:22:56 +03002608
2609/*Device get Onu OMCI TX RX Stats */
2610func (options *OnuOmciTxRxStats) Execute(args []string) error {
2611 conn, err := NewConnection()
2612 if err != nil {
2613 return err
2614 }
2615 defer conn.Close()
2616 client := extension.NewExtensionClient(conn)
2617
2618 singleGetValReq := extension.SingleGetValueRequest{
2619 TargetId: string(options.Args.Id),
2620 Request: &extension.GetValueRequest{
2621 Request: &extension.GetValueRequest_OnuOmciStats{
2622 OnuOmciStats: &extension.GetOnuOmciTxRxStatsRequest{},
2623 },
2624 },
2625 }
2626
2627 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
2628 defer cancel()
2629 rv, err := client.GetExtValue(ctx, &singleGetValReq)
2630 if err != nil {
2631 Error.Printf("Error getting value on device Id %s,err=%s\n", options.Args.Id, ErrorToString(err))
2632 return err
2633 }
2634
2635 if rv.Response.Status != extension.GetValueResponse_OK {
2636 return fmt.Errorf("failed to get onu omci tx rx stats %v", rv.Response.ErrReason.String())
2637 }
2638 outputFormat := CharReplacer.Replace(options.Format)
2639 if outputFormat == "" {
2640 outputFormat = GetCommandOptionWithDefault("device-get-onu-omci-tx-rx-stats", "format", DEFAULT_ONU_OMCI_TX_RX_STATS_FORMAT)
2641 }
2642 result := CommandResult{
2643 Format: format.Format(outputFormat),
2644 OutputAs: toOutputType(options.OutputAs),
2645 NameLimit: options.NameLimit,
2646 Data: rv.GetResponse().GetOnuOmciStats(),
2647 }
2648 GenerateOutput(&result)
2649 return nil
2650}
praneeth nalmas1dd094c2022-12-22 14:15:13 +05302651
2652/*Device get Onu Active Alarms */
2653func (options *GetOnuOmciActiveAlarms) Execute(args []string) error {
2654 conn, err := NewConnection()
2655 if err != nil {
2656 return err
2657 }
2658 defer conn.Close()
2659 client := extension.NewExtensionClient(conn)
2660
2661 singleGetValReq := extension.SingleGetValueRequest{
2662 TargetId: string(options.Args.Id),
2663 Request: &extension.GetValueRequest{
2664 Request: &extension.GetValueRequest_OnuActiveAlarms{
2665 OnuActiveAlarms: &extension.GetOnuOmciActiveAlarmsRequest{},
2666 },
2667 },
2668 }
2669 Info.Printf("Getting omci_active_alarms for device Id %s\n", options.Args.Id)
2670 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
2671 defer cancel()
2672 rv, err := client.GetExtValue(ctx, &singleGetValReq)
2673 if err != nil {
2674 Error.Printf("Error getting value on device Id %s,err=%s\n", options.Args.Id, ErrorToString(err))
2675 return err
2676 }
2677
2678 if rv.Response.Status != extension.GetValueResponse_OK {
2679 return fmt.Errorf("failed to get onu omci active alarms list %v", rv.Response.ErrReason.String())
2680 }
2681 outputFormat := CharReplacer.Replace(options.Format)
2682 if outputFormat == "" {
2683 outputFormat = GetCommandOptionWithDefault("device-get-onu-omci-active-alarms", "format", DEFAULT_DEVICE_ALARMS_FORMAT)
2684 }
2685
2686 orderBy := options.OrderBy
2687 if orderBy == "" {
2688 orderBy = GetCommandOptionWithDefault("device-list", "order", DEFAULT_DEVICE_ALARMS_ORDER)
2689 }
2690
2691 result := CommandResult{
2692 Format: format.Format(outputFormat),
2693 OutputAs: toOutputType(options.OutputAs),
2694 OrderBy: orderBy,
2695 NameLimit: options.NameLimit,
2696 Data: rv.GetResponse().GetOnuActiveAlarms().GetActiveAlarms(),
2697 }
2698 GenerateOutput(&result)
2699 return nil
2700}
praneeth nalmas39c71ad2023-09-27 18:29:04 +05302701
Akash Soni66db9632024-04-15 09:05:15 +05302702/*Device get Onu Active Alarms */
2703func (options *GetOnuDistance) Execute(args []string) error {
2704 conn, err := NewConnection()
2705 if err != nil {
2706 return err
2707 }
2708 defer conn.Close()
2709 client := extension.NewExtensionClient(conn)
2710
2711 singleGetValReq := extension.SingleGetValueRequest{
2712 TargetId: string(options.Args.Id),
2713 Request: &extension.GetValueRequest{
2714 Request: &extension.GetValueRequest_Distance{
2715 Distance: &extension.GetDistanceRequest{
2716 OnuDeviceId: string(options.Args.OnuId),
2717 },
2718 },
2719 },
2720 }
2721 Info.Printf("Getting onu distance for device Id %s\n", options.Args.Id)
2722 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
2723 defer cancel()
2724 rv, err := client.GetExtValue(ctx, &singleGetValReq)
2725 if err != nil {
2726 Error.Printf("Error getting value on device Id %s,err=%s\n", options.Args.Id, ErrorToString(err))
2727 return err
2728 }
2729
2730 if rv.Response.Status != extension.GetValueResponse_OK {
2731 return fmt.Errorf("failed to get onu distance stats %v", rv.Response.ErrReason.String())
2732 }
2733 outputFormat := CharReplacer.Replace(options.Format)
2734 if outputFormat == "" {
2735 outputFormat = GetCommandOptionWithDefault("get-onu-distance", "format", DEFAULT_ONU_DISTANCE_FORMAT)
2736 }
2737
2738 result := CommandResult{
2739 Format: format.Format(outputFormat),
2740 OutputAs: toOutputType(options.OutputAs),
2741 NameLimit: options.NameLimit,
2742 Data: rv.GetResponse().GetDistance().GetDistance(),
2743 }
2744 fmt.Println("onu distance : ", rv)
2745 GenerateOutput(&result)
2746 return nil
2747}
2748
praneeth nalmas39c71ad2023-09-27 18:29:04 +05302749func (options *PonRxPower) Execute(args []string) error {
2750 conn, err := NewConnection()
2751 if err != nil {
2752 return err
2753 }
2754 defer conn.Close()
2755 client := extension.NewExtensionClient(conn)
2756
2757 singleGetValReq := extension.SingleGetValueRequest{
2758 TargetId: string(options.Args.Id),
2759 Request: &extension.GetValueRequest{
2760 Request: &extension.GetValueRequest_OltRxPower{
2761 OltRxPower: &extension.GetOltRxPowerRequest{
2762 PortLabel: options.Args.PortLabel,
2763 OnuSn: options.Args.SerialNo,
2764 },
2765 },
2766 },
2767 }
2768
2769 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
2770 defer cancel()
2771 rv, err := client.GetExtValue(ctx, &singleGetValReq)
2772 if err != nil {
2773 Error.Printf("Error getting value on device Id %s,err=%s\n", options.Args.Id, ErrorToString(err))
2774 return err
2775 }
2776 if rv.Response.Status != extension.GetValueResponse_OK {
2777 return fmt.Errorf("failed to get rx power %v", rv.Response.ErrReason.String())
2778 }
2779 outputFormat := CharReplacer.Replace(options.Format)
2780 if outputFormat == "" {
2781 outputFormat = GetCommandOptionWithDefault("device-get-pon-rx-power", "format", DEFAULT_PON_RX_POWER_STATUS_FORMAT)
2782 }
2783 result := CommandResult{
2784 Format: format.Format(outputFormat),
2785 OutputAs: toOutputType(options.OutputAs),
2786 NameLimit: options.NameLimit,
2787 Data: rv.GetResponse().GetOltRxPower().GetRxPower(),
2788 }
2789 GenerateOutput(&result)
2790 return nil
2791}