[VOL-5386] - onu adapter crash fix during remove gem entries

Change-Id: I0e6da3225e56f037c9bfd9a60dbdfc3742923bc0
Signed-off-by: Sridhar Ravindra <sridhar.ravindra@radisys.com>
diff --git a/internal/pkg/avcfg/onu_uni_tp.go b/internal/pkg/avcfg/onu_uni_tp.go
index 442d0a2..c37e00c 100755
--- a/internal/pkg/avcfg/onu_uni_tp.go
+++ b/internal/pkg/avcfg/onu_uni_tp.go
@@ -619,11 +619,7 @@
 				//if the FSM is not valid, also TP related remove data should not be valid:
 				// remove GemPort from config DB
 				//ensure write protection for access to mapPonAniConfig
-				onuTP.mutexTPState.Lock()
-				delete(onuTP.mapPonAniConfig[uniTPKey].mapGemPortParams, onuTP.mapRemoveGemEntry[uniTPKey].removeGemID)
-				// remove the removeEntry
-				delete(onuTP.mapRemoveGemEntry, uniTPKey)
-				onuTP.mutexTPState.Unlock()
+				onuTP.deleteGemPortParams(ctx, uniTPKey)
 				return
 			}
 			if _, ok := onuTP.PAniConfigFsm[uniTPKey]; !ok {
@@ -636,22 +632,14 @@
 				//if the FSM is not valid, also TP related remove data should not be valid:
 				// remove GemPort from config DB
 				//ensure write protection for access to mapPonAniConfig
-				onuTP.mutexTPState.Lock()
-				delete(onuTP.mapPonAniConfig[uniTPKey].mapGemPortParams, onuTP.mapRemoveGemEntry[uniTPKey].removeGemID)
-				// remove the removeEntry
-				delete(onuTP.mapRemoveGemEntry, uniTPKey)
-				onuTP.mutexTPState.Unlock()
+				onuTP.deleteGemPortParams(ctx, uniTPKey)
 				return
 			}
 			if onuTP.getProfileResetting(uniTPKey) {
 				logger.Debugw(ctx, "aborting GemRemoval on FSM, reset requested in parallel", log.Fields{
 					"device-id": onuTP.deviceID, "uni-id": aUniID, "tp-id": aTpID})
 				//ensure write protection for access to mapPonAniConfig
-				onuTP.mutexTPState.Lock()
-				delete(onuTP.mapPonAniConfig[uniTPKey].mapGemPortParams, onuTP.mapRemoveGemEntry[uniTPKey].removeGemID)
-				// remove the removeEntry
-				delete(onuTP.mapRemoveGemEntry, uniTPKey)
-				onuTP.mutexTPState.Unlock()
+				onuTP.deleteGemPortParams(ctx, uniTPKey)
 				return
 			}
 			// initiate OMCI GemPort related removal
@@ -691,11 +679,7 @@
 		//ensure write protection for access to mapPonAniConfig
 		logger.Debugw(ctx, "UniPonAniConfigFsm removing gem from config data and clearing ani FSM", log.Fields{
 			"device-id": onuTP.deviceID, "gem-id": onuTP.mapRemoveGemEntry[uniTPKey].removeGemID, "uniTPKey": uniTPKey})
-		onuTP.mutexTPState.Lock()
-		delete(onuTP.mapPonAniConfig[uniTPKey].mapGemPortParams, onuTP.mapRemoveGemEntry[uniTPKey].removeGemID)
-		// remove the removeEntry
-		delete(onuTP.mapRemoveGemEntry, uniTPKey)
-		onuTP.mutexTPState.Unlock()
+		onuTP.deleteGemPortParams(ctx, uniTPKey)
 	} else { //if CResourceTcont == aResource {
 		logger.Debugw(ctx, "reset TCont with AllocId", log.Fields{
 			"device-id": onuTP.deviceID, "uni-id": aUniID, "path": aPathString, "allocId": aEntryID})
@@ -854,6 +838,21 @@
 	return onuTP.runAniConfigFsm(ctx, aniEvStart, aProcessingStep, aUniID, aTpID)
 }
 
+// deleteGemPortParams removes GemPort from config DB
+func (onuTP *OnuUniTechProf) deleteGemPortParams(ctx context.Context, uniTPKey uniTP) {
+	//ensure write protection for access to mapPonAniConfig
+	onuTP.mutexTPState.Lock()
+	if _, ok := onuTP.mapPonAniConfig[uniTPKey]; ok {
+		delete(onuTP.mapPonAniConfig[uniTPKey].mapGemPortParams, onuTP.mapRemoveGemEntry[uniTPKey].removeGemID)
+	} else {
+		logger.Warnw(ctx, "GemPort removal - GemPort not found in mapPonAniConfig",
+			log.Fields{"device-id": onuTP.deviceID, "uni-id": uniTPKey.uniID, "tp-id": uniTPKey.tpID})
+	}
+	// remove from the removGemeEntry
+	delete(onuTP.mapRemoveGemEntry, uniTPKey)
+	onuTP.mutexTPState.Unlock()
+}
+
 // runAniConfigFsm starts the AniConfig FSM to transfer the OMCI related commands for  ANI side configuration
 func (onuTP *OnuUniTechProf) runAniConfigFsm(ctx context.Context, aEvent string, aProcessingStep uint8, aUniID uint8, aTpID uint8) error {
 	/*  Uni related ANI config procedure -