[VOL-1524] Use port labels as port IDs

This commit consists of the following:
1) Update the simulated OLT and ONU adapters to set the port label
using the port number
2) Update the ponsim OLT and ONU adapters to set the port label
using the port number.
3) Update the logic to verify whether a logical port exist using
the port label.
4) Update the logical device lock when adding a port to the logical
device to prevent two requests to add the same port from
occuring in parallel.

Change-Id: Ia5a732db1f3fc6e559acd7e70ae9659113b6fa9f
diff --git a/adapters/simulated_olt/adaptercore/device_handler.go b/adapters/simulated_olt/adaptercore/device_handler.go
index b83f265..e7a86ac 100644
--- a/adapters/simulated_olt/adaptercore/device_handler.go
+++ b/adapters/simulated_olt/adaptercore/device_handler.go
@@ -17,6 +17,7 @@
 
 import (
 	"context"
+	"fmt"
 	"github.com/gogo/protobuf/proto"
 	com "github.com/opencord/voltha-go/adapters/common"
 	"github.com/opencord/voltha-go/common/log"
@@ -107,7 +108,7 @@
 	//	Now create the NNI Port
 	dh.nniPort = &voltha.Port{
 		PortNo:     2,
-		Label:      "NNI facing Ethernet port",
+		Label:      fmt.Sprintf("nni-%d", 2),
 		Type:       voltha.Port_ETHERNET_NNI,
 		OperStatus: voltha.OperStatus_ACTIVE,
 	}
@@ -120,7 +121,7 @@
 	//	Now create the PON Port
 	dh.ponPort = &voltha.Port{
 		PortNo:     1,
-		Label:      "PON port",
+		Label:      fmt.Sprintf("pon-%d", 1),
 		Type:       voltha.Port_PON_OLT,
 		OperStatus: voltha.OperStatus_ACTIVE,
 	}
diff --git a/adapters/simulated_onu/adaptercore/device_handler.go b/adapters/simulated_onu/adaptercore/device_handler.go
index 4404643..aad0c86 100644
--- a/adapters/simulated_onu/adaptercore/device_handler.go
+++ b/adapters/simulated_onu/adaptercore/device_handler.go
@@ -17,6 +17,7 @@
 
 import (
 	"context"
+	"fmt"
 	"github.com/gogo/protobuf/proto"
 	com "github.com/opencord/voltha-go/adapters/common"
 	"github.com/opencord/voltha-go/common/log"
@@ -104,10 +105,18 @@
 		log.Errorw("error-updating-device", log.Fields{"deviceId": device.Id, "error": err})
 	}
 
-	//	Now create the NNI Port
+	// Use the channel Id, assigned by the parent device to me, as the port number
+	uni_port := uint32(2)
+	if device.ProxyAddress != nil {
+		if device.ProxyAddress.ChannelId != 0 {
+			uni_port = device.ProxyAddress.ChannelId
+		}
+	}
+
+	//	Now create the UNI Port
 	dh.uniPort = &voltha.Port{
-		PortNo:     2,
-		Label:      "UNI facing Ethernet port",
+		PortNo:     uni_port,
+		Label:      fmt.Sprintf("uni-%d", uni_port),
 		Type:       voltha.Port_ETHERNET_UNI,
 		AdminState: voltha.AdminState_ENABLED,
 		OperStatus: voltha.OperStatus_ACTIVE,
@@ -121,7 +130,7 @@
 	//	Now create the PON Port
 	dh.ponPort = &voltha.Port{
 		PortNo:     1,
-		Label:      "PON port",
+		Label:      fmt.Sprintf("pon-%d", 1),
 		Type:       voltha.Port_PON_ONU,
 		AdminState: voltha.AdminState_ENABLED,
 		OperStatus: voltha.OperStatus_ACTIVE,
diff --git a/python/adapters/ponsim_olt/ponsim_olt.py b/python/adapters/ponsim_olt/ponsim_olt.py
index fc617a3..84e303c 100644
--- a/python/adapters/ponsim_olt/ponsim_olt.py
+++ b/python/adapters/ponsim_olt/ponsim_olt.py
@@ -272,7 +272,7 @@
 
             nni_port = Port(
                 port_no=info.nni_port,
-                label='NNI facing Ethernet port',
+                label='nni-'+ str(info.nni_port),
                 type=Port.ETHERNET_NNI,
                 oper_status=OperStatus.ACTIVE
             )
@@ -280,7 +280,7 @@
             yield self.core_proxy.port_created(device.id, nni_port)
             yield self.core_proxy.port_created(device.id, Port(
                 port_no=1,
-                label='PON port',
+                label='pon-1',
                 type=Port.PON_OLT,
                 oper_status=OperStatus.ACTIVE
             ))
diff --git a/python/adapters/ponsim_onu/ponsim_onu.py b/python/adapters/ponsim_onu/ponsim_onu.py
index a10c235..e0592b4 100644
--- a/python/adapters/ponsim_onu/ponsim_onu.py
+++ b/python/adapters/ponsim_onu/ponsim_onu.py
@@ -209,17 +209,23 @@
         log.info("initial-pm-config", pm_config=pm_config)
         yield self.core_proxy.device_pm_config_update(pm_config, init=True)
 
+        # Use the channel Id, assigned by the parent device to me, as the port number
+        uni_port = 2
+        if device.proxy_address is not None:
+            if device.proxy_address.channel_id != 0:
+                uni_port =  device.proxy_address.channel_id
+
         # register physical ports
         self.uni_port = Port(
-            port_no=2,
-            label='UNI facing Ethernet port',
+            port_no=uni_port,
+            label="uni-" + str(uni_port),
             type=Port.ETHERNET_UNI,
             admin_state=AdminState.ENABLED,
             oper_status=OperStatus.ACTIVE
         )
         self.pon_port = Port(
             port_no=1,
-            label='PON port',
+            label='pon-1',
             type=Port.PON_ONU,
             admin_state=AdminState.ENABLED,
             oper_status=OperStatus.ACTIVE,
diff --git a/rw_core/core/logical_device_agent.go b/rw_core/core/logical_device_agent.go
index e4e855b..03edb57 100644
--- a/rw_core/core/logical_device_agent.go
+++ b/rw_core/core/logical_device_agent.go
@@ -1243,6 +1243,8 @@
 
 func (agent *LogicalDeviceAgent) addNNILogicalPort (device *voltha.Device, port *voltha.Port)  error {
 	log.Infow("addNNILogicalPort", log.Fields{"NNI": port})
+	agent.lockLogicalDevice.Lock()
+	defer agent.lockLogicalDevice.Unlock()
 	if agent.portExist(device, port) {
 		log.Debugw("port-already-exist", log.Fields{"port": port})
 		return nil
@@ -1262,8 +1264,6 @@
 	lp.OfpPort.Name = lp.Id
 	lp.DevicePortNo = port.PortNo
 
-	agent.lockLogicalDevice.Lock()
-	defer agent.lockLogicalDevice.Unlock()
 	var ld *voltha.LogicalDevice
 	if ld, err = agent.getLogicalDeviceWithoutLock(); err != nil {
 		log.Errorw("error-retrieving-logical-device", log.Fields{"error": err})
@@ -1283,14 +1283,9 @@
 }
 
 func (agent *LogicalDeviceAgent) portExist (device *voltha.Device, port *voltha.Port) bool {
-	if ldevice, _ := agent.GetLogicalDevice(); ldevice != nil {
+	if ldevice, _ := agent.getLogicalDeviceWithoutLock(); ldevice != nil {
 		for _, lPort := range ldevice.Ports {
-			if lPort.DeviceId == device.Id && lPort.DevicePortNo == port.PortNo {
-				if lPort.OfpPort != nil && device.ProxyAddress != nil {
-					return lPort.OfpPort.PortNo == device.ProxyAddress.ChannelId
-				} else if lPort.OfpPort != nil || device.ProxyAddress != nil {
-					return false
-				}
+			if lPort.DeviceId == device.Id && lPort.DevicePortNo == port.PortNo && lPort.Id == port.Label {
 				return true
 			}
 		}
@@ -1300,6 +1295,8 @@
 
 func (agent *LogicalDeviceAgent) addUNILogicalPort (childDevice *voltha.Device, port *voltha.Port)  error {
 	log.Debugw("addUNILogicalPort", log.Fields{"port": port})
+	agent.lockLogicalDevice.Lock()
+	defer agent.lockLogicalDevice.Unlock()
 	if agent.portExist(childDevice, port) {
 		log.Debugw("port-already-exist", log.Fields{"port": port})
 		return nil
@@ -1311,8 +1308,8 @@
 		log.Errorw("error-retrieving-port-capabilities", log.Fields{"error": err})
 		return err
 	}
-	agent.lockLogicalDevice.Lock()
-	defer agent.lockLogicalDevice.Unlock()
+	//agent.lockLogicalDevice.Lock()
+	//defer agent.lockLogicalDevice.Unlock()
 	// Get stored logical device
 	if ldevice, err := agent.getLogicalDeviceWithoutLock(); err != nil {
 		return status.Error(codes.NotFound, agent.logicalDeviceId)