VOL-4427: Clear stale keys from etcd

Change-Id: I62416032338d8b4ba57150ad80ce6905bf1e84ea
diff --git a/.gitreview b/.gitreview
index bf04ac9..7769d63 100644
--- a/.gitreview
+++ b/.gitreview
@@ -3,3 +3,4 @@
 port=29418
 project=voltha-lib-go.git
 defaultremote=origin
+defaultbranch=voltha-2.8
diff --git a/VERSION b/VERSION
index 4d5e528..0043336 100644
--- a/VERSION
+++ b/VERSION
@@ -1 +1 @@
-5.0.6
\ No newline at end of file
+5.0.7
diff --git a/pkg/ponresourcemanager/ponresourcemanager.go b/pkg/ponresourcemanager/ponresourcemanager.go
index a8738ee..23bc6c7 100755
--- a/pkg/ponresourcemanager/ponresourcemanager.go
+++ b/pkg/ponresourcemanager/ponresourcemanager.go
@@ -1138,15 +1138,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
 }
@@ -1162,15 +1170,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
 }