VOL-4446: Fix etcd stale data issue post device delete

Change-Id: I7022a632342bc2868ca52024a15d502313852266
diff --git a/internal/pkg/resourcemanager/resourcemanager.go b/internal/pkg/resourcemanager/resourcemanager.go
index 72b7d54..15ef901 100755
--- a/internal/pkg/resourcemanager/resourcemanager.go
+++ b/internal/pkg/resourcemanager/resourcemanager.go
@@ -43,14 +43,19 @@
 	tpIDPathSuffix = "{%d,%d,%d}/tp_id"
 	//MeterIDPathSuffix - <(pon_id, onu_id, uni_id)>/<tp_id>/meter_id/<direction>
 	MeterIDPathSuffix = "{%d,%d,%d}/{%d}/meter_id/{%s}"
-	// OnuPacketINPathPrefix - path prefix where ONU packet-in vlanID/PCP is stored
+
+	// OnuPacketInPathPrefix - path prefix where ONU packet-in vlanID/PCP is stored
 	//format: onu_packetin/{<intfid>,<onuid>,<logicalport>}
-	OnuPacketINPathPrefix = "onu_packetin/{%d,%d,%d}"
-	// OnuPacketINPath path on the kvstore to store packetin gemport,which will be used for packetin, packetout
+	OnuPacketInPathPrefix = "onu_packetin/{%d,%d,%d}"
+	// OnuPacketInPath path on the kvstore to store packetin gemport,which will be used for packetin, packetout
 	//format: onu_packetin/{<intfid>,<onuid>,<logicalport>}/{<vlanId>,<priority>}
-	OnuPacketINPath = OnuPacketINPathPrefix + "/{%d,%d}"
-	//FlowIDsForGem flowids_per_gem/<intfid>/<gemport-id>
-	FlowIDsForGem = "flowids_per_gem/{%d}/{%d}"
+	OnuPacketInPath = OnuPacketInPathPrefix + "/{%d,%d}"
+
+	//FlowIDsForGemPathPrefix format: flowids_for_gem/<intfid>
+	FlowIDsForGemPathPrefix = "flowids_per_gem/{%d}"
+	//FlowIDsForGem flowids_for_gem/<intfid>/<gemport-id>
+	FlowIDsForGem = FlowIDsForGemPathPrefix + "/{%d}"
+
 	//McastQueuesForIntf multicast queues for pon interfaces
 	McastQueuesForIntf = "mcast_qs_for_int"
 	//FlowGroup flow_groups/<flow_group_id>
@@ -70,9 +75,11 @@
 	//Format: BasePathKvStore/<(pon_intf_id, onu_id, uni_id)>/flow_ids
 	FlowIDPath = "{%s}/flow_ids"
 
+	//OnuGemInfoPathPathPrefix format: onu_gem_info/<intfid>
+	OnuGemInfoPathPathPrefix = "onu_gem_info/{%d}"
 	//OnuGemInfoPath is path on the kvstore to store onugem info map
-	//format: <device-id>/onu_gem_info/<intfid>
-	OnuGemInfoPath = "onu_gem_info/{%d}/{%d}" // onu_gem/<intfid>/<onuID>
+	//format: onu_gem_info/<intfid>/<onu_id>
+	OnuGemInfoPath = OnuGemInfoPathPathPrefix + "/{%d}"
 )
 
 // FlowInfo holds the flow information
@@ -320,12 +327,11 @@
 			PonIntfID, ponrmgr.ONU_ID)
 		return 0, err
 	}
-	if len(onuID) > 0 {
-		rsrcMgr.PonRsrMgr.InitResourceMap(ctx, fmt.Sprintf("%d,%d", PonIntfID, onuID[0]))
-		return onuID[0], err
-	}
 
-	return 0, err // return onuID 0 on error
+	if len(onuID) > 0 {
+		return onuID[0], nil
+	}
+	return 0, fmt.Errorf("no-onu-id-allocated")
 }
 
 // GetCurrentFlowIDsForOnu fetches flow ID from the resource manager
@@ -487,13 +493,6 @@
 	} else {
 		logger.Infow(ctx, "freed onu id", log.Fields{"intfID": intfID, "onuID": onuID})
 	}
-
-	/* Free onu id for a particular interface.*/
-	var IntfonuID string
-	for _, onu := range onuID {
-		IntfonuID = fmt.Sprintf("%d,%d", intfID, onu)
-		rsrcMgr.PonRsrMgr.RemoveResourceMap(ctx, IntfonuID)
-	}
 }
 
 // FreeAllocID frees AllocID on the PON resource pool and also frees the allocID association
@@ -979,6 +978,24 @@
 	return nil
 }
 
+//DeleteAllOnuGemInfoForIntf deletes all the all onu gem info on the given pon interface
+func (rsrcMgr *OpenOltResourceMgr) DeleteAllOnuGemInfoForIntf(ctx context.Context, intfID uint32) error {
+
+	path := fmt.Sprintf(OnuGemInfoPathPathPrefix, intfID)
+
+	logger.Debugw(ctx, "delete-all-onu-gem-info-for-pon-intf", log.Fields{"intfID": intfID})
+	if err := rsrcMgr.KVStore.DeleteWithPrefix(ctx, path); err != nil {
+		logger.Errorf(ctx, "failed-to-remove-resource-%s", path)
+		return err
+	}
+
+	// Reset cache. Normally not necessary as the entire device is getting deleted when this API is invoked.
+	rsrcMgr.onuGemInfoLock.Lock()
+	rsrcMgr.onuGemInfo = make(map[string]*OnuGemInfo)
+	rsrcMgr.onuGemInfoLock.Unlock()
+	return nil
+}
+
 // AddUniPortToOnuInfo adds uni port to the onuinfo kvstore. check if the uni is already present if not update the kv store.
 func (rsrcMgr *OpenOltResourceMgr) AddUniPortToOnuInfo(ctx context.Context, intfID uint32, onuID uint32, portNo uint32) {
 
@@ -1010,7 +1027,7 @@
 //UpdateGemPortForPktIn updates gemport for pkt in path to kvstore, path being intfid, onuid, portno, vlan id, priority bit
 func (rsrcMgr *OpenOltResourceMgr) UpdateGemPortForPktIn(ctx context.Context, pktIn PacketInInfoKey, gemPort uint32) {
 
-	path := fmt.Sprintf(OnuPacketINPath, pktIn.IntfID, pktIn.OnuID, pktIn.LogicalPort, pktIn.VlanID, pktIn.Priority)
+	path := fmt.Sprintf(OnuPacketInPath, pktIn.IntfID, pktIn.OnuID, pktIn.LogicalPort, pktIn.VlanID, pktIn.Priority)
 	// update cache
 	rsrcMgr.gemPortForPacketInInfoLock.Lock()
 	rsrcMgr.gemPortForPacketInInfo[path] = gemPort
@@ -1033,7 +1050,7 @@
 
 	var Val []byte
 
-	path := fmt.Sprintf(OnuPacketINPath, packetInInfoKey.IntfID, packetInInfoKey.OnuID, packetInInfoKey.LogicalPort,
+	path := fmt.Sprintf(OnuPacketInPath, packetInInfoKey.IntfID, packetInInfoKey.OnuID, packetInInfoKey.LogicalPort,
 		packetInInfoKey.VlanID, packetInInfoKey.Priority)
 	// get from cache
 	rsrcMgr.gemPortForPacketInInfoLock.RLock()
@@ -1072,7 +1089,7 @@
 
 //DeletePacketInGemPortForOnu deletes the packet-in gemport for ONU
 func (rsrcMgr *OpenOltResourceMgr) DeletePacketInGemPortForOnu(ctx context.Context, intfID uint32, onuID uint32, logicalPort uint32) error {
-	path := fmt.Sprintf(OnuPacketINPathPrefix, intfID, onuID, logicalPort)
+	path := fmt.Sprintf(OnuPacketInPathPrefix, intfID, onuID, logicalPort)
 	value, err := rsrcMgr.KVStore.List(ctx, path)
 	if err != nil {
 		logger.Errorf(ctx, "failed-to-read-value-from-path-%s", path)
@@ -1082,7 +1099,7 @@
 	//remove them one by one
 	for key := range value {
 		// Remove the PathPrefix from the path on KV key.
-		// gemPortForPacketInInfo cache uses OnuPacketINPath as the key
+		// gemPortForPacketInInfo cache uses OnuPacketInPath as the key
 		stringToBeReplaced := rsrcMgr.KVStore.PathPrefix + "/"
 		replacedWith := ""
 		key = strings.Replace(key, stringToBeReplaced, replacedWith, 1)
@@ -1152,7 +1169,11 @@
 	rsrcMgr.flowIDsForGem[gem] = flowIDs
 	rsrcMgr.flowIDsForGemLock.Unlock()
 
+	// If the flowIDs slice is empty delete the key entry from the DB.
 	if flowIDs == nil {
+		if err := rsrcMgr.KVStore.Delete(ctx, path); err != nil {
+			logger.Errorw(ctx, "Failed to delete from kvstore", log.Fields{"err": err, "path": path})
+		}
 		return nil
 	}
 	val, err := json.Marshal(flowIDs)
@@ -1181,6 +1202,24 @@
 	}
 }
 
+//DeleteAllFlowIDsForGemForIntf deletes all the flow ids associated for all the gems on the given pon interface
+func (rsrcMgr *OpenOltResourceMgr) DeleteAllFlowIDsForGemForIntf(ctx context.Context, intfID uint32) error {
+
+	path := fmt.Sprintf(FlowIDsForGemPathPrefix, intfID)
+
+	logger.Debugw(ctx, "delete-flow-ids-for-gem-for-pon-intf", log.Fields{"intfID": intfID})
+	if err := rsrcMgr.KVStore.DeleteWithPrefix(ctx, path); err != nil {
+		logger.Errorf(ctx, "failed-to-remove-resource-%s", path)
+		return err
+	}
+
+	// Reset cache. Normally not necessary as the entire device is getting deleted when this API is invoked.
+	rsrcMgr.flowIDsForGemLock.Lock()
+	rsrcMgr.flowIDsForGem = make(map[uint32][]uint64)
+	rsrcMgr.flowIDsForGemLock.Unlock()
+	return nil
+}
+
 //GetMcastQueuePerInterfaceMap gets multicast queue info per pon interface
 func (rsrcMgr *OpenOltResourceMgr) GetMcastQueuePerInterfaceMap(ctx context.Context) (map[uint32][]uint32, error) {
 	path := McastQueuesForIntf