VOL-1043 Allow reuse of onu_ids on all PON ports

Change-Id: I166e95b0a72581f6d7b706bb7ebb9db53f64edb1
diff --git a/voltha/adapters/openolt/openolt_device.py b/voltha/adapters/openolt/openolt_device.py
index 4af9cc6..25beae4 100644
--- a/voltha/adapters/openolt/openolt_device.py
+++ b/voltha/adapters/openolt/openolt_device.py
@@ -356,9 +356,11 @@
 
         # Post ONU Discover alarm  20180809_0805
         try:
-            OnuDiscoveryAlarm(self.alarm_mgr.alarms, pon_id=intf_id, serial_number=serial_number_str).raise_alarm()
+            OnuDiscoveryAlarm(self.alarm_mgr.alarms, pon_id=intf_id,
+                              serial_number=serial_number_str).raise_alarm()
         except Exception as disc_alarm_error:
-            self.log.exception("onu-discovery-alarm-error", errmsg=disc_alarm_error.message)
+            self.log.exception("onu-discovery-alarm-error",
+                               errmsg=disc_alarm_error.message)
             # continue for now.
 
         pir = self.bw_mgr.pir(serial_number_str)
@@ -539,11 +541,13 @@
 
                 # tcont creation (onu)
                 tcont = TcontsConfigData()
-                tcont.alloc_id = platform.mk_alloc_id(onu_indication.onu_id)
+                tcont.alloc_id = platform.mk_alloc_id(
+                    onu_indication.intf_id, onu_indication.onu_id)
 
                 # gem port creation
                 gem_port = GemportsConfigData()
                 gem_port.gemport_id = platform.mk_gemport_id(
+                    onu_indication.intf_id,
                     onu_indication.onu_id)
 
                 # ports creation/update
@@ -588,11 +592,13 @@
 
                 # tcont creation (onu)
                 tcont = TcontsConfigData()
-                tcont.alloc_id = platform.mk_alloc_id(onu_indication.onu_id)
+                tcont.alloc_id = platform.mk_alloc_id(
+                    onu_indication.intf_id, onu_indication.onu_id)
 
                 # gem port creation
                 gem_port = GemportsConfigData()
                 gem_port.gemport_id = platform.mk_gemport_id(
+                    onu_indication.intf_id,
                     onu_indication.onu_id)
                 gem_port.tcont_ref = str(tcont.alloc_id)
 
@@ -842,18 +848,17 @@
 
 
     def new_onu_id(self, intf_id):
-        onu_id = None
         onu_devices = self.adapter_agent.get_child_devices(self.device_id)
-        for i in range(1, 512):
-            id_not_taken = True
-            for child_device in onu_devices:
-                if child_device.proxy_address.onu_id == i:
-                    id_not_taken = False
-                    break
-            if id_not_taken:
-                onu_id = i
-                break
-        return onu_id
+        pon_onu_ids = [onu_device.proxy_address.onu_id for onu_device in
+                              onu_devices if
+                           onu_device.proxy_address.channel_id == intf_id]
+        for i in range(1, platform.MAX_ONUS_PER_PON):
+            if i not in pon_onu_ids:
+                return i
+
+        self.log.error('All available onu_ids taken on this pon',
+                       intf_id=intf_id, ids_taken=platform.MAX_ONUS_PER_PON)
+        return None
 
     def stringify_vendor_specific(self, vendor_specific):
         return ''.join(str(i) for i in [
diff --git a/voltha/adapters/openolt/openolt_flow_mgr.py b/voltha/adapters/openolt/openolt_flow_mgr.py
index ae388e0..e72c929 100644
--- a/voltha/adapters/openolt/openolt_flow_mgr.py
+++ b/voltha/adapters/openolt/openolt_flow_mgr.py
@@ -268,7 +268,7 @@
     def add_hsia_flow(self, intf_id, onu_id, classifier, action,
                       direction, hsia_id, logical_flow):
 
-        gemport_id = platform.mk_gemport_id(onu_id)
+        gemport_id = platform.mk_gemport_id(intf_id, onu_id)
         flow_id = platform.mk_flow_id(intf_id, onu_id, hsia_id)
 
         flow = openolt_pb2.Flow(
@@ -290,7 +290,7 @@
         classifier['pkt_tag_type'] = 'single_tag'
         classifier.pop('vlan_vid', None)
 
-        gemport_id = platform.mk_gemport_id(onu_id)
+        gemport_id = platform.mk_gemport_id(intf_id, onu_id)
         flow_id = platform.mk_flow_id(intf_id, onu_id, DHCP_FLOW_INDEX)
 
         upstream_flow = openolt_pb2.Flow(
@@ -351,7 +351,7 @@
         uplink_action = {}
         uplink_action['trap_to_host'] = True
 
-        gemport_id = platform.mk_gemport_id(onu_id)
+        gemport_id = platform.mk_gemport_id(intf_id, onu_id)
 
         # Add Upstream EAPOL Flow.
 
diff --git a/voltha/adapters/openolt/openolt_platform.py b/voltha/adapters/openolt/openolt_platform.py
index c2a9b5e..c4b7cac 100644
--- a/voltha/adapters/openolt/openolt_platform.py
+++ b/voltha/adapters/openolt/openolt_platform.py
@@ -93,23 +93,23 @@
 
 """
 
+MAX_ONUS_PER_PON = 112
 
 def mk_uni_port_num(intf_id, onu_id):
     return intf_id << 11 | onu_id << 4
 
 
-def mk_alloc_id(onu_id, idx=0):
+def mk_alloc_id(intf_id, onu_id, idx=0):
     # FIXME - driver should do prefixing 1 << 10 as it is Maple specific
     # return 1<<10 | onu_id<<6 | idx
-    return 1023 + onu_id  # FIXME
+    return 1023 + intf_id * MAX_ONUS_PER_PON + onu_id  # FIXME
 
 
-def mk_gemport_id(onu_id, idx=0):
-    return 1 << 10 | onu_id << 3 | idx
-
+def mk_gemport_id(intf_id, onu_id, idx=0):
+    return 1024 + (((MAX_ONUS_PER_PON * intf_id + onu_id - 1) * 7) + idx)
 
 def onu_id_from_gemport_id(gemport_id):
-    return (gemport_id & ~(1 << 10)) >> 3
+    return (((gemport_id - 1024) // 7) % MAX_ONUS_PER_PON) + 1
 
 
 def mk_flow_id(intf_id, onu_id, idx):