blob: 10ebad80479c34340d1925adba8598f587c837e6 [file] [log] [blame]
Zack Williamse940c7a2019-08-21 14:25:39 -07001/*
2 * Copyright 2019-present Ciena Corporation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package commands
17
18import (
19 "context"
20 "fmt"
David Bainbridge7052fe82020-03-25 10:37:00 -070021 "os"
22 "strconv"
23 "strings"
Girish Gowdra610acb42021-01-27 13:33:57 -080024 "time"
David Bainbridge7052fe82020-03-25 10:37:00 -070025
Scott Baker9173ed82020-05-19 08:30:12 -070026 "github.com/golang/protobuf/ptypes/empty"
Zack Williamse940c7a2019-08-21 14:25:39 -070027 flags "github.com/jessevdk/go-flags"
Scott Baker2b0ad652019-08-21 14:57:07 -070028 "github.com/opencord/voltctl/pkg/format"
kesavand8ec4fc02021-01-27 09:10:22 -050029 "github.com/opencord/voltha-protos/v4/go/common"
30 "github.com/opencord/voltha-protos/v4/go/extension"
31 "github.com/opencord/voltha-protos/v4/go/voltha"
Zack Williamse940c7a2019-08-21 14:25:39 -070032)
33
34const (
David K. Bainbridge89003c42020-02-27 17:22:49 -080035 DEFAULT_DEVICE_FORMAT = "table{{ .Id }}\t{{.Type}}\t{{.Root}}\t{{.ParentId}}\t{{.SerialNumber}}\t{{.AdminState}}\t{{.OperStatus}}\t{{.ConnectStatus}}\t{{.Reason}}"
Zack Williamse940c7a2019-08-21 14:25:39 -070036 DEFAULT_DEVICE_PORTS_FORMAT = "table{{.PortNo}}\t{{.Label}}\t{{.Type}}\t{{.AdminState}}\t{{.OperStatus}}\t{{.DeviceId}}\t{{.Peers}}"
37 DEFAULT_DEVICE_INSPECT_FORMAT = `ID: {{.Id}}
38 TYPE: {{.Type}}
39 ROOT: {{.Root}}
40 PARENTID: {{.ParentId}}
41 SERIALNUMBER: {{.SerialNumber}}
42 VLAN: {{.Vlan}}
43 ADMINSTATE: {{.AdminState}}
44 OPERSTATUS: {{.OperStatus}}
45 CONNECTSTATUS: {{.ConnectStatus}}`
Rohan Agrawal9228d2f2020-06-03 07:48:50 +000046 DEFAULT_DEVICE_PM_CONFIG_GET_FORMAT = "table{{.DefaultFreq}}\t{{.Grouped}}\t{{.FreqOverride}}"
47 DEFAULT_DEVICE_PM_CONFIG_METRIC_LIST_FORMAT = "table{{.Name}}\t{{.Type}}\t{{.Enabled}}\t{{.SampleFreq}}"
48 DEFAULT_DEVICE_PM_CONFIG_GROUP_LIST_FORMAT = "table{{.GroupName}}\t{{.Enabled}}\t{{.GroupFreq}}"
49 DEFAULT_DEVICE_VALUE_GET_FORMAT = "table{{.Name}}\t{{.Result}}"
Andrea Campanella791d88b2021-01-08 13:29:00 +010050 DEFAULT_DEVICE_IMAGE_LIST_GET_FORMAT = "table{{.Name}}\t{{.Url}}\t{{.Crc}}\t{{.DownloadState}}\t{{.ImageVersion}}\t{{.LocalDir}}\t{{.ImageState}}\t{{.FileSize}}"
ssiddiqui7bc89e92021-05-20 20:58:02 +053051 ONU_IMAGE_LIST_FORMAT = "table{{.Version}}\t{{.IsCommited}}\t{{.IsActive}}\t{{.IsValid}}\t{{.ProductCode}}\t{{.Hash}}"
52 ONU_IMAGE_STATUS_FORMAT = "table{{.DeviceId}}\t{{.ImageState.Version}}\t{{.ImageState.DownloadState}}\t{{.ImageState.Reason}}\t{{.ImageState.ImageState}}\t"
kesavand8ec4fc02021-01-27 09:10:22 -050053 DEFAULT_DEVICE_GET_PORT_STATUS_FORMAT = `
54 TXBYTES: {{.TxBytes}}
55 TXPACKETS: {{.TxPackets}}
56 TXERRPACKETS: {{.TxErrorPackets}}
57 TXBCASTPACKETS: {{.TxBcastPackets}}
58 TXUCASTPACKETS: {{.TxUcastPackets}}
59 TXMCASTPACKETS: {{.TxMcastPackets}}
60 RXBYTES: {{.RxBytes}}
61 RXPACKETS: {{.RxPackets}}
62 RXERRPACKETS: {{.RxErrorPackets}}
63 RXBCASTPACKETS: {{.RxBcastPackets}}
64 RXUCASTPACKETS: {{.RxUcastPackets}}
65 RXMCASTPACKETS: {{.RxMcastPackets}}`
kesavand6d1131f2021-02-05 22:38:15 +053066 DEFAULT_DEVICE_GET_UNI_STATUS_FORMAT = `
67 ADMIN_STATE: {{.AdmState}}
68 OPERATIONAL_STATE: {{.OperState}}
69 CONFIG_IND: {{.ConfigInd}}`
Girish Gowdra4f5ce7c2021-04-29 18:53:21 -070070 DEFAULT_ONU_PON_OPTICAL_INFO_STATUS_FORMAT = `
71 POWER_FEED_VOLTAGE__VOLTS: {{.PowerFeedVoltage}}
72 RECEIVED_OPTICAL_POWER__dBm: {{.ReceivedOpticalPower}}
73 MEAN_OPTICAL_LAUNCH_POWER__dBm: {{.MeanOpticalLaunchPower}}
74 LASER_BIAS_CURRENT__mA: {{.LaserBiasCurrent}}
75 TEMPERATURE__Celsius: {{.Temperature}}`
Gamze Abakac857a462021-05-26 13:45:54 +000076 DEFAULT_RX_POWER_STATUS_FORMAT = `
77 INTF_ID: {{.IntfId}}
78 ONU_ID: {{.OnuId}}
79 STATUS: {{.Status}}
80 FAIL_REASON: {{.FailReason}}
81 RX_POWER : {{.RxPower}}`
Zack Williamse940c7a2019-08-21 14:25:39 -070082)
83
84type DeviceList struct {
85 ListOutputOptions
86}
87
88type DeviceCreate struct {
David Bainbridge1a514392020-06-23 11:12:51 -070089 DeviceType string `short:"t" required:"true" long:"devicetype" description:"Device type"`
David Bainbridge835dd0e2020-04-01 10:30:09 -070090 MACAddress string `short:"m" long:"macaddress" default:"" description:"MAC Address"`
Zack Williamse940c7a2019-08-21 14:25:39 -070091 IPAddress string `short:"i" long:"ipaddress" default:"" description:"IP Address"`
92 HostAndPort string `short:"H" long:"hostandport" default:"" description:"Host and port"`
93}
94
95type DeviceId string
96
Rohan Agrawal9228d2f2020-06-03 07:48:50 +000097type MetricName string
98type GroupName string
kesavand12cd8eb2020-01-20 22:25:22 -050099type PortNum uint32
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -0800100type ValueFlag string
kesavand12cd8eb2020-01-20 22:25:22 -0500101
Zack Williamse940c7a2019-08-21 14:25:39 -0700102type DeviceDelete struct {
Himani Chawla9933ddc2020-10-12 23:53:27 +0530103 Force bool `long:"force" description:"Delete device forcefully"`
104 Args struct {
Zack Williamse940c7a2019-08-21 14:25:39 -0700105 Ids []DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
106 } `positional-args:"yes"`
107}
108
109type DeviceEnable struct {
110 Args struct {
111 Ids []DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
112 } `positional-args:"yes"`
113}
114
115type DeviceDisable struct {
116 Args struct {
117 Ids []DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
118 } `positional-args:"yes"`
119}
120
121type DeviceReboot struct {
122 Args struct {
123 Ids []DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
124 } `positional-args:"yes"`
125}
126
127type DeviceFlowList struct {
128 ListOutputOptions
Maninder045921e2020-09-29 16:46:02 +0530129 FlowIdOptions
Zack Williamse940c7a2019-08-21 14:25:39 -0700130 Args struct {
131 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
132 } `positional-args:"yes"`
133}
134
135type DevicePortList struct {
136 ListOutputOptions
137 Args struct {
138 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
139 } `positional-args:"yes"`
140}
141
142type DeviceInspect struct {
143 OutputOptionsJson
144 Args struct {
145 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
146 } `positional-args:"yes"`
147}
148
kesavand12cd8eb2020-01-20 22:25:22 -0500149type DevicePortEnable struct {
150 Args struct {
151 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
152 PortId PortNum `positional-arg-name:"PORT_NUMBER" required:"yes"`
153 } `positional-args:"yes"`
154}
155
156type DevicePortDisable struct {
157 Args struct {
158 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
159 PortId PortNum `positional-arg-name:"PORT_NUMBER" required:"yes"`
160 } `positional-args:"yes"`
161}
162
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000163type DevicePmConfigsGet struct {
164 ListOutputOptions
165 Args struct {
166 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
167 } `positional-args:"yes"`
168}
169
170type DevicePmConfigMetricList struct {
171 ListOutputOptions
172 Args struct {
173 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
174 } `positional-args:"yes"`
175}
176
177type DevicePmConfigGroupList struct {
178 ListOutputOptions
179 Args struct {
180 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
181 } `positional-args:"yes"`
182}
183
184type DevicePmConfigGroupMetricList struct {
185 ListOutputOptions
186 Args struct {
187 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
188 Group GroupName `positional-arg-name:"GROUP_NAME" required:"yes"`
189 } `positional-args:"yes"`
190}
191
192type DevicePmConfigFrequencySet struct {
193 OutputOptions
194 Args struct {
Girish Gowdra610acb42021-01-27 13:33:57 -0800195 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
196 Interval time.Duration `positional-arg-name:"INTERVAL" required:"yes"`
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000197 } `positional-args:"yes"`
198}
199
200type DevicePmConfigMetricEnable struct {
201 Args struct {
202 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
203 Metrics []MetricName `positional-arg-name:"METRIC_NAME" required:"yes"`
204 } `positional-args:"yes"`
205}
206
207type DevicePmConfigMetricDisable struct {
208 Args struct {
209 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
210 Metrics []MetricName `positional-arg-name:"METRIC_NAME" required:"yes"`
211 } `positional-args:"yes"`
212}
213
214type DevicePmConfigGroupEnable struct {
215 Args struct {
Girish Gowdra610acb42021-01-27 13:33:57 -0800216 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
217 Group GroupName `positional-arg-name:"GROUP_NAME" required:"yes"`
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000218 } `positional-args:"yes"`
219}
220
221type DevicePmConfigGroupDisable struct {
222 Args struct {
Girish Gowdra610acb42021-01-27 13:33:57 -0800223 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
224 Group GroupName `positional-arg-name:"GROUP_NAME" required:"yes"`
225 } `positional-args:"yes"`
226}
227
228type DevicePmConfigGroupFrequencySet struct {
229 OutputOptions
230 Args struct {
231 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
232 Group GroupName `positional-arg-name:"GROUP_NAME" required:"yes"`
233 Interval time.Duration `positional-arg-name:"INTERVAL" required:"yes"`
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000234 } `positional-args:"yes"`
235}
236
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -0800237type DeviceGetExtValue struct {
238 ListOutputOptions
239 Args struct {
240 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
241 Valueflag ValueFlag `positional-arg-name:"VALUE_FLAG" required:"yes"`
242 } `positional-args:"yes"`
243}
Rohan Agrawald7df3772020-06-29 11:23:36 +0000244
245type DevicePmConfigSetMaxSkew struct {
246 Args struct {
247 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
248 MaxSkew uint32 `positional-arg-name:"MAX_SKEW" required:"yes"`
249 } `positional-args:"yes"`
250}
251
Andrea Campanella791d88b2021-01-08 13:29:00 +0100252type DeviceOnuListImages struct {
253 ListOutputOptions
254 Args struct {
255 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
256 } `positional-args:"yes"`
257}
258
259type DeviceOnuDownloadImage struct {
260 Args struct {
261 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
262 Name string `positional-arg-name:"IMAGE_NAME" required:"yes"`
263 Url string `positional-arg-name:"IMAGE_URL" required:"yes"`
264 ImageVersion string `positional-arg-name:"IMAGE_VERSION" required:"yes"`
265 Crc uint32 `positional-arg-name:"IMAGE_CRC" required:"yes"`
266 LocalDir string `positional-arg-name:"IMAGE_LOCAL_DIRECTORY"`
267 } `positional-args:"yes"`
268}
269
270type DeviceOnuActivateImageUpdate struct {
271 Args struct {
272 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
273 Name string `positional-arg-name:"IMAGE_NAME" required:"yes"`
274 ImageVersion string `positional-arg-name:"IMAGE_VERSION" required:"yes"`
275 SaveConfig bool `positional-arg-name:"SAVE_EXISTING_CONFIG"`
276 LocalDir string `positional-arg-name:"IMAGE_LOCAL_DIRECTORY"`
Andrea Campanella7b2ecf42021-02-25 12:27:15 +0100277 } `positional-args:"yes"`
kesavand8ec4fc02021-01-27 09:10:22 -0500278}
kesavand3e2f9f62021-04-22 11:06:38 +0530279
280type OnuDownloadImage struct {
281 Args struct {
282 ImageVersion string `positional-arg-name:"IMAGE_VERSION" required:"yes"`
283 Url string `positional-arg-name:"IMAGE_URL" required:"yes"`
ssiddiqui7bc89e92021-05-20 20:58:02 +0530284 Vendor string `positional-arg-name:"IMAGE_VENDOR"`
kesavand3e2f9f62021-04-22 11:06:38 +0530285 ActivateOnSuccess bool `positional-arg-name:"IMAGE_ACTIVATE_ON_SUCCESS"`
286 CommitOnSuccess bool `positional-arg-name:"IMAGE_COMMIT_ON_SUCCESS"`
287 Crc uint32 `positional-arg-name:"IMAGE_CRC"`
288 IDs []DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
289 } `positional-args:"yes"`
290}
291
292type OnuActivateImage struct {
293 Args struct {
294 ImageVersion string `positional-arg-name:"IMAGE_VERSION" required:"yes"`
295 CommitOnSuccess bool `positional-arg-name:"IMAGE_COMMIT_ON_SUCCESS"`
296 IDs []DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
297 } `positional-args:"yes"`
298}
299
300type OnuAbortUpgradeImage struct {
301 Args struct {
302 ImageVersion string `positional-arg-name:"IMAGE_VERSION" required:"yes"`
303 IDs []DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
304 } `positional-args:"yes"`
305}
306
307type OnuCommitImage struct {
308 Args struct {
309 ImageVersion string `positional-arg-name:"IMAGE_VERSION" required:"yes"`
310 IDs []DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
311 } `positional-args:"yes"`
312}
313
314type OnuImageStatus struct {
315 ListOutputOptions
316 Args struct {
317 ImageVersion string `positional-arg-name:"IMAGE_VERSION" required:"yes"`
318 IDs []DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
319 } `positional-args:"yes"`
320}
321
322type OnuListImages struct {
323 ListOutputOptions
324 Args struct {
325 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
326 } `positional-args:"yes"`
327}
328
kesavand8ec4fc02021-01-27 09:10:22 -0500329type DeviceGetPortStats struct {
330 ListOutputOptions
331 Args struct {
332 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
333 PortNo uint32 `positional-arg-name:"PORT_NO" required:"yes"`
334 PortType string `positional-arg-name:"PORT_TYPE" required:"yes"`
Andrea Campanella791d88b2021-01-08 13:29:00 +0100335 } `positional-args:"yes"`
336}
kesavand6d1131f2021-02-05 22:38:15 +0530337type UniStatus struct {
338 ListOutputOptions
339 Args struct {
340 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
341 UniIndex uint32 `positional-arg-name:"UNI_INDEX" required:"yes"`
342 } `positional-args:"yes"`
343}
Girish Gowdra4f5ce7c2021-04-29 18:53:21 -0700344type OnuPonOpticalInfo struct {
345 ListOutputOptions
346 Args struct {
347 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
348 } `positional-args:"yes"`
349}
Himani Chawla40acc122021-05-26 18:52:29 +0530350
351type GetOnuStats struct {
352 ListOutputOptions
353 Args struct {
354 OltId DeviceId `positional-arg-name:"OLT_DEVICE_ID" required:"yes"`
355 IntfId uint32 `positional-arg-name:"PON_INTF_ID" required:"yes"`
356 OnuId uint32 `positional-arg-name:"ONU_ID" required:"yes"`
357 } `positional-args:"yes"`
358}
359
Gamze Abakac857a462021-05-26 13:45:54 +0000360type RxPower struct {
361 ListOutputOptions
362 Args struct {
363 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
364 PortNo uint32 `positional-arg-name:"PORT_NO" required:"yes"`
365 OnuNo uint32 `positional-arg-name:"ONU_NO" required:"yes"`
366 } `positional-args:"yes"`
367}
368
Zack Williamse940c7a2019-08-21 14:25:39 -0700369type DeviceOpts struct {
370 List DeviceList `command:"list"`
371 Create DeviceCreate `command:"create"`
372 Delete DeviceDelete `command:"delete"`
373 Enable DeviceEnable `command:"enable"`
374 Disable DeviceDisable `command:"disable"`
375 Flows DeviceFlowList `command:"flows"`
kesavand12cd8eb2020-01-20 22:25:22 -0500376 Port struct {
377 List DevicePortList `command:"list"`
378 Enable DevicePortEnable `command:"enable"`
379 Disable DevicePortDisable `command:"disable"`
380 } `command:"port"`
381 Inspect DeviceInspect `command:"inspect"`
382 Reboot DeviceReboot `command:"reboot"`
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -0800383 Value struct {
384 Get DeviceGetExtValue `command:"get"`
385 } `command:"value"`
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000386 PmConfig struct {
Rohan Agrawald7df3772020-06-29 11:23:36 +0000387 Get DevicePmConfigsGet `command:"get"`
388 MaxSkew struct {
389 Set DevicePmConfigSetMaxSkew `command:"set"`
390 } `command:"maxskew"`
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000391 Frequency struct {
392 Set DevicePmConfigFrequencySet `command:"set"`
393 } `command:"frequency"`
394 Metric struct {
395 List DevicePmConfigMetricList `command:"list"`
396 Enable DevicePmConfigMetricEnable `command:"enable"`
397 Disable DevicePmConfigMetricDisable `command:"disable"`
398 } `command:"metric"`
399 Group struct {
Girish Gowdra610acb42021-01-27 13:33:57 -0800400 List DevicePmConfigGroupList `command:"list"`
401 Enable DevicePmConfigGroupEnable `command:"enable"`
402 Disable DevicePmConfigGroupDisable `command:"disable"`
403 Set DevicePmConfigGroupFrequencySet `command:"set"`
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000404 } `command:"group"`
405 GroupMetric struct {
406 List DevicePmConfigGroupMetricList `command:"list"`
407 } `command:"groupmetric"`
408 } `command:"pmconfig"`
Andrea Campanella791d88b2021-01-08 13:29:00 +0100409 Image struct {
410 Get DeviceOnuListImages `command:"list"`
411 Download DeviceOnuDownloadImage `command:"download"`
412 Activate DeviceOnuActivateImageUpdate `command:"activate"`
413 } `command:"image"`
kesavand3e2f9f62021-04-22 11:06:38 +0530414 DownloadImage struct {
415 Download OnuDownloadImage `command:"download"`
416 Activate OnuActivateImage `command:"activate"`
417 Commit OnuCommitImage `command:"commit"`
418 AbortUpgrade OnuAbortUpgradeImage `command:"abort"`
419 Status OnuImageStatus `command:"status"`
420 List OnuListImages `command:"list" `
421 } `command:"onuimage"`
kesavand8ec4fc02021-01-27 09:10:22 -0500422 GetExtVal struct {
Girish Gowdra4f5ce7c2021-04-29 18:53:21 -0700423 Stats DeviceGetPortStats `command:"portstats"`
424 UniStatus UniStatus `command:"unistatus"`
425 OpticalInfo OnuPonOpticalInfo `command:"onu_pon_optical_info"`
Himani Chawla40acc122021-05-26 18:52:29 +0530426 OnuStats GetOnuStats `command:"onu_stats"`
Gamze Abakac857a462021-05-26 13:45:54 +0000427 RxPower RxPower `command:"rxpower"`
kesavand8ec4fc02021-01-27 09:10:22 -0500428 } `command:"getextval"`
Zack Williamse940c7a2019-08-21 14:25:39 -0700429}
430
431var deviceOpts = DeviceOpts{}
432
433func RegisterDeviceCommands(parser *flags.Parser) {
David Bainbridge12f036f2019-10-15 22:09:04 +0000434 if _, err := parser.AddCommand("device", "device commands", "Commands to query and manipulate VOLTHA devices", &deviceOpts); err != nil {
David Bainbridgea6722342019-10-24 23:55:53 +0000435 Error.Fatalf("Unexpected error while attempting to register device commands : %s", err)
David Bainbridge12f036f2019-10-15 22:09:04 +0000436 }
Zack Williamse940c7a2019-08-21 14:25:39 -0700437}
438
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000439func (i *MetricName) Complete(match string) []flags.Completion {
440 conn, err := NewConnection()
441 if err != nil {
442 return nil
443 }
444 defer conn.Close()
445
446 client := voltha.NewVolthaServiceClient(conn)
447
448 var deviceId string
449found:
450 for i := len(os.Args) - 1; i >= 0; i -= 1 {
451 switch os.Args[i] {
452 case "enable":
453 fallthrough
454 case "disable":
455 if len(os.Args) > i+1 {
456 deviceId = os.Args[i+1]
457 } else {
458 return nil
459 }
460 break found
461 default:
462 }
463 }
464
465 if len(deviceId) == 0 {
466 return nil
467 }
468
David K. Bainbridge9189c632021-03-26 21:52:21 +0000469 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000470 defer cancel()
471
472 id := voltha.ID{Id: string(deviceId)}
473
474 pmconfigs, err := client.ListDevicePmConfigs(ctx, &id)
475
476 if err != nil {
477 return nil
478 }
479
480 list := make([]flags.Completion, 0)
481 for _, metrics := range pmconfigs.Metrics {
482 if strings.HasPrefix(metrics.Name, match) {
483 list = append(list, flags.Completion{Item: metrics.Name})
484 }
485 }
486
487 return list
488}
489
490func (i *GroupName) Complete(match string) []flags.Completion {
491 conn, err := NewConnection()
492 if err != nil {
493 return nil
494 }
495 defer conn.Close()
496
497 client := voltha.NewVolthaServiceClient(conn)
498
499 var deviceId string
500found:
501 for i := len(os.Args) - 1; i >= 0; i -= 1 {
502 switch os.Args[i] {
503 case "list":
504 fallthrough
505 case "enable":
506 fallthrough
507 case "disable":
508 if len(os.Args) > i+1 {
509 deviceId = os.Args[i+1]
510 } else {
511 return nil
512 }
513 break found
514 default:
515 }
516 }
517
518 if len(deviceId) == 0 {
519 return nil
520 }
521
David K. Bainbridge9189c632021-03-26 21:52:21 +0000522 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000523 defer cancel()
524
525 id := voltha.ID{Id: string(deviceId)}
526
527 pmconfigs, err := client.ListDevicePmConfigs(ctx, &id)
528
529 if err != nil {
530 return nil
531 }
532
533 list := make([]flags.Completion, 0)
534 for _, group := range pmconfigs.Groups {
535 if strings.HasPrefix(group.GroupName, match) {
536 list = append(list, flags.Completion{Item: group.GroupName})
537 }
538 }
539 return list
540}
541
kesavand12cd8eb2020-01-20 22:25:22 -0500542func (i *PortNum) Complete(match string) []flags.Completion {
543 conn, err := NewConnection()
544 if err != nil {
545 return nil
546 }
547 defer conn.Close()
548
Scott Baker9173ed82020-05-19 08:30:12 -0700549 client := voltha.NewVolthaServiceClient(conn)
kesavand12cd8eb2020-01-20 22:25:22 -0500550
551 /*
552 * The command line args when completing for PortNum will be a DeviceId
553 * followed by one or more PortNums. So walk the argument list from the
554 * end and find the first argument that is enable/disable as those are
555 * the subcommands that come before the positional arguments. It would
556 * be nice if this package gave us the list of optional arguments
557 * already parsed.
558 */
559 var deviceId string
560found:
561 for i := len(os.Args) - 1; i >= 0; i -= 1 {
562 switch os.Args[i] {
563 case "enable":
564 fallthrough
565 case "disable":
566 if len(os.Args) > i+1 {
567 deviceId = os.Args[i+1]
568 } else {
569 return nil
570 }
571 break found
572 default:
573 }
574 }
575
576 if len(deviceId) == 0 {
577 return nil
578 }
579
David K. Bainbridge9189c632021-03-26 21:52:21 +0000580 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
kesavand12cd8eb2020-01-20 22:25:22 -0500581 defer cancel()
kesavand12cd8eb2020-01-20 22:25:22 -0500582
Scott Baker9173ed82020-05-19 08:30:12 -0700583 id := voltha.ID{Id: string(deviceId)}
kesavand12cd8eb2020-01-20 22:25:22 -0500584
Scott Baker9173ed82020-05-19 08:30:12 -0700585 ports, err := client.ListDevicePorts(ctx, &id)
kesavand12cd8eb2020-01-20 22:25:22 -0500586 if err != nil {
587 return nil
588 }
589
590 list := make([]flags.Completion, 0)
Scott Baker9173ed82020-05-19 08:30:12 -0700591 for _, item := range ports.Items {
592 pn := strconv.FormatUint(uint64(item.PortNo), 10)
kesavand12cd8eb2020-01-20 22:25:22 -0500593 if strings.HasPrefix(pn, match) {
594 list = append(list, flags.Completion{Item: pn})
595 }
596 }
597
598 return list
599}
600
Zack Williamse940c7a2019-08-21 14:25:39 -0700601func (i *DeviceId) Complete(match string) []flags.Completion {
602 conn, err := NewConnection()
603 if err != nil {
604 return nil
605 }
606 defer conn.Close()
607
Scott Baker9173ed82020-05-19 08:30:12 -0700608 client := voltha.NewVolthaServiceClient(conn)
Zack Williamse940c7a2019-08-21 14:25:39 -0700609
David K. Bainbridge9189c632021-03-26 21:52:21 +0000610 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Zack Williamse940c7a2019-08-21 14:25:39 -0700611 defer cancel()
612
Scott Baker9173ed82020-05-19 08:30:12 -0700613 devices, err := client.ListDevices(ctx, &empty.Empty{})
Zack Williamse940c7a2019-08-21 14:25:39 -0700614 if err != nil {
615 return nil
616 }
617
618 list := make([]flags.Completion, 0)
Scott Baker9173ed82020-05-19 08:30:12 -0700619 for _, item := range devices.Items {
620 if strings.HasPrefix(item.Id, match) {
621 list = append(list, flags.Completion{Item: item.Id})
Zack Williamse940c7a2019-08-21 14:25:39 -0700622 }
623 }
624
625 return list
626}
627
628func (options *DeviceList) Execute(args []string) error {
629
630 conn, err := NewConnection()
631 if err != nil {
632 return err
633 }
634 defer conn.Close()
635
Scott Baker9173ed82020-05-19 08:30:12 -0700636 client := voltha.NewVolthaServiceClient(conn)
Zack Williamse940c7a2019-08-21 14:25:39 -0700637
David K. Bainbridge9189c632021-03-26 21:52:21 +0000638 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Zack Williamse940c7a2019-08-21 14:25:39 -0700639 defer cancel()
640
Scott Baker9173ed82020-05-19 08:30:12 -0700641 devices, err := client.ListDevices(ctx, &empty.Empty{})
Zack Williamse940c7a2019-08-21 14:25:39 -0700642 if err != nil {
643 return err
644 }
645
646 outputFormat := CharReplacer.Replace(options.Format)
647 if outputFormat == "" {
David Bainbridgea6722342019-10-24 23:55:53 +0000648 outputFormat = GetCommandOptionWithDefault("device-list", "format", DEFAULT_DEVICE_FORMAT)
Zack Williamse940c7a2019-08-21 14:25:39 -0700649 }
650 if options.Quiet {
651 outputFormat = "{{.Id}}"
652 }
653
David Bainbridgea6722342019-10-24 23:55:53 +0000654 orderBy := options.OrderBy
655 if orderBy == "" {
656 orderBy = GetCommandOptionWithDefault("device-list", "order", "")
657 }
658
Scott Baker9173ed82020-05-19 08:30:12 -0700659 // Make sure json output prints an empty list, not "null"
660 if devices.Items == nil {
661 devices.Items = make([]*voltha.Device, 0)
Zack Williamse940c7a2019-08-21 14:25:39 -0700662 }
663
664 result := CommandResult{
665 Format: format.Format(outputFormat),
666 Filter: options.Filter,
David Bainbridgea6722342019-10-24 23:55:53 +0000667 OrderBy: orderBy,
Zack Williamse940c7a2019-08-21 14:25:39 -0700668 OutputAs: toOutputType(options.OutputAs),
669 NameLimit: options.NameLimit,
Scott Baker9173ed82020-05-19 08:30:12 -0700670 Data: devices.Items,
Zack Williamse940c7a2019-08-21 14:25:39 -0700671 }
672
673 GenerateOutput(&result)
674 return nil
675}
676
677func (options *DeviceCreate) Execute(args []string) error {
678
Scott Baker9173ed82020-05-19 08:30:12 -0700679 device := voltha.Device{}
Zack Williamse940c7a2019-08-21 14:25:39 -0700680 if options.HostAndPort != "" {
Scott Baker9173ed82020-05-19 08:30:12 -0700681 device.Address = &voltha.Device_HostAndPort{HostAndPort: options.HostAndPort}
Zack Williamse940c7a2019-08-21 14:25:39 -0700682 } else if options.IPAddress != "" {
Scott Baker9173ed82020-05-19 08:30:12 -0700683 device.Address = &voltha.Device_Ipv4Address{Ipv4Address: options.IPAddress}
Hardik Windlassce1de342020-02-04 21:58:07 +0000684 }
685 if options.MACAddress != "" {
Scott Baker9173ed82020-05-19 08:30:12 -0700686 device.MacAddress = strings.ToLower(options.MACAddress)
Zack Williamse940c7a2019-08-21 14:25:39 -0700687 }
688 if options.DeviceType != "" {
Scott Baker9173ed82020-05-19 08:30:12 -0700689 device.Type = options.DeviceType
Zack Williamse940c7a2019-08-21 14:25:39 -0700690 }
691
692 conn, err := NewConnection()
693 if err != nil {
694 return err
695 }
696 defer conn.Close()
697
Scott Baker9173ed82020-05-19 08:30:12 -0700698 client := voltha.NewVolthaServiceClient(conn)
Zack Williamse940c7a2019-08-21 14:25:39 -0700699
David K. Bainbridge9189c632021-03-26 21:52:21 +0000700 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Zack Williamse940c7a2019-08-21 14:25:39 -0700701 defer cancel()
702
Scott Baker9173ed82020-05-19 08:30:12 -0700703 createdDevice, err := client.CreateDevice(ctx, &device)
Zack Williamse940c7a2019-08-21 14:25:39 -0700704 if err != nil {
705 return err
Zack Williamse940c7a2019-08-21 14:25:39 -0700706 }
707
Scott Baker9173ed82020-05-19 08:30:12 -0700708 fmt.Printf("%s\n", createdDevice.Id)
Zack Williamse940c7a2019-08-21 14:25:39 -0700709
710 return nil
711}
712
713func (options *DeviceDelete) Execute(args []string) error {
714
715 conn, err := NewConnection()
716 if err != nil {
717 return err
718 }
719 defer conn.Close()
720
Scott Baker9173ed82020-05-19 08:30:12 -0700721 client := voltha.NewVolthaServiceClient(conn)
David Bainbridge7052fe82020-03-25 10:37:00 -0700722 var lastErr error
Zack Williamse940c7a2019-08-21 14:25:39 -0700723 for _, i := range options.Args.Ids {
David K. Bainbridge9189c632021-03-26 21:52:21 +0000724 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Zack Williamse940c7a2019-08-21 14:25:39 -0700725 defer cancel()
726
Scott Baker9173ed82020-05-19 08:30:12 -0700727 id := voltha.ID{Id: string(i)}
Himani Chawla9933ddc2020-10-12 23:53:27 +0530728 if options.Force {
729 _, err = client.ForceDeleteDevice(ctx, &id)
730 } else {
731 _, err = client.DeleteDevice(ctx, &id)
732 }
Scott Baker9173ed82020-05-19 08:30:12 -0700733
Zack Williamse940c7a2019-08-21 14:25:39 -0700734 if err != nil {
David Bainbridge0f758d42019-10-26 05:17:48 +0000735 Error.Printf("Error while deleting '%s': %s\n", i, err)
David Bainbridge7052fe82020-03-25 10:37:00 -0700736 lastErr = err
Zack Williamse940c7a2019-08-21 14:25:39 -0700737 continue
Zack Williamse940c7a2019-08-21 14:25:39 -0700738 }
739 fmt.Printf("%s\n", i)
740 }
741
David Bainbridge7052fe82020-03-25 10:37:00 -0700742 if lastErr != nil {
743 return NoReportErr
744 }
Zack Williamse940c7a2019-08-21 14:25:39 -0700745 return nil
746}
747
748func (options *DeviceEnable) Execute(args []string) error {
749 conn, err := NewConnection()
750 if err != nil {
751 return err
752 }
753 defer conn.Close()
754
Scott Baker9173ed82020-05-19 08:30:12 -0700755 client := voltha.NewVolthaServiceClient(conn)
Zack Williamse940c7a2019-08-21 14:25:39 -0700756
David Bainbridge7052fe82020-03-25 10:37:00 -0700757 var lastErr error
Zack Williamse940c7a2019-08-21 14:25:39 -0700758 for _, i := range options.Args.Ids {
David K. Bainbridge9189c632021-03-26 21:52:21 +0000759 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Zack Williamse940c7a2019-08-21 14:25:39 -0700760 defer cancel()
761
Scott Baker9173ed82020-05-19 08:30:12 -0700762 id := voltha.ID{Id: string(i)}
763
764 _, err := client.EnableDevice(ctx, &id)
Zack Williamse940c7a2019-08-21 14:25:39 -0700765 if err != nil {
David Bainbridge0f758d42019-10-26 05:17:48 +0000766 Error.Printf("Error while enabling '%s': %s\n", i, err)
David Bainbridge7052fe82020-03-25 10:37:00 -0700767 lastErr = err
Zack Williamse940c7a2019-08-21 14:25:39 -0700768 continue
Zack Williamse940c7a2019-08-21 14:25:39 -0700769 }
770 fmt.Printf("%s\n", i)
771 }
772
David Bainbridge7052fe82020-03-25 10:37:00 -0700773 if lastErr != nil {
774 return NoReportErr
775 }
Zack Williamse940c7a2019-08-21 14:25:39 -0700776 return nil
777}
778
779func (options *DeviceDisable) Execute(args []string) error {
780 conn, err := NewConnection()
781 if err != nil {
782 return err
783 }
784 defer conn.Close()
785
Scott Baker9173ed82020-05-19 08:30:12 -0700786 client := voltha.NewVolthaServiceClient(conn)
Zack Williamse940c7a2019-08-21 14:25:39 -0700787
David Bainbridge7052fe82020-03-25 10:37:00 -0700788 var lastErr error
Zack Williamse940c7a2019-08-21 14:25:39 -0700789 for _, i := range options.Args.Ids {
David K. Bainbridge9189c632021-03-26 21:52:21 +0000790 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Zack Williamse940c7a2019-08-21 14:25:39 -0700791 defer cancel()
792
Scott Baker9173ed82020-05-19 08:30:12 -0700793 id := voltha.ID{Id: string(i)}
794
795 _, err := client.DisableDevice(ctx, &id)
Zack Williamse940c7a2019-08-21 14:25:39 -0700796 if err != nil {
David Bainbridge0f758d42019-10-26 05:17:48 +0000797 Error.Printf("Error while disabling '%s': %s\n", i, err)
David Bainbridge7052fe82020-03-25 10:37:00 -0700798 lastErr = err
Zack Williamse940c7a2019-08-21 14:25:39 -0700799 continue
Zack Williamse940c7a2019-08-21 14:25:39 -0700800 }
801 fmt.Printf("%s\n", i)
802 }
803
David Bainbridge7052fe82020-03-25 10:37:00 -0700804 if lastErr != nil {
805 return NoReportErr
806 }
Zack Williamse940c7a2019-08-21 14:25:39 -0700807 return nil
808}
809
810func (options *DeviceReboot) Execute(args []string) error {
811 conn, err := NewConnection()
812 if err != nil {
813 return err
814 }
815 defer conn.Close()
816
Scott Baker9173ed82020-05-19 08:30:12 -0700817 client := voltha.NewVolthaServiceClient(conn)
Zack Williamse940c7a2019-08-21 14:25:39 -0700818
David Bainbridge7052fe82020-03-25 10:37:00 -0700819 var lastErr error
Zack Williamse940c7a2019-08-21 14:25:39 -0700820 for _, i := range options.Args.Ids {
David K. Bainbridge9189c632021-03-26 21:52:21 +0000821 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Zack Williamse940c7a2019-08-21 14:25:39 -0700822 defer cancel()
823
Scott Baker9173ed82020-05-19 08:30:12 -0700824 id := voltha.ID{Id: string(i)}
825
826 _, err := client.RebootDevice(ctx, &id)
Zack Williamse940c7a2019-08-21 14:25:39 -0700827 if err != nil {
David Bainbridge0f758d42019-10-26 05:17:48 +0000828 Error.Printf("Error while rebooting '%s': %s\n", i, err)
David Bainbridge7052fe82020-03-25 10:37:00 -0700829 lastErr = err
Zack Williamse940c7a2019-08-21 14:25:39 -0700830 continue
Zack Williamse940c7a2019-08-21 14:25:39 -0700831 }
832 fmt.Printf("%s\n", i)
833 }
834
David Bainbridge7052fe82020-03-25 10:37:00 -0700835 if lastErr != nil {
836 return NoReportErr
837 }
Zack Williamse940c7a2019-08-21 14:25:39 -0700838 return nil
839}
840
841func (options *DevicePortList) Execute(args []string) error {
842
843 conn, err := NewConnection()
844 if err != nil {
845 return err
846 }
847 defer conn.Close()
848
Scott Baker9173ed82020-05-19 08:30:12 -0700849 client := voltha.NewVolthaServiceClient(conn)
Zack Williamse940c7a2019-08-21 14:25:39 -0700850
David K. Bainbridge9189c632021-03-26 21:52:21 +0000851 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Zack Williamse940c7a2019-08-21 14:25:39 -0700852 defer cancel()
853
Scott Baker9173ed82020-05-19 08:30:12 -0700854 id := voltha.ID{Id: string(options.Args.Id)}
Zack Williamse940c7a2019-08-21 14:25:39 -0700855
Scott Baker9173ed82020-05-19 08:30:12 -0700856 ports, err := client.ListDevicePorts(ctx, &id)
Zack Williamse940c7a2019-08-21 14:25:39 -0700857 if err != nil {
858 return err
859 }
860
861 outputFormat := CharReplacer.Replace(options.Format)
862 if outputFormat == "" {
David Bainbridgea6722342019-10-24 23:55:53 +0000863 outputFormat = GetCommandOptionWithDefault("device-ports", "format", DEFAULT_DEVICE_PORTS_FORMAT)
Zack Williamse940c7a2019-08-21 14:25:39 -0700864 }
Zack Williamse940c7a2019-08-21 14:25:39 -0700865
David Bainbridgea6722342019-10-24 23:55:53 +0000866 orderBy := options.OrderBy
867 if orderBy == "" {
868 orderBy = GetCommandOptionWithDefault("device-ports", "order", "")
869 }
870
Zack Williamse940c7a2019-08-21 14:25:39 -0700871 result := CommandResult{
872 Format: format.Format(outputFormat),
873 Filter: options.Filter,
David Bainbridgea6722342019-10-24 23:55:53 +0000874 OrderBy: orderBy,
Zack Williamse940c7a2019-08-21 14:25:39 -0700875 OutputAs: toOutputType(options.OutputAs),
876 NameLimit: options.NameLimit,
Scott Baker9173ed82020-05-19 08:30:12 -0700877 Data: ports.Items,
Zack Williamse940c7a2019-08-21 14:25:39 -0700878 }
879
880 GenerateOutput(&result)
881 return nil
882}
883
884func (options *DeviceFlowList) Execute(args []string) error {
885 fl := &FlowList{}
886 fl.ListOutputOptions = options.ListOutputOptions
Maninder045921e2020-09-29 16:46:02 +0530887 fl.FlowIdOptions = options.FlowIdOptions
Zack Williamse940c7a2019-08-21 14:25:39 -0700888 fl.Args.Id = string(options.Args.Id)
David Bainbridgea6722342019-10-24 23:55:53 +0000889 fl.Method = "device-flows"
Zack Williamse940c7a2019-08-21 14:25:39 -0700890 return fl.Execute(args)
891}
892
893func (options *DeviceInspect) Execute(args []string) error {
894 if len(args) > 0 {
895 return fmt.Errorf("only a single argument 'DEVICE_ID' can be provided")
896 }
897
898 conn, err := NewConnection()
899 if err != nil {
900 return err
901 }
902 defer conn.Close()
903
Scott Baker9173ed82020-05-19 08:30:12 -0700904 client := voltha.NewVolthaServiceClient(conn)
Zack Williamse940c7a2019-08-21 14:25:39 -0700905
David K. Bainbridge9189c632021-03-26 21:52:21 +0000906 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Zack Williamse940c7a2019-08-21 14:25:39 -0700907 defer cancel()
908
Scott Baker9173ed82020-05-19 08:30:12 -0700909 id := voltha.ID{Id: string(options.Args.Id)}
Zack Williamse940c7a2019-08-21 14:25:39 -0700910
Scott Baker9173ed82020-05-19 08:30:12 -0700911 device, err := client.GetDevice(ctx, &id)
Zack Williamse940c7a2019-08-21 14:25:39 -0700912 if err != nil {
913 return err
914 }
915
Zack Williamse940c7a2019-08-21 14:25:39 -0700916 outputFormat := CharReplacer.Replace(options.Format)
917 if outputFormat == "" {
David Bainbridgea6722342019-10-24 23:55:53 +0000918 outputFormat = GetCommandOptionWithDefault("device-inspect", "format", DEFAULT_DEVICE_INSPECT_FORMAT)
Zack Williamse940c7a2019-08-21 14:25:39 -0700919 }
920 if options.Quiet {
921 outputFormat = "{{.Id}}"
922 }
923
924 result := CommandResult{
925 Format: format.Format(outputFormat),
926 OutputAs: toOutputType(options.OutputAs),
927 NameLimit: options.NameLimit,
928 Data: device,
929 }
930 GenerateOutput(&result)
931 return nil
932}
kesavand12cd8eb2020-01-20 22:25:22 -0500933
934/*Device Port Enable */
935func (options *DevicePortEnable) Execute(args []string) error {
936 conn, err := NewConnection()
937 if err != nil {
938 return err
939 }
940 defer conn.Close()
941
Scott Baker9173ed82020-05-19 08:30:12 -0700942 client := voltha.NewVolthaServiceClient(conn)
kesavand12cd8eb2020-01-20 22:25:22 -0500943
David K. Bainbridge9189c632021-03-26 21:52:21 +0000944 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
kesavand12cd8eb2020-01-20 22:25:22 -0500945 defer cancel()
946
Scott Baker9173ed82020-05-19 08:30:12 -0700947 port := voltha.Port{DeviceId: string(options.Args.Id), PortNo: uint32(options.Args.PortId)}
948
949 _, err = client.EnablePort(ctx, &port)
kesavand12cd8eb2020-01-20 22:25:22 -0500950 if err != nil {
951 Error.Printf("Error enabling port number %v on device Id %s,err=%s\n", options.Args.PortId, options.Args.Id, ErrorToString(err))
952 return err
kesavand12cd8eb2020-01-20 22:25:22 -0500953 }
954
955 return nil
956}
957
Scott Baker9173ed82020-05-19 08:30:12 -0700958/*Device Port Disable */
kesavand12cd8eb2020-01-20 22:25:22 -0500959func (options *DevicePortDisable) Execute(args []string) error {
960 conn, err := NewConnection()
961 if err != nil {
962 return err
963 }
964 defer conn.Close()
965
Scott Baker9173ed82020-05-19 08:30:12 -0700966 client := voltha.NewVolthaServiceClient(conn)
967
David K. Bainbridge9189c632021-03-26 21:52:21 +0000968 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
kesavand12cd8eb2020-01-20 22:25:22 -0500969 defer cancel()
970
Scott Baker9173ed82020-05-19 08:30:12 -0700971 port := voltha.Port{DeviceId: string(options.Args.Id), PortNo: uint32(options.Args.PortId)}
972
973 _, err = client.DisablePort(ctx, &port)
kesavand12cd8eb2020-01-20 22:25:22 -0500974 if err != nil {
Scott Baker9173ed82020-05-19 08:30:12 -0700975 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 -0500976 return err
kesavand12cd8eb2020-01-20 22:25:22 -0500977 }
Scott Baker9173ed82020-05-19 08:30:12 -0700978
kesavand12cd8eb2020-01-20 22:25:22 -0500979 return nil
980}
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -0800981
Rohan Agrawald7df3772020-06-29 11:23:36 +0000982func (options *DevicePmConfigSetMaxSkew) Execute(args []string) error {
983 conn, err := NewConnection()
984 if err != nil {
985 return err
986 }
987 defer conn.Close()
988
989 client := voltha.NewVolthaServiceClient(conn)
990
David K. Bainbridge9189c632021-03-26 21:52:21 +0000991 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Rohan Agrawald7df3772020-06-29 11:23:36 +0000992 defer cancel()
993
994 id := voltha.ID{Id: string(options.Args.Id)}
995
996 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
997 if err != nil {
998 return err
999 }
1000
1001 pmConfigs.MaxSkew = options.Args.MaxSkew
1002
1003 _, err = client.UpdateDevicePmConfigs(ctx, pmConfigs)
1004 if err != nil {
1005 return err
1006 }
1007
1008 return nil
1009}
1010
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001011func (options *DevicePmConfigsGet) Execute(args []string) error {
1012
1013 conn, err := NewConnection()
1014 if err != nil {
1015 return err
1016 }
1017 defer conn.Close()
1018
1019 client := voltha.NewVolthaServiceClient(conn)
1020
David K. Bainbridge9189c632021-03-26 21:52:21 +00001021 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001022 defer cancel()
1023
1024 id := voltha.ID{Id: string(options.Args.Id)}
1025
1026 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
1027 if err != nil {
1028 return err
1029 }
1030
1031 outputFormat := CharReplacer.Replace(options.Format)
1032 if outputFormat == "" {
1033 outputFormat = GetCommandOptionWithDefault("device-pm-configs", "format", DEFAULT_DEVICE_PM_CONFIG_GET_FORMAT)
1034 }
1035
1036 orderBy := options.OrderBy
1037 if orderBy == "" {
1038 orderBy = GetCommandOptionWithDefault("device-pm-configs", "order", "")
1039 }
1040
1041 result := CommandResult{
1042 Format: format.Format(outputFormat),
1043 Filter: options.Filter,
1044 OrderBy: orderBy,
1045 OutputAs: toOutputType(options.OutputAs),
1046 NameLimit: options.NameLimit,
1047 Data: pmConfigs,
1048 }
1049
1050 GenerateOutput(&result)
1051 return nil
1052
1053}
1054
1055func (options *DevicePmConfigMetricList) Execute(args []string) error {
1056
1057 conn, err := NewConnection()
1058 if err != nil {
1059 return err
1060 }
1061 defer conn.Close()
1062
1063 client := voltha.NewVolthaServiceClient(conn)
1064
David K. Bainbridge9189c632021-03-26 21:52:21 +00001065 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001066 defer cancel()
1067
1068 id := voltha.ID{Id: string(options.Args.Id)}
1069
1070 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
1071 if err != nil {
1072 return err
1073 }
1074
1075 if !pmConfigs.Grouped {
1076 for _, metric := range pmConfigs.Metrics {
1077 if metric.SampleFreq == 0 {
1078 metric.SampleFreq = pmConfigs.DefaultFreq
1079 }
1080 }
Rohan Agrawalbca69122020-06-17 14:59:03 +00001081 outputFormat := CharReplacer.Replace(options.Format)
1082 if outputFormat == "" {
1083 outputFormat = GetCommandOptionWithDefault("device-pm-configs", "format", DEFAULT_DEVICE_PM_CONFIG_METRIC_LIST_FORMAT)
1084 }
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001085
Rohan Agrawalbca69122020-06-17 14:59:03 +00001086 orderBy := options.OrderBy
1087 if orderBy == "" {
1088 orderBy = GetCommandOptionWithDefault("device-pm-configs", "order", "")
1089 }
1090
1091 result := CommandResult{
1092 Format: format.Format(outputFormat),
1093 Filter: options.Filter,
1094 OrderBy: orderBy,
1095 OutputAs: toOutputType(options.OutputAs),
1096 NameLimit: options.NameLimit,
1097 Data: pmConfigs.Metrics,
1098 }
1099
1100 GenerateOutput(&result)
1101 return nil
1102 } else {
1103 return fmt.Errorf("Device '%s' does not have Non Grouped Metrics", options.Args.Id)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001104 }
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001105}
1106
1107func (options *DevicePmConfigMetricEnable) Execute(args []string) error {
1108
1109 conn, err := NewConnection()
1110 if err != nil {
1111 return err
1112 }
1113 defer conn.Close()
1114
1115 client := voltha.NewVolthaServiceClient(conn)
1116
David K. Bainbridge9189c632021-03-26 21:52:21 +00001117 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001118 defer cancel()
1119
1120 id := voltha.ID{Id: string(options.Args.Id)}
1121
1122 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
1123 if err != nil {
1124 return err
1125 }
1126
1127 if !pmConfigs.Grouped {
Rohan Agrawalbca69122020-06-17 14:59:03 +00001128 metrics := make(map[string]struct{})
1129 for _, metric := range pmConfigs.Metrics {
1130 metrics[metric.Name] = struct{}{}
1131 }
1132
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001133 for _, metric := range pmConfigs.Metrics {
1134 for _, mName := range options.Args.Metrics {
Rohan Agrawalbca69122020-06-17 14:59:03 +00001135 if _, exist := metrics[string(mName)]; !exist {
1136 return fmt.Errorf("Metric Name '%s' does not exist", mName)
1137 }
1138
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001139 if string(mName) == metric.Name && !metric.Enabled {
1140 metric.Enabled = true
1141 _, err := client.UpdateDevicePmConfigs(ctx, pmConfigs)
1142 if err != nil {
1143 return err
1144 }
1145 }
1146 }
1147 }
Rohan Agrawalbca69122020-06-17 14:59:03 +00001148 } else {
1149 return fmt.Errorf("Device '%s' does not have Non Grouped Metrics", options.Args.Id)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001150 }
1151 return nil
1152}
1153
1154func (options *DevicePmConfigMetricDisable) Execute(args []string) error {
1155
1156 conn, err := NewConnection()
1157 if err != nil {
1158 return err
1159 }
1160 defer conn.Close()
1161
1162 client := voltha.NewVolthaServiceClient(conn)
1163
David K. Bainbridge9189c632021-03-26 21:52:21 +00001164 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001165 defer cancel()
1166
1167 id := voltha.ID{Id: string(options.Args.Id)}
1168
1169 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
1170 if err != nil {
1171 return err
1172 }
1173
1174 if !pmConfigs.Grouped {
Rohan Agrawalbca69122020-06-17 14:59:03 +00001175 metrics := make(map[string]struct{})
1176 for _, metric := range pmConfigs.Metrics {
1177 metrics[metric.Name] = struct{}{}
1178 }
1179
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001180 for _, metric := range pmConfigs.Metrics {
1181 for _, mName := range options.Args.Metrics {
Rohan Agrawalbca69122020-06-17 14:59:03 +00001182 if _, have := metrics[string(mName)]; !have {
1183 return fmt.Errorf("Metric Name '%s' does not exist", mName)
1184 }
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001185 if string(mName) == metric.Name && metric.Enabled {
1186 metric.Enabled = false
1187 _, err := client.UpdateDevicePmConfigs(ctx, pmConfigs)
1188 if err != nil {
1189 return err
1190 }
Rohan Agrawalbca69122020-06-17 14:59:03 +00001191 } else {
1192 return fmt.Errorf("Metric '%s' cannot be disabled", string(mName))
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001193 }
1194 }
1195 }
Rohan Agrawalbca69122020-06-17 14:59:03 +00001196 } else {
1197 return fmt.Errorf("Device '%s' does not have Non Grouped Metrics", options.Args.Id)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001198 }
1199 return nil
1200}
1201
1202func (options *DevicePmConfigGroupEnable) Execute(args []string) error {
1203
1204 conn, err := NewConnection()
1205 if err != nil {
1206 return err
1207 }
1208 defer conn.Close()
1209
1210 client := voltha.NewVolthaServiceClient(conn)
1211
David K. Bainbridge9189c632021-03-26 21:52:21 +00001212 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001213 defer cancel()
1214
1215 id := voltha.ID{Id: string(options.Args.Id)}
1216
1217 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
1218 if err != nil {
1219 return err
1220 }
1221
1222 if pmConfigs.Grouped {
Rohan Agrawalbca69122020-06-17 14:59:03 +00001223 groups := make(map[string]struct{})
1224 for _, group := range pmConfigs.Groups {
1225 groups[group.GroupName] = struct{}{}
1226 }
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001227 for _, group := range pmConfigs.Groups {
Girish Gowdra610acb42021-01-27 13:33:57 -08001228 if _, have := groups[string(options.Args.Group)]; !have {
1229 return fmt.Errorf("Group Name '%s' does not exist", options.Args.Group)
1230 }
1231 if string(options.Args.Group) == group.GroupName && !group.Enabled {
1232 group.Enabled = true
1233 _, err := client.UpdateDevicePmConfigs(ctx, pmConfigs)
1234 if err != nil {
1235 return err
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001236 }
1237 }
1238 }
Rohan Agrawalbca69122020-06-17 14:59:03 +00001239 } else {
1240 return fmt.Errorf("Device '%s' does not have Group Metrics", options.Args.Id)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001241 }
1242 return nil
1243}
1244
1245func (options *DevicePmConfigGroupDisable) Execute(args []string) error {
1246
1247 conn, err := NewConnection()
1248 if err != nil {
1249 return err
1250 }
1251 defer conn.Close()
1252
1253 client := voltha.NewVolthaServiceClient(conn)
1254
David K. Bainbridge9189c632021-03-26 21:52:21 +00001255 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001256 defer cancel()
1257
1258 id := voltha.ID{Id: string(options.Args.Id)}
1259
1260 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
1261 if err != nil {
1262 return err
1263 }
1264
1265 if pmConfigs.Grouped {
Rohan Agrawalbca69122020-06-17 14:59:03 +00001266 groups := make(map[string]struct{})
1267 for _, group := range pmConfigs.Groups {
1268 groups[group.GroupName] = struct{}{}
1269 }
1270
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001271 for _, group := range pmConfigs.Groups {
Girish Gowdra610acb42021-01-27 13:33:57 -08001272 if _, have := groups[string(options.Args.Group)]; !have {
1273 return fmt.Errorf("Group Name '%s' does not exist", options.Args.Group)
1274 }
Rohan Agrawalbca69122020-06-17 14:59:03 +00001275
Girish Gowdra610acb42021-01-27 13:33:57 -08001276 if string(options.Args.Group) == group.GroupName && group.Enabled {
1277 group.Enabled = false
1278 _, err := client.UpdateDevicePmConfigs(ctx, pmConfigs)
1279 if err != nil {
1280 return err
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001281 }
1282 }
1283 }
Rohan Agrawalbca69122020-06-17 14:59:03 +00001284 } else {
1285 return fmt.Errorf("Device '%s' does not have Group Metrics", options.Args.Id)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001286 }
1287 return nil
1288}
1289
Girish Gowdra610acb42021-01-27 13:33:57 -08001290func (options *DevicePmConfigGroupFrequencySet) Execute(args []string) error {
1291
1292 conn, err := NewConnection()
1293 if err != nil {
1294 return err
1295 }
1296 defer conn.Close()
1297
1298 client := voltha.NewVolthaServiceClient(conn)
1299
David K. Bainbridge9189c632021-03-26 21:52:21 +00001300 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Girish Gowdra610acb42021-01-27 13:33:57 -08001301 defer cancel()
1302
1303 id := voltha.ID{Id: string(options.Args.Id)}
1304
1305 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
1306 if err != nil {
1307 return err
1308 }
1309
1310 if pmConfigs.Grouped {
1311 groups := make(map[string]struct{})
1312 for _, group := range pmConfigs.Groups {
1313 groups[group.GroupName] = struct{}{}
1314 }
1315
1316 for _, group := range pmConfigs.Groups {
1317 if _, have := groups[string(options.Args.Group)]; !have {
1318 return fmt.Errorf("group name '%s' does not exist", options.Args.Group)
1319 }
1320
1321 if string(options.Args.Group) == group.GroupName {
1322 if !group.Enabled {
1323 return fmt.Errorf("group '%s' is not enabled", options.Args.Group)
1324 }
1325 group.GroupFreq = uint32(options.Args.Interval.Seconds())
1326 _, err = client.UpdateDevicePmConfigs(ctx, pmConfigs)
1327 if err != nil {
1328 return err
1329 }
1330 }
1331 }
1332 } else {
1333 return fmt.Errorf("device '%s' does not have group metrics", options.Args.Id)
1334 }
1335 return nil
1336}
1337
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001338func (options *DevicePmConfigGroupList) Execute(args []string) error {
1339
1340 conn, err := NewConnection()
1341 if err != nil {
1342 return err
1343 }
1344 defer conn.Close()
1345
1346 client := voltha.NewVolthaServiceClient(conn)
1347
David K. Bainbridge9189c632021-03-26 21:52:21 +00001348 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001349 defer cancel()
1350
1351 id := voltha.ID{Id: string(options.Args.Id)}
1352
1353 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
1354 if err != nil {
1355 return err
1356 }
1357
1358 if pmConfigs.Grouped {
1359 for _, group := range pmConfigs.Groups {
1360 if group.GroupFreq == 0 {
1361 group.GroupFreq = pmConfigs.DefaultFreq
1362 }
1363 }
Rohan Agrawalbca69122020-06-17 14:59:03 +00001364 outputFormat := CharReplacer.Replace(options.Format)
1365 if outputFormat == "" {
1366 outputFormat = GetCommandOptionWithDefault("device-pm-configs", "format", DEFAULT_DEVICE_PM_CONFIG_GROUP_LIST_FORMAT)
1367 }
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001368
Rohan Agrawalbca69122020-06-17 14:59:03 +00001369 orderBy := options.OrderBy
1370 if orderBy == "" {
1371 orderBy = GetCommandOptionWithDefault("device-pm-configs", "order", "")
1372 }
1373
1374 result := CommandResult{
1375 Format: format.Format(outputFormat),
1376 Filter: options.Filter,
1377 OrderBy: orderBy,
1378 OutputAs: toOutputType(options.OutputAs),
1379 NameLimit: options.NameLimit,
1380 Data: pmConfigs.Groups,
1381 }
1382
1383 GenerateOutput(&result)
1384 } else {
1385 return fmt.Errorf("Device '%s' does not have Group Metrics", string(options.Args.Id))
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001386 }
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001387 return nil
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001388}
1389
1390func (options *DevicePmConfigGroupMetricList) Execute(args []string) error {
1391
1392 var metrics []*voltha.PmConfig
1393 conn, err := NewConnection()
1394 if err != nil {
1395 return err
1396 }
1397 defer conn.Close()
1398
1399 client := voltha.NewVolthaServiceClient(conn)
1400
David K. Bainbridge9189c632021-03-26 21:52:21 +00001401 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001402 defer cancel()
1403
1404 id := voltha.ID{Id: string(options.Args.Id)}
1405
1406 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
1407 if err != nil {
1408 return err
1409 }
1410
1411 for _, groups := range pmConfigs.Groups {
1412
1413 if string(options.Args.Group) == groups.GroupName {
1414 for _, metric := range groups.Metrics {
1415 if metric.SampleFreq == 0 && groups.GroupFreq == 0 {
1416 metric.SampleFreq = pmConfigs.DefaultFreq
1417 } else {
1418 metric.SampleFreq = groups.GroupFreq
1419 }
1420 }
1421 metrics = groups.Metrics
1422 }
1423 }
1424
1425 outputFormat := CharReplacer.Replace(options.Format)
1426 if outputFormat == "" {
1427 outputFormat = GetCommandOptionWithDefault("device-pm-configs", "format", DEFAULT_DEVICE_PM_CONFIG_METRIC_LIST_FORMAT)
1428 }
1429
1430 orderBy := options.OrderBy
1431 if orderBy == "" {
1432 orderBy = GetCommandOptionWithDefault("device-pm-configs", "order", "")
1433 }
1434
1435 result := CommandResult{
1436 Format: format.Format(outputFormat),
1437 Filter: options.Filter,
1438 OrderBy: orderBy,
1439 OutputAs: toOutputType(options.OutputAs),
1440 NameLimit: options.NameLimit,
1441 Data: metrics,
1442 }
1443
1444 GenerateOutput(&result)
1445 return nil
1446
1447}
1448
1449func (options *DevicePmConfigFrequencySet) Execute(args []string) error {
1450
1451 conn, err := NewConnection()
1452 if err != nil {
1453 return err
1454 }
1455 defer conn.Close()
1456
1457 client := voltha.NewVolthaServiceClient(conn)
1458
David K. Bainbridge9189c632021-03-26 21:52:21 +00001459 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001460 defer cancel()
1461
1462 id := voltha.ID{Id: string(options.Args.Id)}
1463
1464 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
1465 if err != nil {
1466 return err
1467 }
1468
Girish Gowdra610acb42021-01-27 13:33:57 -08001469 pmConfigs.DefaultFreq = uint32(options.Args.Interval.Seconds())
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001470
1471 _, err = client.UpdateDevicePmConfigs(ctx, pmConfigs)
1472 if err != nil {
1473 return err
1474 }
1475
1476 outputFormat := CharReplacer.Replace(options.Format)
1477 if outputFormat == "" {
1478 outputFormat = GetCommandOptionWithDefault("device-pm-configs", "format", DEFAULT_DEVICE_PM_CONFIG_GET_FORMAT)
1479 }
1480 if options.Quiet {
1481 outputFormat = "{{.Id}}"
1482 }
1483
1484 result := CommandResult{
1485 Format: format.Format(outputFormat),
1486 OutputAs: toOutputType(options.OutputAs),
1487 NameLimit: options.NameLimit,
1488 Data: pmConfigs,
1489 }
1490
1491 GenerateOutput(&result)
1492 return nil
1493
1494}
1495
kesavand3e2f9f62021-04-22 11:06:38 +05301496func (options *OnuDownloadImage) Execute(args []string) error {
1497
1498 conn, err := NewConnection()
1499 if err != nil {
1500 return err
1501 }
1502 defer conn.Close()
1503
1504 client := voltha.NewVolthaServiceClient(conn)
1505
1506 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
1507 defer cancel()
1508
1509 var devIDList []*common.ID
1510 for _, i := range options.Args.IDs {
1511
1512 devIDList = append(devIDList, &common.ID{Id: string(i)})
1513 }
1514
1515 downloadImage := voltha.DeviceImageDownloadRequest{
1516 DeviceId: devIDList,
1517 Image: &voltha.Image{
1518 Url: options.Args.Url,
1519 Crc32: options.Args.Crc,
ssiddiqui7bc89e92021-05-20 20:58:02 +05301520 Vendor: options.Args.Vendor,
kesavand3e2f9f62021-04-22 11:06:38 +05301521 Version: options.Args.ImageVersion,
1522 },
1523 ActivateOnSuccess: options.Args.ActivateOnSuccess,
1524 CommitOnSuccess: options.Args.CommitOnSuccess,
1525 }
1526
1527 _, err = client.DownloadImageToDevice(ctx, &downloadImage)
1528 if err != nil {
1529 return err
1530 }
1531
1532 return nil
1533
1534}
1535
1536func (options *OnuActivateImage) Execute(args []string) error {
1537
1538 conn, err := NewConnection()
1539 if err != nil {
1540 return err
1541 }
1542 defer conn.Close()
1543
1544 client := voltha.NewVolthaServiceClient(conn)
1545
1546 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
1547 defer cancel()
1548
1549 var devIDList []*common.ID
1550 for _, i := range options.Args.IDs {
1551
1552 devIDList = append(devIDList, &common.ID{Id: string(i)})
1553 }
1554
1555 downloadImage := voltha.DeviceImageRequest{
1556 DeviceId: devIDList,
1557 Version: options.Args.ImageVersion,
1558 CommitOnSuccess: options.Args.CommitOnSuccess,
1559 }
1560
1561 _, err = client.ActivateImage(ctx, &downloadImage)
1562 if err != nil {
1563 return err
1564 }
1565
1566 return nil
1567
1568}
1569
1570func (options *OnuAbortUpgradeImage) Execute(args []string) error {
1571
1572 conn, err := NewConnection()
1573 if err != nil {
1574 return err
1575 }
1576 defer conn.Close()
1577
1578 client := voltha.NewVolthaServiceClient(conn)
1579
1580 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
1581 defer cancel()
1582
1583 var devIDList []*common.ID
1584 for _, i := range options.Args.IDs {
1585
1586 devIDList = append(devIDList, &common.ID{Id: string(i)})
1587 }
1588
1589 downloadImage := voltha.DeviceImageRequest{
1590 DeviceId: devIDList,
1591 Version: options.Args.ImageVersion,
1592 }
1593
1594 _, err = client.AbortImageUpgradeToDevice(ctx, &downloadImage)
1595 if err != nil {
1596 return err
1597 }
1598
1599 return nil
1600
1601}
1602
1603func (options *OnuCommitImage) Execute(args []string) error {
1604
1605 conn, err := NewConnection()
1606 if err != nil {
1607 return err
1608 }
1609 defer conn.Close()
1610
1611 client := voltha.NewVolthaServiceClient(conn)
1612
1613 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
1614 defer cancel()
1615
1616 var devIDList []*common.ID
1617 for _, i := range options.Args.IDs {
1618
1619 devIDList = append(devIDList, &common.ID{Id: string(i)})
1620 }
1621 downloadImage := voltha.DeviceImageRequest{
1622 DeviceId: devIDList,
1623 Version: options.Args.ImageVersion,
1624 }
1625
1626 _, err = client.CommitImage(ctx, &downloadImage)
1627 if err != nil {
1628 return err
1629 }
1630
1631 return nil
1632
1633}
1634
1635func (options *OnuListImages) Execute(args []string) error {
1636
1637 conn, err := NewConnection()
1638 if err != nil {
1639 return err
1640 }
1641 defer conn.Close()
1642
1643 client := voltha.NewVolthaServiceClient(conn)
1644
1645 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
1646 defer cancel()
1647
1648 id := common.ID{Id: string(options.Args.Id)}
1649
1650 onuImages, err := client.GetOnuImages(ctx, &id)
1651 if err != nil {
1652 return err
1653 }
1654
1655 outputFormat := CharReplacer.Replace(options.Format)
1656 if outputFormat == "" {
1657 outputFormat = GetCommandOptionWithDefault("onu-image-list", "format", ONU_IMAGE_LIST_FORMAT)
1658 }
1659
1660 if options.Quiet {
1661 outputFormat = "{{.Id}}"
1662 }
1663
1664 //TODO orderby
1665
1666 // Make sure json output prints an empty list, not "null"
1667 if onuImages.Items == nil {
1668 onuImages.Items = make([]*voltha.OnuImage, 0)
1669 }
1670
1671 result := CommandResult{
1672 Format: format.Format(outputFormat),
1673 OutputAs: toOutputType(options.OutputAs),
1674 NameLimit: options.NameLimit,
1675 Data: onuImages.Items,
1676 }
1677
1678 GenerateOutput(&result)
1679 return nil
1680
1681}
1682
1683func (options *OnuImageStatus) Execute(args []string) error {
1684
1685 conn, err := NewConnection()
1686 if err != nil {
1687 return err
1688 }
1689 defer conn.Close()
1690
1691 client := voltha.NewVolthaServiceClient(conn)
1692
1693 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
1694 defer cancel()
1695
1696 var devIDList []*common.ID
1697 for _, i := range options.Args.IDs {
1698
1699 devIDList = append(devIDList, &common.ID{Id: string(i)})
1700 }
1701
1702 imageStatusReq := voltha.DeviceImageRequest{
1703 DeviceId: devIDList,
1704 Version: options.Args.ImageVersion,
1705 }
1706 imageStatus, err := client.GetImageStatus(ctx, &imageStatusReq)
1707 if err != nil {
1708 return err
1709 }
1710
1711 outputFormat := CharReplacer.Replace(options.Format)
1712 if outputFormat == "" {
1713 outputFormat = GetCommandOptionWithDefault("device-image-list", "format", ONU_IMAGE_STATUS_FORMAT)
1714 }
1715
1716 if options.Quiet {
1717 outputFormat = "{{.Id}}"
1718 }
1719
1720 //TODO orderby
1721
1722 // Make sure json output prints an empty list, not "null"
1723 if imageStatus.DeviceImageStates == nil {
1724 imageStatus.DeviceImageStates = make([]*voltha.DeviceImageState, 0)
1725 }
1726
1727 result := CommandResult{
1728 Format: format.Format(outputFormat),
1729 OutputAs: toOutputType(options.OutputAs),
1730 NameLimit: options.NameLimit,
1731 Data: imageStatus.DeviceImageStates,
1732 }
1733
1734 GenerateOutput(&result)
1735 return nil
1736
1737}
1738
Andrea Campanella791d88b2021-01-08 13:29:00 +01001739func (options *DeviceOnuListImages) Execute(args []string) error {
1740
1741 conn, err := NewConnection()
1742 if err != nil {
1743 return err
1744 }
1745 defer conn.Close()
1746
1747 client := voltha.NewVolthaServiceClient(conn)
1748
David K. Bainbridge9189c632021-03-26 21:52:21 +00001749 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Andrea Campanella791d88b2021-01-08 13:29:00 +01001750 defer cancel()
1751
1752 id := common.ID{Id: string(options.Args.Id)}
1753
1754 imageDownloads, err := client.ListImageDownloads(ctx, &id)
1755 if err != nil {
1756 return err
1757 }
1758
1759 outputFormat := CharReplacer.Replace(options.Format)
1760 if outputFormat == "" {
1761 outputFormat = GetCommandOptionWithDefault("device-image-list", "format", DEFAULT_DEVICE_IMAGE_LIST_GET_FORMAT)
1762 }
1763
1764 if options.Quiet {
1765 outputFormat = "{{.Id}}"
1766 }
1767
1768 //TODO orderby
1769
1770 // Make sure json output prints an empty list, not "null"
1771 if imageDownloads.Items == nil {
1772 imageDownloads.Items = make([]*voltha.ImageDownload, 0)
1773 }
1774
1775 result := CommandResult{
1776 Format: format.Format(outputFormat),
1777 OutputAs: toOutputType(options.OutputAs),
1778 NameLimit: options.NameLimit,
1779 Data: imageDownloads.Items,
1780 }
1781
1782 GenerateOutput(&result)
1783 return nil
1784
1785}
1786
1787func (options *DeviceOnuDownloadImage) Execute(args []string) error {
1788
1789 conn, err := NewConnection()
1790 if err != nil {
1791 return err
1792 }
1793 defer conn.Close()
1794
1795 client := voltha.NewVolthaServiceClient(conn)
1796
David K. Bainbridge9189c632021-03-26 21:52:21 +00001797 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Andrea Campanella791d88b2021-01-08 13:29:00 +01001798 defer cancel()
1799
1800 downloadImage := voltha.ImageDownload{
1801 Id: string(options.Args.Id),
1802 Name: options.Args.Name,
1803 Url: options.Args.Url,
1804 Crc: options.Args.Crc,
1805 LocalDir: options.Args.LocalDir,
1806 }
1807
1808 _, err = client.DownloadImage(ctx, &downloadImage)
1809 if err != nil {
1810 return err
1811 }
1812
1813 return nil
1814
1815}
1816
1817func (options *DeviceOnuActivateImageUpdate) Execute(args []string) error {
1818
1819 conn, err := NewConnection()
1820 if err != nil {
1821 return err
1822 }
1823 defer conn.Close()
1824
1825 client := voltha.NewVolthaServiceClient(conn)
1826
David K. Bainbridge9189c632021-03-26 21:52:21 +00001827 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Andrea Campanella791d88b2021-01-08 13:29:00 +01001828 defer cancel()
1829
1830 downloadImage := voltha.ImageDownload{
1831 Id: string(options.Args.Id),
1832 Name: options.Args.Name,
1833 ImageVersion: options.Args.ImageVersion,
1834 SaveConfig: options.Args.SaveConfig,
1835 LocalDir: options.Args.LocalDir,
1836 }
1837
1838 _, err = client.ActivateImageUpdate(ctx, &downloadImage)
1839 if err != nil {
1840 return err
1841 }
1842
1843 return nil
1844
1845}
1846
Scott Baker9173ed82020-05-19 08:30:12 -07001847type ReturnValueRow struct {
1848 Name string `json:"name"`
1849 Result interface{} `json:"result"`
1850}
1851
kesavand8ec4fc02021-01-27 09:10:22 -05001852func (options *DeviceGetPortStats) Execute(args []string) error {
1853 conn, err := NewConnection()
1854 if err != nil {
1855 return err
1856 }
1857 defer conn.Close()
1858 client := extension.NewExtensionClient(conn)
1859 var portType extension.GetOltPortCounters_PortType
1860
1861 if options.Args.PortType == "pon" {
1862 portType = extension.GetOltPortCounters_Port_PON_OLT
1863 } else if options.Args.PortType == "nni" {
1864
1865 portType = extension.GetOltPortCounters_Port_ETHERNET_NNI
1866 } else {
1867 return fmt.Errorf("expected interface type pon/nni, provided %s", options.Args.PortType)
1868 }
1869
1870 singleGetValReq := extension.SingleGetValueRequest{
1871 TargetId: string(options.Args.Id),
1872 Request: &extension.GetValueRequest{
1873 Request: &extension.GetValueRequest_OltPortInfo{
1874 OltPortInfo: &extension.GetOltPortCounters{
1875 PortNo: options.Args.PortNo,
1876 PortType: portType,
1877 },
1878 },
1879 },
1880 }
1881
David K. Bainbridge9189c632021-03-26 21:52:21 +00001882 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
kesavand8ec4fc02021-01-27 09:10:22 -05001883 defer cancel()
1884 rv, err := client.GetExtValue(ctx, &singleGetValReq)
1885 if err != nil {
1886 Error.Printf("Error getting value on device Id %s,err=%s\n", options.Args.Id, ErrorToString(err))
1887 return err
1888 }
1889
1890 if rv.Response.Status != extension.GetValueResponse_OK {
1891 return fmt.Errorf("failed to get port stats %v", rv.Response.ErrReason.String())
1892 }
1893
1894 outputFormat := CharReplacer.Replace(options.Format)
1895 if outputFormat == "" {
1896 outputFormat = GetCommandOptionWithDefault("device-get-port-status", "format", DEFAULT_DEVICE_GET_PORT_STATUS_FORMAT)
1897 }
1898
1899 result := CommandResult{
1900 Format: format.Format(outputFormat),
1901 OutputAs: toOutputType(options.OutputAs),
1902 NameLimit: options.NameLimit,
1903 Data: rv.GetResponse().GetPortCoutners(),
1904 }
1905 GenerateOutput(&result)
1906 return nil
1907}
1908
Himani Chawla40acc122021-05-26 18:52:29 +05301909func (options *GetOnuStats) Execute(args []string) error {
1910 conn, err := NewConnection()
1911 if err != nil {
1912 return err
1913 }
1914 defer conn.Close()
1915 client := extension.NewExtensionClient(conn)
1916
1917 singleGetValReq := extension.SingleGetValueRequest{
1918 TargetId: string(options.Args.OltId),
1919 Request: &extension.GetValueRequest{
1920 Request: &extension.GetValueRequest_OnuPonInfo{
1921 OnuPonInfo: &extension.GetOnuCountersRequest{
1922 IntfId: options.Args.IntfId,
1923 OnuId: options.Args.OnuId,
1924 },
1925 },
1926 },
1927 }
1928 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
1929 defer cancel()
1930 rv, err := client.GetExtValue(ctx, &singleGetValReq)
1931 if err != nil {
1932 Error.Printf("Error getting value on device Id %s,err=%s\n", options.Args.OltId, ErrorToString(err))
1933 return err
1934 }
1935
1936 if rv.Response.Status != extension.GetValueResponse_OK {
1937 return fmt.Errorf("failed to get onu stats %v", rv.Response.ErrReason.String())
1938 }
1939 outputFormat := CharReplacer.Replace(options.Format)
1940 data, formatStr := buildOnuStatsOutputFormat(rv.GetResponse().GetOnuPonCounters())
1941 if outputFormat == "" {
1942 outputFormat = GetCommandOptionWithDefault("device-get-onu-status", "format", formatStr)
1943 }
1944
1945 result := CommandResult{
1946 Format: format.Format(outputFormat),
1947 OutputAs: toOutputType(options.OutputAs),
1948 NameLimit: options.NameLimit,
1949 Data: data,
1950 }
1951 GenerateOutput(&result)
1952 return nil
1953}
1954
kesavand6d1131f2021-02-05 22:38:15 +05301955func (options *UniStatus) Execute(args []string) error {
1956 conn, err := NewConnection()
1957 if err != nil {
1958 return err
1959 }
1960 defer conn.Close()
1961 client := extension.NewExtensionClient(conn)
1962
1963 singleGetValReq := extension.SingleGetValueRequest{
1964 TargetId: string(options.Args.Id),
1965 Request: &extension.GetValueRequest{
1966 Request: &extension.GetValueRequest_UniInfo{
1967 UniInfo: &extension.GetOnuUniInfoRequest{
1968 UniIndex: options.Args.UniIndex,
1969 },
1970 },
1971 },
1972 }
David K. Bainbridge9189c632021-03-26 21:52:21 +00001973 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
kesavand6d1131f2021-02-05 22:38:15 +05301974 defer cancel()
1975 rv, err := client.GetExtValue(ctx, &singleGetValReq)
1976 if err != nil {
1977 Error.Printf("Error getting value on device Id %s,err=%s\n", options.Args.Id, ErrorToString(err))
1978 return err
1979 }
1980 if rv.Response.Status != extension.GetValueResponse_OK {
1981 return fmt.Errorf("failed to get uni status %v", rv.Response.ErrReason.String())
1982 }
1983 outputFormat := CharReplacer.Replace(options.Format)
1984 if outputFormat == "" {
1985 outputFormat = GetCommandOptionWithDefault("device-get-uni-status", "format", DEFAULT_DEVICE_GET_UNI_STATUS_FORMAT)
1986 }
1987 result := CommandResult{
1988 Format: format.Format(outputFormat),
1989 OutputAs: toOutputType(options.OutputAs),
1990 NameLimit: options.NameLimit,
1991 Data: rv.GetResponse().GetUniInfo(),
1992 }
1993 GenerateOutput(&result)
1994 return nil
1995}
1996
Girish Gowdra4f5ce7c2021-04-29 18:53:21 -07001997func (options *OnuPonOpticalInfo) Execute(args []string) error {
1998 conn, err := NewConnection()
1999 if err != nil {
2000 return err
2001 }
2002 defer conn.Close()
2003 client := extension.NewExtensionClient(conn)
2004
2005 singleGetValReq := extension.SingleGetValueRequest{
2006 TargetId: string(options.Args.Id),
2007 Request: &extension.GetValueRequest{
2008 Request: &extension.GetValueRequest_OnuOpticalInfo{
2009 OnuOpticalInfo: &extension.GetOnuPonOpticalInfo{},
2010 },
2011 },
2012 }
2013 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
2014 defer cancel()
2015 rv, err := client.GetExtValue(ctx, &singleGetValReq)
2016 if err != nil {
2017 Error.Printf("Error getting value on device Id %s,err=%s\n", options.Args.Id, ErrorToString(err))
2018 return err
2019 }
2020 if rv.Response.Status != extension.GetValueResponse_OK {
2021 return fmt.Errorf("failed to get onu pon optical info %v", rv.Response.ErrReason.String())
2022 }
2023 outputFormat := CharReplacer.Replace(options.Format)
2024 if outputFormat == "" {
2025 outputFormat = GetCommandOptionWithDefault("device-get-onu-pon-optical-info", "format", DEFAULT_ONU_PON_OPTICAL_INFO_STATUS_FORMAT)
2026 }
2027 result := CommandResult{
2028 Format: format.Format(outputFormat),
2029 OutputAs: toOutputType(options.OutputAs),
2030 NameLimit: options.NameLimit,
2031 Data: rv.GetResponse().GetOnuOpticalInfo(),
2032 }
2033 GenerateOutput(&result)
2034 return nil
2035}
2036
Gamze Abakac857a462021-05-26 13:45:54 +00002037func (options *RxPower) Execute(args []string) error {
2038 conn, err := NewConnection()
2039 if err != nil {
2040 return err
2041 }
2042 defer conn.Close()
2043 client := extension.NewExtensionClient(conn)
2044
2045 singleGetValReq := extension.SingleGetValueRequest{
2046 TargetId: string(options.Args.Id),
2047 Request: &extension.GetValueRequest{
2048 Request: &extension.GetValueRequest_RxPower{
2049 RxPower: &extension.GetRxPowerRequest{
2050 IntfId: options.Args.PortNo,
2051 OnuId: options.Args.OnuNo,
2052 },
2053 },
2054 },
2055 }
2056
2057 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
2058 defer cancel()
2059 rv, err := client.GetExtValue(ctx, &singleGetValReq)
2060 if err != nil {
2061 Error.Printf("Error getting value on device Id %s,err=%s\n", options.Args.Id, ErrorToString(err))
2062 return err
2063 }
2064 if rv.Response.Status != extension.GetValueResponse_OK {
2065 return fmt.Errorf("failed to get rx power %v", rv.Response.ErrReason.String())
2066 }
2067 outputFormat := CharReplacer.Replace(options.Format)
2068 if outputFormat == "" {
2069 outputFormat = GetCommandOptionWithDefault("device-get-rx-power", "format", DEFAULT_RX_POWER_STATUS_FORMAT)
2070 }
2071 result := CommandResult{
2072 Format: format.Format(outputFormat),
2073 OutputAs: toOutputType(options.OutputAs),
2074 NameLimit: options.NameLimit,
2075 Data: rv.GetResponse().GetRxPower(),
2076 }
2077 GenerateOutput(&result)
2078 return nil
2079}
2080
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -08002081/*Device get Onu Distance */
2082func (options *DeviceGetExtValue) Execute(args []string) error {
2083 conn, err := NewConnection()
2084 if err != nil {
2085 return err
2086 }
2087 defer conn.Close()
2088
Scott Baker9173ed82020-05-19 08:30:12 -07002089 client := voltha.NewVolthaServiceClient(conn)
2090
2091 valueflag, okay := common.ValueType_Type_value[string(options.Args.Valueflag)]
2092 if !okay {
2093 Error.Printf("Unknown valueflag %s\n", options.Args.Valueflag)
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -08002094 }
2095
Scott Baker9173ed82020-05-19 08:30:12 -07002096 val := voltha.ValueSpecifier{Id: string(options.Args.Id), Value: common.ValueType_Type(valueflag)}
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -08002097
David K. Bainbridge9189c632021-03-26 21:52:21 +00002098 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -08002099 defer cancel()
2100
Scott Baker9173ed82020-05-19 08:30:12 -07002101 rv, err := client.GetExtValue(ctx, &val)
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -08002102 if err != nil {
2103 Error.Printf("Error getting value on device Id %s,err=%s\n", options.Args.Id, ErrorToString(err))
2104 return err
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -08002105 }
2106
Scott Baker9173ed82020-05-19 08:30:12 -07002107 var rows []ReturnValueRow
2108 for name, num := range common.ValueType_Type_value {
2109 if num == 0 {
2110 // EMPTY is not a real value
2111 continue
2112 }
2113 if (rv.Error & uint32(num)) != 0 {
2114 row := ReturnValueRow{Name: name, Result: "Error"}
2115 rows = append(rows, row)
2116 }
2117 if (rv.Unsupported & uint32(num)) != 0 {
2118 row := ReturnValueRow{Name: name, Result: "Unsupported"}
2119 rows = append(rows, row)
2120 }
2121 if (rv.Set & uint32(num)) != 0 {
2122 switch name {
2123 case "DISTANCE":
2124 row := ReturnValueRow{Name: name, Result: rv.Distance}
2125 rows = append(rows, row)
2126 default:
2127 row := ReturnValueRow{Name: name, Result: "Unimplemented-in-voltctl"}
2128 rows = append(rows, row)
2129 }
2130 }
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -08002131 }
2132
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -08002133 outputFormat := CharReplacer.Replace(options.Format)
2134 if outputFormat == "" {
2135 outputFormat = GetCommandOptionWithDefault("device-value-get", "format", DEFAULT_DEVICE_VALUE_GET_FORMAT)
2136 }
2137
2138 result := CommandResult{
2139 Format: format.Format(outputFormat),
2140 OutputAs: toOutputType(options.OutputAs),
2141 NameLimit: options.NameLimit,
Scott Baker9173ed82020-05-19 08:30:12 -07002142 Data: rows,
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -08002143 }
2144 GenerateOutput(&result)
2145 return nil
2146}