Simplifying ONU channels and packet responders

Change-Id: I1f3912367a96564986b4209b7864e9fd4b507e8e
diff --git a/internal/bbsim/devices/olt.go b/internal/bbsim/devices/olt.go
index e6248e8..33126a8 100644
--- a/internal/bbsim/devices/olt.go
+++ b/internal/bbsim/devices/olt.go
@@ -543,21 +543,37 @@
 	pon, _ := o.getPonById(onuPkt.IntfId)
 	onu, _ := pon.getOnuById(onuPkt.OnuId)
 
+	oltLogger.WithFields(log.Fields{
+		"IntfId": onu.PonPortID,
+		"OnuId":  onu.ID,
+		"OnuSn":  onu.Sn(),
+	}).Tracef("Received OnuPacketOut")
+
 	rawpkt := gopacket.NewPacket(onuPkt.Pkt, layers.LayerTypeEthernet, gopacket.Default)
 
-	etherType := rawpkt.Layer(layers.LayerTypeEthernet).(*layers.Ethernet).EthernetType
-
-	if etherType == layers.EthernetTypeEAPOL {
-		eapolPkt := bbsim.ByteMsg{IntfId: onuPkt.IntfId, OnuId: onuPkt.OnuId, Bytes: rawpkt.Data()}
-		onu.eapolPktOutCh <- &eapolPkt
-	} else if layerDHCP := rawpkt.Layer(layers.LayerTypeDHCPv4); layerDHCP != nil {
-		// TODO use IsDhcpPacket
-		// TODO we need to untag the packets
-		// NOTE here we receive packets going from the DHCP Server to the ONU
-		// for now we expect them to be double-tagged, but ideally the should be single tagged
-		dhcpPkt := bbsim.ByteMsg{IntfId: onuPkt.IntfId, OnuId: onuPkt.OnuId, Bytes: rawpkt.Data()}
-		onu.dhcpPktOutCh <- &dhcpPkt
+	msg := Message{
+		Type: OnuPacketOut,
+		Data: OnuPacketOutMessage{
+			IntfId: onuPkt.IntfId,
+			OnuId:  onuPkt.OnuId,
+			Packet: rawpkt,
+		},
 	}
+	onu.Channel <- msg
+
+	//etherType := rawpkt.Layer(layers.LayerTypeEthernet).(*layers.Ethernet).EthernetType
+	//
+	//if etherType == layers.EthernetTypeEAPOL {
+	//	eapolPkt := bbsim.ByteMsg{IntfId: onuPkt.IntfId, OnuId: onuPkt.OnuId, Bytes: rawpkt.Data()}
+	//	onu.eapolPktOutCh <- &eapolPkt
+	//} else if layerDHCP := rawpkt.Layer(layers.LayerTypeDHCPv4); layerDHCP != nil {
+	//	// TODO use IsDhcpPacket
+	//	// TODO we need to untag the packets
+	//	// NOTE here we receive packets going from the DHCP Server to the ONU
+	//	// for now we expect them to be double-tagged, but ideally the should be single tagged
+	//	dhcpPkt := bbsim.ByteMsg{IntfId: onuPkt.IntfId, OnuId: onuPkt.OnuId, Bytes: rawpkt.Data()}
+	//	onu.dhcpPktOutCh <- &dhcpPkt
+	//}
 	return new(openolt.Empty), nil
 }
 
diff --git a/internal/bbsim/devices/onu.go b/internal/bbsim/devices/onu.go
index 4587620..3477a62 100644
--- a/internal/bbsim/devices/onu.go
+++ b/internal/bbsim/devices/onu.go
@@ -20,6 +20,7 @@
 	"fmt"
 	"github.com/google/gopacket/layers"
 	"github.com/looplab/fsm"
+	"github.com/opencord/bbsim/internal/bbsim/packetHandlers"
 	"github.com/opencord/bbsim/internal/bbsim/responders/dhcp"
 	"github.com/opencord/bbsim/internal/bbsim/responders/eapol"
 	bbsim "github.com/opencord/bbsim/internal/bbsim/types"
@@ -184,33 +185,22 @@
 			o.handleFlowUpdate(msg, stream)
 		case StartEAPOL:
 			log.Infof("Receive StartEAPOL message on ONU Channel")
-			go func() {
-				// TODO kill this thread
-				eapol.CreateWPASupplicant(
-					o.ID,
-					o.PonPortID,
-					o.Sn(),
-					o.InternalState,
-					stream,
-					o.eapolPktOutCh,
-				)
-			}()
+			eapol.SendEapStart(o.ID, o.PonPortID, o.Sn(), o.InternalState, stream)
 		case StartDHCP:
 			log.Infof("Receive StartDHCP message on ONU Channel")
-			go func() {
-				// TODO kill this thread
-				dhcp.CreateDHCPClient(
-					o.ID,
-					o.PonPortID,
-					o.Sn(),
-					o.HwAddress,
-					o.CTag,
-					o.InternalState,
-					stream,
-					o.dhcpPktOutCh,
-				)
-			}()
+			dhcp.SendDHCPDiscovery(o.PonPortID, o.ID, o.Sn(), o.InternalState, o.HwAddress, o.CTag, stream)
+		case OnuPacketOut:
+			msg, _ := message.Data.(OnuPacketOutMessage)
+			pkt := msg.Packet
+			etherType := pkt.Layer(layers.LayerTypeEthernet).(*layers.Ethernet).EthernetType
 
+			if etherType == layers.EthernetTypeEAPOL {
+				eapol.HandleNextPacket(msg.OnuId, msg.IntfId, o.Sn(), o.InternalState, msg.Packet, stream)
+			} else if packetHandlers.IsDhcpPacket(pkt) {
+				// NOTE here we receive packets going from the DHCP Server to the ONU
+				// for now we expect them to be double-tagged, but ideally the should be single tagged
+				dhcp.HandleNextPacket(o.ID, o.PonPortID, o.Sn(), o.HwAddress, o.CTag, o.InternalState, msg.Packet, stream)
+			}
 		case DyingGaspIndication:
 			msg, _ := message.Data.(DyingGaspIndicationMessage)
 			o.sendDyingGaspInd(msg, stream)
diff --git a/internal/bbsim/devices/types.go b/internal/bbsim/devices/types.go
index 93998e9..eca15d4 100644
--- a/internal/bbsim/devices/types.go
+++ b/internal/bbsim/devices/types.go
@@ -20,6 +20,7 @@
 	"bytes"
 	"errors"
 	"fmt"
+	"github.com/google/gopacket"
 	"github.com/looplab/fsm"
 	bbsim "github.com/opencord/bbsim/internal/bbsim/types"
 	"github.com/opencord/voltha-protos/go/openolt"
@@ -128,7 +129,8 @@
 	FlowUpdate          MessageType = 6
 	StartEAPOL          MessageType = 7
 	StartDHCP           MessageType = 8
-	DyingGaspIndication MessageType = 9
+	OnuPacketOut        MessageType = 9
+	DyingGaspIndication MessageType = 10
 )
 
 func (m MessageType) String() string {
@@ -142,6 +144,7 @@
 		"FlowUpdate",
 		"StartEAPOL",
 		"StartDHCP",
+		"OnuPacketOut",
 		"DyingGaspIndication",
 	}
 	return names[m]
@@ -201,6 +204,12 @@
 	Status    string
 }
 
+type OnuPacketOutMessage struct {
+	IntfId uint32
+	OnuId  uint32
+	Packet gopacket.Packet
+}
+
 type OperState int
 
 const (