blob: 5f6874b8752368ad9c82894a949204f06040de20 [file] [log] [blame]
Matteo Scandolo8df63df2019-09-12 10:34:32 -07001/*
2 * Portions copyright 2019-present Open Networking Foundation
3 * Original copyright 2019-present Ciena Corporation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18package commands
19
20import (
21 "context"
Matteo Scandolo10f965c2019-09-24 10:40:46 -070022 "fmt"
Anand S Katti09541352020-01-29 15:54:01 +053023 "os"
24 "strconv"
25 "strings"
26
Matteo Scandolo8df63df2019-09-12 10:34:32 -070027 "github.com/jessevdk/go-flags"
Anand S Katti09541352020-01-29 15:54:01 +053028 "github.com/olekukonko/tablewriter"
Matteo Scandolo8df63df2019-09-12 10:34:32 -070029 pb "github.com/opencord/bbsim/api/bbsim"
30 "github.com/opencord/bbsim/internal/bbsimctl/config"
31 "github.com/opencord/cordctl/pkg/format"
32 log "github.com/sirupsen/logrus"
33 "google.golang.org/grpc"
Matteo Scandolo8df63df2019-09-12 10:34:32 -070034)
35
36const (
Matteo Scandolo4a036262020-08-17 15:56:13 -070037 DEFAULT_ONU_DEVICE_HEADER_FORMAT = "table{{ .PonPortID }}\t{{ .ID }}\t{{ .PortNo }}\t{{ .SerialNumber }}\t{{ .OperState }}\t{{ .InternalState }}"
38 DEFAULT_ONU_DEVICE_HEADER_FORMAT_WITH_SERVICES = "table{{ .PonPortID }}\t{{ .ID }}\t{{ .PortNo }}\t{{ .SerialNumber }}\t{{ .OperState }}\t{{ .InternalState }}\t{{ .Services }}"
Matteo Scandolo8df63df2019-09-12 10:34:32 -070039)
40
Matteo Scandolo10f965c2019-09-24 10:40:46 -070041type OnuSnString string
Arjun E K57a7fcb2020-01-30 06:44:45 +000042type IgmpSubAction string
43
44const IgmpJoinKey string = "join"
45const IgmpLeaveKey string = "leave"
Arjun E Kdd443f02020-02-07 15:24:01 +000046const IgmpJoinKeyV3 string = "joinv3"
Arjun E K57a7fcb2020-01-30 06:44:45 +000047
Matteo Scandolo618a6582020-09-09 12:21:29 -070048var IgmpAllowedActions = []string{IgmpJoinKey, IgmpLeaveKey, IgmpJoinKeyV3}
49
Matteo Scandolo4a036262020-08-17 15:56:13 -070050type ONUList struct {
51 Verbose bool `short:"v" long:"verbose" description:"Print all the informations we have about ONUs"`
52}
Matteo Scandolod2ca2c72019-10-04 16:50:22 -070053
54type ONUGet struct {
Matteo Scandolo4a036262020-08-17 15:56:13 -070055 Verbose bool `short:"v" long:"verbose" description:"Print all the informations we have about ONUs"`
56 Args struct {
57 OnuSn OnuSnString
58 } `positional-args:"yes" required:"yes"`
59}
60
61type ONUServices struct {
Matteo Scandolod2ca2c72019-10-04 16:50:22 -070062 Args struct {
63 OnuSn OnuSnString
64 } `positional-args:"yes" required:"yes"`
65}
66
Matteo Scandolo10f965c2019-09-24 10:40:46 -070067type ONUShutDown struct {
68 Args struct {
69 OnuSn OnuSnString
70 } `positional-args:"yes" required:"yes"`
Matteo Scandolo8df63df2019-09-12 10:34:32 -070071}
72
Matteo Scandolo10f965c2019-09-24 10:40:46 -070073type ONUPowerOn struct {
74 Args struct {
75 OnuSn OnuSnString
76 } `positional-args:"yes" required:"yes"`
77}
78
Matteo Scandoloe383d5d2019-10-25 14:47:27 -070079type ONUEapolRestart struct {
80 Args struct {
81 OnuSn OnuSnString
82 } `positional-args:"yes" required:"yes"`
83}
84
85type ONUDhcpRestart struct {
86 Args struct {
87 OnuSn OnuSnString
88 } `positional-args:"yes" required:"yes"`
89}
90
Arjun E K57a7fcb2020-01-30 06:44:45 +000091type ONUIgmp struct {
92 Args struct {
93 OnuSn OnuSnString
94 SubAction IgmpSubAction
95 } `positional-args:"yes" required:"yes"`
96}
97
Pragya Arya8bdb4532020-03-02 17:08:09 +053098type ONUTrafficSchedulers struct {
99 Args struct {
100 OnuSn OnuSnString
101 } `positional-args:"yes" required:"yes"`
102}
103
104type ONUFlows struct {
105 Args struct {
106 OnuSn OnuSnString
107 } `positional-args:"yes" required:"yes"`
108}
109
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700110type ONUOptions struct {
Anand S Katti09541352020-01-29 15:54:01 +0530111 List ONUList `command:"list"`
112 Get ONUGet `command:"get"`
Matteo Scandolo4a036262020-08-17 15:56:13 -0700113 Services ONUServices `command:"services"`
Anand S Katti09541352020-01-29 15:54:01 +0530114 ShutDown ONUShutDown `command:"shutdown"`
115 PowerOn ONUPowerOn `command:"poweron"`
116 RestartEapol ONUEapolRestart `command:"auth_restart"`
117 RestartDchp ONUDhcpRestart `command:"dhcp_restart"`
118 Igmp ONUIgmp `command:"igmp"`
119 TrafficSchedulers ONUTrafficSchedulers `command:"traffic_schedulers"`
Anand S Katti86552f92020-03-03 21:56:32 +0530120 Alarms AlarmOptions `command:"alarms"`
Pragya Arya8bdb4532020-03-02 17:08:09 +0530121 Flows ONUFlows `command:"flows"`
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700122}
123
124func RegisterONUCommands(parser *flags.Parser) {
Shrey Baid688b4242020-07-10 20:40:10 +0530125 _, _ = parser.AddCommand("onu", "ONU Commands", "Commands to query and manipulate ONU devices", &ONUOptions{})
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700126}
127
128func connect() (pb.BBSimClient, *grpc.ClientConn) {
Matteo Scandolo8df63df2019-09-12 10:34:32 -0700129 conn, err := grpc.Dial(config.GlobalConfig.Server, grpc.WithInsecure())
130
131 if err != nil {
Matteo Scandolo2bf742a2019-10-01 11:33:34 -0700132 log.Fatalf("did not connect: %v", err)
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700133 return nil, conn
Matteo Scandolo8df63df2019-09-12 10:34:32 -0700134 }
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700135 return pb.NewBBSimClient(conn), conn
136}
137
138func getONUs() *pb.ONUs {
139
140 client, conn := connect()
Matteo Scandolo8df63df2019-09-12 10:34:32 -0700141 defer conn.Close()
Matteo Scandolo8df63df2019-09-12 10:34:32 -0700142
143 // Contact the server and print out its response.
Matteo Scandolo8df63df2019-09-12 10:34:32 -0700144 ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout)
145 defer cancel()
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700146
147 onus, err := client.GetONUs(ctx, &pb.Empty{})
Matteo Scandolo8df63df2019-09-12 10:34:32 -0700148 if err != nil {
Matteo Scandolo2bf742a2019-10-01 11:33:34 -0700149 log.Fatalf("could not get OLT: %v", err)
Matteo Scandolo8df63df2019-09-12 10:34:32 -0700150 return nil
151 }
152 return onus
153}
154
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700155func (options *ONUList) Execute(args []string) error {
Matteo Scandolo8df63df2019-09-12 10:34:32 -0700156 onus := getONUs()
157
158 // print out
Matteo Scandolo4a036262020-08-17 15:56:13 -0700159 var tableFormat format.Format
160 if options.Verbose {
161 tableFormat = format.Format(DEFAULT_ONU_DEVICE_HEADER_FORMAT_WITH_SERVICES)
162 } else {
163 tableFormat = format.Format(DEFAULT_ONU_DEVICE_HEADER_FORMAT)
164 }
Matteo Scandolo8df63df2019-09-12 10:34:32 -0700165 if err := tableFormat.Execute(os.Stdout, true, onus.Items); err != nil {
166 log.Fatalf("Error while formatting ONUs table: %s", err)
167 }
168
169 return nil
170}
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700171
Matteo Scandolod2ca2c72019-10-04 16:50:22 -0700172func (options *ONUGet) Execute(args []string) error {
173 client, conn := connect()
174 defer conn.Close()
175
176 ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout)
177 defer cancel()
178 req := pb.ONURequest{
179 SerialNumber: string(options.Args.OnuSn),
180 }
181 res, err := client.GetONU(ctx, &req)
182
183 if err != nil {
Matteo Scandolo4a036262020-08-17 15:56:13 -0700184 log.Fatalf("Cannot not get ONU %s: %v", options.Args.OnuSn, err)
Matteo Scandolod2ca2c72019-10-04 16:50:22 -0700185 return err
186 }
187
Matteo Scandolo4a036262020-08-17 15:56:13 -0700188 var tableFormat format.Format
189 if options.Verbose {
190 tableFormat = format.Format(DEFAULT_ONU_DEVICE_HEADER_FORMAT_WITH_SERVICES)
191 } else {
192 tableFormat = format.Format(DEFAULT_ONU_DEVICE_HEADER_FORMAT)
193 }
Matteo Scandolod2ca2c72019-10-04 16:50:22 -0700194 if err := tableFormat.Execute(os.Stdout, true, []*pb.ONU{res}); err != nil {
195 log.Fatalf("Error while formatting ONUs table: %s", err)
196 }
197
198 return nil
199}
200
Matteo Scandolo4a036262020-08-17 15:56:13 -0700201func (options *ONUServices) Execute(args []string) error {
202
203 client, conn := connect()
204 defer conn.Close()
205
206 ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout)
207 defer cancel()
208 req := pb.ONURequest{
209 SerialNumber: string(options.Args.OnuSn),
210 }
211 res, err := client.GetOnuServices(ctx, &req)
212
213 if err != nil {
214 log.Fatalf("Cannot not get services for ONU %s: %v", options.Args.OnuSn, err)
215 return err
216 }
217
218 tableFormat := format.Format(DEFAULT_SERVICE_HEADER_FORMAT)
219 if err := tableFormat.Execute(os.Stdout, true, res.Items); err != nil {
220 log.Fatalf("Error while formatting Services table: %s", err)
221 }
222
223 return nil
224}
225
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700226func (options *ONUShutDown) Execute(args []string) error {
227
228 client, conn := connect()
229 defer conn.Close()
230
231 ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout)
232 defer cancel()
233 req := pb.ONURequest{
234 SerialNumber: string(options.Args.OnuSn),
235 }
236 res, err := client.ShutdownONU(ctx, &req)
237
238 if err != nil {
Matteo Scandoloe383d5d2019-10-25 14:47:27 -0700239 log.Fatalf("Cannot shutdown ONU %s: %v", options.Args.OnuSn, err)
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700240 return err
241 }
242
243 fmt.Println(fmt.Sprintf("[Status: %d] %s", res.StatusCode, res.Message))
244
245 return nil
246}
247
248func (options *ONUPowerOn) Execute(args []string) error {
249 client, conn := connect()
250 defer conn.Close()
251
252 ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout)
253 defer cancel()
254 req := pb.ONURequest{
255 SerialNumber: string(options.Args.OnuSn),
256 }
257 res, err := client.PoweronONU(ctx, &req)
258
259 if err != nil {
Matteo Scandoloe383d5d2019-10-25 14:47:27 -0700260 log.Fatalf("Cannot power on ONU %s: %v", options.Args.OnuSn, err)
261 return err
262 }
263
264 fmt.Println(fmt.Sprintf("[Status: %d] %s", res.StatusCode, res.Message))
265
266 return nil
267}
268
269func (options *ONUEapolRestart) Execute(args []string) error {
270 client, conn := connect()
271 defer conn.Close()
272
273 ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout)
274 defer cancel()
275 req := pb.ONURequest{
276 SerialNumber: string(options.Args.OnuSn),
277 }
278 res, err := client.RestartEapol(ctx, &req)
279
280 if err != nil {
281 log.Fatalf("Cannot restart EAPOL for ONU %s: %v", options.Args.OnuSn, err)
282 return err
283 }
284
285 fmt.Println(fmt.Sprintf("[Status: %d] %s", res.StatusCode, res.Message))
286
287 return nil
288}
289
290func (options *ONUDhcpRestart) Execute(args []string) error {
291 client, conn := connect()
292 defer conn.Close()
293
294 ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout)
295 defer cancel()
296 req := pb.ONURequest{
297 SerialNumber: string(options.Args.OnuSn),
298 }
299 res, err := client.RestartDhcp(ctx, &req)
300
301 if err != nil {
302 log.Fatalf("Cannot restart DHCP for ONU %s: %v", options.Args.OnuSn, err)
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700303 return err
304 }
305
306 fmt.Println(fmt.Sprintf("[Status: %d] %s", res.StatusCode, res.Message))
307
308 return nil
309}
310
Arjun E K57a7fcb2020-01-30 06:44:45 +0000311func (options *ONUIgmp) Execute(args []string) error {
312 client, conn := connect()
313 defer conn.Close()
314
315 ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout)
316 defer cancel()
317
318 req := pb.ONURequest{
319 SerialNumber: string(options.Args.OnuSn),
320 }
321
322 var subActionVal pb.SubActionTypes
323 if string(options.Args.SubAction) == IgmpJoinKey {
324 subActionVal = pb.SubActionTypes_JOIN
325 } else if string(options.Args.SubAction) == IgmpLeaveKey {
326 subActionVal = pb.SubActionTypes_LEAVE
Anand S Katti09541352020-01-29 15:54:01 +0530327 } else if string(options.Args.SubAction) == IgmpJoinKeyV3 {
328 subActionVal = pb.SubActionTypes_JOINV3
329 }
Arjun E K57a7fcb2020-01-30 06:44:45 +0000330
331 igmpReq := pb.IgmpRequest{
332 OnuReq: &req,
333 SubActionVal: subActionVal,
334 }
335 res, err := client.GetONU(ctx, igmpReq.OnuReq)
336 if err != nil {
337 log.WithFields(log.Fields{
338 "SerialNumber": options.Args.OnuSn,
339 }).Errorf("Cannot not get details on ONU error: %v", err)
340 }
341 log.WithFields(log.Fields{
342 "SerialNumber": igmpReq.OnuReq.SerialNumber,
343 }).Debugf("ONU has indentified : %s", res)
344
345 igmpRes, igmpErr := client.ChangeIgmpState(ctx, &igmpReq)
346 if igmpErr != nil {
347 log.WithFields(log.Fields{
348 "SubAction": options.Args.SubAction,
349 }).Errorf("Could not process Action: error: %v", igmpErr)
350 } else {
351 log.WithFields(log.Fields{
352 "SubAction": options.Args.SubAction,
353 }).Debugf("igmp state has been changed with response: %s",
354 igmpRes.Message)
355 }
356
357 return nil
358}
359
Pragya Arya8bdb4532020-03-02 17:08:09 +0530360func (options *ONUFlows) Execute(args []string) error {
361 client, conn := connect()
362 defer conn.Close()
363
364 ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout)
365 defer cancel()
366 req := pb.ONURequest{
367 SerialNumber: string(options.Args.OnuSn),
368 }
369 res, err := client.GetFlows(ctx, &req)
370 if err != nil {
371 log.Errorf("Cannot get flows for ONU %s: %v", options.Args.OnuSn, err)
372 return err
373 }
374
375 if res.Flows == nil {
376 fmt.Println(fmt.Sprintf("ONU %s has no flows", options.Args.OnuSn))
377 return nil
378 }
379
380 flowHeader := []string{
381 "access_intf_id",
382 "onu_id",
383 "uni_id",
384 "flow_id",
385 "flow_type",
386 "eth_type",
387 "alloc_id",
388 "network_intf_id",
389 "gemport_id",
390 "classifier",
391 "action",
392 "priority",
393 "cookie",
394 "port_no",
395 }
396
397 tableFlow := tablewriter.NewWriter(os.Stdout)
398 tableFlow.SetRowLine(true)
399 fmt.Fprintf(os.Stdout, "ONU Flows:\n")
400 tableFlow.SetHeader(flowHeader)
401
402 for _, flow := range res.Flows {
403 flowInfo := []string{}
404 flowInfo = append(flowInfo,
405 strconv.Itoa(int(flow.AccessIntfId)),
406 strconv.Itoa(int(flow.OnuId)),
407 strconv.Itoa(int(flow.UniId)),
408 strconv.Itoa(int(flow.FlowId)),
409 flow.FlowType,
410 fmt.Sprintf("%x", flow.Classifier.EthType),
411 strconv.Itoa(int(flow.AllocId)),
412 strconv.Itoa(int(flow.NetworkIntfId)),
413 strconv.Itoa(int(flow.GemportId)),
414 flow.Classifier.String(),
415 flow.Action.String(),
416 strconv.Itoa(int(flow.Priority)),
417 strconv.Itoa(int(flow.Cookie)),
418 strconv.Itoa(int(flow.PortNo)),
419 )
420 tableFlow.Append(flowInfo)
421 }
422 tableFlow.Render()
423 tableFlow.SetNewLine("")
424 return nil
425}
426
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700427func (onuSn *OnuSnString) Complete(match string) []flags.Completion {
428 client, conn := connect()
429 defer conn.Close()
430
431 ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout)
432 defer cancel()
433
434 onus, err := client.GetONUs(ctx, &pb.Empty{})
435 if err != nil {
Matteo Scandolo86e8ce62019-10-11 12:03:10 -0700436 log.Fatalf("could not get ONUs: %v", err)
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700437 return nil
438 }
439
440 list := make([]flags.Completion, 0)
441 for _, k := range onus.Items {
442 if strings.HasPrefix(k.SerialNumber, match) {
443 list = append(list, flags.Completion{Item: k.SerialNumber})
444 }
445 }
446
447 return list
448}
Anand S Katti09541352020-01-29 15:54:01 +0530449
Matteo Scandolo618a6582020-09-09 12:21:29 -0700450func (onuSn *IgmpSubAction) Complete(match string) []flags.Completion {
451 list := make([]flags.Completion, 0)
452 for _, k := range IgmpAllowedActions {
453 if strings.HasPrefix(k, match) {
454 list = append(list, flags.Completion{Item: k})
455 }
456 }
457 return list
458}
459
Anand S Katti09541352020-01-29 15:54:01 +0530460func (options *ONUTrafficSchedulers) Execute(args []string) error {
461 client, conn := connect()
462 defer conn.Close()
463
464 ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout)
465 defer cancel()
466 req := pb.ONURequest{
467 SerialNumber: string(options.Args.OnuSn),
468 }
469 res, err := client.GetOnuTrafficSchedulers(ctx, &req)
470 if err != nil {
471 log.Fatalf("Cannot get traffic schedulers for ONU %s: %v", options.Args.OnuSn, err)
472 return err
473 }
474
475 if res.TraffSchedulers == nil {
476 log.Fatalf("Cannot get traffic schedulers for ONU: %s (unavailable)", options.Args.OnuSn)
477 return nil
478 }
479
480 SchedulerHeader := []string{"Direction",
481 "AllocId",
482 "Scheduler.Direction",
483 "Scheduler.AdditionalBw",
484 "Scheduler.Priority",
485 "Scheduler.Weight",
486 "Scheduler.SchedPolicy",
487 }
488
489 ShapingInfoHeader := []string{"InferredAdditionBwIndication",
490 "Cbs",
491 "Cir",
492 "Gir",
493 "Pbs",
494 "Pir",
495 }
496
497 SchedulerVals := []string{}
498 ShapingInfoVals := []string{}
499 for _, v := range res.TraffSchedulers.TrafficScheds {
500 SchedulerVals = append(SchedulerVals,
501 v.GetDirection().String(),
502 strconv.Itoa(int(v.GetAllocId())),
503 v.Scheduler.GetDirection().String(),
504 v.Scheduler.GetAdditionalBw().String(),
505 strconv.Itoa(int(v.Scheduler.GetPriority())),
506 strconv.Itoa(int(v.Scheduler.GetWeight())),
507 v.GetScheduler().GetSchedPolicy().String(),
508 )
509
510 ShapingInfoVals = append(ShapingInfoVals,
511 v.TrafficShapingInfo.GetAddBwInd().String(),
512 strconv.Itoa(int(v.TrafficShapingInfo.GetCbs())),
513 strconv.Itoa(int(v.TrafficShapingInfo.GetCir())),
514 strconv.Itoa(int(v.TrafficShapingInfo.GetGir())),
515 strconv.Itoa(int(v.TrafficShapingInfo.GetPbs())),
516 strconv.Itoa(int(v.TrafficShapingInfo.GetPir())),
517 )
518 }
519
520 fmt.Fprintf(os.Stdout, "OnuId: %d \n", int(res.TraffSchedulers.OnuId))
521 fmt.Fprintf(os.Stdout, "IntfId: %d \n", int(res.TraffSchedulers.IntfId))
522 fmt.Fprintf(os.Stdout, "UniId: %d \n", int(res.TraffSchedulers.UniId))
523 fmt.Fprintf(os.Stdout, "OnuPortNo: %d \n", int(res.TraffSchedulers.PortNo))
524
525 tableSched := tablewriter.NewWriter(os.Stdout)
526 tableSched.SetRowLine(true)
527 fmt.Fprintf(os.Stdout, "Traffic Schedulers Info:\n")
528 tableSched.SetHeader(SchedulerHeader)
529 tableSched.Append(SchedulerVals)
530 tableSched.Render()
531 tableSched.SetNewLine("")
532
533 tableShap := tablewriter.NewWriter(os.Stdout)
534 tableShap.SetRowLine(true)
535 fmt.Fprintf(os.Stdout, "Traffic Shaping Info:\n")
536 tableShap.SetHeader(ShapingInfoHeader)
537 tableShap.Append(ShapingInfoVals)
538 tableShap.Render()
539
540 return nil
541}