VOL-4197: Add uniport to OnuGemInfo cache in flow-manager module.
Otherwise, during cleanup the uni-ports are not detected and the
cleanup handler leaves out stale entries for the {pon,onu,uni}
tuple on kv store causing failures on new flow-adds due to
meter mismatch (from stale entries on etcd).

Change-Id: Ibacc053fd3435c3d8185b3f9b075861d21a2e4bc
diff --git a/internal/pkg/core/device_handler.go b/internal/pkg/core/device_handler.go
index 4a84b78..50c73ce 100644
--- a/internal/pkg/core/device_handler.go
+++ b/internal/pkg/core/device_handler.go
@@ -940,7 +940,7 @@
 					}
 					logger.Debugw(ctx, "publish-pon-metrics", log.Fields{"pon-port": port.Label})
 
-					onuGemInfoLst := dh.flowMgr[intfID].getOnuGemInfoList()
+					onuGemInfoLst := dh.flowMgr[intfID].getOnuGemInfoList(ctx)
 					if len(onuGemInfoLst) > 0 {
 						go dh.portStats.collectOnuAndGemStats(ctx, onuGemInfoLst)
 					}
@@ -1756,7 +1756,7 @@
 		var ponPort uint32
 		for ponPort = 0; ponPort < dh.totalPonPorts; ponPort++ {
 			var err error
-			onuGemData := dh.flowMgr[ponPort].getOnuGemInfoList()
+			onuGemData := dh.flowMgr[ponPort].getOnuGemInfoList(ctx)
 			for i, onu := range onuGemData {
 				onuID := make([]uint32, 1)
 				logger.Debugw(ctx, "onu-data", log.Fields{"onu": onu})
diff --git a/internal/pkg/core/openolt_flowmgr.go b/internal/pkg/core/openolt_flowmgr.go
index 9c1cb09..978cea8 100644
--- a/internal/pkg/core/openolt_flowmgr.go
+++ b/internal/pkg/core/openolt_flowmgr.go
@@ -2149,6 +2149,24 @@
 	}
 
 	f.resourceMgr.AddUniPortToOnuInfo(ctx, intfID, onuID, portNo)
+	// also update flowmgr cache
+	f.onuGemInfoLock.Lock()
+	onugem, ok := f.onuGemInfoMap[onuID]
+	if ok {
+		found := false
+		for _, uni := range onugem.UniPorts {
+			if uni == portNo {
+				found = true
+				break
+			}
+		}
+		if !found {
+			onugem.UniPorts = append(onugem.UniPorts, portNo)
+			f.onuGemInfoMap[onuID] = onugem
+			logger.Infow(ctx, "added uni port to onugem cache", log.Fields{"uni": portNo})
+		}
+	}
+	f.onuGemInfoLock.Unlock()
 
 	TpID, err := getTpIDFromFlow(ctx, flow)
 	if err != nil {
@@ -2352,6 +2370,7 @@
 		f.onuGemInfoLock.Lock()
 		f.onuGemInfoMap[onuID] = onugem
 		f.onuGemInfoLock.Unlock()
+		logger.Debugw(ctx, "updated onu gem info from cache", log.Fields{"onugem": onugem})
 	} else {
 		logger.Warnw(ctx, "mismatched onu id", log.Fields{
 			"gem-port-id": gemPort,
@@ -3197,7 +3216,7 @@
 	return nil
 }
 
-func (f *OpenOltFlowMgr) getOnuGemInfoList() []rsrcMgr.OnuGemInfo {
+func (f *OpenOltFlowMgr) getOnuGemInfoList(ctx context.Context) []rsrcMgr.OnuGemInfo {
 	var onuGemInfoLst []rsrcMgr.OnuGemInfo
 	f.onuGemInfoLock.RLock()
 	defer f.onuGemInfoLock.RUnlock()
diff --git a/internal/pkg/resourcemanager/resourcemanager.go b/internal/pkg/resourcemanager/resourcemanager.go
index c256a3d..16b2723 100755
--- a/internal/pkg/resourcemanager/resourcemanager.go
+++ b/internal/pkg/resourcemanager/resourcemanager.go
@@ -960,7 +960,7 @@
 		logger.Errorf(ctx, "Failed to update resource %s", Path)
 		return err
 	}
-	logger.Debugw(ctx, "added onu gem info", log.Fields{"onuGemInfo": onuGem})
+	logger.Debugw(ctx, "added onu gem info to store", log.Fields{"onuGemInfo": onuGem})
 	return err
 }