[SEBA-818] Adding onu get command to BBSimCtl

Change-Id: I182effdd2d82e3b57d72c53c89fa978b75f16354
diff --git a/api/bbsim/bbsim.proto b/api/bbsim/bbsim.proto
index 59339cc..bc76a92 100644
--- a/api/bbsim/bbsim.proto
+++ b/api/bbsim/bbsim.proto
@@ -82,6 +82,7 @@
     rpc Version(Empty) returns (VersionNumber) {}
     rpc GetOlt(Empty) returns (Olt) {}
     rpc GetONUs(Empty) returns (ONUs) {}
+    rpc GetONU(ONURequest) returns (ONU) {}
     rpc SetLogLevel(LogLevel) returns (LogLevel) {}
     rpc ShutdownONU (ONURequest) returns (Response) {}
     rpc PoweronONU (ONURequest) returns (Response) {}
diff --git a/internal/bbsim/api/onus_handler.go b/internal/bbsim/api/onus_handler.go
index bd333b0..ce0dba0 100644
--- a/internal/bbsim/api/onus_handler.go
+++ b/internal/bbsim/api/onus_handler.go
@@ -49,6 +49,29 @@
 	return &onus, nil
 }
 
+func (s BBSimServer) GetONU(ctx context.Context, req *bbsim.ONURequest) (*bbsim.ONU, error) {
+	olt := devices.GetOLT()
+
+	onu, err := olt.FindOnu(req.SerialNumber)
+
+	if err != nil {
+		res := bbsim.ONU{}
+		return &res, err
+	}
+
+	res := bbsim.ONU{
+		ID:            int32(onu.ID),
+		SerialNumber:  onu.Sn(),
+		OperState:     onu.OperState.Current(),
+		InternalState: onu.InternalState.Current(),
+		PonPortID:     int32(onu.PonPortID),
+		STag:          int32(onu.STag),
+		CTag:          int32(onu.CTag),
+		HwAddress:     onu.HwAddress.String(),
+	}
+	return &res, nil
+}
+
 func (s BBSimServer) ShutdownONU(ctx context.Context, req *bbsim.ONURequest) (*bbsim.Response, error) {
 	// NOTE this method is now sendying a Dying Gasp and then disabling the device (operState: down, adminState: up),
 	// is this the only way to do? Should we address other cases?
diff --git a/internal/bbsimctl/commands/onu.go b/internal/bbsimctl/commands/onu.go
index f99cb5c..fc89300 100644
--- a/internal/bbsimctl/commands/onu.go
+++ b/internal/bbsimctl/commands/onu.go
@@ -36,6 +36,13 @@
 
 type OnuSnString string
 type ONUList struct{}
+
+type ONUGet struct {
+	Args struct {
+		OnuSn OnuSnString
+	} `positional-args:"yes" required:"yes"`
+}
+
 type ONUShutDown struct {
 	Args struct {
 		OnuSn OnuSnString
@@ -50,6 +57,7 @@
 
 type ONUOptions struct {
 	List     ONUList     `command:"list"`
+	Get      ONUGet      `command:"get"`
 	ShutDown ONUShutDown `command:"shutdown"`
 	PowerOn  ONUPowerOn  `command:"poweron"`
 }
@@ -97,6 +105,30 @@
 	return nil
 }
 
+func (options *ONUGet) Execute(args []string) error {
+	client, conn := connect()
+	defer conn.Close()
+
+	ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout)
+	defer cancel()
+	req := pb.ONURequest{
+		SerialNumber: string(options.Args.OnuSn),
+	}
+	res, err := client.GetONU(ctx, &req)
+
+	if err != nil {
+		log.Fatalf("Cannot not shutdown ONU %s: %v", options.Args.OnuSn, err)
+		return err
+	}
+
+	tableFormat := format.Format(DEFAULT_ONU_DEVICE_HEADER_FORMAT)
+	if err := tableFormat.Execute(os.Stdout, true, []*pb.ONU{res}); err != nil {
+		log.Fatalf("Error while formatting ONUs table: %s", err)
+	}
+
+	return nil
+}
+
 func (options *ONUShutDown) Execute(args []string) error {
 
 	client, conn := connect()