VOL-1904 openolt adapter reconcile.

cache flowsUsedByGemPort is also stored and kvstore kept in sync.

on adapter restart flow ids per gemport will be loaded to flowsUsedByGemPort from kvstore

Change-Id: Ife58fd9cb790e032402e77c630cf9069d0db5bcb
diff --git a/adaptercore/openolt_flowmgr.go b/adaptercore/openolt_flowmgr.go
index e3f0ccb..67d2a2a 100644
--- a/adaptercore/openolt_flowmgr.go
+++ b/adaptercore/openolt_flowmgr.go
@@ -200,6 +200,8 @@
 		if flowMgr.onuGemInfo[idx], err = rMgr.GetOnuGemInfo(idx); err != nil {
 			log.Error("Failed to load onu gem info cache")
 		}
+		//Load flowID list per gem map per interface from the kvstore.
+		flowMgr.loadFlowIDlistForGem(idx)
 	}
 	flowMgr.lockCache = sync.RWMutex{}
 	log.Info("Initialization of  flow manager success!!")
@@ -229,6 +231,8 @@
 	}
 	flowIDList = appendUnique(flowIDList, deviceFlow.FlowId)
 	f.flowsUsedByGemPort[gemPK] = flowIDList
+	// update the flowids for a gem to the KVstore
+	f.resourceMgr.UpdateFlowIDsForGem(uint32(deviceFlow.AccessIntfId), uint32(deviceFlow.GemportId), flowIDList)
 }
 
 func (f *OpenOltFlowMgr) divideAndAddFlow(intfID uint32, onuID uint32, uniID uint32, portNo uint32,
@@ -1256,7 +1260,10 @@
 				for i, flowIDinMap := range flowIDs {
 					if flowIDinMap == flowID {
 						flowIDs = append(flowIDs[:i], flowIDs[i+1:]...)
+						// everytime flowsUsedByGemPort cache is updated the same should be updated
+						// in kv store by calling UpdateFlowIDsForGem
 						f.flowsUsedByGemPort[gemPK] = flowIDs
+						f.resourceMgr.UpdateFlowIDsForGem(Intf, uint32(gemPortID), flowIDs)
 						break
 					}
 				}
@@ -1269,8 +1276,10 @@
 			// But it is anyway eventually  removed later when the TechProfile is freed, so not a big issue for now.
 			f.resourceMgr.RemoveGEMportPonportToOnuMapOnKVStore(uint32(gemPortID), Intf)
 			f.onuIdsLock.Lock()
+			//everytime an entry is deleted from flowsUsedByGemPort cache, the same should be updated in kv as well
+			// by calling DeleteFlowIDsForGem
 			delete(f.flowsUsedByGemPort, gemPK)
-			//delete(f.onuGemPortIds, gemPK)
+			f.resourceMgr.DeleteFlowIDsForGem(Intf, uint32(gemPortID))
 			f.resourceMgr.FreeGemPortID(Intf, uint32(onuID), uint32(uniID), uint32(gemPortID))
 			f.onuIdsLock.Unlock()
 
@@ -2060,3 +2069,16 @@
 	}
 	f.resourceMgr.AddUniPortToOnuInfo(intfID, onuID, portNum)
 }
+
+func (f *OpenOltFlowMgr) loadFlowIDlistForGem(intf uint32) {
+	flowIDsList, err := f.resourceMgr.GetFlowIDsGemMapForInterface(intf)
+	if err != nil {
+		log.Error("Failed to get flowid list per gem", log.Fields{"intf": intf})
+		return
+	}
+	for gem, FlowIDs := range flowIDsList {
+		gemPK := gemPortKey{intf, uint32(gem)}
+		f.flowsUsedByGemPort[gemPK] = FlowIDs
+	}
+	return
+}