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

Change-Id: I23c38351371bef091919c0beb48132f8d59a08dc
diff --git a/internal/bbsim/devices/onu.go b/internal/bbsim/devices/onu.go
index ee16cb8..43759c9 100644
--- a/internal/bbsim/devices/onu.go
+++ b/internal/bbsim/devices/onu.go
@@ -126,7 +126,7 @@
 			// DEVICE Lifecycle
 			{Name: "initialize", Src: []string{"created", "disabled"}, Dst: "initialized"},
 			{Name: "discover", Src: []string{"initialized", "pon_disabled"}, Dst: "discovered"},
-			{Name: "enable", Src: []string{"discovered", "disabled"}, Dst: "enabled"},
+			{Name: "enable", Src: []string{"discovered"}, Dst: "enabled"},
 			{Name: "receive_eapol_flow", Src: []string{"enabled", "gem_port_added"}, Dst: "eapol_flow_received"},
 			{Name: "add_gem_port", Src: []string{"enabled", "eapol_flow_received"}, Dst: "gem_port_added"},
 			// NOTE should disabled state be different for oper_disabled (emulating an error) and admin_disabled (received a disabled call via VOLTHA)?
@@ -163,6 +163,15 @@
 			"enter_initialized": func(e *fsm.Event) {
 				// create new channel for ProcessOnuMessages Go routine
 				o.Channel = make(chan Message, 2048)
+
+				if err := o.OperState.Event("enable"); err != nil {
+					onuLogger.WithFields(log.Fields{
+						"OnuId":  o.ID,
+						"IntfId": o.PonPortID,
+						"OnuSn":  o.Sn(),
+					}).Errorf("Cannot change ONU OperState to up: %s", err.Error())
+				}
+
 				if !isMock {
 					// start ProcessOnuMessages Go routine
 					go o.ProcessOnuMessages(olt.enableContext, *olt.OpenoltStream, nil)
@@ -190,6 +199,13 @@
 				o.Channel <- msg
 			},
 			"enter_disabled": func(event *fsm.Event) {
+				if err := o.OperState.Event("disable"); err != nil {
+					onuLogger.WithFields(log.Fields{
+						"OnuId":  o.ID,
+						"IntfId": o.PonPortID,
+						"OnuSn":  o.Sn(),
+					}).Errorf("Cannot change ONU OperState to down: %s", err.Error())
+				}
 				msg := Message{
 					Type: OnuIndication,
 					Data: OnuIndicationMessage{
@@ -378,9 +394,6 @@
 				} else if msg.Type == packetHandlers.DHCP {
 					dhcp.HandleNextBbrPacket(o.ID, o.PonPortID, o.Sn(), o.STag, o.HwAddress, o.DoneChannel, msg.Packet, client)
 				}
-			case DyingGaspIndication:
-				msg, _ := message.Data.(DyingGaspIndicationMessage)
-				o.sendDyingGaspInd(msg, stream)
 			case OmciIndication:
 				msg, _ := message.Data.(OmciIndicationMessage)
 				o.handleOmci(msg, client)
@@ -481,31 +494,6 @@
 	return sn
 }
 
-// NOTE handle_/process methods can change the ONU internal state as they are receiving messages
-// send method should not change the ONU state
-
-func (o *Onu) sendDyingGaspInd(msg DyingGaspIndicationMessage, stream openolt.Openolt_EnableIndicationServer) error {
-	alarmData := &openolt.AlarmIndication_DyingGaspInd{
-		DyingGaspInd: &openolt.DyingGaspIndication{
-			IntfId: msg.PonPortID,
-			OnuId:  msg.OnuID,
-			Status: "on",
-		},
-	}
-	data := &openolt.Indication_AlarmInd{AlarmInd: &openolt.AlarmIndication{Data: alarmData}}
-
-	if err := stream.Send(&openolt.Indication{Data: data}); err != nil {
-		onuLogger.Errorf("Failed to send DyingGaspInd : %v", err)
-		return err
-	}
-	onuLogger.WithFields(log.Fields{
-		"IntfId": msg.PonPortID,
-		"OnuSn":  o.Sn(),
-		"OnuId":  msg.OnuID,
-	}).Info("sendDyingGaspInd")
-	return nil
-}
-
 func (o *Onu) sendOnuDiscIndication(msg OnuDiscIndicationMessage, stream openolt.Openolt_EnableIndicationServer) {
 	discoverData := &openolt.Indication_OnuDiscInd{OnuDiscInd: &openolt.OnuDiscIndication{
 		IntfId:       msg.Onu.PonPortID,
@@ -547,7 +535,7 @@
 		SerialNumber: o.SerialNumber,
 	}}
 	if err := stream.Send(&openolt.Indication{Data: indData}); err != nil {
-		// TODO do we need to transition to a broken state?
+		// NOTE do we need to transition to a broken state?
 		log.Errorf("Failed to send Indication_OnuInd: %v", err)
 	}
 	onuLogger.WithFields(log.Fields{
@@ -652,20 +640,20 @@
 
 func (o *Onu) handleFlowUpdate(msg OnuFlowUpdateMessage) {
 	onuLogger.WithFields(log.Fields{
-		"DstPort":   msg.Flow.Classifier.DstPort,
-		"EthType":   fmt.Sprintf("%x", msg.Flow.Classifier.EthType),
-		"FlowId":    msg.Flow.FlowId,
-		"FlowType":  msg.Flow.FlowType,
-		"GemportId": msg.Flow.GemportId,
-		"InnerVlan": msg.Flow.Classifier.IVid,
-		"IntfId":    msg.Flow.AccessIntfId,
-		"IpProto":   msg.Flow.Classifier.IpProto,
-		"OnuId":     msg.Flow.OnuId,
-		"OnuSn":     o.Sn(),
-		"OuterVlan": msg.Flow.Classifier.OVid,
-		"PortNo":    msg.Flow.PortNo,
-		"SrcPort":   msg.Flow.Classifier.SrcPort,
-		"UniID":     msg.Flow.UniId,
+		"DstPort":          msg.Flow.Classifier.DstPort,
+		"EthType":          fmt.Sprintf("%x", msg.Flow.Classifier.EthType),
+		"FlowId":           msg.Flow.FlowId,
+		"FlowType":         msg.Flow.FlowType,
+		"GemportId":        msg.Flow.GemportId,
+		"InnerVlan":        msg.Flow.Classifier.IVid,
+		"IntfId":           msg.Flow.AccessIntfId,
+		"IpProto":          msg.Flow.Classifier.IpProto,
+		"OnuId":            msg.Flow.OnuId,
+		"OnuSn":            o.Sn(),
+		"OuterVlan":        msg.Flow.Classifier.OVid,
+		"PortNo":           msg.Flow.PortNo,
+		"SrcPort":          msg.Flow.Classifier.SrcPort,
+		"UniID":            msg.Flow.UniId,
 		"ClassifierOPbits": msg.Flow.Classifier.OPbits,
 	}).Debug("ONU receives Flow")