[VOL-3437] Implement (incremental) flow config removal with according OMCI VLAN configuration and some further code corrections and smaller functional extensions -> version 0.1.13-dev135, now merged with [VOL-3586] und included correction for missing Techprofile configuration at disable/enable procedure

Signed-off-by: mpagenko <michael.pagenkopf@adtran.com>
Change-Id: I438a72867d5da83c505a30169d7d5aba8f8ee8c2
diff --git a/internal/pkg/onuadaptercore/uniportadmin.go b/internal/pkg/onuadaptercore/uniportadmin.go
index 8a0428e..11a6b04 100644
--- a/internal/pkg/onuadaptercore/uniportadmin.go
+++ b/internal/pkg/onuadaptercore/uniportadmin.go
@@ -34,11 +34,13 @@
 //lockStateFsm defines the structure for the state machine to lock/unlock the ONU UNI ports via OMCI
 type lockStateFsm struct {
 	pDeviceHandler           *deviceHandler
+	deviceID                 string
 	pOmciCC                  *omciCC
 	adminState               bool
 	requestEvent             OnuDeviceEvent
 	omciLockResponseReceived chan bool //seperate channel needed for checking UNI port OMCi message responses
 	pAdaptFsm                *AdapterFsm
+	pLastTxMeInstance        *me.ManagedEntity
 }
 
 const (
@@ -67,14 +69,15 @@
 	aName string, apDeviceHandler *deviceHandler, aCommChannel chan Message) *lockStateFsm {
 	instFsm := &lockStateFsm{
 		pDeviceHandler: apDeviceHandler,
+		deviceID:       apDeviceHandler.deviceID,
 		pOmciCC:        apDevOmciCC,
 		adminState:     aAdminState,
 		requestEvent:   aRequestEvent,
 	}
-	instFsm.pAdaptFsm = NewAdapterFsm(aName, apDeviceHandler.deviceID, aCommChannel)
+	instFsm.pAdaptFsm = NewAdapterFsm(aName, instFsm.deviceID, aCommChannel)
 	if instFsm.pAdaptFsm == nil {
 		logger.Errorw("LockStateFsm's AdapterFsm could not be instantiated!!", log.Fields{
-			"device-id": apDeviceHandler.deviceID})
+			"device-id": instFsm.deviceID})
 		return nil
 	}
 	if aAdminState { //port locking requested
@@ -146,11 +149,11 @@
 	}
 	if instFsm.pAdaptFsm.pFsm == nil {
 		logger.Errorw("LockStateFsm's Base FSM could not be instantiated!!", log.Fields{
-			"device-id": apDeviceHandler.deviceID})
+			"device-id": instFsm.deviceID})
 		return nil
 	}
 
-	logger.Infow("LockStateFsm created", log.Fields{"device-id": apDeviceHandler.deviceID})
+	logger.Infow("LockStateFsm created", log.Fields{"device-id": instFsm.deviceID})
 	return instFsm
 }
 
@@ -162,7 +165,7 @@
 
 func (oFsm *lockStateFsm) enterAdminStartingState(e *fsm.Event) {
 	logger.Debugw("LockStateFSM start", log.Fields{"in state": e.FSM.Current(),
-		"device-id": oFsm.pAdaptFsm.deviceID})
+		"device-id": oFsm.deviceID})
 	// in case the used channel is not yet defined (can be re-used after restarts)
 	if oFsm.omciLockResponseReceived == nil {
 		oFsm.omciLockResponseReceived = make(chan bool)
@@ -196,7 +199,7 @@
 		omciAdminState = 0
 	}
 	logger.Debugw("LockStateFSM Tx Set::ONU-G:admin", log.Fields{
-		"omciAdmin": omciAdminState, "in state": e.FSM.Current(), "device-id": oFsm.pAdaptFsm.deviceID})
+		"omciAdmin": omciAdminState, "in state": e.FSM.Current(), "device-id": oFsm.deviceID})
 	requestedAttributes := me.AttributeValueMap{"AdministrativeState": omciAdminState}
 	meInstance := oFsm.pOmciCC.sendSetOnuGLS(context.TODO(), ConstDefaultOmciTimeout, true,
 		requestedAttributes, oFsm.pAdaptFsm.commChan)
@@ -204,19 +207,19 @@
 	//  - this avoids misinterpretation of new received OMCI messages
 	//  we might already abort the processing with nil here, but maybe some auto-recovery may be tried
 	//  - may be improved later, for now we just handle it with the Rx timeout or missing next event (stick in state)
-	oFsm.pOmciCC.pLastTxMeInstance = meInstance
+	oFsm.pLastTxMeInstance = meInstance
 }
 
 func (oFsm *lockStateFsm) enterSettingUnisState(e *fsm.Event) {
 	logger.Infow("LockStateFSM - starting PPTP config loop", log.Fields{
-		"in state": e.FSM.Current(), "device-id": oFsm.pAdaptFsm.deviceID, "LockState": oFsm.adminState})
+		"in state": e.FSM.Current(), "device-id": oFsm.deviceID, "LockState": oFsm.adminState})
 	go oFsm.performUniPortAdminSet()
 }
 
 func (oFsm *lockStateFsm) enterAdminDoneState(e *fsm.Event) {
-	logger.Debugw("LockStateFSM", log.Fields{"send notification to core in State": e.FSM.Current(), "device-id": oFsm.pAdaptFsm.deviceID})
+	logger.Debugw("LockStateFSM", log.Fields{"send notification to core in State": e.FSM.Current(), "device-id": oFsm.deviceID})
 	//use DeviceHandler event notification directly, no need/support to update DeviceEntryState for lock/unlock
-	oFsm.pOmciCC.pBaseDeviceHandler.deviceProcStatusUpdate(oFsm.requestEvent)
+	oFsm.pDeviceHandler.deviceProcStatusUpdate(oFsm.requestEvent)
 
 	//let's reset the state machine in order to release all resources now
 	pLockStateAFsm := oFsm.pAdaptFsm
@@ -231,7 +234,7 @@
 }
 
 func (oFsm *lockStateFsm) enterResettingState(e *fsm.Event) {
-	logger.Debugw("LockStateFSM resetting", log.Fields{"device-id": oFsm.pAdaptFsm.deviceID})
+	logger.Debugw("LockStateFSM resetting", log.Fields{"device-id": oFsm.deviceID})
 	pLockStateAFsm := oFsm.pAdaptFsm
 	if pLockStateAFsm != nil {
 		// abort running message processing
@@ -250,75 +253,76 @@
 				_ = a_pAFsm.pFsm.Event(uniEvRestart)
 			}
 		}(pLockStateAFsm)
+		oFsm.pLastTxMeInstance = nil
 	}
 }
 
 func (oFsm *lockStateFsm) processOmciLockMessages( /*ctx context.Context*/ ) {
-	logger.Debugw("Start LockStateFsm Msg processing", log.Fields{"for device-id": oFsm.pAdaptFsm.deviceID})
+	logger.Debugw("Start LockStateFsm Msg processing", log.Fields{"for device-id": oFsm.deviceID})
 loop:
 	for {
 		// case <-ctx.Done():
-		// 	logger.Info("MibSync Msg", log.Fields{"Message handling canceled via context for device-id": oFsm.pAdaptFsm.deviceID})
+		// 	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.Info("LockStateFsm Rx Msg - could not read from channel", log.Fields{"device-id": oFsm.pAdaptFsm.deviceID})
+			logger.Info("LockStateFsm 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(uniEvRestart)
 			break loop
 		}
-		logger.Debugw("LockStateFsm Rx Msg", log.Fields{"device-id": oFsm.pAdaptFsm.deviceID})
+		logger.Debugw("LockStateFsm Rx Msg", log.Fields{"device-id": oFsm.deviceID})
 
 		switch message.Type {
 		case TestMsg:
 			msg, _ := message.Data.(TestMessage)
 			if msg.TestMessageVal == AbortMessageProcessing {
-				logger.Infow("LockStateFsm abort ProcessMsg", log.Fields{"for device-id": oFsm.pAdaptFsm.deviceID})
+				logger.Infow("LockStateFsm abort ProcessMsg", log.Fields{"for device-id": oFsm.deviceID})
 				break loop
 			}
-			logger.Warnw("LockStateFsm unknown TestMessage", log.Fields{"device-id": oFsm.pAdaptFsm.deviceID, "MessageVal": msg.TestMessageVal})
+			logger.Warnw("LockStateFsm unknown TestMessage", log.Fields{"device-id": oFsm.deviceID, "MessageVal": msg.TestMessageVal})
 		case OMCI:
 			msg, _ := message.Data.(OmciMessage)
 			oFsm.handleOmciLockStateMessage(msg)
 		default:
-			logger.Warn("LockStateFsm Rx unknown message", log.Fields{"device-id": oFsm.pAdaptFsm.deviceID,
+			logger.Warn("LockStateFsm Rx unknown message", log.Fields{"device-id": oFsm.deviceID,
 				"message.Type": message.Type})
 		}
 	}
-	logger.Infow("End LockStateFsm Msg processing", log.Fields{"device-id": oFsm.pAdaptFsm.deviceID})
+	logger.Infow("End LockStateFsm Msg processing", log.Fields{"device-id": oFsm.deviceID})
 }
 
 func (oFsm *lockStateFsm) handleOmciLockStateMessage(msg OmciMessage) {
-	logger.Debugw("Rx OMCI LockStateFsm Msg", log.Fields{"device-id": oFsm.pAdaptFsm.deviceID,
+	logger.Debugw("Rx OMCI LockStateFsm Msg", log.Fields{"device-id": oFsm.deviceID,
 		"msgType": msg.OmciMsg.MessageType})
 
 	if msg.OmciMsg.MessageType == omci.SetResponseType {
 		msgLayer := (*msg.OmciPacket).Layer(omci.LayerTypeSetResponse)
 		if msgLayer == nil {
 			logger.Errorw("LockStateFsm - Omci Msg layer could not be detected for SetResponse",
-				log.Fields{"device-id": oFsm.pAdaptFsm.deviceID})
+				log.Fields{"device-id": oFsm.deviceID})
 			return
 		}
 		msgObj, msgOk := msgLayer.(*omci.SetResponse)
 		if !msgOk {
 			logger.Errorw("LockStateFsm - Omci Msg layer could not be assigned for SetResponse",
-				log.Fields{"device-id": oFsm.pAdaptFsm.deviceID})
+				log.Fields{"device-id": oFsm.deviceID})
 			return
 		}
-		logger.Debugw("LockStateFsm SetResponse Data", log.Fields{"device-id": oFsm.pAdaptFsm.deviceID, "data-fields": msgObj})
+		logger.Debugw("LockStateFsm SetResponse Data", log.Fields{"device-id": oFsm.deviceID, "data-fields": msgObj})
 		if msgObj.Result != me.Success {
 			logger.Errorw("LockStateFsm - Omci SetResponse Error - later: drive FSM to abort state ?", log.Fields{"Error": msgObj.Result})
 			// possibly force FSM into abort or ignore some errors for some messages? store error for mgmt display?
 			return
 		}
 		// compare comments above for CreateResponse (apply also here ...)
-		if msgObj.EntityClass == oFsm.pOmciCC.pLastTxMeInstance.GetClassID() &&
-			msgObj.EntityInstance == oFsm.pOmciCC.pLastTxMeInstance.GetEntityID() {
+		if msgObj.EntityClass == oFsm.pLastTxMeInstance.GetClassID() &&
+			msgObj.EntityInstance == oFsm.pLastTxMeInstance.GetEntityID() {
 			//store the created ME into DB //TODO??? obviously the Python code does not store the config ...
 			// if, then something like:
 			//oFsm.pOnuDB.StoreMe(msgObj)
 
-			switch oFsm.pOmciCC.pLastTxMeInstance.GetName() {
+			switch oFsm.pLastTxMeInstance.GetName() {
 			case "OnuG":
 				{ // let the FSM proceed ...
 					_ = oFsm.pAdaptFsm.pFsm.Event(uniEvRxOnugResp)
@@ -345,20 +349,20 @@
 
 	for uniNo, uniPort := range oFsm.pOmciCC.pBaseDeviceHandler.uniEntityMap {
 		logger.Debugw("Setting PPTP admin state", log.Fields{
-			"device-id": oFsm.pAdaptFsm.deviceID, "for PortNo": uniNo})
+			"device-id": oFsm.deviceID, "for PortNo": uniNo})
 
 		var meInstance *me.ManagedEntity
 		if uniPort.portType == uniPPTP {
 			meInstance = oFsm.pOmciCC.sendSetUniGLS(context.TODO(), uniPort.entityID, ConstDefaultOmciTimeout,
 				true, requestedAttributes, oFsm.pAdaptFsm.commChan)
-			oFsm.pOmciCC.pLastTxMeInstance = meInstance
+			oFsm.pLastTxMeInstance = meInstance
 		} else if uniPort.portType == uniVEIP {
 			meInstance = oFsm.pOmciCC.sendSetVeipLS(context.TODO(), uniPort.entityID, ConstDefaultOmciTimeout,
 				true, requestedAttributes, oFsm.pAdaptFsm.commChan)
-			oFsm.pOmciCC.pLastTxMeInstance = meInstance
+			oFsm.pLastTxMeInstance = meInstance
 		} else {
 			logger.Warnw("Unsupported PPTP type - skip",
-				log.Fields{"device-id": oFsm.pAdaptFsm.deviceID, "Port": uniNo})
+				log.Fields{"device-id": oFsm.deviceID, "Port": uniNo})
 			continue
 		}
 
@@ -366,14 +370,14 @@
 		err := oFsm.waitforOmciResponse(meInstance)
 		if err != nil {
 			logger.Errorw("PPTP Admin State set failed, aborting LockState set!",
-				log.Fields{"device-id": oFsm.pAdaptFsm.deviceID, "Port": uniNo})
+				log.Fields{"device-id": oFsm.deviceID, "Port": uniNo})
 			_ = oFsm.pAdaptFsm.pFsm.Event(uniEvReset)
 			return
 		}
 	} //for all UNI ports
 	// if Config has been done for all UNI related instances let the FSM proceed
 	// while we did not check here, if there is some port at all - !?
-	logger.Infow("PPTP config loop finished", log.Fields{"device-id": oFsm.pAdaptFsm.deviceID})
+	logger.Infow("PPTP config loop finished", log.Fields{"device-id": oFsm.deviceID})
 	_ = oFsm.pAdaptFsm.pFsm.Event(uniEvRxUnisResp)
 }
 
@@ -381,17 +385,17 @@
 	select {
 	// maybe be also some outside cancel (but no context modeled for the moment ...)
 	// case <-ctx.Done():
-	// 		logger.Infow("LockState-bridge-init message reception canceled", log.Fields{"for device-id": oFsm.pAdaptFsm.deviceID})
+	// 		logger.Infow("LockState-bridge-init message reception canceled", log.Fields{"for device-id": oFsm.deviceID})
 	case <-time.After(30 * time.Second): //3s was detected to be to less in 8*8 bbsim test with debug Info/Debug
-		logger.Warnw("LockStateFSM uni-set timeout", log.Fields{"for device-id": oFsm.pAdaptFsm.deviceID})
-		return fmt.Errorf("lockStateFsm uni-set timeout for device-id %s", oFsm.pAdaptFsm.deviceID)
+		logger.Warnw("LockStateFSM uni-set timeout", log.Fields{"for device-id": oFsm.deviceID})
+		return fmt.Errorf("lockStateFsm uni-set timeout for device-id %s", oFsm.deviceID)
 	case success := <-oFsm.omciLockResponseReceived:
 		if success {
 			logger.Debug("LockStateFSM uni-set response received")
 			return nil
 		}
 		// should not happen so far
-		logger.Warnw("LockStateFSM uni-set response error", log.Fields{"for device-id": oFsm.pAdaptFsm.deviceID})
-		return fmt.Errorf("lockStateFsm uni-set responseError for device-id %s", oFsm.pAdaptFsm.deviceID)
+		logger.Warnw("LockStateFSM uni-set response error", log.Fields{"for device-id": oFsm.deviceID})
+		return fmt.Errorf("lockStateFsm uni-set responseError for device-id %s", oFsm.deviceID)
 	}
 }