[VOL-5464] - Reduce call flows in voltha during ONU reboot

Change-Id: I081dabb9d5fa5a5d177a4fee03879e207caa4dc8
Signed-off-by: Sridhar Ravindra <sridhar.ravindra@radisys.com>
diff --git a/internal/pkg/avcfg/omci_ani_config.go b/internal/pkg/avcfg/omci_ani_config.go
index acf5e08..4968d78 100755
--- a/internal/pkg/avcfg/omci_ani_config.go
+++ b/internal/pkg/avcfg/omci_ani_config.go
@@ -355,7 +355,8 @@
 		}
 		// Access critical state with lock
 		oFsm.pUniTechProf.mutexTPState.RLock()
-		tcontInstID, tcontAlreadyExist, err := oFsm.pOnuDeviceEntry.AllocateFreeTcont(ctx, oFsm.pUniTechProf.mapPonAniConfig[oFsm.uniTpKey].tcontParams.allocID)
+		// Set T-Cont in device irrespective of whether it is stored in onu-adapter or not
+		tcontInstID, _, 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
@@ -364,11 +365,9 @@
 			return
 		}
 		oFsm.tcont0ID = tcontInstID
-		oFsm.tcontSetBefore = tcontAlreadyExist
 		logger.Debugw(ctx, "used-tcont-instance-id", log.Fields{"tcont-inst-id": oFsm.tcont0ID,
-			"alloc-id":          oFsm.pUniTechProf.mapPonAniConfig[oFsm.uniTpKey].tcontParams.allocID,
-			"tcontAlreadyExist": tcontAlreadyExist,
-			"device-id":         oFsm.deviceID})
+			"alloc-id":  oFsm.pUniTechProf.mapPonAniConfig[oFsm.uniTpKey].tcontParams.allocID,
+			"device-id": oFsm.deviceID})
 
 		oFsm.alloc0ID = oFsm.pUniTechProf.mapPonAniConfig[oFsm.uniTpKey].tcontParams.allocID
 		mapGemPortParams := oFsm.pUniTechProf.mapPonAniConfig[oFsm.uniTpKey].mapGemPortParams
diff --git a/internal/pkg/avcfg/omci_vlan_config.go b/internal/pkg/avcfg/omci_vlan_config.go
index 5b0772d..30aa96a 100755
--- a/internal/pkg/avcfg/omci_vlan_config.go
+++ b/internal/pkg/avcfg/omci_vlan_config.go
@@ -184,7 +184,8 @@
 	signalOnFlowDelete          bool
 	// Used to indicate if the FSM is for a reconciling flow and if it's the last flow to be reconciled
 	// thus notification needs to be sent on chan.
-	lastFlowToReconcile bool
+	lastFlowToReconcile      bool
+	lastFlowToConfigOnReboot bool
 }
 
 // NewUniVlanConfigFsm is the 'constructor' for the state machine to config the PON ANI ports
@@ -193,7 +194,7 @@
 func NewUniVlanConfigFsm(ctx context.Context, apDeviceHandler cmn.IdeviceHandler, apOnuDeviceEntry cmn.IonuDeviceEntry, apDevOmciCC *cmn.OmciCC, apUniPort *cmn.OnuUniPort,
 	apUniTechProf *OnuUniTechProf, apOnuDB *devdb.OnuDeviceDB, aTechProfileID uint8,
 	aRequestEvent cmn.OnuDeviceEvent, aName string, aCommChannel chan cmn.Message, aAcceptIncrementalEvto bool,
-	aCookieSlice []uint64, aMatchVlan uint16, aMatchPcp uint8, aSetVlan uint16, aSetPcp uint8, innerCvlan uint16, lastFlowToRec bool, aMeter *of.OfpMeterConfig, respChan *chan error) *UniVlanConfigFsm {
+	aCookieSlice []uint64, aMatchVlan uint16, aMatchPcp uint8, aSetVlan uint16, aSetPcp uint8, innerCvlan uint16, lastFlowToRec bool, lastFlowToConfOnReboot bool, aMeter *of.OfpMeterConfig, respChan *chan error) *UniVlanConfigFsm {
 	instFsm := &UniVlanConfigFsm{
 		pDeviceHandler:              apDeviceHandler,
 		pOnuDeviceEntry:             apOnuDeviceEntry,
@@ -208,6 +209,7 @@
 		ConfiguredUniFlow:           0,
 		numRemoveFlows:              0,
 		lastFlowToReconcile:         lastFlowToRec,
+		lastFlowToConfigOnReboot:    lastFlowToConfOnReboot,
 	}
 
 	instFsm.PAdaptFsm = cmn.NewAdapterFsm(aName, instFsm.deviceID, aCommChannel)
@@ -404,7 +406,7 @@
 // ignore complexity by now
 // nolint: gocyclo
 func (oFsm *UniVlanConfigFsm) SetUniFlowParams(ctx context.Context, aTpID uint8, aCookieSlice []uint64,
-	aMatchVlan uint16, aMatchPcp uint8, aSetVlan uint16, aSetPcp uint8, aInnerCvlan uint16, lastFlowToReconcile bool, aMeter *of.OfpMeterConfig, respChan *chan error) error {
+	aMatchVlan uint16, aMatchPcp uint8, aSetVlan uint16, aSetPcp uint8, aInnerCvlan uint16, lastFlowToReconcile bool, lastFlowToConfigOnReboot bool, aMeter *of.OfpMeterConfig, respChan *chan error) error {
 	if oFsm == nil {
 		logger.Error(ctx, "no valid UniVlanConfigFsm!")
 		return fmt.Errorf("no-valid-UniVlanConfigFsm")
@@ -483,6 +485,7 @@
 	flowCookieModify := false
 	requestAppendRule := false
 	oFsm.lastFlowToReconcile = lastFlowToReconcile
+	oFsm.lastFlowToConfigOnReboot = lastFlowToConfigOnReboot
 	//mutex protection is required for possible concurrent access to FSM members
 	oFsm.mutexFlowParams.Lock()
 	for flow, storedUniFlowParams := range oFsm.uniVlanFlowParamsSlice {
@@ -1344,6 +1347,11 @@
 			"device-id": oFsm.deviceID, "uni-id": oFsm.pOnuUniPort.UniID})
 		oFsm.pDeviceHandler.SendChUniVlanConfigFinished(uint16(oFsm.pOnuUniPort.UniID))
 	}
+	if oFsm.lastFlowToConfigOnReboot {
+		logger.Debugw(ctx, "rebooting - flow processing finished", log.Fields{
+			"device-id": oFsm.deviceID, "uni-id": oFsm.pOnuUniPort.UniID})
+		oFsm.pDeviceHandler.SendChUniVlanConfigFinishedOnReboot(uint16(oFsm.pOnuUniPort.UniID))
+	}
 	if oFsm.pDeviceHandler.IsSkipOnuConfigReconciling() {
 		oFsm.ConfiguredUniFlow = oFsm.NumUniFlows
 		logger.Debugw(ctx, "reconciling - skip enterVlanConfigDone processing",
@@ -1901,9 +1909,9 @@
 						// Send response on response channel if the caller is waiting on it with according error indication.
 						oFsm.pushReponseOnFlowResponseChannel(ctx, vlanRule.RespChan, fmt.Errorf("internal-error"))
 					}
-					//permanently remove possibly stored persistent data
-					var emptySlice = make([]cmn.UniVlanFlowParams, 0)
-					_ = oFsm.pDeviceHandler.StorePersUniFlowConfig(ctx, oFsm.pOnuUniPort.UniID, &emptySlice, true) //ignore errors
+					// Do not remove pers uni data during reset of FSM. It will be removed when the device is deleted
+					//var emptySlice = make([]cmn.UniVlanFlowParams, 0)
+					//_ = oFsm.pDeviceHandler.StorePersUniFlowConfig(ctx, oFsm.pOnuUniPort.UniID, &emptySlice, true) //ignore errors
 				} else {
 					// reset (cancel) of all Fsm is always accompanied by global persistency data removal
 					//  no need to remove specific data in this case here