VOL-1360 BBSim - Error handling in AAA/DHCP client activation trigger

Change-Id: Idd327ed5de38ee4026c786e5c384dfa63624ad0a
diff --git a/core/omci.go b/core/omci.go
index acf7bbe..3a4335d 100644
--- a/core/omci.go
+++ b/core/omci.go
@@ -24,6 +24,8 @@
 	"gerrit.opencord.org/voltha-bbsim/protos"
 	"gerrit.opencord.org/voltha-bbsim/device"
 	"time"
+	"context"
+	"errors"
 )
 
 //
@@ -106,7 +108,7 @@
 	DONE
 )
 
-type OmciMsgHandler func(class OmciClass, content OmciContent, key OnuKey) []byte
+type OmciMsgHandler func(class OmciClass, content OmciContent, key OnuKey) ([]byte, error)
 
 var Handlers = map[OmciMsgType]OmciMsgHandler{
 	MibReset:      mibReset,
@@ -120,64 +122,82 @@
 
 var OnuOmciStateMap = map[OnuKey]*OnuOmciState{}
 
-func OmciRun(omciOut chan openolt.OmciMsg, omciIn chan openolt.OmciIndication, onumap map[uint32][] *device.Onu, errch chan error) {
+func OmciRun(ctx context.Context, omciOut chan openolt.OmciMsg, omciIn chan openolt.OmciIndication, onumap map[uint32][] *device.Onu, errch chan error) {
 	go func() { //For monitoring the OMCI states
+		t := time.NewTicker(1 * time.Second)
+		defer t.Stop()
 		for {
-			time.Sleep(1 * time.Second)
-			if isAllOmciInitDone(onumap) {
-				logger.Info("OmciRun - All the omci init process were done")
-				close(errch)
-				break
+			select{
+			case <- t.C:
+				logger.Debug("Monitor omci init state")
+				if isAllOmciInitDone(onumap) {
+					logger.Info("OmciRun - All the omci initialization wes done")
+					close(errch)
+					return
+				}
+			case <- ctx.Done():
+				logger.Debug("Omci Monitoring process was done")
+				return
 			}
 		}
 	}()
 
 	go func(){
+		defer logger.Debug("Omci response process was done")
 		for {
 			var resp openolt.OmciIndication
+			select{
+				case m := <-omciOut:
+					transactionId, deviceId, msgType, class, instance, content, err := ParsePkt(m.Pkt)
+					if err != nil {
+						errch <- err
+						return
+					}
 
-			m := <-omciOut
+					logger.Debug("OmciRun - transactionId: %d msgType: %d, ME Class: %d, ME Instance: %d",
+						transactionId, msgType, class, instance)
 
-			transactionId, deviceId, msgType, class, instance, content := ParsePkt(m.Pkt)
+					key := OnuKey{m.IntfId, m.OnuId}
+					if _, ok := OnuOmciStateMap[key]; !ok {
+						OnuOmciStateMap[key] = NewOnuOmciState()
+					}
 
-			logger.Debug("OmciRun - transactionId: %d msgType: %d, ME Class: %d, ME Instance: %d",
-				transactionId, msgType, class, instance)
+					if _, ok := Handlers[msgType]; !ok {
+						logger.Warn("Ignore omci msg (msgType %d not handled)", msgType)
+						continue
+					}
 
-			key := OnuKey{m.IntfId, m.OnuId}
-			if _, ok := OnuOmciStateMap[key]; !ok {
-				OnuOmciStateMap[key] = NewOnuOmciState()
+					resp.Pkt, err = Handlers[msgType](class, content, key)
+					if err != nil {
+						errch <- err
+						return
+					}
+					resp.Pkt[0] = byte(transactionId >> 8)
+					resp.Pkt[1] = byte(transactionId & 0xFF)
+					resp.Pkt[2] = 0x2<<4 | byte(msgType)
+					resp.Pkt[3] = deviceId
+					resp.IntfId = m.IntfId
+					resp.OnuId = m.OnuId
+					omciIn <- resp
+				case <-ctx.Done():
+					return
 			}
-
-			if _, ok := Handlers[msgType]; !ok {
-				logger.Warn("Ignore omci msg (msgType %d not handled)", msgType)
-				continue
-			}
-
-			resp.Pkt = Handlers[msgType](class, content, key)
-
-			resp.Pkt[0] = byte(transactionId >> 8)
-			resp.Pkt[1] = byte(transactionId & 0xFF)
-			resp.Pkt[2] = 0x2<<4 | byte(msgType)
-			resp.Pkt[3] = deviceId
-			resp.IntfId = m.IntfId
-			resp.OnuId = m.OnuId
-			omciIn <- resp
 		}
 	}()
 }
 
-func ParsePkt(pkt []byte) (uint16, uint8, OmciMsgType, OmciClass, uint16, OmciContent) {
+func ParsePkt(pkt []byte) (uint16, uint8, OmciMsgType, OmciClass, uint16, OmciContent, error) {
 	var m OmciMessage
 
 	r := bytes.NewReader(HexDecode(pkt))
 
 	if err := binary.Read(r, binary.BigEndian, &m); err != nil {
 		logger.Error("binary.Read failed: %s", err)
+		return 0, 0, 0, 0, 0, OmciContent{}, errors.New("binary.Read failed")
 	}
 	logger.Debug("OmciRun - TransactionId: %d MessageType: %d, ME Class: %d, ME Instance: %d, Content: %x",
 		m.TransactionId, m.MessageType&0x0F, m.MessageId.Class, m.MessageId.Instance, m.Content)
-	return m.TransactionId, m.DeviceId, m.MessageType & 0x0F, m.MessageId.Class, m.MessageId.Instance, m.Content
-
+	return m.TransactionId, m.DeviceId, m.MessageType & 0x0F, m.MessageId.Class, m.MessageId.Instance, m.Content, nil
 }
 
 func HexDecode(pkt []byte) []byte {
@@ -198,7 +218,7 @@
 	return &OnuOmciState{gemPortId: 0, mibUploadCtr: 0, uniGInstance: 1, pptpInstance: 1}
 }
 
-func mibReset(class OmciClass, content OmciContent, key OnuKey) []byte {
+func mibReset(class OmciClass, content OmciContent, key OnuKey) ([]byte, error) {
 	var pkt []byte
 
 	logger.Debug("Omci MibReset")
@@ -210,10 +230,10 @@
 		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
 		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
 		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
-	return pkt
+	return pkt, nil
 }
 
-func mibUpload(class OmciClass, content OmciContent, key OnuKey) []byte {
+func mibUpload(class OmciClass, content OmciContent, key OnuKey) ([]byte, error) {
 	var pkt []byte
 
 	logger.Debug("Omci MibUpload")
@@ -228,10 +248,10 @@
 
 	pkt[9] = NumMibUploads // Number of subsequent MibUploadNext cmds
 
-	return pkt
+	return pkt, nil
 }
 
-func mibUploadNext(class OmciClass, content OmciContent, key OnuKey) []byte {
+func mibUploadNext(class OmciClass, content OmciContent, key OnuKey) ([]byte, error) {
 	var pkt []byte
 
 	logger.Debug("Omci MibUploadNext")
@@ -271,14 +291,14 @@
 		state.pptpInstance++
 	default:
 		logger.Error("Invalid MibUpload request %d", state.mibUploadCtr)
+		return nil, errors.New("Invalid MibUpload request")
 	}
 
 	state.mibUploadCtr++
-
-	return pkt
+	return pkt, nil
 }
 
-func set(class OmciClass, content OmciContent, key OnuKey) []byte {
+func set(class OmciClass, content OmciContent, key OnuKey) ([]byte, error) {
 	var pkt []byte
 
 	pkt = []byte{
@@ -291,15 +311,16 @@
 
 	logger.Debug("Omci Set")
 
-	return pkt
+	return pkt, nil
 }
 
-func create(class OmciClass, content OmciContent, key OnuKey) []byte {
+func create(class OmciClass, content OmciContent, key OnuKey) ([]byte, error) {
 	var pkt []byte
 
 	if class == GEMPortNetworkCTP {
 		if onuOmciState, ok := OnuOmciStateMap[key]; !ok {
 			logger.Error("ONU Key Error - IntfId: %d, OnuId:", key.IntfId, key.OnuId)
+			return nil, errors.New("ONU Key Error")
 		} else {
 			onuOmciState.gemPortId = binary.BigEndian.Uint16(content[:2])
 			logger.Debug("Gem Port Id %d", onuOmciState.gemPortId)
@@ -317,10 +338,10 @@
 
 	logger.Debug("Omci Create")
 
-	return pkt
+	return pkt, nil
 }
 
-func get(class OmciClass, content OmciContent, key OnuKey) []byte {
+func get(class OmciClass, content OmciContent, key OnuKey) ([]byte, error) {
 	var pkt []byte
 
 	pkt = []byte{
@@ -333,10 +354,10 @@
 
 	logger.Debug("Omci Get")
 
-	return pkt
+	return pkt, nil
 }
 
-func getAllAlarms(class OmciClass, content OmciContent, key OnuKey) []byte {
+func getAllAlarms(class OmciClass, content OmciContent, key OnuKey) ([]byte, error) {
 	var pkt []byte
 
 	pkt = []byte{
@@ -349,7 +370,7 @@
 
 	logger.Debug("Omci GetAllAlarms")
 
-	return pkt
+	return pkt, nil
 }
 
 func isAllOmciInitDone(onumap map[uint32][] *device.Onu) bool {