VOL-4427: Clear stale keys from etcd

Change-Id: I5e09248ab8f6be48aa164d1e8025cb2be2b379dd
diff --git a/go.mod b/go.mod
index 2eb0e11..44bdffd 100644
--- a/go.mod
+++ b/go.mod
@@ -13,7 +13,7 @@
 	github.com/gogo/protobuf v1.3.2
 	github.com/golang/protobuf v1.5.2
 	github.com/grpc-ecosystem/go-grpc-middleware v1.3.0
-	github.com/opencord/voltha-lib-go/v7 v7.0.4
+	github.com/opencord/voltha-lib-go/v7 v7.0.6
 	github.com/opencord/voltha-protos/v5 v5.0.0
 	go.etcd.io/etcd v3.3.25+incompatible
 	google.golang.org/grpc v1.41.0
diff --git a/go.sum b/go.sum
index 08d95cd..2a4c3ae 100644
--- a/go.sum
+++ b/go.sum
@@ -185,8 +185,8 @@
 github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo=
 github.com/onsi/gomega v1.14.0 h1:ep6kpPVwmr/nTbklSx2nrLNSIO62DoYAhnPNIMhK8gI=
 github.com/onsi/gomega v1.14.0/go.mod h1:cIuvLEne0aoVhAgh/O6ac0Op8WWw9H6eYCriF+tEHG0=
-github.com/opencord/voltha-lib-go/v7 v7.0.4 h1:nVVRkEZyfEkGYewfgmO3NzIAIVdm8G/vVyEaCUwYW6g=
-github.com/opencord/voltha-lib-go/v7 v7.0.4/go.mod h1:iZueJRS4XJ3rpm3iy0Zdnhz1lG5bWx2pZoPormwgUKk=
+github.com/opencord/voltha-lib-go/v7 v7.0.6 h1:IeHIxB+jovITYTEwFExC7ZClrkO2bY1Ki37Yig8y2Lc=
+github.com/opencord/voltha-lib-go/v7 v7.0.6/go.mod h1:iZueJRS4XJ3rpm3iy0Zdnhz1lG5bWx2pZoPormwgUKk=
 github.com/opencord/voltha-protos/v5 v5.0.0 h1:US2k7qYPMnOueOCrprq9LjuMT3wK9uyxPwAVwjMmKhc=
 github.com/opencord/voltha-protos/v5 v5.0.0/go.mod h1:uVKXQB499Ir6G+rc47dSThNja1S4Vy3h9JLSDuJGmzI=
 github.com/opentracing/opentracing-go v1.1.0/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o=
diff --git a/vendor/github.com/opencord/voltha-lib-go/v7/pkg/ponresourcemanager/ponresourcemanager.go b/vendor/github.com/opencord/voltha-lib-go/v7/pkg/ponresourcemanager/ponresourcemanager.go
index ba67aeb..ad6c111 100644
--- a/vendor/github.com/opencord/voltha-lib-go/v7/pkg/ponresourcemanager/ponresourcemanager.go
+++ b/vendor/github.com/opencord/voltha-lib-go/v7/pkg/ponresourcemanager/ponresourcemanager.go
@@ -1132,15 +1132,23 @@
 	var Value []byte
 	var err error
 	Path := fmt.Sprintf(ALLOC_ID_RESOURCE_MAP_PATH, PONRMgr.DeviceID, IntfONUID)
-	Value, err = json.Marshal(AllocIDs)
-	if err != nil {
-		logger.Error(ctx, "failed to Marshal")
-		return err
-	}
+	if AllocIDs == nil {
+		// No more alloc ids associated with the key. Delete the key entirely
+		if err = PONRMgr.KVStore.Delete(ctx, Path); err != nil {
+			logger.Errorf(ctx, "Failed to delete key %s", Path)
+			return err
+		}
+	} else {
+		Value, err = json.Marshal(AllocIDs)
+		if err != nil {
+			logger.Error(ctx, "failed to Marshal")
+			return err
+		}
 
-	if err = PONRMgr.KVStore.Put(ctx, Path, Value); err != nil {
-		logger.Errorf(ctx, "Failed to update resource %s", Path)
-		return err
+		if err = PONRMgr.KVStore.Put(ctx, Path, Value); err != nil {
+			logger.Errorf(ctx, "Failed to update resource %s", Path)
+			return err
+		}
 	}
 	return err
 }
@@ -1156,15 +1164,23 @@
 	var err error
 	Path := fmt.Sprintf(GEMPORT_ID_RESOURCE_MAP_PATH, PONRMgr.DeviceID, IntfONUID)
 	logger.Debugf(ctx, "Updating gemport ids for %s", Path)
-	Value, err = json.Marshal(GEMPortIDs)
-	if err != nil {
-		logger.Error(ctx, "failed to Marshal")
-		return err
-	}
+	if GEMPortIDs == nil {
+		// No more gemport ids associated with the key. Delete the key entirely
+		if err = PONRMgr.KVStore.Delete(ctx, Path); err != nil {
+			logger.Errorf(ctx, "Failed to delete key %s", Path)
+			return err
+		}
+	} else {
+		Value, err = json.Marshal(GEMPortIDs)
+		if err != nil {
+			logger.Error(ctx, "failed to Marshal")
+			return err
+		}
 
-	if err = PONRMgr.KVStore.Put(ctx, Path, Value); err != nil {
-		logger.Errorf(ctx, "Failed to update resource %s", Path)
-		return err
+		if err = PONRMgr.KVStore.Put(ctx, Path, Value); err != nil {
+			logger.Errorf(ctx, "Failed to update resource %s", Path)
+			return err
+		}
 	}
 	return err
 }
@@ -1257,12 +1273,10 @@
 	*/
 	ByteArray, err := ToByte(Resource[POOL])
 	if err != nil {
-		logger.Error(ctx, "Failed to convert resource to byte array")
 		return 0, err
 	}
 	Data := bitmap.TSFromData(ByteArray, false)
 	if Data == nil {
-		logger.Error(ctx, "Failed to get data from byte array")
 		return 0, errors.New("Failed to get data from byte array")
 	}
 
@@ -1273,6 +1287,9 @@
 			break
 		}
 	}
+	if Idx == Len {
+		return 0, errors.New("resource-exhausted--no-free-id-in-the-pool")
+	}
 	Data.Set(Idx, true)
 	res := uint32(Resource[START_IDX].(float64))
 	Resource[POOL] = Data.Data(false)
@@ -1297,6 +1314,10 @@
 		return false
 	}
 	Idx := Id - uint32(Resource[START_IDX].(float64))
+	if Idx >= uint32(Data.Len()) {
+		logger.Errorf(ctx, "ID %d is out of the boundaries of the pool", Id)
+		return false
+	}
 	Data.Set(int(Idx), false)
 	Resource[POOL] = Data.Data(false)
 
@@ -1314,6 +1335,10 @@
 		return false
 	}
 	Idx := Id - StartIndex
+	if Idx >= uint32(Data.Len()) {
+		logger.Errorf(ctx, "Reservation failed. ID %d is out of the boundaries of the pool", Id)
+		return false
+	}
 	Data.Set(int(Idx), true)
 	return true
 }
diff --git a/vendor/modules.txt b/vendor/modules.txt
index db38b89..8ee392d 100644
--- a/vendor/modules.txt
+++ b/vendor/modules.txt
@@ -114,7 +114,7 @@
 github.com/klauspost/compress/huff0
 github.com/klauspost/compress/zstd
 github.com/klauspost/compress/zstd/internal/xxhash
-# github.com/opencord/voltha-lib-go/v7 v7.0.4
+# github.com/opencord/voltha-lib-go/v7 v7.0.6
 ## explicit
 github.com/opencord/voltha-lib-go/v7/pkg/config
 github.com/opencord/voltha-lib-go/v7/pkg/db