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

Change-Id: Id8fa8d1a02191b70b66775ae4d637941d969e20e
diff --git a/rw_core/core/device_agent.go b/rw_core/core/device_agent.go
index 6921612..fea66d6 100755
--- a/rw_core/core/device_agent.go
+++ b/rw_core/core/device_agent.go
@@ -257,35 +257,42 @@
 
 	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)
+			updatedFlows = append(updatedFlows, flow) // append existing flows
 		} 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 { // does not exist now
-			updatedGroups = append(updatedGroups, group)
-		} else {
-			groupsToDelete = append(groupsToDelete, group)
+		if fu.FindGroup(newGroups, group.Desc.GroupId) == -1 {
+			updatedGroups = append(updatedGroups, group) // Add existing groups
 		}
 	}
 
 	// Sanity check
-	if (len(updatedFlows) | len(flowsToDelete) | len(updatedGroups) | len(groupsToDelete)) == 0 {
+	if (len(updatedFlows) | len(flowsToDelete) | len(updatedGroups)) == 0 {
 		log.Debugw("nothing-to-update", log.Fields{"deviceId": agent.deviceId, "flows": newFlows, "groups": newGroups})
 		return nil
 	}
@@ -308,12 +315,12 @@
 
 	} else {
 		flowChanges := &ofp.FlowChanges{
-			ToAdd:    &voltha.Flows{Items: newFlows},
+			ToAdd:    &voltha.Flows{Items: flowsToAdd},
 			ToRemove: &voltha.Flows{Items: flowsToDelete},
 		}
 		groupChanges := &ofp.FlowGroupChanges{
-			ToAdd:    &voltha.FlowGroups{Items: newGroups},
-			ToRemove: &voltha.FlowGroups{Items: groupsToDelete},
+			ToAdd:    &voltha.FlowGroups{Items: groupsToAdd},
+			ToRemove: &voltha.FlowGroups{Items: nil},
 			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 2c72b6d..7e35b04 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 flow
+			// Add new flow
 			flow = fu.FlowStatsEntryFromFlowModMessage(mod)
 			flows = append(flows, flow)
 			updatedFlows = append(updatedFlows, flow)
@@ -870,13 +870,12 @@
 				flow.ByteCount = oldFlow.ByteCount
 				flow.PacketCount = oldFlow.PacketCount
 			}
-			if !reflect.DeepEqual(oldFlow, flow) {
-				flows[idx] = flow
-				updatedFlows = append(updatedFlows, flow)
-				changed = true
-				updated = true
-			}
-		} else {
+			// 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
 			flows = append(flows, flow)
 			updatedFlows = append(updatedFlows, flow)
 			changed = true
@@ -903,7 +902,7 @@
 			log.Errorw("db-flow-update-failed", log.Fields{"logicalDeviceId": agent.logicalDeviceId})
 			return err
 		}
-		if !updated {
+		if !updated { // Newly added
 			changedMeterStats := agent.updateFlowCountOfMeterStats(mod, meters, flow)
 			metersToUpdate := &ofp.Meters{}
 			if lDevice.Meters != nil {