[VOL-3767] Allow the removal of a specific device in DeviceRules

Change-Id: If282dbf3dd4067e9556a4799b3d9430574867c9e
diff --git a/pkg/flows/flow_utils.go b/pkg/flows/flow_utils.go
index 41b615a..1e50a63 100644
--- a/pkg/flows/flow_utils.go
+++ b/pkg/flows/flow_utils.go
@@ -1378,6 +1378,12 @@
 	return filteredDR
 }
 
+func (dr *DeviceRules) RemoveRule(deviceId string) {
+	dr.rulesLock.RLock()
+	defer dr.rulesLock.RUnlock()
+	delete(dr.Rules, deviceId)
+}
+
 func (dr *DeviceRules) AddFlow(deviceId string, flow *ofp.OfpFlowStats) {
 	dr.rulesLock.Lock()
 	defer dr.rulesLock.Unlock()
diff --git a/pkg/flows/flow_utils_test.go b/pkg/flows/flow_utils_test.go
index 05a041a..4947c16 100644
--- a/pkg/flows/flow_utils_test.go
+++ b/pkg/flows/flow_utils_test.go
@@ -314,6 +314,63 @@
 	assert.Equal(t, group.Desc.GroupId, allGroups[0].Desc.GroupId)
 }
 
+func TestDeviceRules_FilterRules(t *testing.T) {
+	dr := NewDeviceRules()
+	rules := dr.GetRules()
+	assert.True(t, len(rules) == 0)
+
+	dr.AddFlow("1", nil)
+	dr.AddFlow("2", nil)
+	dr.AddFlow("3", nil)
+	dr.AddFlow("4", nil)
+	rules = dr.GetRules()
+	assert.True(t, len(rules) == 4)
+
+	keep := map[string]string{"1": "1", "3": "3", "5": "5"}
+	result := dr.FilterRules(keep)
+	rules = result.GetRules()
+	assert.True(t, len(rules) == 2)
+
+	_, ok := rules["1"]
+	assert.True(t, ok)
+	_, ok = rules["2"]
+	assert.False(t, ok)
+	_, ok = rules["3"]
+	assert.True(t, ok)
+	_, ok = rules["4"]
+	assert.False(t, ok)
+}
+
+func TestDeviceRules_RemoveRule(t *testing.T) {
+	dr := NewDeviceRules()
+	rules := dr.GetRules()
+	assert.True(t, len(rules) == 0)
+
+	dr.AddFlow("1", nil)
+	dr.AddFlow("2", nil)
+	dr.AddFlow("3", nil)
+	dr.AddFlow("4", nil)
+	rules = dr.GetRules()
+	assert.True(t, len(rules) == 4)
+
+	dr.RemoveRule("3")
+	rules = dr.GetRules()
+	assert.True(t, len(rules) == 3)
+
+	_, ok := rules["1"]
+	assert.True(t, ok)
+	_, ok = rules["2"]
+	assert.True(t, ok)
+	_, ok = rules["3"]
+	assert.False(t, ok)
+	_, ok = rules["4"]
+	assert.True(t, ok)
+
+	dr.RemoveRule("5")
+	rules = dr.GetRules()
+	assert.True(t, len(rules) == 3)
+}
+
 func TestDeviceRules_AddFlow(t *testing.T) {
 	dr := NewDeviceRules()
 	rules := dr.GetRules()