[VOL-3731] Delete etcd stale data after olt reboot

Change-Id: Ib6669511327870d180262e1f8c2bd6c1e8315496
diff --git a/internal/pkg/resourcemanager/resourcemanager.go b/internal/pkg/resourcemanager/resourcemanager.go
index 0f2a65e..805cffe 100755
--- a/internal/pkg/resourcemanager/resourcemanager.go
+++ b/internal/pkg/resourcemanager/resourcemanager.go
@@ -1076,9 +1076,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)
-
 	value, err := rsrcMgr.KVStore.List(ctx, path)
 	if err != nil {
 		logger.Errorf(ctx, "failed-to-read-value-from-path-%s", path)
@@ -1087,8 +1085,9 @@
 
 	//remove them one by one
 	for key := range value {
-		// Formulate the right key path suffix ti be delete
-		stringToBeReplaced := fmt.Sprintf(BasePathKvStore, rsrcMgr.KVStore.PathPrefix, rsrcMgr.DeviceID) + "/"
+		// Remove the PathPrefix from the path on KV key.
+		// gemPortForPacketInInfo cache uses OnuPacketINPath as the key
+		stringToBeReplaced := rsrcMgr.KVStore.PathPrefix + "/"
 		replacedWith := ""
 		key = strings.Replace(key, stringToBeReplaced, replacedWith, 1)
 		// update cache
@@ -1096,11 +1095,13 @@
 		delete(rsrcMgr.gemPortForPacketInInfo, key)
 		rsrcMgr.gemPortForPacketInInfoLock.Unlock()
 
-		logger.Debugf(ctx, "removing-key-%s", key)
-		if err := rsrcMgr.KVStore.Delete(ctx, key); err != nil {
-			logger.Errorf(ctx, "failed-to-remove-resource-%s", key)
-			return err
-		}
+		logger.Debugw(ctx, "removed-key-from-packetin-gem-port-cache", log.Fields{"key": key, "cache-len": len(rsrcMgr.gemPortForPacketInInfo)})
+	}
+
+	logger.Debugw(ctx, "delete-packetin-gem-port", log.Fields{"realPath": path})
+	if err := rsrcMgr.KVStore.DeleteWithPrefix(ctx, path); err != nil {
+		logger.Errorf(ctx, "failed-to-remove-resource-%s", path)
+		return err
 	}
 
 	return nil