SEBA-907 handling DeleteOnu and Fix for SEBA-920

Change-Id: I0691fc273667675314a347061d59c8058fb682c9
diff --git a/internal/bbsim/api/onus_handler.go b/internal/bbsim/api/onus_handler.go
index 8feee21..7b7d6ce 100644
--- a/internal/bbsim/api/onus_handler.go
+++ b/internal/bbsim/api/onus_handler.go
@@ -19,6 +19,7 @@
 import (
 	"context"
 	"fmt"
+
 	"github.com/opencord/bbsim/api/bbsim"
 	"github.com/opencord/bbsim/internal/bbsim/devices"
 	log "github.com/sirupsen/logrus"
@@ -142,7 +143,7 @@
 		return res, err
 	}
 
-	if err := onu.InternalState.Event("enable"); err != nil {
+	if err := onu.InternalState.Event("discover"); err != nil {
 		logger.WithFields(log.Fields{
 			"OnuId":  onu.ID,
 			"IntfId": onu.PonPortID,
diff --git a/internal/bbsim/devices/olt.go b/internal/bbsim/devices/olt.go
index e5ed6bc..016f148 100644
--- a/internal/bbsim/devices/olt.go
+++ b/internal/bbsim/devices/olt.go
@@ -67,6 +67,8 @@
 
 	enableContext       context.Context
 	enableContextCancel context.CancelFunc
+
+	OpenoltStream *openolt.Openolt_EnableIndicationServer
 }
 
 var olt OltDevice
@@ -134,7 +136,7 @@
 
 		// create ONU devices
 		for j := 0; j < onuPerPon; j++ {
-			o := CreateONU(olt, *p, uint32(j+1), sTag, availableCTag, auth, dhcp)
+			o := CreateONU(&olt, *p, uint32(j+1), sTag, availableCTag, auth, dhcp, isMock)
 			p.Onus = append(p.Onus, o)
 			availableCTag = availableCTag + 1
 		}
@@ -185,15 +187,6 @@
 		}
 	}
 
-	for i := range olt.Pons {
-		for _, onu := range olt.Pons[i].Onus {
-			if err := onu.InternalState.Event("initialize"); err != nil {
-				oltLogger.Errorf("Error initializing ONU: %v", err)
-				return err
-			}
-		}
-	}
-
 	return nil
 }
 
@@ -299,6 +292,8 @@
 	wg := sync.WaitGroup{}
 	wg.Add(3)
 
+	o.OpenoltStream = &stream
+
 	// create Go routine to process all OLT events
 	go o.processOltMessages(o.enableContext, stream, &wg)
 	go o.processNniPacketIns(o.enableContext, stream, &wg)
@@ -338,8 +333,8 @@
 		o.channel <- msg
 
 		for _, onu := range o.Pons[i].Onus {
-			go onu.ProcessOnuMessages(o.enableContext, stream, nil)
-			if onu.InternalState.Current() != "initialized" {
+			if err := onu.InternalState.Event("initialize"); err != nil {
+				log.Errorf("Error initializing ONU: %v", err)
 				continue
 			}
 			if err := onu.InternalState.Event("discover"); err != nil {
@@ -752,8 +747,37 @@
 	return new(openolt.Empty), nil
 }
 
-func (o OltDevice) DeleteOnu(context.Context, *openolt.Onu) (*openolt.Empty, error) {
-	oltLogger.Error("DeleteOnu not implemented")
+func (o OltDevice) DeleteOnu(_ context.Context, onu *openolt.Onu) (*openolt.Empty, error) {
+	oltLogger.WithFields(log.Fields{
+		"IntfId": onu.IntfId,
+		"OnuId":  onu.OnuId,
+	}).Info("Received DeleteOnu call from VOLTHA")
+
+	pon, err := o.GetPonById(onu.IntfId)
+	if err != nil {
+		oltLogger.WithFields(log.Fields{
+			"OnuId":  onu.OnuId,
+			"IntfId": onu.IntfId,
+			"err":    err,
+		}).Error("Can't find PonPort")
+	}
+	_onu, err := pon.GetOnuById(onu.OnuId)
+	if err != nil {
+		oltLogger.WithFields(log.Fields{
+			"OnuId":  onu.OnuId,
+			"IntfId": onu.IntfId,
+			"err":    err,
+		}).Error("Can't find Onu")
+	}
+
+	if err := _onu.InternalState.Event("initialize"); err != nil {
+		oltLogger.WithFields(log.Fields{
+			"IntfId": _onu.PonPortID,
+			"OnuSn":  _onu.Sn(),
+			"OnuId":  _onu.ID,
+		}).Infof("Failed to transition ONU to initialized state: %s", err.Error())
+	}
+
 	return new(openolt.Empty), nil
 }
 
diff --git a/internal/bbsim/devices/onu.go b/internal/bbsim/devices/onu.go
index ef25770..2ec674c 100644
--- a/internal/bbsim/devices/onu.go
+++ b/internal/bbsim/devices/onu.go
@@ -78,7 +78,7 @@
 	return common.OnuSnToString(o.SerialNumber)
 }
 
-func CreateONU(olt OltDevice, pon PonPort, id uint32, sTag int, cTag int, auth bool, dhcp bool) *Onu {
+func CreateONU(olt *OltDevice, pon PonPort, id uint32, sTag int, cTag int, auth bool, dhcp bool, isMock bool) *Onu {
 
 	o := Onu{
 		ID:                  id,
@@ -118,7 +118,7 @@
 			{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)?
-			{Name: "disable", Src: []string{"enabled", "eap_response_success_received", "auth_failed", "dhcp_ack_received", "dhcp_failed"}, Dst: "disabled"},
+			{Name: "disable", Src: []string{"enabled", "eapol_flow_received", "gem_port_added", "eap_response_success_received", "auth_failed", "dhcp_ack_received", "dhcp_failed"}, Dst: "disabled"},
 			// ONU state when PON port is disabled but ONU is power ON(more states should be added in src?)
 			{Name: "pon_disabled", Src: []string{"enabled", "gem_port_added", "eapol_flow_received", "eap_response_success_received", "auth_failed", "dhcp_ack_received", "dhcp_failed"}, Dst: "pon_disabled"},
 			// EAPOL
@@ -146,6 +146,10 @@
 			"enter_initialized": func(e *fsm.Event) {
 				// create new channel for ProcessOnuMessages Go routine
 				o.Channel = make(chan Message, 2048)
+				if !isMock {
+					// start ProcessOnuMessages Go routine
+					go o.ProcessOnuMessages(olt.enableContext, *olt.OpenoltStream, nil)
+				}
 			},
 			"enter_discovered": func(e *fsm.Event) {
 				msg := Message{
diff --git a/internal/bbsim/devices/onu_test_helpers.go b/internal/bbsim/devices/onu_test_helpers.go
index bca0ab8..cba8db6 100644
--- a/internal/bbsim/devices/onu_test_helpers.go
+++ b/internal/bbsim/devices/onu_test_helpers.go
@@ -129,7 +129,7 @@
 	pon := PonPort{
 		ID: 1,
 	}
-	onu := CreateONU(olt, pon, 1, 900, 900, false, false)
+	onu := CreateONU(&olt, pon, 1, 900, 900, false, false, true)
 	// NOTE we need this in order to create the OnuChannel
 	onu.InternalState.Event("initialize")
 	onu.DiscoveryRetryDelay = 100 * time.Millisecond