VOL-1123 Specify serial number for Ponsim v2 ONU on command line

Change-Id: I85a15c0e710f22d2869fbe47976036cfa1ee610d
diff --git a/ponsim/v2/README.md b/ponsim/v2/README.md
index 88841c7..9d004be 100644
--- a/ponsim/v2/README.md
+++ b/ponsim/v2/README.md
@@ -50,6 +50,8 @@
     	Enable promiscuous mode on network interfaces
   -quiet
     	Suppress debug and info logs
+  -serial_number string
+      Serial number of ONU device (default "PSMO12345678")
   -vcore_endpoint string
     	Voltha core endpoint address (default "vcore")
   -verbose
diff --git a/ponsim/v2/core/ponsim_onu.go b/ponsim/v2/core/ponsim_onu.go
index ac3a02a..ae40387 100644
--- a/ponsim/v2/core/ponsim_onu.go
+++ b/ponsim/v2/core/ponsim_onu.go
@@ -43,6 +43,7 @@
 	ParentAddress string
 	ParentPort    int32
 	AssignedPort  int32
+	SerialNumber  string
 	Conn          *grpc.ClientConn
 
 	oltClient ponsim.PonSimCommonClient
@@ -214,6 +215,7 @@
 				Id:      uuid.New().String(),
 				Address: common.GetInterfaceIP(o.InternalIf),
 				Port:    o.Port,
+				SerialNumber: o.SerialNumber,
 			}
 			common.Logger().Printf("Request details %+v\n", rreq)
 
diff --git a/ponsim/v2/grpc/nbi/ponsim_handler.go b/ponsim/v2/grpc/nbi/ponsim_handler.go
index 8205386..49fd71a 100644
--- a/ponsim/v2/grpc/nbi/ponsim_handler.go
+++ b/ponsim/v2/grpc/nbi/ponsim_handler.go
@@ -128,25 +128,28 @@
 		"handler": handler,
 	}).Info("Getting device information")
 
-	var out *voltha.PonSimDeviceInfo
+	out := &voltha.PonSimDeviceInfo{}
 
 	// Check which device type we're currently handling
 	if _, ok := (handler.device).(*core.PonSimOltDevice); ok {
 		common.Logger().WithFields(logrus.Fields{
 			"handler": handler,
 		}).Debug("Handling OLT device")
-		keys := make([]int32, 0, len((handler.device).(*core.PonSimOltDevice).GetOnus()))
-		for k := range (handler.device).(*core.PonSimOltDevice).GetOnus() {
-			keys = append(keys, k)
+		onus := (handler.device).(*core.PonSimOltDevice).GetOnus()
+		for k := range onus {
+			out.Onus = append(
+				out.Onus,
+				&voltha.PonSimOnuDeviceInfo {
+					UniPort: k,
+					SerialNumber: onus[k].Device.SerialNumber,
+				},
+			)
 		}
-		out = &voltha.PonSimDeviceInfo{NniPort: 0, UniPorts: []int32(keys)}
-
+		out.NniPort = 0
 	} else {
 		common.Logger().WithFields(logrus.Fields{
 			"handler": handler,
 		}).Debug("Handling ONU/OTHER device")
-
-		out = &voltha.PonSimDeviceInfo{}
 	}
 
 	common.Logger().WithFields(logrus.Fields{
diff --git a/ponsim/v2/grpc/sbi/olt_handler.go b/ponsim/v2/grpc/sbi/olt_handler.go
index a264532..2f98c96 100644
--- a/ponsim/v2/grpc/sbi/olt_handler.go
+++ b/ponsim/v2/grpc/sbi/olt_handler.go
@@ -48,6 +48,7 @@
 		PonSimDevice: core.PonSimDevice{
 			Address: request.Address, Port: request.Port, //GrpcSecurity: h.olt.GrpcSecurity,
 		}}
+	onu.SerialNumber = request.SerialNumber
 
 	if assignedPort, err := h.olt.AddOnu(onu); assignedPort == -1 || err != nil {
 		return &ponsim.RegistrationReply{
diff --git a/ponsim/v2/ponsim.go b/ponsim/v2/ponsim.go
index eab6a54..cbb863d 100644
--- a/ponsim/v2/ponsim.go
+++ b/ponsim/v2/ponsim.go
@@ -49,6 +49,7 @@
 	default_parent_port    = 50060
 	default_vcore_endpoint = "vcore"
 	default_fluentd_host   = ""
+	default_serial_number  = "PSMO12345678"
 
 	default_snapshot_len = 65535
 	default_promiscuous  = false
@@ -82,6 +83,7 @@
 	parent_port    int    = default_parent_port
 	vcore_endpoint string = default_vcore_endpoint
 	fluentd_host   string = default_fluentd_host
+	serial_number  string = default_serial_number
 
 	snapshot_len int32 = default_snapshot_len
 	promiscuous  bool  = default_promiscuous
@@ -158,6 +160,9 @@
 	help = fmt.Sprintf("Fluentd host address")
 	flag.StringVar(&fluentd_host, "fluentd", default_fluentd_host, help)
 
+	help = fmt.Sprintf("Serial number of ONU device")
+	flag.StringVar(&serial_number, "serial_number", default_serial_number, help)
+
 	flag.Parse()
 }
 
@@ -274,6 +279,7 @@
 		device = core.NewPonSimOnuDevice(pon)
 		device.(*core.PonSimOnuDevice).ParentAddress = parent_addr
 		device.(*core.PonSimOnuDevice).ParentPort = int32(parent_port)
+		device.(*core.PonSimOnuDevice).SerialNumber = serial_number
 
 	default:
 		log.Println("Unknown device type")
diff --git a/ponsim/v2/protos/ponsim_olt.proto b/ponsim/v2/protos/ponsim_olt.proto
index 82214a7..3b8a761 100644
--- a/ponsim/v2/protos/ponsim_olt.proto
+++ b/ponsim/v2/protos/ponsim_olt.proto
@@ -12,6 +12,7 @@
     string id = 1;
     string address = 2;
     int32 port = 3;
+    string serial_number = 4;
 }
 
 message RegistrationReply {
diff --git a/voltha/adapters/ponsim_olt/ponsim_olt.py b/voltha/adapters/ponsim_olt/ponsim_olt.py
index 92a5a0a..64357df 100644
--- a/voltha/adapters/ponsim_olt/ponsim_olt.py
+++ b/voltha/adapters/ponsim_olt/ponsim_olt.py
@@ -494,9 +494,9 @@
         self.adapter_agent.update_device(device)
         self.logical_device_id = ld_initialized.id
 
-        # register ONUS per uni port
-        for port_no in info.uni_ports:
-            vlan_id = port_no
+        # register ONUS
+        for onu in info.onus:
+            vlan_id = onu.uni_port
             self.adapter_agent.child_device_detected(
                 parent_device_id=device.id,
                 parent_port_no=1,
@@ -506,7 +506,8 @@
                     channel_id=vlan_id
                 ),
                 admin_state=AdminState.ENABLED,
-                vlan=vlan_id
+                vlan=vlan_id,
+                serial_number=onu.serial_number
             )
 
         if self.ponsim_comm == 'grpc':
diff --git a/voltha/adapters/ponsim_onu/ponsim_onu.py b/voltha/adapters/ponsim_onu/ponsim_onu.py
index 7f770e1..332845b 100644
--- a/voltha/adapters/ponsim_onu/ponsim_onu.py
+++ b/voltha/adapters/ponsim_onu/ponsim_onu.py
@@ -206,7 +206,7 @@
         self.adapter_agent.register_for_proxied_messages(device.proxy_address)
 
         # populate device info
-        device.root = True
+        device.root = False
         device.vendor = 'ponsim'
         device.model = 'n/a'
         device.connect_status = ConnectStatus.REACHABLE
@@ -247,7 +247,7 @@
             ofp_port=ofp_port(
                 port_no=port_no,
                 hw_addr=mac_str_to_tuple('00:00:00:00:00:%02x' % port_no),
-                name='uni-{}'.format(port_no),
+                name=device.serial_number,
                 config=0,
                 state=OFPPS_LIVE,
                 curr=cap,
@@ -457,7 +457,7 @@
                 ofp_port=ofp_port(
                     port_no=port_no,
                     hw_addr=mac_str_to_tuple('00:00:00:00:00:%02x' % port_no),
-                    name='uni-{}'.format(port_no),
+                    name=device.serial_number,
                     config=0,
                     state=OFPPS_LIVE,
                     curr=cap,
diff --git a/voltha/protos/ponsim.proto b/voltha/protos/ponsim.proto
index 91018ad..bdf4394 100644
--- a/voltha/protos/ponsim.proto
+++ b/voltha/protos/ponsim.proto
@@ -13,9 +13,14 @@
 import "bbf_fiber_tcont_body.proto";
 import "bbf_fiber_traffic_descriptor_profile_body.proto";
 
+message PonSimOnuDeviceInfo {
+    int32 uni_port = 1;
+    string serial_number = 2;
+}
+
 message PonSimDeviceInfo {
     int32 nni_port = 1;
-    repeated int32 uni_ports = 2;
+    repeated PonSimOnuDeviceInfo onus = 2;
 }
 
 message FlowTable {