blob: e3c1e92dbcfca1fce1f55e9d329fe811690b16e0 [file] [log] [blame]
Zack Williamse940c7a2019-08-21 14:25:39 -07001/*
2 * Copyright 2019-present Ciena Corporation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package commands
17
18import (
19 "context"
20 "fmt"
David Bainbridge7052fe82020-03-25 10:37:00 -070021 "os"
22 "strconv"
23 "strings"
Girish Gowdra610acb42021-01-27 13:33:57 -080024 "time"
David Bainbridge7052fe82020-03-25 10:37:00 -070025
Scott Baker9173ed82020-05-19 08:30:12 -070026 "github.com/golang/protobuf/ptypes/empty"
Zack Williamse940c7a2019-08-21 14:25:39 -070027 flags "github.com/jessevdk/go-flags"
Scott Baker2b0ad652019-08-21 14:57:07 -070028 "github.com/opencord/voltctl/pkg/format"
kesavand8ec4fc02021-01-27 09:10:22 -050029 "github.com/opencord/voltha-protos/v4/go/common"
30 "github.com/opencord/voltha-protos/v4/go/extension"
31 "github.com/opencord/voltha-protos/v4/go/voltha"
Zack Williamse940c7a2019-08-21 14:25:39 -070032)
33
34const (
David K. Bainbridge89003c42020-02-27 17:22:49 -080035 DEFAULT_DEVICE_FORMAT = "table{{ .Id }}\t{{.Type}}\t{{.Root}}\t{{.ParentId}}\t{{.SerialNumber}}\t{{.AdminState}}\t{{.OperStatus}}\t{{.ConnectStatus}}\t{{.Reason}}"
Zack Williamse940c7a2019-08-21 14:25:39 -070036 DEFAULT_DEVICE_PORTS_FORMAT = "table{{.PortNo}}\t{{.Label}}\t{{.Type}}\t{{.AdminState}}\t{{.OperStatus}}\t{{.DeviceId}}\t{{.Peers}}"
37 DEFAULT_DEVICE_INSPECT_FORMAT = `ID: {{.Id}}
38 TYPE: {{.Type}}
39 ROOT: {{.Root}}
40 PARENTID: {{.ParentId}}
41 SERIALNUMBER: {{.SerialNumber}}
42 VLAN: {{.Vlan}}
43 ADMINSTATE: {{.AdminState}}
44 OPERSTATUS: {{.OperStatus}}
45 CONNECTSTATUS: {{.ConnectStatus}}`
Rohan Agrawal9228d2f2020-06-03 07:48:50 +000046 DEFAULT_DEVICE_PM_CONFIG_GET_FORMAT = "table{{.DefaultFreq}}\t{{.Grouped}}\t{{.FreqOverride}}"
47 DEFAULT_DEVICE_PM_CONFIG_METRIC_LIST_FORMAT = "table{{.Name}}\t{{.Type}}\t{{.Enabled}}\t{{.SampleFreq}}"
48 DEFAULT_DEVICE_PM_CONFIG_GROUP_LIST_FORMAT = "table{{.GroupName}}\t{{.Enabled}}\t{{.GroupFreq}}"
49 DEFAULT_DEVICE_VALUE_GET_FORMAT = "table{{.Name}}\t{{.Result}}"
Andrea Campanella791d88b2021-01-08 13:29:00 +010050 DEFAULT_DEVICE_IMAGE_LIST_GET_FORMAT = "table{{.Name}}\t{{.Url}}\t{{.Crc}}\t{{.DownloadState}}\t{{.ImageVersion}}\t{{.LocalDir}}\t{{.ImageState}}\t{{.FileSize}}"
ssiddiqui7bc89e92021-05-20 20:58:02 +053051 ONU_IMAGE_LIST_FORMAT = "table{{.Version}}\t{{.IsCommited}}\t{{.IsActive}}\t{{.IsValid}}\t{{.ProductCode}}\t{{.Hash}}"
52 ONU_IMAGE_STATUS_FORMAT = "table{{.DeviceId}}\t{{.ImageState.Version}}\t{{.ImageState.DownloadState}}\t{{.ImageState.Reason}}\t{{.ImageState.ImageState}}\t"
kesavand8ec4fc02021-01-27 09:10:22 -050053 DEFAULT_DEVICE_GET_PORT_STATUS_FORMAT = `
54 TXBYTES: {{.TxBytes}}
55 TXPACKETS: {{.TxPackets}}
56 TXERRPACKETS: {{.TxErrorPackets}}
57 TXBCASTPACKETS: {{.TxBcastPackets}}
58 TXUCASTPACKETS: {{.TxUcastPackets}}
59 TXMCASTPACKETS: {{.TxMcastPackets}}
60 RXBYTES: {{.RxBytes}}
61 RXPACKETS: {{.RxPackets}}
62 RXERRPACKETS: {{.RxErrorPackets}}
63 RXBCASTPACKETS: {{.RxBcastPackets}}
64 RXUCASTPACKETS: {{.RxUcastPackets}}
65 RXMCASTPACKETS: {{.RxMcastPackets}}`
kesavand6d1131f2021-02-05 22:38:15 +053066 DEFAULT_DEVICE_GET_UNI_STATUS_FORMAT = `
67 ADMIN_STATE: {{.AdmState}}
68 OPERATIONAL_STATE: {{.OperState}}
69 CONFIG_IND: {{.ConfigInd}}`
Girish Gowdra4f5ce7c2021-04-29 18:53:21 -070070 DEFAULT_ONU_PON_OPTICAL_INFO_STATUS_FORMAT = `
71 POWER_FEED_VOLTAGE__VOLTS: {{.PowerFeedVoltage}}
72 RECEIVED_OPTICAL_POWER__dBm: {{.ReceivedOpticalPower}}
73 MEAN_OPTICAL_LAUNCH_POWER__dBm: {{.MeanOpticalLaunchPower}}
74 LASER_BIAS_CURRENT__mA: {{.LaserBiasCurrent}}
75 TEMPERATURE__Celsius: {{.Temperature}}`
Zack Williamse940c7a2019-08-21 14:25:39 -070076)
77
78type DeviceList struct {
79 ListOutputOptions
80}
81
82type DeviceCreate struct {
David Bainbridge1a514392020-06-23 11:12:51 -070083 DeviceType string `short:"t" required:"true" long:"devicetype" description:"Device type"`
David Bainbridge835dd0e2020-04-01 10:30:09 -070084 MACAddress string `short:"m" long:"macaddress" default:"" description:"MAC Address"`
Zack Williamse940c7a2019-08-21 14:25:39 -070085 IPAddress string `short:"i" long:"ipaddress" default:"" description:"IP Address"`
86 HostAndPort string `short:"H" long:"hostandport" default:"" description:"Host and port"`
87}
88
89type DeviceId string
90
Rohan Agrawal9228d2f2020-06-03 07:48:50 +000091type MetricName string
92type GroupName string
kesavand12cd8eb2020-01-20 22:25:22 -050093type PortNum uint32
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -080094type ValueFlag string
kesavand12cd8eb2020-01-20 22:25:22 -050095
Zack Williamse940c7a2019-08-21 14:25:39 -070096type DeviceDelete struct {
Himani Chawla9933ddc2020-10-12 23:53:27 +053097 Force bool `long:"force" description:"Delete device forcefully"`
98 Args struct {
Zack Williamse940c7a2019-08-21 14:25:39 -070099 Ids []DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
100 } `positional-args:"yes"`
101}
102
103type DeviceEnable struct {
104 Args struct {
105 Ids []DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
106 } `positional-args:"yes"`
107}
108
109type DeviceDisable struct {
110 Args struct {
111 Ids []DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
112 } `positional-args:"yes"`
113}
114
115type DeviceReboot struct {
116 Args struct {
117 Ids []DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
118 } `positional-args:"yes"`
119}
120
121type DeviceFlowList struct {
122 ListOutputOptions
Maninder045921e2020-09-29 16:46:02 +0530123 FlowIdOptions
Zack Williamse940c7a2019-08-21 14:25:39 -0700124 Args struct {
125 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
126 } `positional-args:"yes"`
127}
128
129type DevicePortList struct {
130 ListOutputOptions
131 Args struct {
132 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
133 } `positional-args:"yes"`
134}
135
136type DeviceInspect struct {
137 OutputOptionsJson
138 Args struct {
139 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
140 } `positional-args:"yes"`
141}
142
kesavand12cd8eb2020-01-20 22:25:22 -0500143type DevicePortEnable struct {
144 Args struct {
145 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
146 PortId PortNum `positional-arg-name:"PORT_NUMBER" required:"yes"`
147 } `positional-args:"yes"`
148}
149
150type DevicePortDisable struct {
151 Args struct {
152 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
153 PortId PortNum `positional-arg-name:"PORT_NUMBER" required:"yes"`
154 } `positional-args:"yes"`
155}
156
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000157type DevicePmConfigsGet struct {
158 ListOutputOptions
159 Args struct {
160 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
161 } `positional-args:"yes"`
162}
163
164type DevicePmConfigMetricList struct {
165 ListOutputOptions
166 Args struct {
167 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
168 } `positional-args:"yes"`
169}
170
171type DevicePmConfigGroupList struct {
172 ListOutputOptions
173 Args struct {
174 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
175 } `positional-args:"yes"`
176}
177
178type DevicePmConfigGroupMetricList struct {
179 ListOutputOptions
180 Args struct {
181 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
182 Group GroupName `positional-arg-name:"GROUP_NAME" required:"yes"`
183 } `positional-args:"yes"`
184}
185
186type DevicePmConfigFrequencySet struct {
187 OutputOptions
188 Args struct {
Girish Gowdra610acb42021-01-27 13:33:57 -0800189 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
190 Interval time.Duration `positional-arg-name:"INTERVAL" required:"yes"`
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000191 } `positional-args:"yes"`
192}
193
194type DevicePmConfigMetricEnable struct {
195 Args struct {
196 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
197 Metrics []MetricName `positional-arg-name:"METRIC_NAME" required:"yes"`
198 } `positional-args:"yes"`
199}
200
201type DevicePmConfigMetricDisable struct {
202 Args struct {
203 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
204 Metrics []MetricName `positional-arg-name:"METRIC_NAME" required:"yes"`
205 } `positional-args:"yes"`
206}
207
208type DevicePmConfigGroupEnable struct {
209 Args struct {
Girish Gowdra610acb42021-01-27 13:33:57 -0800210 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
211 Group GroupName `positional-arg-name:"GROUP_NAME" required:"yes"`
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000212 } `positional-args:"yes"`
213}
214
215type DevicePmConfigGroupDisable struct {
216 Args struct {
Girish Gowdra610acb42021-01-27 13:33:57 -0800217 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
218 Group GroupName `positional-arg-name:"GROUP_NAME" required:"yes"`
219 } `positional-args:"yes"`
220}
221
222type DevicePmConfigGroupFrequencySet struct {
223 OutputOptions
224 Args struct {
225 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
226 Group GroupName `positional-arg-name:"GROUP_NAME" required:"yes"`
227 Interval time.Duration `positional-arg-name:"INTERVAL" required:"yes"`
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000228 } `positional-args:"yes"`
229}
230
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -0800231type DeviceGetExtValue struct {
232 ListOutputOptions
233 Args struct {
234 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
235 Valueflag ValueFlag `positional-arg-name:"VALUE_FLAG" required:"yes"`
236 } `positional-args:"yes"`
237}
Rohan Agrawald7df3772020-06-29 11:23:36 +0000238
239type DevicePmConfigSetMaxSkew struct {
240 Args struct {
241 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
242 MaxSkew uint32 `positional-arg-name:"MAX_SKEW" required:"yes"`
243 } `positional-args:"yes"`
244}
245
Andrea Campanella791d88b2021-01-08 13:29:00 +0100246type DeviceOnuListImages struct {
247 ListOutputOptions
248 Args struct {
249 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
250 } `positional-args:"yes"`
251}
252
253type DeviceOnuDownloadImage struct {
254 Args struct {
255 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
256 Name string `positional-arg-name:"IMAGE_NAME" required:"yes"`
257 Url string `positional-arg-name:"IMAGE_URL" required:"yes"`
258 ImageVersion string `positional-arg-name:"IMAGE_VERSION" required:"yes"`
259 Crc uint32 `positional-arg-name:"IMAGE_CRC" required:"yes"`
260 LocalDir string `positional-arg-name:"IMAGE_LOCAL_DIRECTORY"`
261 } `positional-args:"yes"`
262}
263
264type DeviceOnuActivateImageUpdate struct {
265 Args struct {
266 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
267 Name string `positional-arg-name:"IMAGE_NAME" required:"yes"`
268 ImageVersion string `positional-arg-name:"IMAGE_VERSION" required:"yes"`
269 SaveConfig bool `positional-arg-name:"SAVE_EXISTING_CONFIG"`
270 LocalDir string `positional-arg-name:"IMAGE_LOCAL_DIRECTORY"`
Andrea Campanella7b2ecf42021-02-25 12:27:15 +0100271 } `positional-args:"yes"`
kesavand8ec4fc02021-01-27 09:10:22 -0500272}
kesavand3e2f9f62021-04-22 11:06:38 +0530273
274type OnuDownloadImage struct {
275 Args struct {
276 ImageVersion string `positional-arg-name:"IMAGE_VERSION" required:"yes"`
277 Url string `positional-arg-name:"IMAGE_URL" required:"yes"`
ssiddiqui7bc89e92021-05-20 20:58:02 +0530278 Vendor string `positional-arg-name:"IMAGE_VENDOR"`
kesavand3e2f9f62021-04-22 11:06:38 +0530279 ActivateOnSuccess bool `positional-arg-name:"IMAGE_ACTIVATE_ON_SUCCESS"`
280 CommitOnSuccess bool `positional-arg-name:"IMAGE_COMMIT_ON_SUCCESS"`
281 Crc uint32 `positional-arg-name:"IMAGE_CRC"`
282 IDs []DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
283 } `positional-args:"yes"`
284}
285
286type OnuActivateImage struct {
287 Args struct {
288 ImageVersion string `positional-arg-name:"IMAGE_VERSION" required:"yes"`
289 CommitOnSuccess bool `positional-arg-name:"IMAGE_COMMIT_ON_SUCCESS"`
290 IDs []DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
291 } `positional-args:"yes"`
292}
293
294type OnuAbortUpgradeImage struct {
295 Args struct {
296 ImageVersion string `positional-arg-name:"IMAGE_VERSION" required:"yes"`
297 IDs []DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
298 } `positional-args:"yes"`
299}
300
301type OnuCommitImage struct {
302 Args struct {
303 ImageVersion string `positional-arg-name:"IMAGE_VERSION" required:"yes"`
304 IDs []DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
305 } `positional-args:"yes"`
306}
307
308type OnuImageStatus struct {
309 ListOutputOptions
310 Args struct {
311 ImageVersion string `positional-arg-name:"IMAGE_VERSION" required:"yes"`
312 IDs []DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
313 } `positional-args:"yes"`
314}
315
316type OnuListImages struct {
317 ListOutputOptions
318 Args struct {
319 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
320 } `positional-args:"yes"`
321}
322
kesavand8ec4fc02021-01-27 09:10:22 -0500323type DeviceGetPortStats struct {
324 ListOutputOptions
325 Args struct {
326 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
327 PortNo uint32 `positional-arg-name:"PORT_NO" required:"yes"`
328 PortType string `positional-arg-name:"PORT_TYPE" required:"yes"`
Andrea Campanella791d88b2021-01-08 13:29:00 +0100329 } `positional-args:"yes"`
330}
kesavand6d1131f2021-02-05 22:38:15 +0530331type UniStatus struct {
332 ListOutputOptions
333 Args struct {
334 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
335 UniIndex uint32 `positional-arg-name:"UNI_INDEX" required:"yes"`
336 } `positional-args:"yes"`
337}
Girish Gowdra4f5ce7c2021-04-29 18:53:21 -0700338type OnuPonOpticalInfo struct {
339 ListOutputOptions
340 Args struct {
341 Id DeviceId `positional-arg-name:"DEVICE_ID" required:"yes"`
342 } `positional-args:"yes"`
343}
Himani Chawla40acc122021-05-26 18:52:29 +0530344
345type GetOnuStats struct {
346 ListOutputOptions
347 Args struct {
348 OltId DeviceId `positional-arg-name:"OLT_DEVICE_ID" required:"yes"`
349 IntfId uint32 `positional-arg-name:"PON_INTF_ID" required:"yes"`
350 OnuId uint32 `positional-arg-name:"ONU_ID" required:"yes"`
351 } `positional-args:"yes"`
352}
353
Zack Williamse940c7a2019-08-21 14:25:39 -0700354type DeviceOpts struct {
355 List DeviceList `command:"list"`
356 Create DeviceCreate `command:"create"`
357 Delete DeviceDelete `command:"delete"`
358 Enable DeviceEnable `command:"enable"`
359 Disable DeviceDisable `command:"disable"`
360 Flows DeviceFlowList `command:"flows"`
kesavand12cd8eb2020-01-20 22:25:22 -0500361 Port struct {
362 List DevicePortList `command:"list"`
363 Enable DevicePortEnable `command:"enable"`
364 Disable DevicePortDisable `command:"disable"`
365 } `command:"port"`
366 Inspect DeviceInspect `command:"inspect"`
367 Reboot DeviceReboot `command:"reboot"`
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -0800368 Value struct {
369 Get DeviceGetExtValue `command:"get"`
370 } `command:"value"`
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000371 PmConfig struct {
Rohan Agrawald7df3772020-06-29 11:23:36 +0000372 Get DevicePmConfigsGet `command:"get"`
373 MaxSkew struct {
374 Set DevicePmConfigSetMaxSkew `command:"set"`
375 } `command:"maxskew"`
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000376 Frequency struct {
377 Set DevicePmConfigFrequencySet `command:"set"`
378 } `command:"frequency"`
379 Metric struct {
380 List DevicePmConfigMetricList `command:"list"`
381 Enable DevicePmConfigMetricEnable `command:"enable"`
382 Disable DevicePmConfigMetricDisable `command:"disable"`
383 } `command:"metric"`
384 Group struct {
Girish Gowdra610acb42021-01-27 13:33:57 -0800385 List DevicePmConfigGroupList `command:"list"`
386 Enable DevicePmConfigGroupEnable `command:"enable"`
387 Disable DevicePmConfigGroupDisable `command:"disable"`
388 Set DevicePmConfigGroupFrequencySet `command:"set"`
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000389 } `command:"group"`
390 GroupMetric struct {
391 List DevicePmConfigGroupMetricList `command:"list"`
392 } `command:"groupmetric"`
393 } `command:"pmconfig"`
Andrea Campanella791d88b2021-01-08 13:29:00 +0100394 Image struct {
395 Get DeviceOnuListImages `command:"list"`
396 Download DeviceOnuDownloadImage `command:"download"`
397 Activate DeviceOnuActivateImageUpdate `command:"activate"`
398 } `command:"image"`
kesavand3e2f9f62021-04-22 11:06:38 +0530399 DownloadImage struct {
400 Download OnuDownloadImage `command:"download"`
401 Activate OnuActivateImage `command:"activate"`
402 Commit OnuCommitImage `command:"commit"`
403 AbortUpgrade OnuAbortUpgradeImage `command:"abort"`
404 Status OnuImageStatus `command:"status"`
405 List OnuListImages `command:"list" `
406 } `command:"onuimage"`
kesavand8ec4fc02021-01-27 09:10:22 -0500407 GetExtVal struct {
Girish Gowdra4f5ce7c2021-04-29 18:53:21 -0700408 Stats DeviceGetPortStats `command:"portstats"`
409 UniStatus UniStatus `command:"unistatus"`
410 OpticalInfo OnuPonOpticalInfo `command:"onu_pon_optical_info"`
Himani Chawla40acc122021-05-26 18:52:29 +0530411 OnuStats GetOnuStats `command:"onu_stats"`
kesavand8ec4fc02021-01-27 09:10:22 -0500412 } `command:"getextval"`
Zack Williamse940c7a2019-08-21 14:25:39 -0700413}
414
415var deviceOpts = DeviceOpts{}
416
417func RegisterDeviceCommands(parser *flags.Parser) {
David Bainbridge12f036f2019-10-15 22:09:04 +0000418 if _, err := parser.AddCommand("device", "device commands", "Commands to query and manipulate VOLTHA devices", &deviceOpts); err != nil {
David Bainbridgea6722342019-10-24 23:55:53 +0000419 Error.Fatalf("Unexpected error while attempting to register device commands : %s", err)
David Bainbridge12f036f2019-10-15 22:09:04 +0000420 }
Zack Williamse940c7a2019-08-21 14:25:39 -0700421}
422
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000423func (i *MetricName) Complete(match string) []flags.Completion {
424 conn, err := NewConnection()
425 if err != nil {
426 return nil
427 }
428 defer conn.Close()
429
430 client := voltha.NewVolthaServiceClient(conn)
431
432 var deviceId string
433found:
434 for i := len(os.Args) - 1; i >= 0; i -= 1 {
435 switch os.Args[i] {
436 case "enable":
437 fallthrough
438 case "disable":
439 if len(os.Args) > i+1 {
440 deviceId = os.Args[i+1]
441 } else {
442 return nil
443 }
444 break found
445 default:
446 }
447 }
448
449 if len(deviceId) == 0 {
450 return nil
451 }
452
David K. Bainbridge9189c632021-03-26 21:52:21 +0000453 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000454 defer cancel()
455
456 id := voltha.ID{Id: string(deviceId)}
457
458 pmconfigs, err := client.ListDevicePmConfigs(ctx, &id)
459
460 if err != nil {
461 return nil
462 }
463
464 list := make([]flags.Completion, 0)
465 for _, metrics := range pmconfigs.Metrics {
466 if strings.HasPrefix(metrics.Name, match) {
467 list = append(list, flags.Completion{Item: metrics.Name})
468 }
469 }
470
471 return list
472}
473
474func (i *GroupName) Complete(match string) []flags.Completion {
475 conn, err := NewConnection()
476 if err != nil {
477 return nil
478 }
479 defer conn.Close()
480
481 client := voltha.NewVolthaServiceClient(conn)
482
483 var deviceId string
484found:
485 for i := len(os.Args) - 1; i >= 0; i -= 1 {
486 switch os.Args[i] {
487 case "list":
488 fallthrough
489 case "enable":
490 fallthrough
491 case "disable":
492 if len(os.Args) > i+1 {
493 deviceId = os.Args[i+1]
494 } else {
495 return nil
496 }
497 break found
498 default:
499 }
500 }
501
502 if len(deviceId) == 0 {
503 return nil
504 }
505
David K. Bainbridge9189c632021-03-26 21:52:21 +0000506 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000507 defer cancel()
508
509 id := voltha.ID{Id: string(deviceId)}
510
511 pmconfigs, err := client.ListDevicePmConfigs(ctx, &id)
512
513 if err != nil {
514 return nil
515 }
516
517 list := make([]flags.Completion, 0)
518 for _, group := range pmconfigs.Groups {
519 if strings.HasPrefix(group.GroupName, match) {
520 list = append(list, flags.Completion{Item: group.GroupName})
521 }
522 }
523 return list
524}
525
kesavand12cd8eb2020-01-20 22:25:22 -0500526func (i *PortNum) Complete(match string) []flags.Completion {
527 conn, err := NewConnection()
528 if err != nil {
529 return nil
530 }
531 defer conn.Close()
532
Scott Baker9173ed82020-05-19 08:30:12 -0700533 client := voltha.NewVolthaServiceClient(conn)
kesavand12cd8eb2020-01-20 22:25:22 -0500534
535 /*
536 * The command line args when completing for PortNum will be a DeviceId
537 * followed by one or more PortNums. So walk the argument list from the
538 * end and find the first argument that is enable/disable as those are
539 * the subcommands that come before the positional arguments. It would
540 * be nice if this package gave us the list of optional arguments
541 * already parsed.
542 */
543 var deviceId string
544found:
545 for i := len(os.Args) - 1; i >= 0; i -= 1 {
546 switch os.Args[i] {
547 case "enable":
548 fallthrough
549 case "disable":
550 if len(os.Args) > i+1 {
551 deviceId = os.Args[i+1]
552 } else {
553 return nil
554 }
555 break found
556 default:
557 }
558 }
559
560 if len(deviceId) == 0 {
561 return nil
562 }
563
David K. Bainbridge9189c632021-03-26 21:52:21 +0000564 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
kesavand12cd8eb2020-01-20 22:25:22 -0500565 defer cancel()
kesavand12cd8eb2020-01-20 22:25:22 -0500566
Scott Baker9173ed82020-05-19 08:30:12 -0700567 id := voltha.ID{Id: string(deviceId)}
kesavand12cd8eb2020-01-20 22:25:22 -0500568
Scott Baker9173ed82020-05-19 08:30:12 -0700569 ports, err := client.ListDevicePorts(ctx, &id)
kesavand12cd8eb2020-01-20 22:25:22 -0500570 if err != nil {
571 return nil
572 }
573
574 list := make([]flags.Completion, 0)
Scott Baker9173ed82020-05-19 08:30:12 -0700575 for _, item := range ports.Items {
576 pn := strconv.FormatUint(uint64(item.PortNo), 10)
kesavand12cd8eb2020-01-20 22:25:22 -0500577 if strings.HasPrefix(pn, match) {
578 list = append(list, flags.Completion{Item: pn})
579 }
580 }
581
582 return list
583}
584
Zack Williamse940c7a2019-08-21 14:25:39 -0700585func (i *DeviceId) Complete(match string) []flags.Completion {
586 conn, err := NewConnection()
587 if err != nil {
588 return nil
589 }
590 defer conn.Close()
591
Scott Baker9173ed82020-05-19 08:30:12 -0700592 client := voltha.NewVolthaServiceClient(conn)
Zack Williamse940c7a2019-08-21 14:25:39 -0700593
David K. Bainbridge9189c632021-03-26 21:52:21 +0000594 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Zack Williamse940c7a2019-08-21 14:25:39 -0700595 defer cancel()
596
Scott Baker9173ed82020-05-19 08:30:12 -0700597 devices, err := client.ListDevices(ctx, &empty.Empty{})
Zack Williamse940c7a2019-08-21 14:25:39 -0700598 if err != nil {
599 return nil
600 }
601
602 list := make([]flags.Completion, 0)
Scott Baker9173ed82020-05-19 08:30:12 -0700603 for _, item := range devices.Items {
604 if strings.HasPrefix(item.Id, match) {
605 list = append(list, flags.Completion{Item: item.Id})
Zack Williamse940c7a2019-08-21 14:25:39 -0700606 }
607 }
608
609 return list
610}
611
612func (options *DeviceList) Execute(args []string) error {
613
614 conn, err := NewConnection()
615 if err != nil {
616 return err
617 }
618 defer conn.Close()
619
Scott Baker9173ed82020-05-19 08:30:12 -0700620 client := voltha.NewVolthaServiceClient(conn)
Zack Williamse940c7a2019-08-21 14:25:39 -0700621
David K. Bainbridge9189c632021-03-26 21:52:21 +0000622 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Zack Williamse940c7a2019-08-21 14:25:39 -0700623 defer cancel()
624
Scott Baker9173ed82020-05-19 08:30:12 -0700625 devices, err := client.ListDevices(ctx, &empty.Empty{})
Zack Williamse940c7a2019-08-21 14:25:39 -0700626 if err != nil {
627 return err
628 }
629
630 outputFormat := CharReplacer.Replace(options.Format)
631 if outputFormat == "" {
David Bainbridgea6722342019-10-24 23:55:53 +0000632 outputFormat = GetCommandOptionWithDefault("device-list", "format", DEFAULT_DEVICE_FORMAT)
Zack Williamse940c7a2019-08-21 14:25:39 -0700633 }
634 if options.Quiet {
635 outputFormat = "{{.Id}}"
636 }
637
David Bainbridgea6722342019-10-24 23:55:53 +0000638 orderBy := options.OrderBy
639 if orderBy == "" {
640 orderBy = GetCommandOptionWithDefault("device-list", "order", "")
641 }
642
Scott Baker9173ed82020-05-19 08:30:12 -0700643 // Make sure json output prints an empty list, not "null"
644 if devices.Items == nil {
645 devices.Items = make([]*voltha.Device, 0)
Zack Williamse940c7a2019-08-21 14:25:39 -0700646 }
647
648 result := CommandResult{
649 Format: format.Format(outputFormat),
650 Filter: options.Filter,
David Bainbridgea6722342019-10-24 23:55:53 +0000651 OrderBy: orderBy,
Zack Williamse940c7a2019-08-21 14:25:39 -0700652 OutputAs: toOutputType(options.OutputAs),
653 NameLimit: options.NameLimit,
Scott Baker9173ed82020-05-19 08:30:12 -0700654 Data: devices.Items,
Zack Williamse940c7a2019-08-21 14:25:39 -0700655 }
656
657 GenerateOutput(&result)
658 return nil
659}
660
661func (options *DeviceCreate) Execute(args []string) error {
662
Scott Baker9173ed82020-05-19 08:30:12 -0700663 device := voltha.Device{}
Zack Williamse940c7a2019-08-21 14:25:39 -0700664 if options.HostAndPort != "" {
Scott Baker9173ed82020-05-19 08:30:12 -0700665 device.Address = &voltha.Device_HostAndPort{HostAndPort: options.HostAndPort}
Zack Williamse940c7a2019-08-21 14:25:39 -0700666 } else if options.IPAddress != "" {
Scott Baker9173ed82020-05-19 08:30:12 -0700667 device.Address = &voltha.Device_Ipv4Address{Ipv4Address: options.IPAddress}
Hardik Windlassce1de342020-02-04 21:58:07 +0000668 }
669 if options.MACAddress != "" {
Scott Baker9173ed82020-05-19 08:30:12 -0700670 device.MacAddress = strings.ToLower(options.MACAddress)
Zack Williamse940c7a2019-08-21 14:25:39 -0700671 }
672 if options.DeviceType != "" {
Scott Baker9173ed82020-05-19 08:30:12 -0700673 device.Type = options.DeviceType
Zack Williamse940c7a2019-08-21 14:25:39 -0700674 }
675
676 conn, err := NewConnection()
677 if err != nil {
678 return err
679 }
680 defer conn.Close()
681
Scott Baker9173ed82020-05-19 08:30:12 -0700682 client := voltha.NewVolthaServiceClient(conn)
Zack Williamse940c7a2019-08-21 14:25:39 -0700683
David K. Bainbridge9189c632021-03-26 21:52:21 +0000684 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Zack Williamse940c7a2019-08-21 14:25:39 -0700685 defer cancel()
686
Scott Baker9173ed82020-05-19 08:30:12 -0700687 createdDevice, err := client.CreateDevice(ctx, &device)
Zack Williamse940c7a2019-08-21 14:25:39 -0700688 if err != nil {
689 return err
Zack Williamse940c7a2019-08-21 14:25:39 -0700690 }
691
Scott Baker9173ed82020-05-19 08:30:12 -0700692 fmt.Printf("%s\n", createdDevice.Id)
Zack Williamse940c7a2019-08-21 14:25:39 -0700693
694 return nil
695}
696
697func (options *DeviceDelete) Execute(args []string) error {
698
699 conn, err := NewConnection()
700 if err != nil {
701 return err
702 }
703 defer conn.Close()
704
Scott Baker9173ed82020-05-19 08:30:12 -0700705 client := voltha.NewVolthaServiceClient(conn)
David Bainbridge7052fe82020-03-25 10:37:00 -0700706 var lastErr error
Zack Williamse940c7a2019-08-21 14:25:39 -0700707 for _, i := range options.Args.Ids {
David K. Bainbridge9189c632021-03-26 21:52:21 +0000708 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Zack Williamse940c7a2019-08-21 14:25:39 -0700709 defer cancel()
710
Scott Baker9173ed82020-05-19 08:30:12 -0700711 id := voltha.ID{Id: string(i)}
Himani Chawla9933ddc2020-10-12 23:53:27 +0530712 if options.Force {
713 _, err = client.ForceDeleteDevice(ctx, &id)
714 } else {
715 _, err = client.DeleteDevice(ctx, &id)
716 }
Scott Baker9173ed82020-05-19 08:30:12 -0700717
Zack Williamse940c7a2019-08-21 14:25:39 -0700718 if err != nil {
David Bainbridge0f758d42019-10-26 05:17:48 +0000719 Error.Printf("Error while deleting '%s': %s\n", i, err)
David Bainbridge7052fe82020-03-25 10:37:00 -0700720 lastErr = err
Zack Williamse940c7a2019-08-21 14:25:39 -0700721 continue
Zack Williamse940c7a2019-08-21 14:25:39 -0700722 }
723 fmt.Printf("%s\n", i)
724 }
725
David Bainbridge7052fe82020-03-25 10:37:00 -0700726 if lastErr != nil {
727 return NoReportErr
728 }
Zack Williamse940c7a2019-08-21 14:25:39 -0700729 return nil
730}
731
732func (options *DeviceEnable) Execute(args []string) error {
733 conn, err := NewConnection()
734 if err != nil {
735 return err
736 }
737 defer conn.Close()
738
Scott Baker9173ed82020-05-19 08:30:12 -0700739 client := voltha.NewVolthaServiceClient(conn)
Zack Williamse940c7a2019-08-21 14:25:39 -0700740
David Bainbridge7052fe82020-03-25 10:37:00 -0700741 var lastErr error
Zack Williamse940c7a2019-08-21 14:25:39 -0700742 for _, i := range options.Args.Ids {
David K. Bainbridge9189c632021-03-26 21:52:21 +0000743 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Zack Williamse940c7a2019-08-21 14:25:39 -0700744 defer cancel()
745
Scott Baker9173ed82020-05-19 08:30:12 -0700746 id := voltha.ID{Id: string(i)}
747
748 _, err := client.EnableDevice(ctx, &id)
Zack Williamse940c7a2019-08-21 14:25:39 -0700749 if err != nil {
David Bainbridge0f758d42019-10-26 05:17:48 +0000750 Error.Printf("Error while enabling '%s': %s\n", i, err)
David Bainbridge7052fe82020-03-25 10:37:00 -0700751 lastErr = err
Zack Williamse940c7a2019-08-21 14:25:39 -0700752 continue
Zack Williamse940c7a2019-08-21 14:25:39 -0700753 }
754 fmt.Printf("%s\n", i)
755 }
756
David Bainbridge7052fe82020-03-25 10:37:00 -0700757 if lastErr != nil {
758 return NoReportErr
759 }
Zack Williamse940c7a2019-08-21 14:25:39 -0700760 return nil
761}
762
763func (options *DeviceDisable) Execute(args []string) error {
764 conn, err := NewConnection()
765 if err != nil {
766 return err
767 }
768 defer conn.Close()
769
Scott Baker9173ed82020-05-19 08:30:12 -0700770 client := voltha.NewVolthaServiceClient(conn)
Zack Williamse940c7a2019-08-21 14:25:39 -0700771
David Bainbridge7052fe82020-03-25 10:37:00 -0700772 var lastErr error
Zack Williamse940c7a2019-08-21 14:25:39 -0700773 for _, i := range options.Args.Ids {
David K. Bainbridge9189c632021-03-26 21:52:21 +0000774 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Zack Williamse940c7a2019-08-21 14:25:39 -0700775 defer cancel()
776
Scott Baker9173ed82020-05-19 08:30:12 -0700777 id := voltha.ID{Id: string(i)}
778
779 _, err := client.DisableDevice(ctx, &id)
Zack Williamse940c7a2019-08-21 14:25:39 -0700780 if err != nil {
David Bainbridge0f758d42019-10-26 05:17:48 +0000781 Error.Printf("Error while disabling '%s': %s\n", i, err)
David Bainbridge7052fe82020-03-25 10:37:00 -0700782 lastErr = err
Zack Williamse940c7a2019-08-21 14:25:39 -0700783 continue
Zack Williamse940c7a2019-08-21 14:25:39 -0700784 }
785 fmt.Printf("%s\n", i)
786 }
787
David Bainbridge7052fe82020-03-25 10:37:00 -0700788 if lastErr != nil {
789 return NoReportErr
790 }
Zack Williamse940c7a2019-08-21 14:25:39 -0700791 return nil
792}
793
794func (options *DeviceReboot) Execute(args []string) error {
795 conn, err := NewConnection()
796 if err != nil {
797 return err
798 }
799 defer conn.Close()
800
Scott Baker9173ed82020-05-19 08:30:12 -0700801 client := voltha.NewVolthaServiceClient(conn)
Zack Williamse940c7a2019-08-21 14:25:39 -0700802
David Bainbridge7052fe82020-03-25 10:37:00 -0700803 var lastErr error
Zack Williamse940c7a2019-08-21 14:25:39 -0700804 for _, i := range options.Args.Ids {
David K. Bainbridge9189c632021-03-26 21:52:21 +0000805 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Zack Williamse940c7a2019-08-21 14:25:39 -0700806 defer cancel()
807
Scott Baker9173ed82020-05-19 08:30:12 -0700808 id := voltha.ID{Id: string(i)}
809
810 _, err := client.RebootDevice(ctx, &id)
Zack Williamse940c7a2019-08-21 14:25:39 -0700811 if err != nil {
David Bainbridge0f758d42019-10-26 05:17:48 +0000812 Error.Printf("Error while rebooting '%s': %s\n", i, err)
David Bainbridge7052fe82020-03-25 10:37:00 -0700813 lastErr = err
Zack Williamse940c7a2019-08-21 14:25:39 -0700814 continue
Zack Williamse940c7a2019-08-21 14:25:39 -0700815 }
816 fmt.Printf("%s\n", i)
817 }
818
David Bainbridge7052fe82020-03-25 10:37:00 -0700819 if lastErr != nil {
820 return NoReportErr
821 }
Zack Williamse940c7a2019-08-21 14:25:39 -0700822 return nil
823}
824
825func (options *DevicePortList) Execute(args []string) error {
826
827 conn, err := NewConnection()
828 if err != nil {
829 return err
830 }
831 defer conn.Close()
832
Scott Baker9173ed82020-05-19 08:30:12 -0700833 client := voltha.NewVolthaServiceClient(conn)
Zack Williamse940c7a2019-08-21 14:25:39 -0700834
David K. Bainbridge9189c632021-03-26 21:52:21 +0000835 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Zack Williamse940c7a2019-08-21 14:25:39 -0700836 defer cancel()
837
Scott Baker9173ed82020-05-19 08:30:12 -0700838 id := voltha.ID{Id: string(options.Args.Id)}
Zack Williamse940c7a2019-08-21 14:25:39 -0700839
Scott Baker9173ed82020-05-19 08:30:12 -0700840 ports, err := client.ListDevicePorts(ctx, &id)
Zack Williamse940c7a2019-08-21 14:25:39 -0700841 if err != nil {
842 return err
843 }
844
845 outputFormat := CharReplacer.Replace(options.Format)
846 if outputFormat == "" {
David Bainbridgea6722342019-10-24 23:55:53 +0000847 outputFormat = GetCommandOptionWithDefault("device-ports", "format", DEFAULT_DEVICE_PORTS_FORMAT)
Zack Williamse940c7a2019-08-21 14:25:39 -0700848 }
Zack Williamse940c7a2019-08-21 14:25:39 -0700849
David Bainbridgea6722342019-10-24 23:55:53 +0000850 orderBy := options.OrderBy
851 if orderBy == "" {
852 orderBy = GetCommandOptionWithDefault("device-ports", "order", "")
853 }
854
Zack Williamse940c7a2019-08-21 14:25:39 -0700855 result := CommandResult{
856 Format: format.Format(outputFormat),
857 Filter: options.Filter,
David Bainbridgea6722342019-10-24 23:55:53 +0000858 OrderBy: orderBy,
Zack Williamse940c7a2019-08-21 14:25:39 -0700859 OutputAs: toOutputType(options.OutputAs),
860 NameLimit: options.NameLimit,
Scott Baker9173ed82020-05-19 08:30:12 -0700861 Data: ports.Items,
Zack Williamse940c7a2019-08-21 14:25:39 -0700862 }
863
864 GenerateOutput(&result)
865 return nil
866}
867
868func (options *DeviceFlowList) Execute(args []string) error {
869 fl := &FlowList{}
870 fl.ListOutputOptions = options.ListOutputOptions
Maninder045921e2020-09-29 16:46:02 +0530871 fl.FlowIdOptions = options.FlowIdOptions
Zack Williamse940c7a2019-08-21 14:25:39 -0700872 fl.Args.Id = string(options.Args.Id)
David Bainbridgea6722342019-10-24 23:55:53 +0000873 fl.Method = "device-flows"
Zack Williamse940c7a2019-08-21 14:25:39 -0700874 return fl.Execute(args)
875}
876
877func (options *DeviceInspect) Execute(args []string) error {
878 if len(args) > 0 {
879 return fmt.Errorf("only a single argument 'DEVICE_ID' can be provided")
880 }
881
882 conn, err := NewConnection()
883 if err != nil {
884 return err
885 }
886 defer conn.Close()
887
Scott Baker9173ed82020-05-19 08:30:12 -0700888 client := voltha.NewVolthaServiceClient(conn)
Zack Williamse940c7a2019-08-21 14:25:39 -0700889
David K. Bainbridge9189c632021-03-26 21:52:21 +0000890 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Zack Williamse940c7a2019-08-21 14:25:39 -0700891 defer cancel()
892
Scott Baker9173ed82020-05-19 08:30:12 -0700893 id := voltha.ID{Id: string(options.Args.Id)}
Zack Williamse940c7a2019-08-21 14:25:39 -0700894
Scott Baker9173ed82020-05-19 08:30:12 -0700895 device, err := client.GetDevice(ctx, &id)
Zack Williamse940c7a2019-08-21 14:25:39 -0700896 if err != nil {
897 return err
898 }
899
Zack Williamse940c7a2019-08-21 14:25:39 -0700900 outputFormat := CharReplacer.Replace(options.Format)
901 if outputFormat == "" {
David Bainbridgea6722342019-10-24 23:55:53 +0000902 outputFormat = GetCommandOptionWithDefault("device-inspect", "format", DEFAULT_DEVICE_INSPECT_FORMAT)
Zack Williamse940c7a2019-08-21 14:25:39 -0700903 }
904 if options.Quiet {
905 outputFormat = "{{.Id}}"
906 }
907
908 result := CommandResult{
909 Format: format.Format(outputFormat),
910 OutputAs: toOutputType(options.OutputAs),
911 NameLimit: options.NameLimit,
912 Data: device,
913 }
914 GenerateOutput(&result)
915 return nil
916}
kesavand12cd8eb2020-01-20 22:25:22 -0500917
918/*Device Port Enable */
919func (options *DevicePortEnable) Execute(args []string) error {
920 conn, err := NewConnection()
921 if err != nil {
922 return err
923 }
924 defer conn.Close()
925
Scott Baker9173ed82020-05-19 08:30:12 -0700926 client := voltha.NewVolthaServiceClient(conn)
kesavand12cd8eb2020-01-20 22:25:22 -0500927
David K. Bainbridge9189c632021-03-26 21:52:21 +0000928 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
kesavand12cd8eb2020-01-20 22:25:22 -0500929 defer cancel()
930
Scott Baker9173ed82020-05-19 08:30:12 -0700931 port := voltha.Port{DeviceId: string(options.Args.Id), PortNo: uint32(options.Args.PortId)}
932
933 _, err = client.EnablePort(ctx, &port)
kesavand12cd8eb2020-01-20 22:25:22 -0500934 if err != nil {
935 Error.Printf("Error enabling port number %v on device Id %s,err=%s\n", options.Args.PortId, options.Args.Id, ErrorToString(err))
936 return err
kesavand12cd8eb2020-01-20 22:25:22 -0500937 }
938
939 return nil
940}
941
Scott Baker9173ed82020-05-19 08:30:12 -0700942/*Device Port Disable */
kesavand12cd8eb2020-01-20 22:25:22 -0500943func (options *DevicePortDisable) Execute(args []string) error {
944 conn, err := NewConnection()
945 if err != nil {
946 return err
947 }
948 defer conn.Close()
949
Scott Baker9173ed82020-05-19 08:30:12 -0700950 client := voltha.NewVolthaServiceClient(conn)
951
David K. Bainbridge9189c632021-03-26 21:52:21 +0000952 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
kesavand12cd8eb2020-01-20 22:25:22 -0500953 defer cancel()
954
Scott Baker9173ed82020-05-19 08:30:12 -0700955 port := voltha.Port{DeviceId: string(options.Args.Id), PortNo: uint32(options.Args.PortId)}
956
957 _, err = client.DisablePort(ctx, &port)
kesavand12cd8eb2020-01-20 22:25:22 -0500958 if err != nil {
Scott Baker9173ed82020-05-19 08:30:12 -0700959 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 -0500960 return err
kesavand12cd8eb2020-01-20 22:25:22 -0500961 }
Scott Baker9173ed82020-05-19 08:30:12 -0700962
kesavand12cd8eb2020-01-20 22:25:22 -0500963 return nil
964}
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -0800965
Rohan Agrawald7df3772020-06-29 11:23:36 +0000966func (options *DevicePmConfigSetMaxSkew) Execute(args []string) error {
967 conn, err := NewConnection()
968 if err != nil {
969 return err
970 }
971 defer conn.Close()
972
973 client := voltha.NewVolthaServiceClient(conn)
974
David K. Bainbridge9189c632021-03-26 21:52:21 +0000975 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Rohan Agrawald7df3772020-06-29 11:23:36 +0000976 defer cancel()
977
978 id := voltha.ID{Id: string(options.Args.Id)}
979
980 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
981 if err != nil {
982 return err
983 }
984
985 pmConfigs.MaxSkew = options.Args.MaxSkew
986
987 _, err = client.UpdateDevicePmConfigs(ctx, pmConfigs)
988 if err != nil {
989 return err
990 }
991
992 return nil
993}
994
Rohan Agrawal9228d2f2020-06-03 07:48:50 +0000995func (options *DevicePmConfigsGet) Execute(args []string) error {
996
997 conn, err := NewConnection()
998 if err != nil {
999 return err
1000 }
1001 defer conn.Close()
1002
1003 client := voltha.NewVolthaServiceClient(conn)
1004
David K. Bainbridge9189c632021-03-26 21:52:21 +00001005 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001006 defer cancel()
1007
1008 id := voltha.ID{Id: string(options.Args.Id)}
1009
1010 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
1011 if err != nil {
1012 return err
1013 }
1014
1015 outputFormat := CharReplacer.Replace(options.Format)
1016 if outputFormat == "" {
1017 outputFormat = GetCommandOptionWithDefault("device-pm-configs", "format", DEFAULT_DEVICE_PM_CONFIG_GET_FORMAT)
1018 }
1019
1020 orderBy := options.OrderBy
1021 if orderBy == "" {
1022 orderBy = GetCommandOptionWithDefault("device-pm-configs", "order", "")
1023 }
1024
1025 result := CommandResult{
1026 Format: format.Format(outputFormat),
1027 Filter: options.Filter,
1028 OrderBy: orderBy,
1029 OutputAs: toOutputType(options.OutputAs),
1030 NameLimit: options.NameLimit,
1031 Data: pmConfigs,
1032 }
1033
1034 GenerateOutput(&result)
1035 return nil
1036
1037}
1038
1039func (options *DevicePmConfigMetricList) Execute(args []string) error {
1040
1041 conn, err := NewConnection()
1042 if err != nil {
1043 return err
1044 }
1045 defer conn.Close()
1046
1047 client := voltha.NewVolthaServiceClient(conn)
1048
David K. Bainbridge9189c632021-03-26 21:52:21 +00001049 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001050 defer cancel()
1051
1052 id := voltha.ID{Id: string(options.Args.Id)}
1053
1054 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
1055 if err != nil {
1056 return err
1057 }
1058
1059 if !pmConfigs.Grouped {
1060 for _, metric := range pmConfigs.Metrics {
1061 if metric.SampleFreq == 0 {
1062 metric.SampleFreq = pmConfigs.DefaultFreq
1063 }
1064 }
Rohan Agrawalbca69122020-06-17 14:59:03 +00001065 outputFormat := CharReplacer.Replace(options.Format)
1066 if outputFormat == "" {
1067 outputFormat = GetCommandOptionWithDefault("device-pm-configs", "format", DEFAULT_DEVICE_PM_CONFIG_METRIC_LIST_FORMAT)
1068 }
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001069
Rohan Agrawalbca69122020-06-17 14:59:03 +00001070 orderBy := options.OrderBy
1071 if orderBy == "" {
1072 orderBy = GetCommandOptionWithDefault("device-pm-configs", "order", "")
1073 }
1074
1075 result := CommandResult{
1076 Format: format.Format(outputFormat),
1077 Filter: options.Filter,
1078 OrderBy: orderBy,
1079 OutputAs: toOutputType(options.OutputAs),
1080 NameLimit: options.NameLimit,
1081 Data: pmConfigs.Metrics,
1082 }
1083
1084 GenerateOutput(&result)
1085 return nil
1086 } else {
1087 return fmt.Errorf("Device '%s' does not have Non Grouped Metrics", options.Args.Id)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001088 }
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001089}
1090
1091func (options *DevicePmConfigMetricEnable) Execute(args []string) error {
1092
1093 conn, err := NewConnection()
1094 if err != nil {
1095 return err
1096 }
1097 defer conn.Close()
1098
1099 client := voltha.NewVolthaServiceClient(conn)
1100
David K. Bainbridge9189c632021-03-26 21:52:21 +00001101 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001102 defer cancel()
1103
1104 id := voltha.ID{Id: string(options.Args.Id)}
1105
1106 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
1107 if err != nil {
1108 return err
1109 }
1110
1111 if !pmConfigs.Grouped {
Rohan Agrawalbca69122020-06-17 14:59:03 +00001112 metrics := make(map[string]struct{})
1113 for _, metric := range pmConfigs.Metrics {
1114 metrics[metric.Name] = struct{}{}
1115 }
1116
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001117 for _, metric := range pmConfigs.Metrics {
1118 for _, mName := range options.Args.Metrics {
Rohan Agrawalbca69122020-06-17 14:59:03 +00001119 if _, exist := metrics[string(mName)]; !exist {
1120 return fmt.Errorf("Metric Name '%s' does not exist", mName)
1121 }
1122
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001123 if string(mName) == metric.Name && !metric.Enabled {
1124 metric.Enabled = true
1125 _, err := client.UpdateDevicePmConfigs(ctx, pmConfigs)
1126 if err != nil {
1127 return err
1128 }
1129 }
1130 }
1131 }
Rohan Agrawalbca69122020-06-17 14:59:03 +00001132 } else {
1133 return fmt.Errorf("Device '%s' does not have Non Grouped Metrics", options.Args.Id)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001134 }
1135 return nil
1136}
1137
1138func (options *DevicePmConfigMetricDisable) Execute(args []string) error {
1139
1140 conn, err := NewConnection()
1141 if err != nil {
1142 return err
1143 }
1144 defer conn.Close()
1145
1146 client := voltha.NewVolthaServiceClient(conn)
1147
David K. Bainbridge9189c632021-03-26 21:52:21 +00001148 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001149 defer cancel()
1150
1151 id := voltha.ID{Id: string(options.Args.Id)}
1152
1153 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
1154 if err != nil {
1155 return err
1156 }
1157
1158 if !pmConfigs.Grouped {
Rohan Agrawalbca69122020-06-17 14:59:03 +00001159 metrics := make(map[string]struct{})
1160 for _, metric := range pmConfigs.Metrics {
1161 metrics[metric.Name] = struct{}{}
1162 }
1163
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001164 for _, metric := range pmConfigs.Metrics {
1165 for _, mName := range options.Args.Metrics {
Rohan Agrawalbca69122020-06-17 14:59:03 +00001166 if _, have := metrics[string(mName)]; !have {
1167 return fmt.Errorf("Metric Name '%s' does not exist", mName)
1168 }
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001169 if string(mName) == metric.Name && metric.Enabled {
1170 metric.Enabled = false
1171 _, err := client.UpdateDevicePmConfigs(ctx, pmConfigs)
1172 if err != nil {
1173 return err
1174 }
Rohan Agrawalbca69122020-06-17 14:59:03 +00001175 } else {
1176 return fmt.Errorf("Metric '%s' cannot be disabled", string(mName))
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001177 }
1178 }
1179 }
Rohan Agrawalbca69122020-06-17 14:59:03 +00001180 } else {
1181 return fmt.Errorf("Device '%s' does not have Non Grouped Metrics", options.Args.Id)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001182 }
1183 return nil
1184}
1185
1186func (options *DevicePmConfigGroupEnable) Execute(args []string) error {
1187
1188 conn, err := NewConnection()
1189 if err != nil {
1190 return err
1191 }
1192 defer conn.Close()
1193
1194 client := voltha.NewVolthaServiceClient(conn)
1195
David K. Bainbridge9189c632021-03-26 21:52:21 +00001196 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001197 defer cancel()
1198
1199 id := voltha.ID{Id: string(options.Args.Id)}
1200
1201 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
1202 if err != nil {
1203 return err
1204 }
1205
1206 if pmConfigs.Grouped {
Rohan Agrawalbca69122020-06-17 14:59:03 +00001207 groups := make(map[string]struct{})
1208 for _, group := range pmConfigs.Groups {
1209 groups[group.GroupName] = struct{}{}
1210 }
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001211 for _, group := range pmConfigs.Groups {
Girish Gowdra610acb42021-01-27 13:33:57 -08001212 if _, have := groups[string(options.Args.Group)]; !have {
1213 return fmt.Errorf("Group Name '%s' does not exist", options.Args.Group)
1214 }
1215 if string(options.Args.Group) == group.GroupName && !group.Enabled {
1216 group.Enabled = true
1217 _, err := client.UpdateDevicePmConfigs(ctx, pmConfigs)
1218 if err != nil {
1219 return err
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001220 }
1221 }
1222 }
Rohan Agrawalbca69122020-06-17 14:59:03 +00001223 } else {
1224 return fmt.Errorf("Device '%s' does not have Group Metrics", options.Args.Id)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001225 }
1226 return nil
1227}
1228
1229func (options *DevicePmConfigGroupDisable) Execute(args []string) error {
1230
1231 conn, err := NewConnection()
1232 if err != nil {
1233 return err
1234 }
1235 defer conn.Close()
1236
1237 client := voltha.NewVolthaServiceClient(conn)
1238
David K. Bainbridge9189c632021-03-26 21:52:21 +00001239 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001240 defer cancel()
1241
1242 id := voltha.ID{Id: string(options.Args.Id)}
1243
1244 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
1245 if err != nil {
1246 return err
1247 }
1248
1249 if pmConfigs.Grouped {
Rohan Agrawalbca69122020-06-17 14:59:03 +00001250 groups := make(map[string]struct{})
1251 for _, group := range pmConfigs.Groups {
1252 groups[group.GroupName] = struct{}{}
1253 }
1254
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001255 for _, group := range pmConfigs.Groups {
Girish Gowdra610acb42021-01-27 13:33:57 -08001256 if _, have := groups[string(options.Args.Group)]; !have {
1257 return fmt.Errorf("Group Name '%s' does not exist", options.Args.Group)
1258 }
Rohan Agrawalbca69122020-06-17 14:59:03 +00001259
Girish Gowdra610acb42021-01-27 13:33:57 -08001260 if string(options.Args.Group) == group.GroupName && group.Enabled {
1261 group.Enabled = false
1262 _, err := client.UpdateDevicePmConfigs(ctx, pmConfigs)
1263 if err != nil {
1264 return err
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001265 }
1266 }
1267 }
Rohan Agrawalbca69122020-06-17 14:59:03 +00001268 } else {
1269 return fmt.Errorf("Device '%s' does not have Group Metrics", options.Args.Id)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001270 }
1271 return nil
1272}
1273
Girish Gowdra610acb42021-01-27 13:33:57 -08001274func (options *DevicePmConfigGroupFrequencySet) Execute(args []string) error {
1275
1276 conn, err := NewConnection()
1277 if err != nil {
1278 return err
1279 }
1280 defer conn.Close()
1281
1282 client := voltha.NewVolthaServiceClient(conn)
1283
David K. Bainbridge9189c632021-03-26 21:52:21 +00001284 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Girish Gowdra610acb42021-01-27 13:33:57 -08001285 defer cancel()
1286
1287 id := voltha.ID{Id: string(options.Args.Id)}
1288
1289 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
1290 if err != nil {
1291 return err
1292 }
1293
1294 if pmConfigs.Grouped {
1295 groups := make(map[string]struct{})
1296 for _, group := range pmConfigs.Groups {
1297 groups[group.GroupName] = struct{}{}
1298 }
1299
1300 for _, group := range pmConfigs.Groups {
1301 if _, have := groups[string(options.Args.Group)]; !have {
1302 return fmt.Errorf("group name '%s' does not exist", options.Args.Group)
1303 }
1304
1305 if string(options.Args.Group) == group.GroupName {
1306 if !group.Enabled {
1307 return fmt.Errorf("group '%s' is not enabled", options.Args.Group)
1308 }
1309 group.GroupFreq = uint32(options.Args.Interval.Seconds())
1310 _, err = client.UpdateDevicePmConfigs(ctx, pmConfigs)
1311 if err != nil {
1312 return err
1313 }
1314 }
1315 }
1316 } else {
1317 return fmt.Errorf("device '%s' does not have group metrics", options.Args.Id)
1318 }
1319 return nil
1320}
1321
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001322func (options *DevicePmConfigGroupList) Execute(args []string) error {
1323
1324 conn, err := NewConnection()
1325 if err != nil {
1326 return err
1327 }
1328 defer conn.Close()
1329
1330 client := voltha.NewVolthaServiceClient(conn)
1331
David K. Bainbridge9189c632021-03-26 21:52:21 +00001332 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001333 defer cancel()
1334
1335 id := voltha.ID{Id: string(options.Args.Id)}
1336
1337 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
1338 if err != nil {
1339 return err
1340 }
1341
1342 if pmConfigs.Grouped {
1343 for _, group := range pmConfigs.Groups {
1344 if group.GroupFreq == 0 {
1345 group.GroupFreq = pmConfigs.DefaultFreq
1346 }
1347 }
Rohan Agrawalbca69122020-06-17 14:59:03 +00001348 outputFormat := CharReplacer.Replace(options.Format)
1349 if outputFormat == "" {
1350 outputFormat = GetCommandOptionWithDefault("device-pm-configs", "format", DEFAULT_DEVICE_PM_CONFIG_GROUP_LIST_FORMAT)
1351 }
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001352
Rohan Agrawalbca69122020-06-17 14:59:03 +00001353 orderBy := options.OrderBy
1354 if orderBy == "" {
1355 orderBy = GetCommandOptionWithDefault("device-pm-configs", "order", "")
1356 }
1357
1358 result := CommandResult{
1359 Format: format.Format(outputFormat),
1360 Filter: options.Filter,
1361 OrderBy: orderBy,
1362 OutputAs: toOutputType(options.OutputAs),
1363 NameLimit: options.NameLimit,
1364 Data: pmConfigs.Groups,
1365 }
1366
1367 GenerateOutput(&result)
1368 } else {
1369 return fmt.Errorf("Device '%s' does not have Group Metrics", string(options.Args.Id))
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001370 }
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001371 return nil
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001372}
1373
1374func (options *DevicePmConfigGroupMetricList) Execute(args []string) error {
1375
1376 var metrics []*voltha.PmConfig
1377 conn, err := NewConnection()
1378 if err != nil {
1379 return err
1380 }
1381 defer conn.Close()
1382
1383 client := voltha.NewVolthaServiceClient(conn)
1384
David K. Bainbridge9189c632021-03-26 21:52:21 +00001385 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001386 defer cancel()
1387
1388 id := voltha.ID{Id: string(options.Args.Id)}
1389
1390 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
1391 if err != nil {
1392 return err
1393 }
1394
1395 for _, groups := range pmConfigs.Groups {
1396
1397 if string(options.Args.Group) == groups.GroupName {
1398 for _, metric := range groups.Metrics {
1399 if metric.SampleFreq == 0 && groups.GroupFreq == 0 {
1400 metric.SampleFreq = pmConfigs.DefaultFreq
1401 } else {
1402 metric.SampleFreq = groups.GroupFreq
1403 }
1404 }
1405 metrics = groups.Metrics
1406 }
1407 }
1408
1409 outputFormat := CharReplacer.Replace(options.Format)
1410 if outputFormat == "" {
1411 outputFormat = GetCommandOptionWithDefault("device-pm-configs", "format", DEFAULT_DEVICE_PM_CONFIG_METRIC_LIST_FORMAT)
1412 }
1413
1414 orderBy := options.OrderBy
1415 if orderBy == "" {
1416 orderBy = GetCommandOptionWithDefault("device-pm-configs", "order", "")
1417 }
1418
1419 result := CommandResult{
1420 Format: format.Format(outputFormat),
1421 Filter: options.Filter,
1422 OrderBy: orderBy,
1423 OutputAs: toOutputType(options.OutputAs),
1424 NameLimit: options.NameLimit,
1425 Data: metrics,
1426 }
1427
1428 GenerateOutput(&result)
1429 return nil
1430
1431}
1432
1433func (options *DevicePmConfigFrequencySet) Execute(args []string) error {
1434
1435 conn, err := NewConnection()
1436 if err != nil {
1437 return err
1438 }
1439 defer conn.Close()
1440
1441 client := voltha.NewVolthaServiceClient(conn)
1442
David K. Bainbridge9189c632021-03-26 21:52:21 +00001443 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001444 defer cancel()
1445
1446 id := voltha.ID{Id: string(options.Args.Id)}
1447
1448 pmConfigs, err := client.ListDevicePmConfigs(ctx, &id)
1449 if err != nil {
1450 return err
1451 }
1452
Girish Gowdra610acb42021-01-27 13:33:57 -08001453 pmConfigs.DefaultFreq = uint32(options.Args.Interval.Seconds())
Rohan Agrawal9228d2f2020-06-03 07:48:50 +00001454
1455 _, err = client.UpdateDevicePmConfigs(ctx, pmConfigs)
1456 if err != nil {
1457 return err
1458 }
1459
1460 outputFormat := CharReplacer.Replace(options.Format)
1461 if outputFormat == "" {
1462 outputFormat = GetCommandOptionWithDefault("device-pm-configs", "format", DEFAULT_DEVICE_PM_CONFIG_GET_FORMAT)
1463 }
1464 if options.Quiet {
1465 outputFormat = "{{.Id}}"
1466 }
1467
1468 result := CommandResult{
1469 Format: format.Format(outputFormat),
1470 OutputAs: toOutputType(options.OutputAs),
1471 NameLimit: options.NameLimit,
1472 Data: pmConfigs,
1473 }
1474
1475 GenerateOutput(&result)
1476 return nil
1477
1478}
1479
kesavand3e2f9f62021-04-22 11:06:38 +05301480func (options *OnuDownloadImage) Execute(args []string) error {
1481
1482 conn, err := NewConnection()
1483 if err != nil {
1484 return err
1485 }
1486 defer conn.Close()
1487
1488 client := voltha.NewVolthaServiceClient(conn)
1489
1490 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
1491 defer cancel()
1492
1493 var devIDList []*common.ID
1494 for _, i := range options.Args.IDs {
1495
1496 devIDList = append(devIDList, &common.ID{Id: string(i)})
1497 }
1498
1499 downloadImage := voltha.DeviceImageDownloadRequest{
1500 DeviceId: devIDList,
1501 Image: &voltha.Image{
1502 Url: options.Args.Url,
1503 Crc32: options.Args.Crc,
ssiddiqui7bc89e92021-05-20 20:58:02 +05301504 Vendor: options.Args.Vendor,
kesavand3e2f9f62021-04-22 11:06:38 +05301505 Version: options.Args.ImageVersion,
1506 },
1507 ActivateOnSuccess: options.Args.ActivateOnSuccess,
1508 CommitOnSuccess: options.Args.CommitOnSuccess,
1509 }
1510
1511 _, err = client.DownloadImageToDevice(ctx, &downloadImage)
1512 if err != nil {
1513 return err
1514 }
1515
1516 return nil
1517
1518}
1519
1520func (options *OnuActivateImage) Execute(args []string) error {
1521
1522 conn, err := NewConnection()
1523 if err != nil {
1524 return err
1525 }
1526 defer conn.Close()
1527
1528 client := voltha.NewVolthaServiceClient(conn)
1529
1530 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
1531 defer cancel()
1532
1533 var devIDList []*common.ID
1534 for _, i := range options.Args.IDs {
1535
1536 devIDList = append(devIDList, &common.ID{Id: string(i)})
1537 }
1538
1539 downloadImage := voltha.DeviceImageRequest{
1540 DeviceId: devIDList,
1541 Version: options.Args.ImageVersion,
1542 CommitOnSuccess: options.Args.CommitOnSuccess,
1543 }
1544
1545 _, err = client.ActivateImage(ctx, &downloadImage)
1546 if err != nil {
1547 return err
1548 }
1549
1550 return nil
1551
1552}
1553
1554func (options *OnuAbortUpgradeImage) Execute(args []string) error {
1555
1556 conn, err := NewConnection()
1557 if err != nil {
1558 return err
1559 }
1560 defer conn.Close()
1561
1562 client := voltha.NewVolthaServiceClient(conn)
1563
1564 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
1565 defer cancel()
1566
1567 var devIDList []*common.ID
1568 for _, i := range options.Args.IDs {
1569
1570 devIDList = append(devIDList, &common.ID{Id: string(i)})
1571 }
1572
1573 downloadImage := voltha.DeviceImageRequest{
1574 DeviceId: devIDList,
1575 Version: options.Args.ImageVersion,
1576 }
1577
1578 _, err = client.AbortImageUpgradeToDevice(ctx, &downloadImage)
1579 if err != nil {
1580 return err
1581 }
1582
1583 return nil
1584
1585}
1586
1587func (options *OnuCommitImage) Execute(args []string) error {
1588
1589 conn, err := NewConnection()
1590 if err != nil {
1591 return err
1592 }
1593 defer conn.Close()
1594
1595 client := voltha.NewVolthaServiceClient(conn)
1596
1597 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
1598 defer cancel()
1599
1600 var devIDList []*common.ID
1601 for _, i := range options.Args.IDs {
1602
1603 devIDList = append(devIDList, &common.ID{Id: string(i)})
1604 }
1605 downloadImage := voltha.DeviceImageRequest{
1606 DeviceId: devIDList,
1607 Version: options.Args.ImageVersion,
1608 }
1609
1610 _, err = client.CommitImage(ctx, &downloadImage)
1611 if err != nil {
1612 return err
1613 }
1614
1615 return nil
1616
1617}
1618
1619func (options *OnuListImages) Execute(args []string) error {
1620
1621 conn, err := NewConnection()
1622 if err != nil {
1623 return err
1624 }
1625 defer conn.Close()
1626
1627 client := voltha.NewVolthaServiceClient(conn)
1628
1629 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
1630 defer cancel()
1631
1632 id := common.ID{Id: string(options.Args.Id)}
1633
1634 onuImages, err := client.GetOnuImages(ctx, &id)
1635 if err != nil {
1636 return err
1637 }
1638
1639 outputFormat := CharReplacer.Replace(options.Format)
1640 if outputFormat == "" {
1641 outputFormat = GetCommandOptionWithDefault("onu-image-list", "format", ONU_IMAGE_LIST_FORMAT)
1642 }
1643
1644 if options.Quiet {
1645 outputFormat = "{{.Id}}"
1646 }
1647
1648 //TODO orderby
1649
1650 // Make sure json output prints an empty list, not "null"
1651 if onuImages.Items == nil {
1652 onuImages.Items = make([]*voltha.OnuImage, 0)
1653 }
1654
1655 result := CommandResult{
1656 Format: format.Format(outputFormat),
1657 OutputAs: toOutputType(options.OutputAs),
1658 NameLimit: options.NameLimit,
1659 Data: onuImages.Items,
1660 }
1661
1662 GenerateOutput(&result)
1663 return nil
1664
1665}
1666
1667func (options *OnuImageStatus) Execute(args []string) error {
1668
1669 conn, err := NewConnection()
1670 if err != nil {
1671 return err
1672 }
1673 defer conn.Close()
1674
1675 client := voltha.NewVolthaServiceClient(conn)
1676
1677 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
1678 defer cancel()
1679
1680 var devIDList []*common.ID
1681 for _, i := range options.Args.IDs {
1682
1683 devIDList = append(devIDList, &common.ID{Id: string(i)})
1684 }
1685
1686 imageStatusReq := voltha.DeviceImageRequest{
1687 DeviceId: devIDList,
1688 Version: options.Args.ImageVersion,
1689 }
1690 imageStatus, err := client.GetImageStatus(ctx, &imageStatusReq)
1691 if err != nil {
1692 return err
1693 }
1694
1695 outputFormat := CharReplacer.Replace(options.Format)
1696 if outputFormat == "" {
1697 outputFormat = GetCommandOptionWithDefault("device-image-list", "format", ONU_IMAGE_STATUS_FORMAT)
1698 }
1699
1700 if options.Quiet {
1701 outputFormat = "{{.Id}}"
1702 }
1703
1704 //TODO orderby
1705
1706 // Make sure json output prints an empty list, not "null"
1707 if imageStatus.DeviceImageStates == nil {
1708 imageStatus.DeviceImageStates = make([]*voltha.DeviceImageState, 0)
1709 }
1710
1711 result := CommandResult{
1712 Format: format.Format(outputFormat),
1713 OutputAs: toOutputType(options.OutputAs),
1714 NameLimit: options.NameLimit,
1715 Data: imageStatus.DeviceImageStates,
1716 }
1717
1718 GenerateOutput(&result)
1719 return nil
1720
1721}
1722
Andrea Campanella791d88b2021-01-08 13:29:00 +01001723func (options *DeviceOnuListImages) Execute(args []string) error {
1724
1725 conn, err := NewConnection()
1726 if err != nil {
1727 return err
1728 }
1729 defer conn.Close()
1730
1731 client := voltha.NewVolthaServiceClient(conn)
1732
David K. Bainbridge9189c632021-03-26 21:52:21 +00001733 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Andrea Campanella791d88b2021-01-08 13:29:00 +01001734 defer cancel()
1735
1736 id := common.ID{Id: string(options.Args.Id)}
1737
1738 imageDownloads, err := client.ListImageDownloads(ctx, &id)
1739 if err != nil {
1740 return err
1741 }
1742
1743 outputFormat := CharReplacer.Replace(options.Format)
1744 if outputFormat == "" {
1745 outputFormat = GetCommandOptionWithDefault("device-image-list", "format", DEFAULT_DEVICE_IMAGE_LIST_GET_FORMAT)
1746 }
1747
1748 if options.Quiet {
1749 outputFormat = "{{.Id}}"
1750 }
1751
1752 //TODO orderby
1753
1754 // Make sure json output prints an empty list, not "null"
1755 if imageDownloads.Items == nil {
1756 imageDownloads.Items = make([]*voltha.ImageDownload, 0)
1757 }
1758
1759 result := CommandResult{
1760 Format: format.Format(outputFormat),
1761 OutputAs: toOutputType(options.OutputAs),
1762 NameLimit: options.NameLimit,
1763 Data: imageDownloads.Items,
1764 }
1765
1766 GenerateOutput(&result)
1767 return nil
1768
1769}
1770
1771func (options *DeviceOnuDownloadImage) Execute(args []string) error {
1772
1773 conn, err := NewConnection()
1774 if err != nil {
1775 return err
1776 }
1777 defer conn.Close()
1778
1779 client := voltha.NewVolthaServiceClient(conn)
1780
David K. Bainbridge9189c632021-03-26 21:52:21 +00001781 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Andrea Campanella791d88b2021-01-08 13:29:00 +01001782 defer cancel()
1783
1784 downloadImage := voltha.ImageDownload{
1785 Id: string(options.Args.Id),
1786 Name: options.Args.Name,
1787 Url: options.Args.Url,
1788 Crc: options.Args.Crc,
1789 LocalDir: options.Args.LocalDir,
1790 }
1791
1792 _, err = client.DownloadImage(ctx, &downloadImage)
1793 if err != nil {
1794 return err
1795 }
1796
1797 return nil
1798
1799}
1800
1801func (options *DeviceOnuActivateImageUpdate) Execute(args []string) error {
1802
1803 conn, err := NewConnection()
1804 if err != nil {
1805 return err
1806 }
1807 defer conn.Close()
1808
1809 client := voltha.NewVolthaServiceClient(conn)
1810
David K. Bainbridge9189c632021-03-26 21:52:21 +00001811 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Andrea Campanella791d88b2021-01-08 13:29:00 +01001812 defer cancel()
1813
1814 downloadImage := voltha.ImageDownload{
1815 Id: string(options.Args.Id),
1816 Name: options.Args.Name,
1817 ImageVersion: options.Args.ImageVersion,
1818 SaveConfig: options.Args.SaveConfig,
1819 LocalDir: options.Args.LocalDir,
1820 }
1821
1822 _, err = client.ActivateImageUpdate(ctx, &downloadImage)
1823 if err != nil {
1824 return err
1825 }
1826
1827 return nil
1828
1829}
1830
Scott Baker9173ed82020-05-19 08:30:12 -07001831type ReturnValueRow struct {
1832 Name string `json:"name"`
1833 Result interface{} `json:"result"`
1834}
1835
kesavand8ec4fc02021-01-27 09:10:22 -05001836func (options *DeviceGetPortStats) Execute(args []string) error {
1837 conn, err := NewConnection()
1838 if err != nil {
1839 return err
1840 }
1841 defer conn.Close()
1842 client := extension.NewExtensionClient(conn)
1843 var portType extension.GetOltPortCounters_PortType
1844
1845 if options.Args.PortType == "pon" {
1846 portType = extension.GetOltPortCounters_Port_PON_OLT
1847 } else if options.Args.PortType == "nni" {
1848
1849 portType = extension.GetOltPortCounters_Port_ETHERNET_NNI
1850 } else {
1851 return fmt.Errorf("expected interface type pon/nni, provided %s", options.Args.PortType)
1852 }
1853
1854 singleGetValReq := extension.SingleGetValueRequest{
1855 TargetId: string(options.Args.Id),
1856 Request: &extension.GetValueRequest{
1857 Request: &extension.GetValueRequest_OltPortInfo{
1858 OltPortInfo: &extension.GetOltPortCounters{
1859 PortNo: options.Args.PortNo,
1860 PortType: portType,
1861 },
1862 },
1863 },
1864 }
1865
David K. Bainbridge9189c632021-03-26 21:52:21 +00001866 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
kesavand8ec4fc02021-01-27 09:10:22 -05001867 defer cancel()
1868 rv, err := client.GetExtValue(ctx, &singleGetValReq)
1869 if err != nil {
1870 Error.Printf("Error getting value on device Id %s,err=%s\n", options.Args.Id, ErrorToString(err))
1871 return err
1872 }
1873
1874 if rv.Response.Status != extension.GetValueResponse_OK {
1875 return fmt.Errorf("failed to get port stats %v", rv.Response.ErrReason.String())
1876 }
1877
1878 outputFormat := CharReplacer.Replace(options.Format)
1879 if outputFormat == "" {
1880 outputFormat = GetCommandOptionWithDefault("device-get-port-status", "format", DEFAULT_DEVICE_GET_PORT_STATUS_FORMAT)
1881 }
1882
1883 result := CommandResult{
1884 Format: format.Format(outputFormat),
1885 OutputAs: toOutputType(options.OutputAs),
1886 NameLimit: options.NameLimit,
1887 Data: rv.GetResponse().GetPortCoutners(),
1888 }
1889 GenerateOutput(&result)
1890 return nil
1891}
1892
Himani Chawla40acc122021-05-26 18:52:29 +05301893func (options *GetOnuStats) Execute(args []string) error {
1894 conn, err := NewConnection()
1895 if err != nil {
1896 return err
1897 }
1898 defer conn.Close()
1899 client := extension.NewExtensionClient(conn)
1900
1901 singleGetValReq := extension.SingleGetValueRequest{
1902 TargetId: string(options.Args.OltId),
1903 Request: &extension.GetValueRequest{
1904 Request: &extension.GetValueRequest_OnuPonInfo{
1905 OnuPonInfo: &extension.GetOnuCountersRequest{
1906 IntfId: options.Args.IntfId,
1907 OnuId: options.Args.OnuId,
1908 },
1909 },
1910 },
1911 }
1912 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
1913 defer cancel()
1914 rv, err := client.GetExtValue(ctx, &singleGetValReq)
1915 if err != nil {
1916 Error.Printf("Error getting value on device Id %s,err=%s\n", options.Args.OltId, ErrorToString(err))
1917 return err
1918 }
1919
1920 if rv.Response.Status != extension.GetValueResponse_OK {
1921 return fmt.Errorf("failed to get onu stats %v", rv.Response.ErrReason.String())
1922 }
1923 outputFormat := CharReplacer.Replace(options.Format)
1924 data, formatStr := buildOnuStatsOutputFormat(rv.GetResponse().GetOnuPonCounters())
1925 if outputFormat == "" {
1926 outputFormat = GetCommandOptionWithDefault("device-get-onu-status", "format", formatStr)
1927 }
1928
1929 result := CommandResult{
1930 Format: format.Format(outputFormat),
1931 OutputAs: toOutputType(options.OutputAs),
1932 NameLimit: options.NameLimit,
1933 Data: data,
1934 }
1935 GenerateOutput(&result)
1936 return nil
1937}
1938
kesavand6d1131f2021-02-05 22:38:15 +05301939func (options *UniStatus) Execute(args []string) error {
1940 conn, err := NewConnection()
1941 if err != nil {
1942 return err
1943 }
1944 defer conn.Close()
1945 client := extension.NewExtensionClient(conn)
1946
1947 singleGetValReq := extension.SingleGetValueRequest{
1948 TargetId: string(options.Args.Id),
1949 Request: &extension.GetValueRequest{
1950 Request: &extension.GetValueRequest_UniInfo{
1951 UniInfo: &extension.GetOnuUniInfoRequest{
1952 UniIndex: options.Args.UniIndex,
1953 },
1954 },
1955 },
1956 }
David K. Bainbridge9189c632021-03-26 21:52:21 +00001957 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
kesavand6d1131f2021-02-05 22:38:15 +05301958 defer cancel()
1959 rv, err := client.GetExtValue(ctx, &singleGetValReq)
1960 if err != nil {
1961 Error.Printf("Error getting value on device Id %s,err=%s\n", options.Args.Id, ErrorToString(err))
1962 return err
1963 }
1964 if rv.Response.Status != extension.GetValueResponse_OK {
1965 return fmt.Errorf("failed to get uni status %v", rv.Response.ErrReason.String())
1966 }
1967 outputFormat := CharReplacer.Replace(options.Format)
1968 if outputFormat == "" {
1969 outputFormat = GetCommandOptionWithDefault("device-get-uni-status", "format", DEFAULT_DEVICE_GET_UNI_STATUS_FORMAT)
1970 }
1971 result := CommandResult{
1972 Format: format.Format(outputFormat),
1973 OutputAs: toOutputType(options.OutputAs),
1974 NameLimit: options.NameLimit,
1975 Data: rv.GetResponse().GetUniInfo(),
1976 }
1977 GenerateOutput(&result)
1978 return nil
1979}
1980
Girish Gowdra4f5ce7c2021-04-29 18:53:21 -07001981func (options *OnuPonOpticalInfo) Execute(args []string) error {
1982 conn, err := NewConnection()
1983 if err != nil {
1984 return err
1985 }
1986 defer conn.Close()
1987 client := extension.NewExtensionClient(conn)
1988
1989 singleGetValReq := extension.SingleGetValueRequest{
1990 TargetId: string(options.Args.Id),
1991 Request: &extension.GetValueRequest{
1992 Request: &extension.GetValueRequest_OnuOpticalInfo{
1993 OnuOpticalInfo: &extension.GetOnuPonOpticalInfo{},
1994 },
1995 },
1996 }
1997 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
1998 defer cancel()
1999 rv, err := client.GetExtValue(ctx, &singleGetValReq)
2000 if err != nil {
2001 Error.Printf("Error getting value on device Id %s,err=%s\n", options.Args.Id, ErrorToString(err))
2002 return err
2003 }
2004 if rv.Response.Status != extension.GetValueResponse_OK {
2005 return fmt.Errorf("failed to get onu pon optical info %v", rv.Response.ErrReason.String())
2006 }
2007 outputFormat := CharReplacer.Replace(options.Format)
2008 if outputFormat == "" {
2009 outputFormat = GetCommandOptionWithDefault("device-get-onu-pon-optical-info", "format", DEFAULT_ONU_PON_OPTICAL_INFO_STATUS_FORMAT)
2010 }
2011 result := CommandResult{
2012 Format: format.Format(outputFormat),
2013 OutputAs: toOutputType(options.OutputAs),
2014 NameLimit: options.NameLimit,
2015 Data: rv.GetResponse().GetOnuOpticalInfo(),
2016 }
2017 GenerateOutput(&result)
2018 return nil
2019}
2020
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -08002021/*Device get Onu Distance */
2022func (options *DeviceGetExtValue) Execute(args []string) error {
2023 conn, err := NewConnection()
2024 if err != nil {
2025 return err
2026 }
2027 defer conn.Close()
2028
Scott Baker9173ed82020-05-19 08:30:12 -07002029 client := voltha.NewVolthaServiceClient(conn)
2030
2031 valueflag, okay := common.ValueType_Type_value[string(options.Args.Valueflag)]
2032 if !okay {
2033 Error.Printf("Unknown valueflag %s\n", options.Args.Valueflag)
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -08002034 }
2035
Scott Baker9173ed82020-05-19 08:30:12 -07002036 val := voltha.ValueSpecifier{Id: string(options.Args.Id), Value: common.ValueType_Type(valueflag)}
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -08002037
David K. Bainbridge9189c632021-03-26 21:52:21 +00002038 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -08002039 defer cancel()
2040
Scott Baker9173ed82020-05-19 08:30:12 -07002041 rv, err := client.GetExtValue(ctx, &val)
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -08002042 if err != nil {
2043 Error.Printf("Error getting value on device Id %s,err=%s\n", options.Args.Id, ErrorToString(err))
2044 return err
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -08002045 }
2046
Scott Baker9173ed82020-05-19 08:30:12 -07002047 var rows []ReturnValueRow
2048 for name, num := range common.ValueType_Type_value {
2049 if num == 0 {
2050 // EMPTY is not a real value
2051 continue
2052 }
2053 if (rv.Error & uint32(num)) != 0 {
2054 row := ReturnValueRow{Name: name, Result: "Error"}
2055 rows = append(rows, row)
2056 }
2057 if (rv.Unsupported & uint32(num)) != 0 {
2058 row := ReturnValueRow{Name: name, Result: "Unsupported"}
2059 rows = append(rows, row)
2060 }
2061 if (rv.Set & uint32(num)) != 0 {
2062 switch name {
2063 case "DISTANCE":
2064 row := ReturnValueRow{Name: name, Result: rv.Distance}
2065 rows = append(rows, row)
2066 default:
2067 row := ReturnValueRow{Name: name, Result: "Unimplemented-in-voltctl"}
2068 rows = append(rows, row)
2069 }
2070 }
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -08002071 }
2072
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -08002073 outputFormat := CharReplacer.Replace(options.Format)
2074 if outputFormat == "" {
2075 outputFormat = GetCommandOptionWithDefault("device-value-get", "format", DEFAULT_DEVICE_VALUE_GET_FORMAT)
2076 }
2077
2078 result := CommandResult{
2079 Format: format.Format(outputFormat),
2080 OutputAs: toOutputType(options.OutputAs),
2081 NameLimit: options.NameLimit,
Scott Baker9173ed82020-05-19 08:30:12 -07002082 Data: rows,
Dinesh Belwalkarc9aa6d82020-03-04 15:22:17 -08002083 }
2084 GenerateOutput(&result)
2085 return nil
2086}