Revert "VOL-1847-Add flow only if it does not exist in device,No delete here as delete flows/groups handled now in deleteFlowsAndGroups"

This reverts commit 33f779a44ddacea9ac3312a668ebb75c143f028a.

Change-Id: Id17a9a7d43fb1d894d9fb4aa43268febc9a232f8
diff --git a/rw_core/core/device_agent.go b/rw_core/core/device_agent.go
index d9e8364..bcce15b 100755
--- a/rw_core/core/device_agent.go
+++ b/rw_core/core/device_agent.go
@@ -257,42 +257,35 @@
 
 	var updatedFlows []*ofp.OfpFlowStats
 	var flowsToDelete []*ofp.OfpFlowStats
+	var groupsToDelete []*ofp.OfpGroupEntry
 	var updatedGroups []*ofp.OfpGroupEntry
-	var flowsToAdd []*ofp.OfpFlowStats
-	var groupsToAdd []*ofp.OfpGroupEntry
 
 	// Process flows
 	for _, flow := range newFlows {
 		updatedFlows = append(updatedFlows, flow)
-		if idx := fu.FindFlows(existingFlows.Items, flow); idx == -1 { // does not exist now , add new flow
-			flowsToAdd = append(flowsToAdd, flow)
-		}
 	}
 	for _, flow := range existingFlows.Items {
 		if idx := fu.FindFlows(newFlows, flow); idx == -1 {
-			updatedFlows = append(updatedFlows, flow) // append existing flows
+			updatedFlows = append(updatedFlows, flow)
 		} else {
-			// OF-1.3.1: If a flow entry with identical match fields and priority already resides , clear old flow and add new flow
 			flowsToDelete = append(flowsToDelete, flow)
-			flowsToAdd = append(flowsToAdd, newFlows[idx])
 		}
 	}
 
 	// Process groups
 	for _, g := range newGroups {
 		updatedGroups = append(updatedGroups, g)
-		if fu.FindGroup(existingGroups.Items, g.Desc.GroupId) == -1 { // does not exist now
-			groupsToAdd = append(groupsToAdd, g)
-		}
 	}
 	for _, group := range existingGroups.Items {
-		if fu.FindGroup(newGroups, group.Desc.GroupId) == -1 {
-			updatedGroups = append(updatedGroups, group) // Add existing groups
+		if fu.FindGroup(newGroups, group.Desc.GroupId) == -1 { // does not exist now
+			updatedGroups = append(updatedGroups, group)
+		} else {
+			groupsToDelete = append(groupsToDelete, group)
 		}
 	}
 
 	// Sanity check
-	if (len(updatedFlows) | len(flowsToDelete) | len(updatedGroups)) == 0 {
+	if (len(updatedFlows) | len(flowsToDelete) | len(updatedGroups) | len(groupsToDelete)) == 0 {
 		log.Debugw("nothing-to-update", log.Fields{"deviceId": agent.deviceId, "flows": newFlows, "groups": newGroups})
 		return nil
 	}
@@ -315,12 +308,12 @@
 
 	} else {
 		flowChanges := &ofp.FlowChanges{
-			ToAdd:    &voltha.Flows{Items: flowsToAdd},
+			ToAdd:    &voltha.Flows{Items: newFlows},
 			ToRemove: &voltha.Flows{Items: flowsToDelete},
 		}
 		groupChanges := &ofp.FlowGroupChanges{
-			ToAdd:    &voltha.FlowGroups{Items: groupsToAdd},
-			ToRemove: &voltha.FlowGroups{Items: nil},
+			ToAdd:    &voltha.FlowGroups{Items: newGroups},
+			ToRemove: &voltha.FlowGroups{Items: groupsToDelete},
 			ToUpdate: &voltha.FlowGroups{Items: []*ofp.OfpGroupEntry{}},
 		}
 		go agent.sendIncrementalFlowsToAdapters(device, flowChanges, groupChanges, flowMetadata, chAdapters)
diff --git a/rw_core/core/logical_device_agent.go b/rw_core/core/logical_device_agent.go
index 7e35b04..2c72b6d 100644
--- a/rw_core/core/logical_device_agent.go
+++ b/rw_core/core/logical_device_agent.go
@@ -855,7 +855,7 @@
 			//	TODO:  should this error be notified other than being logged?
 			log.Warnw("overlapped-flows", log.Fields{"logicaldeviceId": agent.logicalDeviceId})
 		} else {
-			// Add new flow
+			//	Add flow
 			flow = fu.FlowStatsEntryFromFlowModMessage(mod)
 			flows = append(flows, flow)
 			updatedFlows = append(updatedFlows, flow)
@@ -870,12 +870,13 @@
 				flow.ByteCount = oldFlow.ByteCount
 				flow.PacketCount = oldFlow.PacketCount
 			}
-			// OF-1.3.1: If a flow entry with identical match fields and priority already resides , clear old flow and add new flow
-			flows[idx] = flow
-			updatedFlows = append(updatedFlows, flow)
-			changed = true
-			updated = true
-		} else { // Add new flow
+			if !reflect.DeepEqual(oldFlow, flow) {
+				flows[idx] = flow
+				updatedFlows = append(updatedFlows, flow)
+				changed = true
+				updated = true
+			}
+		} else {
 			flows = append(flows, flow)
 			updatedFlows = append(updatedFlows, flow)
 			changed = true
@@ -902,7 +903,7 @@
 			log.Errorw("db-flow-update-failed", log.Fields{"logicalDeviceId": agent.logicalDeviceId})
 			return err
 		}
-		if !updated { // Newly added
+		if !updated {
 			changedMeterStats := agent.updateFlowCountOfMeterStats(mod, meters, flow)
 			metersToUpdate := &ofp.Meters{}
 			if lDevice.Meters != nil {