[VOL-2687] Fixing state transition for ONU Shutdown/PowerOn

Change-Id: I23c38351371bef091919c0beb48132f8d59a08dc
diff --git a/internal/bbsim/api/onus_handler.go b/internal/bbsim/api/onus_handler.go
index 975b426..0f898e8 100644
--- a/internal/bbsim/api/onus_handler.go
+++ b/internal/bbsim/api/onus_handler.go
@@ -19,8 +19,8 @@
 import (
 	"context"
 	"fmt"
-
 	"github.com/opencord/bbsim/api/bbsim"
+	"github.com/opencord/bbsim/internal/bbsim/alarmsim"
 	"github.com/opencord/bbsim/internal/bbsim/devices"
 	log "github.com/sirupsen/logrus"
 	"google.golang.org/grpc/codes"
@@ -97,16 +97,41 @@
 		return res, err
 	}
 
-	dyingGasp := devices.Message{
-		Type: devices.DyingGaspIndication,
-		Data: devices.DyingGaspIndicationMessage{
-			OnuID:     onu.ID,
-			PonPortID: onu.PonPortID,
-			Status:    "on", // TODO do we need a type for Dying Gasp Indication?
-		},
+	dyingGasp := bbsim.ONUAlarmRequest{
+		AlarmType:    "DyingGasp",
+		SerialNumber: onu.Sn(),
+		Status:       "on",
 	}
 
-	onu.Channel <- dyingGasp
+	if err := alarmsim.SimulateOnuAlarm(context.TODO(), &dyingGasp, olt); err != nil {
+		logger.WithFields(log.Fields{
+			"OnuId":  onu.ID,
+			"IntfId": onu.PonPortID,
+			"OnuSn":  onu.Sn(),
+		}).Errorf("Cannot send Dying Gasp: %s", err.Error())
+		res.StatusCode = int32(codes.FailedPrecondition)
+		res.Message = err.Error()
+		return res, err
+	}
+
+	losReq := bbsim.ONUAlarmRequest{
+		AlarmType:    "LossOfSignal",
+		SerialNumber: onu.Sn(),
+		Status:       "on",
+	}
+
+	if err := alarmsim.SimulateOnuAlarm(context.TODO(), &losReq, olt); err != nil {
+		logger.WithFields(log.Fields{
+			"OnuId":  onu.ID,
+			"IntfId": onu.PonPortID,
+			"OnuSn":  onu.Sn(),
+		}).Errorf("Cannot send LOS: %s", err.Error())
+		res.StatusCode = int32(codes.FailedPrecondition)
+		res.Message = err.Error()
+		return res, err
+	}
+
+	// TODO if it's the last ONU on the PON, then send a PON LOS
 
 	if err := onu.InternalState.Event("disable"); err != nil {
 		logger.WithFields(log.Fields{
@@ -155,7 +180,7 @@
 		return res, err
 	}
 
-	if onu.InternalState.Current() == "created" {
+	if onu.InternalState.Current() == "created" || onu.InternalState.Current() == "disabled" {
 		if err := onu.InternalState.Event("initialize"); err != nil {
 			logger.WithFields(log.Fields{
 				"OnuId":  onu.ID,
@@ -168,6 +193,23 @@
 		}
 	}
 
+	losReq := bbsim.ONUAlarmRequest{
+		AlarmType:    "LossOfSignal",
+		SerialNumber: onu.Sn(),
+		Status:       "off",
+	}
+
+	if err := alarmsim.SimulateOnuAlarm(context.TODO(), &losReq, olt); err != nil {
+		logger.WithFields(log.Fields{
+			"OnuId":  onu.ID,
+			"IntfId": onu.PonPortID,
+			"OnuSn":  onu.Sn(),
+		}).Errorf("Cannot send LOS: %s", err.Error())
+		res.StatusCode = int32(codes.FailedPrecondition)
+		res.Message = err.Error()
+		return res, err
+	}
+
 	if err := onu.InternalState.Event("discover"); err != nil {
 		logger.WithFields(log.Fields{
 			"OnuId":  onu.ID,
@@ -179,6 +221,17 @@
 		return res, err
 	}
 
+	if err := onu.InternalState.Event("enable"); err != nil {
+		logger.WithFields(log.Fields{
+			"OnuId":  onu.ID,
+			"IntfId": onu.PonPortID,
+			"OnuSn":  onu.Sn(),
+		}).Errorf("Cannot enable ONU: %s", err.Error())
+		res.StatusCode = int32(codes.FailedPrecondition)
+		res.Message = err.Error()
+		return res, err
+	}
+
 	res.StatusCode = int32(codes.OK)
 	res.Message = fmt.Sprintf("ONU %s successfully powered on.", onu.Sn())