[SEBA-820] Preventing the ONU from going to dhcp_started if we don't have the DHCP flow

Change-Id: Ia9c136830a6781fa69d7ead5406c3e354ad35a06
diff --git a/internal/bbsim/devices/onu.go b/internal/bbsim/devices/onu.go
index 0f93e0d..cc467b8 100644
--- a/internal/bbsim/devices/onu.go
+++ b/internal/bbsim/devices/onu.go
@@ -18,6 +18,7 @@
 
 import (
 	"context"
+	"errors"
 	"fmt"
 	"github.com/cboling/omci"
 	"github.com/google/gopacket/layers"
@@ -46,10 +47,13 @@
 	// PortNo comes with flows and it's used when sending packetIndications,
 	// There is one PortNo per UNI Port, for now we're only storing the first one
 	// FIXME add support for multiple UNIs
-	PortNo        uint32
 	HwAddress     net.HardwareAddr
 	InternalState *fsm.FSM
 
+	// ONU State
+	PortNo           uint32
+	DhcpFlowReceived bool
+
 	OperState    *fsm.FSM
 	SerialNumber *openolt.SerialNumber
 
@@ -64,25 +68,26 @@
 	DoneChannel chan bool // this channel is used to signal once the onu is complete (when the struct is used by BBR)
 }
 
-func (o Onu) Sn() string {
+func (o *Onu) Sn() string {
 	return common.OnuSnToString(o.SerialNumber)
 }
 
 func CreateONU(olt OltDevice, pon PonPort, id uint32, sTag int, cTag int) *Onu {
 
 	o := Onu{
-		ID:          id,
-		PonPortID:   pon.ID,
-		PonPort:     pon,
-		STag:        sTag,
-		CTag:        cTag,
-		HwAddress:   net.HardwareAddr{0x2e, 0x60, 0x70, 0x13, byte(pon.ID), byte(id)},
-		PortNo:      0,
-		Channel:     make(chan Message, 2048),
-		tid:         0x1,
-		hpTid:       0x8000,
-		seqNumber:   0,
-		DoneChannel: make(chan bool, 1),
+		ID:               id,
+		PonPortID:        pon.ID,
+		PonPort:          pon,
+		STag:             sTag,
+		CTag:             cTag,
+		HwAddress:        net.HardwareAddr{0x2e, 0x60, 0x70, 0x13, byte(pon.ID), byte(id)},
+		PortNo:           0,
+		Channel:          make(chan Message, 2048),
+		tid:              0x1,
+		hpTid:            0x8000,
+		seqNumber:        0,
+		DoneChannel:      make(chan bool, 1),
+		DhcpFlowReceived: false,
 	}
 	o.SerialNumber = o.NewSN(olt.ID, pon.ID, o.ID)
 
@@ -167,6 +172,11 @@
 					"OnuSn":  o.Sn(),
 				}).Errorf("ONU failed to authenticate!")
 			},
+			"before_start_dhcp": func(e *fsm.Event) {
+				if o.DhcpFlowReceived == false {
+					e.Cancel(errors.New("cannot-go-to-dhcp-started-as-dhcp-flow-is-missing"))
+				}
+			},
 			"enter_dhcp_started": func(e *fsm.Event) {
 				msg := Message{
 					Type: StartDHCP,
@@ -209,7 +219,7 @@
 	}).Debugf("Changing ONU InternalState from %s to %s", src, dst)
 }
 
-func (o Onu) ProcessOnuMessages(stream openolt.Openolt_EnableIndicationServer, client openolt.OpenoltClient) {
+func (o *Onu) ProcessOnuMessages(stream openolt.Openolt_EnableIndicationServer, client openolt.OpenoltClient) {
 	onuLogger.WithFields(log.Fields{
 		"onuID": o.ID,
 		"onuSN": o.Sn(),
@@ -358,6 +368,7 @@
 
 	if err := stream.Send(&openolt.Indication{Data: discoverData}); err != nil {
 		log.Errorf("Failed to send Indication_OnuDiscInd: %v", err)
+		return
 	}
 
 	if err := o.InternalState.Event("discover"); err != nil {
@@ -507,9 +518,12 @@
 	} else if msg.Flow.Classifier.EthType == uint32(layers.EthernetTypeIPv4) &&
 		msg.Flow.Classifier.SrcPort == uint32(68) &&
 		msg.Flow.Classifier.DstPort == uint32(67) {
+
+		// keep track that we reveived the DHCP Flows so that we can transition the state to dhcp_started
+		o.DhcpFlowReceived = true
 		// NOTE we are receiving mulitple DHCP flows but we shouldn't call the transition multiple times
 		if err := o.InternalState.Event("start_dhcp"); err != nil {
-			log.Warnf("Can't go to dhcp_started: %v", err)
+			log.Errorf("Can't go to dhcp_started: %v", err)
 		}
 	}
 }