[VOL-5400] Added locks for mapPonAniConfig and handled the device deletion using commChan during processOmciAniMessages

Signed-off-by: bseeniva <balaji.seenivasan@radisys.com>
Change-Id: Ibb1618eee165bc310517335c9d050b00b381d41a
diff --git a/VERSION b/VERSION
index 95d3ab1..962fc0d 100755
--- a/VERSION
+++ b/VERSION
@@ -1 +1 @@
-2.12.10-dev5
\ No newline at end of file
+2.12.10-dev6
diff --git a/internal/pkg/avcfg/omci_ani_config.go b/internal/pkg/avcfg/omci_ani_config.go
index e141efa..bbcc41a 100755
--- a/internal/pkg/avcfg/omci_ani_config.go
+++ b/internal/pkg/avcfg/omci_ani_config.go
@@ -349,10 +349,13 @@
 			logger.Errorw(ctx, "No valid OnuDevice - aborting", log.Fields{"device-id": oFsm.deviceID})
 			return
 		}
+		// Access critical state with lock
+		oFsm.pUniTechProf.mutexTPState.RLock()
 		tcontInstID, tcontAlreadyExist, err := oFsm.pOnuDeviceEntry.AllocateFreeTcont(ctx, oFsm.pUniTechProf.mapPonAniConfig[oFsm.uniTpKey].tcontParams.allocID)
 		if err != nil {
 			logger.Errorw(ctx, "No TCont instances found", log.Fields{"device-id": oFsm.deviceID, "err": err})
 			//reset the state machine to enable usage on subsequent requests
+			oFsm.pUniTechProf.mutexTPState.RUnlock()
 			_ = aPAFsm.PFsm.Event(aniEvReset)
 			return
 		}
@@ -363,8 +366,6 @@
 			"tcontAlreadyExist": tcontAlreadyExist,
 			"device-id":         oFsm.deviceID})
 
-		// Access critical state with lock
-		oFsm.pUniTechProf.mutexTPState.RLock()
 		oFsm.alloc0ID = oFsm.pUniTechProf.mapPonAniConfig[oFsm.uniTpKey].tcontParams.allocID
 		mapGemPortParams := oFsm.pUniTechProf.mapPonAniConfig[oFsm.uniTpKey].mapGemPortParams
 		oFsm.pUniTechProf.mutexTPState.RUnlock()
@@ -1049,8 +1050,16 @@
 func (oFsm *UniPonAniConfigFsm) enterRemoving1pMapper(ctx context.Context, e *fsm.Event) {
 	logger.Info(ctx, "UniPonAniConfigFsm - start deleting the .1pMapper", log.Fields{
 		"device-id": oFsm.deviceID, "uni-id": oFsm.pOnuUniPort.UniID})
-	mapGemPortParams := oFsm.pUniTechProf.mapPonAniConfig[oFsm.uniTpKey].mapGemPortParams
+	var mapGemPortParams map[uint16]*gemPortParamStruct
 	unicastGemCount := 0
+	oFsm.pUniTechProf.mutexTPState.RLock()
+	if _, ok := oFsm.pUniTechProf.mapPonAniConfig[oFsm.uniTpKey]; ok {
+		mapGemPortParams = oFsm.pUniTechProf.mapPonAniConfig[oFsm.uniTpKey].mapGemPortParams
+	} else {
+		logger.Warnw(ctx, "GemPortParams not found in mapPonAniConfig", log.Fields{"device-id": oFsm.deviceID,
+			"uni-id": oFsm.pOnuUniPort.UniID})
+	}
+	oFsm.pUniTechProf.mutexTPState.RUnlock()
 	for _, gemEntry := range mapGemPortParams {
 		if !gemEntry.isMulticast {
 			unicastGemCount++
@@ -1201,29 +1210,36 @@
 		// case <-ctx.Done():
 		// 	logger.Info("MibSync Msg", log.Fields{"Message handling canceled via context for device-id": oFsm.deviceID})
 		// 	break loop
-		message, ok := <-oFsm.PAdaptFsm.CommChan
-		if !ok {
-			logger.Warn(ctx, "UniPonAniConfigFsm Rx Msg - could not read from channel", log.Fields{"device-id": oFsm.deviceID})
-			// but then we have to ensure a restart of the FSM as well - as exceptional procedure
-			_ = oFsm.PAdaptFsm.PFsm.Event(aniEvReset)
-			break loop
-		}
-		logger.Debugw(ctx, "UniPonAniConfigFsm Rx Msg", log.Fields{"device-id": oFsm.deviceID})
-
-		switch message.Type {
-		case cmn.TestMsg:
-			msg, _ := message.Data.(cmn.TestMessage)
-			if msg.TestMessageVal == cmn.AbortMessageProcessing {
-				logger.Infow(ctx, "UniPonAniConfigFsm abort ProcessMsg", log.Fields{"for device-id": oFsm.deviceID})
+		select {
+		case message, ok := <-oFsm.PAdaptFsm.CommChan:
+			if !ok {
+				logger.Warn(ctx, "UniPonAniConfigFsm Rx Msg - could not read from channel", log.Fields{"device-id": oFsm.deviceID})
+				// but then we have to ensure a restart of the FSM as well - as exceptional procedure
+				_ = oFsm.PAdaptFsm.PFsm.Event(aniEvReset)
 				break loop
 			}
-			logger.Warnw(ctx, "UniPonAniConfigFsm unknown TestMessage", log.Fields{"device-id": oFsm.deviceID, "MessageVal": msg.TestMessageVal})
-		case cmn.OMCI:
-			msg, _ := message.Data.(cmn.OmciMessage)
-			oFsm.handleOmciAniConfigMessage(ctx, msg)
-		default:
-			logger.Warn(ctx, "UniPonAniConfigFsm Rx unknown message", log.Fields{"device-id": oFsm.deviceID,
-				"message.Type": message.Type})
+			logger.Debugw(ctx, "UniPonAniConfigFsm Rx Msg", log.Fields{"device-id": oFsm.deviceID})
+
+			switch message.Type {
+			case cmn.TestMsg:
+				msg, _ := message.Data.(cmn.TestMessage)
+				if msg.TestMessageVal == cmn.AbortMessageProcessing {
+					logger.Infow(ctx, "UniPonAniConfigFsm abort ProcessMsg", log.Fields{"for device-id": oFsm.deviceID})
+					break loop
+				}
+				logger.Warnw(ctx, "UniPonAniConfigFsm unknown TestMessage", log.Fields{"device-id": oFsm.deviceID, "MessageVal": msg.TestMessageVal})
+			case cmn.OMCI:
+				msg, _ := message.Data.(cmn.OmciMessage)
+				oFsm.handleOmciAniConfigMessage(ctx, msg)
+			default:
+				logger.Warn(ctx, "UniPonAniConfigFsm Rx unknown message", log.Fields{"device-id": oFsm.deviceID,
+					"message.Type": message.Type})
+			}
+		case _, ok := <-oFsm.pDeviceHandler.GetDeviceDeleteCommChan(ctx):
+			if !ok {
+				logger.Warnw(ctx, "Device deletion channel closed", log.Fields{"device-id": oFsm.deviceID})
+			}
+			break loop
 		}
 
 	}
@@ -1302,6 +1318,7 @@
 		case "TCont":
 			//If this is for TCONT creation(requestEventOffset=0) and this is the first allocation of TCONT(so noone else is using the same TCONT)
 			//We should revert DB
+			oFsm.pUniTechProf.mutexTPState.RLock()
 			if oFsm.requestEventOffset == 0 && !oFsm.tcontSetBefore && oFsm.pUniTechProf.mapPonAniConfig[oFsm.uniTpKey] != nil {
 				logger.Debugw(ctx, "UniPonAniConfigFsm TCONT creation failed on device. Freeing alloc id", log.Fields{"device-id": oFsm.deviceID,
 					"alloc-id": oFsm.pUniTechProf.mapPonAniConfig[oFsm.uniTpKey].tcontParams.allocID, "uni-tp": oFsm.uniTpKey})
@@ -1312,6 +1329,7 @@
 						log.Fields{"ME name": oFsm.pLastTxMeInstance.GetName(), "device-id": oFsm.deviceID})
 				}
 			}
+			oFsm.pUniTechProf.mutexTPState.RUnlock()
 		default:
 			logger.Warnw(ctx, "Unsupported ME name received with error!",
 				log.Fields{"ME name": oFsm.pLastTxMeInstance.GetName(), "result": msgObj.Result, "device-id": oFsm.deviceID})
diff --git a/internal/pkg/avcfg/onu_uni_tp.go b/internal/pkg/avcfg/onu_uni_tp.go
index 19f8453..4f51b1a 100755
--- a/internal/pkg/avcfg/onu_uni_tp.go
+++ b/internal/pkg/avcfg/onu_uni_tp.go
@@ -773,6 +773,7 @@
 func (onuTP *OnuUniTechProf) IsTechProfileConfigCleared(ctx context.Context, uniID uint8, tpID uint8) bool {
 	uniTPKey := uniTP{uniID: uniID, tpID: tpID}
 	logger.Debugw(ctx, "IsTechProfileConfigCleared", log.Fields{"device-id": onuTP.deviceID})
+	onuTP.mutexTPState.RLock()
 	if onuTP.mapPonAniConfig[uniTPKey] != nil {
 		mapGemPortParams := onuTP.mapPonAniConfig[uniTPKey].mapGemPortParams
 		unicastGemCount := 0
@@ -782,6 +783,7 @@
 			}
 		}
 		if unicastGemCount == 0 || onuTP.mapPonAniConfig[uniTPKey].tcontParams.allocID == 0 {
+			onuTP.mutexTPState.RUnlock()
 			logger.Debugw(ctx, "clearing-ani-side-config", log.Fields{
 				"device-id": onuTP.deviceID, "uniTpKey": uniTPKey})
 			onuTP.clearAniSideConfig(ctx, uniID, tpID)
@@ -792,6 +794,7 @@
 			return true
 		}
 	}
+	onuTP.mutexTPState.RUnlock()
 	return false
 }