[VOL-1788] Fix logical port update

This commit ensures the latest port information is sent to the
controller instead of the older state.

Change-Id: I1214c2b58083175aa16e1a3fdc40e7f750b8f093
diff --git a/rw_core/core/logical_device_agent.go b/rw_core/core/logical_device_agent.go
index 2d6bb13..53faec8 100644
--- a/rw_core/core/logical_device_agent.go
+++ b/rw_core/core/logical_device_agent.go
@@ -1236,10 +1236,8 @@
 	deletedPorts = make([]*voltha.LogicalPort, 0)
 	for _, o := range oldList {
 		found := false
-		changed := false
 		for _, n := range newList {
 			if o.Id == n.Id {
-				changed = !reflect.DeepEqual(o, n)
 				found = true
 				break
 			}
@@ -1247,14 +1245,13 @@
 		if !found {
 			deletedPorts = append(deletedPorts, o)
 		}
-		if changed {
-			changedPorts = append(changedPorts, o)
-		}
 	}
 	for _, n := range newList {
 		found := false
+		changed := false
 		for _, o := range oldList {
 			if o.Id == n.Id {
+				changed = !reflect.DeepEqual(o, n)
 				found = true
 				break
 			}
@@ -1262,6 +1259,9 @@
 		if !found {
 			newPorts = append(newPorts, n)
 		}
+		if changed {
+			changedPorts = append(changedPorts, n)
+		}
 	}
 	return
 }
diff --git a/rw_core/core/logical_device_agent_test.go b/rw_core/core/logical_device_agent_test.go
new file mode 100644
index 0000000..d629af0
--- /dev/null
+++ b/rw_core/core/logical_device_agent_test.go
@@ -0,0 +1,345 @@
+/*
+ * Copyright 2019-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package core
+
+import (
+	ofp "github.com/opencord/voltha-protos/go/openflow_13"
+	"github.com/opencord/voltha-protos/go/voltha"
+	"github.com/stretchr/testify/assert"
+	"testing"
+)
+
+func TestLogicalDeviceAgent_diff_nochange_1(t *testing.T) {
+	currentLogicalPorts := []*voltha.LogicalPort{}
+	updatedLogicalPorts := []*voltha.LogicalPort{}
+	newPorts, changedPorts, deletedPorts := diff(currentLogicalPorts, updatedLogicalPorts)
+	assert.Equal(t, 0, len(newPorts))
+	assert.Equal(t, 0, len(changedPorts))
+	assert.Equal(t, 0, len(deletedPorts))
+}
+
+func TestLogicalDeviceAgent_diff_nochange_2(t *testing.T) {
+	currentLogicalPorts := []*voltha.LogicalPort{
+		{
+			Id:           "1231",
+			DeviceId:     "d1234",
+			DevicePortNo: 1,
+			RootPort:     true,
+			OfpPort: &ofp.OfpPort{
+				PortNo: 1,
+				Name:   "port1",
+				Config: 1,
+				State:  1,
+			},
+		},
+		{
+			Id:           "1232",
+			DeviceId:     "d1234",
+			DevicePortNo: 2,
+			RootPort:     false,
+			OfpPort: &ofp.OfpPort{
+				PortNo: 2,
+				Name:   "port2",
+				Config: 1,
+				State:  1,
+			},
+		},
+		{
+			Id:           "1233",
+			DeviceId:     "d1234",
+			DevicePortNo: 3,
+			RootPort:     false,
+			OfpPort: &ofp.OfpPort{
+				PortNo: 3,
+				Name:   "port3",
+				Config: 1,
+				State:  1,
+			},
+		},
+	}
+	updatedLogicalPorts := []*voltha.LogicalPort{
+		{
+			Id:           "1231",
+			DeviceId:     "d1234",
+			DevicePortNo: 1,
+			RootPort:     true,
+			OfpPort: &ofp.OfpPort{
+				PortNo: 1,
+				Name:   "port1",
+				Config: 1,
+				State:  1,
+			},
+		},
+		{
+			Id:           "1232",
+			DeviceId:     "d1234",
+			DevicePortNo: 2,
+			RootPort:     false,
+			OfpPort: &ofp.OfpPort{
+				PortNo: 2,
+				Name:   "port2",
+				Config: 1,
+				State:  1,
+			},
+		},
+		{
+			Id:           "1233",
+			DeviceId:     "d1234",
+			DevicePortNo: 3,
+			RootPort:     false,
+			OfpPort: &ofp.OfpPort{
+				PortNo: 3,
+				Name:   "port3",
+				Config: 1,
+				State:  1,
+			},
+		},
+	}
+	newPorts, changedPorts, deletedPorts := diff(currentLogicalPorts, updatedLogicalPorts)
+	assert.Equal(t, 0, len(newPorts))
+	assert.Equal(t, 0, len(changedPorts))
+	assert.Equal(t, 0, len(deletedPorts))
+}
+
+func TestLogicalDeviceAgent_diff_add(t *testing.T) {
+	currentLogicalPorts := []*voltha.LogicalPort{}
+	updatedLogicalPorts := []*voltha.LogicalPort{
+		{
+			Id:           "1231",
+			DeviceId:     "d1234",
+			DevicePortNo: 1,
+			RootPort:     true,
+			OfpPort: &ofp.OfpPort{
+				PortNo: 1,
+				Name:   "port1",
+				Config: 1,
+				State:  1,
+			},
+		},
+		{
+			Id:           "1232",
+			DeviceId:     "d1234",
+			DevicePortNo: 2,
+			RootPort:     true,
+			OfpPort: &ofp.OfpPort{
+				PortNo: 2,
+				Name:   "port2",
+				Config: 1,
+				State:  1,
+			},
+		},
+	}
+	newPorts, changedPorts, deletedPorts := diff(currentLogicalPorts, updatedLogicalPorts)
+	assert.Equal(t, 2, len(newPorts))
+	assert.Equal(t, 0, len(changedPorts))
+	assert.Equal(t, 0, len(deletedPorts))
+	assert.Equal(t, updatedLogicalPorts[0], newPorts[0])
+	assert.Equal(t, updatedLogicalPorts[1], newPorts[1])
+}
+
+func TestLogicalDeviceAgent_diff_delete(t *testing.T) {
+	currentLogicalPorts := []*voltha.LogicalPort{
+		{
+			Id:           "1231",
+			DeviceId:     "d1234",
+			DevicePortNo: 1,
+			RootPort:     true,
+			OfpPort: &ofp.OfpPort{
+				PortNo: 1,
+				Name:   "port1",
+				Config: 1,
+				State:  1,
+			},
+		},
+	}
+	updatedLogicalPorts := []*voltha.LogicalPort{}
+	newPorts, changedPorts, deletedPorts := diff(currentLogicalPorts, updatedLogicalPorts)
+	assert.Equal(t, 0, len(newPorts))
+	assert.Equal(t, 0, len(changedPorts))
+	assert.Equal(t, 1, len(deletedPorts))
+	assert.Equal(t, currentLogicalPorts[0], deletedPorts[0])
+}
+
+func TestLogicalDeviceAgent_diff_changed(t *testing.T) {
+	currentLogicalPorts := []*voltha.LogicalPort{
+		{
+			Id:           "1231",
+			DeviceId:     "d1234",
+			DevicePortNo: 1,
+			RootPort:     true,
+			OfpPort: &ofp.OfpPort{
+				PortNo: 1,
+				Name:   "port1",
+				Config: 1,
+				State:  1,
+			},
+		},
+		{
+			Id:           "1232",
+			DeviceId:     "d1234",
+			DevicePortNo: 2,
+			RootPort:     false,
+			OfpPort: &ofp.OfpPort{
+				PortNo: 2,
+				Name:   "port2",
+				Config: 1,
+				State:  1,
+			},
+		},
+		{
+			Id:           "1233",
+			DeviceId:     "d1234",
+			DevicePortNo: 3,
+			RootPort:     false,
+			OfpPort: &ofp.OfpPort{
+				PortNo: 3,
+				Name:   "port3",
+				Config: 1,
+				State:  1,
+			},
+		},
+	}
+	updatedLogicalPorts := []*voltha.LogicalPort{
+		{
+			Id:           "1231",
+			DeviceId:     "d1234",
+			DevicePortNo: 1,
+			RootPort:     true,
+			OfpPort: &ofp.OfpPort{
+				PortNo: 1,
+				Name:   "port1",
+				Config: 4,
+				State:  4,
+			},
+		},
+		{
+			Id:           "1232",
+			DeviceId:     "d1234",
+			DevicePortNo: 2,
+			RootPort:     false,
+			OfpPort: &ofp.OfpPort{
+				PortNo: 2,
+				Name:   "port2",
+				Config: 4,
+				State:  4,
+			},
+		},
+		{
+			Id:           "1233",
+			DeviceId:     "d1234",
+			DevicePortNo: 3,
+			RootPort:     false,
+			OfpPort: &ofp.OfpPort{
+				PortNo: 3,
+				Name:   "port3",
+				Config: 1,
+				State:  1,
+			},
+		},
+	}
+	newPorts, changedPorts, deletedPorts := diff(currentLogicalPorts, updatedLogicalPorts)
+	assert.Equal(t, 0, len(newPorts))
+	assert.Equal(t, 2, len(changedPorts))
+	assert.Equal(t, 0, len(deletedPorts))
+	assert.Equal(t, updatedLogicalPorts[0], changedPorts[0])
+	assert.Equal(t, updatedLogicalPorts[1], changedPorts[1])
+}
+
+func TestLogicalDeviceAgent_diff_mix(t *testing.T) {
+	currentLogicalPorts := []*voltha.LogicalPort{
+		{
+			Id:           "1231",
+			DeviceId:     "d1234",
+			DevicePortNo: 1,
+			RootPort:     true,
+			OfpPort: &ofp.OfpPort{
+				PortNo: 1,
+				Name:   "port1",
+				Config: 1,
+				State:  1,
+			},
+		},
+		{
+			Id:           "1232",
+			DeviceId:     "d1234",
+			DevicePortNo: 2,
+			RootPort:     false,
+			OfpPort: &ofp.OfpPort{
+				PortNo: 2,
+				Name:   "port2",
+				Config: 1,
+				State:  1,
+			},
+		},
+		{
+			Id:           "1233",
+			DeviceId:     "d1234",
+			DevicePortNo: 3,
+			RootPort:     false,
+			OfpPort: &ofp.OfpPort{
+				PortNo: 3,
+				Name:   "port3",
+				Config: 1,
+				State:  1,
+			},
+		},
+	}
+	updatedLogicalPorts := []*voltha.LogicalPort{
+		{
+			Id:           "1231",
+			DeviceId:     "d1234",
+			DevicePortNo: 1,
+			RootPort:     true,
+			OfpPort: &ofp.OfpPort{
+				PortNo: 1,
+				Name:   "port1",
+				Config: 4,
+				State:  4,
+			},
+		},
+		{
+			Id:           "1232",
+			DeviceId:     "d1234",
+			DevicePortNo: 2,
+			RootPort:     false,
+			OfpPort: &ofp.OfpPort{
+				PortNo: 2,
+				Name:   "port2",
+				Config: 4,
+				State:  4,
+			},
+		},
+		{
+			Id:           "1234",
+			DeviceId:     "d1234",
+			DevicePortNo: 4,
+			RootPort:     false,
+			OfpPort: &ofp.OfpPort{
+				PortNo: 4,
+				Name:   "port4",
+				Config: 4,
+				State:  4,
+			},
+		},
+	}
+	newPorts, changedPorts, deletedPorts := diff(currentLogicalPorts, updatedLogicalPorts)
+	assert.Equal(t, 1, len(newPorts))
+	assert.Equal(t, 2, len(changedPorts))
+	assert.Equal(t, 1, len(deletedPorts))
+	assert.Equal(t, updatedLogicalPorts[0], changedPorts[0])
+	assert.Equal(t, updatedLogicalPorts[1], changedPorts[1])
+	assert.Equal(t, currentLogicalPorts[2], deletedPorts[0])
+}